blob: f1f1a56ae4e1a568717b481780025073fb5f73cb [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
Elliott Hughese222ee02012-12-13 14:41:43 -080026#include "base/stringpiece.h"
Elliott Hughes76160052012-12-12 16:31:20 -080027#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070028#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080031#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080032#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070033#include "elf_builder.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070034#include "field_helper.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
184 elf_output_->Flush();
185 elf_output_->Close();
186
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),
388 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet(),
389 new DisassemblerOptions(options_->absolute_addresses_,
390 oat_file.Begin()))) {
Andreas Gampe00b25f32014-09-17 21:49:05 -0700391 CHECK(options_->class_loader_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800392 AddAllOffsets();
393 }
394
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700395 ~OatDumper() {
396 delete options_;
397 delete disassembler_;
398 }
399
400 bool Dump(std::ostream& os) {
401 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800402 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700403
404 os << "MAGIC:\n";
405 os << oat_header.GetMagic() << "\n\n";
406
407 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800408 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700409
Elliott Hughesa72ec822012-03-05 17:12:22 -0800410 os << "INSTRUCTION SET:\n";
411 os << oat_header.GetInstructionSet() << "\n\n";
412
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700413 {
414 std::unique_ptr<const InstructionSetFeatures> features(
415 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
416 oat_header.GetInstructionSetFeaturesBitmap()));
417 os << "INSTRUCTION SET FEATURES:\n";
418 os << features->GetFeatureString() << "\n\n";
419 }
Dave Allison70202782013-10-22 17:52:19 -0700420
Brian Carlstromaded5f72011-10-07 17:15:04 -0700421 os << "DEX FILE COUNT:\n";
422 os << oat_header.GetDexFileCount() << "\n\n";
423
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800424#define DUMP_OAT_HEADER_OFFSET(label, offset) \
425 os << label " OFFSET:\n"; \
426 os << StringPrintf("0x%08x", oat_header.offset()); \
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700427 if (oat_header.offset() != 0 && options_->absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800428 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
429 } \
430 os << StringPrintf("\n\n");
431
432 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
433 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
434 GetInterpreterToInterpreterBridgeOffset);
435 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
436 GetInterpreterToCompiledCodeBridgeOffset);
437 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
438 GetJniDlsymLookupOffset);
439 DUMP_OAT_HEADER_OFFSET("PORTABLE IMT CONFLICT TRAMPOLINE",
440 GetPortableImtConflictTrampolineOffset);
441 DUMP_OAT_HEADER_OFFSET("PORTABLE RESOLUTION TRAMPOLINE",
442 GetPortableResolutionTrampolineOffset);
443 DUMP_OAT_HEADER_OFFSET("PORTABLE TO INTERPRETER BRIDGE",
444 GetPortableToInterpreterBridgeOffset);
445 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
446 GetQuickGenericJniTrampolineOffset);
447 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
448 GetQuickImtConflictTrampolineOffset);
449 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
450 GetQuickResolutionTrampolineOffset);
451 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
452 GetQuickToInterpreterBridgeOffset);
453#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700454
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700455 os << "IMAGE PATCH DELTA:\n";
456 os << StringPrintf("%d (0x%08x)\n\n",
457 oat_header.GetImagePatchDelta(),
458 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700459
Brian Carlstrom28db0122012-10-18 16:20:41 -0700460 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
461 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
462
463 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800464 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700465
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700466 // Print the key-value store.
467 {
468 os << "KEY VALUE STORE:\n";
469 size_t index = 0;
470 const char* key;
471 const char* value;
472 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
473 os << key << " = " << value << "\n";
474 index++;
475 }
476 os << "\n";
477 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700478
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700479 if (options_->absolute_addresses_) {
480 os << "BEGIN:\n";
481 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700482
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700483 os << "END:\n";
484 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
485 }
486
487 os << "SIZE:\n";
488 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700489
490 os << std::flush;
491
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800492 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
493 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700494 CHECK(oat_dex_file != nullptr);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700495 if (!DumpOatDexFile(os, *oat_dex_file)) {
496 success = false;
497 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700498 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700499 os << std::flush;
500 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700501 }
502
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800503 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700504 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
505 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800506 return 0; // Address not in oat file
507 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800508 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
509 reinterpret_cast<uintptr_t>(oat_file_.Begin());
510 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800511 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800512 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800513 return end_offset - begin_offset;
514 }
515
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700516 InstructionSet GetInstructionSet() {
517 return oat_file_.GetOatHeader().GetInstructionSet();
518 }
519
Ian Rogersef7d42f2014-01-06 12:55:46 -0800520 const void* GetQuickOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800521 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
522 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700523 CHECK(oat_dex_file != nullptr);
524 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700525 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700526 if (dex_file.get() == nullptr) {
527 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
528 << "': " << error_msg;
529 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800530 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700531 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800532 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700533 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700534 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100535 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800536 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100537 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800538 }
539 }
540 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700541 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800542 }
543
Brian Carlstromaded5f72011-10-07 17:15:04 -0700544 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800545 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800546 // We don't know the length of the code for each method, but we need to know where to stop
547 // when disassembling. What we do know is that a region of code will be followed by some other
548 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
549 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800550 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
551 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700552 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700553 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700554 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700555 if (dex_file.get() == nullptr) {
556 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
557 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800558 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800559 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800560 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800561 for (size_t class_def_index = 0;
562 class_def_index < dex_file->NumClassDefs();
563 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800564 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100565 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700566 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700567 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800568 ClassDataItemIterator it(*dex_file, class_data);
569 SkipAllFields(it);
570 uint32_t class_method_index = 0;
571 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100572 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800573 it.Next();
574 }
575 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100576 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800577 it.Next();
578 }
579 }
580 }
581 }
582
583 // If the last thing in the file is code for a method, there won't be an offset for the "next"
584 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
585 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800586 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800587 }
588
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700589 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
590 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
591 }
592
Elliott Hughese3c845c2012-02-28 17:23:01 -0800593 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800594 uint32_t code_offset = oat_method.GetCodeOffset();
595 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
596 code_offset &= ~0x1;
597 }
598 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800599 offsets_.insert(oat_method.GetMappingTableOffset());
600 offsets_.insert(oat_method.GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700601 offsets_.insert(oat_method.GetNativeGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800602 }
603
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700604 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
605 bool success = true;
606 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800607 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800608 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700609
610 // Create the verifier early.
611
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700612 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700613 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700614 if (dex_file.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700615 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700616 os << std::flush;
617 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700618 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800619 for (size_t class_def_index = 0;
620 class_def_index < dex_file->NumClassDefs();
621 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700622 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
623 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700624 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100625 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700626 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
627 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100628 << " (" << oat_class.GetStatus() << ")"
629 << " (" << oat_class.GetType() << ")\n";
630 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800631 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
632 std::ostream indented_os(&indent_filter);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700633 if (!DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def)) {
634 success = false;
635 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700636 }
637
638 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700639 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700640 }
641
Elliott Hughese3c845c2012-02-28 17:23:01 -0800642 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700643 while (it.HasNextStaticField()) {
644 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700645 }
Ian Rogers0571d352011-11-03 19:51:38 -0700646 while (it.HasNextInstanceField()) {
647 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700648 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800649 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700650
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700651 bool DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800652 const DexFile::ClassDef& class_def) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700653 bool success = true;
Ian Rogers13735952014-10-08 12:43:28 -0700654 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700655 if (class_data == nullptr) { // empty class such as a marker interface?
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700656 os << std::flush;
657 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800658 }
659 ClassDataItemIterator it(dex_file, class_data);
660 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700661 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700662 while (it.HasNextDirectMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700663 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
664 it.GetMemberIndex(), it.GetMethodCodeItem(),
665 it.GetRawMemberAccessFlags())) {
666 success = false;
667 }
668 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700669 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700670 }
Ian Rogers0571d352011-11-03 19:51:38 -0700671 while (it.HasNextVirtualMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700672 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
673 it.GetMemberIndex(), it.GetMethodCodeItem(),
674 it.GetRawMemberAccessFlags())) {
675 success = false;
676 }
677 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700678 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700679 }
Ian Rogers0571d352011-11-03 19:51:38 -0700680 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700681 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700682 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700683 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800684
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700685 static constexpr uint32_t kPrologueBytes = 16;
686
687 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
688 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
689
690 bool DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700691 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700692 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800693 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
694 uint32_t method_access_flags) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700695 bool success = true;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000696 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
697 if (pretty_method.find(options_->method_filter_) == std::string::npos) {
698 return success;
699 }
700
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800701 os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000702 class_method_index, pretty_method.c_str(),
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700703 dex_method_idx);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800704 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700705 std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter));
706 Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count);
707 std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter));
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800708 {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700709 *indent1_os << "DEX CODE:\n";
710 DumpDexCode(*indent2_os, dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700711 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700712
713 std::unique_ptr<verifier::MethodVerifier> verifier;
714 if (Runtime::Current() != nullptr) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700715 *indent1_os << "VERIFIER TYPE ANALYSIS:\n";
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700716 verifier.reset(DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item,
717 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700718 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700719
720 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
721 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
722 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800723 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700724 *indent1_os << "OatMethodOffsets ";
725 if (options_->absolute_addresses_) {
726 *indent1_os << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100727 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700728 *indent1_os << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
729 if (oat_method_offsets_offset > oat_file_.Size()) {
730 *indent1_os << StringPrintf(
731 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
732 oat_method_offsets_offset, oat_file_.Size());
733 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
734 os << std::flush;
735 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800736 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700737
738 uint32_t code_offset = oat_method.GetCodeOffset();
739 *indent2_os << StringPrintf("code_offset: 0x%08x ", code_offset);
740 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
741 if (aligned_code_begin > oat_file_.Size()) {
742 *indent2_os << StringPrintf("WARNING: "
743 "code offset 0x%08x is past end of file 0x%08zx.\n",
744 aligned_code_begin, oat_file_.Size());
745 success = false;
746 }
747 *indent2_os << "\n";
748
749 *indent2_os << "gc_map: ";
750 if (options_->absolute_addresses_) {
751 *indent2_os << StringPrintf("%p ", oat_method.GetNativeGcMap());
752 }
753 uint32_t gc_map_offset = oat_method.GetNativeGcMapOffset();
754 *indent2_os << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
755 if (gc_map_offset > oat_file_.Size()) {
756 *indent2_os << StringPrintf("WARNING: "
757 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
758 gc_map_offset, oat_file_.Size());
759 success = false;
760 } else if (options_->dump_raw_gc_map_) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700761 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800762 std::ostream indent3_os(&indent3_filter);
763 DumpGcMap(indent3_os, oat_method, code_item);
764 }
765 }
766 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700767 *indent1_os << "OatQuickMethodHeader ";
768 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
769 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700770
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700771 if (options_->absolute_addresses_) {
772 *indent1_os << StringPrintf("%p ", method_header);
773 }
774 *indent1_os << StringPrintf("(offset=0x%08x)\n", method_header_offset);
775 if (method_header_offset > oat_file_.Size()) {
776 *indent1_os << StringPrintf(
777 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
778 method_header_offset, oat_file_.Size());
779 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
780 os << std::flush;
781 return false;
782 }
783
784 *indent2_os << "mapping_table: ";
785 if (options_->absolute_addresses_) {
786 *indent2_os << StringPrintf("%p ", oat_method.GetMappingTable());
787 }
788 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
789 *indent2_os << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
790 if (mapping_table_offset > oat_file_.Size()) {
791 *indent2_os << StringPrintf("WARNING: "
792 "mapping table offset 0x%08x is past end of file 0x%08zx. "
793 "mapping table offset was loaded from offset 0x%08x.\n",
794 mapping_table_offset, oat_file_.Size(),
795 oat_method.GetMappingTableOffsetOffset());
796 success = false;
797 } else if (options_->dump_raw_mapping_table_) {
798 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
799 std::ostream indent3_os(&indent3_filter);
800 DumpMappingTable(indent3_os, oat_method);
801 }
802
803 *indent2_os << "vmap_table: ";
804 if (options_->absolute_addresses_) {
805 *indent2_os << StringPrintf("%p ", oat_method.GetVmapTable());
806 }
807 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
808 *indent2_os << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
809 if (vmap_table_offset > oat_file_.Size()) {
810 *indent2_os << StringPrintf("WARNING: "
811 "vmap table offset 0x%08x is past end of file 0x%08zx. "
812 "vmap table offset was loaded from offset 0x%08x.\n",
813 vmap_table_offset, oat_file_.Size(),
814 oat_method.GetVmapTableOffsetOffset());
815 success = false;
816 } else if (options_->dump_vmap_) {
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +0100817 DumpVmap(*indent2_os, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700818 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800819 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700820 {
821 *indent1_os << "QuickMethodFrameInfo\n";
822
823 *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
824 *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
825 DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false);
826 *indent2_os << "\n";
827 *indent2_os << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
828 DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true);
829 *indent2_os << "\n";
830 }
831 {
832 // Based on spill masks from QuickMethodFrameInfo so placed
833 // after it is dumped, but useful for understanding quick
834 // code, so dumped here.
835 DumpVregLocations(*indent2_os, oat_method, code_item);
836 }
837 {
838 *indent1_os << "CODE: ";
839 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
840 if (code_size_offset > oat_file_.Size()) {
841 *indent2_os << StringPrintf("WARNING: "
842 "code size offset 0x%08x is past end of file 0x%08zx.",
843 code_size_offset, oat_file_.Size());
844 success = false;
845 } else {
846 const void* code = oat_method.GetQuickCode();
847 uint32_t code_size = oat_method.GetQuickCodeSize();
848 if (code == nullptr) {
849 code = oat_method.GetPortableCode();
850 code_size = oat_method.GetPortableCodeSize();
851 code_size_offset = 0;
852 }
853 uint32_t code_offset = oat_method.GetCodeOffset();
854 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
855 uint64_t aligned_code_end = aligned_code_begin + code_size;
856
857 if (options_->absolute_addresses_) {
858 *indent1_os << StringPrintf("%p ", code);
859 }
860 *indent1_os << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
861 code_offset,
862 code_size_offset,
863 code_size,
864 code != nullptr ? "..." : "");
865
866 if (aligned_code_begin > oat_file_.Size()) {
867 *indent2_os << StringPrintf("WARNING: "
868 "start of code at 0x%08x is past end of file 0x%08zx.",
869 aligned_code_begin, oat_file_.Size());
870 success = false;
871 } else if (aligned_code_end > oat_file_.Size()) {
872 *indent2_os << StringPrintf("WARNING: "
873 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
874 "code size is 0x%08x loaded from offset 0x%08x.\n",
875 aligned_code_end, oat_file_.Size(),
876 code_size, code_size_offset);
877 success = false;
878 if (options_->disassemble_code_) {
879 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
880 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
881 }
882 }
883 } else if (code_size > kMaxCodeSize) {
884 *indent2_os << StringPrintf("WARNING: "
885 "code size %d is bigger than max expected threshold of %d. "
886 "code size is 0x%08x loaded from offset 0x%08x.\n",
887 code_size, kMaxCodeSize,
888 code_size, code_size_offset);
889 success = false;
890 if (options_->disassemble_code_) {
891 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
892 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
893 }
894 }
895 } else if (options_->disassemble_code_) {
896 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, !success, 0);
897 }
898 }
899 }
900 os << std::flush;
901 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700902 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800903
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800904 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
905 if (spill_mask == 0) {
906 return;
907 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800908 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800909 for (size_t i = 0; i < 32; i++) {
910 if ((spill_mask & (1 << i)) != 0) {
911 if (is_float) {
912 os << "fr" << i;
913 } else {
914 os << "r" << i;
915 }
916 spill_mask ^= 1 << i; // clear bit
917 if (spill_mask != 0) {
918 os << ", ";
919 } else {
920 break;
921 }
922 }
923 }
924 os << ")";
925 }
926
Ian Rogersb23a7722012-10-09 16:54:26 -0700927 void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) {
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +0100928 // If the native GC map is null, then this method has been compiled with the
929 // optimizing compiler. The optimizing compiler currently outputs its stack map
930 // in the vmap table, and the code below does not work with such a stack map.
931 if (oat_method.GetNativeGcMap() == nullptr) {
932 return;
933 }
Ian Rogers1809a722013-08-09 22:05:32 -0700934 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700935 if (raw_table != nullptr) {
Ian Rogers1809a722013-08-09 22:05:32 -0700936 const VmapTable vmap_table(raw_table);
937 bool first = true;
938 bool processing_fp = false;
939 uint32_t spill_mask = oat_method.GetCoreSpillMask();
940 for (size_t i = 0; i < vmap_table.Size(); i++) {
941 uint16_t dex_reg = vmap_table[i];
942 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
943 processing_fp ? kFloatVReg : kIntVReg);
944 os << (first ? "v" : ", v") << dex_reg;
945 if (!processing_fp) {
946 os << "/r" << cpu_reg;
947 } else {
948 os << "/fr" << cpu_reg;
949 }
950 first = false;
951 if (!processing_fp && dex_reg == 0xFFFF) {
952 processing_fp = true;
953 spill_mask = oat_method.GetFpSpillMask();
954 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800955 }
Ian Rogers1809a722013-08-09 22:05:32 -0700956 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800957 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800958 }
959
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -0700960 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
961 const DexFile::CodeItem* code_item) {
962 if (code_item != nullptr) {
963 size_t num_locals_ins = code_item->registers_size_;
964 size_t num_ins = code_item->ins_size_;
965 size_t num_locals = num_locals_ins - num_ins;
966 size_t num_outs = code_item->outs_size_;
967
968 os << "vr_stack_locations:";
969 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
970 // For readability, delimit the different kinds of VRs.
971 if (reg == num_locals_ins) {
972 os << "\n\tmethod*:";
973 } else if (reg == num_locals && num_ins > 0) {
974 os << "\n\tins:";
975 } else if (reg == 0 && num_locals > 0) {
976 os << "\n\tlocals:";
977 }
978
979 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
980 oat_method.GetFpSpillMask(),
981 oat_method.GetFrameSizeInBytes(), reg,
982 GetInstructionSet());
983 os << " v" << reg << "[sp + #" << offset << "]";
984 }
985
986 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
987 if (out_reg == 0) {
988 os << "\n\touts:";
989 }
990
991 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
992 os << " v" << out_reg << "[sp + #" << offset << "]";
993 }
994
995 os << "\n";
996 }
997 }
998
Ian Rogersb23a7722012-10-09 16:54:26 -0700999 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001000 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001001 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001002 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001003 const VmapTable vmap_table(raw_table);
1004 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001005 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001006 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1007 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1008 : oat_method.GetCoreSpillMask();
1009 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001010 } else {
1011 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
1012 oat_method.GetFpSpillMask(),
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001013 oat_method.GetFrameSizeInBytes(), reg,
1014 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001015 os << "[sp + #" << offset << "]";
1016 }
1017 }
1018 }
1019
Ian Rogersef7d42f2014-01-06 12:55:46 -08001020 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1021 const DexFile::CodeItem* code_item,
1022 size_t num_regs, const uint8_t* reg_bitmap) {
1023 bool first = true;
1024 for (size_t reg = 0; reg < num_regs; reg++) {
1025 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1026 if (first) {
1027 os << " v" << reg << " (";
1028 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1029 os << ")";
1030 first = false;
1031 } else {
1032 os << ", v" << reg << " (";
1033 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1034 os << ")";
1035 }
1036 }
1037 }
1038 if (first) {
1039 os << "No registers in GC map\n";
1040 } else {
1041 os << "\n";
1042 }
1043 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001044 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1045 const DexFile::CodeItem* code_item) {
1046 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001047 if (gc_map_raw == nullptr) {
1048 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001049 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001050 const void* quick_code = oat_method.GetQuickCode();
1051 if (quick_code != nullptr) {
1052 NativePcOffsetToReferenceMap map(gc_map_raw);
1053 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1054 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1055 map.GetNativePcOffset(entry);
1056 os << StringPrintf("%p", native_pc);
1057 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001058 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001059 } else {
1060 const void* portable_code = oat_method.GetPortableCode();
1061 CHECK(portable_code != nullptr);
1062 verifier::DexPcToReferenceMap map(gc_map_raw);
1063 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1064 uint32_t dex_pc = map.GetDexPc(entry);
1065 os << StringPrintf("0x%08x", dex_pc);
1066 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
1067 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001068 }
1069 }
1070
1071 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001072 const void* quick_code = oat_method.GetQuickCode();
1073 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001074 return;
1075 }
Ian Rogers1809a722013-08-09 22:05:32 -07001076 MappingTable table(oat_method.GetMappingTable());
1077 if (table.TotalSize() != 0) {
1078 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1079 std::ostream indent_os(&indent_filter);
1080 if (table.PcToDexSize() != 0) {
1081 typedef MappingTable::PcToDexIterator It;
1082 os << "suspend point mappings {\n";
1083 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1084 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1085 }
1086 os << "}\n";
1087 }
1088 if (table.DexToPcSize() != 0) {
1089 typedef MappingTable::DexToPcIterator It;
1090 os << "catch entry mappings {\n";
1091 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1092 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1093 }
1094 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001095 }
1096 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001097 }
1098
Ian Rogers1809a722013-08-09 22:05:32 -07001099 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1100 size_t offset, bool suspend_point_mapping) {
1101 MappingTable table(oat_method.GetMappingTable());
1102 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1103 typedef MappingTable::PcToDexIterator It;
1104 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1105 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001106 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001107 return cur.DexPc();
1108 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001109 }
Ian Rogers1809a722013-08-09 22:05:32 -07001110 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1111 typedef MappingTable::DexToPcIterator It;
1112 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1113 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001114 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001115 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001116 }
1117 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001118 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001119 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001120 }
1121
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001122 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1123 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001124 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001125 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001126 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001127 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001128 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001129 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001130 bool first = true;
1131 for (size_t reg = 0; reg < num_regs; reg++) {
1132 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1133 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001134 os << "GC map objects: v" << reg << " (";
1135 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001136 os << ")";
1137 first = false;
1138 } else {
1139 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001140 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001141 os << ")";
1142 }
1143 }
1144 }
1145 if (!first) {
1146 os << "\n";
1147 }
1148 }
1149 }
1150 }
1151
Mathieu Chartier590fee92013-09-13 13:46:47 -07001152 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1153 const OatFile::OatMethod& oat_method,
1154 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1155 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001156 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001157 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001158 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1159 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1160 if (kind != kUndefined) {
1161 if (first) {
1162 os << "VRegs: v";
1163 first = false;
1164 } else {
1165 os << ", v";
1166 }
1167 os << reg << " (";
1168 switch (kind) {
1169 case kImpreciseConstant:
1170 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1171 DescribeVReg(os, oat_method, code_item, reg, kind);
1172 break;
1173 case kConstant:
1174 os << "Constant: " << kinds.at((reg * 2) + 1);
1175 break;
1176 default:
1177 DescribeVReg(os, oat_method, code_item, reg, kind);
1178 break;
1179 }
1180 os << ")";
1181 }
1182 }
1183 if (!first) {
1184 os << "\n";
1185 }
1186 }
1187
1188
Ian Rogersb23a7722012-10-09 16:54:26 -07001189 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001190 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001191 size_t i = 0;
1192 while (i < code_item->insns_size_in_code_units_) {
1193 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001194 os << StringPrintf("0x%04zx: %s\n", i, instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001195 i += instruction->SizeInCodeUnits();
1196 }
1197 }
1198 }
1199
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001200 verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx,
1201 const DexFile* dex_file,
1202 const DexFile::ClassDef& class_def,
1203 const DexFile::CodeItem* code_item,
1204 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001205 if ((method_access_flags & kAccNative) == 0) {
1206 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001207 StackHandleScope<1> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001208 Handle<mirror::DexCache> dex_cache(
1209 hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
Andreas Gampe00b25f32014-09-17 21:49:05 -07001210 DCHECK(options_->class_loader_ != nullptr);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001211 return verifier::MethodVerifier::VerifyMethodAndDump(soa.Self(), os, dex_method_idx, dex_file,
1212 dex_cache,
Andreas Gampe00b25f32014-09-17 21:49:05 -07001213 *options_->class_loader_,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001214 &class_def, code_item,
1215 NullHandle<mirror::ArtMethod>(),
1216 method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001217 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001218
1219 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001220 }
1221
Mathieu Chartier590fee92013-09-13 13:46:47 -07001222 void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001223 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1224 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001225 const void* portable_code = oat_method.GetPortableCode();
1226 const void* quick_code = oat_method.GetQuickCode();
1227
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001228 if (code_size == 0) {
1229 code_size = oat_method.GetQuickCodeSize();
1230 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001231 if ((code_size == 0) || ((portable_code == nullptr) && (quick_code == nullptr))) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001232 os << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001233 return;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001234 } else if (quick_code != nullptr) {
1235 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1236 size_t offset = 0;
1237 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001238 if (!bad_input) {
1239 DumpMappingAtOffset(os, oat_method, offset, false);
1240 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001241 offset += disassembler_->Dump(os, quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001242 if (!bad_input) {
1243 uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
1244 if (dex_pc != DexFile::kDexNoIndex) {
1245 DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
1246 if (verifier != nullptr) {
1247 DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
1248 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001249 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001250 }
1251 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001252 } else {
1253 CHECK(portable_code != nullptr);
1254 CHECK_EQ(code_size, 0U); // TODO: disassembly of portable is currently not supported.
Ian Rogersb23a7722012-10-09 16:54:26 -07001255 }
1256 }
1257
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001258 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001259 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
1260 const OatDumperOptions* options_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001261 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001262 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001263};
1264
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001265class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001266 public:
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001267 explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001268 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
1269 : os_(os),
1270 image_space_(image_space),
1271 image_header_(image_header),
1272 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001273
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001274 bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001275 std::ostream& os = *os_;
1276 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001277
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001278 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001279
Mathieu Chartier31e89252013-08-28 11:29:12 -07001280 os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
1281 << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
1282
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001283 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001284
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001285 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001286
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001287 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1288
1289 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1290
1291 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001292
Alex Lighta59dd802014-07-02 16:28:08 -07001293 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1294
Igor Murashkin46774762014-10-22 11:37:02 -07001295 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1296
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001297 {
1298 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
1299 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1300 std::ostream indent1_os(&indent1_filter);
1301 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
1302 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1303 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1304 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001305 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001306 indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
1307 if (image_root_object->IsObjectArray()) {
1308 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
1309 std::ostream indent2_os(&indent2_filter);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001310 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001311 = image_root_object->AsObjectArray<mirror::Object>();
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001312 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1313 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001314 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001315 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1316 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001317 run++;
1318 } else {
1319 break;
1320 }
1321 }
1322 if (run == 0) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001323 indent2_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001324 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001325 indent2_os << StringPrintf("%d to %zd: ", j, j + run);
1326 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001327 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001328 if (value != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001329 PrettyObjectValue(indent2_os, value->GetClass(), value);
1330 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001331 indent2_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001332 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001333 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001334 }
1335 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001336 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001337 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001338
Brian Carlstromaded5f72011-10-07 17:15:04 -07001339 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001340 std::string image_filename = image_space_.GetImageFilename();
1341 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001342 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001343 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001344 std::string error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -07001345 const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1346 if (oat_file == nullptr) {
Igor Murashkin46774762014-10-22 11:37:02 -07001347 oat_file = OatFile::Open(oat_location, oat_location, nullptr, nullptr, false, &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001348 if (oat_file == nullptr) {
1349 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001350 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001351 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001352 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001353 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001354
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001355 stats_.oat_file_bytes = oat_file->Size();
1356
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001357 oat_dumper_.reset(new OatDumper(*oat_file, oat_dumper_options_.release()));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001358
Mathieu Chartier02e25112013-08-14 16:14:24 -07001359 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001360 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001361 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1362 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001363 }
1364
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001365 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001366
1367 // Loop through all the image spaces and dump their objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001368 gc::Heap* heap = Runtime::Current()->GetHeap();
1369 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07001370 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001371 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001372 {
1373 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1374 heap->FlushAllocStack();
1375 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001376 // Since FlushAllocStack() above resets the (active) allocation
1377 // stack. Need to revoke the thread-local allocation stacks that
1378 // point into it.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001379 {
1380 self->TransitionFromRunnableToSuspended(kNative);
1381 ThreadList* thread_list = Runtime::Current()->GetThreadList();
1382 thread_list->SuspendAll();
1383 heap->RevokeAllThreadLocalAllocationStacks(self);
1384 thread_list->ResumeAll();
1385 self->TransitionFromSuspendedToRunnable();
1386 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001387 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001388 {
1389 std::ostream* saved_os = os_;
1390 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1391 std::ostream indent_os(&indent_filter);
1392 os_ = &indent_os;
1393 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001394 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001395 if (space->IsImageSpace()) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001396 gc::space::ImageSpace* image_space = space->AsImageSpace();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001397 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1398 indent_os << "\n";
1399 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001400 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001401 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001402 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001403 indent_os << "\n";
1404 os_ = saved_os;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001405 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001406 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001407 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001408 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001409 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001410 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001411 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001412 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001413 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001414 size_t header_bytes = sizeof(ImageHeader);
1415 stats_.header_bytes = header_bytes;
1416 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1417 stats_.alignment_bytes += alignment_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001418 stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
1419 stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001420 stats_.Dump(os);
1421 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001422
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001423 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001424
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001425 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001426 }
1427
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001428 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001429 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001430 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001431 CHECK(type != nullptr);
1432 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001433 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001434 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001435 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001436 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001437 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001438 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001439 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001440 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001441 } else if (type->IsArtFieldClass()) {
1442 mirror::ArtField* field = value->AsArtField();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001443 os << StringPrintf("%p Field: %s\n", field, PrettyField(field).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001444 } else if (type->IsArtMethodClass()) {
1445 mirror::ArtMethod* method = value->AsArtMethod();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001446 os << StringPrintf("%p Method: %s\n", method, PrettyMethod(method).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001447 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001448 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001449 }
1450 }
1451
Brian Carlstromea46f952013-07-30 01:26:50 -07001452 static void PrintField(std::ostream& os, mirror::ArtField* field, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001453 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001454 const char* descriptor = field->GetTypeDescriptor();
1455 os << StringPrintf("%s: ", field->GetName());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001456 if (descriptor[0] != 'L' && descriptor[0] != '[') {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001457 StackHandleScope<1> hs(Thread::Current());
1458 FieldHelper fh(hs.NewHandle(field));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001459 mirror::Class* type = fh.GetType();
Fred Shih37f05ef2014-07-16 18:38:08 -07001460 DCHECK(type->IsPrimitive());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001461 if (type->IsPrimitiveLong()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001462 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -07001463 } else if (type->IsPrimitiveDouble()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001464 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -07001465 } else if (type->IsPrimitiveFloat()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001466 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Fred Shih37f05ef2014-07-16 18:38:08 -07001467 } else if (type->IsPrimitiveInt()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001468 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Fred Shih37f05ef2014-07-16 18:38:08 -07001469 } else if (type->IsPrimitiveChar()) {
1470 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
1471 } else if (type->IsPrimitiveShort()) {
1472 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
1473 } else if (type->IsPrimitiveBoolean()) {
1474 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1475 field->GetBoolean(obj));
1476 } else if (type->IsPrimitiveByte()) {
1477 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
1478 } else {
1479 LOG(FATAL) << "Unknown type: " << PrettyClass(type);
Ian Rogers48efc2b2012-08-27 17:20:31 -07001480 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001481 } else {
Ian Rogers48efc2b2012-08-27 17:20:31 -07001482 // Get the value, don't compute the type unless it is non-null as we don't want
1483 // to cause class loading.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001484 mirror::Object* value = field->GetObj(obj);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001485 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001486 os << StringPrintf("null %s\n", PrettyDescriptor(descriptor).c_str());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001487 } else {
Ian Rogers50239c72013-06-17 14:53:22 -07001488 // Grab the field type without causing resolution.
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001489 StackHandleScope<1> hs(Thread::Current());
1490 FieldHelper fh(hs.NewHandle(field));
Ian Rogers50239c72013-06-17 14:53:22 -07001491 mirror::Class* field_type = fh.GetType(false);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001492 if (field_type != nullptr) {
Ian Rogers50239c72013-06-17 14:53:22 -07001493 PrettyObjectValue(os, field_type, value);
1494 } else {
1495 os << StringPrintf("%p %s\n", value, PrettyDescriptor(descriptor).c_str());
1496 }
Ian Rogers48efc2b2012-08-27 17:20:31 -07001497 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001498 }
1499 }
1500
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001501 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001502 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001503 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001504 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001505 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001506 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001507 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001508 if (fields != nullptr) {
Ian Rogersd5b32602012-02-26 16:40:04 -08001509 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001510 mirror::ArtField* field = fields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001511 PrintField(os, field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001512 }
1513 }
1514 }
1515
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001516 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001517 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001518 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001519
Ian Rogersef7d42f2014-01-06 12:55:46 -08001520 const void* GetQuickOatCodeBegin(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001521 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001522 const void* quick_code = m->GetEntryPointFromQuickCompiledCode();
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001523 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001524 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001525 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001526 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001527 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001528 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001529 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001530 }
1531
Ian Rogersef7d42f2014-01-06 12:55:46 -08001532 uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001533 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001534 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1535 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001536 return 0;
1537 }
1538 return oat_code_begin[-1];
1539 }
1540
Ian Rogersef7d42f2014-01-06 12:55:46 -08001541 const void* GetQuickOatCodeEnd(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001542 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001543 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001544 if (oat_code_begin == nullptr) {
1545 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001546 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001547 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001548 }
1549
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001550 static void Callback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001551 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001552 DCHECK(obj != nullptr);
1553 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001554 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001555 if (!state->InDumpSpace(obj)) {
1556 return;
1557 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001558
1559 size_t object_bytes = obj->SizeOf();
1560 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1561 state->stats_.object_bytes += object_bytes;
1562 state->stats_.alignment_bytes += alignment_bytes;
1563
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001564 std::ostream& os = *state->os_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001565 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001566 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001567 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1568 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001569 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001570 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001571 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1572 << klass->GetStatus() << ")\n";
Brian Carlstromea46f952013-07-30 01:26:50 -07001573 } else if (obj->IsArtField()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001574 os << StringPrintf("%p: java.lang.reflect.ArtField %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001575 PrettyField(obj->AsArtField()).c_str());
1576 } else if (obj->IsArtMethod()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001577 os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001578 PrettyMethod(obj->AsArtMethod()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001579 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001580 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001581 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001582 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001583 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001584 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001585 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1586 std::ostream indent_os(&indent_filter);
1587 DumpFields(indent_os, obj, obj_class);
Ian Rogersd5b32602012-02-26 16:40:04 -08001588 if (obj->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001589 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Ian Rogersd5b32602012-02-26 16:40:04 -08001590 int32_t length = obj_array->GetLength();
1591 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001592 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001593 size_t run = 0;
1594 for (int32_t j = i + 1; j < length; j++) {
1595 if (value == obj_array->Get(j)) {
1596 run++;
1597 } else {
1598 break;
1599 }
1600 }
1601 if (run == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001602 indent_os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001603 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001604 indent_os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001605 i = i + run;
1606 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001607 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001608 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001609 PrettyObjectValue(indent_os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001610 }
1611 } else if (obj->IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001612 mirror::ObjectArray<mirror::ArtField>* sfields = obj->AsClass()->GetSFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001613 if (sfields != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001614 indent_os << "STATICS:\n";
1615 Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
1616 std::ostream indent2_os(&indent2_filter);
Ian Rogersd5b32602012-02-26 16:40:04 -08001617 for (int32_t i = 0; i < sfields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001618 mirror::ArtField* field = sfields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001619 PrintField(indent2_os, field, field->GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001620 }
1621 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001622 } else if (obj->IsArtMethod()) {
1623 mirror::ArtMethod* method = obj->AsArtMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001624 if (method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001625 // TODO: portable dumping.
1626 DCHECK(method->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1627 DCHECK(method->GetMappingTable() == nullptr) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001628 bool first_occurrence;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001629 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1630 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1631 state->ComputeOatSize(quick_oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001632 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001633 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001634 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001635 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCode()) {
1636 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001637 }
Ian Rogers19846512012-02-24 11:42:47 -08001638 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
Jeff Hao88474b42013-10-23 16:24:40 -07001639 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001640 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001641 DCHECK(method->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1642 DCHECK(method->GetMappingTable() == nullptr) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001643 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001644 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -07001645 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001646 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001647
Elliott Hughesa0e18062012-04-13 15:59:59 -07001648 bool first_occurrence;
Ian Rogers0c7abda2012-09-19 13:33:42 -07001649 size_t gc_map_bytes = state->ComputeOatSize(method->GetNativeGcMap(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001650 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001651 state->stats_.gc_map_bytes += gc_map_bytes;
1652 }
1653
1654 size_t pc_mapping_table_bytes =
Ian Rogers1809a722013-08-09 22:05:32 -07001655 state->ComputeOatSize(method->GetMappingTable(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001656 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001657 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1658 }
1659
1660 size_t vmap_table_bytes =
Ian Rogers1809a722013-08-09 22:05:32 -07001661 state->ComputeOatSize(method->GetVmapTable(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001662 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001663 state->stats_.vmap_table_bytes += vmap_table_bytes;
1664 }
1665
Ian Rogersef7d42f2014-01-06 12:55:46 -08001666 // TODO: portable dumping.
1667 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1668 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1669 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1670 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001671 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001672 state->stats_.managed_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001673 if (method->IsConstructor()) {
1674 if (method->IsStatic()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001675 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001676 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001677 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001678 }
1679 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001680 state->stats_.large_method_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001681 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001682 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001683 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001684
Ian Rogersef7d42f2014-01-06 12:55:46 -08001685 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001686 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
1687 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001688
1689 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersef7d42f2014-01-06 12:55:46 -08001690 vmap_table_bytes + quick_oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001691
1692 double expansion =
Ian Rogersef7d42f2014-01-06 12:55:46 -08001693 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001694 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001695 }
1696 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001697 std::string temp;
1698 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001699 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001700
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001701 std::set<const void*> already_seen_;
1702 // Compute the size of the given data within the oat file and whether this is the first time
1703 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001704 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001705 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001706 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001707 already_seen_.insert(oat_data);
1708 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001709 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001710 }
1711 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001712 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001713
1714 public:
1715 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001716 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001717 size_t file_bytes;
1718
1719 size_t header_bytes;
1720 size_t object_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001721 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001722 size_t alignment_bytes;
1723
1724 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001725 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001726 size_t managed_to_native_code_bytes;
1727 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001728 size_t class_initializer_code_bytes;
1729 size_t large_initializer_code_bytes;
1730 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001731
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001732 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001733 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001734 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001735
1736 size_t dex_instruction_bytes;
1737
Brian Carlstromea46f952013-07-30 01:26:50 -07001738 std::vector<mirror::ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001739 std::vector<size_t> method_outlier_size;
1740 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07001741 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001742
1743 explicit Stats()
1744 : oat_file_bytes(0),
1745 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001746 header_bytes(0),
1747 object_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07001748 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001749 alignment_bytes(0),
1750 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001751 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001752 managed_to_native_code_bytes(0),
1753 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001754 class_initializer_code_bytes(0),
1755 large_initializer_code_bytes(0),
1756 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001757 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001758 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001759 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001760 dex_instruction_bytes(0) {}
1761
Elliott Hughesa0e18062012-04-13 15:59:59 -07001762 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001763 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07001764 size_t bytes;
1765 size_t count;
1766 };
1767 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1768 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001769
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001770 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001771 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1772 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001773 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001774 it->second.count += 1;
1775 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001776 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07001777 }
1778 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001779
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001780 double PercentOfOatBytes(size_t size) {
1781 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1782 }
1783
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001784 double PercentOfFileBytes(size_t size) {
1785 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1786 }
1787
1788 double PercentOfObjectBytes(size_t size) {
1789 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1790 }
1791
Brian Carlstromea46f952013-07-30 01:26:50 -07001792 void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001793 method_outlier_size.push_back(total_size);
1794 method_outlier_expansion.push_back(expansion);
1795 method_outlier.push_back(method);
1796 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001797
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001798 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001799 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001800 size_t sum_of_sizes = 0;
1801 size_t sum_of_sizes_squared = 0;
1802 size_t sum_of_expansion = 0;
1803 size_t sum_of_expansion_squared = 0;
1804 size_t n = method_outlier_size.size();
1805 for (size_t i = 0; i < n; i++) {
1806 size_t cur_size = method_outlier_size[i];
1807 sum_of_sizes += cur_size;
1808 sum_of_sizes_squared += cur_size * cur_size;
1809 double cur_expansion = method_outlier_expansion[i];
1810 sum_of_expansion += cur_expansion;
1811 sum_of_expansion_squared += cur_expansion * cur_expansion;
1812 }
1813 size_t size_mean = sum_of_sizes / n;
1814 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1815 double expansion_mean = sum_of_expansion / n;
1816 double expansion_variance =
1817 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1818
1819 // Dump methods whose size is a certain number of standard deviations from the mean
1820 size_t dumped_values = 0;
1821 size_t skipped_values = 0;
1822 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1823 size_t cur_size_variance = i * i * size_variance;
1824 bool first = true;
1825 for (size_t j = 0; j < n; j++) {
1826 size_t cur_size = method_outlier_size[j];
1827 if (cur_size > size_mean) {
1828 size_t cur_var = cur_size - size_mean;
1829 cur_var = cur_var * cur_var;
1830 if (cur_var > cur_size_variance) {
1831 if (dumped_values > 20) {
1832 if (i == 1) {
1833 skipped_values++;
1834 } else {
1835 i = 2; // jump to counting for 1 standard deviation
1836 break;
1837 }
1838 } else {
1839 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001840 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001841 first = false;
1842 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001843 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001844 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001845 method_outlier_size[j] = 0; // don't consider this method again
1846 dumped_values++;
1847 }
1848 }
1849 }
1850 }
1851 }
1852 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001853 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001854 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001855 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001856 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001857
1858 // Dump methods whose expansion is a certain number of standard deviations from the mean
1859 dumped_values = 0;
1860 skipped_values = 0;
1861 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1862 double cur_expansion_variance = i * i * expansion_variance;
1863 bool first = true;
1864 for (size_t j = 0; j < n; j++) {
1865 double cur_expansion = method_outlier_expansion[j];
1866 if (cur_expansion > expansion_mean) {
1867 size_t cur_var = cur_expansion - expansion_mean;
1868 cur_var = cur_var * cur_var;
1869 if (cur_var > cur_expansion_variance) {
1870 if (dumped_values > 20) {
1871 if (i == 1) {
1872 skipped_values++;
1873 } else {
1874 i = 2; // jump to counting for 1 standard deviation
1875 break;
1876 }
1877 } else {
1878 if (first) {
1879 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001880 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001881 first = false;
1882 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001883 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001884 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001885 method_outlier_expansion[j] = 0.0; // don't consider this method again
1886 dumped_values++;
1887 }
1888 }
1889 }
1890 }
1891 }
1892 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001893 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001894 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001895 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001896 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001897 }
1898
Ian Rogersb726dcb2012-09-05 08:57:23 -07001899 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001900 {
1901 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1902 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
1903 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1904 std::ostream indent_os(&indent_filter);
1905 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
1906 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier32327092013-08-30 14:04:08 -07001907 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001908 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
1909 header_bytes, PercentOfFileBytes(header_bytes),
1910 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07001911 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001912 alignment_bytes, PercentOfFileBytes(alignment_bytes))
1913 << std::flush;
Mathieu Chartier32327092013-08-30 14:04:08 -07001914 CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001915 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001916
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001917 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001918 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07001919 for (const auto& sizes_and_count : sizes_and_counts) {
1920 const std::string& descriptor(sizes_and_count.first);
1921 double average = static_cast<double>(sizes_and_count.second.bytes) /
1922 static_cast<double>(sizes_and_count.second.count);
1923 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001924 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001925 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001926 descriptor.c_str(), sizes_and_count.second.bytes,
1927 sizes_and_count.second.count, average, percent);
1928 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001929 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001930 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001931 CHECK_EQ(object_bytes, object_bytes_total);
1932
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001933 os << StringPrintf("oat_file_bytes = %8zd\n"
1934 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1935 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1936 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1937 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1938 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1939 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001940 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001941 managed_code_bytes,
1942 PercentOfOatBytes(managed_code_bytes),
1943 managed_to_native_code_bytes,
1944 PercentOfOatBytes(managed_to_native_code_bytes),
1945 native_to_managed_code_bytes,
1946 PercentOfOatBytes(native_to_managed_code_bytes),
1947 class_initializer_code_bytes,
1948 PercentOfOatBytes(class_initializer_code_bytes),
1949 large_initializer_code_bytes,
1950 PercentOfOatBytes(large_initializer_code_bytes),
1951 large_method_code_bytes,
1952 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001953 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07001954 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001955 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001956 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
1957 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07001958 }
1959
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001960 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1961 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1962 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001963 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1964 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1965 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001966 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001967
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001968 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
1969 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001970 static_cast<double>(managed_code_bytes) /
1971 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07001972 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001973 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001974 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001975
1976 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001977 }
1978 } stats_;
1979
1980 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001981 enum {
1982 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1983 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1984 kLargeConstructorDexBytes = 4000,
1985 // Number of bytes for a method to be considered large. Based on the 4000 basic block
1986 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1987 kLargeMethodDexBytes = 16000
1988 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001989 std::ostream* os_;
Ian Rogers1d54e732013-05-02 21:10:01 -07001990 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001991 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001992 std::unique_ptr<OatDumper> oat_dumper_;
1993 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001994
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001995 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001996};
1997
Andreas Gampe00b25f32014-09-17 21:49:05 -07001998static NoopCompilerCallbacks callbacks;
Elliott Hughes72395bf2012-04-24 13:45:26 -07001999
Andreas Gampe00b25f32014-09-17 21:49:05 -07002000static Runtime* StartRuntime(const char* boot_image_location, const char* image_location,
2001 InstructionSet instruction_set) {
Ian Rogerse63db272014-07-15 15:36:11 -07002002 RuntimeOptions options;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002003 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07002004 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002005 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07002006 std::string boot_oat_option;
Brian Carlstrom6c871802013-07-17 14:25:35 -07002007
2008 // We are more like a compiler than a run-time. We don't want to execute code.
Brian Carlstromc0a1b182014-03-04 23:19:06 -08002009 options.push_back(std::make_pair("compilercallbacks", &callbacks));
Brian Carlstrom6c871802013-07-17 14:25:35 -07002010
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002011 if (boot_image_location != nullptr) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -07002012 boot_image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002013 boot_image_option += boot_image_location;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002014 options.push_back(std::make_pair(boot_image_option.c_str(), nullptr));
Brian Carlstrom78128a62011-09-15 17:21:19 -07002015 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002016 if (image_location != nullptr) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07002017 image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002018 image_option += image_location;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002019 options.push_back(std::make_pair(image_option.c_str(), nullptr));
Brian Carlstromaded5f72011-10-07 17:15:04 -07002020 }
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002021 options.push_back(
2022 std::make_pair("imageinstructionset",
2023 reinterpret_cast<const void*>(GetInstructionSetString(instruction_set))));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07002024
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002025 if (!Runtime::Create(options, false)) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07002026 fprintf(stderr, "Failed to create runtime\n");
Andreas Gampe00b25f32014-09-17 21:49:05 -07002027 return nullptr;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002028 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002029
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002030 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
Ian Rogersb9beb2e2014-05-09 16:57:40 -07002031 // give it away now and then switch to a more manageable ScopedObjectAccess.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002032 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002033
2034 return Runtime::Current();
2035}
2036
2037static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
2038 std::ostream* os) {
2039 // Dumping the image, no explicit class loader.
2040 NullHandle<mirror::ClassLoader> null_class_loader;
2041 options->class_loader_ = &null_class_loader;
2042
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002043 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002044 gc::Heap* heap = runtime->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -07002045 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002046 CHECK(image_space != nullptr);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002047 const ImageHeader& image_header = image_space->GetImageHeader();
2048 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002049 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002050 return EXIT_FAILURE;
2051 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002052 ImageDumper image_dumper(os, *image_space, image_header, options);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002053 bool success = image_dumper.Dump();
2054 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002055}
2056
Andreas Gampe00b25f32014-09-17 21:49:05 -07002057static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2058 std::ostream* os) {
2059 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2060
2061 Thread* self = Thread::Current();
2062 CHECK(self != nullptr);
2063 // Need well-known-classes.
2064 WellKnownClasses::Init(self->GetJniEnv());
2065
2066 // Need to register dex files to get a working dex cache.
2067 ScopedObjectAccess soa(self);
2068 ClassLinker* class_linker = runtime->GetClassLinker();
2069 class_linker->RegisterOatFile(oat_file);
2070 std::vector<const DexFile*> dex_files;
2071 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2072 std::string error_msg;
2073 const DexFile* dex_file = odf->OpenDexFile(&error_msg);
2074 CHECK(dex_file != nullptr) << error_msg;
2075 class_linker->RegisterDexFile(*dex_file);
2076 dex_files.push_back(dex_file);
2077 }
2078
2079 // Need a class loader.
2080 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader);
2081 ScopedLocalRef<jobject> class_loader_local(soa.Env(),
2082 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
2083 jobject class_loader = soa.Env()->NewGlobalRef(class_loader_local.get());
2084 // Fake that we're a compiler.
2085 runtime->SetCompileTimeClassPath(class_loader, dex_files);
2086
2087 // Use the class loader while dumping.
2088 StackHandleScope<1> scope(self);
2089 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2090 soa.Decode<mirror::ClassLoader*>(class_loader));
2091 options->class_loader_ = &loader_handle;
2092
2093 OatDumper oat_dumper(*oat_file, options);
2094 bool success = oat_dumper.Dump(*os);
2095 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2096}
2097
2098static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
2099 // No image = no class loader.
2100 NullHandle<mirror::ClassLoader> null_class_loader;
2101 options->class_loader_ = &null_class_loader;
2102
2103 OatDumper oat_dumper(*oat_file, options);
2104 bool success = oat_dumper.Dump(*os);
2105 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2106}
2107
2108static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2109 std::ostream* os) {
2110 std::string error_msg;
Igor Murashkin46774762014-10-22 11:37:02 -07002111 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002112 if (oat_file == nullptr) {
2113 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2114 return EXIT_FAILURE;
2115 }
2116
2117 if (runtime != nullptr) {
2118 return DumpOatWithRuntime(runtime, oat_file, options, os);
2119 } else {
2120 return DumpOatWithoutRuntime(oat_file, options, os);
2121 }
2122}
2123
2124static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2125 std::string error_msg;
Igor Murashkin46774762014-10-22 11:37:02 -07002126 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002127 if (oat_file == nullptr) {
2128 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2129 return EXIT_FAILURE;
2130 }
2131
2132 OatSymbolizer oat_symbolizer(oat_file, output_name);
2133 if (!oat_symbolizer.Init()) {
2134 fprintf(stderr, "Failed to initialize symbolizer\n");
2135 return EXIT_FAILURE;
2136 }
2137 if (!oat_symbolizer.Symbolize()) {
2138 fprintf(stderr, "Failed to symbolize\n");
2139 return EXIT_FAILURE;
2140 }
2141
2142 return EXIT_SUCCESS;
2143}
2144
2145struct OatdumpArgs {
2146 bool Parse(int argc, char** argv) {
2147 // Skip over argv[0].
2148 argv++;
2149 argc--;
2150
2151 if (argc == 0) {
2152 fprintf(stderr, "No arguments specified\n");
2153 usage();
2154 return false;
2155 }
2156
2157 for (int i = 0; i < argc; i++) {
2158 const StringPiece option(argv[i]);
2159 if (option.starts_with("--oat-file=")) {
2160 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2161 } else if (option.starts_with("--image=")) {
2162 image_location_ = option.substr(strlen("--image=")).data();
2163 } else if (option.starts_with("--boot-image=")) {
2164 boot_image_location_ = option.substr(strlen("--boot-image=")).data();
2165 } else if (option.starts_with("--instruction-set=")) {
2166 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
2167 instruction_set_ = GetInstructionSetFromString(instruction_set_str.data());
2168 if (instruction_set_ == kNone) {
2169 fprintf(stderr, "Unsupported instruction set %s\n", instruction_set_str.data());
2170 usage();
2171 return false;
2172 }
2173 } else if (option =="--dump:raw_mapping_table") {
2174 dump_raw_mapping_table_ = true;
2175 } else if (option == "--dump:raw_gc_map") {
2176 dump_raw_gc_map_ = true;
2177 } else if (option == "--no-dump:vmap") {
2178 dump_vmap_ = false;
2179 } else if (option == "--no-disassemble") {
2180 disassemble_code_ = false;
2181 } else if (option.starts_with("--output=")) {
2182 output_name_ = option.substr(strlen("--output=")).ToString();
2183 const char* filename = output_name_.c_str();
2184 out_.reset(new std::ofstream(filename));
2185 if (!out_->good()) {
2186 fprintf(stderr, "Failed to open output filename %s\n", filename);
2187 usage();
2188 return false;
2189 }
2190 os_ = out_.get();
2191 } else if (option.starts_with("--symbolize=")) {
2192 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2193 symbolize_ = true;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002194 } else if (option.starts_with("--method-filter=")) {
2195 method_filter_ = option.substr(strlen("--method-filter=")).data();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002196 } else {
2197 fprintf(stderr, "Unknown argument %s\n", option.data());
2198 usage();
2199 return false;
2200 }
2201 }
2202
2203 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2204 fprintf(stderr, "Either --image or --oat must be specified\n");
2205 return false;
2206 }
2207
2208 if (image_location_ != nullptr && oat_filename_ != nullptr) {
2209 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
2210 return false;
2211 }
2212
2213 return true;
2214 }
2215
2216 const char* oat_filename_ = nullptr;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002217 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002218 const char* image_location_ = nullptr;
2219 const char* boot_image_location_ = nullptr;
2220 InstructionSet instruction_set_ = kRuntimeISA;
2221 std::string elf_filename_prefix_;
2222 std::ostream* os_ = &std::cout;
2223 std::unique_ptr<std::ofstream> out_;
2224 std::string output_name_;
2225 bool dump_raw_mapping_table_ = false;
2226 bool dump_raw_gc_map_ = false;
2227 bool dump_vmap_ = true;
2228 bool disassemble_code_ = true;
2229 bool symbolize_ = false;
2230};
2231
2232static int oatdump(int argc, char** argv) {
2233 InitLogging(argv);
2234
2235 OatdumpArgs args;
2236 if (!args.Parse(argc, argv)) {
2237 return EXIT_FAILURE;
2238 }
2239
2240 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2241 bool absolute_addresses = (args.oat_filename_ == nullptr);
2242
2243 std::unique_ptr<OatDumperOptions> oat_dumper_options(new OatDumperOptions(
2244 args.dump_raw_mapping_table_,
2245 args.dump_raw_gc_map_,
2246 args.dump_vmap_,
2247 args.disassemble_code_,
2248 absolute_addresses,
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002249 args.method_filter_,
Andreas Gampe00b25f32014-09-17 21:49:05 -07002250 nullptr));
2251
2252 std::unique_ptr<Runtime> runtime;
2253 if ((args.boot_image_location_ != nullptr || args.image_location_ != nullptr) &&
2254 !args.symbolize_) {
2255 // If we have a boot image option, try to start the runtime; except when just symbolizing.
2256 runtime.reset(StartRuntime(args.boot_image_location_,
2257 args.image_location_,
2258 args.instruction_set_));
Mathieu Chartierd424d082014-10-15 10:31:46 -07002259 } else {
2260 MemMap::Init();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002261 }
2262
2263 if (args.oat_filename_ != nullptr) {
2264 if (args.symbolize_) {
2265 return SymbolizeOat(args.oat_filename_, args.output_name_);
2266 } else {
2267 return DumpOat(runtime.get(), args.oat_filename_, oat_dumper_options.release(), args.os_);
2268 }
2269 }
2270
2271 if (runtime.get() == nullptr) {
2272 // We need the runtime when printing an image.
2273 return EXIT_FAILURE;
2274 }
2275
2276 return DumpImage(runtime.get(), args.image_location_, oat_dumper_options.release(), args.os_);
2277}
2278
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002279} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002280
2281int main(int argc, char** argv) {
2282 return art::oatdump(argc, argv);
2283}