blob: 7ab66263e59b714294b2685bd9107a8f5c3000e6 [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 Chartier3d21bdf2015-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 Chartier3d21bdf2015-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 Chartier3d21bdf2015-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,
309 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700310 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800311 const char* class_filter,
312 const char* method_filter,
313 bool list_classes,
314 bool list_methods,
315 const char* export_dex_location,
316 uint32_t addr2instr)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700317 : dump_raw_mapping_table_(dump_raw_mapping_table),
318 dump_raw_gc_map_(dump_raw_gc_map),
319 dump_vmap_(dump_vmap),
320 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700321 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800322 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000323 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800324 list_classes_(list_classes),
325 list_methods_(list_methods),
326 export_dex_location_(export_dex_location),
327 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800328 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700329
330 const bool dump_raw_mapping_table_;
331 const bool dump_raw_gc_map_;
332 const bool dump_vmap_;
333 const bool disassemble_code_;
334 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800335 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000336 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800337 const bool list_classes_;
338 const bool list_methods_;
339 const char* const export_dex_location_;
340 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700341 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700342};
343
Elliott Hughese3c845c2012-02-28 17:23:01 -0800344class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700345 public:
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800346 explicit OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000347 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800348 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700349 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800350 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800351 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
352 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800353 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000354 oat_file.Begin(),
355 true /* can_read_litals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800356 CHECK(options_.class_loader_ != nullptr);
357 CHECK(options_.class_filter_ != nullptr);
358 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800359 AddAllOffsets();
360 }
361
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700362 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700363 delete disassembler_;
364 }
365
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800366 InstructionSet GetInstructionSet() {
367 return instruction_set_;
368 }
369
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700370 bool Dump(std::ostream& os) {
371 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800372 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700373
374 os << "MAGIC:\n";
375 os << oat_header.GetMagic() << "\n\n";
376
377 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800378 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700379
Elliott Hughesa72ec822012-03-05 17:12:22 -0800380 os << "INSTRUCTION SET:\n";
381 os << oat_header.GetInstructionSet() << "\n\n";
382
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700383 {
384 std::unique_ptr<const InstructionSetFeatures> features(
385 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
386 oat_header.GetInstructionSetFeaturesBitmap()));
387 os << "INSTRUCTION SET FEATURES:\n";
388 os << features->GetFeatureString() << "\n\n";
389 }
Dave Allison70202782013-10-22 17:52:19 -0700390
Brian Carlstromaded5f72011-10-07 17:15:04 -0700391 os << "DEX FILE COUNT:\n";
392 os << oat_header.GetDexFileCount() << "\n\n";
393
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800394#define DUMP_OAT_HEADER_OFFSET(label, offset) \
395 os << label " OFFSET:\n"; \
396 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800397 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800398 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
399 } \
400 os << StringPrintf("\n\n");
401
402 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
403 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
404 GetInterpreterToInterpreterBridgeOffset);
405 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
406 GetInterpreterToCompiledCodeBridgeOffset);
407 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
408 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800409 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
410 GetQuickGenericJniTrampolineOffset);
411 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
412 GetQuickImtConflictTrampolineOffset);
413 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
414 GetQuickResolutionTrampolineOffset);
415 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
416 GetQuickToInterpreterBridgeOffset);
417#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700418
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700419 os << "IMAGE PATCH DELTA:\n";
420 os << StringPrintf("%d (0x%08x)\n\n",
421 oat_header.GetImagePatchDelta(),
422 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700423
Brian Carlstrom28db0122012-10-18 16:20:41 -0700424 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
425 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
426
427 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800428 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700429
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700430 // Print the key-value store.
431 {
432 os << "KEY VALUE STORE:\n";
433 size_t index = 0;
434 const char* key;
435 const char* value;
436 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
437 os << key << " = " << value << "\n";
438 index++;
439 }
440 os << "\n";
441 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700442
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800443 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700444 os << "BEGIN:\n";
445 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700446
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700447 os << "END:\n";
448 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
449 }
450
451 os << "SIZE:\n";
452 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700453
454 os << std::flush;
455
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800456 // If set, adjust relative address to be searched
457 if (options_.addr2instr_ != 0) {
458 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
459 os << "SEARCH ADDRESS (executable offset + input):\n";
460 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
461 }
462
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800463 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
464 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700465 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800466
467 // If file export selected skip file analysis
468 if (options_.export_dex_location_) {
469 if (!ExportDexFile(os, *oat_dex_file)) {
470 success = false;
471 }
472 } else {
473 if (!DumpOatDexFile(os, *oat_dex_file)) {
474 success = false;
475 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700476 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700477 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700478 os << std::flush;
479 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700480 }
481
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800482 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700483 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
484 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800485 return 0; // Address not in oat file
486 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800487 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
488 reinterpret_cast<uintptr_t>(oat_file_.Begin());
489 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800490 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800491 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800492 return end_offset - begin_offset;
493 }
494
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800495 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700496 return oat_file_.GetOatHeader().GetInstructionSet();
497 }
498
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -0700499 const void* GetQuickOatCode(ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800500 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
501 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700502 CHECK(oat_dex_file != nullptr);
503 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700504 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700505 if (dex_file.get() == nullptr) {
506 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
507 << "': " << error_msg;
508 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800509 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700510 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800511 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700512 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700513 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100514 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800515 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100516 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800517 }
518 }
519 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700520 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800521 }
522
Brian Carlstromaded5f72011-10-07 17:15:04 -0700523 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800524 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800525 // We don't know the length of the code for each method, but we need to know where to stop
526 // when disassembling. What we do know is that a region of code will be followed by some other
527 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
528 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800529 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
530 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700531 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700532 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700533 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700534 if (dex_file.get() == nullptr) {
535 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
536 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800537 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800538 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800539 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800540 for (size_t class_def_index = 0;
541 class_def_index < dex_file->NumClassDefs();
542 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800543 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100544 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700545 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700546 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800547 ClassDataItemIterator it(*dex_file, class_data);
548 SkipAllFields(it);
549 uint32_t class_method_index = 0;
550 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100551 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800552 it.Next();
553 }
554 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100555 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800556 it.Next();
557 }
558 }
559 }
560 }
561
562 // If the last thing in the file is code for a method, there won't be an offset for the "next"
563 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
564 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800565 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800566 }
567
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700568 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
569 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
570 }
571
Elliott Hughese3c845c2012-02-28 17:23:01 -0800572 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800573 uint32_t code_offset = oat_method.GetCodeOffset();
574 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
575 code_offset &= ~0x1;
576 }
577 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800578 offsets_.insert(oat_method.GetMappingTableOffset());
579 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800580 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800581 }
582
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700583 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
584 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800585 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700586 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800587 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800588 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700589
590 // Create the verifier early.
591
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700592 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700593 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700594 if (dex_file.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700595 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700596 os << std::flush;
597 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700598 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800599 for (size_t class_def_index = 0;
600 class_def_index < dex_file->NumClassDefs();
601 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700602 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
603 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800604
605 // TODO: Support regex
606 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
607 continue;
608 }
609
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700610 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100611 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700612 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
613 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100614 << " (" << oat_class.GetStatus() << ")"
615 << " (" << oat_class.GetType() << ")\n";
616 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800617 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
618 std::ostream indented_os(&indent_filter);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800619 if (options_.list_classes_) continue;
620 if (!DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700621 success = false;
622 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800623 if (stop_analysis) {
624 os << std::flush;
625 return success;
626 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700627 }
628
629 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700630 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700631 }
632
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800633 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
634 std::string error_msg;
635 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
636
637 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
638 if (dex_file == nullptr) {
639 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
640 return false;
641 }
642 size_t fsize = oat_dex_file.FileSize();
643
644 // Some quick checks just in case
645 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
646 os << "Invalid dex file\n";
647 return false;
648 }
649
650 // Verify output directory exists
651 if (!OS::DirectoryExists(options_.export_dex_location_)) {
652 // TODO: Extend OS::DirectoryExists if symlink support is required
653 os << options_.export_dex_location_ << " output directory not found or symlink\n";
654 return false;
655 }
656
657 // Beautify path names
658 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
659 return false;
660 }
661
662 std::string dex_orig_name;
663 size_t dex_orig_pos = dex_file_location.rfind('/');
664 if (dex_orig_pos == std::string::npos)
665 dex_orig_name = dex_file_location;
666 else
667 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
668
669 // A more elegant approach to efficiently name user installed apps is welcome
670 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
671 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
672 size_t apk_orig_pos = dex_file_location.rfind('/');
673 if (apk_orig_pos != std::string::npos) {
674 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
675 }
676 }
677
678 std::string out_dex_path(options_.export_dex_location_);
679 if (out_dex_path.back() != '/') {
680 out_dex_path.append("/");
681 }
682 out_dex_path.append(dex_orig_name);
683 out_dex_path.append("_export.dex");
684 if (out_dex_path.length() > PATH_MAX) {
685 return false;
686 }
687
688 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
689 if (file.get() == nullptr) {
690 os << "Failed to open output dex file " << out_dex_path;
691 return false;
692 }
693
694 if (!file->WriteFully(dex_file->Begin(), fsize)) {
695 os << "Failed to write dex file";
696 file->Erase();
697 return false;
698 }
699
700 if (file->FlushCloseOrErase() != 0) {
701 os << "Flush and close failed";
702 return false;
703 }
704
705 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
706 os << std::flush;
707
708 return true;
709 }
710
Elliott Hughese3c845c2012-02-28 17:23:01 -0800711 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700712 while (it.HasNextStaticField()) {
713 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700714 }
Ian Rogers0571d352011-11-03 19:51:38 -0700715 while (it.HasNextInstanceField()) {
716 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700717 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800718 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700719
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700720 bool DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800721 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700722 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800723 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700724 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700725 if (class_data == nullptr) { // empty class such as a marker interface?
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700726 os << std::flush;
727 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800728 }
729 ClassDataItemIterator it(dex_file, class_data);
730 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700731 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700732 while (it.HasNextDirectMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700733 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
734 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800735 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700736 success = false;
737 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800738 if (addr_found) {
739 *stop_analysis = true;
740 return success;
741 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700742 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700743 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700744 }
Ian Rogers0571d352011-11-03 19:51:38 -0700745 while (it.HasNextVirtualMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700746 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
747 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800748 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700749 success = false;
750 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800751 if (addr_found) {
752 *stop_analysis = true;
753 return success;
754 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700755 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700756 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700757 }
Ian Rogers0571d352011-11-03 19:51:38 -0700758 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700759 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700760 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700761 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800762
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700763 static constexpr uint32_t kPrologueBytes = 16;
764
765 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
766 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
767
768 bool DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700769 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700770 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800771 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800772 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700773 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800774
775 // TODO: Support regex
776 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
777 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000778 return success;
779 }
780
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800781 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800782 os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000783 class_method_index, pretty_method.c_str(),
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700784 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800785 if (options_.list_methods_) return success;
786
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800787 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700788 std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter));
789 Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count);
790 std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800791
792 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
793 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
794 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
795 uint32_t code_offset = oat_method.GetCodeOffset();
796 uint32_t code_size = oat_method.GetQuickCodeSize();
797 if (resolved_addr2instr_ != 0) {
798 if (resolved_addr2instr_ > code_offset + code_size) {
799 return success;
800 } else {
801 *addr_found = true; // stop analyzing file at next iteration
802 }
803 }
804
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800805 {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700806 *indent1_os << "DEX CODE:\n";
807 DumpDexCode(*indent2_os, dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700808 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700809
810 std::unique_ptr<verifier::MethodVerifier> verifier;
811 if (Runtime::Current() != nullptr) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700812 *indent1_os << "VERIFIER TYPE ANALYSIS:\n";
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700813 verifier.reset(DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item,
814 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700815 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800816 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700817 *indent1_os << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800818 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700819 *indent1_os << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100820 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700821 *indent1_os << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
822 if (oat_method_offsets_offset > oat_file_.Size()) {
823 *indent1_os << StringPrintf(
824 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
825 oat_method_offsets_offset, oat_file_.Size());
826 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
827 os << std::flush;
828 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800829 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700830
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700831 *indent2_os << StringPrintf("code_offset: 0x%08x ", code_offset);
832 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
833 if (aligned_code_begin > oat_file_.Size()) {
834 *indent2_os << StringPrintf("WARNING: "
835 "code offset 0x%08x is past end of file 0x%08zx.\n",
836 aligned_code_begin, oat_file_.Size());
837 success = false;
838 }
839 *indent2_os << "\n";
840
841 *indent2_os << "gc_map: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800842 if (options_.absolute_addresses_) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800843 *indent2_os << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700844 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800845 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700846 *indent2_os << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
847 if (gc_map_offset > oat_file_.Size()) {
848 *indent2_os << StringPrintf("WARNING: "
849 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
850 gc_map_offset, oat_file_.Size());
851 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800852 } else if (options_.dump_raw_gc_map_) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700853 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800854 std::ostream indent3_os(&indent3_filter);
855 DumpGcMap(indent3_os, oat_method, code_item);
856 }
857 }
858 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700859 *indent1_os << "OatQuickMethodHeader ";
860 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
861 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700862
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800863 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700864 *indent1_os << StringPrintf("%p ", method_header);
865 }
866 *indent1_os << StringPrintf("(offset=0x%08x)\n", method_header_offset);
867 if (method_header_offset > oat_file_.Size()) {
868 *indent1_os << StringPrintf(
869 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
870 method_header_offset, oat_file_.Size());
871 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
872 os << std::flush;
873 return false;
874 }
875
876 *indent2_os << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800877 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700878 *indent2_os << StringPrintf("%p ", oat_method.GetMappingTable());
879 }
880 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
881 *indent2_os << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
882 if (mapping_table_offset > oat_file_.Size()) {
883 *indent2_os << StringPrintf("WARNING: "
884 "mapping table offset 0x%08x is past end of file 0x%08zx. "
885 "mapping table offset was loaded from offset 0x%08x.\n",
886 mapping_table_offset, oat_file_.Size(),
887 oat_method.GetMappingTableOffsetOffset());
888 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800889 } else if (options_.dump_raw_mapping_table_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700890 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
891 std::ostream indent3_os(&indent3_filter);
892 DumpMappingTable(indent3_os, oat_method);
893 }
894
895 *indent2_os << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800896 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700897 *indent2_os << StringPrintf("%p ", oat_method.GetVmapTable());
898 }
899 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
900 *indent2_os << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
901 if (vmap_table_offset > oat_file_.Size()) {
902 *indent2_os << StringPrintf("WARNING: "
903 "vmap table offset 0x%08x is past end of file 0x%08zx. "
904 "vmap table offset was loaded from offset 0x%08x.\n",
905 vmap_table_offset, oat_file_.Size(),
906 oat_method.GetVmapTableOffsetOffset());
907 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800908 } else if (options_.dump_vmap_) {
Roland Levillain442b46a2015-02-18 16:54:21 +0000909 DumpVmapData(*indent2_os, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700910 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800911 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700912 {
913 *indent1_os << "QuickMethodFrameInfo\n";
914
915 *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
916 *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
917 DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false);
918 *indent2_os << "\n";
919 *indent2_os << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
920 DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true);
921 *indent2_os << "\n";
922 }
923 {
924 // Based on spill masks from QuickMethodFrameInfo so placed
925 // after it is dumped, but useful for understanding quick
926 // code, so dumped here.
927 DumpVregLocations(*indent2_os, oat_method, code_item);
928 }
929 {
930 *indent1_os << "CODE: ";
931 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
932 if (code_size_offset > oat_file_.Size()) {
933 *indent2_os << StringPrintf("WARNING: "
934 "code size offset 0x%08x is past end of file 0x%08zx.",
935 code_size_offset, oat_file_.Size());
936 success = false;
937 } else {
938 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700939 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
940 uint64_t aligned_code_end = aligned_code_begin + code_size;
941
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800942 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700943 *indent1_os << StringPrintf("%p ", code);
944 }
945 *indent1_os << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
946 code_offset,
947 code_size_offset,
948 code_size,
949 code != nullptr ? "..." : "");
950
951 if (aligned_code_begin > oat_file_.Size()) {
952 *indent2_os << StringPrintf("WARNING: "
953 "start of code at 0x%08x is past end of file 0x%08zx.",
954 aligned_code_begin, oat_file_.Size());
955 success = false;
956 } else if (aligned_code_end > oat_file_.Size()) {
957 *indent2_os << StringPrintf("WARNING: "
958 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
959 "code size is 0x%08x loaded from offset 0x%08x.\n",
960 aligned_code_end, oat_file_.Size(),
961 code_size, code_size_offset);
962 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800963 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700964 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
965 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
966 }
967 }
968 } else if (code_size > kMaxCodeSize) {
969 *indent2_os << StringPrintf("WARNING: "
970 "code size %d is bigger than max expected threshold of %d. "
971 "code size is 0x%08x loaded from offset 0x%08x.\n",
972 code_size, kMaxCodeSize,
973 code_size, code_size_offset);
974 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800975 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700976 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
977 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
978 }
979 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800980 } else if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700981 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, !success, 0);
982 }
983 }
984 }
985 os << std::flush;
986 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700987 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800988
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800989 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
990 if (spill_mask == 0) {
991 return;
992 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800993 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800994 for (size_t i = 0; i < 32; i++) {
995 if ((spill_mask & (1 << i)) != 0) {
996 if (is_float) {
997 os << "fr" << i;
998 } else {
999 os << "r" << i;
1000 }
1001 spill_mask ^= 1 << i; // clear bit
1002 if (spill_mask != 0) {
1003 os << ", ";
1004 } else {
1005 break;
1006 }
1007 }
1008 }
1009 os << ")";
1010 }
1011
Roland Levillain442b46a2015-02-18 16:54:21 +00001012 // Display data stored at the the vmap offset of an oat method.
1013 void DumpVmapData(std::ostream& os,
1014 const OatFile::OatMethod& oat_method,
1015 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001016 if (oat_method.GetGcMap() == nullptr) {
Roland Levillain442b46a2015-02-18 16:54:21 +00001017 // If the native GC map is null, then this method has been
1018 // compiled with the optimizing compiler. The optimizing
1019 // compiler currently outputs its stack maps in the vmap table.
1020 const void* raw_code_info = oat_method.GetVmapTable();
1021 if (raw_code_info != nullptr) {
1022 CodeInfo code_info(raw_code_info);
1023 DCHECK(code_item != nullptr);
1024 DumpCodeInfo(os, code_info, *code_item);
1025 }
1026 } else {
1027 // Otherwise, display the vmap table.
1028 const uint8_t* raw_table = oat_method.GetVmapTable();
1029 if (raw_table != nullptr) {
1030 VmapTable vmap_table(raw_table);
1031 DumpVmapTable(os, oat_method, vmap_table);
1032 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001033 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001034 }
1035
1036 // Display a CodeInfo object emitted by the optimizing compiler.
1037 void DumpCodeInfo(std::ostream& os,
1038 const CodeInfo& code_info,
1039 const DexFile::CodeItem& code_item) {
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001040 code_info.Dump(os, code_item.registers_size_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001041 }
1042
Roland Levillain442b46a2015-02-18 16:54:21 +00001043 // Display a vmap table.
1044 void DumpVmapTable(std::ostream& os,
1045 const OatFile::OatMethod& oat_method,
1046 const VmapTable& vmap_table) {
1047 bool first = true;
1048 bool processing_fp = false;
1049 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1050 for (size_t i = 0; i < vmap_table.Size(); i++) {
1051 uint16_t dex_reg = vmap_table[i];
1052 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1053 processing_fp ? kFloatVReg : kIntVReg);
1054 os << (first ? "v" : ", v") << dex_reg;
1055 if (!processing_fp) {
1056 os << "/r" << cpu_reg;
1057 } else {
1058 os << "/fr" << cpu_reg;
1059 }
1060 first = false;
1061 if (!processing_fp && dex_reg == 0xFFFF) {
1062 processing_fp = true;
1063 spill_mask = oat_method.GetFpSpillMask();
1064 }
1065 }
1066 os << "\n";
1067 }
1068
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001069 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1070 const DexFile::CodeItem* code_item) {
1071 if (code_item != nullptr) {
1072 size_t num_locals_ins = code_item->registers_size_;
1073 size_t num_ins = code_item->ins_size_;
1074 size_t num_locals = num_locals_ins - num_ins;
1075 size_t num_outs = code_item->outs_size_;
1076
1077 os << "vr_stack_locations:";
1078 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1079 // For readability, delimit the different kinds of VRs.
1080 if (reg == num_locals_ins) {
1081 os << "\n\tmethod*:";
1082 } else if (reg == num_locals && num_ins > 0) {
1083 os << "\n\tins:";
1084 } else if (reg == 0 && num_locals > 0) {
1085 os << "\n\tlocals:";
1086 }
1087
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001088 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1089 code_item,
1090 oat_method.GetCoreSpillMask(),
1091 oat_method.GetFpSpillMask(),
1092 oat_method.GetFrameSizeInBytes(),
1093 reg,
1094 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001095 os << " v" << reg << "[sp + #" << offset << "]";
1096 }
1097
1098 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1099 if (out_reg == 0) {
1100 os << "\n\touts:";
1101 }
1102
1103 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1104 os << " v" << out_reg << "[sp + #" << offset << "]";
1105 }
1106
1107 os << "\n";
1108 }
1109 }
1110
Ian Rogersb23a7722012-10-09 16:54:26 -07001111 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001112 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001113 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001114 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001115 const VmapTable vmap_table(raw_table);
1116 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001117 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001118 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1119 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1120 : oat_method.GetCoreSpillMask();
1121 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001122 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001123 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1124 code_item,
1125 oat_method.GetCoreSpillMask(),
1126 oat_method.GetFpSpillMask(),
1127 oat_method.GetFrameSizeInBytes(),
1128 reg,
1129 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001130 os << "[sp + #" << offset << "]";
1131 }
1132 }
1133 }
1134
Ian Rogersef7d42f2014-01-06 12:55:46 -08001135 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1136 const DexFile::CodeItem* code_item,
1137 size_t num_regs, const uint8_t* reg_bitmap) {
1138 bool first = true;
1139 for (size_t reg = 0; reg < num_regs; reg++) {
1140 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1141 if (first) {
1142 os << " v" << reg << " (";
1143 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1144 os << ")";
1145 first = false;
1146 } else {
1147 os << ", v" << reg << " (";
1148 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1149 os << ")";
1150 }
1151 }
1152 }
1153 if (first) {
1154 os << "No registers in GC map\n";
1155 } else {
1156 os << "\n";
1157 }
1158 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001159 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1160 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001161 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001162 if (gc_map_raw == nullptr) {
1163 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001164 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001165 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001166 NativePcOffsetToReferenceMap map(gc_map_raw);
1167 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1168 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1169 map.GetNativePcOffset(entry);
1170 os << StringPrintf("%p", native_pc);
1171 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001172 }
1173 }
1174
1175 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001176 const void* quick_code = oat_method.GetQuickCode();
1177 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001178 return;
1179 }
Ian Rogers1809a722013-08-09 22:05:32 -07001180 MappingTable table(oat_method.GetMappingTable());
1181 if (table.TotalSize() != 0) {
1182 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1183 std::ostream indent_os(&indent_filter);
1184 if (table.PcToDexSize() != 0) {
1185 typedef MappingTable::PcToDexIterator It;
1186 os << "suspend point mappings {\n";
1187 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1188 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1189 }
1190 os << "}\n";
1191 }
1192 if (table.DexToPcSize() != 0) {
1193 typedef MappingTable::DexToPcIterator It;
1194 os << "catch entry mappings {\n";
1195 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1196 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1197 }
1198 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001199 }
1200 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001201 }
1202
Ian Rogers1809a722013-08-09 22:05:32 -07001203 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1204 size_t offset, bool suspend_point_mapping) {
1205 MappingTable table(oat_method.GetMappingTable());
1206 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1207 typedef MappingTable::PcToDexIterator It;
1208 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1209 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001210 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001211 return cur.DexPc();
1212 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001213 }
Ian Rogers1809a722013-08-09 22:05:32 -07001214 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1215 typedef MappingTable::DexToPcIterator It;
1216 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1217 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001218 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001219 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001220 }
1221 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001222 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001223 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001224 }
1225
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001226 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1227 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001228 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001229 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001230 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001231 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001232 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001233 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001234 bool first = true;
1235 for (size_t reg = 0; reg < num_regs; reg++) {
1236 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1237 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001238 os << "GC map objects: v" << reg << " (";
1239 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001240 os << ")";
1241 first = false;
1242 } else {
1243 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001244 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001245 os << ")";
1246 }
1247 }
1248 }
1249 if (!first) {
1250 os << "\n";
1251 }
1252 }
1253 }
1254 }
1255
Mathieu Chartier590fee92013-09-13 13:46:47 -07001256 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1257 const OatFile::OatMethod& oat_method,
1258 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1259 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001260 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001261 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001262 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1263 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1264 if (kind != kUndefined) {
1265 if (first) {
1266 os << "VRegs: v";
1267 first = false;
1268 } else {
1269 os << ", v";
1270 }
1271 os << reg << " (";
1272 switch (kind) {
1273 case kImpreciseConstant:
1274 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1275 DescribeVReg(os, oat_method, code_item, reg, kind);
1276 break;
1277 case kConstant:
1278 os << "Constant: " << kinds.at((reg * 2) + 1);
1279 break;
1280 default:
1281 DescribeVReg(os, oat_method, code_item, reg, kind);
1282 break;
1283 }
1284 os << ")";
1285 }
1286 }
1287 if (!first) {
1288 os << "\n";
1289 }
1290 }
1291
1292
Ian Rogersb23a7722012-10-09 16:54:26 -07001293 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001294 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001295 size_t i = 0;
1296 while (i < code_item->insns_size_in_code_units_) {
1297 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001298 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1299 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001300 i += instruction->SizeInCodeUnits();
1301 }
1302 }
1303 }
1304
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001305 verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx,
1306 const DexFile* dex_file,
1307 const DexFile::ClassDef& class_def,
1308 const DexFile::CodeItem* code_item,
1309 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001310 if ((method_access_flags & kAccNative) == 0) {
1311 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001312 StackHandleScope<1> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001313 Handle<mirror::DexCache> dex_cache(
1314 hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001315 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001316 return verifier::MethodVerifier::VerifyMethodAndDump(
1317 soa.Self(), os, dex_method_idx, dex_file, dex_cache, *options_.class_loader_, &class_def,
1318 code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001319 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001320
1321 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001322 }
1323
Mathieu Chartier590fee92013-09-13 13:46:47 -07001324 void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001325 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1326 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001327 const void* quick_code = oat_method.GetQuickCode();
1328
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001329 if (code_size == 0) {
1330 code_size = oat_method.GetQuickCodeSize();
1331 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001332 if (code_size == 0 || quick_code == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001333 os << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001334 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001335 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001336 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1337 size_t offset = 0;
1338 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001339 if (!bad_input) {
1340 DumpMappingAtOffset(os, oat_method, offset, false);
1341 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001342 offset += disassembler_->Dump(os, quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001343 if (!bad_input) {
1344 uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
1345 if (dex_pc != DexFile::kDexNoIndex) {
1346 DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
1347 if (verifier != nullptr) {
1348 DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
1349 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001350 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001351 }
1352 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001353 }
1354 }
1355
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001356 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001357 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001358 const OatDumperOptions& options_;
1359 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001360 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001361 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001362 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001363};
1364
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001365class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001366 public:
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001367 explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001368 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
1369 : os_(os),
1370 image_space_(image_space),
1371 image_header_(image_header),
1372 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001373
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001374 bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001375 std::ostream& os = *os_;
1376 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001377
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001378 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001379
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001380 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1381
1382 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1383 auto section = static_cast<ImageHeader::ImageSections>(i);
1384 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1385 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001386
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001387 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001388
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001389 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001390
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001391 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1392
1393 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1394
1395 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001396
Alex Lighta59dd802014-07-02 16:28:08 -07001397 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1398
Igor Murashkin46774762014-10-22 11:37:02 -07001399 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1400
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001401 {
1402 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
1403 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1404 std::ostream indent1_os(&indent1_filter);
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001405 static_assert(arraysize(image_roots_descriptions_) ==
1406 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001407 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1408 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1409 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001410 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001411 indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
1412 if (image_root_object->IsObjectArray()) {
1413 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
1414 std::ostream indent2_os(&indent2_filter);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001415 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001416 = image_root_object->AsObjectArray<mirror::Object>();
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001417 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1418 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001419 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001420 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1421 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001422 run++;
1423 } else {
1424 break;
1425 }
1426 }
1427 if (run == 0) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001428 indent2_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001429 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001430 indent2_os << StringPrintf("%d to %zd: ", j, j + run);
1431 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001432 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001433 if (value != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001434 PrettyObjectValue(indent2_os, value->GetClass(), value);
1435 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001436 indent2_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001437 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001438 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001439 }
1440 }
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001441
1442 os << "METHOD ROOTS\n";
1443 static_assert(arraysize(image_methods_descriptions_) ==
1444 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1445 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1446 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1447 const char* description = image_methods_descriptions_[i];
1448 auto* image_method = image_header_.GetImageMethod(image_root);
1449 indent1_os << StringPrintf("%s: %p\n", description, image_method);
1450 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001451 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001452 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001453
Brian Carlstromaded5f72011-10-07 17:15:04 -07001454 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001455 std::string image_filename = image_space_.GetImageFilename();
1456 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001457 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001458 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001459 std::string error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -07001460 const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1461 if (oat_file == nullptr) {
Richard Uhlere5fed032015-03-18 08:21:11 -07001462 oat_file = OatFile::Open(oat_location, oat_location,
1463 nullptr, nullptr, false, nullptr,
1464 &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001465 if (oat_file == nullptr) {
1466 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001467 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001468 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001469 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001470 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001471
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001472 stats_.oat_file_bytes = oat_file->Size();
1473
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001474 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001475
Mathieu Chartier02e25112013-08-14 16:14:24 -07001476 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001477 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001478 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1479 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001480 }
1481
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001482 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001483
1484 // Loop through all the image spaces and dump their objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001485 gc::Heap* heap = Runtime::Current()->GetHeap();
1486 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07001487 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001488 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001489 {
1490 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1491 heap->FlushAllocStack();
1492 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001493 // Since FlushAllocStack() above resets the (active) allocation
1494 // stack. Need to revoke the thread-local allocation stacks that
1495 // point into it.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001496 {
1497 self->TransitionFromRunnableToSuspended(kNative);
1498 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Mathieu Chartierbf9fc582015-03-13 17:21:25 -07001499 thread_list->SuspendAll(__FUNCTION__);
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001500 heap->RevokeAllThreadLocalAllocationStacks(self);
1501 thread_list->ResumeAll();
1502 self->TransitionFromSuspendedToRunnable();
1503 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001504 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001505 {
1506 std::ostream* saved_os = os_;
1507 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1508 std::ostream indent_os(&indent_filter);
1509 os_ = &indent_os;
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001510
1511 // Mark dex caches.
1512 dex_cache_arrays_.clear();
1513 {
1514 ReaderMutexLock mu(self, *class_linker->DexLock());
1515 for (size_t i = 0; i < class_linker->GetDexCacheCount(); ++i) {
1516 auto* dex_cache = class_linker->GetDexCache(i);
1517 dex_cache_arrays_.insert(dex_cache->GetResolvedFields());
1518 dex_cache_arrays_.insert(dex_cache->GetResolvedMethods());
1519 }
1520 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001521 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001522 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001523 if (space->IsImageSpace()) {
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001524 auto* image_space = space->AsImageSpace();
1525 // Dump the normal objects before ArtMethods.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001526 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1527 indent_os << "\n";
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001528 // TODO: Dump fields.
1529 // Dump methods after.
1530 const auto& methods_section = image_header_.GetMethodsSection();
1531 const auto pointer_size =
1532 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
1533 const auto method_size = ArtMethod::ObjectSize(pointer_size);
1534 for (size_t pos = 0; pos < methods_section.Size(); pos += method_size) {
1535 auto* method = reinterpret_cast<ArtMethod*>(
1536 image_space->Begin() + pos + methods_section.Offset());
1537 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
1538 DumpMethod(method, this, indent_os);
1539 indent_os << "\n";
1540 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001541 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001542 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001543 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001544 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001545 indent_os << "\n";
1546 os_ = saved_os;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001547 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001548 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001549 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001550 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001551 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001552 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001553 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001554 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001555 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001556 size_t header_bytes = sizeof(ImageHeader);
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001557 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1558 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1559 const auto& method_section = image_header_.GetMethodsSection();
Mathieu Chartierfac3a392015-06-09 17:50:29 -07001560 const auto& intern_section = image_header_.GetImageSection(
1561 ImageHeader::kSectionInternedStrings);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001562 stats_.header_bytes = header_bytes;
1563 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1564 stats_.alignment_bytes += alignment_bytes;
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001565 stats_.alignment_bytes += bitmap_section.Offset() - image_header_.GetImageSize();
1566 stats_.bitmap_bytes += bitmap_section.Size();
1567 stats_.art_field_bytes += field_section.Size();
1568 stats_.art_method_bytes += method_section.Size();
Mathieu Chartierfac3a392015-06-09 17:50:29 -07001569 stats_.interned_strings_bytes += intern_section.Size();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001570 stats_.Dump(os);
1571 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001572
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001573 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001574
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001575 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001576 }
1577
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001578 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001579 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001580 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001581 CHECK(type != nullptr);
1582 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001583 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001584 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001585 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001586 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001587 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001588 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001589 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001590 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001591 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001592 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001593 }
1594 }
1595
Mathieu Chartierc7853442015-03-27 14:35:38 -07001596 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001597 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001598 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001599 switch (field->GetTypeAsPrimitiveType()) {
1600 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001601 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001602 break;
1603 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001604 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001605 break;
1606 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001607 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001608 break;
1609 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001610 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001611 break;
1612 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001613 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001614 break;
1615 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001616 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001617 break;
1618 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001619 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1620 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001621 break;
1622 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001623 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001624 break;
1625 case Primitive::kPrimNot: {
1626 // Get the value, don't compute the type unless it is non-null as we don't want
1627 // to cause class loading.
1628 mirror::Object* value = field->GetObj(obj);
1629 if (value == nullptr) {
1630 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001631 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001632 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001633 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001634 if (field_type != nullptr) {
1635 PrettyObjectValue(os, field_type, value);
1636 } else {
1637 os << StringPrintf("%p %s\n", value,
1638 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1639 }
Ian Rogers50239c72013-06-17 14:53:22 -07001640 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001641 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001642 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001643 default:
1644 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1645 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001646 }
1647 }
1648
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001649 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001650 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001651 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001652 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001653 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001654 }
Mathieu Chartierc7853442015-03-27 14:35:38 -07001655 ArtField* fields = klass->GetIFields();
1656 for (size_t i = 0, count = klass->NumInstanceFields(); i < count; i++) {
1657 PrintField(os, &fields[i], obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001658 }
1659 }
1660
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001661 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001662 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001663 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001664
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001665 const void* GetQuickOatCodeBegin(ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001666 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001667 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1668 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001669 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001670 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001671 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001672 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001673 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001674 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001675 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001676 }
1677
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001678 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001679 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001680 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1681 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001682 return 0;
1683 }
1684 return oat_code_begin[-1];
1685 }
1686
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001687 const void* GetQuickOatCodeEnd(ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001688 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001689 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001690 if (oat_code_begin == nullptr) {
1691 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001692 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001693 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001694 }
1695
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001696 static void Callback(mirror::Object* obj, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001697 DCHECK(obj != nullptr);
1698 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001699 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001700 if (!state->InDumpSpace(obj)) {
1701 return;
1702 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001703
1704 size_t object_bytes = obj->SizeOf();
1705 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1706 state->stats_.object_bytes += object_bytes;
1707 state->stats_.alignment_bytes += alignment_bytes;
1708
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001709 std::ostream& os = *state->os_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001710 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001711 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001712 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1713 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001714 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001715 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001716 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1717 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001718 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001719 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001720 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001721 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001722 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001723 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001724 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1725 std::ostream indent_os(&indent_filter);
1726 DumpFields(indent_os, obj, obj_class);
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001727 const auto image_pointer_size =
1728 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Ian Rogersd5b32602012-02-26 16:40:04 -08001729 if (obj->IsObjectArray()) {
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001730 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1731 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001732 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001733 size_t run = 0;
1734 for (int32_t j = i + 1; j < length; j++) {
1735 if (value == obj_array->Get(j)) {
1736 run++;
1737 } else {
1738 break;
1739 }
1740 }
1741 if (run == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001742 indent_os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001743 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001744 indent_os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001745 i = i + run;
1746 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001747 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001748 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001749 PrettyObjectValue(indent_os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001750 }
1751 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001752 mirror::Class* klass = obj->AsClass();
1753 ArtField* sfields = klass->GetSFields();
1754 const size_t num_fields = klass->NumStaticFields();
1755 if (num_fields != 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001756 indent_os << "STATICS:\n";
1757 Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
1758 std::ostream indent2_os(&indent2_filter);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001759 for (size_t i = 0; i < num_fields; i++) {
1760 PrintField(indent2_os, &sfields[i], sfields[i].GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001761 }
1762 }
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001763 } else {
1764 auto it = state->dex_cache_arrays_.find(obj);
1765 if (it != state->dex_cache_arrays_.end()) {
1766 const auto& field_section = state->image_header_.GetImageSection(
1767 ImageHeader::kSectionArtFields);
1768 const auto& method_section = state->image_header_.GetMethodsSection();
1769 auto* arr = down_cast<mirror::PointerArray*>(obj);
1770 for (int32_t i = 0, length = arr->GetLength(); i < length; i++) {
1771 void* elem = arr->GetElementPtrSize<void*>(i, image_pointer_size);
1772 size_t run = 0;
1773 for (int32_t j = i + 1; j < length &&
1774 elem == arr->GetElementPtrSize<void*>(j, image_pointer_size); j++, run++) { }
1775 if (run == 0) {
1776 indent_os << StringPrintf("%d: ", i);
1777 } else {
1778 indent_os << StringPrintf("%d to %zd: ", i, i + run);
1779 i = i + run;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001780 }
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001781 auto offset = reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin();
1782 std::string msg;
1783 if (field_section.Contains(offset)) {
1784 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
1785 } else if (method_section.Contains(offset)) {
1786 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1787 } else {
1788 msg = "Unknown type";
1789 }
1790 indent_os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001791 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001792 }
1793 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001794 std::string temp;
1795 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001796 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001797
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001798 void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
1799 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1800 DCHECK(method != nullptr);
1801 const auto image_pointer_size =
1802 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
1803 if (method->IsNative()) {
1804 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1805 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1806 bool first_occurrence;
1807 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1808 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1809 state->ComputeOatSize(quick_oat_code, &first_occurrence);
1810 if (first_occurrence) {
1811 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
1812 }
1813 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
1814 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
1815 }
1816 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
1817 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
1818 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
1819 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1820 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1821 } else {
1822 const DexFile::CodeItem* code_item = method->GetCodeItem();
1823 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
1824 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
1825
1826 bool first_occurrence;
1827 size_t gc_map_bytes = state->ComputeOatSize(
1828 method->GetNativeGcMap(image_pointer_size), &first_occurrence);
1829 if (first_occurrence) {
1830 state->stats_.gc_map_bytes += gc_map_bytes;
1831 }
1832
1833 size_t pc_mapping_table_bytes = state->ComputeOatSize(
1834 method->GetMappingTable(image_pointer_size), &first_occurrence);
1835 if (first_occurrence) {
1836 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1837 }
1838
1839 size_t vmap_table_bytes = state->ComputeOatSize(
1840 method->GetVmapTable(image_pointer_size), &first_occurrence);
1841 if (first_occurrence) {
1842 state->stats_.vmap_table_bytes += vmap_table_bytes;
1843 }
1844
1845 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1846 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1847 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1848 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
1849 if (first_occurrence) {
1850 state->stats_.managed_code_bytes += quick_oat_code_size;
1851 if (method->IsConstructor()) {
1852 if (method->IsStatic()) {
1853 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
1854 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
1855 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
1856 }
1857 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
1858 state->stats_.large_method_code_bytes += quick_oat_code_size;
1859 }
1860 }
1861 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
1862
1863 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
1864 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
1865 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
1866
1867 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
1868 vmap_table_bytes + quick_oat_code_size + ArtMethod::ObjectSize(image_pointer_size);
1869
1870 double expansion =
1871 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
1872 state->stats_.ComputeOutliers(total_size, expansion, method);
1873 }
1874 }
1875
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001876 std::set<const void*> already_seen_;
1877 // Compute the size of the given data within the oat file and whether this is the first time
1878 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001879 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001880 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001881 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001882 already_seen_.insert(oat_data);
1883 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001884 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001885 }
1886 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001887 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001888
1889 public:
1890 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001891 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001892 size_t file_bytes;
1893
1894 size_t header_bytes;
1895 size_t object_bytes;
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001896 size_t art_field_bytes;
1897 size_t art_method_bytes;
Mathieu Chartierfac3a392015-06-09 17:50:29 -07001898 size_t interned_strings_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001899 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001900 size_t alignment_bytes;
1901
1902 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001903 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001904 size_t managed_to_native_code_bytes;
1905 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001906 size_t class_initializer_code_bytes;
1907 size_t large_initializer_code_bytes;
1908 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001909
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001910 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001911 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001912 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001913
1914 size_t dex_instruction_bytes;
1915
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001916 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001917 std::vector<size_t> method_outlier_size;
1918 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07001919 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001920
1921 explicit Stats()
1922 : oat_file_bytes(0),
1923 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001924 header_bytes(0),
1925 object_bytes(0),
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001926 art_field_bytes(0),
1927 art_method_bytes(0),
Mathieu Chartierfac3a392015-06-09 17:50:29 -07001928 interned_strings_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07001929 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001930 alignment_bytes(0),
1931 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001932 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001933 managed_to_native_code_bytes(0),
1934 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001935 class_initializer_code_bytes(0),
1936 large_initializer_code_bytes(0),
1937 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001938 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001939 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001940 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001941 dex_instruction_bytes(0) {}
1942
Elliott Hughesa0e18062012-04-13 15:59:59 -07001943 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001944 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07001945 size_t bytes;
1946 size_t count;
1947 };
1948 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1949 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001950
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001951 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001952 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1953 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001954 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001955 it->second.count += 1;
1956 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001957 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07001958 }
1959 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001960
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001961 double PercentOfOatBytes(size_t size) {
1962 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1963 }
1964
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001965 double PercentOfFileBytes(size_t size) {
1966 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1967 }
1968
1969 double PercentOfObjectBytes(size_t size) {
1970 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1971 }
1972
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07001973 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001974 method_outlier_size.push_back(total_size);
1975 method_outlier_expansion.push_back(expansion);
1976 method_outlier.push_back(method);
1977 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001978
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001979 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001980 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001981 size_t sum_of_sizes = 0;
1982 size_t sum_of_sizes_squared = 0;
1983 size_t sum_of_expansion = 0;
1984 size_t sum_of_expansion_squared = 0;
1985 size_t n = method_outlier_size.size();
1986 for (size_t i = 0; i < n; i++) {
1987 size_t cur_size = method_outlier_size[i];
1988 sum_of_sizes += cur_size;
1989 sum_of_sizes_squared += cur_size * cur_size;
1990 double cur_expansion = method_outlier_expansion[i];
1991 sum_of_expansion += cur_expansion;
1992 sum_of_expansion_squared += cur_expansion * cur_expansion;
1993 }
1994 size_t size_mean = sum_of_sizes / n;
1995 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1996 double expansion_mean = sum_of_expansion / n;
1997 double expansion_variance =
1998 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1999
2000 // Dump methods whose size is a certain number of standard deviations from the mean
2001 size_t dumped_values = 0;
2002 size_t skipped_values = 0;
2003 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2004 size_t cur_size_variance = i * i * size_variance;
2005 bool first = true;
2006 for (size_t j = 0; j < n; j++) {
2007 size_t cur_size = method_outlier_size[j];
2008 if (cur_size > size_mean) {
2009 size_t cur_var = cur_size - size_mean;
2010 cur_var = cur_var * cur_var;
2011 if (cur_var > cur_size_variance) {
2012 if (dumped_values > 20) {
2013 if (i == 1) {
2014 skipped_values++;
2015 } else {
2016 i = 2; // jump to counting for 1 standard deviation
2017 break;
2018 }
2019 } else {
2020 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002021 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002022 first = false;
2023 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002024 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002025 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002026 method_outlier_size[j] = 0; // don't consider this method again
2027 dumped_values++;
2028 }
2029 }
2030 }
2031 }
2032 }
2033 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002034 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002035 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002036 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002037 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002038
2039 // Dump methods whose expansion is a certain number of standard deviations from the mean
2040 dumped_values = 0;
2041 skipped_values = 0;
2042 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2043 double cur_expansion_variance = i * i * expansion_variance;
2044 bool first = true;
2045 for (size_t j = 0; j < n; j++) {
2046 double cur_expansion = method_outlier_expansion[j];
2047 if (cur_expansion > expansion_mean) {
2048 size_t cur_var = cur_expansion - expansion_mean;
2049 cur_var = cur_var * cur_var;
2050 if (cur_var > cur_expansion_variance) {
2051 if (dumped_values > 20) {
2052 if (i == 1) {
2053 skipped_values++;
2054 } else {
2055 i = 2; // jump to counting for 1 standard deviation
2056 break;
2057 }
2058 } else {
2059 if (first) {
2060 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002061 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002062 first = false;
2063 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002064 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002065 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002066 method_outlier_expansion[j] = 0.0; // don't consider this method again
2067 dumped_values++;
2068 }
2069 }
2070 }
2071 }
2072 }
2073 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002074 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002075 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002076 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002077 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002078 }
2079
Ian Rogersb726dcb2012-09-05 08:57:23 -07002080 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002081 {
2082 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2083 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
2084 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
2085 std::ostream indent_os(&indent_filter);
Mathieu Chartierfac3a392015-06-09 17:50:29 -07002086 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2087 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2088 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2089 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2090 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
2091 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2092 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002093 header_bytes, PercentOfFileBytes(header_bytes),
2094 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07002095 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2096 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Mathieu Chartierfac3a392015-06-09 17:50:29 -07002097 interned_strings_bytes,
2098 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002099 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002100 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2101 << std::flush;
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07002102 CHECK_EQ(file_bytes, header_bytes + object_bytes + art_field_bytes + art_method_bytes +
Mathieu Chartierfac3a392015-06-09 17:50:29 -07002103 interned_strings_bytes + bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002104 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002105
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002106 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002107 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002108 for (const auto& sizes_and_count : sizes_and_counts) {
2109 const std::string& descriptor(sizes_and_count.first);
2110 double average = static_cast<double>(sizes_and_count.second.bytes) /
2111 static_cast<double>(sizes_and_count.second.count);
2112 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002113 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002114 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002115 descriptor.c_str(), sizes_and_count.second.bytes,
2116 sizes_and_count.second.count, average, percent);
2117 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002118 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002119 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002120 CHECK_EQ(object_bytes, object_bytes_total);
2121
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002122 os << StringPrintf("oat_file_bytes = %8zd\n"
2123 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2124 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2125 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2126 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2127 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2128 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002129 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002130 managed_code_bytes,
2131 PercentOfOatBytes(managed_code_bytes),
2132 managed_to_native_code_bytes,
2133 PercentOfOatBytes(managed_to_native_code_bytes),
2134 native_to_managed_code_bytes,
2135 PercentOfOatBytes(native_to_managed_code_bytes),
2136 class_initializer_code_bytes,
2137 PercentOfOatBytes(class_initializer_code_bytes),
2138 large_initializer_code_bytes,
2139 PercentOfOatBytes(large_initializer_code_bytes),
2140 large_method_code_bytes,
2141 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002142 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002143 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002144 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002145 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2146 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002147 }
2148
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002149 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2150 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2151 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002152 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2153 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2154 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002155 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002156
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002157 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2158 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002159 static_cast<double>(managed_code_bytes) /
2160 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002161 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002162 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002163 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002164
2165 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002166 }
2167 } stats_;
2168
2169 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002170 enum {
2171 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2172 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2173 kLargeConstructorDexBytes = 4000,
2174 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2175 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2176 kLargeMethodDexBytes = 16000
2177 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002178 std::ostream* os_;
Ian Rogers1d54e732013-05-02 21:10:01 -07002179 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002180 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002181 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002182 OatDumperOptions* oat_dumper_options_;
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -07002183 std::set<mirror::Object*> dex_cache_arrays_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002184
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002185 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002186};
2187
Andreas Gampe00b25f32014-09-17 21:49:05 -07002188static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
2189 std::ostream* os) {
2190 // Dumping the image, no explicit class loader.
2191 NullHandle<mirror::ClassLoader> null_class_loader;
2192 options->class_loader_ = &null_class_loader;
2193
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002194 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002195 gc::Heap* heap = runtime->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -07002196 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002197 CHECK(image_space != nullptr);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002198 const ImageHeader& image_header = image_space->GetImageHeader();
2199 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002200 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002201 return EXIT_FAILURE;
2202 }
Igor Murashkin37743352014-11-13 14:38:00 -08002203
Andreas Gampe00b25f32014-09-17 21:49:05 -07002204 ImageDumper image_dumper(os, *image_space, image_header, options);
Igor Murashkin37743352014-11-13 14:38:00 -08002205
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002206 bool success = image_dumper.Dump();
2207 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002208}
2209
Andreas Gampe00b25f32014-09-17 21:49:05 -07002210static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2211 std::ostream* os) {
2212 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2213
2214 Thread* self = Thread::Current();
2215 CHECK(self != nullptr);
2216 // Need well-known-classes.
2217 WellKnownClasses::Init(self->GetJniEnv());
2218
2219 // Need to register dex files to get a working dex cache.
2220 ScopedObjectAccess soa(self);
2221 ClassLinker* class_linker = runtime->GetClassLinker();
2222 class_linker->RegisterOatFile(oat_file);
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002223 std::vector<std::unique_ptr<const DexFile>> dex_files;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002224 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2225 std::string error_msg;
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002226 std::unique_ptr<const DexFile> dex_file = odf->OpenDexFile(&error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002227 CHECK(dex_file != nullptr) << error_msg;
2228 class_linker->RegisterDexFile(*dex_file);
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002229 dex_files.push_back(std::move(dex_file));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002230 }
2231
2232 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002233 // Fake that we're a compiler.
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002234 std::vector<const DexFile*> class_path;
2235 for (auto& dex_file : dex_files) {
2236 class_path.push_back(dex_file.get());
2237 }
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07002238 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002239
2240 // Use the class loader while dumping.
2241 StackHandleScope<1> scope(self);
2242 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2243 soa.Decode<mirror::ClassLoader*>(class_loader));
2244 options->class_loader_ = &loader_handle;
2245
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002246 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002247 bool success = oat_dumper.Dump(*os);
2248 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2249}
2250
2251static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002252 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002253 // No image = no class loader.
2254 NullHandle<mirror::ClassLoader> null_class_loader;
2255 options->class_loader_ = &null_class_loader;
2256
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002257 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002258 bool success = oat_dumper.Dump(*os);
2259 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2260}
2261
2262static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2263 std::ostream* os) {
2264 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002265 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Richard Uhlere5fed032015-03-18 08:21:11 -07002266 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002267 if (oat_file == nullptr) {
2268 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2269 return EXIT_FAILURE;
2270 }
2271
2272 if (runtime != nullptr) {
2273 return DumpOatWithRuntime(runtime, oat_file, options, os);
2274 } else {
2275 return DumpOatWithoutRuntime(oat_file, options, os);
2276 }
2277}
2278
2279static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2280 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002281 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Richard Uhlere5fed032015-03-18 08:21:11 -07002282 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002283 if (oat_file == nullptr) {
2284 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2285 return EXIT_FAILURE;
2286 }
2287
2288 OatSymbolizer oat_symbolizer(oat_file, output_name);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002289 if (!oat_symbolizer.Symbolize()) {
2290 fprintf(stderr, "Failed to symbolize\n");
2291 return EXIT_FAILURE;
2292 }
2293
2294 return EXIT_SUCCESS;
2295}
2296
Igor Murashkin37743352014-11-13 14:38:00 -08002297struct OatdumpArgs : public CmdlineArgs {
2298 protected:
2299 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002300
Igor Murashkin37743352014-11-13 14:38:00 -08002301 virtual ParseStatus ParseCustom(const StringPiece& option,
2302 std::string* error_msg) OVERRIDE {
2303 {
2304 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2305 if (base_parse != kParseUnknownArgument) {
2306 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002307 }
2308 }
2309
Igor Murashkin37743352014-11-13 14:38:00 -08002310 if (option.starts_with("--oat-file=")) {
2311 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2312 } else if (option.starts_with("--image=")) {
2313 image_location_ = option.substr(strlen("--image=")).data();
2314 } else if (option =="--dump:raw_mapping_table") {
2315 dump_raw_mapping_table_ = true;
2316 } else if (option == "--dump:raw_gc_map") {
2317 dump_raw_gc_map_ = true;
2318 } else if (option == "--no-dump:vmap") {
2319 dump_vmap_ = false;
2320 } else if (option == "--no-disassemble") {
2321 disassemble_code_ = false;
2322 } else if (option.starts_with("--symbolize=")) {
2323 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2324 symbolize_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002325 } else if (option.starts_with("--class-filter=")) {
2326 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002327 } else if (option.starts_with("--method-filter=")) {
2328 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002329 } else if (option.starts_with("--list-classes")) {
2330 list_classes_ = true;
2331 } else if (option.starts_with("--list-methods")) {
2332 list_methods_ = true;
2333 } else if (option.starts_with("--export-dex-to=")) {
2334 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2335 } else if (option.starts_with("--addr2instr=")) {
2336 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2337 *error_msg = "Address conversion failed";
2338 return kParseError;
2339 }
Igor Murashkin37743352014-11-13 14:38:00 -08002340 } else {
2341 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002342 }
2343
Igor Murashkin37743352014-11-13 14:38:00 -08002344 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002345 }
2346
Igor Murashkin37743352014-11-13 14:38:00 -08002347 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2348 // Infer boot image location from the image location if possible.
2349 if (boot_image_location_ == nullptr) {
2350 boot_image_location_ = image_location_;
2351 }
2352
2353 // Perform the parent checks.
2354 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2355 if (parent_checks != kParseOk) {
2356 return parent_checks;
2357 }
2358
2359 // Perform our own checks.
2360 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2361 *error_msg = "Either --image or --oat-file must be specified";
2362 return kParseError;
2363 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2364 *error_msg = "Either --image or --oat-file must be specified but not both";
2365 return kParseError;
2366 }
2367
2368 return kParseOk;
2369 }
2370
2371 virtual std::string GetUsage() const {
2372 std::string usage;
2373
2374 usage +=
2375 "Usage: oatdump [options] ...\n"
2376 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2377 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2378 "\n"
2379 // Either oat-file or image is required.
2380 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2381 " Example: --oat-file=/system/framework/boot.oat\n"
2382 "\n"
2383 " --image=<file.art>: specifies an input image location.\n"
2384 " Example: --image=/system/framework/boot.art\n"
2385 "\n";
2386
2387 usage += Base::GetUsage();
2388
2389 usage += // Optional.
2390 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2391 " Example: --dump:raw_mapping_table\n"
2392 "\n"
Mathieu Chartierc9309802015-05-26 14:44:35 -07002393 " --dump:raw_gc_map enables dumping of the GC map.\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002394 " Example: --dump:raw_gc_map\n"
2395 "\n"
2396 " --no-dump:vmap may be used to disable vmap dumping.\n"
2397 " Example: --no-dump:vmap\n"
2398 "\n"
2399 " --no-disassemble may be used to disable disassembly.\n"
2400 " Example: --no-disassemble\n"
2401 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002402 " --list-classes may be used to list target file classes (can be used with filters).\n"
2403 " Example: --list-classes\n"
2404 " Example: --list-classes --class-filter=com.example.foo\n"
2405 "\n"
2406 " --list-methods may be used to list target file methods (can be used with filters).\n"
2407 " Example: --list-methods\n"
2408 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2409 "\n"
2410 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2411 " Example: --symbolize=/system/framework/boot.oat\n"
2412 "\n"
2413 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2414 " Example: --class-filter=com.example.foo\n"
2415 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002416 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2417 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002418 "\n"
2419 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2420 " Example: --export-dex-to=/data/local/tmp\n"
2421 "\n"
2422 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2423 " address (e.g. PC from crash dump)\n"
2424 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002425 "\n";
2426
2427 return usage;
2428 }
2429
2430 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002431 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002432 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002433 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002434 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002435 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002436 bool dump_raw_mapping_table_ = false;
2437 bool dump_raw_gc_map_ = false;
2438 bool dump_vmap_ = true;
2439 bool disassemble_code_ = true;
2440 bool symbolize_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002441 bool list_classes_ = false;
2442 bool list_methods_ = false;
2443 uint32_t addr2instr_ = 0;
2444 const char* export_dex_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002445};
2446
Igor Murashkin37743352014-11-13 14:38:00 -08002447struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2448 virtual bool NeedsRuntime() OVERRIDE {
2449 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002450
Igor Murashkin37743352014-11-13 14:38:00 -08002451 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2452 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2453
2454 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2455 args_->dump_raw_mapping_table_,
2456 args_->dump_raw_gc_map_,
2457 args_->dump_vmap_,
2458 args_->disassemble_code_,
2459 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002460 args_->class_filter_,
2461 args_->method_filter_,
2462 args_->list_classes_,
2463 args_->list_methods_,
2464 args_->export_dex_location_,
2465 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002466
2467 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2468 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002469 }
2470
Igor Murashkin37743352014-11-13 14:38:00 -08002471 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2472 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002473 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002474
Mathieu Chartierd424d082014-10-15 10:31:46 -07002475 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002476
Andreas Gampec24f3992014-12-17 20:40:11 -08002477 if (args_->symbolize_) {
2478 return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2479 } else {
2480 return DumpOat(nullptr,
2481 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002482 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002483 args_->os_) == EXIT_SUCCESS;
2484 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002485 }
2486
Igor Murashkin37743352014-11-13 14:38:00 -08002487 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2488 CHECK(args_ != nullptr);
2489
2490 if (args_->oat_filename_ != nullptr) {
2491 return DumpOat(runtime,
2492 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002493 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002494 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002495 }
Igor Murashkin37743352014-11-13 14:38:00 -08002496
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002497 return DumpImage(runtime, args_->image_location_, oat_dumper_options_.get(), args_->os_)
Igor Murashkin37743352014-11-13 14:38:00 -08002498 == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002499 }
2500
Igor Murashkin37743352014-11-13 14:38:00 -08002501 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2502};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002503
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002504} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002505
2506int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002507 art::OatdumpMain main;
2508 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002509}