blob: b048833431f0c0427cf9f8e7cb47fc3a6ad05ed9 [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 Rogers2bcb4a42012-11-08 10:39:18 -080035#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070036#include "gc/space/image_space.h"
37#include "gc/space/large_object_space.h"
38#include "gc/space/space-inl.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070039#include "image.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080040#include "indenter.h"
Ian Rogers1809a722013-08-09 22:05:32 -070041#include "mapping_table.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070042#include "mirror/art_field-inl.h"
43#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044#include "mirror/array-inl.h"
45#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046#include "mirror/object-inl.h"
47#include "mirror/object_array-inl.h"
Brian Carlstromc0a1b182014-03-04 23:19:06 -080048#include "noop_compiler_callbacks.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080049#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010050#include "oat_file-inl.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080051#include "os.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070052#include "output_stream.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070053#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070054#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055#include "scoped_thread_state_change.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070056#include "ScopedLocalRef.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080057#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080058#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080059#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070060#include "vmap_table.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070061#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070062
63namespace art {
64
65static void usage() {
66 fprintf(stderr,
67 "Usage: oatdump [options] ...\n"
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +000068 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080069 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070070 "\n");
71 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080072 " --oat-file=<file.oat>: specifies an input oat filename.\n"
TDYa127a0a2a6c2012-10-16 22:47:38 -070073 " Example: --oat-file=/system/framework/boot.oat\n"
Brian Carlstromaded5f72011-10-07 17:15:04 -070074 "\n");
75 fprintf(stderr,
76 " --image=<file.art>: specifies an input image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080077 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070078 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070079 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070080 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080081 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070082 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070083 fprintf(stderr,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070084 " --instruction-set=(arm|arm64|mips|x86|x86_64): for locating the image\n"
85 " file based on the image location set.\n"
Brian Carlstrom0f5baa02014-05-22 11:54:18 -070086 " Example: --instruction-set=x86\n"
87 " Default: %s\n"
88 "\n",
89 GetInstructionSetString(kRuntimeISA));
90 fprintf(stderr,
Brian Carlstrom27ec9612011-09-19 20:20:38 -070091 " --output=<file> may be used to send the output to a file.\n"
92 " Example: --output=/tmp/oatdump.txt\n"
93 "\n");
Dave Allison404f59f2014-02-24 11:10:01 -080094 fprintf(stderr,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070095 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
96 " Example: --dump:raw_mapping_table\n"
97 "\n");
98 fprintf(stderr,
99 " --dump:raw_mapping_table enables dumping of the GC map.\n"
100 " Example: --dump:raw_gc_map\n"
101 "\n");
102 fprintf(stderr,
103 " --no-dump:vmap may be used to disable vmap dumping.\n"
104 " Example: --no-dump:vmap\n"
105 "\n");
106 fprintf(stderr,
107 " --no-disassemble may be used to disable disassembly.\n"
108 " Example: --no-disassemble\n"
Dave Allison404f59f2014-02-24 11:10:01 -0800109 "\n");
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000110 fprintf(stderr,
111 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
112 " Example: --method-filter=foo\n"
113 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700114}
115
Ian Rogersff1ed472011-09-20 13:46:24 -0700116const char* image_roots_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -0800117 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -0700118 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700119 "kImtUnimplementedMethod",
Jeff Hao88474b42013-10-23 16:24:40 -0700120 "kDefaultImt",
Brian Carlstrome24fa612011-09-29 00:53:55 -0700121 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -0700122 "kRefsOnlySaveMethod",
123 "kRefsAndArgsSaveMethod",
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700124 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700125 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700126};
127
Ian Rogers0279ebb2014-10-08 17:27:48 -0700128class OatSymbolizer FINAL : public CodeOutput {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700129 public:
Ian Rogers0279ebb2014-10-08 17:27:48 -0700130 explicit OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
131 oat_file_(oat_file), builder_(nullptr), elf_output_(nullptr),
132 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
133 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700134
135 bool Init() {
136 Elf32_Word oat_data_size = oat_file_->GetOatHeader().GetExecutableOffset();
137
138 uint32_t diff = static_cast<uint32_t>(oat_file_->End() - oat_file_->Begin());
139 uint32_t oat_exec_size = diff - oat_data_size;
140
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700141 elf_output_ = OS::CreateEmptyFile(output_name_.c_str());
142
143 builder_.reset(new ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
144 Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(
145 this,
146 elf_output_,
147 oat_file_->GetOatHeader().GetInstructionSet(),
148 0,
149 oat_data_size,
150 oat_data_size,
151 oat_exec_size,
152 true,
153 false));
154
155 if (!builder_->Init()) {
156 builder_.reset(nullptr);
157 return false;
158 }
159
160 return true;
161 }
162
163 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
164 uint32_t,
165 const OatFile::OatMethod&,
166 const DexFile&,
167 uint32_t,
168 const DexFile::CodeItem*,
169 uint32_t);
170
171 bool Symbolize() {
172 if (builder_.get() == nullptr) {
173 return false;
174 }
175
176 Walk(&art::OatSymbolizer::RegisterForDedup);
177
178 NormalizeState();
179
180 Walk(&art::OatSymbolizer::AddSymbol);
181
182 bool result = builder_->Write();
183
Andreas Gampe4303ba92014-11-06 01:00:46 -0800184 // Ignore I/O errors.
185 UNUSED(elf_output_->FlushClose());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700186
187 return result;
188 }
189
190 void Walk(Callback callback) {
191 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
192 for (size_t i = 0; i < oat_dex_files.size(); i++) {
193 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
194 CHECK(oat_dex_file != NULL);
195 WalkOatDexFile(oat_dex_file, callback);
196 }
197 }
198
199 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
200 std::string error_msg;
201 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
202 if (dex_file.get() == nullptr) {
203 return;
204 }
205 for (size_t class_def_index = 0;
206 class_def_index < dex_file->NumClassDefs();
207 class_def_index++) {
208 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
209 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
210 OatClassType type = oat_class.GetType();
211 switch (type) {
212 case kOatClassAllCompiled:
213 case kOatClassSomeCompiled:
214 WalkOatClass(oat_class, *dex_file.get(), class_def, callback);
215 break;
216
217 case kOatClassNoneCompiled:
218 case kOatClassMax:
219 // Ignore.
220 break;
221 }
222 }
223 }
224
225 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
226 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700227 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700228 if (class_data == nullptr) { // empty class such as a marker interface?
229 return;
230 }
231 // Note: even if this is an interface or a native class, we still have to walk it, as there
232 // might be a static initializer.
233 ClassDataItemIterator it(dex_file, class_data);
234 SkipAllFields(&it);
235 uint32_t class_method_idx = 0;
236 while (it.HasNextDirectMethod()) {
237 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
238 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700239 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700240 class_method_idx++;
241 it.Next();
242 }
243 while (it.HasNextVirtualMethod()) {
244 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
245 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700246 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700247 class_method_idx++;
248 it.Next();
249 }
250 DCHECK(!it.HasNext());
251 }
252
253 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
254 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
255 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
256 uint32_t method_access_flags, Callback callback) {
257 if ((method_access_flags & kAccAbstract) != 0) {
258 // Abstract method, no code.
259 return;
260 }
261 if (oat_method.GetCodeOffset() == 0) {
262 // No code.
263 return;
264 }
265
266 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
267 method_access_flags);
268 }
269
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700270 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
271 uint32_t class_method_index ATTRIBUTE_UNUSED,
272 const OatFile::OatMethod& oat_method,
273 const DexFile& dex_file ATTRIBUTE_UNUSED,
274 uint32_t dex_method_idx ATTRIBUTE_UNUSED,
275 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
276 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700277 state_[oat_method.GetCodeOffset()]++;
278 }
279
280 void NormalizeState() {
281 for (auto& x : state_) {
282 if (x.second == 1) {
283 state_[x.first] = 0;
284 }
285 }
286 }
287
288 enum class DedupState { // private
289 kNotDeduplicated,
290 kDeduplicatedFirst,
291 kDeduplicatedOther
292 };
293 DedupState IsDuplicated(uint32_t offset) {
294 if (state_[offset] == 0) {
295 return DedupState::kNotDeduplicated;
296 }
297 if (state_[offset] == 1) {
298 return DedupState::kDeduplicatedOther;
299 }
300 state_[offset] = 1;
301 return DedupState::kDeduplicatedFirst;
302 }
303
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700304 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
305 uint32_t class_method_index ATTRIBUTE_UNUSED,
306 const OatFile::OatMethod& oat_method,
307 const DexFile& dex_file,
308 uint32_t dex_method_idx,
309 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
310 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700311 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
312 if (dedup != DedupState::kDeduplicatedOther) {
313 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
314
315 if (dedup == DedupState::kDeduplicatedFirst) {
316 pretty_name = "[Dedup]" + pretty_name;
317 }
318
319 ElfSymtabBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr,
Ian Rogers0279ebb2014-10-08 17:27:48 -0700320 Elf32_Sym, Elf32_Shdr>* symtab = builder_->GetSymtabBuilder();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700321
Ian Rogers0279ebb2014-10-08 17:27:48 -0700322 symtab->AddSymbol(pretty_name, &builder_->GetTextBuilder(),
323 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
324 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700325 }
326 }
327
Vladimir Markof4da6752014-08-01 19:04:18 +0100328 // Set oat data offset. Required by ElfBuilder/CodeOutput.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700329 void SetCodeOffset(size_t offset ATTRIBUTE_UNUSED) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100330 // Nothing to do.
331 }
332
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700333 // Write oat code. Required by ElfBuilder/CodeOutput.
334 bool Write(OutputStream* out) {
335 return out->WriteFully(oat_file_->Begin(), oat_file_->End() - oat_file_->Begin());
336 }
337
338 private:
339 static void SkipAllFields(ClassDataItemIterator* it) {
340 while (it->HasNextStaticField()) {
341 it->Next();
342 }
343 while (it->HasNextInstanceField()) {
344 it->Next();
345 }
346 }
347
348 const OatFile* oat_file_;
349 std::unique_ptr<ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
350 Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr> > builder_;
351 File* elf_output_;
352 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700353 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700354};
355
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700356class OatDumperOptions {
357 public:
358 OatDumperOptions(bool dump_raw_mapping_table,
359 bool dump_raw_gc_map,
360 bool dump_vmap,
361 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700362 bool absolute_addresses,
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000363 const char* method_filter,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700364 Handle<mirror::ClassLoader>* class_loader)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700365 : dump_raw_mapping_table_(dump_raw_mapping_table),
366 dump_raw_gc_map_(dump_raw_gc_map),
367 dump_vmap_(dump_vmap),
368 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700369 absolute_addresses_(absolute_addresses),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000370 method_filter_(method_filter),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700371 class_loader_(class_loader) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700372
373 const bool dump_raw_mapping_table_;
374 const bool dump_raw_gc_map_;
375 const bool dump_vmap_;
376 const bool disassemble_code_;
377 const bool absolute_addresses_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000378 const char* const method_filter_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700379 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700380};
381
Elliott Hughese3c845c2012-02-28 17:23:01 -0800382class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700383 public:
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700384 explicit OatDumper(const OatFile& oat_file, OatDumperOptions* options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000385 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800386 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700387 options_(options),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800388 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
389 disassembler_(Disassembler::Create(instruction_set_,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700390 new DisassemblerOptions(options_->absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000391 oat_file.Begin(),
392 true /* can_read_litals_ */))) {
Andreas Gampe00b25f32014-09-17 21:49:05 -0700393 CHECK(options_->class_loader_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800394 AddAllOffsets();
395 }
396
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700397 ~OatDumper() {
398 delete options_;
399 delete disassembler_;
400 }
401
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800402 InstructionSet GetInstructionSet() {
403 return instruction_set_;
404 }
405
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700406 bool Dump(std::ostream& os) {
407 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800408 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700409
410 os << "MAGIC:\n";
411 os << oat_header.GetMagic() << "\n\n";
412
413 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800414 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700415
Elliott Hughesa72ec822012-03-05 17:12:22 -0800416 os << "INSTRUCTION SET:\n";
417 os << oat_header.GetInstructionSet() << "\n\n";
418
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700419 {
420 std::unique_ptr<const InstructionSetFeatures> features(
421 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
422 oat_header.GetInstructionSetFeaturesBitmap()));
423 os << "INSTRUCTION SET FEATURES:\n";
424 os << features->GetFeatureString() << "\n\n";
425 }
Dave Allison70202782013-10-22 17:52:19 -0700426
Brian Carlstromaded5f72011-10-07 17:15:04 -0700427 os << "DEX FILE COUNT:\n";
428 os << oat_header.GetDexFileCount() << "\n\n";
429
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800430#define DUMP_OAT_HEADER_OFFSET(label, offset) \
431 os << label " OFFSET:\n"; \
432 os << StringPrintf("0x%08x", oat_header.offset()); \
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700433 if (oat_header.offset() != 0 && options_->absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800434 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
435 } \
436 os << StringPrintf("\n\n");
437
438 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
439 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
440 GetInterpreterToInterpreterBridgeOffset);
441 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
442 GetInterpreterToCompiledCodeBridgeOffset);
443 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
444 GetJniDlsymLookupOffset);
445 DUMP_OAT_HEADER_OFFSET("PORTABLE IMT CONFLICT TRAMPOLINE",
446 GetPortableImtConflictTrampolineOffset);
447 DUMP_OAT_HEADER_OFFSET("PORTABLE RESOLUTION TRAMPOLINE",
448 GetPortableResolutionTrampolineOffset);
449 DUMP_OAT_HEADER_OFFSET("PORTABLE TO INTERPRETER BRIDGE",
450 GetPortableToInterpreterBridgeOffset);
451 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
452 GetQuickGenericJniTrampolineOffset);
453 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
454 GetQuickImtConflictTrampolineOffset);
455 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
456 GetQuickResolutionTrampolineOffset);
457 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
458 GetQuickToInterpreterBridgeOffset);
459#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700460
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700461 os << "IMAGE PATCH DELTA:\n";
462 os << StringPrintf("%d (0x%08x)\n\n",
463 oat_header.GetImagePatchDelta(),
464 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700465
Brian Carlstrom28db0122012-10-18 16:20:41 -0700466 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
467 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
468
469 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800470 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700471
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700472 // Print the key-value store.
473 {
474 os << "KEY VALUE STORE:\n";
475 size_t index = 0;
476 const char* key;
477 const char* value;
478 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
479 os << key << " = " << value << "\n";
480 index++;
481 }
482 os << "\n";
483 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700484
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700485 if (options_->absolute_addresses_) {
486 os << "BEGIN:\n";
487 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700488
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700489 os << "END:\n";
490 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
491 }
492
493 os << "SIZE:\n";
494 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700495
496 os << std::flush;
497
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800498 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
499 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700500 CHECK(oat_dex_file != nullptr);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700501 if (!DumpOatDexFile(os, *oat_dex_file)) {
502 success = false;
503 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700504 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700505 os << std::flush;
506 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700507 }
508
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800509 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700510 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
511 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800512 return 0; // Address not in oat file
513 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800514 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
515 reinterpret_cast<uintptr_t>(oat_file_.Begin());
516 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800517 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800518 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800519 return end_offset - begin_offset;
520 }
521
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800522 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700523 return oat_file_.GetOatHeader().GetInstructionSet();
524 }
525
Ian Rogersef7d42f2014-01-06 12:55:46 -0800526 const void* GetQuickOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800527 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
528 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700529 CHECK(oat_dex_file != nullptr);
530 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700531 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700532 if (dex_file.get() == nullptr) {
533 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
534 << "': " << error_msg;
535 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800536 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700537 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800538 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700539 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700540 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100541 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800542 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100543 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800544 }
545 }
546 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700547 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800548 }
549
Brian Carlstromaded5f72011-10-07 17:15:04 -0700550 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800551 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800552 // We don't know the length of the code for each method, but we need to know where to stop
553 // when disassembling. What we do know is that a region of code will be followed by some other
554 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
555 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800556 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
557 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700558 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700559 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700560 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700561 if (dex_file.get() == nullptr) {
562 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
563 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800564 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800565 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800566 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800567 for (size_t class_def_index = 0;
568 class_def_index < dex_file->NumClassDefs();
569 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800570 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100571 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700572 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700573 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800574 ClassDataItemIterator it(*dex_file, class_data);
575 SkipAllFields(it);
576 uint32_t class_method_index = 0;
577 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100578 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800579 it.Next();
580 }
581 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100582 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800583 it.Next();
584 }
585 }
586 }
587 }
588
589 // If the last thing in the file is code for a method, there won't be an offset for the "next"
590 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
591 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800592 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800593 }
594
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700595 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
596 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
597 }
598
Elliott Hughese3c845c2012-02-28 17:23:01 -0800599 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800600 uint32_t code_offset = oat_method.GetCodeOffset();
601 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
602 code_offset &= ~0x1;
603 }
604 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800605 offsets_.insert(oat_method.GetMappingTableOffset());
606 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800607 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800608 }
609
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700610 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
611 bool success = true;
612 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800613 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800614 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700615
616 // Create the verifier early.
617
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700618 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700619 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700620 if (dex_file.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700621 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700622 os << std::flush;
623 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700624 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800625 for (size_t class_def_index = 0;
626 class_def_index < dex_file->NumClassDefs();
627 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700628 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
629 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700630 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100631 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700632 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
633 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100634 << " (" << oat_class.GetStatus() << ")"
635 << " (" << oat_class.GetType() << ")\n";
636 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800637 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
638 std::ostream indented_os(&indent_filter);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700639 if (!DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def)) {
640 success = false;
641 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700642 }
643
644 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700645 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700646 }
647
Elliott Hughese3c845c2012-02-28 17:23:01 -0800648 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700649 while (it.HasNextStaticField()) {
650 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700651 }
Ian Rogers0571d352011-11-03 19:51:38 -0700652 while (it.HasNextInstanceField()) {
653 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700654 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800655 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700656
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700657 bool DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800658 const DexFile::ClassDef& class_def) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700659 bool success = true;
Ian Rogers13735952014-10-08 12:43:28 -0700660 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700661 if (class_data == nullptr) { // empty class such as a marker interface?
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700662 os << std::flush;
663 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800664 }
665 ClassDataItemIterator it(dex_file, class_data);
666 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700667 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700668 while (it.HasNextDirectMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700669 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
670 it.GetMemberIndex(), it.GetMethodCodeItem(),
671 it.GetRawMemberAccessFlags())) {
672 success = false;
673 }
674 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700675 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700676 }
Ian Rogers0571d352011-11-03 19:51:38 -0700677 while (it.HasNextVirtualMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700678 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
679 it.GetMemberIndex(), it.GetMethodCodeItem(),
680 it.GetRawMemberAccessFlags())) {
681 success = false;
682 }
683 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700684 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700685 }
Ian Rogers0571d352011-11-03 19:51:38 -0700686 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700687 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700688 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700689 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800690
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700691 static constexpr uint32_t kPrologueBytes = 16;
692
693 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
694 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
695
696 bool DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700697 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700698 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800699 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
700 uint32_t method_access_flags) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700701 bool success = true;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000702 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
703 if (pretty_method.find(options_->method_filter_) == std::string::npos) {
704 return success;
705 }
706
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800707 os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000708 class_method_index, pretty_method.c_str(),
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700709 dex_method_idx);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800710 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700711 std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter));
712 Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count);
713 std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter));
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800714 {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700715 *indent1_os << "DEX CODE:\n";
716 DumpDexCode(*indent2_os, dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700717 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700718
719 std::unique_ptr<verifier::MethodVerifier> verifier;
720 if (Runtime::Current() != nullptr) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700721 *indent1_os << "VERIFIER TYPE ANALYSIS:\n";
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700722 verifier.reset(DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item,
723 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700724 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700725
726 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
727 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
728 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800729 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700730 *indent1_os << "OatMethodOffsets ";
731 if (options_->absolute_addresses_) {
732 *indent1_os << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100733 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700734 *indent1_os << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
735 if (oat_method_offsets_offset > oat_file_.Size()) {
736 *indent1_os << StringPrintf(
737 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
738 oat_method_offsets_offset, oat_file_.Size());
739 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
740 os << std::flush;
741 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800742 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700743
744 uint32_t code_offset = oat_method.GetCodeOffset();
745 *indent2_os << StringPrintf("code_offset: 0x%08x ", code_offset);
746 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
747 if (aligned_code_begin > oat_file_.Size()) {
748 *indent2_os << StringPrintf("WARNING: "
749 "code offset 0x%08x is past end of file 0x%08zx.\n",
750 aligned_code_begin, oat_file_.Size());
751 success = false;
752 }
753 *indent2_os << "\n";
754
755 *indent2_os << "gc_map: ";
756 if (options_->absolute_addresses_) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800757 *indent2_os << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700758 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800759 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700760 *indent2_os << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
761 if (gc_map_offset > oat_file_.Size()) {
762 *indent2_os << StringPrintf("WARNING: "
763 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
764 gc_map_offset, oat_file_.Size());
765 success = false;
766 } else if (options_->dump_raw_gc_map_) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700767 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800768 std::ostream indent3_os(&indent3_filter);
769 DumpGcMap(indent3_os, oat_method, code_item);
770 }
771 }
772 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700773 *indent1_os << "OatQuickMethodHeader ";
774 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
775 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700776
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700777 if (options_->absolute_addresses_) {
778 *indent1_os << StringPrintf("%p ", method_header);
779 }
780 *indent1_os << StringPrintf("(offset=0x%08x)\n", method_header_offset);
781 if (method_header_offset > oat_file_.Size()) {
782 *indent1_os << StringPrintf(
783 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
784 method_header_offset, oat_file_.Size());
785 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
786 os << std::flush;
787 return false;
788 }
789
790 *indent2_os << "mapping_table: ";
791 if (options_->absolute_addresses_) {
792 *indent2_os << StringPrintf("%p ", oat_method.GetMappingTable());
793 }
794 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
795 *indent2_os << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
796 if (mapping_table_offset > oat_file_.Size()) {
797 *indent2_os << StringPrintf("WARNING: "
798 "mapping table offset 0x%08x is past end of file 0x%08zx. "
799 "mapping table offset was loaded from offset 0x%08x.\n",
800 mapping_table_offset, oat_file_.Size(),
801 oat_method.GetMappingTableOffsetOffset());
802 success = false;
803 } else if (options_->dump_raw_mapping_table_) {
804 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
805 std::ostream indent3_os(&indent3_filter);
806 DumpMappingTable(indent3_os, oat_method);
807 }
808
809 *indent2_os << "vmap_table: ";
810 if (options_->absolute_addresses_) {
811 *indent2_os << StringPrintf("%p ", oat_method.GetVmapTable());
812 }
813 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
814 *indent2_os << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
815 if (vmap_table_offset > oat_file_.Size()) {
816 *indent2_os << StringPrintf("WARNING: "
817 "vmap table offset 0x%08x is past end of file 0x%08zx. "
818 "vmap table offset was loaded from offset 0x%08x.\n",
819 vmap_table_offset, oat_file_.Size(),
820 oat_method.GetVmapTableOffsetOffset());
821 success = false;
822 } else if (options_->dump_vmap_) {
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +0100823 DumpVmap(*indent2_os, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700824 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800825 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700826 {
827 *indent1_os << "QuickMethodFrameInfo\n";
828
829 *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
830 *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
831 DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false);
832 *indent2_os << "\n";
833 *indent2_os << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
834 DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true);
835 *indent2_os << "\n";
836 }
837 {
838 // Based on spill masks from QuickMethodFrameInfo so placed
839 // after it is dumped, but useful for understanding quick
840 // code, so dumped here.
841 DumpVregLocations(*indent2_os, oat_method, code_item);
842 }
843 {
844 *indent1_os << "CODE: ";
845 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
846 if (code_size_offset > oat_file_.Size()) {
847 *indent2_os << StringPrintf("WARNING: "
848 "code size offset 0x%08x is past end of file 0x%08zx.",
849 code_size_offset, oat_file_.Size());
850 success = false;
851 } else {
852 const void* code = oat_method.GetQuickCode();
853 uint32_t code_size = oat_method.GetQuickCodeSize();
854 if (code == nullptr) {
855 code = oat_method.GetPortableCode();
856 code_size = oat_method.GetPortableCodeSize();
857 code_size_offset = 0;
858 }
859 uint32_t code_offset = oat_method.GetCodeOffset();
860 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
861 uint64_t aligned_code_end = aligned_code_begin + code_size;
862
863 if (options_->absolute_addresses_) {
864 *indent1_os << StringPrintf("%p ", code);
865 }
866 *indent1_os << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
867 code_offset,
868 code_size_offset,
869 code_size,
870 code != nullptr ? "..." : "");
871
872 if (aligned_code_begin > oat_file_.Size()) {
873 *indent2_os << StringPrintf("WARNING: "
874 "start of code at 0x%08x is past end of file 0x%08zx.",
875 aligned_code_begin, oat_file_.Size());
876 success = false;
877 } else if (aligned_code_end > oat_file_.Size()) {
878 *indent2_os << StringPrintf("WARNING: "
879 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
880 "code size is 0x%08x loaded from offset 0x%08x.\n",
881 aligned_code_end, oat_file_.Size(),
882 code_size, code_size_offset);
883 success = false;
884 if (options_->disassemble_code_) {
885 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
886 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
887 }
888 }
889 } else if (code_size > kMaxCodeSize) {
890 *indent2_os << StringPrintf("WARNING: "
891 "code size %d is bigger than max expected threshold of %d. "
892 "code size is 0x%08x loaded from offset 0x%08x.\n",
893 code_size, kMaxCodeSize,
894 code_size, code_size_offset);
895 success = false;
896 if (options_->disassemble_code_) {
897 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
898 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
899 }
900 }
901 } else if (options_->disassemble_code_) {
902 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, !success, 0);
903 }
904 }
905 }
906 os << std::flush;
907 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700908 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800909
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800910 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
911 if (spill_mask == 0) {
912 return;
913 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800914 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800915 for (size_t i = 0; i < 32; i++) {
916 if ((spill_mask & (1 << i)) != 0) {
917 if (is_float) {
918 os << "fr" << i;
919 } else {
920 os << "r" << i;
921 }
922 spill_mask ^= 1 << i; // clear bit
923 if (spill_mask != 0) {
924 os << ", ";
925 } else {
926 break;
927 }
928 }
929 }
930 os << ")";
931 }
932
Ian Rogersb23a7722012-10-09 16:54:26 -0700933 void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) {
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +0100934 // If the native GC map is null, then this method has been compiled with the
935 // optimizing compiler. The optimizing compiler currently outputs its stack map
936 // in the vmap table, and the code below does not work with such a stack map.
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800937 if (oat_method.GetGcMap() == nullptr) {
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +0100938 return;
939 }
Ian Rogers1809a722013-08-09 22:05:32 -0700940 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700941 if (raw_table != nullptr) {
Ian Rogers1809a722013-08-09 22:05:32 -0700942 const VmapTable vmap_table(raw_table);
943 bool first = true;
944 bool processing_fp = false;
945 uint32_t spill_mask = oat_method.GetCoreSpillMask();
946 for (size_t i = 0; i < vmap_table.Size(); i++) {
947 uint16_t dex_reg = vmap_table[i];
948 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
949 processing_fp ? kFloatVReg : kIntVReg);
950 os << (first ? "v" : ", v") << dex_reg;
951 if (!processing_fp) {
952 os << "/r" << cpu_reg;
953 } else {
954 os << "/fr" << cpu_reg;
955 }
956 first = false;
957 if (!processing_fp && dex_reg == 0xFFFF) {
958 processing_fp = true;
959 spill_mask = oat_method.GetFpSpillMask();
960 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800961 }
Ian Rogers1809a722013-08-09 22:05:32 -0700962 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800963 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800964 }
965
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -0700966 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
967 const DexFile::CodeItem* code_item) {
968 if (code_item != nullptr) {
969 size_t num_locals_ins = code_item->registers_size_;
970 size_t num_ins = code_item->ins_size_;
971 size_t num_locals = num_locals_ins - num_ins;
972 size_t num_outs = code_item->outs_size_;
973
974 os << "vr_stack_locations:";
975 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
976 // For readability, delimit the different kinds of VRs.
977 if (reg == num_locals_ins) {
978 os << "\n\tmethod*:";
979 } else if (reg == num_locals && num_ins > 0) {
980 os << "\n\tins:";
981 } else if (reg == 0 && num_locals > 0) {
982 os << "\n\tlocals:";
983 }
984
985 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
986 oat_method.GetFpSpillMask(),
987 oat_method.GetFrameSizeInBytes(), reg,
988 GetInstructionSet());
989 os << " v" << reg << "[sp + #" << offset << "]";
990 }
991
992 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
993 if (out_reg == 0) {
994 os << "\n\touts:";
995 }
996
997 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
998 os << " v" << out_reg << "[sp + #" << offset << "]";
999 }
1000
1001 os << "\n";
1002 }
1003 }
1004
Ian Rogersb23a7722012-10-09 16:54:26 -07001005 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001006 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001007 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001008 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001009 const VmapTable vmap_table(raw_table);
1010 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001011 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001012 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1013 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1014 : oat_method.GetCoreSpillMask();
1015 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001016 } else {
1017 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
1018 oat_method.GetFpSpillMask(),
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001019 oat_method.GetFrameSizeInBytes(), reg,
1020 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001021 os << "[sp + #" << offset << "]";
1022 }
1023 }
1024 }
1025
Ian Rogersef7d42f2014-01-06 12:55:46 -08001026 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1027 const DexFile::CodeItem* code_item,
1028 size_t num_regs, const uint8_t* reg_bitmap) {
1029 bool first = true;
1030 for (size_t reg = 0; reg < num_regs; reg++) {
1031 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1032 if (first) {
1033 os << " v" << reg << " (";
1034 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1035 os << ")";
1036 first = false;
1037 } else {
1038 os << ", v" << reg << " (";
1039 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1040 os << ")";
1041 }
1042 }
1043 }
1044 if (first) {
1045 os << "No registers in GC map\n";
1046 } else {
1047 os << "\n";
1048 }
1049 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001050 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1051 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001052 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001053 if (gc_map_raw == nullptr) {
1054 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001055 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001056 const void* quick_code = oat_method.GetQuickCode();
1057 if (quick_code != nullptr) {
1058 NativePcOffsetToReferenceMap map(gc_map_raw);
1059 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1060 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1061 map.GetNativePcOffset(entry);
1062 os << StringPrintf("%p", native_pc);
1063 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001064 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001065 } else {
1066 const void* portable_code = oat_method.GetPortableCode();
1067 CHECK(portable_code != nullptr);
1068 verifier::DexPcToReferenceMap map(gc_map_raw);
1069 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1070 uint32_t dex_pc = map.GetDexPc(entry);
1071 os << StringPrintf("0x%08x", dex_pc);
1072 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
1073 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001074 }
1075 }
1076
1077 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001078 const void* quick_code = oat_method.GetQuickCode();
1079 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001080 return;
1081 }
Ian Rogers1809a722013-08-09 22:05:32 -07001082 MappingTable table(oat_method.GetMappingTable());
1083 if (table.TotalSize() != 0) {
1084 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1085 std::ostream indent_os(&indent_filter);
1086 if (table.PcToDexSize() != 0) {
1087 typedef MappingTable::PcToDexIterator It;
1088 os << "suspend point mappings {\n";
1089 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1090 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1091 }
1092 os << "}\n";
1093 }
1094 if (table.DexToPcSize() != 0) {
1095 typedef MappingTable::DexToPcIterator It;
1096 os << "catch entry mappings {\n";
1097 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1098 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1099 }
1100 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001101 }
1102 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001103 }
1104
Ian Rogers1809a722013-08-09 22:05:32 -07001105 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1106 size_t offset, bool suspend_point_mapping) {
1107 MappingTable table(oat_method.GetMappingTable());
1108 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1109 typedef MappingTable::PcToDexIterator It;
1110 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1111 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001112 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001113 return cur.DexPc();
1114 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001115 }
Ian Rogers1809a722013-08-09 22:05:32 -07001116 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1117 typedef MappingTable::DexToPcIterator It;
1118 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1119 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001120 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001121 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001122 }
1123 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001124 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001125 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001126 }
1127
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001128 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1129 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001130 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001131 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001132 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001133 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001134 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001135 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001136 bool first = true;
1137 for (size_t reg = 0; reg < num_regs; reg++) {
1138 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1139 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001140 os << "GC map objects: v" << reg << " (";
1141 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001142 os << ")";
1143 first = false;
1144 } else {
1145 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001146 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001147 os << ")";
1148 }
1149 }
1150 }
1151 if (!first) {
1152 os << "\n";
1153 }
1154 }
1155 }
1156 }
1157
Mathieu Chartier590fee92013-09-13 13:46:47 -07001158 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1159 const OatFile::OatMethod& oat_method,
1160 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1161 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001162 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001163 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001164 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1165 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1166 if (kind != kUndefined) {
1167 if (first) {
1168 os << "VRegs: v";
1169 first = false;
1170 } else {
1171 os << ", v";
1172 }
1173 os << reg << " (";
1174 switch (kind) {
1175 case kImpreciseConstant:
1176 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1177 DescribeVReg(os, oat_method, code_item, reg, kind);
1178 break;
1179 case kConstant:
1180 os << "Constant: " << kinds.at((reg * 2) + 1);
1181 break;
1182 default:
1183 DescribeVReg(os, oat_method, code_item, reg, kind);
1184 break;
1185 }
1186 os << ")";
1187 }
1188 }
1189 if (!first) {
1190 os << "\n";
1191 }
1192 }
1193
1194
Ian Rogersb23a7722012-10-09 16:54:26 -07001195 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001196 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001197 size_t i = 0;
1198 while (i < code_item->insns_size_in_code_units_) {
1199 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001200 os << StringPrintf("0x%04zx: %s\n", i, instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001201 i += instruction->SizeInCodeUnits();
1202 }
1203 }
1204 }
1205
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001206 verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx,
1207 const DexFile* dex_file,
1208 const DexFile::ClassDef& class_def,
1209 const DexFile::CodeItem* code_item,
1210 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001211 if ((method_access_flags & kAccNative) == 0) {
1212 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001213 StackHandleScope<1> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001214 Handle<mirror::DexCache> dex_cache(
1215 hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
Andreas Gampe00b25f32014-09-17 21:49:05 -07001216 DCHECK(options_->class_loader_ != nullptr);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001217 return verifier::MethodVerifier::VerifyMethodAndDump(soa.Self(), os, dex_method_idx, dex_file,
1218 dex_cache,
Andreas Gampe00b25f32014-09-17 21:49:05 -07001219 *options_->class_loader_,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001220 &class_def, code_item,
1221 NullHandle<mirror::ArtMethod>(),
1222 method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001223 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001224
1225 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001226 }
1227
Mathieu Chartier590fee92013-09-13 13:46:47 -07001228 void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001229 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1230 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001231 const void* portable_code = oat_method.GetPortableCode();
1232 const void* quick_code = oat_method.GetQuickCode();
1233
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001234 if (code_size == 0) {
1235 code_size = oat_method.GetQuickCodeSize();
1236 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001237 if ((code_size == 0) || ((portable_code == nullptr) && (quick_code == nullptr))) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001238 os << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001239 return;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001240 } else if (quick_code != nullptr) {
1241 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1242 size_t offset = 0;
1243 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001244 if (!bad_input) {
1245 DumpMappingAtOffset(os, oat_method, offset, false);
1246 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001247 offset += disassembler_->Dump(os, quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001248 if (!bad_input) {
1249 uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
1250 if (dex_pc != DexFile::kDexNoIndex) {
1251 DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
1252 if (verifier != nullptr) {
1253 DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
1254 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001255 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001256 }
1257 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001258 } else {
1259 CHECK(portable_code != nullptr);
1260 CHECK_EQ(code_size, 0U); // TODO: disassembly of portable is currently not supported.
Ian Rogersb23a7722012-10-09 16:54:26 -07001261 }
1262 }
1263
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001264 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001265 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
1266 const OatDumperOptions* options_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001267 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001268 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001269 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001270};
1271
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001272class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001273 public:
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001274 explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001275 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
1276 : os_(os),
1277 image_space_(image_space),
1278 image_header_(image_header),
1279 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001280
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001281 bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001282 std::ostream& os = *os_;
1283 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001284
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001285 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001286
Mathieu Chartier31e89252013-08-28 11:29:12 -07001287 os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
1288 << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
1289
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001290 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001291
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001292 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001293
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001294 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1295
1296 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1297
1298 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001299
Alex Lighta59dd802014-07-02 16:28:08 -07001300 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1301
Igor Murashkin46774762014-10-22 11:37:02 -07001302 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1303
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001304 {
1305 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
1306 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1307 std::ostream indent1_os(&indent1_filter);
1308 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
1309 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1310 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1311 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001312 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001313 indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
1314 if (image_root_object->IsObjectArray()) {
1315 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
1316 std::ostream indent2_os(&indent2_filter);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001317 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001318 = image_root_object->AsObjectArray<mirror::Object>();
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001319 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1320 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001321 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001322 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1323 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001324 run++;
1325 } else {
1326 break;
1327 }
1328 }
1329 if (run == 0) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001330 indent2_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001331 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001332 indent2_os << StringPrintf("%d to %zd: ", j, j + run);
1333 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001334 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001335 if (value != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001336 PrettyObjectValue(indent2_os, value->GetClass(), value);
1337 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001338 indent2_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001339 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001340 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001341 }
1342 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001343 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001344 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001345
Brian Carlstromaded5f72011-10-07 17:15:04 -07001346 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001347 std::string image_filename = image_space_.GetImageFilename();
1348 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001349 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001350 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001351 std::string error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -07001352 const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1353 if (oat_file == nullptr) {
Igor Murashkin46774762014-10-22 11:37:02 -07001354 oat_file = OatFile::Open(oat_location, oat_location, nullptr, nullptr, false, &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001355 if (oat_file == nullptr) {
1356 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001357 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001358 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001359 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001360 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001361
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001362 stats_.oat_file_bytes = oat_file->Size();
1363
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001364 oat_dumper_.reset(new OatDumper(*oat_file, oat_dumper_options_.release()));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001365
Mathieu Chartier02e25112013-08-14 16:14:24 -07001366 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001367 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001368 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1369 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001370 }
1371
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001372 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001373
1374 // Loop through all the image spaces and dump their objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001375 gc::Heap* heap = Runtime::Current()->GetHeap();
1376 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07001377 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001378 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001379 {
1380 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1381 heap->FlushAllocStack();
1382 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001383 // Since FlushAllocStack() above resets the (active) allocation
1384 // stack. Need to revoke the thread-local allocation stacks that
1385 // point into it.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001386 {
1387 self->TransitionFromRunnableToSuspended(kNative);
1388 ThreadList* thread_list = Runtime::Current()->GetThreadList();
1389 thread_list->SuspendAll();
1390 heap->RevokeAllThreadLocalAllocationStacks(self);
1391 thread_list->ResumeAll();
1392 self->TransitionFromSuspendedToRunnable();
1393 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001394 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001395 {
1396 std::ostream* saved_os = os_;
1397 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1398 std::ostream indent_os(&indent_filter);
1399 os_ = &indent_os;
1400 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001401 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001402 if (space->IsImageSpace()) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001403 gc::space::ImageSpace* image_space = space->AsImageSpace();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001404 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1405 indent_os << "\n";
1406 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001407 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001408 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001409 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001410 indent_os << "\n";
1411 os_ = saved_os;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001412 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001413 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001414 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001415 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001416 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001417 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001418 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001419 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001420 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001421 size_t header_bytes = sizeof(ImageHeader);
1422 stats_.header_bytes = header_bytes;
1423 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1424 stats_.alignment_bytes += alignment_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001425 stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
1426 stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001427 stats_.Dump(os);
1428 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001429
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001430 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001431
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001432 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001433 }
1434
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001435 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001436 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001437 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001438 CHECK(type != nullptr);
1439 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001440 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001441 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001442 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001443 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001444 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001445 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001446 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001447 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001448 } else if (type->IsArtFieldClass()) {
1449 mirror::ArtField* field = value->AsArtField();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001450 os << StringPrintf("%p Field: %s\n", field, PrettyField(field).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001451 } else if (type->IsArtMethodClass()) {
1452 mirror::ArtMethod* method = value->AsArtMethod();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001453 os << StringPrintf("%p Method: %s\n", method, PrettyMethod(method).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001454 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001455 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001456 }
1457 }
1458
Brian Carlstromea46f952013-07-30 01:26:50 -07001459 static void PrintField(std::ostream& os, mirror::ArtField* field, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001460 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001461 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001462 switch (field->GetTypeAsPrimitiveType()) {
1463 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001464 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001465 break;
1466 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001467 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001468 break;
1469 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001470 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001471 break;
1472 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001473 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001474 break;
1475 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001476 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001477 break;
1478 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001479 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001480 break;
1481 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001482 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1483 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001484 break;
1485 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001486 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001487 break;
1488 case Primitive::kPrimNot: {
1489 // Get the value, don't compute the type unless it is non-null as we don't want
1490 // to cause class loading.
1491 mirror::Object* value = field->GetObj(obj);
1492 if (value == nullptr) {
1493 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001494 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001495 // Grab the field type without causing resolution.
1496 mirror::Class* field_type = field->GetType(false);
1497 if (field_type != nullptr) {
1498 PrettyObjectValue(os, field_type, value);
1499 } else {
1500 os << StringPrintf("%p %s\n", value,
1501 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1502 }
Ian Rogers50239c72013-06-17 14:53:22 -07001503 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001504 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001505 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001506 default:
1507 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1508 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001509 }
1510 }
1511
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001512 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001513 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001514 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001515 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001516 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001517 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001518 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001519 if (fields != nullptr) {
Ian Rogersd5b32602012-02-26 16:40:04 -08001520 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001521 mirror::ArtField* field = fields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001522 PrintField(os, field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001523 }
1524 }
1525 }
1526
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001527 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001528 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001529 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001530
Ian Rogersef7d42f2014-01-06 12:55:46 -08001531 const void* GetQuickOatCodeBegin(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001532 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001533 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1534 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001535 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001536 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001537 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001538 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001539 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001540 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001541 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001542 }
1543
Ian Rogersef7d42f2014-01-06 12:55:46 -08001544 uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001545 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001546 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1547 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001548 return 0;
1549 }
1550 return oat_code_begin[-1];
1551 }
1552
Ian Rogersef7d42f2014-01-06 12:55:46 -08001553 const void* GetQuickOatCodeEnd(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001554 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001555 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001556 if (oat_code_begin == nullptr) {
1557 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001558 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001559 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001560 }
1561
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001562 static void Callback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001563 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001564 DCHECK(obj != nullptr);
1565 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001566 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001567 if (!state->InDumpSpace(obj)) {
1568 return;
1569 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001570
1571 size_t object_bytes = obj->SizeOf();
1572 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1573 state->stats_.object_bytes += object_bytes;
1574 state->stats_.alignment_bytes += alignment_bytes;
1575
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001576 std::ostream& os = *state->os_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001577 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001578 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001579 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1580 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001581 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001582 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001583 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1584 << klass->GetStatus() << ")\n";
Brian Carlstromea46f952013-07-30 01:26:50 -07001585 } else if (obj->IsArtField()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001586 os << StringPrintf("%p: java.lang.reflect.ArtField %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001587 PrettyField(obj->AsArtField()).c_str());
1588 } else if (obj->IsArtMethod()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001589 os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001590 PrettyMethod(obj->AsArtMethod()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001591 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001592 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001593 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001594 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001595 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001596 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001597 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1598 std::ostream indent_os(&indent_filter);
1599 DumpFields(indent_os, obj, obj_class);
Ian Rogersd5b32602012-02-26 16:40:04 -08001600 if (obj->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001601 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Ian Rogersd5b32602012-02-26 16:40:04 -08001602 int32_t length = obj_array->GetLength();
1603 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001604 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001605 size_t run = 0;
1606 for (int32_t j = i + 1; j < length; j++) {
1607 if (value == obj_array->Get(j)) {
1608 run++;
1609 } else {
1610 break;
1611 }
1612 }
1613 if (run == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001614 indent_os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001615 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001616 indent_os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001617 i = i + run;
1618 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001619 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001620 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001621 PrettyObjectValue(indent_os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001622 }
1623 } else if (obj->IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001624 mirror::ObjectArray<mirror::ArtField>* sfields = obj->AsClass()->GetSFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001625 if (sfields != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001626 indent_os << "STATICS:\n";
1627 Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
1628 std::ostream indent2_os(&indent2_filter);
Ian Rogersd5b32602012-02-26 16:40:04 -08001629 for (int32_t i = 0; i < sfields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001630 mirror::ArtField* field = sfields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001631 PrintField(indent2_os, field, field->GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001632 }
1633 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001634 } else if (obj->IsArtMethod()) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001635 const size_t image_pointer_size = InstructionSetPointerSize(
1636 state->oat_dumper_->GetOatInstructionSet());
Brian Carlstromea46f952013-07-30 01:26:50 -07001637 mirror::ArtMethod* method = obj->AsArtMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001638 if (method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001639 // TODO: portable dumping.
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001640 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001641 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001642 bool first_occurrence;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001643 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1644 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1645 state->ComputeOatSize(quick_oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001646 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001647 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001648 }
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001649 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCodePtrSize(
1650 image_pointer_size)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001651 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001652 }
Ian Rogers19846512012-02-24 11:42:47 -08001653 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
Jeff Hao88474b42013-10-23 16:24:40 -07001654 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001655 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001656 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001657 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001658 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001659 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -07001660 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001661 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001662
Elliott Hughesa0e18062012-04-13 15:59:59 -07001663 bool first_occurrence;
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001664 size_t gc_map_bytes =
1665 state->ComputeOatSize(method->GetNativeGcMap(image_pointer_size), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001666 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001667 state->stats_.gc_map_bytes += gc_map_bytes;
1668 }
1669
1670 size_t pc_mapping_table_bytes =
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001671 state->ComputeOatSize(method->GetMappingTable(image_pointer_size), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001672 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001673 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1674 }
1675
1676 size_t vmap_table_bytes =
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001677 state->ComputeOatSize(method->GetVmapTable(image_pointer_size), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001678 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001679 state->stats_.vmap_table_bytes += vmap_table_bytes;
1680 }
1681
Ian Rogersef7d42f2014-01-06 12:55:46 -08001682 // TODO: portable dumping.
1683 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1684 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1685 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1686 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001687 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001688 state->stats_.managed_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001689 if (method->IsConstructor()) {
1690 if (method->IsStatic()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001691 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001692 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001693 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001694 }
1695 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001696 state->stats_.large_method_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001697 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001698 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001699 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001700
Ian Rogersef7d42f2014-01-06 12:55:46 -08001701 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001702 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
1703 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001704
1705 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersef7d42f2014-01-06 12:55:46 -08001706 vmap_table_bytes + quick_oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001707
1708 double expansion =
Ian Rogersef7d42f2014-01-06 12:55:46 -08001709 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001710 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001711 }
1712 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001713 std::string temp;
1714 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001715 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001716
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001717 std::set<const void*> already_seen_;
1718 // Compute the size of the given data within the oat file and whether this is the first time
1719 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001720 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001721 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001722 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001723 already_seen_.insert(oat_data);
1724 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001725 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001726 }
1727 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001728 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001729
1730 public:
1731 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001732 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001733 size_t file_bytes;
1734
1735 size_t header_bytes;
1736 size_t object_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001737 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001738 size_t alignment_bytes;
1739
1740 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001741 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001742 size_t managed_to_native_code_bytes;
1743 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001744 size_t class_initializer_code_bytes;
1745 size_t large_initializer_code_bytes;
1746 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001747
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001748 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001749 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001750 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001751
1752 size_t dex_instruction_bytes;
1753
Brian Carlstromea46f952013-07-30 01:26:50 -07001754 std::vector<mirror::ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001755 std::vector<size_t> method_outlier_size;
1756 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07001757 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001758
1759 explicit Stats()
1760 : oat_file_bytes(0),
1761 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001762 header_bytes(0),
1763 object_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07001764 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001765 alignment_bytes(0),
1766 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001767 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001768 managed_to_native_code_bytes(0),
1769 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001770 class_initializer_code_bytes(0),
1771 large_initializer_code_bytes(0),
1772 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001773 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001774 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001775 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001776 dex_instruction_bytes(0) {}
1777
Elliott Hughesa0e18062012-04-13 15:59:59 -07001778 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001779 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07001780 size_t bytes;
1781 size_t count;
1782 };
1783 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1784 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001785
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001786 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001787 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1788 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001789 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001790 it->second.count += 1;
1791 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001792 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07001793 }
1794 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001795
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001796 double PercentOfOatBytes(size_t size) {
1797 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1798 }
1799
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001800 double PercentOfFileBytes(size_t size) {
1801 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1802 }
1803
1804 double PercentOfObjectBytes(size_t size) {
1805 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1806 }
1807
Brian Carlstromea46f952013-07-30 01:26:50 -07001808 void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001809 method_outlier_size.push_back(total_size);
1810 method_outlier_expansion.push_back(expansion);
1811 method_outlier.push_back(method);
1812 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001813
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001814 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001815 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001816 size_t sum_of_sizes = 0;
1817 size_t sum_of_sizes_squared = 0;
1818 size_t sum_of_expansion = 0;
1819 size_t sum_of_expansion_squared = 0;
1820 size_t n = method_outlier_size.size();
1821 for (size_t i = 0; i < n; i++) {
1822 size_t cur_size = method_outlier_size[i];
1823 sum_of_sizes += cur_size;
1824 sum_of_sizes_squared += cur_size * cur_size;
1825 double cur_expansion = method_outlier_expansion[i];
1826 sum_of_expansion += cur_expansion;
1827 sum_of_expansion_squared += cur_expansion * cur_expansion;
1828 }
1829 size_t size_mean = sum_of_sizes / n;
1830 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1831 double expansion_mean = sum_of_expansion / n;
1832 double expansion_variance =
1833 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1834
1835 // Dump methods whose size is a certain number of standard deviations from the mean
1836 size_t dumped_values = 0;
1837 size_t skipped_values = 0;
1838 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1839 size_t cur_size_variance = i * i * size_variance;
1840 bool first = true;
1841 for (size_t j = 0; j < n; j++) {
1842 size_t cur_size = method_outlier_size[j];
1843 if (cur_size > size_mean) {
1844 size_t cur_var = cur_size - size_mean;
1845 cur_var = cur_var * cur_var;
1846 if (cur_var > cur_size_variance) {
1847 if (dumped_values > 20) {
1848 if (i == 1) {
1849 skipped_values++;
1850 } else {
1851 i = 2; // jump to counting for 1 standard deviation
1852 break;
1853 }
1854 } else {
1855 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001856 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001857 first = false;
1858 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001859 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001860 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001861 method_outlier_size[j] = 0; // don't consider this method again
1862 dumped_values++;
1863 }
1864 }
1865 }
1866 }
1867 }
1868 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001869 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001870 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001871 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001872 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001873
1874 // Dump methods whose expansion is a certain number of standard deviations from the mean
1875 dumped_values = 0;
1876 skipped_values = 0;
1877 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1878 double cur_expansion_variance = i * i * expansion_variance;
1879 bool first = true;
1880 for (size_t j = 0; j < n; j++) {
1881 double cur_expansion = method_outlier_expansion[j];
1882 if (cur_expansion > expansion_mean) {
1883 size_t cur_var = cur_expansion - expansion_mean;
1884 cur_var = cur_var * cur_var;
1885 if (cur_var > cur_expansion_variance) {
1886 if (dumped_values > 20) {
1887 if (i == 1) {
1888 skipped_values++;
1889 } else {
1890 i = 2; // jump to counting for 1 standard deviation
1891 break;
1892 }
1893 } else {
1894 if (first) {
1895 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001896 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001897 first = false;
1898 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001899 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001900 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001901 method_outlier_expansion[j] = 0.0; // don't consider this method again
1902 dumped_values++;
1903 }
1904 }
1905 }
1906 }
1907 }
1908 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001909 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001910 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001911 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001912 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001913 }
1914
Ian Rogersb726dcb2012-09-05 08:57:23 -07001915 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001916 {
1917 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1918 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
1919 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1920 std::ostream indent_os(&indent_filter);
1921 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
1922 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier32327092013-08-30 14:04:08 -07001923 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001924 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
1925 header_bytes, PercentOfFileBytes(header_bytes),
1926 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07001927 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001928 alignment_bytes, PercentOfFileBytes(alignment_bytes))
1929 << std::flush;
Mathieu Chartier32327092013-08-30 14:04:08 -07001930 CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001931 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001932
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001933 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001934 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07001935 for (const auto& sizes_and_count : sizes_and_counts) {
1936 const std::string& descriptor(sizes_and_count.first);
1937 double average = static_cast<double>(sizes_and_count.second.bytes) /
1938 static_cast<double>(sizes_and_count.second.count);
1939 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001940 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001941 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001942 descriptor.c_str(), sizes_and_count.second.bytes,
1943 sizes_and_count.second.count, average, percent);
1944 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001945 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001946 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001947 CHECK_EQ(object_bytes, object_bytes_total);
1948
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001949 os << StringPrintf("oat_file_bytes = %8zd\n"
1950 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1951 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1952 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1953 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1954 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1955 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001956 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001957 managed_code_bytes,
1958 PercentOfOatBytes(managed_code_bytes),
1959 managed_to_native_code_bytes,
1960 PercentOfOatBytes(managed_to_native_code_bytes),
1961 native_to_managed_code_bytes,
1962 PercentOfOatBytes(native_to_managed_code_bytes),
1963 class_initializer_code_bytes,
1964 PercentOfOatBytes(class_initializer_code_bytes),
1965 large_initializer_code_bytes,
1966 PercentOfOatBytes(large_initializer_code_bytes),
1967 large_method_code_bytes,
1968 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001969 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07001970 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001971 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001972 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
1973 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07001974 }
1975
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001976 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1977 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1978 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001979 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1980 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1981 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001982 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001983
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001984 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
1985 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001986 static_cast<double>(managed_code_bytes) /
1987 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07001988 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001989 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001990 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001991
1992 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001993 }
1994 } stats_;
1995
1996 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001997 enum {
1998 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1999 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2000 kLargeConstructorDexBytes = 4000,
2001 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2002 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2003 kLargeMethodDexBytes = 16000
2004 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002005 std::ostream* os_;
Ian Rogers1d54e732013-05-02 21:10:01 -07002006 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002007 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002008 std::unique_ptr<OatDumper> oat_dumper_;
2009 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002010
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002011 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002012};
2013
Andreas Gampe00b25f32014-09-17 21:49:05 -07002014static NoopCompilerCallbacks callbacks;
Elliott Hughes72395bf2012-04-24 13:45:26 -07002015
Andreas Gampe00b25f32014-09-17 21:49:05 -07002016static Runtime* StartRuntime(const char* boot_image_location, const char* image_location,
2017 InstructionSet instruction_set) {
Ian Rogerse63db272014-07-15 15:36:11 -07002018 RuntimeOptions options;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002019 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07002020 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002021 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07002022 std::string boot_oat_option;
Brian Carlstrom6c871802013-07-17 14:25:35 -07002023
2024 // We are more like a compiler than a run-time. We don't want to execute code.
Brian Carlstromc0a1b182014-03-04 23:19:06 -08002025 options.push_back(std::make_pair("compilercallbacks", &callbacks));
Brian Carlstrom6c871802013-07-17 14:25:35 -07002026
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002027 if (boot_image_location != nullptr) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -07002028 boot_image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002029 boot_image_option += boot_image_location;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002030 options.push_back(std::make_pair(boot_image_option.c_str(), nullptr));
Brian Carlstrom78128a62011-09-15 17:21:19 -07002031 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002032 if (image_location != nullptr) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07002033 image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002034 image_option += image_location;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002035 options.push_back(std::make_pair(image_option.c_str(), nullptr));
Brian Carlstromaded5f72011-10-07 17:15:04 -07002036 }
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002037 options.push_back(
2038 std::make_pair("imageinstructionset",
2039 reinterpret_cast<const void*>(GetInstructionSetString(instruction_set))));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07002040
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002041 if (!Runtime::Create(options, false)) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07002042 fprintf(stderr, "Failed to create runtime\n");
Andreas Gampe00b25f32014-09-17 21:49:05 -07002043 return nullptr;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002044 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002045
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002046 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
Ian Rogersb9beb2e2014-05-09 16:57:40 -07002047 // give it away now and then switch to a more manageable ScopedObjectAccess.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002048 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002049
2050 return Runtime::Current();
2051}
2052
2053static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
2054 std::ostream* os) {
2055 // Dumping the image, no explicit class loader.
2056 NullHandle<mirror::ClassLoader> null_class_loader;
2057 options->class_loader_ = &null_class_loader;
2058
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002059 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002060 gc::Heap* heap = runtime->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -07002061 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002062 CHECK(image_space != nullptr);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002063 const ImageHeader& image_header = image_space->GetImageHeader();
2064 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002065 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002066 return EXIT_FAILURE;
2067 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002068 ImageDumper image_dumper(os, *image_space, image_header, options);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002069 bool success = image_dumper.Dump();
2070 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002071}
2072
Andreas Gampe00b25f32014-09-17 21:49:05 -07002073static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2074 std::ostream* os) {
2075 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2076
2077 Thread* self = Thread::Current();
2078 CHECK(self != nullptr);
2079 // Need well-known-classes.
2080 WellKnownClasses::Init(self->GetJniEnv());
2081
2082 // Need to register dex files to get a working dex cache.
2083 ScopedObjectAccess soa(self);
2084 ClassLinker* class_linker = runtime->GetClassLinker();
2085 class_linker->RegisterOatFile(oat_file);
2086 std::vector<const DexFile*> dex_files;
2087 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2088 std::string error_msg;
2089 const DexFile* dex_file = odf->OpenDexFile(&error_msg);
2090 CHECK(dex_file != nullptr) << error_msg;
2091 class_linker->RegisterDexFile(*dex_file);
2092 dex_files.push_back(dex_file);
2093 }
2094
2095 // Need a class loader.
2096 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader);
2097 ScopedLocalRef<jobject> class_loader_local(soa.Env(),
2098 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
2099 jobject class_loader = soa.Env()->NewGlobalRef(class_loader_local.get());
2100 // Fake that we're a compiler.
2101 runtime->SetCompileTimeClassPath(class_loader, dex_files);
2102
2103 // Use the class loader while dumping.
2104 StackHandleScope<1> scope(self);
2105 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2106 soa.Decode<mirror::ClassLoader*>(class_loader));
2107 options->class_loader_ = &loader_handle;
2108
2109 OatDumper oat_dumper(*oat_file, options);
2110 bool success = oat_dumper.Dump(*os);
2111 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2112}
2113
2114static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
2115 // No image = no class loader.
2116 NullHandle<mirror::ClassLoader> null_class_loader;
2117 options->class_loader_ = &null_class_loader;
2118
2119 OatDumper oat_dumper(*oat_file, options);
2120 bool success = oat_dumper.Dump(*os);
2121 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2122}
2123
2124static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2125 std::ostream* os) {
2126 std::string error_msg;
Igor Murashkin46774762014-10-22 11:37:02 -07002127 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002128 if (oat_file == nullptr) {
2129 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2130 return EXIT_FAILURE;
2131 }
2132
2133 if (runtime != nullptr) {
2134 return DumpOatWithRuntime(runtime, oat_file, options, os);
2135 } else {
2136 return DumpOatWithoutRuntime(oat_file, options, os);
2137 }
2138}
2139
2140static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2141 std::string error_msg;
Igor Murashkin46774762014-10-22 11:37:02 -07002142 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002143 if (oat_file == nullptr) {
2144 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2145 return EXIT_FAILURE;
2146 }
2147
2148 OatSymbolizer oat_symbolizer(oat_file, output_name);
2149 if (!oat_symbolizer.Init()) {
2150 fprintf(stderr, "Failed to initialize symbolizer\n");
2151 return EXIT_FAILURE;
2152 }
2153 if (!oat_symbolizer.Symbolize()) {
2154 fprintf(stderr, "Failed to symbolize\n");
2155 return EXIT_FAILURE;
2156 }
2157
2158 return EXIT_SUCCESS;
2159}
2160
2161struct OatdumpArgs {
2162 bool Parse(int argc, char** argv) {
2163 // Skip over argv[0].
2164 argv++;
2165 argc--;
2166
2167 if (argc == 0) {
2168 fprintf(stderr, "No arguments specified\n");
2169 usage();
2170 return false;
2171 }
2172
2173 for (int i = 0; i < argc; i++) {
2174 const StringPiece option(argv[i]);
2175 if (option.starts_with("--oat-file=")) {
2176 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2177 } else if (option.starts_with("--image=")) {
2178 image_location_ = option.substr(strlen("--image=")).data();
2179 } else if (option.starts_with("--boot-image=")) {
2180 boot_image_location_ = option.substr(strlen("--boot-image=")).data();
2181 } else if (option.starts_with("--instruction-set=")) {
2182 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
2183 instruction_set_ = GetInstructionSetFromString(instruction_set_str.data());
2184 if (instruction_set_ == kNone) {
2185 fprintf(stderr, "Unsupported instruction set %s\n", instruction_set_str.data());
2186 usage();
2187 return false;
2188 }
2189 } else if (option =="--dump:raw_mapping_table") {
2190 dump_raw_mapping_table_ = true;
2191 } else if (option == "--dump:raw_gc_map") {
2192 dump_raw_gc_map_ = true;
2193 } else if (option == "--no-dump:vmap") {
2194 dump_vmap_ = false;
2195 } else if (option == "--no-disassemble") {
2196 disassemble_code_ = false;
2197 } else if (option.starts_with("--output=")) {
2198 output_name_ = option.substr(strlen("--output=")).ToString();
2199 const char* filename = output_name_.c_str();
2200 out_.reset(new std::ofstream(filename));
2201 if (!out_->good()) {
2202 fprintf(stderr, "Failed to open output filename %s\n", filename);
2203 usage();
2204 return false;
2205 }
2206 os_ = out_.get();
2207 } else if (option.starts_with("--symbolize=")) {
2208 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2209 symbolize_ = true;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002210 } else if (option.starts_with("--method-filter=")) {
2211 method_filter_ = option.substr(strlen("--method-filter=")).data();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002212 } else {
2213 fprintf(stderr, "Unknown argument %s\n", option.data());
2214 usage();
2215 return false;
2216 }
2217 }
2218
2219 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2220 fprintf(stderr, "Either --image or --oat must be specified\n");
2221 return false;
2222 }
2223
2224 if (image_location_ != nullptr && oat_filename_ != nullptr) {
2225 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
2226 return false;
2227 }
2228
2229 return true;
2230 }
2231
2232 const char* oat_filename_ = nullptr;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002233 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002234 const char* image_location_ = nullptr;
2235 const char* boot_image_location_ = nullptr;
2236 InstructionSet instruction_set_ = kRuntimeISA;
2237 std::string elf_filename_prefix_;
2238 std::ostream* os_ = &std::cout;
2239 std::unique_ptr<std::ofstream> out_;
2240 std::string output_name_;
2241 bool dump_raw_mapping_table_ = false;
2242 bool dump_raw_gc_map_ = false;
2243 bool dump_vmap_ = true;
2244 bool disassemble_code_ = true;
2245 bool symbolize_ = false;
2246};
2247
2248static int oatdump(int argc, char** argv) {
2249 InitLogging(argv);
2250
2251 OatdumpArgs args;
2252 if (!args.Parse(argc, argv)) {
2253 return EXIT_FAILURE;
2254 }
2255
2256 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2257 bool absolute_addresses = (args.oat_filename_ == nullptr);
2258
2259 std::unique_ptr<OatDumperOptions> oat_dumper_options(new OatDumperOptions(
2260 args.dump_raw_mapping_table_,
2261 args.dump_raw_gc_map_,
2262 args.dump_vmap_,
2263 args.disassemble_code_,
2264 absolute_addresses,
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002265 args.method_filter_,
Andreas Gampe00b25f32014-09-17 21:49:05 -07002266 nullptr));
2267
2268 std::unique_ptr<Runtime> runtime;
2269 if ((args.boot_image_location_ != nullptr || args.image_location_ != nullptr) &&
2270 !args.symbolize_) {
2271 // If we have a boot image option, try to start the runtime; except when just symbolizing.
2272 runtime.reset(StartRuntime(args.boot_image_location_,
2273 args.image_location_,
2274 args.instruction_set_));
Mathieu Chartierd424d082014-10-15 10:31:46 -07002275 } else {
2276 MemMap::Init();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002277 }
2278
2279 if (args.oat_filename_ != nullptr) {
2280 if (args.symbolize_) {
2281 return SymbolizeOat(args.oat_filename_, args.output_name_);
2282 } else {
2283 return DumpOat(runtime.get(), args.oat_filename_, oat_dumper_options.release(), args.os_);
2284 }
2285 }
2286
2287 if (runtime.get() == nullptr) {
2288 // We need the runtime when printing an image.
2289 return EXIT_FAILURE;
2290 }
2291
2292 return DumpImage(runtime.get(), args.image_location_, oat_dumper_options.release(), args.os_);
2293}
2294
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002295} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002296
2297int main(int argc, char** argv) {
2298 return art::oatdump(argc, argv);
2299}