blob: 5a19d536f808b5a1659409ad0a570c6d1a049a0f [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
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700123class OatSymbolizer : public CodeOutput {
124 public:
125 explicit OatSymbolizer(const OatFile* oat_file, std::string& output_name) :
126 oat_file_(oat_file), builder_(nullptr), elf_output_(nullptr), output_name_(output_name) {}
127
128 bool Init() {
129 Elf32_Word oat_data_size = oat_file_->GetOatHeader().GetExecutableOffset();
130
131 uint32_t diff = static_cast<uint32_t>(oat_file_->End() - oat_file_->Begin());
132 uint32_t oat_exec_size = diff - oat_data_size;
133
134 if (output_name_.empty()) {
135 output_name_ = "symbolized.oat";
136 }
137 elf_output_ = OS::CreateEmptyFile(output_name_.c_str());
138
139 builder_.reset(new ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
140 Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(
141 this,
142 elf_output_,
143 oat_file_->GetOatHeader().GetInstructionSet(),
144 0,
145 oat_data_size,
146 oat_data_size,
147 oat_exec_size,
148 true,
149 false));
150
151 if (!builder_->Init()) {
152 builder_.reset(nullptr);
153 return false;
154 }
155
156 return true;
157 }
158
159 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
160 uint32_t,
161 const OatFile::OatMethod&,
162 const DexFile&,
163 uint32_t,
164 const DexFile::CodeItem*,
165 uint32_t);
166
167 bool Symbolize() {
168 if (builder_.get() == nullptr) {
169 return false;
170 }
171
172 Walk(&art::OatSymbolizer::RegisterForDedup);
173
174 NormalizeState();
175
176 Walk(&art::OatSymbolizer::AddSymbol);
177
178 bool result = builder_->Write();
179
180 elf_output_->Flush();
181 elf_output_->Close();
182
183 return result;
184 }
185
186 void Walk(Callback callback) {
187 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
188 for (size_t i = 0; i < oat_dex_files.size(); i++) {
189 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
190 CHECK(oat_dex_file != NULL);
191 WalkOatDexFile(oat_dex_file, callback);
192 }
193 }
194
195 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
196 std::string error_msg;
197 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
198 if (dex_file.get() == nullptr) {
199 return;
200 }
201 for (size_t class_def_index = 0;
202 class_def_index < dex_file->NumClassDefs();
203 class_def_index++) {
204 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
205 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
206 OatClassType type = oat_class.GetType();
207 switch (type) {
208 case kOatClassAllCompiled:
209 case kOatClassSomeCompiled:
210 WalkOatClass(oat_class, *dex_file.get(), class_def, callback);
211 break;
212
213 case kOatClassNoneCompiled:
214 case kOatClassMax:
215 // Ignore.
216 break;
217 }
218 }
219 }
220
221 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
222 const DexFile::ClassDef& class_def, Callback callback) {
223 const byte* class_data = dex_file.GetClassData(class_def);
224 if (class_data == nullptr) { // empty class such as a marker interface?
225 return;
226 }
227 // Note: even if this is an interface or a native class, we still have to walk it, as there
228 // might be a static initializer.
229 ClassDataItemIterator it(dex_file, class_data);
230 SkipAllFields(&it);
231 uint32_t class_method_idx = 0;
232 while (it.HasNextDirectMethod()) {
233 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
234 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700235 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700236 class_method_idx++;
237 it.Next();
238 }
239 while (it.HasNextVirtualMethod()) {
240 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
241 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700242 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700243 class_method_idx++;
244 it.Next();
245 }
246 DCHECK(!it.HasNext());
247 }
248
249 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
250 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
251 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
252 uint32_t method_access_flags, Callback callback) {
253 if ((method_access_flags & kAccAbstract) != 0) {
254 // Abstract method, no code.
255 return;
256 }
257 if (oat_method.GetCodeOffset() == 0) {
258 // No code.
259 return;
260 }
261
262 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
263 method_access_flags);
264 }
265
266 void RegisterForDedup(const DexFile::ClassDef& class_def, uint32_t class_method_index,
267 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
268 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
269 uint32_t method_access_flags) {
270 state_[oat_method.GetCodeOffset()]++;
271 }
272
273 void NormalizeState() {
274 for (auto& x : state_) {
275 if (x.second == 1) {
276 state_[x.first] = 0;
277 }
278 }
279 }
280
281 enum class DedupState { // private
282 kNotDeduplicated,
283 kDeduplicatedFirst,
284 kDeduplicatedOther
285 };
286 DedupState IsDuplicated(uint32_t offset) {
287 if (state_[offset] == 0) {
288 return DedupState::kNotDeduplicated;
289 }
290 if (state_[offset] == 1) {
291 return DedupState::kDeduplicatedOther;
292 }
293 state_[offset] = 1;
294 return DedupState::kDeduplicatedFirst;
295 }
296
297 void AddSymbol(const DexFile::ClassDef& class_def, uint32_t class_method_index,
298 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
299 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
300 uint32_t method_access_flags) {
301 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
302 if (dedup != DedupState::kDeduplicatedOther) {
303 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
304
305 if (dedup == DedupState::kDeduplicatedFirst) {
306 pretty_name = "[Dedup]" + pretty_name;
307 }
308
309 ElfSymtabBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr,
310 Elf32_Sym, Elf32_Shdr>* symtab = &builder_->symtab_builder_;
311
312 symtab->AddSymbol(pretty_name, &builder_->text_builder_, oat_method.GetCodeOffset() -
313 oat_file_->GetOatHeader().GetExecutableOffset(), true,
314 oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
315 }
316 }
317
318 // Write oat code. Required by ElfBuilder/CodeOutput.
319 bool Write(OutputStream* out) {
320 return out->WriteFully(oat_file_->Begin(), oat_file_->End() - oat_file_->Begin());
321 }
322
323 private:
324 static void SkipAllFields(ClassDataItemIterator* it) {
325 while (it->HasNextStaticField()) {
326 it->Next();
327 }
328 while (it->HasNextInstanceField()) {
329 it->Next();
330 }
331 }
332
333 const OatFile* oat_file_;
334 std::unique_ptr<ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
335 Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr> > builder_;
336 File* elf_output_;
337 std::unordered_map<uint32_t, uint32_t> state_;
338 std::string output_name_;
339};
340
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700341class OatDumperOptions {
342 public:
343 OatDumperOptions(bool dump_raw_mapping_table,
344 bool dump_raw_gc_map,
345 bool dump_vmap,
346 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700347 bool absolute_addresses,
348 Handle<mirror::ClassLoader>* class_loader)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700349 : dump_raw_mapping_table_(dump_raw_mapping_table),
350 dump_raw_gc_map_(dump_raw_gc_map),
351 dump_vmap_(dump_vmap),
352 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700353 absolute_addresses_(absolute_addresses),
354 class_loader_(class_loader) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700355
356 const bool dump_raw_mapping_table_;
357 const bool dump_raw_gc_map_;
358 const bool dump_vmap_;
359 const bool disassemble_code_;
360 const bool absolute_addresses_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700361 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700362};
363
Elliott Hughese3c845c2012-02-28 17:23:01 -0800364class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700365 public:
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700366 explicit OatDumper(const OatFile& oat_file, OatDumperOptions* options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000367 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800368 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700369 options_(options),
370 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet(),
371 new DisassemblerOptions(options_->absolute_addresses_,
372 oat_file.Begin()))) {
Andreas Gampe00b25f32014-09-17 21:49:05 -0700373 CHECK(options_->class_loader_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800374 AddAllOffsets();
375 }
376
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700377 ~OatDumper() {
378 delete options_;
379 delete disassembler_;
380 }
381
382 bool Dump(std::ostream& os) {
383 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800384 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700385
386 os << "MAGIC:\n";
387 os << oat_header.GetMagic() << "\n\n";
388
389 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800390 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700391
Elliott Hughesa72ec822012-03-05 17:12:22 -0800392 os << "INSTRUCTION SET:\n";
393 os << oat_header.GetInstructionSet() << "\n\n";
394
Dave Allison70202782013-10-22 17:52:19 -0700395 os << "INSTRUCTION SET FEATURES:\n";
396 os << oat_header.GetInstructionSetFeatures().GetFeatureString() << "\n\n";
397
Brian Carlstromaded5f72011-10-07 17:15:04 -0700398 os << "DEX FILE COUNT:\n";
399 os << oat_header.GetDexFileCount() << "\n\n";
400
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800401#define DUMP_OAT_HEADER_OFFSET(label, offset) \
402 os << label " OFFSET:\n"; \
403 os << StringPrintf("0x%08x", oat_header.offset()); \
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700404 if (oat_header.offset() != 0 && options_->absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800405 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
406 } \
407 os << StringPrintf("\n\n");
408
409 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
410 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
411 GetInterpreterToInterpreterBridgeOffset);
412 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
413 GetInterpreterToCompiledCodeBridgeOffset);
414 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
415 GetJniDlsymLookupOffset);
416 DUMP_OAT_HEADER_OFFSET("PORTABLE IMT CONFLICT TRAMPOLINE",
417 GetPortableImtConflictTrampolineOffset);
418 DUMP_OAT_HEADER_OFFSET("PORTABLE RESOLUTION TRAMPOLINE",
419 GetPortableResolutionTrampolineOffset);
420 DUMP_OAT_HEADER_OFFSET("PORTABLE TO INTERPRETER BRIDGE",
421 GetPortableToInterpreterBridgeOffset);
422 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
423 GetQuickGenericJniTrampolineOffset);
424 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
425 GetQuickImtConflictTrampolineOffset);
426 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
427 GetQuickResolutionTrampolineOffset);
428 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
429 GetQuickToInterpreterBridgeOffset);
430#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700431
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700432 os << "IMAGE PATCH DELTA:\n";
433 os << StringPrintf("%d (0x%08x)\n\n",
434 oat_header.GetImagePatchDelta(),
435 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700436
Brian Carlstrom28db0122012-10-18 16:20:41 -0700437 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
438 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
439
440 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800441 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700442
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700443 // Print the key-value store.
444 {
445 os << "KEY VALUE STORE:\n";
446 size_t index = 0;
447 const char* key;
448 const char* value;
449 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
450 os << key << " = " << value << "\n";
451 index++;
452 }
453 os << "\n";
454 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700455
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700456 if (options_->absolute_addresses_) {
457 os << "BEGIN:\n";
458 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700459
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700460 os << "END:\n";
461 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
462 }
463
464 os << "SIZE:\n";
465 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700466
467 os << std::flush;
468
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800469 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
470 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700471 CHECK(oat_dex_file != nullptr);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700472 if (!DumpOatDexFile(os, *oat_dex_file)) {
473 success = false;
474 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700475 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700476 os << std::flush;
477 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700478 }
479
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800480 size_t ComputeSize(const void* oat_data) {
481 if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() ||
482 reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) {
483 return 0; // Address not in oat file
484 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800485 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
486 reinterpret_cast<uintptr_t>(oat_file_.Begin());
487 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800488 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800489 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800490 return end_offset - begin_offset;
491 }
492
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700493 InstructionSet GetInstructionSet() {
494 return oat_file_.GetOatHeader().GetInstructionSet();
495 }
496
Ian Rogersef7d42f2014-01-06 12:55:46 -0800497 const void* GetQuickOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800498 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
499 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700500 CHECK(oat_dex_file != nullptr);
501 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700502 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700503 if (dex_file.get() == nullptr) {
504 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
505 << "': " << error_msg;
506 } else {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700507 const DexFile::ClassDef* class_def =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700508 dex_file->FindClassDef(m->GetDeclaringClassDescriptor());
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700509 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700510 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100511 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800512 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100513 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800514 }
515 }
516 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700517 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800518 }
519
Brian Carlstromaded5f72011-10-07 17:15:04 -0700520 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800521 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800522 // We don't know the length of the code for each method, but we need to know where to stop
523 // when disassembling. What we do know is that a region of code will be followed by some other
524 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
525 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800526 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
527 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700528 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700529 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700530 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700531 if (dex_file.get() == nullptr) {
532 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
533 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800534 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800535 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800536 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800537 for (size_t class_def_index = 0;
538 class_def_index < dex_file->NumClassDefs();
539 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800540 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100541 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800542 const byte* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700543 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800544 ClassDataItemIterator it(*dex_file, class_data);
545 SkipAllFields(it);
546 uint32_t class_method_index = 0;
547 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100548 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800549 it.Next();
550 }
551 while (it.HasNextVirtualMethod()) {
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 }
556 }
557 }
558
559 // If the last thing in the file is code for a method, there won't be an offset for the "next"
560 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
561 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800562 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800563 }
564
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700565 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
566 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
567 }
568
Elliott Hughese3c845c2012-02-28 17:23:01 -0800569 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800570 uint32_t code_offset = oat_method.GetCodeOffset();
571 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
572 code_offset &= ~0x1;
573 }
574 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800575 offsets_.insert(oat_method.GetMappingTableOffset());
576 offsets_.insert(oat_method.GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700577 offsets_.insert(oat_method.GetNativeGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800578 }
579
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700580 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
581 bool success = true;
582 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800583 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800584 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700585
586 // Create the verifier early.
587
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700588 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700589 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700590 if (dex_file.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700591 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700592 os << std::flush;
593 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700594 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800595 for (size_t class_def_index = 0;
596 class_def_index < dex_file->NumClassDefs();
597 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700598 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
599 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700600 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100601 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700602 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
603 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100604 << " (" << oat_class.GetStatus() << ")"
605 << " (" << oat_class.GetType() << ")\n";
606 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800607 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
608 std::ostream indented_os(&indent_filter);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700609 if (!DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def)) {
610 success = false;
611 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700612 }
613
614 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700615 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700616 }
617
Elliott Hughese3c845c2012-02-28 17:23:01 -0800618 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700619 while (it.HasNextStaticField()) {
620 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700621 }
Ian Rogers0571d352011-11-03 19:51:38 -0700622 while (it.HasNextInstanceField()) {
623 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700624 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800625 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700626
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700627 bool DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800628 const DexFile::ClassDef& class_def) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700629 bool success = true;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800630 const byte* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700631 if (class_data == nullptr) { // empty class such as a marker interface?
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700632 os << std::flush;
633 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800634 }
635 ClassDataItemIterator it(dex_file, class_data);
636 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700637 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700638 while (it.HasNextDirectMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700639 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
640 it.GetMemberIndex(), it.GetMethodCodeItem(),
641 it.GetRawMemberAccessFlags())) {
642 success = false;
643 }
644 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700645 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700646 }
Ian Rogers0571d352011-11-03 19:51:38 -0700647 while (it.HasNextVirtualMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700648 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
649 it.GetMemberIndex(), it.GetMethodCodeItem(),
650 it.GetRawMemberAccessFlags())) {
651 success = false;
652 }
653 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700654 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700655 }
Ian Rogers0571d352011-11-03 19:51:38 -0700656 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700657 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700658 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700659 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800660
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700661 static constexpr uint32_t kPrologueBytes = 16;
662
663 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
664 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
665
666 bool DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700667 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700668 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800669 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
670 uint32_t method_access_flags) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700671 bool success = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800672 os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700673 class_method_index, PrettyMethod(dex_method_idx, dex_file, true).c_str(),
674 dex_method_idx);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800675 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700676 std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter));
677 Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count);
678 std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter));
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800679 {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700680 *indent1_os << "DEX CODE:\n";
681 DumpDexCode(*indent2_os, dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700682 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700683
684 std::unique_ptr<verifier::MethodVerifier> verifier;
685 if (Runtime::Current() != nullptr) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700686 *indent1_os << "VERIFIER TYPE ANALYSIS:\n";
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700687 verifier.reset(DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item,
688 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700689 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700690
691 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
692 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
693 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800694 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700695 *indent1_os << "OatMethodOffsets ";
696 if (options_->absolute_addresses_) {
697 *indent1_os << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100698 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700699 *indent1_os << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
700 if (oat_method_offsets_offset > oat_file_.Size()) {
701 *indent1_os << StringPrintf(
702 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
703 oat_method_offsets_offset, oat_file_.Size());
704 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
705 os << std::flush;
706 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800707 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700708
709 uint32_t code_offset = oat_method.GetCodeOffset();
710 *indent2_os << StringPrintf("code_offset: 0x%08x ", code_offset);
711 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
712 if (aligned_code_begin > oat_file_.Size()) {
713 *indent2_os << StringPrintf("WARNING: "
714 "code offset 0x%08x is past end of file 0x%08zx.\n",
715 aligned_code_begin, oat_file_.Size());
716 success = false;
717 }
718 *indent2_os << "\n";
719
720 *indent2_os << "gc_map: ";
721 if (options_->absolute_addresses_) {
722 *indent2_os << StringPrintf("%p ", oat_method.GetNativeGcMap());
723 }
724 uint32_t gc_map_offset = oat_method.GetNativeGcMapOffset();
725 *indent2_os << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
726 if (gc_map_offset > oat_file_.Size()) {
727 *indent2_os << StringPrintf("WARNING: "
728 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
729 gc_map_offset, oat_file_.Size());
730 success = false;
731 } else if (options_->dump_raw_gc_map_) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700732 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800733 std::ostream indent3_os(&indent3_filter);
734 DumpGcMap(indent3_os, oat_method, code_item);
735 }
736 }
737 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700738 *indent1_os << "OatQuickMethodHeader ";
739 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
740 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700741
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700742 if (options_->absolute_addresses_) {
743 *indent1_os << StringPrintf("%p ", method_header);
744 }
745 *indent1_os << StringPrintf("(offset=0x%08x)\n", method_header_offset);
746 if (method_header_offset > oat_file_.Size()) {
747 *indent1_os << StringPrintf(
748 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
749 method_header_offset, oat_file_.Size());
750 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
751 os << std::flush;
752 return false;
753 }
754
755 *indent2_os << "mapping_table: ";
756 if (options_->absolute_addresses_) {
757 *indent2_os << StringPrintf("%p ", oat_method.GetMappingTable());
758 }
759 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
760 *indent2_os << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
761 if (mapping_table_offset > oat_file_.Size()) {
762 *indent2_os << StringPrintf("WARNING: "
763 "mapping table offset 0x%08x is past end of file 0x%08zx. "
764 "mapping table offset was loaded from offset 0x%08x.\n",
765 mapping_table_offset, oat_file_.Size(),
766 oat_method.GetMappingTableOffsetOffset());
767 success = false;
768 } else if (options_->dump_raw_mapping_table_) {
769 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
770 std::ostream indent3_os(&indent3_filter);
771 DumpMappingTable(indent3_os, oat_method);
772 }
773
774 *indent2_os << "vmap_table: ";
775 if (options_->absolute_addresses_) {
776 *indent2_os << StringPrintf("%p ", oat_method.GetVmapTable());
777 }
778 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
779 *indent2_os << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
780 if (vmap_table_offset > oat_file_.Size()) {
781 *indent2_os << StringPrintf("WARNING: "
782 "vmap table offset 0x%08x is past end of file 0x%08zx. "
783 "vmap table offset was loaded from offset 0x%08x.\n",
784 vmap_table_offset, oat_file_.Size(),
785 oat_method.GetVmapTableOffsetOffset());
786 success = false;
787 } else if (options_->dump_vmap_) {
788 if (oat_method.GetNativeGcMap() != nullptr) {
789 // The native GC map is null for methods compiled with the optimizing compiler.
790 DumpVmap(*indent2_os, oat_method);
791 }
792 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800793 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700794 {
795 *indent1_os << "QuickMethodFrameInfo\n";
796
797 *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
798 *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
799 DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false);
800 *indent2_os << "\n";
801 *indent2_os << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
802 DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true);
803 *indent2_os << "\n";
804 }
805 {
806 // Based on spill masks from QuickMethodFrameInfo so placed
807 // after it is dumped, but useful for understanding quick
808 // code, so dumped here.
809 DumpVregLocations(*indent2_os, oat_method, code_item);
810 }
811 {
812 *indent1_os << "CODE: ";
813 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
814 if (code_size_offset > oat_file_.Size()) {
815 *indent2_os << StringPrintf("WARNING: "
816 "code size offset 0x%08x is past end of file 0x%08zx.",
817 code_size_offset, oat_file_.Size());
818 success = false;
819 } else {
820 const void* code = oat_method.GetQuickCode();
821 uint32_t code_size = oat_method.GetQuickCodeSize();
822 if (code == nullptr) {
823 code = oat_method.GetPortableCode();
824 code_size = oat_method.GetPortableCodeSize();
825 code_size_offset = 0;
826 }
827 uint32_t code_offset = oat_method.GetCodeOffset();
828 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
829 uint64_t aligned_code_end = aligned_code_begin + code_size;
830
831 if (options_->absolute_addresses_) {
832 *indent1_os << StringPrintf("%p ", code);
833 }
834 *indent1_os << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
835 code_offset,
836 code_size_offset,
837 code_size,
838 code != nullptr ? "..." : "");
839
840 if (aligned_code_begin > oat_file_.Size()) {
841 *indent2_os << StringPrintf("WARNING: "
842 "start of code at 0x%08x is past end of file 0x%08zx.",
843 aligned_code_begin, oat_file_.Size());
844 success = false;
845 } else if (aligned_code_end > oat_file_.Size()) {
846 *indent2_os << StringPrintf("WARNING: "
847 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
848 "code size is 0x%08x loaded from offset 0x%08x.\n",
849 aligned_code_end, oat_file_.Size(),
850 code_size, code_size_offset);
851 success = false;
852 if (options_->disassemble_code_) {
853 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
854 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
855 }
856 }
857 } else if (code_size > kMaxCodeSize) {
858 *indent2_os << StringPrintf("WARNING: "
859 "code size %d is bigger than max expected threshold of %d. "
860 "code size is 0x%08x loaded from offset 0x%08x.\n",
861 code_size, kMaxCodeSize,
862 code_size, code_size_offset);
863 success = false;
864 if (options_->disassemble_code_) {
865 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
866 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
867 }
868 }
869 } else if (options_->disassemble_code_) {
870 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, !success, 0);
871 }
872 }
873 }
874 os << std::flush;
875 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700876 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800877
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800878 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
879 if (spill_mask == 0) {
880 return;
881 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800882 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800883 for (size_t i = 0; i < 32; i++) {
884 if ((spill_mask & (1 << i)) != 0) {
885 if (is_float) {
886 os << "fr" << i;
887 } else {
888 os << "r" << i;
889 }
890 spill_mask ^= 1 << i; // clear bit
891 if (spill_mask != 0) {
892 os << ", ";
893 } else {
894 break;
895 }
896 }
897 }
898 os << ")";
899 }
900
Ian Rogersb23a7722012-10-09 16:54:26 -0700901 void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogers1809a722013-08-09 22:05:32 -0700902 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700903 if (raw_table != nullptr) {
Ian Rogers1809a722013-08-09 22:05:32 -0700904 const VmapTable vmap_table(raw_table);
905 bool first = true;
906 bool processing_fp = false;
907 uint32_t spill_mask = oat_method.GetCoreSpillMask();
908 for (size_t i = 0; i < vmap_table.Size(); i++) {
909 uint16_t dex_reg = vmap_table[i];
910 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
911 processing_fp ? kFloatVReg : kIntVReg);
912 os << (first ? "v" : ", v") << dex_reg;
913 if (!processing_fp) {
914 os << "/r" << cpu_reg;
915 } else {
916 os << "/fr" << cpu_reg;
917 }
918 first = false;
919 if (!processing_fp && dex_reg == 0xFFFF) {
920 processing_fp = true;
921 spill_mask = oat_method.GetFpSpillMask();
922 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800923 }
Ian Rogers1809a722013-08-09 22:05:32 -0700924 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800925 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800926 }
927
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -0700928 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
929 const DexFile::CodeItem* code_item) {
930 if (code_item != nullptr) {
931 size_t num_locals_ins = code_item->registers_size_;
932 size_t num_ins = code_item->ins_size_;
933 size_t num_locals = num_locals_ins - num_ins;
934 size_t num_outs = code_item->outs_size_;
935
936 os << "vr_stack_locations:";
937 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
938 // For readability, delimit the different kinds of VRs.
939 if (reg == num_locals_ins) {
940 os << "\n\tmethod*:";
941 } else if (reg == num_locals && num_ins > 0) {
942 os << "\n\tins:";
943 } else if (reg == 0 && num_locals > 0) {
944 os << "\n\tlocals:";
945 }
946
947 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
948 oat_method.GetFpSpillMask(),
949 oat_method.GetFrameSizeInBytes(), reg,
950 GetInstructionSet());
951 os << " v" << reg << "[sp + #" << offset << "]";
952 }
953
954 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
955 if (out_reg == 0) {
956 os << "\n\touts:";
957 }
958
959 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
960 os << " v" << out_reg << "[sp + #" << offset << "]";
961 }
962
963 os << "\n";
964 }
965 }
966
Ian Rogersb23a7722012-10-09 16:54:26 -0700967 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800968 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -0700969 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700970 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700971 const VmapTable vmap_table(raw_table);
972 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -0700973 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800974 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
975 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
976 : oat_method.GetCoreSpillMask();
977 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -0700978 } else {
979 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
980 oat_method.GetFpSpillMask(),
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000981 oat_method.GetFrameSizeInBytes(), reg,
982 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -0700983 os << "[sp + #" << offset << "]";
984 }
985 }
986 }
987
Ian Rogersef7d42f2014-01-06 12:55:46 -0800988 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
989 const DexFile::CodeItem* code_item,
990 size_t num_regs, const uint8_t* reg_bitmap) {
991 bool first = true;
992 for (size_t reg = 0; reg < num_regs; reg++) {
993 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
994 if (first) {
995 os << " v" << reg << " (";
996 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
997 os << ")";
998 first = false;
999 } else {
1000 os << ", v" << reg << " (";
1001 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1002 os << ")";
1003 }
1004 }
1005 }
1006 if (first) {
1007 os << "No registers in GC map\n";
1008 } else {
1009 os << "\n";
1010 }
1011 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001012 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1013 const DexFile::CodeItem* code_item) {
1014 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001015 if (gc_map_raw == nullptr) {
1016 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001017 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001018 const void* quick_code = oat_method.GetQuickCode();
1019 if (quick_code != nullptr) {
1020 NativePcOffsetToReferenceMap map(gc_map_raw);
1021 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1022 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1023 map.GetNativePcOffset(entry);
1024 os << StringPrintf("%p", native_pc);
1025 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001026 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001027 } else {
1028 const void* portable_code = oat_method.GetPortableCode();
1029 CHECK(portable_code != nullptr);
1030 verifier::DexPcToReferenceMap map(gc_map_raw);
1031 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1032 uint32_t dex_pc = map.GetDexPc(entry);
1033 os << StringPrintf("0x%08x", dex_pc);
1034 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
1035 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001036 }
1037 }
1038
1039 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001040 const void* quick_code = oat_method.GetQuickCode();
1041 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001042 return;
1043 }
Ian Rogers1809a722013-08-09 22:05:32 -07001044 MappingTable table(oat_method.GetMappingTable());
1045 if (table.TotalSize() != 0) {
1046 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1047 std::ostream indent_os(&indent_filter);
1048 if (table.PcToDexSize() != 0) {
1049 typedef MappingTable::PcToDexIterator It;
1050 os << "suspend point mappings {\n";
1051 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1052 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1053 }
1054 os << "}\n";
1055 }
1056 if (table.DexToPcSize() != 0) {
1057 typedef MappingTable::DexToPcIterator It;
1058 os << "catch entry mappings {\n";
1059 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1060 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1061 }
1062 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001063 }
1064 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001065 }
1066
Ian Rogers1809a722013-08-09 22:05:32 -07001067 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1068 size_t offset, bool suspend_point_mapping) {
1069 MappingTable table(oat_method.GetMappingTable());
1070 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1071 typedef MappingTable::PcToDexIterator It;
1072 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1073 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001074 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001075 return cur.DexPc();
1076 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001077 }
Ian Rogers1809a722013-08-09 22:05:32 -07001078 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1079 typedef MappingTable::DexToPcIterator It;
1080 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1081 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001082 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001083 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001084 }
1085 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001086 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001087 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001088 }
1089
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001090 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1091 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001092 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001093 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001094 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001095 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001096 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001097 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001098 bool first = true;
1099 for (size_t reg = 0; reg < num_regs; reg++) {
1100 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1101 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001102 os << "GC map objects: v" << reg << " (";
1103 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001104 os << ")";
1105 first = false;
1106 } else {
1107 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001108 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001109 os << ")";
1110 }
1111 }
1112 }
1113 if (!first) {
1114 os << "\n";
1115 }
1116 }
1117 }
1118 }
1119
Mathieu Chartier590fee92013-09-13 13:46:47 -07001120 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1121 const OatFile::OatMethod& oat_method,
1122 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1123 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001124 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001125 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001126 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1127 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1128 if (kind != kUndefined) {
1129 if (first) {
1130 os << "VRegs: v";
1131 first = false;
1132 } else {
1133 os << ", v";
1134 }
1135 os << reg << " (";
1136 switch (kind) {
1137 case kImpreciseConstant:
1138 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1139 DescribeVReg(os, oat_method, code_item, reg, kind);
1140 break;
1141 case kConstant:
1142 os << "Constant: " << kinds.at((reg * 2) + 1);
1143 break;
1144 default:
1145 DescribeVReg(os, oat_method, code_item, reg, kind);
1146 break;
1147 }
1148 os << ")";
1149 }
1150 }
1151 if (!first) {
1152 os << "\n";
1153 }
1154 }
1155
1156
Ian Rogersb23a7722012-10-09 16:54:26 -07001157 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001158 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001159 size_t i = 0;
1160 while (i < code_item->insns_size_in_code_units_) {
1161 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001162 os << StringPrintf("0x%04zx: %s\n", i, instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001163 i += instruction->SizeInCodeUnits();
1164 }
1165 }
1166 }
1167
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001168 verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx,
1169 const DexFile* dex_file,
1170 const DexFile::ClassDef& class_def,
1171 const DexFile::CodeItem* code_item,
1172 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001173 if ((method_access_flags & kAccNative) == 0) {
1174 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001175 StackHandleScope<1> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001176 Handle<mirror::DexCache> dex_cache(
1177 hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
Andreas Gampe00b25f32014-09-17 21:49:05 -07001178 DCHECK(options_->class_loader_ != nullptr);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001179 return verifier::MethodVerifier::VerifyMethodAndDump(soa.Self(), os, dex_method_idx, dex_file,
1180 dex_cache,
Andreas Gampe00b25f32014-09-17 21:49:05 -07001181 *options_->class_loader_,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001182 &class_def, code_item,
1183 NullHandle<mirror::ArtMethod>(),
1184 method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001185 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001186
1187 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001188 }
1189
Mathieu Chartier590fee92013-09-13 13:46:47 -07001190 void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001191 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1192 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001193 const void* portable_code = oat_method.GetPortableCode();
1194 const void* quick_code = oat_method.GetQuickCode();
1195
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001196 if (code_size == 0) {
1197 code_size = oat_method.GetQuickCodeSize();
1198 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001199 if ((code_size == 0) || ((portable_code == nullptr) && (quick_code == nullptr))) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001200 os << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001201 return;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001202 } else if (quick_code != nullptr) {
1203 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1204 size_t offset = 0;
1205 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001206 if (!bad_input) {
1207 DumpMappingAtOffset(os, oat_method, offset, false);
1208 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001209 offset += disassembler_->Dump(os, quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001210 if (!bad_input) {
1211 uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
1212 if (dex_pc != DexFile::kDexNoIndex) {
1213 DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
1214 if (verifier != nullptr) {
1215 DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
1216 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001217 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001218 }
1219 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001220 } else {
1221 CHECK(portable_code != nullptr);
1222 CHECK_EQ(code_size, 0U); // TODO: disassembly of portable is currently not supported.
Ian Rogersb23a7722012-10-09 16:54:26 -07001223 }
1224 }
1225
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001226 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001227 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
1228 const OatDumperOptions* options_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001229 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001230 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001231};
1232
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001233class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001234 public:
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001235 explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001236 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
1237 : os_(os),
1238 image_space_(image_space),
1239 image_header_(image_header),
1240 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001241
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001242 bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001243 std::ostream& os = *os_;
1244 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001245
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001246 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001247
Mathieu Chartier31e89252013-08-28 11:29:12 -07001248 os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
1249 << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
1250
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001251 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001252
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001253 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001254
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001255 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1256
1257 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1258
1259 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001260
Alex Lighta59dd802014-07-02 16:28:08 -07001261 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1262
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001263 {
1264 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
1265 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1266 std::ostream indent1_os(&indent1_filter);
1267 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
1268 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1269 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1270 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001271 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001272 indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
1273 if (image_root_object->IsObjectArray()) {
1274 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
1275 std::ostream indent2_os(&indent2_filter);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001276 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001277 = image_root_object->AsObjectArray<mirror::Object>();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001278 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001279 mirror::Object* value = image_root_object_array->Get(i);
Ian Rogersfa824272013-11-05 16:12:57 -08001280 size_t run = 0;
1281 for (int32_t j = i + 1; j < image_root_object_array->GetLength(); j++) {
1282 if (value == image_root_object_array->Get(j)) {
1283 run++;
1284 } else {
1285 break;
1286 }
1287 }
1288 if (run == 0) {
1289 indent2_os << StringPrintf("%d: ", i);
1290 } else {
1291 indent2_os << StringPrintf("%d to %zd: ", i, i + run);
1292 i = i + run;
1293 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001294 if (value != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001295 PrettyObjectValue(indent2_os, value->GetClass(), value);
1296 } else {
1297 indent2_os << i << ": null\n";
1298 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001299 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001300 }
1301 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001302 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001303 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001304
Brian Carlstromaded5f72011-10-07 17:15:04 -07001305 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001306 std::string image_filename = image_space_.GetImageFilename();
1307 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001308 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001309 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001310 std::string error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -07001311 const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1312 if (oat_file == nullptr) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001313 oat_file = OatFile::Open(oat_location, oat_location, nullptr, false, &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001314 if (oat_file == nullptr) {
1315 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001316 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001317 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001318 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001319 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001320
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001321 stats_.oat_file_bytes = oat_file->Size();
1322
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001323 oat_dumper_.reset(new OatDumper(*oat_file, oat_dumper_options_.release()));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001324
Mathieu Chartier02e25112013-08-14 16:14:24 -07001325 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001326 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001327 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1328 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001329 }
1330
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001331 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001332
1333 // Loop through all the image spaces and dump their objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001334 gc::Heap* heap = Runtime::Current()->GetHeap();
1335 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07001336 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001337 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001338 {
1339 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1340 heap->FlushAllocStack();
1341 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001342 // Since FlushAllocStack() above resets the (active) allocation
1343 // stack. Need to revoke the thread-local allocation stacks that
1344 // point into it.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001345 {
1346 self->TransitionFromRunnableToSuspended(kNative);
1347 ThreadList* thread_list = Runtime::Current()->GetThreadList();
1348 thread_list->SuspendAll();
1349 heap->RevokeAllThreadLocalAllocationStacks(self);
1350 thread_list->ResumeAll();
1351 self->TransitionFromSuspendedToRunnable();
1352 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001353 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001354 {
1355 std::ostream* saved_os = os_;
1356 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1357 std::ostream indent_os(&indent_filter);
1358 os_ = &indent_os;
1359 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001360 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001361 if (space->IsImageSpace()) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001362 gc::space::ImageSpace* image_space = space->AsImageSpace();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001363 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1364 indent_os << "\n";
1365 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001366 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001367 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001368 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001369 indent_os << "\n";
1370 os_ = saved_os;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001371 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001372 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001373 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001374 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001375 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001376 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001377 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001378 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001379 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001380 size_t header_bytes = sizeof(ImageHeader);
1381 stats_.header_bytes = header_bytes;
1382 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1383 stats_.alignment_bytes += alignment_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001384 stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
1385 stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001386 stats_.Dump(os);
1387 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001388
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001389 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001390
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001391 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001392 }
1393
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001394 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001395 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001396 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001397 CHECK(type != nullptr);
1398 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001399 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001400 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001401 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001402 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001403 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001404 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001405 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001406 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001407 } else if (type->IsArtFieldClass()) {
1408 mirror::ArtField* field = value->AsArtField();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001409 os << StringPrintf("%p Field: %s\n", field, PrettyField(field).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001410 } else if (type->IsArtMethodClass()) {
1411 mirror::ArtMethod* method = value->AsArtMethod();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001412 os << StringPrintf("%p Method: %s\n", method, PrettyMethod(method).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001413 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001414 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001415 }
1416 }
1417
Brian Carlstromea46f952013-07-30 01:26:50 -07001418 static void PrintField(std::ostream& os, mirror::ArtField* field, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001419 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001420 const char* descriptor = field->GetTypeDescriptor();
1421 os << StringPrintf("%s: ", field->GetName());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001422 if (descriptor[0] != 'L' && descriptor[0] != '[') {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001423 StackHandleScope<1> hs(Thread::Current());
1424 FieldHelper fh(hs.NewHandle(field));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001425 mirror::Class* type = fh.GetType();
Fred Shih37f05ef2014-07-16 18:38:08 -07001426 DCHECK(type->IsPrimitive());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001427 if (type->IsPrimitiveLong()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001428 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -07001429 } else if (type->IsPrimitiveDouble()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001430 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -07001431 } else if (type->IsPrimitiveFloat()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001432 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Fred Shih37f05ef2014-07-16 18:38:08 -07001433 } else if (type->IsPrimitiveInt()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001434 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Fred Shih37f05ef2014-07-16 18:38:08 -07001435 } else if (type->IsPrimitiveChar()) {
1436 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
1437 } else if (type->IsPrimitiveShort()) {
1438 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
1439 } else if (type->IsPrimitiveBoolean()) {
1440 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1441 field->GetBoolean(obj));
1442 } else if (type->IsPrimitiveByte()) {
1443 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
1444 } else {
1445 LOG(FATAL) << "Unknown type: " << PrettyClass(type);
Ian Rogers48efc2b2012-08-27 17:20:31 -07001446 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001447 } else {
Ian Rogers48efc2b2012-08-27 17:20:31 -07001448 // Get the value, don't compute the type unless it is non-null as we don't want
1449 // to cause class loading.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001450 mirror::Object* value = field->GetObj(obj);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001451 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001452 os << StringPrintf("null %s\n", PrettyDescriptor(descriptor).c_str());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001453 } else {
Ian Rogers50239c72013-06-17 14:53:22 -07001454 // Grab the field type without causing resolution.
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001455 StackHandleScope<1> hs(Thread::Current());
1456 FieldHelper fh(hs.NewHandle(field));
Ian Rogers50239c72013-06-17 14:53:22 -07001457 mirror::Class* field_type = fh.GetType(false);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001458 if (field_type != nullptr) {
Ian Rogers50239c72013-06-17 14:53:22 -07001459 PrettyObjectValue(os, field_type, value);
1460 } else {
1461 os << StringPrintf("%p %s\n", value, PrettyDescriptor(descriptor).c_str());
1462 }
Ian Rogers48efc2b2012-08-27 17:20:31 -07001463 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001464 }
1465 }
1466
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001467 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001468 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001469 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001470 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001471 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001472 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001473 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001474 if (fields != nullptr) {
Ian Rogersd5b32602012-02-26 16:40:04 -08001475 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001476 mirror::ArtField* field = fields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001477 PrintField(os, field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001478 }
1479 }
1480 }
1481
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001482 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001483 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001484 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001485
Ian Rogersef7d42f2014-01-06 12:55:46 -08001486 const void* GetQuickOatCodeBegin(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001487 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001488 const void* quick_code = m->GetEntryPointFromQuickCompiledCode();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001489 if (quick_code == Runtime::Current()->GetClassLinker()->GetQuickResolutionTrampoline()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001490 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001491 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001492 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001493 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001494 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001495 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001496 }
1497
Ian Rogersef7d42f2014-01-06 12:55:46 -08001498 uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001499 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001500 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1501 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001502 return 0;
1503 }
1504 return oat_code_begin[-1];
1505 }
1506
Ian Rogersef7d42f2014-01-06 12:55:46 -08001507 const void* GetQuickOatCodeEnd(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001508 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001509 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001510 if (oat_code_begin == nullptr) {
1511 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001512 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001513 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001514 }
1515
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001516 static void Callback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001517 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001518 DCHECK(obj != nullptr);
1519 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001520 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001521 if (!state->InDumpSpace(obj)) {
1522 return;
1523 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001524
1525 size_t object_bytes = obj->SizeOf();
1526 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1527 state->stats_.object_bytes += object_bytes;
1528 state->stats_.alignment_bytes += alignment_bytes;
1529
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001530 std::ostream& os = *state->os_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001531 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001532 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001533 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1534 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001535 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001536 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001537 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1538 << klass->GetStatus() << ")\n";
Brian Carlstromea46f952013-07-30 01:26:50 -07001539 } else if (obj->IsArtField()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001540 os << StringPrintf("%p: java.lang.reflect.ArtField %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001541 PrettyField(obj->AsArtField()).c_str());
1542 } else if (obj->IsArtMethod()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001543 os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001544 PrettyMethod(obj->AsArtMethod()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001545 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001546 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001547 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001548 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001549 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001550 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001551 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1552 std::ostream indent_os(&indent_filter);
1553 DumpFields(indent_os, obj, obj_class);
Ian Rogersd5b32602012-02-26 16:40:04 -08001554 if (obj->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001555 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Ian Rogersd5b32602012-02-26 16:40:04 -08001556 int32_t length = obj_array->GetLength();
1557 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001558 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001559 size_t run = 0;
1560 for (int32_t j = i + 1; j < length; j++) {
1561 if (value == obj_array->Get(j)) {
1562 run++;
1563 } else {
1564 break;
1565 }
1566 }
1567 if (run == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001568 indent_os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001569 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001570 indent_os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001571 i = i + run;
1572 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001573 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001574 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001575 PrettyObjectValue(indent_os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001576 }
1577 } else if (obj->IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001578 mirror::ObjectArray<mirror::ArtField>* sfields = obj->AsClass()->GetSFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001579 if (sfields != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001580 indent_os << "STATICS:\n";
1581 Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
1582 std::ostream indent2_os(&indent2_filter);
Ian Rogersd5b32602012-02-26 16:40:04 -08001583 for (int32_t i = 0; i < sfields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001584 mirror::ArtField* field = sfields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001585 PrintField(indent2_os, field, field->GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001586 }
1587 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001588 } else if (obj->IsArtMethod()) {
1589 mirror::ArtMethod* method = obj->AsArtMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001590 if (method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001591 // TODO: portable dumping.
1592 DCHECK(method->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1593 DCHECK(method->GetMappingTable() == nullptr) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001594 bool first_occurrence;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001595 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1596 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1597 state->ComputeOatSize(quick_oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001598 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001599 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001600 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001601 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCode()) {
1602 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001603 }
Ian Rogers19846512012-02-24 11:42:47 -08001604 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
Jeff Hao88474b42013-10-23 16:24:40 -07001605 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001606 method->IsClassInitializer()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001607 DCHECK(method->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1608 DCHECK(method->GetMappingTable() == nullptr) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001609 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001610 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -07001611 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001612 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001613
Elliott Hughesa0e18062012-04-13 15:59:59 -07001614 bool first_occurrence;
Ian Rogers0c7abda2012-09-19 13:33:42 -07001615 size_t gc_map_bytes = state->ComputeOatSize(method->GetNativeGcMap(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001616 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001617 state->stats_.gc_map_bytes += gc_map_bytes;
1618 }
1619
1620 size_t pc_mapping_table_bytes =
Ian Rogers1809a722013-08-09 22:05:32 -07001621 state->ComputeOatSize(method->GetMappingTable(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001622 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001623 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1624 }
1625
1626 size_t vmap_table_bytes =
Ian Rogers1809a722013-08-09 22:05:32 -07001627 state->ComputeOatSize(method->GetVmapTable(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001628 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001629 state->stats_.vmap_table_bytes += vmap_table_bytes;
1630 }
1631
Ian Rogersef7d42f2014-01-06 12:55:46 -08001632 // TODO: portable dumping.
1633 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1634 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1635 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1636 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001637 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001638 state->stats_.managed_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001639 if (method->IsConstructor()) {
1640 if (method->IsStatic()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001641 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001642 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001643 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001644 }
1645 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001646 state->stats_.large_method_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001647 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001648 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001649 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001650
Ian Rogersef7d42f2014-01-06 12:55:46 -08001651 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001652 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
1653 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001654
1655 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersef7d42f2014-01-06 12:55:46 -08001656 vmap_table_bytes + quick_oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001657
1658 double expansion =
Ian Rogersef7d42f2014-01-06 12:55:46 -08001659 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001660 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001661 }
1662 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001663 std::string temp;
1664 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001665 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001666
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001667 std::set<const void*> already_seen_;
1668 // Compute the size of the given data within the oat file and whether this is the first time
1669 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001670 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001671 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001672 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001673 already_seen_.insert(oat_data);
1674 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001675 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001676 }
1677 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001678 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001679
1680 public:
1681 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001682 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001683 size_t file_bytes;
1684
1685 size_t header_bytes;
1686 size_t object_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001687 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001688 size_t alignment_bytes;
1689
1690 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001691 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001692 size_t managed_to_native_code_bytes;
1693 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001694 size_t class_initializer_code_bytes;
1695 size_t large_initializer_code_bytes;
1696 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001697
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001698 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001699 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001700 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001701
1702 size_t dex_instruction_bytes;
1703
Brian Carlstromea46f952013-07-30 01:26:50 -07001704 std::vector<mirror::ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001705 std::vector<size_t> method_outlier_size;
1706 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07001707 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001708
1709 explicit Stats()
1710 : oat_file_bytes(0),
1711 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001712 header_bytes(0),
1713 object_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07001714 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001715 alignment_bytes(0),
1716 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001717 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001718 managed_to_native_code_bytes(0),
1719 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001720 class_initializer_code_bytes(0),
1721 large_initializer_code_bytes(0),
1722 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001723 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001724 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001725 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001726 dex_instruction_bytes(0) {}
1727
Elliott Hughesa0e18062012-04-13 15:59:59 -07001728 struct SizeAndCount {
1729 SizeAndCount(size_t bytes, size_t count) : bytes(bytes), count(count) {}
1730 size_t bytes;
1731 size_t count;
1732 };
1733 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1734 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001735
Ian Rogersfc0e94b2013-09-23 23:51:32 -07001736 void Update(const char* descriptor, size_t object_bytes) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001737 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1738 if (it != sizes_and_counts.end()) {
1739 it->second.bytes += object_bytes;
1740 it->second.count += 1;
1741 } else {
1742 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes, 1));
1743 }
1744 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001745
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001746 double PercentOfOatBytes(size_t size) {
1747 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1748 }
1749
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001750 double PercentOfFileBytes(size_t size) {
1751 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1752 }
1753
1754 double PercentOfObjectBytes(size_t size) {
1755 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1756 }
1757
Brian Carlstromea46f952013-07-30 01:26:50 -07001758 void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001759 method_outlier_size.push_back(total_size);
1760 method_outlier_expansion.push_back(expansion);
1761 method_outlier.push_back(method);
1762 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001763
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001764 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001765 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001766 size_t sum_of_sizes = 0;
1767 size_t sum_of_sizes_squared = 0;
1768 size_t sum_of_expansion = 0;
1769 size_t sum_of_expansion_squared = 0;
1770 size_t n = method_outlier_size.size();
1771 for (size_t i = 0; i < n; i++) {
1772 size_t cur_size = method_outlier_size[i];
1773 sum_of_sizes += cur_size;
1774 sum_of_sizes_squared += cur_size * cur_size;
1775 double cur_expansion = method_outlier_expansion[i];
1776 sum_of_expansion += cur_expansion;
1777 sum_of_expansion_squared += cur_expansion * cur_expansion;
1778 }
1779 size_t size_mean = sum_of_sizes / n;
1780 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1781 double expansion_mean = sum_of_expansion / n;
1782 double expansion_variance =
1783 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1784
1785 // Dump methods whose size is a certain number of standard deviations from the mean
1786 size_t dumped_values = 0;
1787 size_t skipped_values = 0;
1788 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1789 size_t cur_size_variance = i * i * size_variance;
1790 bool first = true;
1791 for (size_t j = 0; j < n; j++) {
1792 size_t cur_size = method_outlier_size[j];
1793 if (cur_size > size_mean) {
1794 size_t cur_var = cur_size - size_mean;
1795 cur_var = cur_var * cur_var;
1796 if (cur_var > cur_size_variance) {
1797 if (dumped_values > 20) {
1798 if (i == 1) {
1799 skipped_values++;
1800 } else {
1801 i = 2; // jump to counting for 1 standard deviation
1802 break;
1803 }
1804 } else {
1805 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001806 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001807 first = false;
1808 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001809 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001810 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001811 method_outlier_size[j] = 0; // don't consider this method again
1812 dumped_values++;
1813 }
1814 }
1815 }
1816 }
1817 }
1818 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001819 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001820 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001821 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001822 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001823
1824 // Dump methods whose expansion is a certain number of standard deviations from the mean
1825 dumped_values = 0;
1826 skipped_values = 0;
1827 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1828 double cur_expansion_variance = i * i * expansion_variance;
1829 bool first = true;
1830 for (size_t j = 0; j < n; j++) {
1831 double cur_expansion = method_outlier_expansion[j];
1832 if (cur_expansion > expansion_mean) {
1833 size_t cur_var = cur_expansion - expansion_mean;
1834 cur_var = cur_var * cur_var;
1835 if (cur_var > cur_expansion_variance) {
1836 if (dumped_values > 20) {
1837 if (i == 1) {
1838 skipped_values++;
1839 } else {
1840 i = 2; // jump to counting for 1 standard deviation
1841 break;
1842 }
1843 } else {
1844 if (first) {
1845 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001846 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001847 first = false;
1848 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001849 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001850 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001851 method_outlier_expansion[j] = 0.0; // don't consider this method again
1852 dumped_values++;
1853 }
1854 }
1855 }
1856 }
1857 }
1858 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001859 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001860 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001861 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001862 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001863 }
1864
Ian Rogersb726dcb2012-09-05 08:57:23 -07001865 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001866 {
1867 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1868 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
1869 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1870 std::ostream indent_os(&indent_filter);
1871 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
1872 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier32327092013-08-30 14:04:08 -07001873 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001874 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
1875 header_bytes, PercentOfFileBytes(header_bytes),
1876 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07001877 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001878 alignment_bytes, PercentOfFileBytes(alignment_bytes))
1879 << std::flush;
Mathieu Chartier32327092013-08-30 14:04:08 -07001880 CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001881 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001882
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001883 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001884 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07001885 for (const auto& sizes_and_count : sizes_and_counts) {
1886 const std::string& descriptor(sizes_and_count.first);
1887 double average = static_cast<double>(sizes_and_count.second.bytes) /
1888 static_cast<double>(sizes_and_count.second.count);
1889 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001890 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001891 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001892 descriptor.c_str(), sizes_and_count.second.bytes,
1893 sizes_and_count.second.count, average, percent);
1894 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001895 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001896 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001897 CHECK_EQ(object_bytes, object_bytes_total);
1898
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001899 os << StringPrintf("oat_file_bytes = %8zd\n"
1900 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1901 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1902 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1903 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1904 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1905 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001906 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001907 managed_code_bytes,
1908 PercentOfOatBytes(managed_code_bytes),
1909 managed_to_native_code_bytes,
1910 PercentOfOatBytes(managed_to_native_code_bytes),
1911 native_to_managed_code_bytes,
1912 PercentOfOatBytes(native_to_managed_code_bytes),
1913 class_initializer_code_bytes,
1914 PercentOfOatBytes(class_initializer_code_bytes),
1915 large_initializer_code_bytes,
1916 PercentOfOatBytes(large_initializer_code_bytes),
1917 large_method_code_bytes,
1918 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001919 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07001920 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001921 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001922 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
1923 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07001924 }
1925
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001926 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1927 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1928 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001929 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1930 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1931 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001932 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001933
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001934 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
1935 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001936 static_cast<double>(managed_code_bytes) /
1937 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07001938 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001939 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001940 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001941
1942 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001943 }
1944 } stats_;
1945
1946 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001947 enum {
1948 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1949 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1950 kLargeConstructorDexBytes = 4000,
1951 // Number of bytes for a method to be considered large. Based on the 4000 basic block
1952 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1953 kLargeMethodDexBytes = 16000
1954 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001955 std::ostream* os_;
Ian Rogers1d54e732013-05-02 21:10:01 -07001956 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001957 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001958 std::unique_ptr<OatDumper> oat_dumper_;
1959 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001960
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001961 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001962};
1963
Andreas Gampe00b25f32014-09-17 21:49:05 -07001964static NoopCompilerCallbacks callbacks;
Elliott Hughes72395bf2012-04-24 13:45:26 -07001965
Andreas Gampe00b25f32014-09-17 21:49:05 -07001966static Runtime* StartRuntime(const char* boot_image_location, const char* image_location,
1967 InstructionSet instruction_set) {
Ian Rogerse63db272014-07-15 15:36:11 -07001968 RuntimeOptions options;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001969 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001970 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001971 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001972 std::string boot_oat_option;
Brian Carlstrom6c871802013-07-17 14:25:35 -07001973
1974 // We are more like a compiler than a run-time. We don't want to execute code.
Brian Carlstromc0a1b182014-03-04 23:19:06 -08001975 options.push_back(std::make_pair("compilercallbacks", &callbacks));
Brian Carlstrom6c871802013-07-17 14:25:35 -07001976
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001977 if (boot_image_location != nullptr) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001978 boot_image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001979 boot_image_option += boot_image_location;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001980 options.push_back(std::make_pair(boot_image_option.c_str(), nullptr));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001981 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001982 if (image_location != nullptr) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001983 image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001984 image_option += image_location;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001985 options.push_back(std::make_pair(image_option.c_str(), nullptr));
Brian Carlstromaded5f72011-10-07 17:15:04 -07001986 }
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001987 options.push_back(
1988 std::make_pair("imageinstructionset",
1989 reinterpret_cast<const void*>(GetInstructionSetString(instruction_set))));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001990
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001991 if (!Runtime::Create(options, false)) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001992 fprintf(stderr, "Failed to create runtime\n");
Andreas Gampe00b25f32014-09-17 21:49:05 -07001993 return nullptr;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001994 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07001995
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001996 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
Ian Rogersb9beb2e2014-05-09 16:57:40 -07001997 // give it away now and then switch to a more manageable ScopedObjectAccess.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001998 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
Andreas Gampe00b25f32014-09-17 21:49:05 -07001999
2000 return Runtime::Current();
2001}
2002
2003static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
2004 std::ostream* os) {
2005 // Dumping the image, no explicit class loader.
2006 NullHandle<mirror::ClassLoader> null_class_loader;
2007 options->class_loader_ = &null_class_loader;
2008
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002009 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002010 gc::Heap* heap = runtime->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -07002011 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002012 CHECK(image_space != nullptr);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002013 const ImageHeader& image_header = image_space->GetImageHeader();
2014 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002015 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002016 return EXIT_FAILURE;
2017 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002018 ImageDumper image_dumper(os, *image_space, image_header, options);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002019 bool success = image_dumper.Dump();
2020 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002021}
2022
Andreas Gampe00b25f32014-09-17 21:49:05 -07002023static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2024 std::ostream* os) {
2025 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2026
2027 Thread* self = Thread::Current();
2028 CHECK(self != nullptr);
2029 // Need well-known-classes.
2030 WellKnownClasses::Init(self->GetJniEnv());
2031
2032 // Need to register dex files to get a working dex cache.
2033 ScopedObjectAccess soa(self);
2034 ClassLinker* class_linker = runtime->GetClassLinker();
2035 class_linker->RegisterOatFile(oat_file);
2036 std::vector<const DexFile*> dex_files;
2037 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2038 std::string error_msg;
2039 const DexFile* dex_file = odf->OpenDexFile(&error_msg);
2040 CHECK(dex_file != nullptr) << error_msg;
2041 class_linker->RegisterDexFile(*dex_file);
2042 dex_files.push_back(dex_file);
2043 }
2044
2045 // Need a class loader.
2046 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader);
2047 ScopedLocalRef<jobject> class_loader_local(soa.Env(),
2048 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
2049 jobject class_loader = soa.Env()->NewGlobalRef(class_loader_local.get());
2050 // Fake that we're a compiler.
2051 runtime->SetCompileTimeClassPath(class_loader, dex_files);
2052
2053 // Use the class loader while dumping.
2054 StackHandleScope<1> scope(self);
2055 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2056 soa.Decode<mirror::ClassLoader*>(class_loader));
2057 options->class_loader_ = &loader_handle;
2058
2059 OatDumper oat_dumper(*oat_file, options);
2060 bool success = oat_dumper.Dump(*os);
2061 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2062}
2063
2064static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
2065 // No image = no class loader.
2066 NullHandle<mirror::ClassLoader> null_class_loader;
2067 options->class_loader_ = &null_class_loader;
2068
2069 OatDumper oat_dumper(*oat_file, options);
2070 bool success = oat_dumper.Dump(*os);
2071 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2072}
2073
2074static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2075 std::ostream* os) {
2076 std::string error_msg;
2077 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, false, &error_msg);
2078 if (oat_file == nullptr) {
2079 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2080 return EXIT_FAILURE;
2081 }
2082
2083 if (runtime != nullptr) {
2084 return DumpOatWithRuntime(runtime, oat_file, options, os);
2085 } else {
2086 return DumpOatWithoutRuntime(oat_file, options, os);
2087 }
2088}
2089
2090static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2091 std::string error_msg;
2092 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, false, &error_msg);
2093 if (oat_file == nullptr) {
2094 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2095 return EXIT_FAILURE;
2096 }
2097
2098 OatSymbolizer oat_symbolizer(oat_file, output_name);
2099 if (!oat_symbolizer.Init()) {
2100 fprintf(stderr, "Failed to initialize symbolizer\n");
2101 return EXIT_FAILURE;
2102 }
2103 if (!oat_symbolizer.Symbolize()) {
2104 fprintf(stderr, "Failed to symbolize\n");
2105 return EXIT_FAILURE;
2106 }
2107
2108 return EXIT_SUCCESS;
2109}
2110
2111struct OatdumpArgs {
2112 bool Parse(int argc, char** argv) {
2113 // Skip over argv[0].
2114 argv++;
2115 argc--;
2116
2117 if (argc == 0) {
2118 fprintf(stderr, "No arguments specified\n");
2119 usage();
2120 return false;
2121 }
2122
2123 for (int i = 0; i < argc; i++) {
2124 const StringPiece option(argv[i]);
2125 if (option.starts_with("--oat-file=")) {
2126 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2127 } else if (option.starts_with("--image=")) {
2128 image_location_ = option.substr(strlen("--image=")).data();
2129 } else if (option.starts_with("--boot-image=")) {
2130 boot_image_location_ = option.substr(strlen("--boot-image=")).data();
2131 } else if (option.starts_with("--instruction-set=")) {
2132 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
2133 instruction_set_ = GetInstructionSetFromString(instruction_set_str.data());
2134 if (instruction_set_ == kNone) {
2135 fprintf(stderr, "Unsupported instruction set %s\n", instruction_set_str.data());
2136 usage();
2137 return false;
2138 }
2139 } else if (option =="--dump:raw_mapping_table") {
2140 dump_raw_mapping_table_ = true;
2141 } else if (option == "--dump:raw_gc_map") {
2142 dump_raw_gc_map_ = true;
2143 } else if (option == "--no-dump:vmap") {
2144 dump_vmap_ = false;
2145 } else if (option == "--no-disassemble") {
2146 disassemble_code_ = false;
2147 } else if (option.starts_with("--output=")) {
2148 output_name_ = option.substr(strlen("--output=")).ToString();
2149 const char* filename = output_name_.c_str();
2150 out_.reset(new std::ofstream(filename));
2151 if (!out_->good()) {
2152 fprintf(stderr, "Failed to open output filename %s\n", filename);
2153 usage();
2154 return false;
2155 }
2156 os_ = out_.get();
2157 } else if (option.starts_with("--symbolize=")) {
2158 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2159 symbolize_ = true;
2160 } else {
2161 fprintf(stderr, "Unknown argument %s\n", option.data());
2162 usage();
2163 return false;
2164 }
2165 }
2166
2167 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2168 fprintf(stderr, "Either --image or --oat must be specified\n");
2169 return false;
2170 }
2171
2172 if (image_location_ != nullptr && oat_filename_ != nullptr) {
2173 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
2174 return false;
2175 }
2176
2177 return true;
2178 }
2179
2180 const char* oat_filename_ = nullptr;
2181 const char* image_location_ = nullptr;
2182 const char* boot_image_location_ = nullptr;
2183 InstructionSet instruction_set_ = kRuntimeISA;
2184 std::string elf_filename_prefix_;
2185 std::ostream* os_ = &std::cout;
2186 std::unique_ptr<std::ofstream> out_;
2187 std::string output_name_;
2188 bool dump_raw_mapping_table_ = false;
2189 bool dump_raw_gc_map_ = false;
2190 bool dump_vmap_ = true;
2191 bool disassemble_code_ = true;
2192 bool symbolize_ = false;
2193};
2194
2195static int oatdump(int argc, char** argv) {
2196 InitLogging(argv);
2197
2198 OatdumpArgs args;
2199 if (!args.Parse(argc, argv)) {
2200 return EXIT_FAILURE;
2201 }
2202
2203 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2204 bool absolute_addresses = (args.oat_filename_ == nullptr);
2205
2206 std::unique_ptr<OatDumperOptions> oat_dumper_options(new OatDumperOptions(
2207 args.dump_raw_mapping_table_,
2208 args.dump_raw_gc_map_,
2209 args.dump_vmap_,
2210 args.disassemble_code_,
2211 absolute_addresses,
2212 nullptr));
2213
2214 std::unique_ptr<Runtime> runtime;
2215 if ((args.boot_image_location_ != nullptr || args.image_location_ != nullptr) &&
2216 !args.symbolize_) {
2217 // If we have a boot image option, try to start the runtime; except when just symbolizing.
2218 runtime.reset(StartRuntime(args.boot_image_location_,
2219 args.image_location_,
2220 args.instruction_set_));
2221 }
2222
2223 if (args.oat_filename_ != nullptr) {
2224 if (args.symbolize_) {
2225 return SymbolizeOat(args.oat_filename_, args.output_name_);
2226 } else {
2227 return DumpOat(runtime.get(), args.oat_filename_, oat_dumper_options.release(), args.os_);
2228 }
2229 }
2230
2231 if (runtime.get() == nullptr) {
2232 // We need the runtime when printing an image.
2233 return EXIT_FAILURE;
2234 }
2235
2236 return DumpImage(runtime.get(), args.image_location_, oat_dumper_options.release(), args.os_);
2237}
2238
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002239} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002240
2241int main(int argc, char** argv) {
2242 return art::oatdump(argc, argv);
2243}