blob: 2649ab767ee93930efadb837cec5e120e0ea319d [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",
115 "kDefaultImt",
Brian Carlstrome24fa612011-09-29 00:53:55 -0700116 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -0700117 "kRefsOnlySaveMethod",
118 "kRefsAndArgsSaveMethod",
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700119 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700120 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700121};
122
Ian Rogers0279ebb2014-10-08 17:27:48 -0700123class OatSymbolizer FINAL : public CodeOutput {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700124 public:
Ian Rogers0279ebb2014-10-08 17:27:48 -0700125 explicit OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
126 oat_file_(oat_file), builder_(nullptr), elf_output_(nullptr),
127 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
128 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700129
130 bool Init() {
131 Elf32_Word oat_data_size = oat_file_->GetOatHeader().GetExecutableOffset();
132
133 uint32_t diff = static_cast<uint32_t>(oat_file_->End() - oat_file_->Begin());
134 uint32_t oat_exec_size = diff - oat_data_size;
135
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700136 elf_output_ = OS::CreateEmptyFile(output_name_.c_str());
137
138 builder_.reset(new ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
139 Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(
140 this,
141 elf_output_,
142 oat_file_->GetOatHeader().GetInstructionSet(),
143 0,
144 oat_data_size,
145 oat_data_size,
146 oat_exec_size,
147 true,
148 false));
149
150 if (!builder_->Init()) {
151 builder_.reset(nullptr);
152 return false;
153 }
154
155 return true;
156 }
157
158 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
159 uint32_t,
160 const OatFile::OatMethod&,
161 const DexFile&,
162 uint32_t,
163 const DexFile::CodeItem*,
164 uint32_t);
165
166 bool Symbolize() {
167 if (builder_.get() == nullptr) {
168 return false;
169 }
170
171 Walk(&art::OatSymbolizer::RegisterForDedup);
172
173 NormalizeState();
174
175 Walk(&art::OatSymbolizer::AddSymbol);
176
177 bool result = builder_->Write();
178
179 elf_output_->Flush();
180 elf_output_->Close();
181
182 return result;
183 }
184
185 void Walk(Callback callback) {
186 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
187 for (size_t i = 0; i < oat_dex_files.size(); i++) {
188 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
189 CHECK(oat_dex_file != NULL);
190 WalkOatDexFile(oat_dex_file, callback);
191 }
192 }
193
194 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
195 std::string error_msg;
196 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
197 if (dex_file.get() == nullptr) {
198 return;
199 }
200 for (size_t class_def_index = 0;
201 class_def_index < dex_file->NumClassDefs();
202 class_def_index++) {
203 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
204 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
205 OatClassType type = oat_class.GetType();
206 switch (type) {
207 case kOatClassAllCompiled:
208 case kOatClassSomeCompiled:
209 WalkOatClass(oat_class, *dex_file.get(), class_def, callback);
210 break;
211
212 case kOatClassNoneCompiled:
213 case kOatClassMax:
214 // Ignore.
215 break;
216 }
217 }
218 }
219
220 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
221 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700222 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700223 if (class_data == nullptr) { // empty class such as a marker interface?
224 return;
225 }
226 // Note: even if this is an interface or a native class, we still have to walk it, as there
227 // might be a static initializer.
228 ClassDataItemIterator it(dex_file, class_data);
229 SkipAllFields(&it);
230 uint32_t class_method_idx = 0;
231 while (it.HasNextDirectMethod()) {
232 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
233 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700234 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700235 class_method_idx++;
236 it.Next();
237 }
238 while (it.HasNextVirtualMethod()) {
239 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
240 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700241 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700242 class_method_idx++;
243 it.Next();
244 }
245 DCHECK(!it.HasNext());
246 }
247
248 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
249 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
250 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
251 uint32_t method_access_flags, Callback callback) {
252 if ((method_access_flags & kAccAbstract) != 0) {
253 // Abstract method, no code.
254 return;
255 }
256 if (oat_method.GetCodeOffset() == 0) {
257 // No code.
258 return;
259 }
260
261 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
262 method_access_flags);
263 }
264
265 void RegisterForDedup(const DexFile::ClassDef& class_def, uint32_t class_method_index,
266 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
267 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
268 uint32_t method_access_flags) {
269 state_[oat_method.GetCodeOffset()]++;
270 }
271
272 void NormalizeState() {
273 for (auto& x : state_) {
274 if (x.second == 1) {
275 state_[x.first] = 0;
276 }
277 }
278 }
279
280 enum class DedupState { // private
281 kNotDeduplicated,
282 kDeduplicatedFirst,
283 kDeduplicatedOther
284 };
285 DedupState IsDuplicated(uint32_t offset) {
286 if (state_[offset] == 0) {
287 return DedupState::kNotDeduplicated;
288 }
289 if (state_[offset] == 1) {
290 return DedupState::kDeduplicatedOther;
291 }
292 state_[offset] = 1;
293 return DedupState::kDeduplicatedFirst;
294 }
295
296 void AddSymbol(const DexFile::ClassDef& class_def, uint32_t class_method_index,
297 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
298 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
299 uint32_t method_access_flags) {
300 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
301 if (dedup != DedupState::kDeduplicatedOther) {
302 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
303
304 if (dedup == DedupState::kDeduplicatedFirst) {
305 pretty_name = "[Dedup]" + pretty_name;
306 }
307
308 ElfSymtabBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr,
Ian Rogers0279ebb2014-10-08 17:27:48 -0700309 Elf32_Sym, Elf32_Shdr>* symtab = builder_->GetSymtabBuilder();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700310
Ian Rogers0279ebb2014-10-08 17:27:48 -0700311 symtab->AddSymbol(pretty_name, &builder_->GetTextBuilder(),
312 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
313 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700314 }
315 }
316
Vladimir Markof4da6752014-08-01 19:04:18 +0100317 // Set oat data offset. Required by ElfBuilder/CodeOutput.
318 void SetCodeOffset(size_t offset) {
319 // Nothing to do.
320 }
321
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700322 // Write oat code. Required by ElfBuilder/CodeOutput.
323 bool Write(OutputStream* out) {
324 return out->WriteFully(oat_file_->Begin(), oat_file_->End() - oat_file_->Begin());
325 }
326
327 private:
328 static void SkipAllFields(ClassDataItemIterator* it) {
329 while (it->HasNextStaticField()) {
330 it->Next();
331 }
332 while (it->HasNextInstanceField()) {
333 it->Next();
334 }
335 }
336
337 const OatFile* oat_file_;
338 std::unique_ptr<ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
339 Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr> > builder_;
340 File* elf_output_;
341 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700342 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700343};
344
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700345class OatDumperOptions {
346 public:
347 OatDumperOptions(bool dump_raw_mapping_table,
348 bool dump_raw_gc_map,
349 bool dump_vmap,
350 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700351 bool absolute_addresses,
352 Handle<mirror::ClassLoader>* class_loader)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700353 : dump_raw_mapping_table_(dump_raw_mapping_table),
354 dump_raw_gc_map_(dump_raw_gc_map),
355 dump_vmap_(dump_vmap),
356 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700357 absolute_addresses_(absolute_addresses),
358 class_loader_(class_loader) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700359
360 const bool dump_raw_mapping_table_;
361 const bool dump_raw_gc_map_;
362 const bool dump_vmap_;
363 const bool disassemble_code_;
364 const bool absolute_addresses_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700365 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700366};
367
Elliott Hughese3c845c2012-02-28 17:23:01 -0800368class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700369 public:
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700370 explicit OatDumper(const OatFile& oat_file, OatDumperOptions* options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000371 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800372 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700373 options_(options),
374 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet(),
375 new DisassemblerOptions(options_->absolute_addresses_,
376 oat_file.Begin()))) {
Andreas Gampe00b25f32014-09-17 21:49:05 -0700377 CHECK(options_->class_loader_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800378 AddAllOffsets();
379 }
380
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700381 ~OatDumper() {
382 delete options_;
383 delete disassembler_;
384 }
385
386 bool Dump(std::ostream& os) {
387 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800388 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700389
390 os << "MAGIC:\n";
391 os << oat_header.GetMagic() << "\n\n";
392
393 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800394 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700395
Elliott Hughesa72ec822012-03-05 17:12:22 -0800396 os << "INSTRUCTION SET:\n";
397 os << oat_header.GetInstructionSet() << "\n\n";
398
Dave Allison70202782013-10-22 17:52:19 -0700399 os << "INSTRUCTION SET FEATURES:\n";
400 os << oat_header.GetInstructionSetFeatures().GetFeatureString() << "\n\n";
401
Brian Carlstromaded5f72011-10-07 17:15:04 -0700402 os << "DEX FILE COUNT:\n";
403 os << oat_header.GetDexFileCount() << "\n\n";
404
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800405#define DUMP_OAT_HEADER_OFFSET(label, offset) \
406 os << label " OFFSET:\n"; \
407 os << StringPrintf("0x%08x", oat_header.offset()); \
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700408 if (oat_header.offset() != 0 && options_->absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800409 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
410 } \
411 os << StringPrintf("\n\n");
412
413 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
414 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
415 GetInterpreterToInterpreterBridgeOffset);
416 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
417 GetInterpreterToCompiledCodeBridgeOffset);
418 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
419 GetJniDlsymLookupOffset);
420 DUMP_OAT_HEADER_OFFSET("PORTABLE IMT CONFLICT TRAMPOLINE",
421 GetPortableImtConflictTrampolineOffset);
422 DUMP_OAT_HEADER_OFFSET("PORTABLE RESOLUTION TRAMPOLINE",
423 GetPortableResolutionTrampolineOffset);
424 DUMP_OAT_HEADER_OFFSET("PORTABLE TO INTERPRETER BRIDGE",
425 GetPortableToInterpreterBridgeOffset);
426 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
427 GetQuickGenericJniTrampolineOffset);
428 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
429 GetQuickImtConflictTrampolineOffset);
430 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
431 GetQuickResolutionTrampolineOffset);
432 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
433 GetQuickToInterpreterBridgeOffset);
434#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700435
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700436 os << "IMAGE PATCH DELTA:\n";
437 os << StringPrintf("%d (0x%08x)\n\n",
438 oat_header.GetImagePatchDelta(),
439 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700440
Brian Carlstrom28db0122012-10-18 16:20:41 -0700441 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
442 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
443
444 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800445 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700446
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700447 // Print the key-value store.
448 {
449 os << "KEY VALUE STORE:\n";
450 size_t index = 0;
451 const char* key;
452 const char* value;
453 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
454 os << key << " = " << value << "\n";
455 index++;
456 }
457 os << "\n";
458 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700459
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700460 if (options_->absolute_addresses_) {
461 os << "BEGIN:\n";
462 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700463
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700464 os << "END:\n";
465 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
466 }
467
468 os << "SIZE:\n";
469 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700470
471 os << std::flush;
472
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800473 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
474 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700475 CHECK(oat_dex_file != nullptr);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700476 if (!DumpOatDexFile(os, *oat_dex_file)) {
477 success = false;
478 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700479 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700480 os << std::flush;
481 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700482 }
483
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800484 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700485 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
486 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800487 return 0; // Address not in oat file
488 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800489 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
490 reinterpret_cast<uintptr_t>(oat_file_.Begin());
491 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800492 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800493 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800494 return end_offset - begin_offset;
495 }
496
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700497 InstructionSet GetInstructionSet() {
498 return oat_file_.GetOatHeader().GetInstructionSet();
499 }
500
Ian Rogersef7d42f2014-01-06 12:55:46 -0800501 const void* GetQuickOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800502 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
503 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700504 CHECK(oat_dex_file != nullptr);
505 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700506 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700507 if (dex_file.get() == nullptr) {
508 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
509 << "': " << error_msg;
510 } else {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700511 const DexFile::ClassDef* class_def =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700512 dex_file->FindClassDef(m->GetDeclaringClassDescriptor());
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700513 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700514 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100515 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800516 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100517 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800518 }
519 }
520 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700521 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800522 }
523
Brian Carlstromaded5f72011-10-07 17:15:04 -0700524 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800525 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800526 // We don't know the length of the code for each method, but we need to know where to stop
527 // when disassembling. What we do know is that a region of code will be followed by some other
528 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
529 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800530 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
531 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700532 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700533 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700534 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700535 if (dex_file.get() == nullptr) {
536 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
537 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800538 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800539 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800540 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800541 for (size_t class_def_index = 0;
542 class_def_index < dex_file->NumClassDefs();
543 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800544 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100545 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700546 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700547 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800548 ClassDataItemIterator it(*dex_file, class_data);
549 SkipAllFields(it);
550 uint32_t class_method_index = 0;
551 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100552 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800553 it.Next();
554 }
555 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100556 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800557 it.Next();
558 }
559 }
560 }
561 }
562
563 // If the last thing in the file is code for a method, there won't be an offset for the "next"
564 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
565 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800566 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800567 }
568
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700569 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
570 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
571 }
572
Elliott Hughese3c845c2012-02-28 17:23:01 -0800573 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800574 uint32_t code_offset = oat_method.GetCodeOffset();
575 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
576 code_offset &= ~0x1;
577 }
578 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800579 offsets_.insert(oat_method.GetMappingTableOffset());
580 offsets_.insert(oat_method.GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700581 offsets_.insert(oat_method.GetNativeGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800582 }
583
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700584 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
585 bool success = true;
586 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800587 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800588 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700589
590 // Create the verifier early.
591
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700592 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700593 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700594 if (dex_file.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700595 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700596 os << std::flush;
597 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700598 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800599 for (size_t class_def_index = 0;
600 class_def_index < dex_file->NumClassDefs();
601 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700602 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
603 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700604 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100605 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700606 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
607 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100608 << " (" << oat_class.GetStatus() << ")"
609 << " (" << oat_class.GetType() << ")\n";
610 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800611 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
612 std::ostream indented_os(&indent_filter);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700613 if (!DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def)) {
614 success = false;
615 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700616 }
617
618 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700619 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700620 }
621
Elliott Hughese3c845c2012-02-28 17:23:01 -0800622 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700623 while (it.HasNextStaticField()) {
624 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700625 }
Ian Rogers0571d352011-11-03 19:51:38 -0700626 while (it.HasNextInstanceField()) {
627 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700628 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800629 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700630
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700631 bool DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800632 const DexFile::ClassDef& class_def) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700633 bool success = true;
Ian Rogers13735952014-10-08 12:43:28 -0700634 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700635 if (class_data == nullptr) { // empty class such as a marker interface?
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700636 os << std::flush;
637 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800638 }
639 ClassDataItemIterator it(dex_file, class_data);
640 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700641 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700642 while (it.HasNextDirectMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700643 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
644 it.GetMemberIndex(), it.GetMethodCodeItem(),
645 it.GetRawMemberAccessFlags())) {
646 success = false;
647 }
648 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700649 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700650 }
Ian Rogers0571d352011-11-03 19:51:38 -0700651 while (it.HasNextVirtualMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700652 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
653 it.GetMemberIndex(), it.GetMethodCodeItem(),
654 it.GetRawMemberAccessFlags())) {
655 success = false;
656 }
657 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700658 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700659 }
Ian Rogers0571d352011-11-03 19:51:38 -0700660 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700661 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700662 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700663 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800664
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700665 static constexpr uint32_t kPrologueBytes = 16;
666
667 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
668 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
669
670 bool DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700671 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700672 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800673 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
674 uint32_t method_access_flags) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700675 bool success = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800676 os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700677 class_method_index, PrettyMethod(dex_method_idx, dex_file, true).c_str(),
678 dex_method_idx);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800679 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700680 std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter));
681 Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count);
682 std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter));
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800683 {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700684 *indent1_os << "DEX CODE:\n";
685 DumpDexCode(*indent2_os, dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700686 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700687
688 std::unique_ptr<verifier::MethodVerifier> verifier;
689 if (Runtime::Current() != nullptr) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700690 *indent1_os << "VERIFIER TYPE ANALYSIS:\n";
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700691 verifier.reset(DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item,
692 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700693 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700694
695 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
696 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
697 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800698 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700699 *indent1_os << "OatMethodOffsets ";
700 if (options_->absolute_addresses_) {
701 *indent1_os << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100702 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700703 *indent1_os << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
704 if (oat_method_offsets_offset > oat_file_.Size()) {
705 *indent1_os << StringPrintf(
706 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
707 oat_method_offsets_offset, oat_file_.Size());
708 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
709 os << std::flush;
710 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800711 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700712
713 uint32_t code_offset = oat_method.GetCodeOffset();
714 *indent2_os << StringPrintf("code_offset: 0x%08x ", code_offset);
715 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
716 if (aligned_code_begin > oat_file_.Size()) {
717 *indent2_os << StringPrintf("WARNING: "
718 "code offset 0x%08x is past end of file 0x%08zx.\n",
719 aligned_code_begin, oat_file_.Size());
720 success = false;
721 }
722 *indent2_os << "\n";
723
724 *indent2_os << "gc_map: ";
725 if (options_->absolute_addresses_) {
726 *indent2_os << StringPrintf("%p ", oat_method.GetNativeGcMap());
727 }
728 uint32_t gc_map_offset = oat_method.GetNativeGcMapOffset();
729 *indent2_os << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
730 if (gc_map_offset > oat_file_.Size()) {
731 *indent2_os << StringPrintf("WARNING: "
732 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
733 gc_map_offset, oat_file_.Size());
734 success = false;
735 } else if (options_->dump_raw_gc_map_) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700736 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800737 std::ostream indent3_os(&indent3_filter);
738 DumpGcMap(indent3_os, oat_method, code_item);
739 }
740 }
741 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700742 *indent1_os << "OatQuickMethodHeader ";
743 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
744 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700745
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700746 if (options_->absolute_addresses_) {
747 *indent1_os << StringPrintf("%p ", method_header);
748 }
749 *indent1_os << StringPrintf("(offset=0x%08x)\n", method_header_offset);
750 if (method_header_offset > oat_file_.Size()) {
751 *indent1_os << StringPrintf(
752 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
753 method_header_offset, oat_file_.Size());
754 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
755 os << std::flush;
756 return false;
757 }
758
759 *indent2_os << "mapping_table: ";
760 if (options_->absolute_addresses_) {
761 *indent2_os << StringPrintf("%p ", oat_method.GetMappingTable());
762 }
763 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
764 *indent2_os << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
765 if (mapping_table_offset > oat_file_.Size()) {
766 *indent2_os << StringPrintf("WARNING: "
767 "mapping table offset 0x%08x is past end of file 0x%08zx. "
768 "mapping table offset was loaded from offset 0x%08x.\n",
769 mapping_table_offset, oat_file_.Size(),
770 oat_method.GetMappingTableOffsetOffset());
771 success = false;
772 } else if (options_->dump_raw_mapping_table_) {
773 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
774 std::ostream indent3_os(&indent3_filter);
775 DumpMappingTable(indent3_os, oat_method);
776 }
777
778 *indent2_os << "vmap_table: ";
779 if (options_->absolute_addresses_) {
780 *indent2_os << StringPrintf("%p ", oat_method.GetVmapTable());
781 }
782 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
783 *indent2_os << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
784 if (vmap_table_offset > oat_file_.Size()) {
785 *indent2_os << StringPrintf("WARNING: "
786 "vmap table offset 0x%08x is past end of file 0x%08zx. "
787 "vmap table offset was loaded from offset 0x%08x.\n",
788 vmap_table_offset, oat_file_.Size(),
789 oat_method.GetVmapTableOffsetOffset());
790 success = false;
791 } else if (options_->dump_vmap_) {
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +0100792 DumpVmap(*indent2_os, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700793 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800794 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700795 {
796 *indent1_os << "QuickMethodFrameInfo\n";
797
798 *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
799 *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
800 DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false);
801 *indent2_os << "\n";
802 *indent2_os << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
803 DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true);
804 *indent2_os << "\n";
805 }
806 {
807 // Based on spill masks from QuickMethodFrameInfo so placed
808 // after it is dumped, but useful for understanding quick
809 // code, so dumped here.
810 DumpVregLocations(*indent2_os, oat_method, code_item);
811 }
812 {
813 *indent1_os << "CODE: ";
814 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
815 if (code_size_offset > oat_file_.Size()) {
816 *indent2_os << StringPrintf("WARNING: "
817 "code size offset 0x%08x is past end of file 0x%08zx.",
818 code_size_offset, oat_file_.Size());
819 success = false;
820 } else {
821 const void* code = oat_method.GetQuickCode();
822 uint32_t code_size = oat_method.GetQuickCodeSize();
823 if (code == nullptr) {
824 code = oat_method.GetPortableCode();
825 code_size = oat_method.GetPortableCodeSize();
826 code_size_offset = 0;
827 }
828 uint32_t code_offset = oat_method.GetCodeOffset();
829 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
830 uint64_t aligned_code_end = aligned_code_begin + code_size;
831
832 if (options_->absolute_addresses_) {
833 *indent1_os << StringPrintf("%p ", code);
834 }
835 *indent1_os << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
836 code_offset,
837 code_size_offset,
838 code_size,
839 code != nullptr ? "..." : "");
840
841 if (aligned_code_begin > oat_file_.Size()) {
842 *indent2_os << StringPrintf("WARNING: "
843 "start of code at 0x%08x is past end of file 0x%08zx.",
844 aligned_code_begin, oat_file_.Size());
845 success = false;
846 } else if (aligned_code_end > oat_file_.Size()) {
847 *indent2_os << StringPrintf("WARNING: "
848 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
849 "code size is 0x%08x loaded from offset 0x%08x.\n",
850 aligned_code_end, oat_file_.Size(),
851 code_size, code_size_offset);
852 success = false;
853 if (options_->disassemble_code_) {
854 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
855 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
856 }
857 }
858 } else if (code_size > kMaxCodeSize) {
859 *indent2_os << StringPrintf("WARNING: "
860 "code size %d is bigger than max expected threshold of %d. "
861 "code size is 0x%08x loaded from offset 0x%08x.\n",
862 code_size, kMaxCodeSize,
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 (options_->disassemble_code_) {
871 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, !success, 0);
872 }
873 }
874 }
875 os << std::flush;
876 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700877 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800878
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800879 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
880 if (spill_mask == 0) {
881 return;
882 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800883 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800884 for (size_t i = 0; i < 32; i++) {
885 if ((spill_mask & (1 << i)) != 0) {
886 if (is_float) {
887 os << "fr" << i;
888 } else {
889 os << "r" << i;
890 }
891 spill_mask ^= 1 << i; // clear bit
892 if (spill_mask != 0) {
893 os << ", ";
894 } else {
895 break;
896 }
897 }
898 }
899 os << ")";
900 }
901
Ian Rogersb23a7722012-10-09 16:54:26 -0700902 void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) {
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +0100903 // If the native GC map is null, then this method has been compiled with the
904 // optimizing compiler. The optimizing compiler currently outputs its stack map
905 // in the vmap table, and the code below does not work with such a stack map.
906 if (oat_method.GetNativeGcMap() == nullptr) {
907 return;
908 }
Ian Rogers1809a722013-08-09 22:05:32 -0700909 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700910 if (raw_table != nullptr) {
Ian Rogers1809a722013-08-09 22:05:32 -0700911 const VmapTable vmap_table(raw_table);
912 bool first = true;
913 bool processing_fp = false;
914 uint32_t spill_mask = oat_method.GetCoreSpillMask();
915 for (size_t i = 0; i < vmap_table.Size(); i++) {
916 uint16_t dex_reg = vmap_table[i];
917 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
918 processing_fp ? kFloatVReg : kIntVReg);
919 os << (first ? "v" : ", v") << dex_reg;
920 if (!processing_fp) {
921 os << "/r" << cpu_reg;
922 } else {
923 os << "/fr" << cpu_reg;
924 }
925 first = false;
926 if (!processing_fp && dex_reg == 0xFFFF) {
927 processing_fp = true;
928 spill_mask = oat_method.GetFpSpillMask();
929 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800930 }
Ian Rogers1809a722013-08-09 22:05:32 -0700931 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800932 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800933 }
934
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -0700935 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
936 const DexFile::CodeItem* code_item) {
937 if (code_item != nullptr) {
938 size_t num_locals_ins = code_item->registers_size_;
939 size_t num_ins = code_item->ins_size_;
940 size_t num_locals = num_locals_ins - num_ins;
941 size_t num_outs = code_item->outs_size_;
942
943 os << "vr_stack_locations:";
944 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
945 // For readability, delimit the different kinds of VRs.
946 if (reg == num_locals_ins) {
947 os << "\n\tmethod*:";
948 } else if (reg == num_locals && num_ins > 0) {
949 os << "\n\tins:";
950 } else if (reg == 0 && num_locals > 0) {
951 os << "\n\tlocals:";
952 }
953
954 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
955 oat_method.GetFpSpillMask(),
956 oat_method.GetFrameSizeInBytes(), reg,
957 GetInstructionSet());
958 os << " v" << reg << "[sp + #" << offset << "]";
959 }
960
961 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
962 if (out_reg == 0) {
963 os << "\n\touts:";
964 }
965
966 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
967 os << " v" << out_reg << "[sp + #" << offset << "]";
968 }
969
970 os << "\n";
971 }
972 }
973
Ian Rogersb23a7722012-10-09 16:54:26 -0700974 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800975 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -0700976 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700977 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700978 const VmapTable vmap_table(raw_table);
979 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -0700980 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800981 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
982 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
983 : oat_method.GetCoreSpillMask();
984 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -0700985 } else {
986 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
987 oat_method.GetFpSpillMask(),
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000988 oat_method.GetFrameSizeInBytes(), reg,
989 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -0700990 os << "[sp + #" << offset << "]";
991 }
992 }
993 }
994
Ian Rogersef7d42f2014-01-06 12:55:46 -0800995 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
996 const DexFile::CodeItem* code_item,
997 size_t num_regs, const uint8_t* reg_bitmap) {
998 bool first = true;
999 for (size_t reg = 0; reg < num_regs; reg++) {
1000 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1001 if (first) {
1002 os << " v" << reg << " (";
1003 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1004 os << ")";
1005 first = false;
1006 } else {
1007 os << ", v" << reg << " (";
1008 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1009 os << ")";
1010 }
1011 }
1012 }
1013 if (first) {
1014 os << "No registers in GC map\n";
1015 } else {
1016 os << "\n";
1017 }
1018 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001019 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1020 const DexFile::CodeItem* code_item) {
1021 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001022 if (gc_map_raw == nullptr) {
1023 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001024 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001025 const void* quick_code = oat_method.GetQuickCode();
1026 if (quick_code != nullptr) {
1027 NativePcOffsetToReferenceMap map(gc_map_raw);
1028 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1029 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1030 map.GetNativePcOffset(entry);
1031 os << StringPrintf("%p", native_pc);
1032 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001033 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001034 } else {
1035 const void* portable_code = oat_method.GetPortableCode();
1036 CHECK(portable_code != nullptr);
1037 verifier::DexPcToReferenceMap map(gc_map_raw);
1038 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1039 uint32_t dex_pc = map.GetDexPc(entry);
1040 os << StringPrintf("0x%08x", dex_pc);
1041 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
1042 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001043 }
1044 }
1045
1046 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001047 const void* quick_code = oat_method.GetQuickCode();
1048 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001049 return;
1050 }
Ian Rogers1809a722013-08-09 22:05:32 -07001051 MappingTable table(oat_method.GetMappingTable());
1052 if (table.TotalSize() != 0) {
1053 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1054 std::ostream indent_os(&indent_filter);
1055 if (table.PcToDexSize() != 0) {
1056 typedef MappingTable::PcToDexIterator It;
1057 os << "suspend point mappings {\n";
1058 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1059 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1060 }
1061 os << "}\n";
1062 }
1063 if (table.DexToPcSize() != 0) {
1064 typedef MappingTable::DexToPcIterator It;
1065 os << "catch entry mappings {\n";
1066 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1067 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1068 }
1069 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001070 }
1071 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001072 }
1073
Ian Rogers1809a722013-08-09 22:05:32 -07001074 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1075 size_t offset, bool suspend_point_mapping) {
1076 MappingTable table(oat_method.GetMappingTable());
1077 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1078 typedef MappingTable::PcToDexIterator It;
1079 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1080 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001081 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001082 return cur.DexPc();
1083 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001084 }
Ian Rogers1809a722013-08-09 22:05:32 -07001085 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1086 typedef MappingTable::DexToPcIterator It;
1087 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1088 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001089 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001090 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001091 }
1092 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001093 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001094 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001095 }
1096
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001097 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1098 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001099 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001100 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001101 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001102 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001103 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001104 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001105 bool first = true;
1106 for (size_t reg = 0; reg < num_regs; reg++) {
1107 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1108 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001109 os << "GC map objects: v" << reg << " (";
1110 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001111 os << ")";
1112 first = false;
1113 } else {
1114 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001115 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001116 os << ")";
1117 }
1118 }
1119 }
1120 if (!first) {
1121 os << "\n";
1122 }
1123 }
1124 }
1125 }
1126
Mathieu Chartier590fee92013-09-13 13:46:47 -07001127 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1128 const OatFile::OatMethod& oat_method,
1129 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1130 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001131 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001132 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001133 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1134 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1135 if (kind != kUndefined) {
1136 if (first) {
1137 os << "VRegs: v";
1138 first = false;
1139 } else {
1140 os << ", v";
1141 }
1142 os << reg << " (";
1143 switch (kind) {
1144 case kImpreciseConstant:
1145 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1146 DescribeVReg(os, oat_method, code_item, reg, kind);
1147 break;
1148 case kConstant:
1149 os << "Constant: " << kinds.at((reg * 2) + 1);
1150 break;
1151 default:
1152 DescribeVReg(os, oat_method, code_item, reg, kind);
1153 break;
1154 }
1155 os << ")";
1156 }
1157 }
1158 if (!first) {
1159 os << "\n";
1160 }
1161 }
1162
1163
Ian Rogersb23a7722012-10-09 16:54:26 -07001164 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001165 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001166 size_t i = 0;
1167 while (i < code_item->insns_size_in_code_units_) {
1168 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001169 os << StringPrintf("0x%04zx: %s\n", i, instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001170 i += instruction->SizeInCodeUnits();
1171 }
1172 }
1173 }
1174
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001175 verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx,
1176 const DexFile* dex_file,
1177 const DexFile::ClassDef& class_def,
1178 const DexFile::CodeItem* code_item,
1179 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001180 if ((method_access_flags & kAccNative) == 0) {
1181 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001182 StackHandleScope<1> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001183 Handle<mirror::DexCache> dex_cache(
1184 hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
Andreas Gampe00b25f32014-09-17 21:49:05 -07001185 DCHECK(options_->class_loader_ != nullptr);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001186 return verifier::MethodVerifier::VerifyMethodAndDump(soa.Self(), os, dex_method_idx, dex_file,
1187 dex_cache,
Andreas Gampe00b25f32014-09-17 21:49:05 -07001188 *options_->class_loader_,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001189 &class_def, code_item,
1190 NullHandle<mirror::ArtMethod>(),
1191 method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001192 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001193
1194 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001195 }
1196
Mathieu Chartier590fee92013-09-13 13:46:47 -07001197 void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001198 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1199 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001200 const void* portable_code = oat_method.GetPortableCode();
1201 const void* quick_code = oat_method.GetQuickCode();
1202
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001203 if (code_size == 0) {
1204 code_size = oat_method.GetQuickCodeSize();
1205 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001206 if ((code_size == 0) || ((portable_code == nullptr) && (quick_code == nullptr))) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001207 os << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001208 return;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001209 } else if (quick_code != nullptr) {
1210 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1211 size_t offset = 0;
1212 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001213 if (!bad_input) {
1214 DumpMappingAtOffset(os, oat_method, offset, false);
1215 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001216 offset += disassembler_->Dump(os, quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001217 if (!bad_input) {
1218 uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
1219 if (dex_pc != DexFile::kDexNoIndex) {
1220 DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
1221 if (verifier != nullptr) {
1222 DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
1223 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001224 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001225 }
1226 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001227 } else {
1228 CHECK(portable_code != nullptr);
1229 CHECK_EQ(code_size, 0U); // TODO: disassembly of portable is currently not supported.
Ian Rogersb23a7722012-10-09 16:54:26 -07001230 }
1231 }
1232
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001233 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001234 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
1235 const OatDumperOptions* options_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001236 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001237 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001238};
1239
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001240class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001241 public:
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001242 explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001243 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
1244 : os_(os),
1245 image_space_(image_space),
1246 image_header_(image_header),
1247 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001248
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001249 bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001250 std::ostream& os = *os_;
1251 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001252
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001253 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001254
Mathieu Chartier31e89252013-08-28 11:29:12 -07001255 os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
1256 << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
1257
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001258 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001259
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001260 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001261
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001262 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1263
1264 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1265
1266 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001267
Alex Lighta59dd802014-07-02 16:28:08 -07001268 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1269
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001270 {
1271 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
1272 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1273 std::ostream indent1_os(&indent1_filter);
1274 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
1275 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1276 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1277 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001278 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001279 indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
1280 if (image_root_object->IsObjectArray()) {
1281 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
1282 std::ostream indent2_os(&indent2_filter);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001283 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001284 = image_root_object->AsObjectArray<mirror::Object>();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001285 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001286 mirror::Object* value = image_root_object_array->Get(i);
Ian Rogersfa824272013-11-05 16:12:57 -08001287 size_t run = 0;
1288 for (int32_t j = i + 1; j < image_root_object_array->GetLength(); j++) {
1289 if (value == image_root_object_array->Get(j)) {
1290 run++;
1291 } else {
1292 break;
1293 }
1294 }
1295 if (run == 0) {
1296 indent2_os << StringPrintf("%d: ", i);
1297 } else {
1298 indent2_os << StringPrintf("%d to %zd: ", i, i + run);
1299 i = i + run;
1300 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001301 if (value != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001302 PrettyObjectValue(indent2_os, value->GetClass(), value);
1303 } else {
1304 indent2_os << i << ": null\n";
1305 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001306 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001307 }
1308 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001309 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001310 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001311
Brian Carlstromaded5f72011-10-07 17:15:04 -07001312 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001313 std::string image_filename = image_space_.GetImageFilename();
1314 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001315 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001316 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001317 std::string error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -07001318 const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1319 if (oat_file == nullptr) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001320 oat_file = OatFile::Open(oat_location, oat_location, nullptr, false, &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001321 if (oat_file == nullptr) {
1322 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001323 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001324 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001325 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001326 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001327
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001328 stats_.oat_file_bytes = oat_file->Size();
1329
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001330 oat_dumper_.reset(new OatDumper(*oat_file, oat_dumper_options_.release()));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001331
Mathieu Chartier02e25112013-08-14 16:14:24 -07001332 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001333 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001334 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1335 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001336 }
1337
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001338 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001339
1340 // Loop through all the image spaces and dump their objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001341 gc::Heap* heap = Runtime::Current()->GetHeap();
1342 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07001343 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001344 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001345 {
1346 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1347 heap->FlushAllocStack();
1348 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001349 // Since FlushAllocStack() above resets the (active) allocation
1350 // stack. Need to revoke the thread-local allocation stacks that
1351 // point into it.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001352 {
1353 self->TransitionFromRunnableToSuspended(kNative);
1354 ThreadList* thread_list = Runtime::Current()->GetThreadList();
1355 thread_list->SuspendAll();
1356 heap->RevokeAllThreadLocalAllocationStacks(self);
1357 thread_list->ResumeAll();
1358 self->TransitionFromSuspendedToRunnable();
1359 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001360 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001361 {
1362 std::ostream* saved_os = os_;
1363 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1364 std::ostream indent_os(&indent_filter);
1365 os_ = &indent_os;
1366 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001367 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001368 if (space->IsImageSpace()) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001369 gc::space::ImageSpace* image_space = space->AsImageSpace();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001370 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1371 indent_os << "\n";
1372 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001373 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001374 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001375 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001376 indent_os << "\n";
1377 os_ = saved_os;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001378 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001379 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001380 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001381 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001382 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001383 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001384 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001385 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001386 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001387 size_t header_bytes = sizeof(ImageHeader);
1388 stats_.header_bytes = header_bytes;
1389 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1390 stats_.alignment_bytes += alignment_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001391 stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
1392 stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001393 stats_.Dump(os);
1394 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001395
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001396 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001397
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001398 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001399 }
1400
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001401 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001402 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001403 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001404 CHECK(type != nullptr);
1405 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001406 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001407 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001408 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001409 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001410 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001411 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001412 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001413 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001414 } else if (type->IsArtFieldClass()) {
1415 mirror::ArtField* field = value->AsArtField();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001416 os << StringPrintf("%p Field: %s\n", field, PrettyField(field).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001417 } else if (type->IsArtMethodClass()) {
1418 mirror::ArtMethod* method = value->AsArtMethod();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001419 os << StringPrintf("%p Method: %s\n", method, PrettyMethod(method).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001420 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001421 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001422 }
1423 }
1424
Brian Carlstromea46f952013-07-30 01:26:50 -07001425 static void PrintField(std::ostream& os, mirror::ArtField* field, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001426 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001427 const char* descriptor = field->GetTypeDescriptor();
1428 os << StringPrintf("%s: ", field->GetName());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001429 if (descriptor[0] != 'L' && descriptor[0] != '[') {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001430 StackHandleScope<1> hs(Thread::Current());
1431 FieldHelper fh(hs.NewHandle(field));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001432 mirror::Class* type = fh.GetType();
Fred Shih37f05ef2014-07-16 18:38:08 -07001433 DCHECK(type->IsPrimitive());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001434 if (type->IsPrimitiveLong()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001435 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -07001436 } else if (type->IsPrimitiveDouble()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001437 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -07001438 } else if (type->IsPrimitiveFloat()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001439 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Fred Shih37f05ef2014-07-16 18:38:08 -07001440 } else if (type->IsPrimitiveInt()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001441 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Fred Shih37f05ef2014-07-16 18:38:08 -07001442 } else if (type->IsPrimitiveChar()) {
1443 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
1444 } else if (type->IsPrimitiveShort()) {
1445 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
1446 } else if (type->IsPrimitiveBoolean()) {
1447 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1448 field->GetBoolean(obj));
1449 } else if (type->IsPrimitiveByte()) {
1450 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
1451 } else {
1452 LOG(FATAL) << "Unknown type: " << PrettyClass(type);
Ian Rogers48efc2b2012-08-27 17:20:31 -07001453 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001454 } else {
Ian Rogers48efc2b2012-08-27 17:20:31 -07001455 // Get the value, don't compute the type unless it is non-null as we don't want
1456 // to cause class loading.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001457 mirror::Object* value = field->GetObj(obj);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001458 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001459 os << StringPrintf("null %s\n", PrettyDescriptor(descriptor).c_str());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001460 } else {
Ian Rogers50239c72013-06-17 14:53:22 -07001461 // Grab the field type without causing resolution.
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001462 StackHandleScope<1> hs(Thread::Current());
1463 FieldHelper fh(hs.NewHandle(field));
Ian Rogers50239c72013-06-17 14:53:22 -07001464 mirror::Class* field_type = fh.GetType(false);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001465 if (field_type != nullptr) {
Ian Rogers50239c72013-06-17 14:53:22 -07001466 PrettyObjectValue(os, field_type, value);
1467 } else {
1468 os << StringPrintf("%p %s\n", value, PrettyDescriptor(descriptor).c_str());
1469 }
Ian Rogers48efc2b2012-08-27 17:20:31 -07001470 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001471 }
1472 }
1473
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001474 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001475 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001476 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001477 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001478 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001479 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001480 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001481 if (fields != nullptr) {
Ian Rogersd5b32602012-02-26 16:40:04 -08001482 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001483 mirror::ArtField* field = fields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001484 PrintField(os, field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001485 }
1486 }
1487 }
1488
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001489 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001490 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001491 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001492
Ian Rogersef7d42f2014-01-06 12:55:46 -08001493 const void* GetQuickOatCodeBegin(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001494 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001495 const void* quick_code = m->GetEntryPointFromQuickCompiledCode();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001496 if (quick_code == Runtime::Current()->GetClassLinker()->GetQuickResolutionTrampoline()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001497 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001498 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001499 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001500 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001501 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001502 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001503 }
1504
Ian Rogersef7d42f2014-01-06 12:55:46 -08001505 uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001506 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001507 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1508 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001509 return 0;
1510 }
1511 return oat_code_begin[-1];
1512 }
1513
Ian Rogersef7d42f2014-01-06 12:55:46 -08001514 const void* GetQuickOatCodeEnd(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001515 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001516 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001517 if (oat_code_begin == nullptr) {
1518 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001519 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001520 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001521 }
1522
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001523 static void Callback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001524 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001525 DCHECK(obj != nullptr);
1526 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001527 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001528 if (!state->InDumpSpace(obj)) {
1529 return;
1530 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001531
1532 size_t object_bytes = obj->SizeOf();
1533 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1534 state->stats_.object_bytes += object_bytes;
1535 state->stats_.alignment_bytes += alignment_bytes;
1536
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001537 std::ostream& os = *state->os_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001538 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001539 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001540 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1541 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001542 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001543 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001544 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1545 << klass->GetStatus() << ")\n";
Brian Carlstromea46f952013-07-30 01:26:50 -07001546 } else if (obj->IsArtField()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001547 os << StringPrintf("%p: java.lang.reflect.ArtField %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001548 PrettyField(obj->AsArtField()).c_str());
1549 } else if (obj->IsArtMethod()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001550 os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001551 PrettyMethod(obj->AsArtMethod()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001552 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001553 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001554 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001555 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001556 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001557 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001558 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1559 std::ostream indent_os(&indent_filter);
1560 DumpFields(indent_os, obj, obj_class);
Ian Rogersd5b32602012-02-26 16:40:04 -08001561 if (obj->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001562 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Ian Rogersd5b32602012-02-26 16:40:04 -08001563 int32_t length = obj_array->GetLength();
1564 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001565 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001566 size_t run = 0;
1567 for (int32_t j = i + 1; j < length; j++) {
1568 if (value == obj_array->Get(j)) {
1569 run++;
1570 } else {
1571 break;
1572 }
1573 }
1574 if (run == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001575 indent_os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001576 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001577 indent_os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001578 i = i + run;
1579 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001580 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001581 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001582 PrettyObjectValue(indent_os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001583 }
1584 } else if (obj->IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001585 mirror::ObjectArray<mirror::ArtField>* sfields = obj->AsClass()->GetSFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001586 if (sfields != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001587 indent_os << "STATICS:\n";
1588 Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
1589 std::ostream indent2_os(&indent2_filter);
Ian Rogersd5b32602012-02-26 16:40:04 -08001590 for (int32_t i = 0; i < sfields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001591 mirror::ArtField* field = sfields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001592 PrintField(indent2_os, field, field->GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001593 }
1594 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001595 } else if (obj->IsArtMethod()) {
1596 mirror::ArtMethod* method = obj->AsArtMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001597 if (method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001598 // TODO: portable dumping.
1599 DCHECK(method->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1600 DCHECK(method->GetMappingTable() == nullptr) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001601 bool first_occurrence;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001602 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1603 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1604 state->ComputeOatSize(quick_oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001605 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001606 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001607 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001608 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCode()) {
1609 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001610 }
Ian Rogers19846512012-02-24 11:42:47 -08001611 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
Jeff Hao88474b42013-10-23 16:24:40 -07001612 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001613 method->IsClassInitializer()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001614 DCHECK(method->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1615 DCHECK(method->GetMappingTable() == nullptr) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001616 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001617 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -07001618 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001619 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001620
Elliott Hughesa0e18062012-04-13 15:59:59 -07001621 bool first_occurrence;
Ian Rogers0c7abda2012-09-19 13:33:42 -07001622 size_t gc_map_bytes = state->ComputeOatSize(method->GetNativeGcMap(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001623 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001624 state->stats_.gc_map_bytes += gc_map_bytes;
1625 }
1626
1627 size_t pc_mapping_table_bytes =
Ian Rogers1809a722013-08-09 22:05:32 -07001628 state->ComputeOatSize(method->GetMappingTable(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001629 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001630 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1631 }
1632
1633 size_t vmap_table_bytes =
Ian Rogers1809a722013-08-09 22:05:32 -07001634 state->ComputeOatSize(method->GetVmapTable(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001635 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001636 state->stats_.vmap_table_bytes += vmap_table_bytes;
1637 }
1638
Ian Rogersef7d42f2014-01-06 12:55:46 -08001639 // TODO: portable dumping.
1640 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1641 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1642 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1643 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001644 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001645 state->stats_.managed_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001646 if (method->IsConstructor()) {
1647 if (method->IsStatic()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001648 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001649 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001650 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001651 }
1652 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001653 state->stats_.large_method_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001654 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001655 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001656 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001657
Ian Rogersef7d42f2014-01-06 12:55:46 -08001658 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001659 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
1660 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001661
1662 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersef7d42f2014-01-06 12:55:46 -08001663 vmap_table_bytes + quick_oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001664
1665 double expansion =
Ian Rogersef7d42f2014-01-06 12:55:46 -08001666 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001667 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001668 }
1669 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001670 std::string temp;
1671 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001672 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001673
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001674 std::set<const void*> already_seen_;
1675 // Compute the size of the given data within the oat file and whether this is the first time
1676 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001677 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001678 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001679 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001680 already_seen_.insert(oat_data);
1681 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001682 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001683 }
1684 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001685 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001686
1687 public:
1688 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001689 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001690 size_t file_bytes;
1691
1692 size_t header_bytes;
1693 size_t object_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001694 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001695 size_t alignment_bytes;
1696
1697 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001698 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001699 size_t managed_to_native_code_bytes;
1700 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001701 size_t class_initializer_code_bytes;
1702 size_t large_initializer_code_bytes;
1703 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001704
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001705 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001706 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001707 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001708
1709 size_t dex_instruction_bytes;
1710
Brian Carlstromea46f952013-07-30 01:26:50 -07001711 std::vector<mirror::ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001712 std::vector<size_t> method_outlier_size;
1713 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07001714 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001715
1716 explicit Stats()
1717 : oat_file_bytes(0),
1718 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001719 header_bytes(0),
1720 object_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07001721 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001722 alignment_bytes(0),
1723 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001724 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001725 managed_to_native_code_bytes(0),
1726 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001727 class_initializer_code_bytes(0),
1728 large_initializer_code_bytes(0),
1729 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001730 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001731 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001732 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001733 dex_instruction_bytes(0) {}
1734
Elliott Hughesa0e18062012-04-13 15:59:59 -07001735 struct SizeAndCount {
1736 SizeAndCount(size_t bytes, size_t count) : bytes(bytes), count(count) {}
1737 size_t bytes;
1738 size_t count;
1739 };
1740 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1741 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001742
Ian Rogersfc0e94b2013-09-23 23:51:32 -07001743 void Update(const char* descriptor, size_t object_bytes) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001744 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1745 if (it != sizes_and_counts.end()) {
1746 it->second.bytes += object_bytes;
1747 it->second.count += 1;
1748 } else {
1749 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes, 1));
1750 }
1751 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001752
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001753 double PercentOfOatBytes(size_t size) {
1754 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1755 }
1756
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001757 double PercentOfFileBytes(size_t size) {
1758 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1759 }
1760
1761 double PercentOfObjectBytes(size_t size) {
1762 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1763 }
1764
Brian Carlstromea46f952013-07-30 01:26:50 -07001765 void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001766 method_outlier_size.push_back(total_size);
1767 method_outlier_expansion.push_back(expansion);
1768 method_outlier.push_back(method);
1769 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001770
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001771 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001772 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001773 size_t sum_of_sizes = 0;
1774 size_t sum_of_sizes_squared = 0;
1775 size_t sum_of_expansion = 0;
1776 size_t sum_of_expansion_squared = 0;
1777 size_t n = method_outlier_size.size();
1778 for (size_t i = 0; i < n; i++) {
1779 size_t cur_size = method_outlier_size[i];
1780 sum_of_sizes += cur_size;
1781 sum_of_sizes_squared += cur_size * cur_size;
1782 double cur_expansion = method_outlier_expansion[i];
1783 sum_of_expansion += cur_expansion;
1784 sum_of_expansion_squared += cur_expansion * cur_expansion;
1785 }
1786 size_t size_mean = sum_of_sizes / n;
1787 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1788 double expansion_mean = sum_of_expansion / n;
1789 double expansion_variance =
1790 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1791
1792 // Dump methods whose size is a certain number of standard deviations from the mean
1793 size_t dumped_values = 0;
1794 size_t skipped_values = 0;
1795 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1796 size_t cur_size_variance = i * i * size_variance;
1797 bool first = true;
1798 for (size_t j = 0; j < n; j++) {
1799 size_t cur_size = method_outlier_size[j];
1800 if (cur_size > size_mean) {
1801 size_t cur_var = cur_size - size_mean;
1802 cur_var = cur_var * cur_var;
1803 if (cur_var > cur_size_variance) {
1804 if (dumped_values > 20) {
1805 if (i == 1) {
1806 skipped_values++;
1807 } else {
1808 i = 2; // jump to counting for 1 standard deviation
1809 break;
1810 }
1811 } else {
1812 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001813 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001814 first = false;
1815 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001816 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001817 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001818 method_outlier_size[j] = 0; // don't consider this method again
1819 dumped_values++;
1820 }
1821 }
1822 }
1823 }
1824 }
1825 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001826 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001827 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001828 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001829 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001830
1831 // Dump methods whose expansion is a certain number of standard deviations from the mean
1832 dumped_values = 0;
1833 skipped_values = 0;
1834 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1835 double cur_expansion_variance = i * i * expansion_variance;
1836 bool first = true;
1837 for (size_t j = 0; j < n; j++) {
1838 double cur_expansion = method_outlier_expansion[j];
1839 if (cur_expansion > expansion_mean) {
1840 size_t cur_var = cur_expansion - expansion_mean;
1841 cur_var = cur_var * cur_var;
1842 if (cur_var > cur_expansion_variance) {
1843 if (dumped_values > 20) {
1844 if (i == 1) {
1845 skipped_values++;
1846 } else {
1847 i = 2; // jump to counting for 1 standard deviation
1848 break;
1849 }
1850 } else {
1851 if (first) {
1852 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001853 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001854 first = false;
1855 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001856 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001857 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001858 method_outlier_expansion[j] = 0.0; // don't consider this method again
1859 dumped_values++;
1860 }
1861 }
1862 }
1863 }
1864 }
1865 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001866 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001867 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001868 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001869 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001870 }
1871
Ian Rogersb726dcb2012-09-05 08:57:23 -07001872 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001873 {
1874 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1875 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
1876 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1877 std::ostream indent_os(&indent_filter);
1878 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
1879 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier32327092013-08-30 14:04:08 -07001880 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001881 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
1882 header_bytes, PercentOfFileBytes(header_bytes),
1883 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07001884 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001885 alignment_bytes, PercentOfFileBytes(alignment_bytes))
1886 << std::flush;
Mathieu Chartier32327092013-08-30 14:04:08 -07001887 CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001888 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001889
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001890 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001891 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07001892 for (const auto& sizes_and_count : sizes_and_counts) {
1893 const std::string& descriptor(sizes_and_count.first);
1894 double average = static_cast<double>(sizes_and_count.second.bytes) /
1895 static_cast<double>(sizes_and_count.second.count);
1896 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001897 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001898 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001899 descriptor.c_str(), sizes_and_count.second.bytes,
1900 sizes_and_count.second.count, average, percent);
1901 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001902 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001903 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001904 CHECK_EQ(object_bytes, object_bytes_total);
1905
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001906 os << StringPrintf("oat_file_bytes = %8zd\n"
1907 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1908 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1909 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1910 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1911 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1912 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001913 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001914 managed_code_bytes,
1915 PercentOfOatBytes(managed_code_bytes),
1916 managed_to_native_code_bytes,
1917 PercentOfOatBytes(managed_to_native_code_bytes),
1918 native_to_managed_code_bytes,
1919 PercentOfOatBytes(native_to_managed_code_bytes),
1920 class_initializer_code_bytes,
1921 PercentOfOatBytes(class_initializer_code_bytes),
1922 large_initializer_code_bytes,
1923 PercentOfOatBytes(large_initializer_code_bytes),
1924 large_method_code_bytes,
1925 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001926 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07001927 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001928 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001929 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
1930 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07001931 }
1932
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001933 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1934 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1935 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001936 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1937 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1938 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001939 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001940
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001941 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
1942 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001943 static_cast<double>(managed_code_bytes) /
1944 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07001945 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001946 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001947 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001948
1949 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001950 }
1951 } stats_;
1952
1953 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001954 enum {
1955 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1956 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1957 kLargeConstructorDexBytes = 4000,
1958 // Number of bytes for a method to be considered large. Based on the 4000 basic block
1959 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1960 kLargeMethodDexBytes = 16000
1961 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001962 std::ostream* os_;
Ian Rogers1d54e732013-05-02 21:10:01 -07001963 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001964 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001965 std::unique_ptr<OatDumper> oat_dumper_;
1966 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001967
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001968 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001969};
1970
Andreas Gampe00b25f32014-09-17 21:49:05 -07001971static NoopCompilerCallbacks callbacks;
Elliott Hughes72395bf2012-04-24 13:45:26 -07001972
Andreas Gampe00b25f32014-09-17 21:49:05 -07001973static Runtime* StartRuntime(const char* boot_image_location, const char* image_location,
1974 InstructionSet instruction_set) {
Ian Rogerse63db272014-07-15 15:36:11 -07001975 RuntimeOptions options;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001976 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001977 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001978 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001979 std::string boot_oat_option;
Brian Carlstrom6c871802013-07-17 14:25:35 -07001980
1981 // We are more like a compiler than a run-time. We don't want to execute code.
Brian Carlstromc0a1b182014-03-04 23:19:06 -08001982 options.push_back(std::make_pair("compilercallbacks", &callbacks));
Brian Carlstrom6c871802013-07-17 14:25:35 -07001983
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001984 if (boot_image_location != nullptr) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001985 boot_image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001986 boot_image_option += boot_image_location;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001987 options.push_back(std::make_pair(boot_image_option.c_str(), nullptr));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001988 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001989 if (image_location != nullptr) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001990 image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001991 image_option += image_location;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001992 options.push_back(std::make_pair(image_option.c_str(), nullptr));
Brian Carlstromaded5f72011-10-07 17:15:04 -07001993 }
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001994 options.push_back(
1995 std::make_pair("imageinstructionset",
1996 reinterpret_cast<const void*>(GetInstructionSetString(instruction_set))));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001997
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001998 if (!Runtime::Create(options, false)) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001999 fprintf(stderr, "Failed to create runtime\n");
Andreas Gampe00b25f32014-09-17 21:49:05 -07002000 return nullptr;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002001 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002002
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002003 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
Ian Rogersb9beb2e2014-05-09 16:57:40 -07002004 // give it away now and then switch to a more manageable ScopedObjectAccess.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002005 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002006
2007 return Runtime::Current();
2008}
2009
2010static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
2011 std::ostream* os) {
2012 // Dumping the image, no explicit class loader.
2013 NullHandle<mirror::ClassLoader> null_class_loader;
2014 options->class_loader_ = &null_class_loader;
2015
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002016 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002017 gc::Heap* heap = runtime->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -07002018 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002019 CHECK(image_space != nullptr);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002020 const ImageHeader& image_header = image_space->GetImageHeader();
2021 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002022 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002023 return EXIT_FAILURE;
2024 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002025 ImageDumper image_dumper(os, *image_space, image_header, options);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002026 bool success = image_dumper.Dump();
2027 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002028}
2029
Andreas Gampe00b25f32014-09-17 21:49:05 -07002030static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2031 std::ostream* os) {
2032 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2033
2034 Thread* self = Thread::Current();
2035 CHECK(self != nullptr);
2036 // Need well-known-classes.
2037 WellKnownClasses::Init(self->GetJniEnv());
2038
2039 // Need to register dex files to get a working dex cache.
2040 ScopedObjectAccess soa(self);
2041 ClassLinker* class_linker = runtime->GetClassLinker();
2042 class_linker->RegisterOatFile(oat_file);
2043 std::vector<const DexFile*> dex_files;
2044 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2045 std::string error_msg;
2046 const DexFile* dex_file = odf->OpenDexFile(&error_msg);
2047 CHECK(dex_file != nullptr) << error_msg;
2048 class_linker->RegisterDexFile(*dex_file);
2049 dex_files.push_back(dex_file);
2050 }
2051
2052 // Need a class loader.
2053 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader);
2054 ScopedLocalRef<jobject> class_loader_local(soa.Env(),
2055 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
2056 jobject class_loader = soa.Env()->NewGlobalRef(class_loader_local.get());
2057 // Fake that we're a compiler.
2058 runtime->SetCompileTimeClassPath(class_loader, dex_files);
2059
2060 // Use the class loader while dumping.
2061 StackHandleScope<1> scope(self);
2062 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2063 soa.Decode<mirror::ClassLoader*>(class_loader));
2064 options->class_loader_ = &loader_handle;
2065
2066 OatDumper oat_dumper(*oat_file, options);
2067 bool success = oat_dumper.Dump(*os);
2068 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2069}
2070
2071static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
2072 // No image = no class loader.
2073 NullHandle<mirror::ClassLoader> null_class_loader;
2074 options->class_loader_ = &null_class_loader;
2075
2076 OatDumper oat_dumper(*oat_file, options);
2077 bool success = oat_dumper.Dump(*os);
2078 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2079}
2080
2081static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2082 std::ostream* os) {
2083 std::string error_msg;
2084 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, false, &error_msg);
2085 if (oat_file == nullptr) {
2086 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2087 return EXIT_FAILURE;
2088 }
2089
2090 if (runtime != nullptr) {
2091 return DumpOatWithRuntime(runtime, oat_file, options, os);
2092 } else {
2093 return DumpOatWithoutRuntime(oat_file, options, os);
2094 }
2095}
2096
2097static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2098 std::string error_msg;
2099 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, false, &error_msg);
2100 if (oat_file == nullptr) {
2101 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2102 return EXIT_FAILURE;
2103 }
2104
2105 OatSymbolizer oat_symbolizer(oat_file, output_name);
2106 if (!oat_symbolizer.Init()) {
2107 fprintf(stderr, "Failed to initialize symbolizer\n");
2108 return EXIT_FAILURE;
2109 }
2110 if (!oat_symbolizer.Symbolize()) {
2111 fprintf(stderr, "Failed to symbolize\n");
2112 return EXIT_FAILURE;
2113 }
2114
2115 return EXIT_SUCCESS;
2116}
2117
2118struct OatdumpArgs {
2119 bool Parse(int argc, char** argv) {
2120 // Skip over argv[0].
2121 argv++;
2122 argc--;
2123
2124 if (argc == 0) {
2125 fprintf(stderr, "No arguments specified\n");
2126 usage();
2127 return false;
2128 }
2129
2130 for (int i = 0; i < argc; i++) {
2131 const StringPiece option(argv[i]);
2132 if (option.starts_with("--oat-file=")) {
2133 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2134 } else if (option.starts_with("--image=")) {
2135 image_location_ = option.substr(strlen("--image=")).data();
2136 } else if (option.starts_with("--boot-image=")) {
2137 boot_image_location_ = option.substr(strlen("--boot-image=")).data();
2138 } else if (option.starts_with("--instruction-set=")) {
2139 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
2140 instruction_set_ = GetInstructionSetFromString(instruction_set_str.data());
2141 if (instruction_set_ == kNone) {
2142 fprintf(stderr, "Unsupported instruction set %s\n", instruction_set_str.data());
2143 usage();
2144 return false;
2145 }
2146 } else if (option =="--dump:raw_mapping_table") {
2147 dump_raw_mapping_table_ = true;
2148 } else if (option == "--dump:raw_gc_map") {
2149 dump_raw_gc_map_ = true;
2150 } else if (option == "--no-dump:vmap") {
2151 dump_vmap_ = false;
2152 } else if (option == "--no-disassemble") {
2153 disassemble_code_ = false;
2154 } else if (option.starts_with("--output=")) {
2155 output_name_ = option.substr(strlen("--output=")).ToString();
2156 const char* filename = output_name_.c_str();
2157 out_.reset(new std::ofstream(filename));
2158 if (!out_->good()) {
2159 fprintf(stderr, "Failed to open output filename %s\n", filename);
2160 usage();
2161 return false;
2162 }
2163 os_ = out_.get();
2164 } else if (option.starts_with("--symbolize=")) {
2165 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2166 symbolize_ = true;
2167 } else {
2168 fprintf(stderr, "Unknown argument %s\n", option.data());
2169 usage();
2170 return false;
2171 }
2172 }
2173
2174 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2175 fprintf(stderr, "Either --image or --oat must be specified\n");
2176 return false;
2177 }
2178
2179 if (image_location_ != nullptr && oat_filename_ != nullptr) {
2180 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
2181 return false;
2182 }
2183
2184 return true;
2185 }
2186
2187 const char* oat_filename_ = nullptr;
2188 const char* image_location_ = nullptr;
2189 const char* boot_image_location_ = nullptr;
2190 InstructionSet instruction_set_ = kRuntimeISA;
2191 std::string elf_filename_prefix_;
2192 std::ostream* os_ = &std::cout;
2193 std::unique_ptr<std::ofstream> out_;
2194 std::string output_name_;
2195 bool dump_raw_mapping_table_ = false;
2196 bool dump_raw_gc_map_ = false;
2197 bool dump_vmap_ = true;
2198 bool disassemble_code_ = true;
2199 bool symbolize_ = false;
2200};
2201
2202static int oatdump(int argc, char** argv) {
2203 InitLogging(argv);
2204
2205 OatdumpArgs args;
2206 if (!args.Parse(argc, argv)) {
2207 return EXIT_FAILURE;
2208 }
2209
2210 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2211 bool absolute_addresses = (args.oat_filename_ == nullptr);
2212
2213 std::unique_ptr<OatDumperOptions> oat_dumper_options(new OatDumperOptions(
2214 args.dump_raw_mapping_table_,
2215 args.dump_raw_gc_map_,
2216 args.dump_vmap_,
2217 args.disassemble_code_,
2218 absolute_addresses,
2219 nullptr));
2220
2221 std::unique_ptr<Runtime> runtime;
2222 if ((args.boot_image_location_ != nullptr || args.image_location_ != nullptr) &&
2223 !args.symbolize_) {
2224 // If we have a boot image option, try to start the runtime; except when just symbolizing.
2225 runtime.reset(StartRuntime(args.boot_image_location_,
2226 args.image_location_,
2227 args.instruction_set_));
Mathieu Chartierd424d082014-10-15 10:31:46 -07002228 } else {
2229 MemMap::Init();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002230 }
2231
2232 if (args.oat_filename_ != nullptr) {
2233 if (args.symbolize_) {
2234 return SymbolizeOat(args.oat_filename_, args.output_name_);
2235 } else {
2236 return DumpOat(runtime.get(), args.oat_filename_, oat_dumper_options.release(), args.os_);
2237 }
2238 }
2239
2240 if (runtime.get() == nullptr) {
2241 // We need the runtime when printing an image.
2242 return EXIT_FAILURE;
2243 }
2244
2245 return DumpImage(runtime.get(), args.image_location_, oat_dumper_options.release(), args.os_);
2246}
2247
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002248} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002249
2250int main(int argc, char** argv) {
2251 return art::oatdump(argc, argv);
2252}