blob: 08352de7e6049ee6ce48c555a00b89a8590b2666 [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),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800389 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
390 disassembler_(Disassembler::Create(instruction_set_,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700391 new DisassemblerOptions(options_->absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000392 oat_file.Begin(),
393 true /* can_read_litals_ */))) {
Andreas Gampe00b25f32014-09-17 21:49:05 -0700394 CHECK(options_->class_loader_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800395 AddAllOffsets();
396 }
397
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700398 ~OatDumper() {
399 delete options_;
400 delete disassembler_;
401 }
402
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800403 InstructionSet GetInstructionSet() {
404 return instruction_set_;
405 }
406
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700407 bool Dump(std::ostream& os) {
408 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800409 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700410
411 os << "MAGIC:\n";
412 os << oat_header.GetMagic() << "\n\n";
413
414 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800415 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700416
Elliott Hughesa72ec822012-03-05 17:12:22 -0800417 os << "INSTRUCTION SET:\n";
418 os << oat_header.GetInstructionSet() << "\n\n";
419
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700420 {
421 std::unique_ptr<const InstructionSetFeatures> features(
422 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
423 oat_header.GetInstructionSetFeaturesBitmap()));
424 os << "INSTRUCTION SET FEATURES:\n";
425 os << features->GetFeatureString() << "\n\n";
426 }
Dave Allison70202782013-10-22 17:52:19 -0700427
Brian Carlstromaded5f72011-10-07 17:15:04 -0700428 os << "DEX FILE COUNT:\n";
429 os << oat_header.GetDexFileCount() << "\n\n";
430
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800431#define DUMP_OAT_HEADER_OFFSET(label, offset) \
432 os << label " OFFSET:\n"; \
433 os << StringPrintf("0x%08x", oat_header.offset()); \
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700434 if (oat_header.offset() != 0 && options_->absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800435 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
436 } \
437 os << StringPrintf("\n\n");
438
439 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
440 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
441 GetInterpreterToInterpreterBridgeOffset);
442 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
443 GetInterpreterToCompiledCodeBridgeOffset);
444 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
445 GetJniDlsymLookupOffset);
446 DUMP_OAT_HEADER_OFFSET("PORTABLE IMT CONFLICT TRAMPOLINE",
447 GetPortableImtConflictTrampolineOffset);
448 DUMP_OAT_HEADER_OFFSET("PORTABLE RESOLUTION TRAMPOLINE",
449 GetPortableResolutionTrampolineOffset);
450 DUMP_OAT_HEADER_OFFSET("PORTABLE TO INTERPRETER BRIDGE",
451 GetPortableToInterpreterBridgeOffset);
452 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
453 GetQuickGenericJniTrampolineOffset);
454 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
455 GetQuickImtConflictTrampolineOffset);
456 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
457 GetQuickResolutionTrampolineOffset);
458 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
459 GetQuickToInterpreterBridgeOffset);
460#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700461
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700462 os << "IMAGE PATCH DELTA:\n";
463 os << StringPrintf("%d (0x%08x)\n\n",
464 oat_header.GetImagePatchDelta(),
465 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700466
Brian Carlstrom28db0122012-10-18 16:20:41 -0700467 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
468 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
469
470 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800471 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700472
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700473 // Print the key-value store.
474 {
475 os << "KEY VALUE STORE:\n";
476 size_t index = 0;
477 const char* key;
478 const char* value;
479 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
480 os << key << " = " << value << "\n";
481 index++;
482 }
483 os << "\n";
484 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700485
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700486 if (options_->absolute_addresses_) {
487 os << "BEGIN:\n";
488 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700489
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700490 os << "END:\n";
491 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
492 }
493
494 os << "SIZE:\n";
495 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700496
497 os << std::flush;
498
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800499 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
500 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700501 CHECK(oat_dex_file != nullptr);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700502 if (!DumpOatDexFile(os, *oat_dex_file)) {
503 success = false;
504 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700505 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700506 os << std::flush;
507 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700508 }
509
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800510 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700511 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
512 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800513 return 0; // Address not in oat file
514 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800515 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
516 reinterpret_cast<uintptr_t>(oat_file_.Begin());
517 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800518 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800519 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800520 return end_offset - begin_offset;
521 }
522
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800523 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700524 return oat_file_.GetOatHeader().GetInstructionSet();
525 }
526
Ian Rogersef7d42f2014-01-06 12:55:46 -0800527 const void* GetQuickOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800528 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
529 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700530 CHECK(oat_dex_file != nullptr);
531 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700532 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700533 if (dex_file.get() == nullptr) {
534 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
535 << "': " << error_msg;
536 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800537 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700538 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800539 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700540 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700541 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100542 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800543 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100544 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800545 }
546 }
547 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700548 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800549 }
550
Brian Carlstromaded5f72011-10-07 17:15:04 -0700551 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800552 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800553 // We don't know the length of the code for each method, but we need to know where to stop
554 // when disassembling. What we do know is that a region of code will be followed by some other
555 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
556 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800557 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
558 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700559 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700560 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700561 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700562 if (dex_file.get() == nullptr) {
563 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
564 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800565 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800566 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800567 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800568 for (size_t class_def_index = 0;
569 class_def_index < dex_file->NumClassDefs();
570 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800571 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100572 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700573 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700574 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800575 ClassDataItemIterator it(*dex_file, class_data);
576 SkipAllFields(it);
577 uint32_t class_method_index = 0;
578 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100579 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800580 it.Next();
581 }
582 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100583 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800584 it.Next();
585 }
586 }
587 }
588 }
589
590 // If the last thing in the file is code for a method, there won't be an offset for the "next"
591 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
592 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800593 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800594 }
595
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700596 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
597 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
598 }
599
Elliott Hughese3c845c2012-02-28 17:23:01 -0800600 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800601 uint32_t code_offset = oat_method.GetCodeOffset();
602 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
603 code_offset &= ~0x1;
604 }
605 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800606 offsets_.insert(oat_method.GetMappingTableOffset());
607 offsets_.insert(oat_method.GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700608 offsets_.insert(oat_method.GetNativeGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800609 }
610
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700611 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
612 bool success = true;
613 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800614 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800615 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700616
617 // Create the verifier early.
618
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700619 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700620 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700621 if (dex_file.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700622 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700623 os << std::flush;
624 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700625 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800626 for (size_t class_def_index = 0;
627 class_def_index < dex_file->NumClassDefs();
628 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700629 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
630 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700631 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100632 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700633 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
634 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100635 << " (" << oat_class.GetStatus() << ")"
636 << " (" << oat_class.GetType() << ")\n";
637 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800638 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
639 std::ostream indented_os(&indent_filter);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700640 if (!DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def)) {
641 success = false;
642 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700643 }
644
645 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700646 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700647 }
648
Elliott Hughese3c845c2012-02-28 17:23:01 -0800649 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700650 while (it.HasNextStaticField()) {
651 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700652 }
Ian Rogers0571d352011-11-03 19:51:38 -0700653 while (it.HasNextInstanceField()) {
654 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700655 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800656 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700657
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700658 bool DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800659 const DexFile::ClassDef& class_def) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700660 bool success = true;
Ian Rogers13735952014-10-08 12:43:28 -0700661 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700662 if (class_data == nullptr) { // empty class such as a marker interface?
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700663 os << std::flush;
664 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800665 }
666 ClassDataItemIterator it(dex_file, class_data);
667 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700668 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700669 while (it.HasNextDirectMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700670 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
671 it.GetMemberIndex(), it.GetMethodCodeItem(),
672 it.GetRawMemberAccessFlags())) {
673 success = false;
674 }
675 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700676 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700677 }
Ian Rogers0571d352011-11-03 19:51:38 -0700678 while (it.HasNextVirtualMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700679 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
680 it.GetMemberIndex(), it.GetMethodCodeItem(),
681 it.GetRawMemberAccessFlags())) {
682 success = false;
683 }
684 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700685 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700686 }
Ian Rogers0571d352011-11-03 19:51:38 -0700687 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700688 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700689 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700690 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800691
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700692 static constexpr uint32_t kPrologueBytes = 16;
693
694 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
695 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
696
697 bool DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700698 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700699 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800700 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
701 uint32_t method_access_flags) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700702 bool success = true;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000703 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
704 if (pretty_method.find(options_->method_filter_) == std::string::npos) {
705 return success;
706 }
707
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800708 os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000709 class_method_index, pretty_method.c_str(),
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700710 dex_method_idx);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800711 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700712 std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter));
713 Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count);
714 std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter));
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800715 {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700716 *indent1_os << "DEX CODE:\n";
717 DumpDexCode(*indent2_os, dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700718 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700719
720 std::unique_ptr<verifier::MethodVerifier> verifier;
721 if (Runtime::Current() != nullptr) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700722 *indent1_os << "VERIFIER TYPE ANALYSIS:\n";
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700723 verifier.reset(DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item,
724 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700725 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700726
727 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
728 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
729 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800730 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700731 *indent1_os << "OatMethodOffsets ";
732 if (options_->absolute_addresses_) {
733 *indent1_os << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100734 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700735 *indent1_os << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
736 if (oat_method_offsets_offset > oat_file_.Size()) {
737 *indent1_os << StringPrintf(
738 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
739 oat_method_offsets_offset, oat_file_.Size());
740 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
741 os << std::flush;
742 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800743 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700744
745 uint32_t code_offset = oat_method.GetCodeOffset();
746 *indent2_os << StringPrintf("code_offset: 0x%08x ", code_offset);
747 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
748 if (aligned_code_begin > oat_file_.Size()) {
749 *indent2_os << StringPrintf("WARNING: "
750 "code offset 0x%08x is past end of file 0x%08zx.\n",
751 aligned_code_begin, oat_file_.Size());
752 success = false;
753 }
754 *indent2_os << "\n";
755
756 *indent2_os << "gc_map: ";
757 if (options_->absolute_addresses_) {
758 *indent2_os << StringPrintf("%p ", oat_method.GetNativeGcMap());
759 }
760 uint32_t gc_map_offset = oat_method.GetNativeGcMapOffset();
761 *indent2_os << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
762 if (gc_map_offset > oat_file_.Size()) {
763 *indent2_os << StringPrintf("WARNING: "
764 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
765 gc_map_offset, oat_file_.Size());
766 success = false;
767 } else if (options_->dump_raw_gc_map_) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700768 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800769 std::ostream indent3_os(&indent3_filter);
770 DumpGcMap(indent3_os, oat_method, code_item);
771 }
772 }
773 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700774 *indent1_os << "OatQuickMethodHeader ";
775 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
776 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700777
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700778 if (options_->absolute_addresses_) {
779 *indent1_os << StringPrintf("%p ", method_header);
780 }
781 *indent1_os << StringPrintf("(offset=0x%08x)\n", method_header_offset);
782 if (method_header_offset > oat_file_.Size()) {
783 *indent1_os << StringPrintf(
784 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
785 method_header_offset, oat_file_.Size());
786 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
787 os << std::flush;
788 return false;
789 }
790
791 *indent2_os << "mapping_table: ";
792 if (options_->absolute_addresses_) {
793 *indent2_os << StringPrintf("%p ", oat_method.GetMappingTable());
794 }
795 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
796 *indent2_os << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
797 if (mapping_table_offset > oat_file_.Size()) {
798 *indent2_os << StringPrintf("WARNING: "
799 "mapping table offset 0x%08x is past end of file 0x%08zx. "
800 "mapping table offset was loaded from offset 0x%08x.\n",
801 mapping_table_offset, oat_file_.Size(),
802 oat_method.GetMappingTableOffsetOffset());
803 success = false;
804 } else if (options_->dump_raw_mapping_table_) {
805 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
806 std::ostream indent3_os(&indent3_filter);
807 DumpMappingTable(indent3_os, oat_method);
808 }
809
810 *indent2_os << "vmap_table: ";
811 if (options_->absolute_addresses_) {
812 *indent2_os << StringPrintf("%p ", oat_method.GetVmapTable());
813 }
814 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
815 *indent2_os << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
816 if (vmap_table_offset > oat_file_.Size()) {
817 *indent2_os << StringPrintf("WARNING: "
818 "vmap table offset 0x%08x is past end of file 0x%08zx. "
819 "vmap table offset was loaded from offset 0x%08x.\n",
820 vmap_table_offset, oat_file_.Size(),
821 oat_method.GetVmapTableOffsetOffset());
822 success = false;
823 } else if (options_->dump_vmap_) {
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +0100824 DumpVmap(*indent2_os, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700825 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800826 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700827 {
828 *indent1_os << "QuickMethodFrameInfo\n";
829
830 *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
831 *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
832 DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false);
833 *indent2_os << "\n";
834 *indent2_os << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
835 DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true);
836 *indent2_os << "\n";
837 }
838 {
839 // Based on spill masks from QuickMethodFrameInfo so placed
840 // after it is dumped, but useful for understanding quick
841 // code, so dumped here.
842 DumpVregLocations(*indent2_os, oat_method, code_item);
843 }
844 {
845 *indent1_os << "CODE: ";
846 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
847 if (code_size_offset > oat_file_.Size()) {
848 *indent2_os << StringPrintf("WARNING: "
849 "code size offset 0x%08x is past end of file 0x%08zx.",
850 code_size_offset, oat_file_.Size());
851 success = false;
852 } else {
853 const void* code = oat_method.GetQuickCode();
854 uint32_t code_size = oat_method.GetQuickCodeSize();
855 if (code == nullptr) {
856 code = oat_method.GetPortableCode();
857 code_size = oat_method.GetPortableCodeSize();
858 code_size_offset = 0;
859 }
860 uint32_t code_offset = oat_method.GetCodeOffset();
861 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
862 uint64_t aligned_code_end = aligned_code_begin + code_size;
863
864 if (options_->absolute_addresses_) {
865 *indent1_os << StringPrintf("%p ", code);
866 }
867 *indent1_os << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
868 code_offset,
869 code_size_offset,
870 code_size,
871 code != nullptr ? "..." : "");
872
873 if (aligned_code_begin > oat_file_.Size()) {
874 *indent2_os << StringPrintf("WARNING: "
875 "start of code at 0x%08x is past end of file 0x%08zx.",
876 aligned_code_begin, oat_file_.Size());
877 success = false;
878 } else if (aligned_code_end > oat_file_.Size()) {
879 *indent2_os << StringPrintf("WARNING: "
880 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
881 "code size is 0x%08x loaded from offset 0x%08x.\n",
882 aligned_code_end, oat_file_.Size(),
883 code_size, code_size_offset);
884 success = false;
885 if (options_->disassemble_code_) {
886 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
887 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
888 }
889 }
890 } else if (code_size > kMaxCodeSize) {
891 *indent2_os << StringPrintf("WARNING: "
892 "code size %d is bigger than max expected threshold of %d. "
893 "code size is 0x%08x loaded from offset 0x%08x.\n",
894 code_size, kMaxCodeSize,
895 code_size, code_size_offset);
896 success = false;
897 if (options_->disassemble_code_) {
898 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
899 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
900 }
901 }
902 } else if (options_->disassemble_code_) {
903 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, !success, 0);
904 }
905 }
906 }
907 os << std::flush;
908 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700909 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800910
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800911 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
912 if (spill_mask == 0) {
913 return;
914 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800915 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800916 for (size_t i = 0; i < 32; i++) {
917 if ((spill_mask & (1 << i)) != 0) {
918 if (is_float) {
919 os << "fr" << i;
920 } else {
921 os << "r" << i;
922 }
923 spill_mask ^= 1 << i; // clear bit
924 if (spill_mask != 0) {
925 os << ", ";
926 } else {
927 break;
928 }
929 }
930 }
931 os << ")";
932 }
933
Ian Rogersb23a7722012-10-09 16:54:26 -0700934 void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) {
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +0100935 // If the native GC map is null, then this method has been compiled with the
936 // optimizing compiler. The optimizing compiler currently outputs its stack map
937 // in the vmap table, and the code below does not work with such a stack map.
938 if (oat_method.GetNativeGcMap() == nullptr) {
939 return;
940 }
Ian Rogers1809a722013-08-09 22:05:32 -0700941 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700942 if (raw_table != nullptr) {
Ian Rogers1809a722013-08-09 22:05:32 -0700943 const VmapTable vmap_table(raw_table);
944 bool first = true;
945 bool processing_fp = false;
946 uint32_t spill_mask = oat_method.GetCoreSpillMask();
947 for (size_t i = 0; i < vmap_table.Size(); i++) {
948 uint16_t dex_reg = vmap_table[i];
949 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
950 processing_fp ? kFloatVReg : kIntVReg);
951 os << (first ? "v" : ", v") << dex_reg;
952 if (!processing_fp) {
953 os << "/r" << cpu_reg;
954 } else {
955 os << "/fr" << cpu_reg;
956 }
957 first = false;
958 if (!processing_fp && dex_reg == 0xFFFF) {
959 processing_fp = true;
960 spill_mask = oat_method.GetFpSpillMask();
961 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800962 }
Ian Rogers1809a722013-08-09 22:05:32 -0700963 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800964 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800965 }
966
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -0700967 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
968 const DexFile::CodeItem* code_item) {
969 if (code_item != nullptr) {
970 size_t num_locals_ins = code_item->registers_size_;
971 size_t num_ins = code_item->ins_size_;
972 size_t num_locals = num_locals_ins - num_ins;
973 size_t num_outs = code_item->outs_size_;
974
975 os << "vr_stack_locations:";
976 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
977 // For readability, delimit the different kinds of VRs.
978 if (reg == num_locals_ins) {
979 os << "\n\tmethod*:";
980 } else if (reg == num_locals && num_ins > 0) {
981 os << "\n\tins:";
982 } else if (reg == 0 && num_locals > 0) {
983 os << "\n\tlocals:";
984 }
985
986 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
987 oat_method.GetFpSpillMask(),
988 oat_method.GetFrameSizeInBytes(), reg,
989 GetInstructionSet());
990 os << " v" << reg << "[sp + #" << offset << "]";
991 }
992
993 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
994 if (out_reg == 0) {
995 os << "\n\touts:";
996 }
997
998 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
999 os << " v" << out_reg << "[sp + #" << offset << "]";
1000 }
1001
1002 os << "\n";
1003 }
1004 }
1005
Ian Rogersb23a7722012-10-09 16:54:26 -07001006 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001007 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001008 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001009 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001010 const VmapTable vmap_table(raw_table);
1011 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001012 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001013 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1014 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1015 : oat_method.GetCoreSpillMask();
1016 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001017 } else {
1018 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
1019 oat_method.GetFpSpillMask(),
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001020 oat_method.GetFrameSizeInBytes(), reg,
1021 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001022 os << "[sp + #" << offset << "]";
1023 }
1024 }
1025 }
1026
Ian Rogersef7d42f2014-01-06 12:55:46 -08001027 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1028 const DexFile::CodeItem* code_item,
1029 size_t num_regs, const uint8_t* reg_bitmap) {
1030 bool first = true;
1031 for (size_t reg = 0; reg < num_regs; reg++) {
1032 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1033 if (first) {
1034 os << " v" << reg << " (";
1035 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1036 os << ")";
1037 first = false;
1038 } else {
1039 os << ", v" << reg << " (";
1040 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1041 os << ")";
1042 }
1043 }
1044 }
1045 if (first) {
1046 os << "No registers in GC map\n";
1047 } else {
1048 os << "\n";
1049 }
1050 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001051 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1052 const DexFile::CodeItem* code_item) {
1053 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001054 if (gc_map_raw == nullptr) {
1055 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001056 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001057 const void* quick_code = oat_method.GetQuickCode();
1058 if (quick_code != nullptr) {
1059 NativePcOffsetToReferenceMap map(gc_map_raw);
1060 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1061 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1062 map.GetNativePcOffset(entry);
1063 os << StringPrintf("%p", native_pc);
1064 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001065 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001066 } else {
1067 const void* portable_code = oat_method.GetPortableCode();
1068 CHECK(portable_code != nullptr);
1069 verifier::DexPcToReferenceMap map(gc_map_raw);
1070 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1071 uint32_t dex_pc = map.GetDexPc(entry);
1072 os << StringPrintf("0x%08x", dex_pc);
1073 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
1074 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001075 }
1076 }
1077
1078 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001079 const void* quick_code = oat_method.GetQuickCode();
1080 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001081 return;
1082 }
Ian Rogers1809a722013-08-09 22:05:32 -07001083 MappingTable table(oat_method.GetMappingTable());
1084 if (table.TotalSize() != 0) {
1085 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1086 std::ostream indent_os(&indent_filter);
1087 if (table.PcToDexSize() != 0) {
1088 typedef MappingTable::PcToDexIterator It;
1089 os << "suspend point mappings {\n";
1090 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1091 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1092 }
1093 os << "}\n";
1094 }
1095 if (table.DexToPcSize() != 0) {
1096 typedef MappingTable::DexToPcIterator It;
1097 os << "catch entry mappings {\n";
1098 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1099 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1100 }
1101 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001102 }
1103 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001104 }
1105
Ian Rogers1809a722013-08-09 22:05:32 -07001106 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1107 size_t offset, bool suspend_point_mapping) {
1108 MappingTable table(oat_method.GetMappingTable());
1109 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1110 typedef MappingTable::PcToDexIterator It;
1111 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1112 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001113 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001114 return cur.DexPc();
1115 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001116 }
Ian Rogers1809a722013-08-09 22:05:32 -07001117 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1118 typedef MappingTable::DexToPcIterator It;
1119 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1120 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001121 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001122 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001123 }
1124 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001125 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001126 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001127 }
1128
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001129 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1130 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001131 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001132 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001133 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001134 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001135 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001136 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001137 bool first = true;
1138 for (size_t reg = 0; reg < num_regs; reg++) {
1139 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1140 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001141 os << "GC map objects: v" << reg << " (";
1142 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001143 os << ")";
1144 first = false;
1145 } else {
1146 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001147 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001148 os << ")";
1149 }
1150 }
1151 }
1152 if (!first) {
1153 os << "\n";
1154 }
1155 }
1156 }
1157 }
1158
Mathieu Chartier590fee92013-09-13 13:46:47 -07001159 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1160 const OatFile::OatMethod& oat_method,
1161 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1162 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001163 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001164 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001165 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1166 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1167 if (kind != kUndefined) {
1168 if (first) {
1169 os << "VRegs: v";
1170 first = false;
1171 } else {
1172 os << ", v";
1173 }
1174 os << reg << " (";
1175 switch (kind) {
1176 case kImpreciseConstant:
1177 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1178 DescribeVReg(os, oat_method, code_item, reg, kind);
1179 break;
1180 case kConstant:
1181 os << "Constant: " << kinds.at((reg * 2) + 1);
1182 break;
1183 default:
1184 DescribeVReg(os, oat_method, code_item, reg, kind);
1185 break;
1186 }
1187 os << ")";
1188 }
1189 }
1190 if (!first) {
1191 os << "\n";
1192 }
1193 }
1194
1195
Ian Rogersb23a7722012-10-09 16:54:26 -07001196 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001197 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001198 size_t i = 0;
1199 while (i < code_item->insns_size_in_code_units_) {
1200 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001201 os << StringPrintf("0x%04zx: %s\n", i, instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001202 i += instruction->SizeInCodeUnits();
1203 }
1204 }
1205 }
1206
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001207 verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx,
1208 const DexFile* dex_file,
1209 const DexFile::ClassDef& class_def,
1210 const DexFile::CodeItem* code_item,
1211 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001212 if ((method_access_flags & kAccNative) == 0) {
1213 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001214 StackHandleScope<1> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001215 Handle<mirror::DexCache> dex_cache(
1216 hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
Andreas Gampe00b25f32014-09-17 21:49:05 -07001217 DCHECK(options_->class_loader_ != nullptr);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001218 return verifier::MethodVerifier::VerifyMethodAndDump(soa.Self(), os, dex_method_idx, dex_file,
1219 dex_cache,
Andreas Gampe00b25f32014-09-17 21:49:05 -07001220 *options_->class_loader_,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001221 &class_def, code_item,
1222 NullHandle<mirror::ArtMethod>(),
1223 method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001224 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001225
1226 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001227 }
1228
Mathieu Chartier590fee92013-09-13 13:46:47 -07001229 void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001230 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1231 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001232 const void* portable_code = oat_method.GetPortableCode();
1233 const void* quick_code = oat_method.GetQuickCode();
1234
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001235 if (code_size == 0) {
1236 code_size = oat_method.GetQuickCodeSize();
1237 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001238 if ((code_size == 0) || ((portable_code == nullptr) && (quick_code == nullptr))) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001239 os << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001240 return;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001241 } else if (quick_code != nullptr) {
1242 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1243 size_t offset = 0;
1244 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001245 if (!bad_input) {
1246 DumpMappingAtOffset(os, oat_method, offset, false);
1247 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001248 offset += disassembler_->Dump(os, quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001249 if (!bad_input) {
1250 uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
1251 if (dex_pc != DexFile::kDexNoIndex) {
1252 DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
1253 if (verifier != nullptr) {
1254 DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
1255 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001256 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001257 }
1258 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001259 } else {
1260 CHECK(portable_code != nullptr);
1261 CHECK_EQ(code_size, 0U); // TODO: disassembly of portable is currently not supported.
Ian Rogersb23a7722012-10-09 16:54:26 -07001262 }
1263 }
1264
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001265 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001266 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
1267 const OatDumperOptions* options_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001268 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001269 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001270 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001271};
1272
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001273class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001274 public:
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001275 explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001276 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
1277 : os_(os),
1278 image_space_(image_space),
1279 image_header_(image_header),
1280 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001281
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001282 bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001283 std::ostream& os = *os_;
1284 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001285
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001286 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001287
Mathieu Chartier31e89252013-08-28 11:29:12 -07001288 os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
1289 << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
1290
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001291 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001292
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001293 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001294
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001295 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1296
1297 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1298
1299 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001300
Alex Lighta59dd802014-07-02 16:28:08 -07001301 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1302
Igor Murashkin46774762014-10-22 11:37:02 -07001303 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1304
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001305 {
1306 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
1307 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1308 std::ostream indent1_os(&indent1_filter);
1309 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
1310 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1311 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1312 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001313 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001314 indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
1315 if (image_root_object->IsObjectArray()) {
1316 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
1317 std::ostream indent2_os(&indent2_filter);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001318 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001319 = image_root_object->AsObjectArray<mirror::Object>();
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001320 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1321 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001322 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001323 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1324 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001325 run++;
1326 } else {
1327 break;
1328 }
1329 }
1330 if (run == 0) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001331 indent2_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001332 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001333 indent2_os << StringPrintf("%d to %zd: ", j, j + run);
1334 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001335 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001336 if (value != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001337 PrettyObjectValue(indent2_os, value->GetClass(), value);
1338 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001339 indent2_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001340 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001341 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001342 }
1343 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001344 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001345 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001346
Brian Carlstromaded5f72011-10-07 17:15:04 -07001347 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001348 std::string image_filename = image_space_.GetImageFilename();
1349 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001350 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001351 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001352 std::string error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -07001353 const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1354 if (oat_file == nullptr) {
Igor Murashkin46774762014-10-22 11:37:02 -07001355 oat_file = OatFile::Open(oat_location, oat_location, nullptr, nullptr, false, &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001356 if (oat_file == nullptr) {
1357 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001358 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001359 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001360 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001361 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001362
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001363 stats_.oat_file_bytes = oat_file->Size();
1364
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001365 oat_dumper_.reset(new OatDumper(*oat_file, oat_dumper_options_.release()));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001366
Mathieu Chartier02e25112013-08-14 16:14:24 -07001367 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001368 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001369 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1370 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001371 }
1372
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001373 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001374
1375 // Loop through all the image spaces and dump their objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001376 gc::Heap* heap = Runtime::Current()->GetHeap();
1377 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07001378 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001379 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001380 {
1381 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1382 heap->FlushAllocStack();
1383 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001384 // Since FlushAllocStack() above resets the (active) allocation
1385 // stack. Need to revoke the thread-local allocation stacks that
1386 // point into it.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001387 {
1388 self->TransitionFromRunnableToSuspended(kNative);
1389 ThreadList* thread_list = Runtime::Current()->GetThreadList();
1390 thread_list->SuspendAll();
1391 heap->RevokeAllThreadLocalAllocationStacks(self);
1392 thread_list->ResumeAll();
1393 self->TransitionFromSuspendedToRunnable();
1394 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001395 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001396 {
1397 std::ostream* saved_os = os_;
1398 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1399 std::ostream indent_os(&indent_filter);
1400 os_ = &indent_os;
1401 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001402 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001403 if (space->IsImageSpace()) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001404 gc::space::ImageSpace* image_space = space->AsImageSpace();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001405 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1406 indent_os << "\n";
1407 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001408 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001409 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001410 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001411 indent_os << "\n";
1412 os_ = saved_os;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001413 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001414 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001415 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001416 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001417 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001418 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001419 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001420 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001421 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001422 size_t header_bytes = sizeof(ImageHeader);
1423 stats_.header_bytes = header_bytes;
1424 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1425 stats_.alignment_bytes += alignment_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001426 stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
1427 stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001428 stats_.Dump(os);
1429 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001430
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001431 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001432
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001433 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001434 }
1435
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001436 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001437 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001438 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001439 CHECK(type != nullptr);
1440 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001441 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001442 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001443 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001444 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001445 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001446 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001447 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001448 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001449 } else if (type->IsArtFieldClass()) {
1450 mirror::ArtField* field = value->AsArtField();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001451 os << StringPrintf("%p Field: %s\n", field, PrettyField(field).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001452 } else if (type->IsArtMethodClass()) {
1453 mirror::ArtMethod* method = value->AsArtMethod();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001454 os << StringPrintf("%p Method: %s\n", method, PrettyMethod(method).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001455 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001456 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001457 }
1458 }
1459
Brian Carlstromea46f952013-07-30 01:26:50 -07001460 static void PrintField(std::ostream& os, mirror::ArtField* field, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001461 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001462 const char* descriptor = field->GetTypeDescriptor();
1463 os << StringPrintf("%s: ", field->GetName());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001464 if (descriptor[0] != 'L' && descriptor[0] != '[') {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001465 StackHandleScope<1> hs(Thread::Current());
1466 FieldHelper fh(hs.NewHandle(field));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001467 mirror::Class* type = fh.GetType();
Fred Shih37f05ef2014-07-16 18:38:08 -07001468 DCHECK(type->IsPrimitive());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001469 if (type->IsPrimitiveLong()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001470 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -07001471 } else if (type->IsPrimitiveDouble()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001472 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -07001473 } else if (type->IsPrimitiveFloat()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001474 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Fred Shih37f05ef2014-07-16 18:38:08 -07001475 } else if (type->IsPrimitiveInt()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001476 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Fred Shih37f05ef2014-07-16 18:38:08 -07001477 } else if (type->IsPrimitiveChar()) {
1478 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
1479 } else if (type->IsPrimitiveShort()) {
1480 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
1481 } else if (type->IsPrimitiveBoolean()) {
1482 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1483 field->GetBoolean(obj));
1484 } else if (type->IsPrimitiveByte()) {
1485 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
1486 } else {
1487 LOG(FATAL) << "Unknown type: " << PrettyClass(type);
Ian Rogers48efc2b2012-08-27 17:20:31 -07001488 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001489 } else {
Ian Rogers48efc2b2012-08-27 17:20:31 -07001490 // Get the value, don't compute the type unless it is non-null as we don't want
1491 // to cause class loading.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001492 mirror::Object* value = field->GetObj(obj);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001493 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001494 os << StringPrintf("null %s\n", PrettyDescriptor(descriptor).c_str());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001495 } else {
Ian Rogers50239c72013-06-17 14:53:22 -07001496 // Grab the field type without causing resolution.
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001497 StackHandleScope<1> hs(Thread::Current());
1498 FieldHelper fh(hs.NewHandle(field));
Ian Rogers50239c72013-06-17 14:53:22 -07001499 mirror::Class* field_type = fh.GetType(false);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001500 if (field_type != nullptr) {
Ian Rogers50239c72013-06-17 14:53:22 -07001501 PrettyObjectValue(os, field_type, value);
1502 } else {
1503 os << StringPrintf("%p %s\n", value, PrettyDescriptor(descriptor).c_str());
1504 }
Ian Rogers48efc2b2012-08-27 17:20:31 -07001505 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001506 }
1507 }
1508
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001509 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001510 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001511 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001512 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001513 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001514 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001515 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001516 if (fields != nullptr) {
Ian Rogersd5b32602012-02-26 16:40:04 -08001517 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001518 mirror::ArtField* field = fields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001519 PrintField(os, field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001520 }
1521 }
1522 }
1523
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001524 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001525 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001526 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001527
Ian Rogersef7d42f2014-01-06 12:55:46 -08001528 const void* GetQuickOatCodeBegin(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001529 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001530 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1531 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001532 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001533 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001534 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001535 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001536 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001537 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001538 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001539 }
1540
Ian Rogersef7d42f2014-01-06 12:55:46 -08001541 uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001542 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001543 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1544 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001545 return 0;
1546 }
1547 return oat_code_begin[-1];
1548 }
1549
Ian Rogersef7d42f2014-01-06 12:55:46 -08001550 const void* GetQuickOatCodeEnd(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001551 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001552 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001553 if (oat_code_begin == nullptr) {
1554 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001555 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001556 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001557 }
1558
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001559 static void Callback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001560 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001561 DCHECK(obj != nullptr);
1562 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001563 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001564 if (!state->InDumpSpace(obj)) {
1565 return;
1566 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001567
1568 size_t object_bytes = obj->SizeOf();
1569 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1570 state->stats_.object_bytes += object_bytes;
1571 state->stats_.alignment_bytes += alignment_bytes;
1572
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001573 std::ostream& os = *state->os_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001574 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001575 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001576 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1577 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001578 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001579 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001580 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1581 << klass->GetStatus() << ")\n";
Brian Carlstromea46f952013-07-30 01:26:50 -07001582 } else if (obj->IsArtField()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001583 os << StringPrintf("%p: java.lang.reflect.ArtField %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001584 PrettyField(obj->AsArtField()).c_str());
1585 } else if (obj->IsArtMethod()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001586 os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001587 PrettyMethod(obj->AsArtMethod()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001588 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001589 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001590 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001591 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001592 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001593 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001594 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1595 std::ostream indent_os(&indent_filter);
1596 DumpFields(indent_os, obj, obj_class);
Ian Rogersd5b32602012-02-26 16:40:04 -08001597 if (obj->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001598 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Ian Rogersd5b32602012-02-26 16:40:04 -08001599 int32_t length = obj_array->GetLength();
1600 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001601 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001602 size_t run = 0;
1603 for (int32_t j = i + 1; j < length; j++) {
1604 if (value == obj_array->Get(j)) {
1605 run++;
1606 } else {
1607 break;
1608 }
1609 }
1610 if (run == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001611 indent_os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001612 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001613 indent_os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001614 i = i + run;
1615 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001616 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001617 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001618 PrettyObjectValue(indent_os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001619 }
1620 } else if (obj->IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001621 mirror::ObjectArray<mirror::ArtField>* sfields = obj->AsClass()->GetSFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001622 if (sfields != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001623 indent_os << "STATICS:\n";
1624 Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
1625 std::ostream indent2_os(&indent2_filter);
Ian Rogersd5b32602012-02-26 16:40:04 -08001626 for (int32_t i = 0; i < sfields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001627 mirror::ArtField* field = sfields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001628 PrintField(indent2_os, field, field->GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001629 }
1630 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001631 } else if (obj->IsArtMethod()) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001632 const size_t image_pointer_size = InstructionSetPointerSize(
1633 state->oat_dumper_->GetOatInstructionSet());
Brian Carlstromea46f952013-07-30 01:26:50 -07001634 mirror::ArtMethod* method = obj->AsArtMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001635 if (method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001636 // TODO: portable dumping.
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001637 DCHECK(method->GetNativeGcMapPtrSize(image_pointer_size) == nullptr)
1638 << PrettyMethod(method);
1639 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001640 bool first_occurrence;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001641 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1642 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1643 state->ComputeOatSize(quick_oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001644 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001645 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001646 }
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001647 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCodePtrSize(
1648 image_pointer_size)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001649 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001650 }
Ian Rogers19846512012-02-24 11:42:47 -08001651 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
Jeff Hao88474b42013-10-23 16:24:40 -07001652 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001653 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001654 DCHECK(method->GetNativeGcMapPtrSize(image_pointer_size) == nullptr)
1655 << PrettyMethod(method);
1656 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001657 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001658 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -07001659 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001660 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001661
Elliott Hughesa0e18062012-04-13 15:59:59 -07001662 bool first_occurrence;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001663 size_t gc_map_bytes = state->ComputeOatSize(
1664 method->GetNativeGcMapPtrSize(image_pointer_size), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001665 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001666 state->stats_.gc_map_bytes += gc_map_bytes;
1667 }
1668
1669 size_t pc_mapping_table_bytes =
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001670 state->ComputeOatSize(method->GetMappingTable(image_pointer_size), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001671 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001672 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1673 }
1674
1675 size_t vmap_table_bytes =
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001676 state->ComputeOatSize(method->GetVmapTable(image_pointer_size), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001677 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001678 state->stats_.vmap_table_bytes += vmap_table_bytes;
1679 }
1680
Ian Rogersef7d42f2014-01-06 12:55:46 -08001681 // TODO: portable dumping.
1682 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1683 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1684 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1685 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001686 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001687 state->stats_.managed_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001688 if (method->IsConstructor()) {
1689 if (method->IsStatic()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001690 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001691 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001692 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001693 }
1694 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001695 state->stats_.large_method_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001696 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001697 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001698 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001699
Ian Rogersef7d42f2014-01-06 12:55:46 -08001700 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001701 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
1702 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001703
1704 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersef7d42f2014-01-06 12:55:46 -08001705 vmap_table_bytes + quick_oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001706
1707 double expansion =
Ian Rogersef7d42f2014-01-06 12:55:46 -08001708 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001709 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001710 }
1711 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001712 std::string temp;
1713 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001714 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001715
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001716 std::set<const void*> already_seen_;
1717 // Compute the size of the given data within the oat file and whether this is the first time
1718 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001719 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001720 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001721 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001722 already_seen_.insert(oat_data);
1723 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001724 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001725 }
1726 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001727 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001728
1729 public:
1730 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001731 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001732 size_t file_bytes;
1733
1734 size_t header_bytes;
1735 size_t object_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001736 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001737 size_t alignment_bytes;
1738
1739 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001740 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001741 size_t managed_to_native_code_bytes;
1742 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001743 size_t class_initializer_code_bytes;
1744 size_t large_initializer_code_bytes;
1745 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001746
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001747 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001748 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001749 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001750
1751 size_t dex_instruction_bytes;
1752
Brian Carlstromea46f952013-07-30 01:26:50 -07001753 std::vector<mirror::ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001754 std::vector<size_t> method_outlier_size;
1755 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07001756 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001757
1758 explicit Stats()
1759 : oat_file_bytes(0),
1760 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001761 header_bytes(0),
1762 object_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07001763 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001764 alignment_bytes(0),
1765 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001766 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001767 managed_to_native_code_bytes(0),
1768 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001769 class_initializer_code_bytes(0),
1770 large_initializer_code_bytes(0),
1771 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001772 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001773 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001774 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001775 dex_instruction_bytes(0) {}
1776
Elliott Hughesa0e18062012-04-13 15:59:59 -07001777 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001778 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07001779 size_t bytes;
1780 size_t count;
1781 };
1782 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1783 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001784
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001785 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001786 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1787 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001788 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001789 it->second.count += 1;
1790 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001791 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07001792 }
1793 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001794
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001795 double PercentOfOatBytes(size_t size) {
1796 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1797 }
1798
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001799 double PercentOfFileBytes(size_t size) {
1800 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1801 }
1802
1803 double PercentOfObjectBytes(size_t size) {
1804 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1805 }
1806
Brian Carlstromea46f952013-07-30 01:26:50 -07001807 void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001808 method_outlier_size.push_back(total_size);
1809 method_outlier_expansion.push_back(expansion);
1810 method_outlier.push_back(method);
1811 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001812
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001813 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001814 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001815 size_t sum_of_sizes = 0;
1816 size_t sum_of_sizes_squared = 0;
1817 size_t sum_of_expansion = 0;
1818 size_t sum_of_expansion_squared = 0;
1819 size_t n = method_outlier_size.size();
1820 for (size_t i = 0; i < n; i++) {
1821 size_t cur_size = method_outlier_size[i];
1822 sum_of_sizes += cur_size;
1823 sum_of_sizes_squared += cur_size * cur_size;
1824 double cur_expansion = method_outlier_expansion[i];
1825 sum_of_expansion += cur_expansion;
1826 sum_of_expansion_squared += cur_expansion * cur_expansion;
1827 }
1828 size_t size_mean = sum_of_sizes / n;
1829 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1830 double expansion_mean = sum_of_expansion / n;
1831 double expansion_variance =
1832 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1833
1834 // Dump methods whose size is a certain number of standard deviations from the mean
1835 size_t dumped_values = 0;
1836 size_t skipped_values = 0;
1837 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1838 size_t cur_size_variance = i * i * size_variance;
1839 bool first = true;
1840 for (size_t j = 0; j < n; j++) {
1841 size_t cur_size = method_outlier_size[j];
1842 if (cur_size > size_mean) {
1843 size_t cur_var = cur_size - size_mean;
1844 cur_var = cur_var * cur_var;
1845 if (cur_var > cur_size_variance) {
1846 if (dumped_values > 20) {
1847 if (i == 1) {
1848 skipped_values++;
1849 } else {
1850 i = 2; // jump to counting for 1 standard deviation
1851 break;
1852 }
1853 } else {
1854 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001855 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001856 first = false;
1857 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001858 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001859 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001860 method_outlier_size[j] = 0; // don't consider this method again
1861 dumped_values++;
1862 }
1863 }
1864 }
1865 }
1866 }
1867 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001868 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001869 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001870 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001871 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001872
1873 // Dump methods whose expansion is a certain number of standard deviations from the mean
1874 dumped_values = 0;
1875 skipped_values = 0;
1876 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1877 double cur_expansion_variance = i * i * expansion_variance;
1878 bool first = true;
1879 for (size_t j = 0; j < n; j++) {
1880 double cur_expansion = method_outlier_expansion[j];
1881 if (cur_expansion > expansion_mean) {
1882 size_t cur_var = cur_expansion - expansion_mean;
1883 cur_var = cur_var * cur_var;
1884 if (cur_var > cur_expansion_variance) {
1885 if (dumped_values > 20) {
1886 if (i == 1) {
1887 skipped_values++;
1888 } else {
1889 i = 2; // jump to counting for 1 standard deviation
1890 break;
1891 }
1892 } else {
1893 if (first) {
1894 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001895 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001896 first = false;
1897 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001898 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001899 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001900 method_outlier_expansion[j] = 0.0; // don't consider this method again
1901 dumped_values++;
1902 }
1903 }
1904 }
1905 }
1906 }
1907 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001908 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001909 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001910 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001911 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001912 }
1913
Ian Rogersb726dcb2012-09-05 08:57:23 -07001914 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001915 {
1916 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1917 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
1918 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1919 std::ostream indent_os(&indent_filter);
1920 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
1921 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier32327092013-08-30 14:04:08 -07001922 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001923 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
1924 header_bytes, PercentOfFileBytes(header_bytes),
1925 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07001926 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001927 alignment_bytes, PercentOfFileBytes(alignment_bytes))
1928 << std::flush;
Mathieu Chartier32327092013-08-30 14:04:08 -07001929 CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001930 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001931
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001932 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001933 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07001934 for (const auto& sizes_and_count : sizes_and_counts) {
1935 const std::string& descriptor(sizes_and_count.first);
1936 double average = static_cast<double>(sizes_and_count.second.bytes) /
1937 static_cast<double>(sizes_and_count.second.count);
1938 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001939 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001940 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001941 descriptor.c_str(), sizes_and_count.second.bytes,
1942 sizes_and_count.second.count, average, percent);
1943 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001944 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001945 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001946 CHECK_EQ(object_bytes, object_bytes_total);
1947
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001948 os << StringPrintf("oat_file_bytes = %8zd\n"
1949 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1950 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1951 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1952 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1953 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1954 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001955 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001956 managed_code_bytes,
1957 PercentOfOatBytes(managed_code_bytes),
1958 managed_to_native_code_bytes,
1959 PercentOfOatBytes(managed_to_native_code_bytes),
1960 native_to_managed_code_bytes,
1961 PercentOfOatBytes(native_to_managed_code_bytes),
1962 class_initializer_code_bytes,
1963 PercentOfOatBytes(class_initializer_code_bytes),
1964 large_initializer_code_bytes,
1965 PercentOfOatBytes(large_initializer_code_bytes),
1966 large_method_code_bytes,
1967 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001968 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07001969 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001970 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001971 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
1972 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07001973 }
1974
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001975 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1976 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1977 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001978 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1979 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1980 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001981 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001982
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001983 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
1984 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001985 static_cast<double>(managed_code_bytes) /
1986 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07001987 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001988 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001989 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001990
1991 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001992 }
1993 } stats_;
1994
1995 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001996 enum {
1997 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1998 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1999 kLargeConstructorDexBytes = 4000,
2000 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2001 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2002 kLargeMethodDexBytes = 16000
2003 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002004 std::ostream* os_;
Ian Rogers1d54e732013-05-02 21:10:01 -07002005 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002006 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002007 std::unique_ptr<OatDumper> oat_dumper_;
2008 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002009
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002010 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002011};
2012
Andreas Gampe00b25f32014-09-17 21:49:05 -07002013static NoopCompilerCallbacks callbacks;
Elliott Hughes72395bf2012-04-24 13:45:26 -07002014
Andreas Gampe00b25f32014-09-17 21:49:05 -07002015static Runtime* StartRuntime(const char* boot_image_location, const char* image_location,
2016 InstructionSet instruction_set) {
Ian Rogerse63db272014-07-15 15:36:11 -07002017 RuntimeOptions options;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002018 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07002019 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002020 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07002021 std::string boot_oat_option;
Brian Carlstrom6c871802013-07-17 14:25:35 -07002022
2023 // We are more like a compiler than a run-time. We don't want to execute code.
Brian Carlstromc0a1b182014-03-04 23:19:06 -08002024 options.push_back(std::make_pair("compilercallbacks", &callbacks));
Brian Carlstrom6c871802013-07-17 14:25:35 -07002025
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002026 if (boot_image_location != nullptr) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -07002027 boot_image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002028 boot_image_option += boot_image_location;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002029 options.push_back(std::make_pair(boot_image_option.c_str(), nullptr));
Brian Carlstrom78128a62011-09-15 17:21:19 -07002030 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002031 if (image_location != nullptr) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07002032 image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002033 image_option += image_location;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002034 options.push_back(std::make_pair(image_option.c_str(), nullptr));
Brian Carlstromaded5f72011-10-07 17:15:04 -07002035 }
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002036 options.push_back(
2037 std::make_pair("imageinstructionset",
2038 reinterpret_cast<const void*>(GetInstructionSetString(instruction_set))));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07002039
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002040 if (!Runtime::Create(options, false)) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07002041 fprintf(stderr, "Failed to create runtime\n");
Andreas Gampe00b25f32014-09-17 21:49:05 -07002042 return nullptr;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002043 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002044
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002045 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
Ian Rogersb9beb2e2014-05-09 16:57:40 -07002046 // give it away now and then switch to a more manageable ScopedObjectAccess.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002047 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002048
2049 return Runtime::Current();
2050}
2051
2052static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
2053 std::ostream* os) {
2054 // Dumping the image, no explicit class loader.
2055 NullHandle<mirror::ClassLoader> null_class_loader;
2056 options->class_loader_ = &null_class_loader;
2057
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002058 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002059 gc::Heap* heap = runtime->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -07002060 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002061 CHECK(image_space != nullptr);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002062 const ImageHeader& image_header = image_space->GetImageHeader();
2063 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002064 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002065 return EXIT_FAILURE;
2066 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002067 ImageDumper image_dumper(os, *image_space, image_header, options);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002068 bool success = image_dumper.Dump();
2069 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002070}
2071
Andreas Gampe00b25f32014-09-17 21:49:05 -07002072static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2073 std::ostream* os) {
2074 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2075
2076 Thread* self = Thread::Current();
2077 CHECK(self != nullptr);
2078 // Need well-known-classes.
2079 WellKnownClasses::Init(self->GetJniEnv());
2080
2081 // Need to register dex files to get a working dex cache.
2082 ScopedObjectAccess soa(self);
2083 ClassLinker* class_linker = runtime->GetClassLinker();
2084 class_linker->RegisterOatFile(oat_file);
2085 std::vector<const DexFile*> dex_files;
2086 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2087 std::string error_msg;
2088 const DexFile* dex_file = odf->OpenDexFile(&error_msg);
2089 CHECK(dex_file != nullptr) << error_msg;
2090 class_linker->RegisterDexFile(*dex_file);
2091 dex_files.push_back(dex_file);
2092 }
2093
2094 // Need a class loader.
2095 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader);
2096 ScopedLocalRef<jobject> class_loader_local(soa.Env(),
2097 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
2098 jobject class_loader = soa.Env()->NewGlobalRef(class_loader_local.get());
2099 // Fake that we're a compiler.
2100 runtime->SetCompileTimeClassPath(class_loader, dex_files);
2101
2102 // Use the class loader while dumping.
2103 StackHandleScope<1> scope(self);
2104 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2105 soa.Decode<mirror::ClassLoader*>(class_loader));
2106 options->class_loader_ = &loader_handle;
2107
2108 OatDumper oat_dumper(*oat_file, options);
2109 bool success = oat_dumper.Dump(*os);
2110 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2111}
2112
2113static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
2114 // No image = no class loader.
2115 NullHandle<mirror::ClassLoader> null_class_loader;
2116 options->class_loader_ = &null_class_loader;
2117
2118 OatDumper oat_dumper(*oat_file, options);
2119 bool success = oat_dumper.Dump(*os);
2120 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2121}
2122
2123static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2124 std::ostream* os) {
2125 std::string error_msg;
Igor Murashkin46774762014-10-22 11:37:02 -07002126 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002127 if (oat_file == nullptr) {
2128 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2129 return EXIT_FAILURE;
2130 }
2131
2132 if (runtime != nullptr) {
2133 return DumpOatWithRuntime(runtime, oat_file, options, os);
2134 } else {
2135 return DumpOatWithoutRuntime(oat_file, options, os);
2136 }
2137}
2138
2139static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2140 std::string error_msg;
Igor Murashkin46774762014-10-22 11:37:02 -07002141 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002142 if (oat_file == nullptr) {
2143 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2144 return EXIT_FAILURE;
2145 }
2146
2147 OatSymbolizer oat_symbolizer(oat_file, output_name);
2148 if (!oat_symbolizer.Init()) {
2149 fprintf(stderr, "Failed to initialize symbolizer\n");
2150 return EXIT_FAILURE;
2151 }
2152 if (!oat_symbolizer.Symbolize()) {
2153 fprintf(stderr, "Failed to symbolize\n");
2154 return EXIT_FAILURE;
2155 }
2156
2157 return EXIT_SUCCESS;
2158}
2159
2160struct OatdumpArgs {
2161 bool Parse(int argc, char** argv) {
2162 // Skip over argv[0].
2163 argv++;
2164 argc--;
2165
2166 if (argc == 0) {
2167 fprintf(stderr, "No arguments specified\n");
2168 usage();
2169 return false;
2170 }
2171
2172 for (int i = 0; i < argc; i++) {
2173 const StringPiece option(argv[i]);
2174 if (option.starts_with("--oat-file=")) {
2175 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2176 } else if (option.starts_with("--image=")) {
2177 image_location_ = option.substr(strlen("--image=")).data();
2178 } else if (option.starts_with("--boot-image=")) {
2179 boot_image_location_ = option.substr(strlen("--boot-image=")).data();
2180 } else if (option.starts_with("--instruction-set=")) {
2181 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
2182 instruction_set_ = GetInstructionSetFromString(instruction_set_str.data());
2183 if (instruction_set_ == kNone) {
2184 fprintf(stderr, "Unsupported instruction set %s\n", instruction_set_str.data());
2185 usage();
2186 return false;
2187 }
2188 } else if (option =="--dump:raw_mapping_table") {
2189 dump_raw_mapping_table_ = true;
2190 } else if (option == "--dump:raw_gc_map") {
2191 dump_raw_gc_map_ = true;
2192 } else if (option == "--no-dump:vmap") {
2193 dump_vmap_ = false;
2194 } else if (option == "--no-disassemble") {
2195 disassemble_code_ = false;
2196 } else if (option.starts_with("--output=")) {
2197 output_name_ = option.substr(strlen("--output=")).ToString();
2198 const char* filename = output_name_.c_str();
2199 out_.reset(new std::ofstream(filename));
2200 if (!out_->good()) {
2201 fprintf(stderr, "Failed to open output filename %s\n", filename);
2202 usage();
2203 return false;
2204 }
2205 os_ = out_.get();
2206 } else if (option.starts_with("--symbolize=")) {
2207 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2208 symbolize_ = true;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002209 } else if (option.starts_with("--method-filter=")) {
2210 method_filter_ = option.substr(strlen("--method-filter=")).data();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002211 } else {
2212 fprintf(stderr, "Unknown argument %s\n", option.data());
2213 usage();
2214 return false;
2215 }
2216 }
2217
2218 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2219 fprintf(stderr, "Either --image or --oat must be specified\n");
2220 return false;
2221 }
2222
2223 if (image_location_ != nullptr && oat_filename_ != nullptr) {
2224 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
2225 return false;
2226 }
2227
2228 return true;
2229 }
2230
2231 const char* oat_filename_ = nullptr;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002232 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002233 const char* image_location_ = nullptr;
2234 const char* boot_image_location_ = nullptr;
2235 InstructionSet instruction_set_ = kRuntimeISA;
2236 std::string elf_filename_prefix_;
2237 std::ostream* os_ = &std::cout;
2238 std::unique_ptr<std::ofstream> out_;
2239 std::string output_name_;
2240 bool dump_raw_mapping_table_ = false;
2241 bool dump_raw_gc_map_ = false;
2242 bool dump_vmap_ = true;
2243 bool disassemble_code_ = true;
2244 bool symbolize_ = false;
2245};
2246
2247static int oatdump(int argc, char** argv) {
2248 InitLogging(argv);
2249
2250 OatdumpArgs args;
2251 if (!args.Parse(argc, argv)) {
2252 return EXIT_FAILURE;
2253 }
2254
2255 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2256 bool absolute_addresses = (args.oat_filename_ == nullptr);
2257
2258 std::unique_ptr<OatDumperOptions> oat_dumper_options(new OatDumperOptions(
2259 args.dump_raw_mapping_table_,
2260 args.dump_raw_gc_map_,
2261 args.dump_vmap_,
2262 args.disassemble_code_,
2263 absolute_addresses,
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002264 args.method_filter_,
Andreas Gampe00b25f32014-09-17 21:49:05 -07002265 nullptr));
2266
2267 std::unique_ptr<Runtime> runtime;
2268 if ((args.boot_image_location_ != nullptr || args.image_location_ != nullptr) &&
2269 !args.symbolize_) {
2270 // If we have a boot image option, try to start the runtime; except when just symbolizing.
2271 runtime.reset(StartRuntime(args.boot_image_location_,
2272 args.image_location_,
2273 args.instruction_set_));
Mathieu Chartierd424d082014-10-15 10:31:46 -07002274 } else {
2275 MemMap::Init();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002276 }
2277
2278 if (args.oat_filename_ != nullptr) {
2279 if (args.symbolize_) {
2280 return SymbolizeOat(args.oat_filename_, args.output_name_);
2281 } else {
2282 return DumpOat(runtime.get(), args.oat_filename_, oat_dumper_options.release(), args.os_);
2283 }
2284 }
2285
2286 if (runtime.get() == nullptr) {
2287 // We need the runtime when printing an image.
2288 return EXIT_FAILURE;
2289 }
2290
2291 return DumpImage(runtime.get(), args.image_location_, oat_dumper_options.release(), args.os_);
2292}
2293
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002294} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002295
2296int main(int argc, char** argv) {
2297 return art::oatdump(argc, argv);
2298}