blob: b78daf0d2f03b49317d5e919a0f1e82db9f5e915 [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");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700110}
111
Ian Rogersff1ed472011-09-20 13:46:24 -0700112const char* image_roots_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -0800113 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -0700114 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700115 "kImtUnimplementedMethod",
Jeff Hao88474b42013-10-23 16:24:40 -0700116 "kDefaultImt",
Brian Carlstrome24fa612011-09-29 00:53:55 -0700117 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -0700118 "kRefsOnlySaveMethod",
119 "kRefsAndArgsSaveMethod",
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700120 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700121 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700122};
123
Ian Rogers0279ebb2014-10-08 17:27:48 -0700124class OatSymbolizer FINAL : public CodeOutput {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700125 public:
Ian Rogers0279ebb2014-10-08 17:27:48 -0700126 explicit OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
127 oat_file_(oat_file), builder_(nullptr), elf_output_(nullptr),
128 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
129 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700130
131 bool Init() {
132 Elf32_Word oat_data_size = oat_file_->GetOatHeader().GetExecutableOffset();
133
134 uint32_t diff = static_cast<uint32_t>(oat_file_->End() - oat_file_->Begin());
135 uint32_t oat_exec_size = diff - oat_data_size;
136
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700137 elf_output_ = OS::CreateEmptyFile(output_name_.c_str());
138
139 builder_.reset(new ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
140 Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(
141 this,
142 elf_output_,
143 oat_file_->GetOatHeader().GetInstructionSet(),
144 0,
145 oat_data_size,
146 oat_data_size,
147 oat_exec_size,
148 true,
149 false));
150
151 if (!builder_->Init()) {
152 builder_.reset(nullptr);
153 return false;
154 }
155
156 return true;
157 }
158
159 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
160 uint32_t,
161 const OatFile::OatMethod&,
162 const DexFile&,
163 uint32_t,
164 const DexFile::CodeItem*,
165 uint32_t);
166
167 bool Symbolize() {
168 if (builder_.get() == nullptr) {
169 return false;
170 }
171
172 Walk(&art::OatSymbolizer::RegisterForDedup);
173
174 NormalizeState();
175
176 Walk(&art::OatSymbolizer::AddSymbol);
177
178 bool result = builder_->Write();
179
180 elf_output_->Flush();
181 elf_output_->Close();
182
183 return result;
184 }
185
186 void Walk(Callback callback) {
187 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
188 for (size_t i = 0; i < oat_dex_files.size(); i++) {
189 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
190 CHECK(oat_dex_file != NULL);
191 WalkOatDexFile(oat_dex_file, callback);
192 }
193 }
194
195 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
196 std::string error_msg;
197 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
198 if (dex_file.get() == nullptr) {
199 return;
200 }
201 for (size_t class_def_index = 0;
202 class_def_index < dex_file->NumClassDefs();
203 class_def_index++) {
204 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
205 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
206 OatClassType type = oat_class.GetType();
207 switch (type) {
208 case kOatClassAllCompiled:
209 case kOatClassSomeCompiled:
210 WalkOatClass(oat_class, *dex_file.get(), class_def, callback);
211 break;
212
213 case kOatClassNoneCompiled:
214 case kOatClassMax:
215 // Ignore.
216 break;
217 }
218 }
219 }
220
221 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
222 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700223 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700224 if (class_data == nullptr) { // empty class such as a marker interface?
225 return;
226 }
227 // Note: even if this is an interface or a native class, we still have to walk it, as there
228 // might be a static initializer.
229 ClassDataItemIterator it(dex_file, class_data);
230 SkipAllFields(&it);
231 uint32_t class_method_idx = 0;
232 while (it.HasNextDirectMethod()) {
233 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
234 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700235 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700236 class_method_idx++;
237 it.Next();
238 }
239 while (it.HasNextVirtualMethod()) {
240 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
241 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700242 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700243 class_method_idx++;
244 it.Next();
245 }
246 DCHECK(!it.HasNext());
247 }
248
249 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
250 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
251 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
252 uint32_t method_access_flags, Callback callback) {
253 if ((method_access_flags & kAccAbstract) != 0) {
254 // Abstract method, no code.
255 return;
256 }
257 if (oat_method.GetCodeOffset() == 0) {
258 // No code.
259 return;
260 }
261
262 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
263 method_access_flags);
264 }
265
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700266 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
267 uint32_t class_method_index ATTRIBUTE_UNUSED,
268 const OatFile::OatMethod& oat_method,
269 const DexFile& dex_file ATTRIBUTE_UNUSED,
270 uint32_t dex_method_idx ATTRIBUTE_UNUSED,
271 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
272 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700273 state_[oat_method.GetCodeOffset()]++;
274 }
275
276 void NormalizeState() {
277 for (auto& x : state_) {
278 if (x.second == 1) {
279 state_[x.first] = 0;
280 }
281 }
282 }
283
284 enum class DedupState { // private
285 kNotDeduplicated,
286 kDeduplicatedFirst,
287 kDeduplicatedOther
288 };
289 DedupState IsDuplicated(uint32_t offset) {
290 if (state_[offset] == 0) {
291 return DedupState::kNotDeduplicated;
292 }
293 if (state_[offset] == 1) {
294 return DedupState::kDeduplicatedOther;
295 }
296 state_[offset] = 1;
297 return DedupState::kDeduplicatedFirst;
298 }
299
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700300 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
301 uint32_t class_method_index ATTRIBUTE_UNUSED,
302 const OatFile::OatMethod& oat_method,
303 const DexFile& dex_file,
304 uint32_t dex_method_idx,
305 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
306 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700307 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
308 if (dedup != DedupState::kDeduplicatedOther) {
309 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
310
311 if (dedup == DedupState::kDeduplicatedFirst) {
312 pretty_name = "[Dedup]" + pretty_name;
313 }
314
315 ElfSymtabBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr,
Ian Rogers0279ebb2014-10-08 17:27:48 -0700316 Elf32_Sym, Elf32_Shdr>* symtab = builder_->GetSymtabBuilder();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700317
Ian Rogers0279ebb2014-10-08 17:27:48 -0700318 symtab->AddSymbol(pretty_name, &builder_->GetTextBuilder(),
319 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
320 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700321 }
322 }
323
Vladimir Markof4da6752014-08-01 19:04:18 +0100324 // Set oat data offset. Required by ElfBuilder/CodeOutput.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700325 void SetCodeOffset(size_t offset ATTRIBUTE_UNUSED) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100326 // Nothing to do.
327 }
328
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700329 // Write oat code. Required by ElfBuilder/CodeOutput.
330 bool Write(OutputStream* out) {
331 return out->WriteFully(oat_file_->Begin(), oat_file_->End() - oat_file_->Begin());
332 }
333
334 private:
335 static void SkipAllFields(ClassDataItemIterator* it) {
336 while (it->HasNextStaticField()) {
337 it->Next();
338 }
339 while (it->HasNextInstanceField()) {
340 it->Next();
341 }
342 }
343
344 const OatFile* oat_file_;
345 std::unique_ptr<ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
346 Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr> > builder_;
347 File* elf_output_;
348 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700349 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700350};
351
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700352class OatDumperOptions {
353 public:
354 OatDumperOptions(bool dump_raw_mapping_table,
355 bool dump_raw_gc_map,
356 bool dump_vmap,
357 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700358 bool absolute_addresses,
359 Handle<mirror::ClassLoader>* class_loader)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700360 : dump_raw_mapping_table_(dump_raw_mapping_table),
361 dump_raw_gc_map_(dump_raw_gc_map),
362 dump_vmap_(dump_vmap),
363 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700364 absolute_addresses_(absolute_addresses),
365 class_loader_(class_loader) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700366
367 const bool dump_raw_mapping_table_;
368 const bool dump_raw_gc_map_;
369 const bool dump_vmap_;
370 const bool disassemble_code_;
371 const bool absolute_addresses_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700372 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700373};
374
Elliott Hughese3c845c2012-02-28 17:23:01 -0800375class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700376 public:
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700377 explicit OatDumper(const OatFile& oat_file, OatDumperOptions* options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000378 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800379 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700380 options_(options),
381 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet(),
382 new DisassemblerOptions(options_->absolute_addresses_,
383 oat_file.Begin()))) {
Andreas Gampe00b25f32014-09-17 21:49:05 -0700384 CHECK(options_->class_loader_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800385 AddAllOffsets();
386 }
387
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700388 ~OatDumper() {
389 delete options_;
390 delete disassembler_;
391 }
392
393 bool Dump(std::ostream& os) {
394 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800395 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700396
397 os << "MAGIC:\n";
398 os << oat_header.GetMagic() << "\n\n";
399
400 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800401 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700402
Elliott Hughesa72ec822012-03-05 17:12:22 -0800403 os << "INSTRUCTION SET:\n";
404 os << oat_header.GetInstructionSet() << "\n\n";
405
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700406 {
407 std::unique_ptr<const InstructionSetFeatures> features(
408 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
409 oat_header.GetInstructionSetFeaturesBitmap()));
410 os << "INSTRUCTION SET FEATURES:\n";
411 os << features->GetFeatureString() << "\n\n";
412 }
Dave Allison70202782013-10-22 17:52:19 -0700413
Brian Carlstromaded5f72011-10-07 17:15:04 -0700414 os << "DEX FILE COUNT:\n";
415 os << oat_header.GetDexFileCount() << "\n\n";
416
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800417#define DUMP_OAT_HEADER_OFFSET(label, offset) \
418 os << label " OFFSET:\n"; \
419 os << StringPrintf("0x%08x", oat_header.offset()); \
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700420 if (oat_header.offset() != 0 && options_->absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800421 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
422 } \
423 os << StringPrintf("\n\n");
424
425 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
426 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
427 GetInterpreterToInterpreterBridgeOffset);
428 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
429 GetInterpreterToCompiledCodeBridgeOffset);
430 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
431 GetJniDlsymLookupOffset);
432 DUMP_OAT_HEADER_OFFSET("PORTABLE IMT CONFLICT TRAMPOLINE",
433 GetPortableImtConflictTrampolineOffset);
434 DUMP_OAT_HEADER_OFFSET("PORTABLE RESOLUTION TRAMPOLINE",
435 GetPortableResolutionTrampolineOffset);
436 DUMP_OAT_HEADER_OFFSET("PORTABLE TO INTERPRETER BRIDGE",
437 GetPortableToInterpreterBridgeOffset);
438 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
439 GetQuickGenericJniTrampolineOffset);
440 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
441 GetQuickImtConflictTrampolineOffset);
442 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
443 GetQuickResolutionTrampolineOffset);
444 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
445 GetQuickToInterpreterBridgeOffset);
446#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700447
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700448 os << "IMAGE PATCH DELTA:\n";
449 os << StringPrintf("%d (0x%08x)\n\n",
450 oat_header.GetImagePatchDelta(),
451 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700452
Brian Carlstrom28db0122012-10-18 16:20:41 -0700453 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
454 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
455
456 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800457 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700458
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700459 // Print the key-value store.
460 {
461 os << "KEY VALUE STORE:\n";
462 size_t index = 0;
463 const char* key;
464 const char* value;
465 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
466 os << key << " = " << value << "\n";
467 index++;
468 }
469 os << "\n";
470 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700471
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700472 if (options_->absolute_addresses_) {
473 os << "BEGIN:\n";
474 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700475
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700476 os << "END:\n";
477 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
478 }
479
480 os << "SIZE:\n";
481 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700482
483 os << std::flush;
484
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800485 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
486 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700487 CHECK(oat_dex_file != nullptr);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700488 if (!DumpOatDexFile(os, *oat_dex_file)) {
489 success = false;
490 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700491 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700492 os << std::flush;
493 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700494 }
495
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800496 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700497 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
498 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800499 return 0; // Address not in oat file
500 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800501 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
502 reinterpret_cast<uintptr_t>(oat_file_.Begin());
503 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800504 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800505 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800506 return end_offset - begin_offset;
507 }
508
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700509 InstructionSet GetInstructionSet() {
510 return oat_file_.GetOatHeader().GetInstructionSet();
511 }
512
Ian Rogersef7d42f2014-01-06 12:55:46 -0800513 const void* GetQuickOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800514 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
515 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700516 CHECK(oat_dex_file != nullptr);
517 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700518 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700519 if (dex_file.get() == nullptr) {
520 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
521 << "': " << error_msg;
522 } else {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700523 const DexFile::ClassDef* class_def =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700524 dex_file->FindClassDef(m->GetDeclaringClassDescriptor());
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700525 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700526 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100527 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800528 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100529 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800530 }
531 }
532 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700533 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800534 }
535
Brian Carlstromaded5f72011-10-07 17:15:04 -0700536 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800537 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800538 // We don't know the length of the code for each method, but we need to know where to stop
539 // when disassembling. What we do know is that a region of code will be followed by some other
540 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
541 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800542 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
543 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700544 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700545 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700546 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700547 if (dex_file.get() == nullptr) {
548 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
549 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800550 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800551 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800552 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800553 for (size_t class_def_index = 0;
554 class_def_index < dex_file->NumClassDefs();
555 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800556 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100557 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700558 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700559 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800560 ClassDataItemIterator it(*dex_file, class_data);
561 SkipAllFields(it);
562 uint32_t class_method_index = 0;
563 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100564 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800565 it.Next();
566 }
567 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100568 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800569 it.Next();
570 }
571 }
572 }
573 }
574
575 // If the last thing in the file is code for a method, there won't be an offset for the "next"
576 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
577 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800578 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800579 }
580
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700581 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
582 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
583 }
584
Elliott Hughese3c845c2012-02-28 17:23:01 -0800585 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800586 uint32_t code_offset = oat_method.GetCodeOffset();
587 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
588 code_offset &= ~0x1;
589 }
590 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800591 offsets_.insert(oat_method.GetMappingTableOffset());
592 offsets_.insert(oat_method.GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700593 offsets_.insert(oat_method.GetNativeGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800594 }
595
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700596 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
597 bool success = true;
598 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800599 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800600 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700601
602 // Create the verifier early.
603
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700604 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700605 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700606 if (dex_file.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700607 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700608 os << std::flush;
609 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700610 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800611 for (size_t class_def_index = 0;
612 class_def_index < dex_file->NumClassDefs();
613 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700614 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
615 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700616 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100617 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700618 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
619 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100620 << " (" << oat_class.GetStatus() << ")"
621 << " (" << oat_class.GetType() << ")\n";
622 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800623 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
624 std::ostream indented_os(&indent_filter);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700625 if (!DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def)) {
626 success = false;
627 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700628 }
629
630 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700631 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700632 }
633
Elliott Hughese3c845c2012-02-28 17:23:01 -0800634 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700635 while (it.HasNextStaticField()) {
636 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700637 }
Ian Rogers0571d352011-11-03 19:51:38 -0700638 while (it.HasNextInstanceField()) {
639 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700640 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800641 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700642
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700643 bool DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800644 const DexFile::ClassDef& class_def) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700645 bool success = true;
Ian Rogers13735952014-10-08 12:43:28 -0700646 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700647 if (class_data == nullptr) { // empty class such as a marker interface?
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700648 os << std::flush;
649 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800650 }
651 ClassDataItemIterator it(dex_file, class_data);
652 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700653 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700654 while (it.HasNextDirectMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700655 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
656 it.GetMemberIndex(), it.GetMethodCodeItem(),
657 it.GetRawMemberAccessFlags())) {
658 success = false;
659 }
660 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700661 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700662 }
Ian Rogers0571d352011-11-03 19:51:38 -0700663 while (it.HasNextVirtualMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700664 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
665 it.GetMemberIndex(), it.GetMethodCodeItem(),
666 it.GetRawMemberAccessFlags())) {
667 success = false;
668 }
669 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700670 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700671 }
Ian Rogers0571d352011-11-03 19:51:38 -0700672 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700673 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700674 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700675 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800676
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700677 static constexpr uint32_t kPrologueBytes = 16;
678
679 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
680 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
681
682 bool DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700683 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700684 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800685 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
686 uint32_t method_access_flags) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700687 bool success = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800688 os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700689 class_method_index, PrettyMethod(dex_method_idx, dex_file, true).c_str(),
690 dex_method_idx);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800691 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700692 std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter));
693 Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count);
694 std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter));
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800695 {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700696 *indent1_os << "DEX CODE:\n";
697 DumpDexCode(*indent2_os, dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700698 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700699
700 std::unique_ptr<verifier::MethodVerifier> verifier;
701 if (Runtime::Current() != nullptr) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700702 *indent1_os << "VERIFIER TYPE ANALYSIS:\n";
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700703 verifier.reset(DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item,
704 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700705 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700706
707 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
708 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
709 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800710 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700711 *indent1_os << "OatMethodOffsets ";
712 if (options_->absolute_addresses_) {
713 *indent1_os << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100714 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700715 *indent1_os << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
716 if (oat_method_offsets_offset > oat_file_.Size()) {
717 *indent1_os << StringPrintf(
718 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
719 oat_method_offsets_offset, oat_file_.Size());
720 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
721 os << std::flush;
722 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800723 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700724
725 uint32_t code_offset = oat_method.GetCodeOffset();
726 *indent2_os << StringPrintf("code_offset: 0x%08x ", code_offset);
727 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
728 if (aligned_code_begin > oat_file_.Size()) {
729 *indent2_os << StringPrintf("WARNING: "
730 "code offset 0x%08x is past end of file 0x%08zx.\n",
731 aligned_code_begin, oat_file_.Size());
732 success = false;
733 }
734 *indent2_os << "\n";
735
736 *indent2_os << "gc_map: ";
737 if (options_->absolute_addresses_) {
738 *indent2_os << StringPrintf("%p ", oat_method.GetNativeGcMap());
739 }
740 uint32_t gc_map_offset = oat_method.GetNativeGcMapOffset();
741 *indent2_os << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
742 if (gc_map_offset > oat_file_.Size()) {
743 *indent2_os << StringPrintf("WARNING: "
744 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
745 gc_map_offset, oat_file_.Size());
746 success = false;
747 } else if (options_->dump_raw_gc_map_) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700748 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800749 std::ostream indent3_os(&indent3_filter);
750 DumpGcMap(indent3_os, oat_method, code_item);
751 }
752 }
753 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700754 *indent1_os << "OatQuickMethodHeader ";
755 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
756 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700757
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700758 if (options_->absolute_addresses_) {
759 *indent1_os << StringPrintf("%p ", method_header);
760 }
761 *indent1_os << StringPrintf("(offset=0x%08x)\n", method_header_offset);
762 if (method_header_offset > oat_file_.Size()) {
763 *indent1_os << StringPrintf(
764 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
765 method_header_offset, oat_file_.Size());
766 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
767 os << std::flush;
768 return false;
769 }
770
771 *indent2_os << "mapping_table: ";
772 if (options_->absolute_addresses_) {
773 *indent2_os << StringPrintf("%p ", oat_method.GetMappingTable());
774 }
775 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
776 *indent2_os << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
777 if (mapping_table_offset > oat_file_.Size()) {
778 *indent2_os << StringPrintf("WARNING: "
779 "mapping table offset 0x%08x is past end of file 0x%08zx. "
780 "mapping table offset was loaded from offset 0x%08x.\n",
781 mapping_table_offset, oat_file_.Size(),
782 oat_method.GetMappingTableOffsetOffset());
783 success = false;
784 } else if (options_->dump_raw_mapping_table_) {
785 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
786 std::ostream indent3_os(&indent3_filter);
787 DumpMappingTable(indent3_os, oat_method);
788 }
789
790 *indent2_os << "vmap_table: ";
791 if (options_->absolute_addresses_) {
792 *indent2_os << StringPrintf("%p ", oat_method.GetVmapTable());
793 }
794 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
795 *indent2_os << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
796 if (vmap_table_offset > oat_file_.Size()) {
797 *indent2_os << StringPrintf("WARNING: "
798 "vmap table offset 0x%08x is past end of file 0x%08zx. "
799 "vmap table offset was loaded from offset 0x%08x.\n",
800 vmap_table_offset, oat_file_.Size(),
801 oat_method.GetVmapTableOffsetOffset());
802 success = false;
803 } else if (options_->dump_vmap_) {
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +0100804 DumpVmap(*indent2_os, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700805 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800806 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700807 {
808 *indent1_os << "QuickMethodFrameInfo\n";
809
810 *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
811 *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
812 DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false);
813 *indent2_os << "\n";
814 *indent2_os << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
815 DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true);
816 *indent2_os << "\n";
817 }
818 {
819 // Based on spill masks from QuickMethodFrameInfo so placed
820 // after it is dumped, but useful for understanding quick
821 // code, so dumped here.
822 DumpVregLocations(*indent2_os, oat_method, code_item);
823 }
824 {
825 *indent1_os << "CODE: ";
826 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
827 if (code_size_offset > oat_file_.Size()) {
828 *indent2_os << StringPrintf("WARNING: "
829 "code size offset 0x%08x is past end of file 0x%08zx.",
830 code_size_offset, oat_file_.Size());
831 success = false;
832 } else {
833 const void* code = oat_method.GetQuickCode();
834 uint32_t code_size = oat_method.GetQuickCodeSize();
835 if (code == nullptr) {
836 code = oat_method.GetPortableCode();
837 code_size = oat_method.GetPortableCodeSize();
838 code_size_offset = 0;
839 }
840 uint32_t code_offset = oat_method.GetCodeOffset();
841 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
842 uint64_t aligned_code_end = aligned_code_begin + code_size;
843
844 if (options_->absolute_addresses_) {
845 *indent1_os << StringPrintf("%p ", code);
846 }
847 *indent1_os << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
848 code_offset,
849 code_size_offset,
850 code_size,
851 code != nullptr ? "..." : "");
852
853 if (aligned_code_begin > oat_file_.Size()) {
854 *indent2_os << StringPrintf("WARNING: "
855 "start of code at 0x%08x is past end of file 0x%08zx.",
856 aligned_code_begin, oat_file_.Size());
857 success = false;
858 } else if (aligned_code_end > oat_file_.Size()) {
859 *indent2_os << StringPrintf("WARNING: "
860 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
861 "code size is 0x%08x loaded from offset 0x%08x.\n",
862 aligned_code_end, oat_file_.Size(),
863 code_size, code_size_offset);
864 success = false;
865 if (options_->disassemble_code_) {
866 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
867 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
868 }
869 }
870 } else if (code_size > kMaxCodeSize) {
871 *indent2_os << StringPrintf("WARNING: "
872 "code size %d is bigger than max expected threshold of %d. "
873 "code size is 0x%08x loaded from offset 0x%08x.\n",
874 code_size, kMaxCodeSize,
875 code_size, code_size_offset);
876 success = false;
877 if (options_->disassemble_code_) {
878 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
879 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
880 }
881 }
882 } else if (options_->disassemble_code_) {
883 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, !success, 0);
884 }
885 }
886 }
887 os << std::flush;
888 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700889 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800890
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800891 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
892 if (spill_mask == 0) {
893 return;
894 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800895 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800896 for (size_t i = 0; i < 32; i++) {
897 if ((spill_mask & (1 << i)) != 0) {
898 if (is_float) {
899 os << "fr" << i;
900 } else {
901 os << "r" << i;
902 }
903 spill_mask ^= 1 << i; // clear bit
904 if (spill_mask != 0) {
905 os << ", ";
906 } else {
907 break;
908 }
909 }
910 }
911 os << ")";
912 }
913
Ian Rogersb23a7722012-10-09 16:54:26 -0700914 void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) {
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +0100915 // If the native GC map is null, then this method has been compiled with the
916 // optimizing compiler. The optimizing compiler currently outputs its stack map
917 // in the vmap table, and the code below does not work with such a stack map.
918 if (oat_method.GetNativeGcMap() == nullptr) {
919 return;
920 }
Ian Rogers1809a722013-08-09 22:05:32 -0700921 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700922 if (raw_table != nullptr) {
Ian Rogers1809a722013-08-09 22:05:32 -0700923 const VmapTable vmap_table(raw_table);
924 bool first = true;
925 bool processing_fp = false;
926 uint32_t spill_mask = oat_method.GetCoreSpillMask();
927 for (size_t i = 0; i < vmap_table.Size(); i++) {
928 uint16_t dex_reg = vmap_table[i];
929 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
930 processing_fp ? kFloatVReg : kIntVReg);
931 os << (first ? "v" : ", v") << dex_reg;
932 if (!processing_fp) {
933 os << "/r" << cpu_reg;
934 } else {
935 os << "/fr" << cpu_reg;
936 }
937 first = false;
938 if (!processing_fp && dex_reg == 0xFFFF) {
939 processing_fp = true;
940 spill_mask = oat_method.GetFpSpillMask();
941 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800942 }
Ian Rogers1809a722013-08-09 22:05:32 -0700943 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800944 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800945 }
946
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -0700947 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
948 const DexFile::CodeItem* code_item) {
949 if (code_item != nullptr) {
950 size_t num_locals_ins = code_item->registers_size_;
951 size_t num_ins = code_item->ins_size_;
952 size_t num_locals = num_locals_ins - num_ins;
953 size_t num_outs = code_item->outs_size_;
954
955 os << "vr_stack_locations:";
956 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
957 // For readability, delimit the different kinds of VRs.
958 if (reg == num_locals_ins) {
959 os << "\n\tmethod*:";
960 } else if (reg == num_locals && num_ins > 0) {
961 os << "\n\tins:";
962 } else if (reg == 0 && num_locals > 0) {
963 os << "\n\tlocals:";
964 }
965
966 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
967 oat_method.GetFpSpillMask(),
968 oat_method.GetFrameSizeInBytes(), reg,
969 GetInstructionSet());
970 os << " v" << reg << "[sp + #" << offset << "]";
971 }
972
973 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
974 if (out_reg == 0) {
975 os << "\n\touts:";
976 }
977
978 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
979 os << " v" << out_reg << "[sp + #" << offset << "]";
980 }
981
982 os << "\n";
983 }
984 }
985
Ian Rogersb23a7722012-10-09 16:54:26 -0700986 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800987 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -0700988 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700989 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700990 const VmapTable vmap_table(raw_table);
991 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -0700992 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800993 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
994 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
995 : oat_method.GetCoreSpillMask();
996 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -0700997 } else {
998 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
999 oat_method.GetFpSpillMask(),
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001000 oat_method.GetFrameSizeInBytes(), reg,
1001 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001002 os << "[sp + #" << offset << "]";
1003 }
1004 }
1005 }
1006
Ian Rogersef7d42f2014-01-06 12:55:46 -08001007 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1008 const DexFile::CodeItem* code_item,
1009 size_t num_regs, const uint8_t* reg_bitmap) {
1010 bool first = true;
1011 for (size_t reg = 0; reg < num_regs; reg++) {
1012 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1013 if (first) {
1014 os << " v" << reg << " (";
1015 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1016 os << ")";
1017 first = false;
1018 } else {
1019 os << ", v" << reg << " (";
1020 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1021 os << ")";
1022 }
1023 }
1024 }
1025 if (first) {
1026 os << "No registers in GC map\n";
1027 } else {
1028 os << "\n";
1029 }
1030 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001031 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1032 const DexFile::CodeItem* code_item) {
1033 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001034 if (gc_map_raw == nullptr) {
1035 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001036 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001037 const void* quick_code = oat_method.GetQuickCode();
1038 if (quick_code != nullptr) {
1039 NativePcOffsetToReferenceMap map(gc_map_raw);
1040 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1041 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1042 map.GetNativePcOffset(entry);
1043 os << StringPrintf("%p", native_pc);
1044 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001045 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001046 } else {
1047 const void* portable_code = oat_method.GetPortableCode();
1048 CHECK(portable_code != nullptr);
1049 verifier::DexPcToReferenceMap map(gc_map_raw);
1050 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1051 uint32_t dex_pc = map.GetDexPc(entry);
1052 os << StringPrintf("0x%08x", dex_pc);
1053 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
1054 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001055 }
1056 }
1057
1058 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001059 const void* quick_code = oat_method.GetQuickCode();
1060 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001061 return;
1062 }
Ian Rogers1809a722013-08-09 22:05:32 -07001063 MappingTable table(oat_method.GetMappingTable());
1064 if (table.TotalSize() != 0) {
1065 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1066 std::ostream indent_os(&indent_filter);
1067 if (table.PcToDexSize() != 0) {
1068 typedef MappingTable::PcToDexIterator It;
1069 os << "suspend point mappings {\n";
1070 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1071 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1072 }
1073 os << "}\n";
1074 }
1075 if (table.DexToPcSize() != 0) {
1076 typedef MappingTable::DexToPcIterator It;
1077 os << "catch entry mappings {\n";
1078 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1079 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1080 }
1081 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001082 }
1083 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001084 }
1085
Ian Rogers1809a722013-08-09 22:05:32 -07001086 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1087 size_t offset, bool suspend_point_mapping) {
1088 MappingTable table(oat_method.GetMappingTable());
1089 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1090 typedef MappingTable::PcToDexIterator It;
1091 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1092 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001093 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001094 return cur.DexPc();
1095 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001096 }
Ian Rogers1809a722013-08-09 22:05:32 -07001097 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1098 typedef MappingTable::DexToPcIterator It;
1099 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1100 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001101 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001102 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001103 }
1104 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001105 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001106 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001107 }
1108
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001109 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1110 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001111 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001112 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001113 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001114 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001115 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001116 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001117 bool first = true;
1118 for (size_t reg = 0; reg < num_regs; reg++) {
1119 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1120 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001121 os << "GC map objects: v" << reg << " (";
1122 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001123 os << ")";
1124 first = false;
1125 } else {
1126 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001127 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001128 os << ")";
1129 }
1130 }
1131 }
1132 if (!first) {
1133 os << "\n";
1134 }
1135 }
1136 }
1137 }
1138
Mathieu Chartier590fee92013-09-13 13:46:47 -07001139 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1140 const OatFile::OatMethod& oat_method,
1141 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1142 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001143 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001144 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001145 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1146 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1147 if (kind != kUndefined) {
1148 if (first) {
1149 os << "VRegs: v";
1150 first = false;
1151 } else {
1152 os << ", v";
1153 }
1154 os << reg << " (";
1155 switch (kind) {
1156 case kImpreciseConstant:
1157 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1158 DescribeVReg(os, oat_method, code_item, reg, kind);
1159 break;
1160 case kConstant:
1161 os << "Constant: " << kinds.at((reg * 2) + 1);
1162 break;
1163 default:
1164 DescribeVReg(os, oat_method, code_item, reg, kind);
1165 break;
1166 }
1167 os << ")";
1168 }
1169 }
1170 if (!first) {
1171 os << "\n";
1172 }
1173 }
1174
1175
Ian Rogersb23a7722012-10-09 16:54:26 -07001176 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001177 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001178 size_t i = 0;
1179 while (i < code_item->insns_size_in_code_units_) {
1180 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001181 os << StringPrintf("0x%04zx: %s\n", i, instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001182 i += instruction->SizeInCodeUnits();
1183 }
1184 }
1185 }
1186
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001187 verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx,
1188 const DexFile* dex_file,
1189 const DexFile::ClassDef& class_def,
1190 const DexFile::CodeItem* code_item,
1191 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001192 if ((method_access_flags & kAccNative) == 0) {
1193 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001194 StackHandleScope<1> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001195 Handle<mirror::DexCache> dex_cache(
1196 hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
Andreas Gampe00b25f32014-09-17 21:49:05 -07001197 DCHECK(options_->class_loader_ != nullptr);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001198 return verifier::MethodVerifier::VerifyMethodAndDump(soa.Self(), os, dex_method_idx, dex_file,
1199 dex_cache,
Andreas Gampe00b25f32014-09-17 21:49:05 -07001200 *options_->class_loader_,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001201 &class_def, code_item,
1202 NullHandle<mirror::ArtMethod>(),
1203 method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001204 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001205
1206 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001207 }
1208
Mathieu Chartier590fee92013-09-13 13:46:47 -07001209 void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001210 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1211 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001212 const void* portable_code = oat_method.GetPortableCode();
1213 const void* quick_code = oat_method.GetQuickCode();
1214
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001215 if (code_size == 0) {
1216 code_size = oat_method.GetQuickCodeSize();
1217 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001218 if ((code_size == 0) || ((portable_code == nullptr) && (quick_code == nullptr))) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001219 os << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001220 return;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001221 } else if (quick_code != nullptr) {
1222 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1223 size_t offset = 0;
1224 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001225 if (!bad_input) {
1226 DumpMappingAtOffset(os, oat_method, offset, false);
1227 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001228 offset += disassembler_->Dump(os, quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001229 if (!bad_input) {
1230 uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
1231 if (dex_pc != DexFile::kDexNoIndex) {
1232 DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
1233 if (verifier != nullptr) {
1234 DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
1235 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001236 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001237 }
1238 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001239 } else {
1240 CHECK(portable_code != nullptr);
1241 CHECK_EQ(code_size, 0U); // TODO: disassembly of portable is currently not supported.
Ian Rogersb23a7722012-10-09 16:54:26 -07001242 }
1243 }
1244
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001245 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001246 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
1247 const OatDumperOptions* options_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001248 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001249 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001250};
1251
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001252class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001253 public:
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001254 explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001255 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
1256 : os_(os),
1257 image_space_(image_space),
1258 image_header_(image_header),
1259 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001260
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001261 bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001262 std::ostream& os = *os_;
1263 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001264
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001265 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001266
Mathieu Chartier31e89252013-08-28 11:29:12 -07001267 os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
1268 << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
1269
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001270 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001271
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001272 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001273
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001274 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1275
1276 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1277
1278 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001279
Alex Lighta59dd802014-07-02 16:28:08 -07001280 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1281
Igor Murashkin46774762014-10-22 11:37:02 -07001282 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1283
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001284 {
1285 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
1286 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1287 std::ostream indent1_os(&indent1_filter);
1288 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
1289 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1290 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1291 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001292 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001293 indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
1294 if (image_root_object->IsObjectArray()) {
1295 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
1296 std::ostream indent2_os(&indent2_filter);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001297 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001298 = image_root_object->AsObjectArray<mirror::Object>();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001299 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001300 mirror::Object* value = image_root_object_array->Get(i);
Ian Rogersfa824272013-11-05 16:12:57 -08001301 size_t run = 0;
1302 for (int32_t j = i + 1; j < image_root_object_array->GetLength(); j++) {
1303 if (value == image_root_object_array->Get(j)) {
1304 run++;
1305 } else {
1306 break;
1307 }
1308 }
1309 if (run == 0) {
1310 indent2_os << StringPrintf("%d: ", i);
1311 } else {
1312 indent2_os << StringPrintf("%d to %zd: ", i, i + run);
1313 i = i + run;
1314 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001315 if (value != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001316 PrettyObjectValue(indent2_os, value->GetClass(), value);
1317 } else {
1318 indent2_os << i << ": null\n";
1319 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001320 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001321 }
1322 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001323 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001324 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001325
Brian Carlstromaded5f72011-10-07 17:15:04 -07001326 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001327 std::string image_filename = image_space_.GetImageFilename();
1328 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001329 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001330 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001331 std::string error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -07001332 const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1333 if (oat_file == nullptr) {
Igor Murashkin46774762014-10-22 11:37:02 -07001334 oat_file = OatFile::Open(oat_location, oat_location, nullptr, nullptr, false, &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001335 if (oat_file == nullptr) {
1336 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001337 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001338 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001339 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001340 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001341
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001342 stats_.oat_file_bytes = oat_file->Size();
1343
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001344 oat_dumper_.reset(new OatDumper(*oat_file, oat_dumper_options_.release()));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001345
Mathieu Chartier02e25112013-08-14 16:14:24 -07001346 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001347 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001348 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1349 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001350 }
1351
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001352 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001353
1354 // Loop through all the image spaces and dump their objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001355 gc::Heap* heap = Runtime::Current()->GetHeap();
1356 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07001357 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001358 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001359 {
1360 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1361 heap->FlushAllocStack();
1362 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001363 // Since FlushAllocStack() above resets the (active) allocation
1364 // stack. Need to revoke the thread-local allocation stacks that
1365 // point into it.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001366 {
1367 self->TransitionFromRunnableToSuspended(kNative);
1368 ThreadList* thread_list = Runtime::Current()->GetThreadList();
1369 thread_list->SuspendAll();
1370 heap->RevokeAllThreadLocalAllocationStacks(self);
1371 thread_list->ResumeAll();
1372 self->TransitionFromSuspendedToRunnable();
1373 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001374 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001375 {
1376 std::ostream* saved_os = os_;
1377 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1378 std::ostream indent_os(&indent_filter);
1379 os_ = &indent_os;
1380 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001381 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001382 if (space->IsImageSpace()) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001383 gc::space::ImageSpace* image_space = space->AsImageSpace();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001384 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1385 indent_os << "\n";
1386 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001387 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001388 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001389 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001390 indent_os << "\n";
1391 os_ = saved_os;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001392 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001393 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001394 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001395 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001396 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001397 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001398 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001399 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001400 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001401 size_t header_bytes = sizeof(ImageHeader);
1402 stats_.header_bytes = header_bytes;
1403 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1404 stats_.alignment_bytes += alignment_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001405 stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
1406 stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001407 stats_.Dump(os);
1408 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001409
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001410 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001411
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001412 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001413 }
1414
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001415 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001416 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001417 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001418 CHECK(type != nullptr);
1419 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001420 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001421 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001422 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001423 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001424 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001425 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001426 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001427 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001428 } else if (type->IsArtFieldClass()) {
1429 mirror::ArtField* field = value->AsArtField();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001430 os << StringPrintf("%p Field: %s\n", field, PrettyField(field).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001431 } else if (type->IsArtMethodClass()) {
1432 mirror::ArtMethod* method = value->AsArtMethod();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001433 os << StringPrintf("%p Method: %s\n", method, PrettyMethod(method).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001434 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001435 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001436 }
1437 }
1438
Brian Carlstromea46f952013-07-30 01:26:50 -07001439 static void PrintField(std::ostream& os, mirror::ArtField* field, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001440 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001441 const char* descriptor = field->GetTypeDescriptor();
1442 os << StringPrintf("%s: ", field->GetName());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001443 if (descriptor[0] != 'L' && descriptor[0] != '[') {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001444 StackHandleScope<1> hs(Thread::Current());
1445 FieldHelper fh(hs.NewHandle(field));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001446 mirror::Class* type = fh.GetType();
Fred Shih37f05ef2014-07-16 18:38:08 -07001447 DCHECK(type->IsPrimitive());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001448 if (type->IsPrimitiveLong()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001449 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -07001450 } else if (type->IsPrimitiveDouble()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001451 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -07001452 } else if (type->IsPrimitiveFloat()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001453 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Fred Shih37f05ef2014-07-16 18:38:08 -07001454 } else if (type->IsPrimitiveInt()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001455 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Fred Shih37f05ef2014-07-16 18:38:08 -07001456 } else if (type->IsPrimitiveChar()) {
1457 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
1458 } else if (type->IsPrimitiveShort()) {
1459 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
1460 } else if (type->IsPrimitiveBoolean()) {
1461 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1462 field->GetBoolean(obj));
1463 } else if (type->IsPrimitiveByte()) {
1464 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
1465 } else {
1466 LOG(FATAL) << "Unknown type: " << PrettyClass(type);
Ian Rogers48efc2b2012-08-27 17:20:31 -07001467 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001468 } else {
Ian Rogers48efc2b2012-08-27 17:20:31 -07001469 // Get the value, don't compute the type unless it is non-null as we don't want
1470 // to cause class loading.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001471 mirror::Object* value = field->GetObj(obj);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001472 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001473 os << StringPrintf("null %s\n", PrettyDescriptor(descriptor).c_str());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001474 } else {
Ian Rogers50239c72013-06-17 14:53:22 -07001475 // Grab the field type without causing resolution.
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001476 StackHandleScope<1> hs(Thread::Current());
1477 FieldHelper fh(hs.NewHandle(field));
Ian Rogers50239c72013-06-17 14:53:22 -07001478 mirror::Class* field_type = fh.GetType(false);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001479 if (field_type != nullptr) {
Ian Rogers50239c72013-06-17 14:53:22 -07001480 PrettyObjectValue(os, field_type, value);
1481 } else {
1482 os << StringPrintf("%p %s\n", value, PrettyDescriptor(descriptor).c_str());
1483 }
Ian Rogers48efc2b2012-08-27 17:20:31 -07001484 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001485 }
1486 }
1487
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001488 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001489 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001490 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001491 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001492 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001493 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001494 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001495 if (fields != nullptr) {
Ian Rogersd5b32602012-02-26 16:40:04 -08001496 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001497 mirror::ArtField* field = fields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001498 PrintField(os, field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001499 }
1500 }
1501 }
1502
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001503 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001504 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001505 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001506
Ian Rogersef7d42f2014-01-06 12:55:46 -08001507 const void* GetQuickOatCodeBegin(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001508 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001509 const void* quick_code = m->GetEntryPointFromQuickCompiledCode();
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001510 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001511 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001512 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001513 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001514 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001515 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001516 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001517 }
1518
Ian Rogersef7d42f2014-01-06 12:55:46 -08001519 uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001520 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001521 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1522 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001523 return 0;
1524 }
1525 return oat_code_begin[-1];
1526 }
1527
Ian Rogersef7d42f2014-01-06 12:55:46 -08001528 const void* GetQuickOatCodeEnd(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001529 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001530 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001531 if (oat_code_begin == nullptr) {
1532 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001533 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001534 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001535 }
1536
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001537 static void Callback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001538 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001539 DCHECK(obj != nullptr);
1540 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001541 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001542 if (!state->InDumpSpace(obj)) {
1543 return;
1544 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001545
1546 size_t object_bytes = obj->SizeOf();
1547 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1548 state->stats_.object_bytes += object_bytes;
1549 state->stats_.alignment_bytes += alignment_bytes;
1550
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001551 std::ostream& os = *state->os_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001552 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001553 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001554 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1555 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001556 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001557 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001558 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1559 << klass->GetStatus() << ")\n";
Brian Carlstromea46f952013-07-30 01:26:50 -07001560 } else if (obj->IsArtField()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001561 os << StringPrintf("%p: java.lang.reflect.ArtField %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001562 PrettyField(obj->AsArtField()).c_str());
1563 } else if (obj->IsArtMethod()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001564 os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001565 PrettyMethod(obj->AsArtMethod()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001566 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001567 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001568 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001569 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001570 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001571 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001572 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1573 std::ostream indent_os(&indent_filter);
1574 DumpFields(indent_os, obj, obj_class);
Ian Rogersd5b32602012-02-26 16:40:04 -08001575 if (obj->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001576 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Ian Rogersd5b32602012-02-26 16:40:04 -08001577 int32_t length = obj_array->GetLength();
1578 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001579 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001580 size_t run = 0;
1581 for (int32_t j = i + 1; j < length; j++) {
1582 if (value == obj_array->Get(j)) {
1583 run++;
1584 } else {
1585 break;
1586 }
1587 }
1588 if (run == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001589 indent_os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001590 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001591 indent_os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001592 i = i + run;
1593 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001594 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001595 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001596 PrettyObjectValue(indent_os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001597 }
1598 } else if (obj->IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001599 mirror::ObjectArray<mirror::ArtField>* sfields = obj->AsClass()->GetSFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001600 if (sfields != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001601 indent_os << "STATICS:\n";
1602 Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
1603 std::ostream indent2_os(&indent2_filter);
Ian Rogersd5b32602012-02-26 16:40:04 -08001604 for (int32_t i = 0; i < sfields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001605 mirror::ArtField* field = sfields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001606 PrintField(indent2_os, field, field->GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001607 }
1608 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001609 } else if (obj->IsArtMethod()) {
1610 mirror::ArtMethod* method = obj->AsArtMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001611 if (method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001612 // TODO: portable dumping.
1613 DCHECK(method->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1614 DCHECK(method->GetMappingTable() == nullptr) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001615 bool first_occurrence;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001616 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1617 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1618 state->ComputeOatSize(quick_oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001619 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001620 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001621 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001622 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCode()) {
1623 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001624 }
Ian Rogers19846512012-02-24 11:42:47 -08001625 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
Jeff Hao88474b42013-10-23 16:24:40 -07001626 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001627 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001628 DCHECK(method->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1629 DCHECK(method->GetMappingTable() == nullptr) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001630 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001631 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -07001632 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001633 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001634
Elliott Hughesa0e18062012-04-13 15:59:59 -07001635 bool first_occurrence;
Ian Rogers0c7abda2012-09-19 13:33:42 -07001636 size_t gc_map_bytes = state->ComputeOatSize(method->GetNativeGcMap(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001637 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001638 state->stats_.gc_map_bytes += gc_map_bytes;
1639 }
1640
1641 size_t pc_mapping_table_bytes =
Ian Rogers1809a722013-08-09 22:05:32 -07001642 state->ComputeOatSize(method->GetMappingTable(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001643 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001644 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1645 }
1646
1647 size_t vmap_table_bytes =
Ian Rogers1809a722013-08-09 22:05:32 -07001648 state->ComputeOatSize(method->GetVmapTable(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001649 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001650 state->stats_.vmap_table_bytes += vmap_table_bytes;
1651 }
1652
Ian Rogersef7d42f2014-01-06 12:55:46 -08001653 // TODO: portable dumping.
1654 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1655 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1656 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1657 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001658 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001659 state->stats_.managed_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001660 if (method->IsConstructor()) {
1661 if (method->IsStatic()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001662 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001663 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001664 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001665 }
1666 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001667 state->stats_.large_method_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001668 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001669 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001670 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001671
Ian Rogersef7d42f2014-01-06 12:55:46 -08001672 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001673 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
1674 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001675
1676 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersef7d42f2014-01-06 12:55:46 -08001677 vmap_table_bytes + quick_oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001678
1679 double expansion =
Ian Rogersef7d42f2014-01-06 12:55:46 -08001680 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001681 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001682 }
1683 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001684 std::string temp;
1685 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001686 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001687
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001688 std::set<const void*> already_seen_;
1689 // Compute the size of the given data within the oat file and whether this is the first time
1690 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001691 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001692 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001693 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001694 already_seen_.insert(oat_data);
1695 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001696 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001697 }
1698 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001699 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001700
1701 public:
1702 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001703 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001704 size_t file_bytes;
1705
1706 size_t header_bytes;
1707 size_t object_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001708 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001709 size_t alignment_bytes;
1710
1711 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001712 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001713 size_t managed_to_native_code_bytes;
1714 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001715 size_t class_initializer_code_bytes;
1716 size_t large_initializer_code_bytes;
1717 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001718
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001719 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001720 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001721 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001722
1723 size_t dex_instruction_bytes;
1724
Brian Carlstromea46f952013-07-30 01:26:50 -07001725 std::vector<mirror::ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001726 std::vector<size_t> method_outlier_size;
1727 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07001728 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001729
1730 explicit Stats()
1731 : oat_file_bytes(0),
1732 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001733 header_bytes(0),
1734 object_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07001735 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001736 alignment_bytes(0),
1737 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001738 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001739 managed_to_native_code_bytes(0),
1740 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001741 class_initializer_code_bytes(0),
1742 large_initializer_code_bytes(0),
1743 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001744 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001745 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001746 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001747 dex_instruction_bytes(0) {}
1748
Elliott Hughesa0e18062012-04-13 15:59:59 -07001749 struct SizeAndCount {
1750 SizeAndCount(size_t bytes, size_t count) : bytes(bytes), count(count) {}
1751 size_t bytes;
1752 size_t count;
1753 };
1754 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1755 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001756
Ian Rogersfc0e94b2013-09-23 23:51:32 -07001757 void Update(const char* descriptor, size_t object_bytes) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001758 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1759 if (it != sizes_and_counts.end()) {
1760 it->second.bytes += object_bytes;
1761 it->second.count += 1;
1762 } else {
1763 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes, 1));
1764 }
1765 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001766
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001767 double PercentOfOatBytes(size_t size) {
1768 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1769 }
1770
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001771 double PercentOfFileBytes(size_t size) {
1772 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1773 }
1774
1775 double PercentOfObjectBytes(size_t size) {
1776 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1777 }
1778
Brian Carlstromea46f952013-07-30 01:26:50 -07001779 void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001780 method_outlier_size.push_back(total_size);
1781 method_outlier_expansion.push_back(expansion);
1782 method_outlier.push_back(method);
1783 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001784
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001785 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001786 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001787 size_t sum_of_sizes = 0;
1788 size_t sum_of_sizes_squared = 0;
1789 size_t sum_of_expansion = 0;
1790 size_t sum_of_expansion_squared = 0;
1791 size_t n = method_outlier_size.size();
1792 for (size_t i = 0; i < n; i++) {
1793 size_t cur_size = method_outlier_size[i];
1794 sum_of_sizes += cur_size;
1795 sum_of_sizes_squared += cur_size * cur_size;
1796 double cur_expansion = method_outlier_expansion[i];
1797 sum_of_expansion += cur_expansion;
1798 sum_of_expansion_squared += cur_expansion * cur_expansion;
1799 }
1800 size_t size_mean = sum_of_sizes / n;
1801 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1802 double expansion_mean = sum_of_expansion / n;
1803 double expansion_variance =
1804 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1805
1806 // Dump methods whose size is a certain number of standard deviations from the mean
1807 size_t dumped_values = 0;
1808 size_t skipped_values = 0;
1809 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1810 size_t cur_size_variance = i * i * size_variance;
1811 bool first = true;
1812 for (size_t j = 0; j < n; j++) {
1813 size_t cur_size = method_outlier_size[j];
1814 if (cur_size > size_mean) {
1815 size_t cur_var = cur_size - size_mean;
1816 cur_var = cur_var * cur_var;
1817 if (cur_var > cur_size_variance) {
1818 if (dumped_values > 20) {
1819 if (i == 1) {
1820 skipped_values++;
1821 } else {
1822 i = 2; // jump to counting for 1 standard deviation
1823 break;
1824 }
1825 } else {
1826 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001827 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001828 first = false;
1829 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001830 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001831 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001832 method_outlier_size[j] = 0; // don't consider this method again
1833 dumped_values++;
1834 }
1835 }
1836 }
1837 }
1838 }
1839 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001840 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001841 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001842 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001843 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001844
1845 // Dump methods whose expansion is a certain number of standard deviations from the mean
1846 dumped_values = 0;
1847 skipped_values = 0;
1848 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1849 double cur_expansion_variance = i * i * expansion_variance;
1850 bool first = true;
1851 for (size_t j = 0; j < n; j++) {
1852 double cur_expansion = method_outlier_expansion[j];
1853 if (cur_expansion > expansion_mean) {
1854 size_t cur_var = cur_expansion - expansion_mean;
1855 cur_var = cur_var * cur_var;
1856 if (cur_var > cur_expansion_variance) {
1857 if (dumped_values > 20) {
1858 if (i == 1) {
1859 skipped_values++;
1860 } else {
1861 i = 2; // jump to counting for 1 standard deviation
1862 break;
1863 }
1864 } else {
1865 if (first) {
1866 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001867 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001868 first = false;
1869 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001870 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001871 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001872 method_outlier_expansion[j] = 0.0; // don't consider this method again
1873 dumped_values++;
1874 }
1875 }
1876 }
1877 }
1878 }
1879 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001880 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001881 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001882 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001883 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001884 }
1885
Ian Rogersb726dcb2012-09-05 08:57:23 -07001886 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001887 {
1888 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1889 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
1890 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1891 std::ostream indent_os(&indent_filter);
1892 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
1893 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier32327092013-08-30 14:04:08 -07001894 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001895 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
1896 header_bytes, PercentOfFileBytes(header_bytes),
1897 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07001898 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001899 alignment_bytes, PercentOfFileBytes(alignment_bytes))
1900 << std::flush;
Mathieu Chartier32327092013-08-30 14:04:08 -07001901 CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001902 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001903
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001904 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001905 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07001906 for (const auto& sizes_and_count : sizes_and_counts) {
1907 const std::string& descriptor(sizes_and_count.first);
1908 double average = static_cast<double>(sizes_and_count.second.bytes) /
1909 static_cast<double>(sizes_and_count.second.count);
1910 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001911 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001912 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001913 descriptor.c_str(), sizes_and_count.second.bytes,
1914 sizes_and_count.second.count, average, percent);
1915 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001916 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001917 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001918 CHECK_EQ(object_bytes, object_bytes_total);
1919
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001920 os << StringPrintf("oat_file_bytes = %8zd\n"
1921 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1922 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1923 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1924 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1925 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1926 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001927 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001928 managed_code_bytes,
1929 PercentOfOatBytes(managed_code_bytes),
1930 managed_to_native_code_bytes,
1931 PercentOfOatBytes(managed_to_native_code_bytes),
1932 native_to_managed_code_bytes,
1933 PercentOfOatBytes(native_to_managed_code_bytes),
1934 class_initializer_code_bytes,
1935 PercentOfOatBytes(class_initializer_code_bytes),
1936 large_initializer_code_bytes,
1937 PercentOfOatBytes(large_initializer_code_bytes),
1938 large_method_code_bytes,
1939 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001940 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07001941 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001942 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001943 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
1944 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07001945 }
1946
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001947 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1948 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1949 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001950 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1951 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1952 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001953 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001954
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001955 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
1956 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001957 static_cast<double>(managed_code_bytes) /
1958 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07001959 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001960 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001961 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001962
1963 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001964 }
1965 } stats_;
1966
1967 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001968 enum {
1969 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1970 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1971 kLargeConstructorDexBytes = 4000,
1972 // Number of bytes for a method to be considered large. Based on the 4000 basic block
1973 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1974 kLargeMethodDexBytes = 16000
1975 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001976 std::ostream* os_;
Ian Rogers1d54e732013-05-02 21:10:01 -07001977 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001978 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001979 std::unique_ptr<OatDumper> oat_dumper_;
1980 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001981
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001982 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001983};
1984
Andreas Gampe00b25f32014-09-17 21:49:05 -07001985static NoopCompilerCallbacks callbacks;
Elliott Hughes72395bf2012-04-24 13:45:26 -07001986
Andreas Gampe00b25f32014-09-17 21:49:05 -07001987static Runtime* StartRuntime(const char* boot_image_location, const char* image_location,
1988 InstructionSet instruction_set) {
Ian Rogerse63db272014-07-15 15:36:11 -07001989 RuntimeOptions options;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001990 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001991 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001992 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001993 std::string boot_oat_option;
Brian Carlstrom6c871802013-07-17 14:25:35 -07001994
1995 // We are more like a compiler than a run-time. We don't want to execute code.
Brian Carlstromc0a1b182014-03-04 23:19:06 -08001996 options.push_back(std::make_pair("compilercallbacks", &callbacks));
Brian Carlstrom6c871802013-07-17 14:25:35 -07001997
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001998 if (boot_image_location != nullptr) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001999 boot_image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002000 boot_image_option += boot_image_location;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002001 options.push_back(std::make_pair(boot_image_option.c_str(), nullptr));
Brian Carlstrom78128a62011-09-15 17:21:19 -07002002 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002003 if (image_location != nullptr) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07002004 image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002005 image_option += image_location;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002006 options.push_back(std::make_pair(image_option.c_str(), nullptr));
Brian Carlstromaded5f72011-10-07 17:15:04 -07002007 }
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002008 options.push_back(
2009 std::make_pair("imageinstructionset",
2010 reinterpret_cast<const void*>(GetInstructionSetString(instruction_set))));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07002011
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002012 if (!Runtime::Create(options, false)) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07002013 fprintf(stderr, "Failed to create runtime\n");
Andreas Gampe00b25f32014-09-17 21:49:05 -07002014 return nullptr;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002015 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002016
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002017 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
Ian Rogersb9beb2e2014-05-09 16:57:40 -07002018 // give it away now and then switch to a more manageable ScopedObjectAccess.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002019 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002020
2021 return Runtime::Current();
2022}
2023
2024static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
2025 std::ostream* os) {
2026 // Dumping the image, no explicit class loader.
2027 NullHandle<mirror::ClassLoader> null_class_loader;
2028 options->class_loader_ = &null_class_loader;
2029
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002030 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002031 gc::Heap* heap = runtime->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -07002032 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002033 CHECK(image_space != nullptr);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002034 const ImageHeader& image_header = image_space->GetImageHeader();
2035 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002036 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002037 return EXIT_FAILURE;
2038 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002039 ImageDumper image_dumper(os, *image_space, image_header, options);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002040 bool success = image_dumper.Dump();
2041 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002042}
2043
Andreas Gampe00b25f32014-09-17 21:49:05 -07002044static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2045 std::ostream* os) {
2046 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2047
2048 Thread* self = Thread::Current();
2049 CHECK(self != nullptr);
2050 // Need well-known-classes.
2051 WellKnownClasses::Init(self->GetJniEnv());
2052
2053 // Need to register dex files to get a working dex cache.
2054 ScopedObjectAccess soa(self);
2055 ClassLinker* class_linker = runtime->GetClassLinker();
2056 class_linker->RegisterOatFile(oat_file);
2057 std::vector<const DexFile*> dex_files;
2058 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2059 std::string error_msg;
2060 const DexFile* dex_file = odf->OpenDexFile(&error_msg);
2061 CHECK(dex_file != nullptr) << error_msg;
2062 class_linker->RegisterDexFile(*dex_file);
2063 dex_files.push_back(dex_file);
2064 }
2065
2066 // Need a class loader.
2067 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader);
2068 ScopedLocalRef<jobject> class_loader_local(soa.Env(),
2069 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
2070 jobject class_loader = soa.Env()->NewGlobalRef(class_loader_local.get());
2071 // Fake that we're a compiler.
2072 runtime->SetCompileTimeClassPath(class_loader, dex_files);
2073
2074 // Use the class loader while dumping.
2075 StackHandleScope<1> scope(self);
2076 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2077 soa.Decode<mirror::ClassLoader*>(class_loader));
2078 options->class_loader_ = &loader_handle;
2079
2080 OatDumper oat_dumper(*oat_file, options);
2081 bool success = oat_dumper.Dump(*os);
2082 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2083}
2084
2085static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
2086 // No image = no class loader.
2087 NullHandle<mirror::ClassLoader> null_class_loader;
2088 options->class_loader_ = &null_class_loader;
2089
2090 OatDumper oat_dumper(*oat_file, options);
2091 bool success = oat_dumper.Dump(*os);
2092 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2093}
2094
2095static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2096 std::ostream* os) {
2097 std::string error_msg;
Igor Murashkin46774762014-10-22 11:37:02 -07002098 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002099 if (oat_file == nullptr) {
2100 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2101 return EXIT_FAILURE;
2102 }
2103
2104 if (runtime != nullptr) {
2105 return DumpOatWithRuntime(runtime, oat_file, options, os);
2106 } else {
2107 return DumpOatWithoutRuntime(oat_file, options, os);
2108 }
2109}
2110
2111static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2112 std::string error_msg;
Igor Murashkin46774762014-10-22 11:37:02 -07002113 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002114 if (oat_file == nullptr) {
2115 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2116 return EXIT_FAILURE;
2117 }
2118
2119 OatSymbolizer oat_symbolizer(oat_file, output_name);
2120 if (!oat_symbolizer.Init()) {
2121 fprintf(stderr, "Failed to initialize symbolizer\n");
2122 return EXIT_FAILURE;
2123 }
2124 if (!oat_symbolizer.Symbolize()) {
2125 fprintf(stderr, "Failed to symbolize\n");
2126 return EXIT_FAILURE;
2127 }
2128
2129 return EXIT_SUCCESS;
2130}
2131
2132struct OatdumpArgs {
2133 bool Parse(int argc, char** argv) {
2134 // Skip over argv[0].
2135 argv++;
2136 argc--;
2137
2138 if (argc == 0) {
2139 fprintf(stderr, "No arguments specified\n");
2140 usage();
2141 return false;
2142 }
2143
2144 for (int i = 0; i < argc; i++) {
2145 const StringPiece option(argv[i]);
2146 if (option.starts_with("--oat-file=")) {
2147 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2148 } else if (option.starts_with("--image=")) {
2149 image_location_ = option.substr(strlen("--image=")).data();
2150 } else if (option.starts_with("--boot-image=")) {
2151 boot_image_location_ = option.substr(strlen("--boot-image=")).data();
2152 } else if (option.starts_with("--instruction-set=")) {
2153 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
2154 instruction_set_ = GetInstructionSetFromString(instruction_set_str.data());
2155 if (instruction_set_ == kNone) {
2156 fprintf(stderr, "Unsupported instruction set %s\n", instruction_set_str.data());
2157 usage();
2158 return false;
2159 }
2160 } else if (option =="--dump:raw_mapping_table") {
2161 dump_raw_mapping_table_ = true;
2162 } else if (option == "--dump:raw_gc_map") {
2163 dump_raw_gc_map_ = true;
2164 } else if (option == "--no-dump:vmap") {
2165 dump_vmap_ = false;
2166 } else if (option == "--no-disassemble") {
2167 disassemble_code_ = false;
2168 } else if (option.starts_with("--output=")) {
2169 output_name_ = option.substr(strlen("--output=")).ToString();
2170 const char* filename = output_name_.c_str();
2171 out_.reset(new std::ofstream(filename));
2172 if (!out_->good()) {
2173 fprintf(stderr, "Failed to open output filename %s\n", filename);
2174 usage();
2175 return false;
2176 }
2177 os_ = out_.get();
2178 } else if (option.starts_with("--symbolize=")) {
2179 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2180 symbolize_ = true;
2181 } else {
2182 fprintf(stderr, "Unknown argument %s\n", option.data());
2183 usage();
2184 return false;
2185 }
2186 }
2187
2188 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2189 fprintf(stderr, "Either --image or --oat must be specified\n");
2190 return false;
2191 }
2192
2193 if (image_location_ != nullptr && oat_filename_ != nullptr) {
2194 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
2195 return false;
2196 }
2197
2198 return true;
2199 }
2200
2201 const char* oat_filename_ = nullptr;
2202 const char* image_location_ = nullptr;
2203 const char* boot_image_location_ = nullptr;
2204 InstructionSet instruction_set_ = kRuntimeISA;
2205 std::string elf_filename_prefix_;
2206 std::ostream* os_ = &std::cout;
2207 std::unique_ptr<std::ofstream> out_;
2208 std::string output_name_;
2209 bool dump_raw_mapping_table_ = false;
2210 bool dump_raw_gc_map_ = false;
2211 bool dump_vmap_ = true;
2212 bool disassemble_code_ = true;
2213 bool symbolize_ = false;
2214};
2215
2216static int oatdump(int argc, char** argv) {
2217 InitLogging(argv);
2218
2219 OatdumpArgs args;
2220 if (!args.Parse(argc, argv)) {
2221 return EXIT_FAILURE;
2222 }
2223
2224 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2225 bool absolute_addresses = (args.oat_filename_ == nullptr);
2226
2227 std::unique_ptr<OatDumperOptions> oat_dumper_options(new OatDumperOptions(
2228 args.dump_raw_mapping_table_,
2229 args.dump_raw_gc_map_,
2230 args.dump_vmap_,
2231 args.disassemble_code_,
2232 absolute_addresses,
2233 nullptr));
2234
2235 std::unique_ptr<Runtime> runtime;
2236 if ((args.boot_image_location_ != nullptr || args.image_location_ != nullptr) &&
2237 !args.symbolize_) {
2238 // If we have a boot image option, try to start the runtime; except when just symbolizing.
2239 runtime.reset(StartRuntime(args.boot_image_location_,
2240 args.image_location_,
2241 args.instruction_set_));
Mathieu Chartierd424d082014-10-15 10:31:46 -07002242 } else {
2243 MemMap::Init();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002244 }
2245
2246 if (args.oat_filename_ != nullptr) {
2247 if (args.symbolize_) {
2248 return SymbolizeOat(args.oat_filename_, args.output_name_);
2249 } else {
2250 return DumpOat(runtime.get(), args.oat_filename_, oat_dumper_options.release(), args.os_);
2251 }
2252 }
2253
2254 if (runtime.get() == nullptr) {
2255 // We need the runtime when printing an image.
2256 return EXIT_FAILURE;
2257 }
2258
2259 return DumpImage(runtime.get(), args.image_location_, oat_dumper_options.release(), args.os_);
2260}
2261
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002262} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002263
2264int main(int argc, char** argv) {
2265 return art::oatdump(argc, argv);
2266}