blob: ea71996d1f90821df9b509abb4fbb6bb94be6b33 [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>
Brian Carlstrom78128a62011-09-15 17:21:19 -070022#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070023#include <unordered_map>
Brian Carlstrom78128a62011-09-15 17:21:19 -070024#include <vector>
25
Ian Rogersd582fa42014-11-05 23:46:43 -080026#include "arch/instruction_set_features.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080027#include "base/stringpiece.h"
Elliott Hughes76160052012-12-12 16:31:20 -080028#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070029#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080032#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080033#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070034#include "elf_builder.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070035#include "field_helper.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080036#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070037#include "gc/space/image_space.h"
38#include "gc/space/large_object_space.h"
39#include "gc/space/space-inl.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070040#include "image.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080041#include "indenter.h"
Ian Rogers1809a722013-08-09 22:05:32 -070042#include "mapping_table.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070043#include "mirror/art_field-inl.h"
44#include "mirror/art_method-inl.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 Carlstromc0a1b182014-03-04 23:19:06 -080049#include "noop_compiler_callbacks.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080050#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010051#include "oat_file-inl.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080052#include "os.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070053#include "output_stream.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070054#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070055#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070056#include "scoped_thread_state_change.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070057#include "ScopedLocalRef.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080058#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080059#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080060#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070061#include "vmap_table.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070062#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070063
64namespace art {
65
66static void usage() {
67 fprintf(stderr,
68 "Usage: oatdump [options] ...\n"
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +000069 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080070 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070071 "\n");
72 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080073 " --oat-file=<file.oat>: specifies an input oat filename.\n"
TDYa127a0a2a6c2012-10-16 22:47:38 -070074 " Example: --oat-file=/system/framework/boot.oat\n"
Brian Carlstromaded5f72011-10-07 17:15:04 -070075 "\n");
76 fprintf(stderr,
77 " --image=<file.art>: specifies an input image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080078 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070079 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070080 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070081 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080082 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070083 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070084 fprintf(stderr,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070085 " --instruction-set=(arm|arm64|mips|x86|x86_64): for locating the image\n"
86 " file based on the image location set.\n"
Brian Carlstrom0f5baa02014-05-22 11:54:18 -070087 " Example: --instruction-set=x86\n"
88 " Default: %s\n"
89 "\n",
90 GetInstructionSetString(kRuntimeISA));
91 fprintf(stderr,
Brian Carlstrom27ec9612011-09-19 20:20:38 -070092 " --output=<file> may be used to send the output to a file.\n"
93 " Example: --output=/tmp/oatdump.txt\n"
94 "\n");
Dave Allison404f59f2014-02-24 11:10:01 -080095 fprintf(stderr,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070096 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
97 " Example: --dump:raw_mapping_table\n"
98 "\n");
99 fprintf(stderr,
100 " --dump:raw_mapping_table enables dumping of the GC map.\n"
101 " Example: --dump:raw_gc_map\n"
102 "\n");
103 fprintf(stderr,
104 " --no-dump:vmap may be used to disable vmap dumping.\n"
105 " Example: --no-dump:vmap\n"
106 "\n");
107 fprintf(stderr,
108 " --no-disassemble may be used to disable disassembly.\n"
109 " Example: --no-disassemble\n"
Dave Allison404f59f2014-02-24 11:10:01 -0800110 "\n");
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000111 fprintf(stderr,
112 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
113 " Example: --method-filter=foo\n"
114 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700115}
116
Ian Rogersff1ed472011-09-20 13:46:24 -0700117const char* image_roots_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -0800118 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -0700119 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700120 "kImtUnimplementedMethod",
Jeff Hao88474b42013-10-23 16:24:40 -0700121 "kDefaultImt",
Brian Carlstrome24fa612011-09-29 00:53:55 -0700122 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -0700123 "kRefsOnlySaveMethod",
124 "kRefsAndArgsSaveMethod",
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700125 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700126 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700127};
128
Ian Rogers0279ebb2014-10-08 17:27:48 -0700129class OatSymbolizer FINAL : public CodeOutput {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700130 public:
Ian Rogers0279ebb2014-10-08 17:27:48 -0700131 explicit OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
132 oat_file_(oat_file), builder_(nullptr), elf_output_(nullptr),
133 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
134 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700135
136 bool Init() {
137 Elf32_Word oat_data_size = oat_file_->GetOatHeader().GetExecutableOffset();
138
139 uint32_t diff = static_cast<uint32_t>(oat_file_->End() - oat_file_->Begin());
140 uint32_t oat_exec_size = diff - oat_data_size;
141
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700142 elf_output_ = OS::CreateEmptyFile(output_name_.c_str());
143
144 builder_.reset(new ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
145 Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(
146 this,
147 elf_output_,
148 oat_file_->GetOatHeader().GetInstructionSet(),
149 0,
150 oat_data_size,
151 oat_data_size,
152 oat_exec_size,
153 true,
154 false));
155
156 if (!builder_->Init()) {
157 builder_.reset(nullptr);
158 return false;
159 }
160
161 return true;
162 }
163
164 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
165 uint32_t,
166 const OatFile::OatMethod&,
167 const DexFile&,
168 uint32_t,
169 const DexFile::CodeItem*,
170 uint32_t);
171
172 bool Symbolize() {
173 if (builder_.get() == nullptr) {
174 return false;
175 }
176
177 Walk(&art::OatSymbolizer::RegisterForDedup);
178
179 NormalizeState();
180
181 Walk(&art::OatSymbolizer::AddSymbol);
182
183 bool result = builder_->Write();
184
Andreas Gampe4303ba92014-11-06 01:00:46 -0800185 // Ignore I/O errors.
186 UNUSED(elf_output_->FlushClose());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700187
188 return result;
189 }
190
191 void Walk(Callback callback) {
192 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
193 for (size_t i = 0; i < oat_dex_files.size(); i++) {
194 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
195 CHECK(oat_dex_file != NULL);
196 WalkOatDexFile(oat_dex_file, callback);
197 }
198 }
199
200 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
201 std::string error_msg;
202 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
203 if (dex_file.get() == nullptr) {
204 return;
205 }
206 for (size_t class_def_index = 0;
207 class_def_index < dex_file->NumClassDefs();
208 class_def_index++) {
209 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
210 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
211 OatClassType type = oat_class.GetType();
212 switch (type) {
213 case kOatClassAllCompiled:
214 case kOatClassSomeCompiled:
215 WalkOatClass(oat_class, *dex_file.get(), class_def, callback);
216 break;
217
218 case kOatClassNoneCompiled:
219 case kOatClassMax:
220 // Ignore.
221 break;
222 }
223 }
224 }
225
226 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
227 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700228 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700229 if (class_data == nullptr) { // empty class such as a marker interface?
230 return;
231 }
232 // Note: even if this is an interface or a native class, we still have to walk it, as there
233 // might be a static initializer.
234 ClassDataItemIterator it(dex_file, class_data);
235 SkipAllFields(&it);
236 uint32_t class_method_idx = 0;
237 while (it.HasNextDirectMethod()) {
238 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
239 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700240 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700241 class_method_idx++;
242 it.Next();
243 }
244 while (it.HasNextVirtualMethod()) {
245 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
246 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700247 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700248 class_method_idx++;
249 it.Next();
250 }
251 DCHECK(!it.HasNext());
252 }
253
254 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
255 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
256 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
257 uint32_t method_access_flags, Callback callback) {
258 if ((method_access_flags & kAccAbstract) != 0) {
259 // Abstract method, no code.
260 return;
261 }
262 if (oat_method.GetCodeOffset() == 0) {
263 // No code.
264 return;
265 }
266
267 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
268 method_access_flags);
269 }
270
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700271 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
272 uint32_t class_method_index ATTRIBUTE_UNUSED,
273 const OatFile::OatMethod& oat_method,
274 const DexFile& dex_file ATTRIBUTE_UNUSED,
275 uint32_t dex_method_idx ATTRIBUTE_UNUSED,
276 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
277 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700278 state_[oat_method.GetCodeOffset()]++;
279 }
280
281 void NormalizeState() {
282 for (auto& x : state_) {
283 if (x.second == 1) {
284 state_[x.first] = 0;
285 }
286 }
287 }
288
289 enum class DedupState { // private
290 kNotDeduplicated,
291 kDeduplicatedFirst,
292 kDeduplicatedOther
293 };
294 DedupState IsDuplicated(uint32_t offset) {
295 if (state_[offset] == 0) {
296 return DedupState::kNotDeduplicated;
297 }
298 if (state_[offset] == 1) {
299 return DedupState::kDeduplicatedOther;
300 }
301 state_[offset] = 1;
302 return DedupState::kDeduplicatedFirst;
303 }
304
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700305 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
306 uint32_t class_method_index ATTRIBUTE_UNUSED,
307 const OatFile::OatMethod& oat_method,
308 const DexFile& dex_file,
309 uint32_t dex_method_idx,
310 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
311 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700312 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
313 if (dedup != DedupState::kDeduplicatedOther) {
314 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
315
316 if (dedup == DedupState::kDeduplicatedFirst) {
317 pretty_name = "[Dedup]" + pretty_name;
318 }
319
320 ElfSymtabBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr,
Ian Rogers0279ebb2014-10-08 17:27:48 -0700321 Elf32_Sym, Elf32_Shdr>* symtab = builder_->GetSymtabBuilder();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700322
Ian Rogers0279ebb2014-10-08 17:27:48 -0700323 symtab->AddSymbol(pretty_name, &builder_->GetTextBuilder(),
324 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
325 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700326 }
327 }
328
Vladimir Markof4da6752014-08-01 19:04:18 +0100329 // Set oat data offset. Required by ElfBuilder/CodeOutput.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700330 void SetCodeOffset(size_t offset ATTRIBUTE_UNUSED) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100331 // Nothing to do.
332 }
333
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700334 // Write oat code. Required by ElfBuilder/CodeOutput.
335 bool Write(OutputStream* out) {
336 return out->WriteFully(oat_file_->Begin(), oat_file_->End() - oat_file_->Begin());
337 }
338
339 private:
340 static void SkipAllFields(ClassDataItemIterator* it) {
341 while (it->HasNextStaticField()) {
342 it->Next();
343 }
344 while (it->HasNextInstanceField()) {
345 it->Next();
346 }
347 }
348
349 const OatFile* oat_file_;
350 std::unique_ptr<ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
351 Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr> > builder_;
352 File* elf_output_;
353 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700354 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700355};
356
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700357class OatDumperOptions {
358 public:
359 OatDumperOptions(bool dump_raw_mapping_table,
360 bool dump_raw_gc_map,
361 bool dump_vmap,
362 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700363 bool absolute_addresses,
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000364 const char* method_filter,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700365 Handle<mirror::ClassLoader>* class_loader)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700366 : dump_raw_mapping_table_(dump_raw_mapping_table),
367 dump_raw_gc_map_(dump_raw_gc_map),
368 dump_vmap_(dump_vmap),
369 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700370 absolute_addresses_(absolute_addresses),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000371 method_filter_(method_filter),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700372 class_loader_(class_loader) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700373
374 const bool dump_raw_mapping_table_;
375 const bool dump_raw_gc_map_;
376 const bool dump_vmap_;
377 const bool disassemble_code_;
378 const bool absolute_addresses_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000379 const char* const method_filter_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700380 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700381};
382
Elliott Hughese3c845c2012-02-28 17:23:01 -0800383class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700384 public:
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700385 explicit OatDumper(const OatFile& oat_file, OatDumperOptions* options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000386 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800387 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700388 options_(options),
389 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet(),
390 new DisassemblerOptions(options_->absolute_addresses_,
391 oat_file.Begin()))) {
Andreas Gampe00b25f32014-09-17 21:49:05 -0700392 CHECK(options_->class_loader_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800393 AddAllOffsets();
394 }
395
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700396 ~OatDumper() {
397 delete options_;
398 delete disassembler_;
399 }
400
401 bool Dump(std::ostream& os) {
402 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800403 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700404
405 os << "MAGIC:\n";
406 os << oat_header.GetMagic() << "\n\n";
407
408 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800409 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700410
Elliott Hughesa72ec822012-03-05 17:12:22 -0800411 os << "INSTRUCTION SET:\n";
412 os << oat_header.GetInstructionSet() << "\n\n";
413
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700414 {
415 std::unique_ptr<const InstructionSetFeatures> features(
416 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
417 oat_header.GetInstructionSetFeaturesBitmap()));
418 os << "INSTRUCTION SET FEATURES:\n";
419 os << features->GetFeatureString() << "\n\n";
420 }
Dave Allison70202782013-10-22 17:52:19 -0700421
Brian Carlstromaded5f72011-10-07 17:15:04 -0700422 os << "DEX FILE COUNT:\n";
423 os << oat_header.GetDexFileCount() << "\n\n";
424
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800425#define DUMP_OAT_HEADER_OFFSET(label, offset) \
426 os << label " OFFSET:\n"; \
427 os << StringPrintf("0x%08x", oat_header.offset()); \
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700428 if (oat_header.offset() != 0 && options_->absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800429 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
430 } \
431 os << StringPrintf("\n\n");
432
433 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
434 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
435 GetInterpreterToInterpreterBridgeOffset);
436 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
437 GetInterpreterToCompiledCodeBridgeOffset);
438 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
439 GetJniDlsymLookupOffset);
440 DUMP_OAT_HEADER_OFFSET("PORTABLE IMT CONFLICT TRAMPOLINE",
441 GetPortableImtConflictTrampolineOffset);
442 DUMP_OAT_HEADER_OFFSET("PORTABLE RESOLUTION TRAMPOLINE",
443 GetPortableResolutionTrampolineOffset);
444 DUMP_OAT_HEADER_OFFSET("PORTABLE TO INTERPRETER BRIDGE",
445 GetPortableToInterpreterBridgeOffset);
446 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
447 GetQuickGenericJniTrampolineOffset);
448 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
449 GetQuickImtConflictTrampolineOffset);
450 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
451 GetQuickResolutionTrampolineOffset);
452 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
453 GetQuickToInterpreterBridgeOffset);
454#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700455
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700456 os << "IMAGE PATCH DELTA:\n";
457 os << StringPrintf("%d (0x%08x)\n\n",
458 oat_header.GetImagePatchDelta(),
459 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700460
Brian Carlstrom28db0122012-10-18 16:20:41 -0700461 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
462 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
463
464 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800465 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700466
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700467 // Print the key-value store.
468 {
469 os << "KEY VALUE STORE:\n";
470 size_t index = 0;
471 const char* key;
472 const char* value;
473 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
474 os << key << " = " << value << "\n";
475 index++;
476 }
477 os << "\n";
478 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700479
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700480 if (options_->absolute_addresses_) {
481 os << "BEGIN:\n";
482 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700483
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700484 os << "END:\n";
485 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
486 }
487
488 os << "SIZE:\n";
489 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700490
491 os << std::flush;
492
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800493 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
494 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700495 CHECK(oat_dex_file != nullptr);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700496 if (!DumpOatDexFile(os, *oat_dex_file)) {
497 success = false;
498 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700499 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700500 os << std::flush;
501 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700502 }
503
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800504 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700505 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
506 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800507 return 0; // Address not in oat file
508 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800509 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
510 reinterpret_cast<uintptr_t>(oat_file_.Begin());
511 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800512 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800513 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800514 return end_offset - begin_offset;
515 }
516
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700517 InstructionSet GetInstructionSet() {
518 return oat_file_.GetOatHeader().GetInstructionSet();
519 }
520
Ian Rogersef7d42f2014-01-06 12:55:46 -0800521 const void* GetQuickOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800522 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
523 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700524 CHECK(oat_dex_file != nullptr);
525 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700526 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700527 if (dex_file.get() == nullptr) {
528 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
529 << "': " << error_msg;
530 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800531 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700532 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800533 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700534 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700535 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100536 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800537 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100538 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800539 }
540 }
541 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700542 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800543 }
544
Brian Carlstromaded5f72011-10-07 17:15:04 -0700545 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800546 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800547 // We don't know the length of the code for each method, but we need to know where to stop
548 // when disassembling. What we do know is that a region of code will be followed by some other
549 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
550 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800551 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
552 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700553 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700554 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700555 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700556 if (dex_file.get() == nullptr) {
557 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
558 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800559 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800560 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800561 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800562 for (size_t class_def_index = 0;
563 class_def_index < dex_file->NumClassDefs();
564 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800565 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100566 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700567 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700568 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800569 ClassDataItemIterator it(*dex_file, class_data);
570 SkipAllFields(it);
571 uint32_t class_method_index = 0;
572 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100573 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800574 it.Next();
575 }
576 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100577 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800578 it.Next();
579 }
580 }
581 }
582 }
583
584 // If the last thing in the file is code for a method, there won't be an offset for the "next"
585 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
586 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800587 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800588 }
589
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700590 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
591 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
592 }
593
Elliott Hughese3c845c2012-02-28 17:23:01 -0800594 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800595 uint32_t code_offset = oat_method.GetCodeOffset();
596 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
597 code_offset &= ~0x1;
598 }
599 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800600 offsets_.insert(oat_method.GetMappingTableOffset());
601 offsets_.insert(oat_method.GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700602 offsets_.insert(oat_method.GetNativeGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800603 }
604
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700605 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
606 bool success = true;
607 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800608 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800609 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700610
611 // Create the verifier early.
612
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700613 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700614 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700615 if (dex_file.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700616 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700617 os << std::flush;
618 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700619 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800620 for (size_t class_def_index = 0;
621 class_def_index < dex_file->NumClassDefs();
622 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700623 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
624 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700625 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100626 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700627 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
628 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100629 << " (" << oat_class.GetStatus() << ")"
630 << " (" << oat_class.GetType() << ")\n";
631 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800632 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
633 std::ostream indented_os(&indent_filter);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700634 if (!DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def)) {
635 success = false;
636 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700637 }
638
639 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700640 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700641 }
642
Elliott Hughese3c845c2012-02-28 17:23:01 -0800643 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700644 while (it.HasNextStaticField()) {
645 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700646 }
Ian Rogers0571d352011-11-03 19:51:38 -0700647 while (it.HasNextInstanceField()) {
648 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700649 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800650 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700651
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700652 bool DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800653 const DexFile::ClassDef& class_def) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700654 bool success = true;
Ian Rogers13735952014-10-08 12:43:28 -0700655 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700656 if (class_data == nullptr) { // empty class such as a marker interface?
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700657 os << std::flush;
658 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800659 }
660 ClassDataItemIterator it(dex_file, class_data);
661 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700662 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700663 while (it.HasNextDirectMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700664 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
665 it.GetMemberIndex(), it.GetMethodCodeItem(),
666 it.GetRawMemberAccessFlags())) {
667 success = false;
668 }
669 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700670 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700671 }
Ian Rogers0571d352011-11-03 19:51:38 -0700672 while (it.HasNextVirtualMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700673 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
674 it.GetMemberIndex(), it.GetMethodCodeItem(),
675 it.GetRawMemberAccessFlags())) {
676 success = false;
677 }
678 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700679 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700680 }
Ian Rogers0571d352011-11-03 19:51:38 -0700681 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700682 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700683 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700684 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800685
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700686 static constexpr uint32_t kPrologueBytes = 16;
687
688 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
689 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
690
691 bool DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700692 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700693 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800694 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
695 uint32_t method_access_flags) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700696 bool success = true;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000697 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
698 if (pretty_method.find(options_->method_filter_) == std::string::npos) {
699 return success;
700 }
701
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800702 os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000703 class_method_index, pretty_method.c_str(),
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700704 dex_method_idx);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800705 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700706 std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter));
707 Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count);
708 std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter));
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800709 {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700710 *indent1_os << "DEX CODE:\n";
711 DumpDexCode(*indent2_os, dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700712 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700713
714 std::unique_ptr<verifier::MethodVerifier> verifier;
715 if (Runtime::Current() != nullptr) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700716 *indent1_os << "VERIFIER TYPE ANALYSIS:\n";
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700717 verifier.reset(DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item,
718 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700719 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700720
721 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
722 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
723 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800724 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700725 *indent1_os << "OatMethodOffsets ";
726 if (options_->absolute_addresses_) {
727 *indent1_os << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100728 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700729 *indent1_os << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
730 if (oat_method_offsets_offset > oat_file_.Size()) {
731 *indent1_os << StringPrintf(
732 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
733 oat_method_offsets_offset, oat_file_.Size());
734 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
735 os << std::flush;
736 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800737 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700738
739 uint32_t code_offset = oat_method.GetCodeOffset();
740 *indent2_os << StringPrintf("code_offset: 0x%08x ", code_offset);
741 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
742 if (aligned_code_begin > oat_file_.Size()) {
743 *indent2_os << StringPrintf("WARNING: "
744 "code offset 0x%08x is past end of file 0x%08zx.\n",
745 aligned_code_begin, oat_file_.Size());
746 success = false;
747 }
748 *indent2_os << "\n";
749
750 *indent2_os << "gc_map: ";
751 if (options_->absolute_addresses_) {
752 *indent2_os << StringPrintf("%p ", oat_method.GetNativeGcMap());
753 }
754 uint32_t gc_map_offset = oat_method.GetNativeGcMapOffset();
755 *indent2_os << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
756 if (gc_map_offset > oat_file_.Size()) {
757 *indent2_os << StringPrintf("WARNING: "
758 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
759 gc_map_offset, oat_file_.Size());
760 success = false;
761 } else if (options_->dump_raw_gc_map_) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700762 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800763 std::ostream indent3_os(&indent3_filter);
764 DumpGcMap(indent3_os, oat_method, code_item);
765 }
766 }
767 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700768 *indent1_os << "OatQuickMethodHeader ";
769 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
770 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700771
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700772 if (options_->absolute_addresses_) {
773 *indent1_os << StringPrintf("%p ", method_header);
774 }
775 *indent1_os << StringPrintf("(offset=0x%08x)\n", method_header_offset);
776 if (method_header_offset > oat_file_.Size()) {
777 *indent1_os << StringPrintf(
778 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
779 method_header_offset, oat_file_.Size());
780 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
781 os << std::flush;
782 return false;
783 }
784
785 *indent2_os << "mapping_table: ";
786 if (options_->absolute_addresses_) {
787 *indent2_os << StringPrintf("%p ", oat_method.GetMappingTable());
788 }
789 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
790 *indent2_os << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
791 if (mapping_table_offset > oat_file_.Size()) {
792 *indent2_os << StringPrintf("WARNING: "
793 "mapping table offset 0x%08x is past end of file 0x%08zx. "
794 "mapping table offset was loaded from offset 0x%08x.\n",
795 mapping_table_offset, oat_file_.Size(),
796 oat_method.GetMappingTableOffsetOffset());
797 success = false;
798 } else if (options_->dump_raw_mapping_table_) {
799 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
800 std::ostream indent3_os(&indent3_filter);
801 DumpMappingTable(indent3_os, oat_method);
802 }
803
804 *indent2_os << "vmap_table: ";
805 if (options_->absolute_addresses_) {
806 *indent2_os << StringPrintf("%p ", oat_method.GetVmapTable());
807 }
808 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
809 *indent2_os << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
810 if (vmap_table_offset > oat_file_.Size()) {
811 *indent2_os << StringPrintf("WARNING: "
812 "vmap table offset 0x%08x is past end of file 0x%08zx. "
813 "vmap table offset was loaded from offset 0x%08x.\n",
814 vmap_table_offset, oat_file_.Size(),
815 oat_method.GetVmapTableOffsetOffset());
816 success = false;
817 } else if (options_->dump_vmap_) {
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +0100818 DumpVmap(*indent2_os, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700819 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800820 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700821 {
822 *indent1_os << "QuickMethodFrameInfo\n";
823
824 *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
825 *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
826 DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false);
827 *indent2_os << "\n";
828 *indent2_os << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
829 DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true);
830 *indent2_os << "\n";
831 }
832 {
833 // Based on spill masks from QuickMethodFrameInfo so placed
834 // after it is dumped, but useful for understanding quick
835 // code, so dumped here.
836 DumpVregLocations(*indent2_os, oat_method, code_item);
837 }
838 {
839 *indent1_os << "CODE: ";
840 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
841 if (code_size_offset > oat_file_.Size()) {
842 *indent2_os << StringPrintf("WARNING: "
843 "code size offset 0x%08x is past end of file 0x%08zx.",
844 code_size_offset, oat_file_.Size());
845 success = false;
846 } else {
847 const void* code = oat_method.GetQuickCode();
848 uint32_t code_size = oat_method.GetQuickCodeSize();
849 if (code == nullptr) {
850 code = oat_method.GetPortableCode();
851 code_size = oat_method.GetPortableCodeSize();
852 code_size_offset = 0;
853 }
854 uint32_t code_offset = oat_method.GetCodeOffset();
855 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
856 uint64_t aligned_code_end = aligned_code_begin + code_size;
857
858 if (options_->absolute_addresses_) {
859 *indent1_os << StringPrintf("%p ", code);
860 }
861 *indent1_os << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
862 code_offset,
863 code_size_offset,
864 code_size,
865 code != nullptr ? "..." : "");
866
867 if (aligned_code_begin > oat_file_.Size()) {
868 *indent2_os << StringPrintf("WARNING: "
869 "start of code at 0x%08x is past end of file 0x%08zx.",
870 aligned_code_begin, oat_file_.Size());
871 success = false;
872 } else if (aligned_code_end > oat_file_.Size()) {
873 *indent2_os << StringPrintf("WARNING: "
874 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
875 "code size is 0x%08x loaded from offset 0x%08x.\n",
876 aligned_code_end, oat_file_.Size(),
877 code_size, code_size_offset);
878 success = false;
879 if (options_->disassemble_code_) {
880 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
881 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
882 }
883 }
884 } else if (code_size > kMaxCodeSize) {
885 *indent2_os << StringPrintf("WARNING: "
886 "code size %d is bigger than max expected threshold of %d. "
887 "code size is 0x%08x loaded from offset 0x%08x.\n",
888 code_size, kMaxCodeSize,
889 code_size, code_size_offset);
890 success = false;
891 if (options_->disassemble_code_) {
892 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
893 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
894 }
895 }
896 } else if (options_->disassemble_code_) {
897 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, !success, 0);
898 }
899 }
900 }
901 os << std::flush;
902 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700903 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800904
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800905 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
906 if (spill_mask == 0) {
907 return;
908 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800909 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800910 for (size_t i = 0; i < 32; i++) {
911 if ((spill_mask & (1 << i)) != 0) {
912 if (is_float) {
913 os << "fr" << i;
914 } else {
915 os << "r" << i;
916 }
917 spill_mask ^= 1 << i; // clear bit
918 if (spill_mask != 0) {
919 os << ", ";
920 } else {
921 break;
922 }
923 }
924 }
925 os << ")";
926 }
927
Ian Rogersb23a7722012-10-09 16:54:26 -0700928 void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) {
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +0100929 // If the native GC map is null, then this method has been compiled with the
930 // optimizing compiler. The optimizing compiler currently outputs its stack map
931 // in the vmap table, and the code below does not work with such a stack map.
932 if (oat_method.GetNativeGcMap() == nullptr) {
933 return;
934 }
Ian Rogers1809a722013-08-09 22:05:32 -0700935 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700936 if (raw_table != nullptr) {
Ian Rogers1809a722013-08-09 22:05:32 -0700937 const VmapTable vmap_table(raw_table);
938 bool first = true;
939 bool processing_fp = false;
940 uint32_t spill_mask = oat_method.GetCoreSpillMask();
941 for (size_t i = 0; i < vmap_table.Size(); i++) {
942 uint16_t dex_reg = vmap_table[i];
943 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
944 processing_fp ? kFloatVReg : kIntVReg);
945 os << (first ? "v" : ", v") << dex_reg;
946 if (!processing_fp) {
947 os << "/r" << cpu_reg;
948 } else {
949 os << "/fr" << cpu_reg;
950 }
951 first = false;
952 if (!processing_fp && dex_reg == 0xFFFF) {
953 processing_fp = true;
954 spill_mask = oat_method.GetFpSpillMask();
955 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800956 }
Ian Rogers1809a722013-08-09 22:05:32 -0700957 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800958 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800959 }
960
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -0700961 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
962 const DexFile::CodeItem* code_item) {
963 if (code_item != nullptr) {
964 size_t num_locals_ins = code_item->registers_size_;
965 size_t num_ins = code_item->ins_size_;
966 size_t num_locals = num_locals_ins - num_ins;
967 size_t num_outs = code_item->outs_size_;
968
969 os << "vr_stack_locations:";
970 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
971 // For readability, delimit the different kinds of VRs.
972 if (reg == num_locals_ins) {
973 os << "\n\tmethod*:";
974 } else if (reg == num_locals && num_ins > 0) {
975 os << "\n\tins:";
976 } else if (reg == 0 && num_locals > 0) {
977 os << "\n\tlocals:";
978 }
979
980 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
981 oat_method.GetFpSpillMask(),
982 oat_method.GetFrameSizeInBytes(), reg,
983 GetInstructionSet());
984 os << " v" << reg << "[sp + #" << offset << "]";
985 }
986
987 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
988 if (out_reg == 0) {
989 os << "\n\touts:";
990 }
991
992 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
993 os << " v" << out_reg << "[sp + #" << offset << "]";
994 }
995
996 os << "\n";
997 }
998 }
999
Ian Rogersb23a7722012-10-09 16:54:26 -07001000 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001001 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001002 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001003 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001004 const VmapTable vmap_table(raw_table);
1005 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001006 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001007 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1008 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1009 : oat_method.GetCoreSpillMask();
1010 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001011 } else {
1012 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
1013 oat_method.GetFpSpillMask(),
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001014 oat_method.GetFrameSizeInBytes(), reg,
1015 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001016 os << "[sp + #" << offset << "]";
1017 }
1018 }
1019 }
1020
Ian Rogersef7d42f2014-01-06 12:55:46 -08001021 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1022 const DexFile::CodeItem* code_item,
1023 size_t num_regs, const uint8_t* reg_bitmap) {
1024 bool first = true;
1025 for (size_t reg = 0; reg < num_regs; reg++) {
1026 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1027 if (first) {
1028 os << " v" << reg << " (";
1029 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1030 os << ")";
1031 first = false;
1032 } else {
1033 os << ", v" << reg << " (";
1034 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1035 os << ")";
1036 }
1037 }
1038 }
1039 if (first) {
1040 os << "No registers in GC map\n";
1041 } else {
1042 os << "\n";
1043 }
1044 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001045 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1046 const DexFile::CodeItem* code_item) {
1047 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001048 if (gc_map_raw == nullptr) {
1049 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001050 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001051 const void* quick_code = oat_method.GetQuickCode();
1052 if (quick_code != nullptr) {
1053 NativePcOffsetToReferenceMap map(gc_map_raw);
1054 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1055 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1056 map.GetNativePcOffset(entry);
1057 os << StringPrintf("%p", native_pc);
1058 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001059 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001060 } else {
1061 const void* portable_code = oat_method.GetPortableCode();
1062 CHECK(portable_code != nullptr);
1063 verifier::DexPcToReferenceMap map(gc_map_raw);
1064 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1065 uint32_t dex_pc = map.GetDexPc(entry);
1066 os << StringPrintf("0x%08x", dex_pc);
1067 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
1068 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001069 }
1070 }
1071
1072 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001073 const void* quick_code = oat_method.GetQuickCode();
1074 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001075 return;
1076 }
Ian Rogers1809a722013-08-09 22:05:32 -07001077 MappingTable table(oat_method.GetMappingTable());
1078 if (table.TotalSize() != 0) {
1079 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1080 std::ostream indent_os(&indent_filter);
1081 if (table.PcToDexSize() != 0) {
1082 typedef MappingTable::PcToDexIterator It;
1083 os << "suspend point mappings {\n";
1084 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1085 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1086 }
1087 os << "}\n";
1088 }
1089 if (table.DexToPcSize() != 0) {
1090 typedef MappingTable::DexToPcIterator It;
1091 os << "catch entry mappings {\n";
1092 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1093 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1094 }
1095 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001096 }
1097 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001098 }
1099
Ian Rogers1809a722013-08-09 22:05:32 -07001100 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1101 size_t offset, bool suspend_point_mapping) {
1102 MappingTable table(oat_method.GetMappingTable());
1103 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1104 typedef MappingTable::PcToDexIterator It;
1105 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1106 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001107 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001108 return cur.DexPc();
1109 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001110 }
Ian Rogers1809a722013-08-09 22:05:32 -07001111 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1112 typedef MappingTable::DexToPcIterator It;
1113 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1114 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001115 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001116 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001117 }
1118 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001119 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001120 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001121 }
1122
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001123 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1124 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001125 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001126 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001127 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001128 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001129 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001130 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001131 bool first = true;
1132 for (size_t reg = 0; reg < num_regs; reg++) {
1133 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1134 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001135 os << "GC map objects: v" << reg << " (";
1136 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001137 os << ")";
1138 first = false;
1139 } else {
1140 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001141 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001142 os << ")";
1143 }
1144 }
1145 }
1146 if (!first) {
1147 os << "\n";
1148 }
1149 }
1150 }
1151 }
1152
Mathieu Chartier590fee92013-09-13 13:46:47 -07001153 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1154 const OatFile::OatMethod& oat_method,
1155 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1156 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001157 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001158 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001159 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1160 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1161 if (kind != kUndefined) {
1162 if (first) {
1163 os << "VRegs: v";
1164 first = false;
1165 } else {
1166 os << ", v";
1167 }
1168 os << reg << " (";
1169 switch (kind) {
1170 case kImpreciseConstant:
1171 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1172 DescribeVReg(os, oat_method, code_item, reg, kind);
1173 break;
1174 case kConstant:
1175 os << "Constant: " << kinds.at((reg * 2) + 1);
1176 break;
1177 default:
1178 DescribeVReg(os, oat_method, code_item, reg, kind);
1179 break;
1180 }
1181 os << ")";
1182 }
1183 }
1184 if (!first) {
1185 os << "\n";
1186 }
1187 }
1188
1189
Ian Rogersb23a7722012-10-09 16:54:26 -07001190 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001191 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001192 size_t i = 0;
1193 while (i < code_item->insns_size_in_code_units_) {
1194 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001195 os << StringPrintf("0x%04zx: %s\n", i, instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001196 i += instruction->SizeInCodeUnits();
1197 }
1198 }
1199 }
1200
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001201 verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx,
1202 const DexFile* dex_file,
1203 const DexFile::ClassDef& class_def,
1204 const DexFile::CodeItem* code_item,
1205 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001206 if ((method_access_flags & kAccNative) == 0) {
1207 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001208 StackHandleScope<1> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001209 Handle<mirror::DexCache> dex_cache(
1210 hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
Andreas Gampe00b25f32014-09-17 21:49:05 -07001211 DCHECK(options_->class_loader_ != nullptr);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001212 return verifier::MethodVerifier::VerifyMethodAndDump(soa.Self(), os, dex_method_idx, dex_file,
1213 dex_cache,
Andreas Gampe00b25f32014-09-17 21:49:05 -07001214 *options_->class_loader_,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001215 &class_def, code_item,
1216 NullHandle<mirror::ArtMethod>(),
1217 method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001218 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001219
1220 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001221 }
1222
Mathieu Chartier590fee92013-09-13 13:46:47 -07001223 void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001224 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1225 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001226 const void* portable_code = oat_method.GetPortableCode();
1227 const void* quick_code = oat_method.GetQuickCode();
1228
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001229 if (code_size == 0) {
1230 code_size = oat_method.GetQuickCodeSize();
1231 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001232 if ((code_size == 0) || ((portable_code == nullptr) && (quick_code == nullptr))) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001233 os << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001234 return;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001235 } else if (quick_code != nullptr) {
1236 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1237 size_t offset = 0;
1238 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001239 if (!bad_input) {
1240 DumpMappingAtOffset(os, oat_method, offset, false);
1241 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001242 offset += disassembler_->Dump(os, quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001243 if (!bad_input) {
1244 uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
1245 if (dex_pc != DexFile::kDexNoIndex) {
1246 DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
1247 if (verifier != nullptr) {
1248 DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
1249 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001250 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001251 }
1252 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001253 } else {
1254 CHECK(portable_code != nullptr);
1255 CHECK_EQ(code_size, 0U); // TODO: disassembly of portable is currently not supported.
Ian Rogersb23a7722012-10-09 16:54:26 -07001256 }
1257 }
1258
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001259 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001260 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
1261 const OatDumperOptions* options_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001262 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001263 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001264};
1265
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001266class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001267 public:
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001268 explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001269 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
1270 : os_(os),
1271 image_space_(image_space),
1272 image_header_(image_header),
1273 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001274
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001275 bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001276 std::ostream& os = *os_;
1277 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001278
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001279 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001280
Mathieu Chartier31e89252013-08-28 11:29:12 -07001281 os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
1282 << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
1283
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001284 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001285
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001286 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001287
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001288 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1289
1290 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1291
1292 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001293
Alex Lighta59dd802014-07-02 16:28:08 -07001294 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1295
Igor Murashkin46774762014-10-22 11:37:02 -07001296 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1297
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001298 {
1299 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
1300 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1301 std::ostream indent1_os(&indent1_filter);
1302 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
1303 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1304 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1305 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001306 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001307 indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
1308 if (image_root_object->IsObjectArray()) {
1309 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
1310 std::ostream indent2_os(&indent2_filter);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001311 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001312 = image_root_object->AsObjectArray<mirror::Object>();
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001313 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1314 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001315 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001316 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1317 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001318 run++;
1319 } else {
1320 break;
1321 }
1322 }
1323 if (run == 0) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001324 indent2_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001325 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001326 indent2_os << StringPrintf("%d to %zd: ", j, j + run);
1327 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001328 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001329 if (value != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001330 PrettyObjectValue(indent2_os, value->GetClass(), value);
1331 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001332 indent2_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001333 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001334 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001335 }
1336 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001337 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001338 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001339
Brian Carlstromaded5f72011-10-07 17:15:04 -07001340 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001341 std::string image_filename = image_space_.GetImageFilename();
1342 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001343 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001344 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001345 std::string error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -07001346 const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1347 if (oat_file == nullptr) {
Igor Murashkin46774762014-10-22 11:37:02 -07001348 oat_file = OatFile::Open(oat_location, oat_location, nullptr, nullptr, false, &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001349 if (oat_file == nullptr) {
1350 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001351 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001352 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001353 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001354 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001355
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001356 stats_.oat_file_bytes = oat_file->Size();
1357
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001358 oat_dumper_.reset(new OatDumper(*oat_file, oat_dumper_options_.release()));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001359
Mathieu Chartier02e25112013-08-14 16:14:24 -07001360 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001361 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001362 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1363 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001364 }
1365
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001366 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001367
1368 // Loop through all the image spaces and dump their objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001369 gc::Heap* heap = Runtime::Current()->GetHeap();
1370 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07001371 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001372 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001373 {
1374 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1375 heap->FlushAllocStack();
1376 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001377 // Since FlushAllocStack() above resets the (active) allocation
1378 // stack. Need to revoke the thread-local allocation stacks that
1379 // point into it.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001380 {
1381 self->TransitionFromRunnableToSuspended(kNative);
1382 ThreadList* thread_list = Runtime::Current()->GetThreadList();
1383 thread_list->SuspendAll();
1384 heap->RevokeAllThreadLocalAllocationStacks(self);
1385 thread_list->ResumeAll();
1386 self->TransitionFromSuspendedToRunnable();
1387 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001388 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001389 {
1390 std::ostream* saved_os = os_;
1391 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1392 std::ostream indent_os(&indent_filter);
1393 os_ = &indent_os;
1394 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001395 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001396 if (space->IsImageSpace()) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001397 gc::space::ImageSpace* image_space = space->AsImageSpace();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001398 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1399 indent_os << "\n";
1400 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001401 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001402 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001403 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001404 indent_os << "\n";
1405 os_ = saved_os;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001406 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001407 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001408 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001409 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001410 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001411 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001412 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001413 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001414 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001415 size_t header_bytes = sizeof(ImageHeader);
1416 stats_.header_bytes = header_bytes;
1417 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1418 stats_.alignment_bytes += alignment_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001419 stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
1420 stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001421 stats_.Dump(os);
1422 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001423
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001424 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001425
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001426 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001427 }
1428
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001429 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001430 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001431 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001432 CHECK(type != nullptr);
1433 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001434 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001435 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001436 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001437 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001438 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001439 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001440 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001441 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001442 } else if (type->IsArtFieldClass()) {
1443 mirror::ArtField* field = value->AsArtField();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001444 os << StringPrintf("%p Field: %s\n", field, PrettyField(field).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001445 } else if (type->IsArtMethodClass()) {
1446 mirror::ArtMethod* method = value->AsArtMethod();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001447 os << StringPrintf("%p Method: %s\n", method, PrettyMethod(method).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001448 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001449 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001450 }
1451 }
1452
Brian Carlstromea46f952013-07-30 01:26:50 -07001453 static void PrintField(std::ostream& os, mirror::ArtField* field, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001454 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001455 const char* descriptor = field->GetTypeDescriptor();
1456 os << StringPrintf("%s: ", field->GetName());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001457 if (descriptor[0] != 'L' && descriptor[0] != '[') {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001458 StackHandleScope<1> hs(Thread::Current());
1459 FieldHelper fh(hs.NewHandle(field));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001460 mirror::Class* type = fh.GetType();
Fred Shih37f05ef2014-07-16 18:38:08 -07001461 DCHECK(type->IsPrimitive());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001462 if (type->IsPrimitiveLong()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001463 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -07001464 } else if (type->IsPrimitiveDouble()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001465 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -07001466 } else if (type->IsPrimitiveFloat()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001467 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Fred Shih37f05ef2014-07-16 18:38:08 -07001468 } else if (type->IsPrimitiveInt()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001469 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Fred Shih37f05ef2014-07-16 18:38:08 -07001470 } else if (type->IsPrimitiveChar()) {
1471 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
1472 } else if (type->IsPrimitiveShort()) {
1473 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
1474 } else if (type->IsPrimitiveBoolean()) {
1475 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1476 field->GetBoolean(obj));
1477 } else if (type->IsPrimitiveByte()) {
1478 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
1479 } else {
1480 LOG(FATAL) << "Unknown type: " << PrettyClass(type);
Ian Rogers48efc2b2012-08-27 17:20:31 -07001481 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001482 } else {
Ian Rogers48efc2b2012-08-27 17:20:31 -07001483 // Get the value, don't compute the type unless it is non-null as we don't want
1484 // to cause class loading.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001485 mirror::Object* value = field->GetObj(obj);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001486 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001487 os << StringPrintf("null %s\n", PrettyDescriptor(descriptor).c_str());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001488 } else {
Ian Rogers50239c72013-06-17 14:53:22 -07001489 // Grab the field type without causing resolution.
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001490 StackHandleScope<1> hs(Thread::Current());
1491 FieldHelper fh(hs.NewHandle(field));
Ian Rogers50239c72013-06-17 14:53:22 -07001492 mirror::Class* field_type = fh.GetType(false);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001493 if (field_type != nullptr) {
Ian Rogers50239c72013-06-17 14:53:22 -07001494 PrettyObjectValue(os, field_type, value);
1495 } else {
1496 os << StringPrintf("%p %s\n", value, PrettyDescriptor(descriptor).c_str());
1497 }
Ian Rogers48efc2b2012-08-27 17:20:31 -07001498 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001499 }
1500 }
1501
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001502 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001503 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001504 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001505 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001506 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001507 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001508 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001509 if (fields != nullptr) {
Ian Rogersd5b32602012-02-26 16:40:04 -08001510 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001511 mirror::ArtField* field = fields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001512 PrintField(os, field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001513 }
1514 }
1515 }
1516
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001517 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001518 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001519 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001520
Ian Rogersef7d42f2014-01-06 12:55:46 -08001521 const void* GetQuickOatCodeBegin(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001522 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001523 const void* quick_code = m->GetEntryPointFromQuickCompiledCode();
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001524 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001525 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001526 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001527 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001528 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001529 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001530 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001531 }
1532
Ian Rogersef7d42f2014-01-06 12:55:46 -08001533 uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001534 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001535 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1536 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001537 return 0;
1538 }
1539 return oat_code_begin[-1];
1540 }
1541
Ian Rogersef7d42f2014-01-06 12:55:46 -08001542 const void* GetQuickOatCodeEnd(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001543 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001544 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001545 if (oat_code_begin == nullptr) {
1546 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001547 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001548 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001549 }
1550
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001551 static void Callback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001552 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001553 DCHECK(obj != nullptr);
1554 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001555 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001556 if (!state->InDumpSpace(obj)) {
1557 return;
1558 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001559
1560 size_t object_bytes = obj->SizeOf();
1561 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1562 state->stats_.object_bytes += object_bytes;
1563 state->stats_.alignment_bytes += alignment_bytes;
1564
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001565 std::ostream& os = *state->os_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001566 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001567 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001568 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1569 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001570 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001571 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001572 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1573 << klass->GetStatus() << ")\n";
Brian Carlstromea46f952013-07-30 01:26:50 -07001574 } else if (obj->IsArtField()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001575 os << StringPrintf("%p: java.lang.reflect.ArtField %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001576 PrettyField(obj->AsArtField()).c_str());
1577 } else if (obj->IsArtMethod()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001578 os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001579 PrettyMethod(obj->AsArtMethod()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001580 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001581 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001582 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001583 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001584 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001585 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001586 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1587 std::ostream indent_os(&indent_filter);
1588 DumpFields(indent_os, obj, obj_class);
Ian Rogersd5b32602012-02-26 16:40:04 -08001589 if (obj->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001590 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Ian Rogersd5b32602012-02-26 16:40:04 -08001591 int32_t length = obj_array->GetLength();
1592 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001593 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001594 size_t run = 0;
1595 for (int32_t j = i + 1; j < length; j++) {
1596 if (value == obj_array->Get(j)) {
1597 run++;
1598 } else {
1599 break;
1600 }
1601 }
1602 if (run == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001603 indent_os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001604 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001605 indent_os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001606 i = i + run;
1607 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001608 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001609 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001610 PrettyObjectValue(indent_os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001611 }
1612 } else if (obj->IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001613 mirror::ObjectArray<mirror::ArtField>* sfields = obj->AsClass()->GetSFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001614 if (sfields != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001615 indent_os << "STATICS:\n";
1616 Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
1617 std::ostream indent2_os(&indent2_filter);
Ian Rogersd5b32602012-02-26 16:40:04 -08001618 for (int32_t i = 0; i < sfields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001619 mirror::ArtField* field = sfields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001620 PrintField(indent2_os, field, field->GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001621 }
1622 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001623 } else if (obj->IsArtMethod()) {
1624 mirror::ArtMethod* method = obj->AsArtMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001625 if (method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001626 // TODO: portable dumping.
1627 DCHECK(method->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1628 DCHECK(method->GetMappingTable() == nullptr) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001629 bool first_occurrence;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001630 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1631 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1632 state->ComputeOatSize(quick_oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001633 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001634 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001635 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001636 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCode()) {
1637 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001638 }
Ian Rogers19846512012-02-24 11:42:47 -08001639 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
Jeff Hao88474b42013-10-23 16:24:40 -07001640 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001641 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001642 DCHECK(method->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1643 DCHECK(method->GetMappingTable() == nullptr) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001644 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001645 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -07001646 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001647 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001648
Elliott Hughesa0e18062012-04-13 15:59:59 -07001649 bool first_occurrence;
Ian Rogers0c7abda2012-09-19 13:33:42 -07001650 size_t gc_map_bytes = state->ComputeOatSize(method->GetNativeGcMap(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001651 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001652 state->stats_.gc_map_bytes += gc_map_bytes;
1653 }
1654
1655 size_t pc_mapping_table_bytes =
Ian Rogers1809a722013-08-09 22:05:32 -07001656 state->ComputeOatSize(method->GetMappingTable(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001657 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001658 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1659 }
1660
1661 size_t vmap_table_bytes =
Ian Rogers1809a722013-08-09 22:05:32 -07001662 state->ComputeOatSize(method->GetVmapTable(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001663 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001664 state->stats_.vmap_table_bytes += vmap_table_bytes;
1665 }
1666
Ian Rogersef7d42f2014-01-06 12:55:46 -08001667 // TODO: portable dumping.
1668 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1669 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1670 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1671 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001672 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001673 state->stats_.managed_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001674 if (method->IsConstructor()) {
1675 if (method->IsStatic()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001676 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001677 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001678 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001679 }
1680 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001681 state->stats_.large_method_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001682 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001683 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001684 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001685
Ian Rogersef7d42f2014-01-06 12:55:46 -08001686 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001687 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
1688 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001689
1690 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersef7d42f2014-01-06 12:55:46 -08001691 vmap_table_bytes + quick_oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001692
1693 double expansion =
Ian Rogersef7d42f2014-01-06 12:55:46 -08001694 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001695 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001696 }
1697 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001698 std::string temp;
1699 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001700 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001701
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001702 std::set<const void*> already_seen_;
1703 // Compute the size of the given data within the oat file and whether this is the first time
1704 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001705 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001706 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001707 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001708 already_seen_.insert(oat_data);
1709 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001710 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001711 }
1712 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001713 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001714
1715 public:
1716 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001717 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001718 size_t file_bytes;
1719
1720 size_t header_bytes;
1721 size_t object_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001722 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001723 size_t alignment_bytes;
1724
1725 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001726 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001727 size_t managed_to_native_code_bytes;
1728 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001729 size_t class_initializer_code_bytes;
1730 size_t large_initializer_code_bytes;
1731 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001732
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001733 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001734 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001735 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001736
1737 size_t dex_instruction_bytes;
1738
Brian Carlstromea46f952013-07-30 01:26:50 -07001739 std::vector<mirror::ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001740 std::vector<size_t> method_outlier_size;
1741 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07001742 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001743
1744 explicit Stats()
1745 : oat_file_bytes(0),
1746 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001747 header_bytes(0),
1748 object_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07001749 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001750 alignment_bytes(0),
1751 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001752 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001753 managed_to_native_code_bytes(0),
1754 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001755 class_initializer_code_bytes(0),
1756 large_initializer_code_bytes(0),
1757 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001758 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001759 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001760 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001761 dex_instruction_bytes(0) {}
1762
Elliott Hughesa0e18062012-04-13 15:59:59 -07001763 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001764 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07001765 size_t bytes;
1766 size_t count;
1767 };
1768 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1769 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001770
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001771 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001772 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1773 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001774 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001775 it->second.count += 1;
1776 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001777 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07001778 }
1779 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001780
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001781 double PercentOfOatBytes(size_t size) {
1782 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1783 }
1784
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001785 double PercentOfFileBytes(size_t size) {
1786 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1787 }
1788
1789 double PercentOfObjectBytes(size_t size) {
1790 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1791 }
1792
Brian Carlstromea46f952013-07-30 01:26:50 -07001793 void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001794 method_outlier_size.push_back(total_size);
1795 method_outlier_expansion.push_back(expansion);
1796 method_outlier.push_back(method);
1797 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001798
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001799 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001800 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001801 size_t sum_of_sizes = 0;
1802 size_t sum_of_sizes_squared = 0;
1803 size_t sum_of_expansion = 0;
1804 size_t sum_of_expansion_squared = 0;
1805 size_t n = method_outlier_size.size();
1806 for (size_t i = 0; i < n; i++) {
1807 size_t cur_size = method_outlier_size[i];
1808 sum_of_sizes += cur_size;
1809 sum_of_sizes_squared += cur_size * cur_size;
1810 double cur_expansion = method_outlier_expansion[i];
1811 sum_of_expansion += cur_expansion;
1812 sum_of_expansion_squared += cur_expansion * cur_expansion;
1813 }
1814 size_t size_mean = sum_of_sizes / n;
1815 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1816 double expansion_mean = sum_of_expansion / n;
1817 double expansion_variance =
1818 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1819
1820 // Dump methods whose size is a certain number of standard deviations from the mean
1821 size_t dumped_values = 0;
1822 size_t skipped_values = 0;
1823 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1824 size_t cur_size_variance = i * i * size_variance;
1825 bool first = true;
1826 for (size_t j = 0; j < n; j++) {
1827 size_t cur_size = method_outlier_size[j];
1828 if (cur_size > size_mean) {
1829 size_t cur_var = cur_size - size_mean;
1830 cur_var = cur_var * cur_var;
1831 if (cur_var > cur_size_variance) {
1832 if (dumped_values > 20) {
1833 if (i == 1) {
1834 skipped_values++;
1835 } else {
1836 i = 2; // jump to counting for 1 standard deviation
1837 break;
1838 }
1839 } else {
1840 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001841 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001842 first = false;
1843 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001844 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001845 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001846 method_outlier_size[j] = 0; // don't consider this method again
1847 dumped_values++;
1848 }
1849 }
1850 }
1851 }
1852 }
1853 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001854 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001855 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001856 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001857 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001858
1859 // Dump methods whose expansion is a certain number of standard deviations from the mean
1860 dumped_values = 0;
1861 skipped_values = 0;
1862 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1863 double cur_expansion_variance = i * i * expansion_variance;
1864 bool first = true;
1865 for (size_t j = 0; j < n; j++) {
1866 double cur_expansion = method_outlier_expansion[j];
1867 if (cur_expansion > expansion_mean) {
1868 size_t cur_var = cur_expansion - expansion_mean;
1869 cur_var = cur_var * cur_var;
1870 if (cur_var > cur_expansion_variance) {
1871 if (dumped_values > 20) {
1872 if (i == 1) {
1873 skipped_values++;
1874 } else {
1875 i = 2; // jump to counting for 1 standard deviation
1876 break;
1877 }
1878 } else {
1879 if (first) {
1880 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001881 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001882 first = false;
1883 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001884 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001885 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001886 method_outlier_expansion[j] = 0.0; // don't consider this method again
1887 dumped_values++;
1888 }
1889 }
1890 }
1891 }
1892 }
1893 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001894 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001895 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001896 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001897 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001898 }
1899
Ian Rogersb726dcb2012-09-05 08:57:23 -07001900 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001901 {
1902 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1903 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
1904 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1905 std::ostream indent_os(&indent_filter);
1906 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
1907 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier32327092013-08-30 14:04:08 -07001908 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001909 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
1910 header_bytes, PercentOfFileBytes(header_bytes),
1911 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07001912 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001913 alignment_bytes, PercentOfFileBytes(alignment_bytes))
1914 << std::flush;
Mathieu Chartier32327092013-08-30 14:04:08 -07001915 CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001916 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001917
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001918 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001919 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07001920 for (const auto& sizes_and_count : sizes_and_counts) {
1921 const std::string& descriptor(sizes_and_count.first);
1922 double average = static_cast<double>(sizes_and_count.second.bytes) /
1923 static_cast<double>(sizes_and_count.second.count);
1924 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001925 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001926 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001927 descriptor.c_str(), sizes_and_count.second.bytes,
1928 sizes_and_count.second.count, average, percent);
1929 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001930 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001931 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001932 CHECK_EQ(object_bytes, object_bytes_total);
1933
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001934 os << StringPrintf("oat_file_bytes = %8zd\n"
1935 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1936 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1937 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1938 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1939 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1940 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001941 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001942 managed_code_bytes,
1943 PercentOfOatBytes(managed_code_bytes),
1944 managed_to_native_code_bytes,
1945 PercentOfOatBytes(managed_to_native_code_bytes),
1946 native_to_managed_code_bytes,
1947 PercentOfOatBytes(native_to_managed_code_bytes),
1948 class_initializer_code_bytes,
1949 PercentOfOatBytes(class_initializer_code_bytes),
1950 large_initializer_code_bytes,
1951 PercentOfOatBytes(large_initializer_code_bytes),
1952 large_method_code_bytes,
1953 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001954 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07001955 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001956 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001957 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
1958 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07001959 }
1960
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001961 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1962 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1963 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001964 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1965 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1966 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001967 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001968
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001969 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
1970 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001971 static_cast<double>(managed_code_bytes) /
1972 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07001973 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001974 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001975 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001976
1977 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001978 }
1979 } stats_;
1980
1981 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001982 enum {
1983 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1984 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1985 kLargeConstructorDexBytes = 4000,
1986 // Number of bytes for a method to be considered large. Based on the 4000 basic block
1987 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1988 kLargeMethodDexBytes = 16000
1989 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001990 std::ostream* os_;
Ian Rogers1d54e732013-05-02 21:10:01 -07001991 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001992 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001993 std::unique_ptr<OatDumper> oat_dumper_;
1994 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001995
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001996 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001997};
1998
Andreas Gampe00b25f32014-09-17 21:49:05 -07001999static NoopCompilerCallbacks callbacks;
Elliott Hughes72395bf2012-04-24 13:45:26 -07002000
Andreas Gampe00b25f32014-09-17 21:49:05 -07002001static Runtime* StartRuntime(const char* boot_image_location, const char* image_location,
2002 InstructionSet instruction_set) {
Ian Rogerse63db272014-07-15 15:36:11 -07002003 RuntimeOptions options;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002004 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07002005 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002006 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07002007 std::string boot_oat_option;
Brian Carlstrom6c871802013-07-17 14:25:35 -07002008
2009 // We are more like a compiler than a run-time. We don't want to execute code.
Brian Carlstromc0a1b182014-03-04 23:19:06 -08002010 options.push_back(std::make_pair("compilercallbacks", &callbacks));
Brian Carlstrom6c871802013-07-17 14:25:35 -07002011
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002012 if (boot_image_location != nullptr) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -07002013 boot_image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002014 boot_image_option += boot_image_location;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002015 options.push_back(std::make_pair(boot_image_option.c_str(), nullptr));
Brian Carlstrom78128a62011-09-15 17:21:19 -07002016 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002017 if (image_location != nullptr) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07002018 image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002019 image_option += image_location;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002020 options.push_back(std::make_pair(image_option.c_str(), nullptr));
Brian Carlstromaded5f72011-10-07 17:15:04 -07002021 }
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002022 options.push_back(
2023 std::make_pair("imageinstructionset",
2024 reinterpret_cast<const void*>(GetInstructionSetString(instruction_set))));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07002025
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002026 if (!Runtime::Create(options, false)) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07002027 fprintf(stderr, "Failed to create runtime\n");
Andreas Gampe00b25f32014-09-17 21:49:05 -07002028 return nullptr;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002029 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002030
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002031 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
Ian Rogersb9beb2e2014-05-09 16:57:40 -07002032 // give it away now and then switch to a more manageable ScopedObjectAccess.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002033 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002034
2035 return Runtime::Current();
2036}
2037
2038static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
2039 std::ostream* os) {
2040 // Dumping the image, no explicit class loader.
2041 NullHandle<mirror::ClassLoader> null_class_loader;
2042 options->class_loader_ = &null_class_loader;
2043
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002044 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002045 gc::Heap* heap = runtime->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -07002046 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002047 CHECK(image_space != nullptr);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002048 const ImageHeader& image_header = image_space->GetImageHeader();
2049 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002050 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002051 return EXIT_FAILURE;
2052 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002053 ImageDumper image_dumper(os, *image_space, image_header, options);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002054 bool success = image_dumper.Dump();
2055 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002056}
2057
Andreas Gampe00b25f32014-09-17 21:49:05 -07002058static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2059 std::ostream* os) {
2060 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2061
2062 Thread* self = Thread::Current();
2063 CHECK(self != nullptr);
2064 // Need well-known-classes.
2065 WellKnownClasses::Init(self->GetJniEnv());
2066
2067 // Need to register dex files to get a working dex cache.
2068 ScopedObjectAccess soa(self);
2069 ClassLinker* class_linker = runtime->GetClassLinker();
2070 class_linker->RegisterOatFile(oat_file);
2071 std::vector<const DexFile*> dex_files;
2072 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2073 std::string error_msg;
2074 const DexFile* dex_file = odf->OpenDexFile(&error_msg);
2075 CHECK(dex_file != nullptr) << error_msg;
2076 class_linker->RegisterDexFile(*dex_file);
2077 dex_files.push_back(dex_file);
2078 }
2079
2080 // Need a class loader.
2081 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader);
2082 ScopedLocalRef<jobject> class_loader_local(soa.Env(),
2083 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
2084 jobject class_loader = soa.Env()->NewGlobalRef(class_loader_local.get());
2085 // Fake that we're a compiler.
2086 runtime->SetCompileTimeClassPath(class_loader, dex_files);
2087
2088 // Use the class loader while dumping.
2089 StackHandleScope<1> scope(self);
2090 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2091 soa.Decode<mirror::ClassLoader*>(class_loader));
2092 options->class_loader_ = &loader_handle;
2093
2094 OatDumper oat_dumper(*oat_file, options);
2095 bool success = oat_dumper.Dump(*os);
2096 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2097}
2098
2099static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
2100 // No image = no class loader.
2101 NullHandle<mirror::ClassLoader> null_class_loader;
2102 options->class_loader_ = &null_class_loader;
2103
2104 OatDumper oat_dumper(*oat_file, options);
2105 bool success = oat_dumper.Dump(*os);
2106 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2107}
2108
2109static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2110 std::ostream* os) {
2111 std::string error_msg;
Igor Murashkin46774762014-10-22 11:37:02 -07002112 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002113 if (oat_file == nullptr) {
2114 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2115 return EXIT_FAILURE;
2116 }
2117
2118 if (runtime != nullptr) {
2119 return DumpOatWithRuntime(runtime, oat_file, options, os);
2120 } else {
2121 return DumpOatWithoutRuntime(oat_file, options, os);
2122 }
2123}
2124
2125static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2126 std::string error_msg;
Igor Murashkin46774762014-10-22 11:37:02 -07002127 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002128 if (oat_file == nullptr) {
2129 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2130 return EXIT_FAILURE;
2131 }
2132
2133 OatSymbolizer oat_symbolizer(oat_file, output_name);
2134 if (!oat_symbolizer.Init()) {
2135 fprintf(stderr, "Failed to initialize symbolizer\n");
2136 return EXIT_FAILURE;
2137 }
2138 if (!oat_symbolizer.Symbolize()) {
2139 fprintf(stderr, "Failed to symbolize\n");
2140 return EXIT_FAILURE;
2141 }
2142
2143 return EXIT_SUCCESS;
2144}
2145
2146struct OatdumpArgs {
2147 bool Parse(int argc, char** argv) {
2148 // Skip over argv[0].
2149 argv++;
2150 argc--;
2151
2152 if (argc == 0) {
2153 fprintf(stderr, "No arguments specified\n");
2154 usage();
2155 return false;
2156 }
2157
2158 for (int i = 0; i < argc; i++) {
2159 const StringPiece option(argv[i]);
2160 if (option.starts_with("--oat-file=")) {
2161 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2162 } else if (option.starts_with("--image=")) {
2163 image_location_ = option.substr(strlen("--image=")).data();
2164 } else if (option.starts_with("--boot-image=")) {
2165 boot_image_location_ = option.substr(strlen("--boot-image=")).data();
2166 } else if (option.starts_with("--instruction-set=")) {
2167 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
2168 instruction_set_ = GetInstructionSetFromString(instruction_set_str.data());
2169 if (instruction_set_ == kNone) {
2170 fprintf(stderr, "Unsupported instruction set %s\n", instruction_set_str.data());
2171 usage();
2172 return false;
2173 }
2174 } else if (option =="--dump:raw_mapping_table") {
2175 dump_raw_mapping_table_ = true;
2176 } else if (option == "--dump:raw_gc_map") {
2177 dump_raw_gc_map_ = true;
2178 } else if (option == "--no-dump:vmap") {
2179 dump_vmap_ = false;
2180 } else if (option == "--no-disassemble") {
2181 disassemble_code_ = false;
2182 } else if (option.starts_with("--output=")) {
2183 output_name_ = option.substr(strlen("--output=")).ToString();
2184 const char* filename = output_name_.c_str();
2185 out_.reset(new std::ofstream(filename));
2186 if (!out_->good()) {
2187 fprintf(stderr, "Failed to open output filename %s\n", filename);
2188 usage();
2189 return false;
2190 }
2191 os_ = out_.get();
2192 } else if (option.starts_with("--symbolize=")) {
2193 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2194 symbolize_ = true;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002195 } else if (option.starts_with("--method-filter=")) {
2196 method_filter_ = option.substr(strlen("--method-filter=")).data();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002197 } else {
2198 fprintf(stderr, "Unknown argument %s\n", option.data());
2199 usage();
2200 return false;
2201 }
2202 }
2203
2204 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2205 fprintf(stderr, "Either --image or --oat must be specified\n");
2206 return false;
2207 }
2208
2209 if (image_location_ != nullptr && oat_filename_ != nullptr) {
2210 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
2211 return false;
2212 }
2213
2214 return true;
2215 }
2216
2217 const char* oat_filename_ = nullptr;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002218 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002219 const char* image_location_ = nullptr;
2220 const char* boot_image_location_ = nullptr;
2221 InstructionSet instruction_set_ = kRuntimeISA;
2222 std::string elf_filename_prefix_;
2223 std::ostream* os_ = &std::cout;
2224 std::unique_ptr<std::ofstream> out_;
2225 std::string output_name_;
2226 bool dump_raw_mapping_table_ = false;
2227 bool dump_raw_gc_map_ = false;
2228 bool dump_vmap_ = true;
2229 bool disassemble_code_ = true;
2230 bool symbolize_ = false;
2231};
2232
2233static int oatdump(int argc, char** argv) {
2234 InitLogging(argv);
2235
2236 OatdumpArgs args;
2237 if (!args.Parse(argc, argv)) {
2238 return EXIT_FAILURE;
2239 }
2240
2241 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2242 bool absolute_addresses = (args.oat_filename_ == nullptr);
2243
2244 std::unique_ptr<OatDumperOptions> oat_dumper_options(new OatDumperOptions(
2245 args.dump_raw_mapping_table_,
2246 args.dump_raw_gc_map_,
2247 args.dump_vmap_,
2248 args.disassemble_code_,
2249 absolute_addresses,
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002250 args.method_filter_,
Andreas Gampe00b25f32014-09-17 21:49:05 -07002251 nullptr));
2252
2253 std::unique_ptr<Runtime> runtime;
2254 if ((args.boot_image_location_ != nullptr || args.image_location_ != nullptr) &&
2255 !args.symbolize_) {
2256 // If we have a boot image option, try to start the runtime; except when just symbolizing.
2257 runtime.reset(StartRuntime(args.boot_image_location_,
2258 args.image_location_,
2259 args.instruction_set_));
Mathieu Chartierd424d082014-10-15 10:31:46 -07002260 } else {
2261 MemMap::Init();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002262 }
2263
2264 if (args.oat_filename_ != nullptr) {
2265 if (args.symbolize_) {
2266 return SymbolizeOat(args.oat_filename_, args.output_name_);
2267 } else {
2268 return DumpOat(runtime.get(), args.oat_filename_, oat_dumper_options.release(), args.os_);
2269 }
2270 }
2271
2272 if (runtime.get() == nullptr) {
2273 // We need the runtime when printing an image.
2274 return EXIT_FAILURE;
2275 }
2276
2277 return DumpImage(runtime.get(), args.image_location_, oat_dumper_options.release(), args.os_);
2278}
2279
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002280} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002281
2282int main(int argc, char** argv) {
2283 return art::oatdump(argc, argv);
2284}