blob: ece9f7d93ad24cad5c763446a79da41fc4a65ded [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Brian Carlstrom78128a62011-09-15 17:21:19 -070022#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070023#include <unordered_map>
Brian Carlstrom78128a62011-09-15 17:21:19 -070024#include <vector>
25
Elliott Hughese222ee02012-12-13 14:41:43 -080026#include "base/stringpiece.h"
Elliott Hughes76160052012-12-12 16:31:20 -080027#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070028#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080031#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080032#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070033#include "elf_builder.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070034#include "field_helper.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080035#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070036#include "gc/space/image_space.h"
37#include "gc/space/large_object_space.h"
38#include "gc/space/space-inl.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070039#include "image.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080040#include "indenter.h"
Ian Rogers1809a722013-08-09 22:05:32 -070041#include "mapping_table.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070042#include "mirror/art_field-inl.h"
43#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044#include "mirror/array-inl.h"
45#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046#include "mirror/object-inl.h"
47#include "mirror/object_array-inl.h"
Brian Carlstromc0a1b182014-03-04 23:19:06 -080048#include "noop_compiler_callbacks.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080049#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010050#include "oat_file-inl.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080051#include "os.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070052#include "output_stream.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070053#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070054#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055#include "scoped_thread_state_change.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070056#include "ScopedLocalRef.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080057#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080058#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080059#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070060#include "vmap_table.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070061#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070062
63namespace art {
64
65static void usage() {
66 fprintf(stderr,
67 "Usage: oatdump [options] ...\n"
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +000068 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080069 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070070 "\n");
71 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080072 " --oat-file=<file.oat>: specifies an input oat filename.\n"
TDYa127a0a2a6c2012-10-16 22:47:38 -070073 " Example: --oat-file=/system/framework/boot.oat\n"
Brian Carlstromaded5f72011-10-07 17:15:04 -070074 "\n");
75 fprintf(stderr,
76 " --image=<file.art>: specifies an input image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080077 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070078 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070079 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070080 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080081 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070082 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070083 fprintf(stderr,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070084 " --instruction-set=(arm|arm64|mips|x86|x86_64): for locating the image\n"
85 " file based on the image location set.\n"
Brian Carlstrom0f5baa02014-05-22 11:54:18 -070086 " Example: --instruction-set=x86\n"
87 " Default: %s\n"
88 "\n",
89 GetInstructionSetString(kRuntimeISA));
90 fprintf(stderr,
Brian Carlstrom27ec9612011-09-19 20:20:38 -070091 " --output=<file> may be used to send the output to a file.\n"
92 " Example: --output=/tmp/oatdump.txt\n"
93 "\n");
Dave Allison404f59f2014-02-24 11:10:01 -080094 fprintf(stderr,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070095 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
96 " Example: --dump:raw_mapping_table\n"
97 "\n");
98 fprintf(stderr,
99 " --dump:raw_mapping_table enables dumping of the GC map.\n"
100 " Example: --dump:raw_gc_map\n"
101 "\n");
102 fprintf(stderr,
103 " --no-dump:vmap may be used to disable vmap dumping.\n"
104 " Example: --no-dump:vmap\n"
105 "\n");
106 fprintf(stderr,
107 " --no-disassemble may be used to disable disassembly.\n"
108 " Example: --no-disassemble\n"
Dave Allison404f59f2014-02-24 11:10:01 -0800109 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700110}
111
Ian Rogersff1ed472011-09-20 13:46:24 -0700112const char* image_roots_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -0800113 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -0700114 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700115 "kImtUnimplementedMethod",
Jeff Hao88474b42013-10-23 16:24:40 -0700116 "kDefaultImt",
Brian Carlstrome24fa612011-09-29 00:53:55 -0700117 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -0700118 "kRefsOnlySaveMethod",
119 "kRefsAndArgsSaveMethod",
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700120 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700121 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700122};
123
Ian Rogers0279ebb2014-10-08 17:27:48 -0700124class OatSymbolizer FINAL : public CodeOutput {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700125 public:
Ian Rogers0279ebb2014-10-08 17:27:48 -0700126 explicit OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
127 oat_file_(oat_file), builder_(nullptr), elf_output_(nullptr),
128 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
129 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700130
131 bool Init() {
132 Elf32_Word oat_data_size = oat_file_->GetOatHeader().GetExecutableOffset();
133
134 uint32_t diff = static_cast<uint32_t>(oat_file_->End() - oat_file_->Begin());
135 uint32_t oat_exec_size = diff - oat_data_size;
136
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700137 elf_output_ = OS::CreateEmptyFile(output_name_.c_str());
138
139 builder_.reset(new ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
140 Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(
141 this,
142 elf_output_,
143 oat_file_->GetOatHeader().GetInstructionSet(),
144 0,
145 oat_data_size,
146 oat_data_size,
147 oat_exec_size,
148 true,
149 false));
150
151 if (!builder_->Init()) {
152 builder_.reset(nullptr);
153 return false;
154 }
155
156 return true;
157 }
158
159 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
160 uint32_t,
161 const OatFile::OatMethod&,
162 const DexFile&,
163 uint32_t,
164 const DexFile::CodeItem*,
165 uint32_t);
166
167 bool Symbolize() {
168 if (builder_.get() == nullptr) {
169 return false;
170 }
171
172 Walk(&art::OatSymbolizer::RegisterForDedup);
173
174 NormalizeState();
175
176 Walk(&art::OatSymbolizer::AddSymbol);
177
178 bool result = builder_->Write();
179
180 elf_output_->Flush();
181 elf_output_->Close();
182
183 return result;
184 }
185
186 void Walk(Callback callback) {
187 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
188 for (size_t i = 0; i < oat_dex_files.size(); i++) {
189 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
190 CHECK(oat_dex_file != NULL);
191 WalkOatDexFile(oat_dex_file, callback);
192 }
193 }
194
195 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
196 std::string error_msg;
197 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
198 if (dex_file.get() == nullptr) {
199 return;
200 }
201 for (size_t class_def_index = 0;
202 class_def_index < dex_file->NumClassDefs();
203 class_def_index++) {
204 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
205 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
206 OatClassType type = oat_class.GetType();
207 switch (type) {
208 case kOatClassAllCompiled:
209 case kOatClassSomeCompiled:
210 WalkOatClass(oat_class, *dex_file.get(), class_def, callback);
211 break;
212
213 case kOatClassNoneCompiled:
214 case kOatClassMax:
215 // Ignore.
216 break;
217 }
218 }
219 }
220
221 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
222 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700223 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700224 if (class_data == nullptr) { // empty class such as a marker interface?
225 return;
226 }
227 // Note: even if this is an interface or a native class, we still have to walk it, as there
228 // might be a static initializer.
229 ClassDataItemIterator it(dex_file, class_data);
230 SkipAllFields(&it);
231 uint32_t class_method_idx = 0;
232 while (it.HasNextDirectMethod()) {
233 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
234 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700235 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700236 class_method_idx++;
237 it.Next();
238 }
239 while (it.HasNextVirtualMethod()) {
240 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
241 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700242 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700243 class_method_idx++;
244 it.Next();
245 }
246 DCHECK(!it.HasNext());
247 }
248
249 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
250 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
251 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
252 uint32_t method_access_flags, Callback callback) {
253 if ((method_access_flags & kAccAbstract) != 0) {
254 // Abstract method, no code.
255 return;
256 }
257 if (oat_method.GetCodeOffset() == 0) {
258 // No code.
259 return;
260 }
261
262 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
263 method_access_flags);
264 }
265
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,
Ian Rogers0279ebb2014-10-08 17:27:48 -0700310 Elf32_Sym, Elf32_Shdr>* symtab = builder_->GetSymtabBuilder();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700311
Ian Rogers0279ebb2014-10-08 17:27:48 -0700312 symtab->AddSymbol(pretty_name, &builder_->GetTextBuilder(),
313 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
314 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700315 }
316 }
317
Vladimir Markof4da6752014-08-01 19:04:18 +0100318 // Set oat data offset. Required by ElfBuilder/CodeOutput.
319 void SetCodeOffset(size_t offset) {
320 // Nothing to do.
321 }
322
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700323 // Write oat code. Required by ElfBuilder/CodeOutput.
324 bool Write(OutputStream* out) {
325 return out->WriteFully(oat_file_->Begin(), oat_file_->End() - oat_file_->Begin());
326 }
327
328 private:
329 static void SkipAllFields(ClassDataItemIterator* it) {
330 while (it->HasNextStaticField()) {
331 it->Next();
332 }
333 while (it->HasNextInstanceField()) {
334 it->Next();
335 }
336 }
337
338 const OatFile* oat_file_;
339 std::unique_ptr<ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
340 Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr> > builder_;
341 File* elf_output_;
342 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700343 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700344};
345
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700346class OatDumperOptions {
347 public:
348 OatDumperOptions(bool dump_raw_mapping_table,
349 bool dump_raw_gc_map,
350 bool dump_vmap,
351 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700352 bool absolute_addresses,
353 Handle<mirror::ClassLoader>* class_loader)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700354 : dump_raw_mapping_table_(dump_raw_mapping_table),
355 dump_raw_gc_map_(dump_raw_gc_map),
356 dump_vmap_(dump_vmap),
357 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700358 absolute_addresses_(absolute_addresses),
359 class_loader_(class_loader) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700360
361 const bool dump_raw_mapping_table_;
362 const bool dump_raw_gc_map_;
363 const bool dump_vmap_;
364 const bool disassemble_code_;
365 const bool absolute_addresses_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700366 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700367};
368
Elliott Hughese3c845c2012-02-28 17:23:01 -0800369class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700370 public:
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700371 explicit OatDumper(const OatFile& oat_file, OatDumperOptions* options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000372 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800373 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700374 options_(options),
375 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet(),
376 new DisassemblerOptions(options_->absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000377 oat_file.Begin(),
378 true /* can_read_litals_ */))) {
Andreas Gampe00b25f32014-09-17 21:49:05 -0700379 CHECK(options_->class_loader_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800380 AddAllOffsets();
381 }
382
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700383 ~OatDumper() {
384 delete options_;
385 delete disassembler_;
386 }
387
388 bool Dump(std::ostream& os) {
389 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800390 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700391
392 os << "MAGIC:\n";
393 os << oat_header.GetMagic() << "\n\n";
394
395 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800396 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700397
Elliott Hughesa72ec822012-03-05 17:12:22 -0800398 os << "INSTRUCTION SET:\n";
399 os << oat_header.GetInstructionSet() << "\n\n";
400
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700401 {
402 std::unique_ptr<const InstructionSetFeatures> features(
403 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
404 oat_header.GetInstructionSetFeaturesBitmap()));
405 os << "INSTRUCTION SET FEATURES:\n";
406 os << features->GetFeatureString() << "\n\n";
407 }
Dave Allison70202782013-10-22 17:52:19 -0700408
Brian Carlstromaded5f72011-10-07 17:15:04 -0700409 os << "DEX FILE COUNT:\n";
410 os << oat_header.GetDexFileCount() << "\n\n";
411
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800412#define DUMP_OAT_HEADER_OFFSET(label, offset) \
413 os << label " OFFSET:\n"; \
414 os << StringPrintf("0x%08x", oat_header.offset()); \
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700415 if (oat_header.offset() != 0 && options_->absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800416 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
417 } \
418 os << StringPrintf("\n\n");
419
420 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
421 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
422 GetInterpreterToInterpreterBridgeOffset);
423 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
424 GetInterpreterToCompiledCodeBridgeOffset);
425 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
426 GetJniDlsymLookupOffset);
427 DUMP_OAT_HEADER_OFFSET("PORTABLE IMT CONFLICT TRAMPOLINE",
428 GetPortableImtConflictTrampolineOffset);
429 DUMP_OAT_HEADER_OFFSET("PORTABLE RESOLUTION TRAMPOLINE",
430 GetPortableResolutionTrampolineOffset);
431 DUMP_OAT_HEADER_OFFSET("PORTABLE TO INTERPRETER BRIDGE",
432 GetPortableToInterpreterBridgeOffset);
433 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
434 GetQuickGenericJniTrampolineOffset);
435 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
436 GetQuickImtConflictTrampolineOffset);
437 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
438 GetQuickResolutionTrampolineOffset);
439 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
440 GetQuickToInterpreterBridgeOffset);
441#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700442
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700443 os << "IMAGE PATCH DELTA:\n";
444 os << StringPrintf("%d (0x%08x)\n\n",
445 oat_header.GetImagePatchDelta(),
446 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700447
Brian Carlstrom28db0122012-10-18 16:20:41 -0700448 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
449 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
450
451 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800452 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700453
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700454 // Print the key-value store.
455 {
456 os << "KEY VALUE STORE:\n";
457 size_t index = 0;
458 const char* key;
459 const char* value;
460 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
461 os << key << " = " << value << "\n";
462 index++;
463 }
464 os << "\n";
465 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700466
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700467 if (options_->absolute_addresses_) {
468 os << "BEGIN:\n";
469 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700470
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700471 os << "END:\n";
472 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
473 }
474
475 os << "SIZE:\n";
476 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700477
478 os << std::flush;
479
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800480 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
481 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700482 CHECK(oat_dex_file != nullptr);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700483 if (!DumpOatDexFile(os, *oat_dex_file)) {
484 success = false;
485 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700486 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700487 os << std::flush;
488 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700489 }
490
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800491 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700492 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
493 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800494 return 0; // Address not in oat file
495 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800496 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
497 reinterpret_cast<uintptr_t>(oat_file_.Begin());
498 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800499 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800500 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800501 return end_offset - begin_offset;
502 }
503
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700504 InstructionSet GetInstructionSet() {
505 return oat_file_.GetOatHeader().GetInstructionSet();
506 }
507
Ian Rogersef7d42f2014-01-06 12:55:46 -0800508 const void* GetQuickOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800509 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
510 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700511 CHECK(oat_dex_file != nullptr);
512 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700513 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700514 if (dex_file.get() == nullptr) {
515 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
516 << "': " << error_msg;
517 } else {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700518 const DexFile::ClassDef* class_def =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700519 dex_file->FindClassDef(m->GetDeclaringClassDescriptor());
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700520 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700521 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100522 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800523 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100524 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800525 }
526 }
527 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700528 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800529 }
530
Brian Carlstromaded5f72011-10-07 17:15:04 -0700531 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800532 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800533 // We don't know the length of the code for each method, but we need to know where to stop
534 // when disassembling. What we do know is that a region of code will be followed by some other
535 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
536 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800537 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
538 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700539 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700540 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700541 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700542 if (dex_file.get() == nullptr) {
543 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
544 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800545 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800546 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800547 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800548 for (size_t class_def_index = 0;
549 class_def_index < dex_file->NumClassDefs();
550 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800551 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100552 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700553 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700554 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800555 ClassDataItemIterator it(*dex_file, class_data);
556 SkipAllFields(it);
557 uint32_t class_method_index = 0;
558 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100559 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800560 it.Next();
561 }
562 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100563 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800564 it.Next();
565 }
566 }
567 }
568 }
569
570 // If the last thing in the file is code for a method, there won't be an offset for the "next"
571 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
572 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800573 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800574 }
575
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700576 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
577 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
578 }
579
Elliott Hughese3c845c2012-02-28 17:23:01 -0800580 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800581 uint32_t code_offset = oat_method.GetCodeOffset();
582 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
583 code_offset &= ~0x1;
584 }
585 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800586 offsets_.insert(oat_method.GetMappingTableOffset());
587 offsets_.insert(oat_method.GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700588 offsets_.insert(oat_method.GetNativeGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800589 }
590
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700591 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
592 bool success = true;
593 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800594 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800595 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700596
597 // Create the verifier early.
598
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700599 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700600 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700601 if (dex_file.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700602 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700603 os << std::flush;
604 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700605 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800606 for (size_t class_def_index = 0;
607 class_def_index < dex_file->NumClassDefs();
608 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700609 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
610 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700611 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100612 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700613 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
614 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100615 << " (" << oat_class.GetStatus() << ")"
616 << " (" << oat_class.GetType() << ")\n";
617 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800618 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
619 std::ostream indented_os(&indent_filter);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700620 if (!DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def)) {
621 success = false;
622 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700623 }
624
625 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700626 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700627 }
628
Elliott Hughese3c845c2012-02-28 17:23:01 -0800629 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700630 while (it.HasNextStaticField()) {
631 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700632 }
Ian Rogers0571d352011-11-03 19:51:38 -0700633 while (it.HasNextInstanceField()) {
634 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700635 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800636 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700637
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700638 bool DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800639 const DexFile::ClassDef& class_def) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700640 bool success = true;
Ian Rogers13735952014-10-08 12:43:28 -0700641 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700642 if (class_data == nullptr) { // empty class such as a marker interface?
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700643 os << std::flush;
644 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800645 }
646 ClassDataItemIterator it(dex_file, class_data);
647 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700648 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700649 while (it.HasNextDirectMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700650 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
651 it.GetMemberIndex(), it.GetMethodCodeItem(),
652 it.GetRawMemberAccessFlags())) {
653 success = false;
654 }
655 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700656 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700657 }
Ian Rogers0571d352011-11-03 19:51:38 -0700658 while (it.HasNextVirtualMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700659 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
660 it.GetMemberIndex(), it.GetMethodCodeItem(),
661 it.GetRawMemberAccessFlags())) {
662 success = false;
663 }
664 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700665 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700666 }
Ian Rogers0571d352011-11-03 19:51:38 -0700667 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700668 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700669 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700670 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800671
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700672 static constexpr uint32_t kPrologueBytes = 16;
673
674 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
675 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
676
677 bool DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700678 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700679 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800680 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
681 uint32_t method_access_flags) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700682 bool success = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800683 os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700684 class_method_index, PrettyMethod(dex_method_idx, dex_file, true).c_str(),
685 dex_method_idx);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800686 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700687 std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter));
688 Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count);
689 std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter));
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800690 {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700691 *indent1_os << "DEX CODE:\n";
692 DumpDexCode(*indent2_os, dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700693 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700694
695 std::unique_ptr<verifier::MethodVerifier> verifier;
696 if (Runtime::Current() != nullptr) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700697 *indent1_os << "VERIFIER TYPE ANALYSIS:\n";
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700698 verifier.reset(DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item,
699 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700700 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700701
702 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
703 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
704 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800705 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700706 *indent1_os << "OatMethodOffsets ";
707 if (options_->absolute_addresses_) {
708 *indent1_os << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100709 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700710 *indent1_os << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
711 if (oat_method_offsets_offset > oat_file_.Size()) {
712 *indent1_os << StringPrintf(
713 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
714 oat_method_offsets_offset, oat_file_.Size());
715 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
716 os << std::flush;
717 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800718 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700719
720 uint32_t code_offset = oat_method.GetCodeOffset();
721 *indent2_os << StringPrintf("code_offset: 0x%08x ", code_offset);
722 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
723 if (aligned_code_begin > oat_file_.Size()) {
724 *indent2_os << StringPrintf("WARNING: "
725 "code offset 0x%08x is past end of file 0x%08zx.\n",
726 aligned_code_begin, oat_file_.Size());
727 success = false;
728 }
729 *indent2_os << "\n";
730
731 *indent2_os << "gc_map: ";
732 if (options_->absolute_addresses_) {
733 *indent2_os << StringPrintf("%p ", oat_method.GetNativeGcMap());
734 }
735 uint32_t gc_map_offset = oat_method.GetNativeGcMapOffset();
736 *indent2_os << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
737 if (gc_map_offset > oat_file_.Size()) {
738 *indent2_os << StringPrintf("WARNING: "
739 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
740 gc_map_offset, oat_file_.Size());
741 success = false;
742 } else if (options_->dump_raw_gc_map_) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700743 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800744 std::ostream indent3_os(&indent3_filter);
745 DumpGcMap(indent3_os, oat_method, code_item);
746 }
747 }
748 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700749 *indent1_os << "OatQuickMethodHeader ";
750 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
751 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700752
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700753 if (options_->absolute_addresses_) {
754 *indent1_os << StringPrintf("%p ", method_header);
755 }
756 *indent1_os << StringPrintf("(offset=0x%08x)\n", method_header_offset);
757 if (method_header_offset > oat_file_.Size()) {
758 *indent1_os << StringPrintf(
759 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
760 method_header_offset, oat_file_.Size());
761 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
762 os << std::flush;
763 return false;
764 }
765
766 *indent2_os << "mapping_table: ";
767 if (options_->absolute_addresses_) {
768 *indent2_os << StringPrintf("%p ", oat_method.GetMappingTable());
769 }
770 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
771 *indent2_os << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
772 if (mapping_table_offset > oat_file_.Size()) {
773 *indent2_os << StringPrintf("WARNING: "
774 "mapping table offset 0x%08x is past end of file 0x%08zx. "
775 "mapping table offset was loaded from offset 0x%08x.\n",
776 mapping_table_offset, oat_file_.Size(),
777 oat_method.GetMappingTableOffsetOffset());
778 success = false;
779 } else if (options_->dump_raw_mapping_table_) {
780 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
781 std::ostream indent3_os(&indent3_filter);
782 DumpMappingTable(indent3_os, oat_method);
783 }
784
785 *indent2_os << "vmap_table: ";
786 if (options_->absolute_addresses_) {
787 *indent2_os << StringPrintf("%p ", oat_method.GetVmapTable());
788 }
789 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
790 *indent2_os << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
791 if (vmap_table_offset > oat_file_.Size()) {
792 *indent2_os << StringPrintf("WARNING: "
793 "vmap table offset 0x%08x is past end of file 0x%08zx. "
794 "vmap table offset was loaded from offset 0x%08x.\n",
795 vmap_table_offset, oat_file_.Size(),
796 oat_method.GetVmapTableOffsetOffset());
797 success = false;
798 } else if (options_->dump_vmap_) {
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +0100799 DumpVmap(*indent2_os, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700800 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800801 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700802 {
803 *indent1_os << "QuickMethodFrameInfo\n";
804
805 *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
806 *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
807 DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false);
808 *indent2_os << "\n";
809 *indent2_os << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
810 DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true);
811 *indent2_os << "\n";
812 }
813 {
814 // Based on spill masks from QuickMethodFrameInfo so placed
815 // after it is dumped, but useful for understanding quick
816 // code, so dumped here.
817 DumpVregLocations(*indent2_os, oat_method, code_item);
818 }
819 {
820 *indent1_os << "CODE: ";
821 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
822 if (code_size_offset > oat_file_.Size()) {
823 *indent2_os << StringPrintf("WARNING: "
824 "code size offset 0x%08x is past end of file 0x%08zx.",
825 code_size_offset, oat_file_.Size());
826 success = false;
827 } else {
828 const void* code = oat_method.GetQuickCode();
829 uint32_t code_size = oat_method.GetQuickCodeSize();
830 if (code == nullptr) {
831 code = oat_method.GetPortableCode();
832 code_size = oat_method.GetPortableCodeSize();
833 code_size_offset = 0;
834 }
835 uint32_t code_offset = oat_method.GetCodeOffset();
836 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
837 uint64_t aligned_code_end = aligned_code_begin + code_size;
838
839 if (options_->absolute_addresses_) {
840 *indent1_os << StringPrintf("%p ", code);
841 }
842 *indent1_os << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
843 code_offset,
844 code_size_offset,
845 code_size,
846 code != nullptr ? "..." : "");
847
848 if (aligned_code_begin > oat_file_.Size()) {
849 *indent2_os << StringPrintf("WARNING: "
850 "start of code at 0x%08x is past end of file 0x%08zx.",
851 aligned_code_begin, oat_file_.Size());
852 success = false;
853 } else if (aligned_code_end > oat_file_.Size()) {
854 *indent2_os << StringPrintf("WARNING: "
855 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
856 "code size is 0x%08x loaded from offset 0x%08x.\n",
857 aligned_code_end, oat_file_.Size(),
858 code_size, code_size_offset);
859 success = false;
860 if (options_->disassemble_code_) {
861 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
862 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
863 }
864 }
865 } else if (code_size > kMaxCodeSize) {
866 *indent2_os << StringPrintf("WARNING: "
867 "code size %d is bigger than max expected threshold of %d. "
868 "code size is 0x%08x loaded from offset 0x%08x.\n",
869 code_size, kMaxCodeSize,
870 code_size, code_size_offset);
871 success = false;
872 if (options_->disassemble_code_) {
873 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
874 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
875 }
876 }
877 } else if (options_->disassemble_code_) {
878 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, !success, 0);
879 }
880 }
881 }
882 os << std::flush;
883 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700884 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800885
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800886 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
887 if (spill_mask == 0) {
888 return;
889 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800890 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800891 for (size_t i = 0; i < 32; i++) {
892 if ((spill_mask & (1 << i)) != 0) {
893 if (is_float) {
894 os << "fr" << i;
895 } else {
896 os << "r" << i;
897 }
898 spill_mask ^= 1 << i; // clear bit
899 if (spill_mask != 0) {
900 os << ", ";
901 } else {
902 break;
903 }
904 }
905 }
906 os << ")";
907 }
908
Ian Rogersb23a7722012-10-09 16:54:26 -0700909 void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) {
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +0100910 // If the native GC map is null, then this method has been compiled with the
911 // optimizing compiler. The optimizing compiler currently outputs its stack map
912 // in the vmap table, and the code below does not work with such a stack map.
913 if (oat_method.GetNativeGcMap() == nullptr) {
914 return;
915 }
Ian Rogers1809a722013-08-09 22:05:32 -0700916 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700917 if (raw_table != nullptr) {
Ian Rogers1809a722013-08-09 22:05:32 -0700918 const VmapTable vmap_table(raw_table);
919 bool first = true;
920 bool processing_fp = false;
921 uint32_t spill_mask = oat_method.GetCoreSpillMask();
922 for (size_t i = 0; i < vmap_table.Size(); i++) {
923 uint16_t dex_reg = vmap_table[i];
924 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
925 processing_fp ? kFloatVReg : kIntVReg);
926 os << (first ? "v" : ", v") << dex_reg;
927 if (!processing_fp) {
928 os << "/r" << cpu_reg;
929 } else {
930 os << "/fr" << cpu_reg;
931 }
932 first = false;
933 if (!processing_fp && dex_reg == 0xFFFF) {
934 processing_fp = true;
935 spill_mask = oat_method.GetFpSpillMask();
936 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800937 }
Ian Rogers1809a722013-08-09 22:05:32 -0700938 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800939 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800940 }
941
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -0700942 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
943 const DexFile::CodeItem* code_item) {
944 if (code_item != nullptr) {
945 size_t num_locals_ins = code_item->registers_size_;
946 size_t num_ins = code_item->ins_size_;
947 size_t num_locals = num_locals_ins - num_ins;
948 size_t num_outs = code_item->outs_size_;
949
950 os << "vr_stack_locations:";
951 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
952 // For readability, delimit the different kinds of VRs.
953 if (reg == num_locals_ins) {
954 os << "\n\tmethod*:";
955 } else if (reg == num_locals && num_ins > 0) {
956 os << "\n\tins:";
957 } else if (reg == 0 && num_locals > 0) {
958 os << "\n\tlocals:";
959 }
960
961 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
962 oat_method.GetFpSpillMask(),
963 oat_method.GetFrameSizeInBytes(), reg,
964 GetInstructionSet());
965 os << " v" << reg << "[sp + #" << offset << "]";
966 }
967
968 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
969 if (out_reg == 0) {
970 os << "\n\touts:";
971 }
972
973 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
974 os << " v" << out_reg << "[sp + #" << offset << "]";
975 }
976
977 os << "\n";
978 }
979 }
980
Ian Rogersb23a7722012-10-09 16:54:26 -0700981 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800982 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -0700983 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700984 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700985 const VmapTable vmap_table(raw_table);
986 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -0700987 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800988 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
989 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
990 : oat_method.GetCoreSpillMask();
991 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -0700992 } else {
993 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
994 oat_method.GetFpSpillMask(),
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000995 oat_method.GetFrameSizeInBytes(), reg,
996 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -0700997 os << "[sp + #" << offset << "]";
998 }
999 }
1000 }
1001
Ian Rogersef7d42f2014-01-06 12:55:46 -08001002 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1003 const DexFile::CodeItem* code_item,
1004 size_t num_regs, const uint8_t* reg_bitmap) {
1005 bool first = true;
1006 for (size_t reg = 0; reg < num_regs; reg++) {
1007 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1008 if (first) {
1009 os << " v" << reg << " (";
1010 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1011 os << ")";
1012 first = false;
1013 } else {
1014 os << ", v" << reg << " (";
1015 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1016 os << ")";
1017 }
1018 }
1019 }
1020 if (first) {
1021 os << "No registers in GC map\n";
1022 } else {
1023 os << "\n";
1024 }
1025 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001026 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1027 const DexFile::CodeItem* code_item) {
1028 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001029 if (gc_map_raw == nullptr) {
1030 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001031 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001032 const void* quick_code = oat_method.GetQuickCode();
1033 if (quick_code != nullptr) {
1034 NativePcOffsetToReferenceMap map(gc_map_raw);
1035 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1036 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1037 map.GetNativePcOffset(entry);
1038 os << StringPrintf("%p", native_pc);
1039 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001040 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001041 } else {
1042 const void* portable_code = oat_method.GetPortableCode();
1043 CHECK(portable_code != nullptr);
1044 verifier::DexPcToReferenceMap map(gc_map_raw);
1045 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1046 uint32_t dex_pc = map.GetDexPc(entry);
1047 os << StringPrintf("0x%08x", dex_pc);
1048 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
1049 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001050 }
1051 }
1052
1053 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001054 const void* quick_code = oat_method.GetQuickCode();
1055 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001056 return;
1057 }
Ian Rogers1809a722013-08-09 22:05:32 -07001058 MappingTable table(oat_method.GetMappingTable());
1059 if (table.TotalSize() != 0) {
1060 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1061 std::ostream indent_os(&indent_filter);
1062 if (table.PcToDexSize() != 0) {
1063 typedef MappingTable::PcToDexIterator It;
1064 os << "suspend point mappings {\n";
1065 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1066 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1067 }
1068 os << "}\n";
1069 }
1070 if (table.DexToPcSize() != 0) {
1071 typedef MappingTable::DexToPcIterator It;
1072 os << "catch entry mappings {\n";
1073 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1074 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1075 }
1076 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001077 }
1078 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001079 }
1080
Ian Rogers1809a722013-08-09 22:05:32 -07001081 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1082 size_t offset, bool suspend_point_mapping) {
1083 MappingTable table(oat_method.GetMappingTable());
1084 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1085 typedef MappingTable::PcToDexIterator It;
1086 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1087 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001088 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001089 return cur.DexPc();
1090 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001091 }
Ian Rogers1809a722013-08-09 22:05:32 -07001092 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1093 typedef MappingTable::DexToPcIterator It;
1094 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1095 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001096 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001097 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001098 }
1099 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001100 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001101 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001102 }
1103
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001104 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1105 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001106 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001107 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001108 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001109 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001110 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001111 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001112 bool first = true;
1113 for (size_t reg = 0; reg < num_regs; reg++) {
1114 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1115 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001116 os << "GC map objects: v" << reg << " (";
1117 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001118 os << ")";
1119 first = false;
1120 } else {
1121 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001122 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001123 os << ")";
1124 }
1125 }
1126 }
1127 if (!first) {
1128 os << "\n";
1129 }
1130 }
1131 }
1132 }
1133
Mathieu Chartier590fee92013-09-13 13:46:47 -07001134 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1135 const OatFile::OatMethod& oat_method,
1136 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1137 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001138 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001139 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001140 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1141 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1142 if (kind != kUndefined) {
1143 if (first) {
1144 os << "VRegs: v";
1145 first = false;
1146 } else {
1147 os << ", v";
1148 }
1149 os << reg << " (";
1150 switch (kind) {
1151 case kImpreciseConstant:
1152 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1153 DescribeVReg(os, oat_method, code_item, reg, kind);
1154 break;
1155 case kConstant:
1156 os << "Constant: " << kinds.at((reg * 2) + 1);
1157 break;
1158 default:
1159 DescribeVReg(os, oat_method, code_item, reg, kind);
1160 break;
1161 }
1162 os << ")";
1163 }
1164 }
1165 if (!first) {
1166 os << "\n";
1167 }
1168 }
1169
1170
Ian Rogersb23a7722012-10-09 16:54:26 -07001171 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001172 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001173 size_t i = 0;
1174 while (i < code_item->insns_size_in_code_units_) {
1175 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001176 os << StringPrintf("0x%04zx: %s\n", i, instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001177 i += instruction->SizeInCodeUnits();
1178 }
1179 }
1180 }
1181
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001182 verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx,
1183 const DexFile* dex_file,
1184 const DexFile::ClassDef& class_def,
1185 const DexFile::CodeItem* code_item,
1186 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001187 if ((method_access_flags & kAccNative) == 0) {
1188 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001189 StackHandleScope<1> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001190 Handle<mirror::DexCache> dex_cache(
1191 hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
Andreas Gampe00b25f32014-09-17 21:49:05 -07001192 DCHECK(options_->class_loader_ != nullptr);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001193 return verifier::MethodVerifier::VerifyMethodAndDump(soa.Self(), os, dex_method_idx, dex_file,
1194 dex_cache,
Andreas Gampe00b25f32014-09-17 21:49:05 -07001195 *options_->class_loader_,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001196 &class_def, code_item,
1197 NullHandle<mirror::ArtMethod>(),
1198 method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001199 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001200
1201 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001202 }
1203
Mathieu Chartier590fee92013-09-13 13:46:47 -07001204 void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001205 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1206 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001207 const void* portable_code = oat_method.GetPortableCode();
1208 const void* quick_code = oat_method.GetQuickCode();
1209
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001210 if (code_size == 0) {
1211 code_size = oat_method.GetQuickCodeSize();
1212 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001213 if ((code_size == 0) || ((portable_code == nullptr) && (quick_code == nullptr))) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001214 os << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001215 return;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001216 } else if (quick_code != nullptr) {
1217 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1218 size_t offset = 0;
1219 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001220 if (!bad_input) {
1221 DumpMappingAtOffset(os, oat_method, offset, false);
1222 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001223 offset += disassembler_->Dump(os, quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001224 if (!bad_input) {
1225 uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
1226 if (dex_pc != DexFile::kDexNoIndex) {
1227 DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
1228 if (verifier != nullptr) {
1229 DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
1230 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001231 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001232 }
1233 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001234 } else {
1235 CHECK(portable_code != nullptr);
1236 CHECK_EQ(code_size, 0U); // TODO: disassembly of portable is currently not supported.
Ian Rogersb23a7722012-10-09 16:54:26 -07001237 }
1238 }
1239
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001240 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001241 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
1242 const OatDumperOptions* options_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001243 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001244 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001245};
1246
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001247class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001248 public:
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001249 explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001250 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
1251 : os_(os),
1252 image_space_(image_space),
1253 image_header_(image_header),
1254 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001255
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001256 bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001257 std::ostream& os = *os_;
1258 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001259
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001260 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001261
Mathieu Chartier31e89252013-08-28 11:29:12 -07001262 os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
1263 << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
1264
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001265 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001266
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001267 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001268
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001269 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1270
1271 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1272
1273 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001274
Alex Lighta59dd802014-07-02 16:28:08 -07001275 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1276
Igor Murashkin46774762014-10-22 11:37:02 -07001277 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1278
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001279 {
1280 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
1281 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1282 std::ostream indent1_os(&indent1_filter);
1283 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
1284 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1285 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1286 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001287 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001288 indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
1289 if (image_root_object->IsObjectArray()) {
1290 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
1291 std::ostream indent2_os(&indent2_filter);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001292 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001293 = image_root_object->AsObjectArray<mirror::Object>();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001294 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001295 mirror::Object* value = image_root_object_array->Get(i);
Ian Rogersfa824272013-11-05 16:12:57 -08001296 size_t run = 0;
1297 for (int32_t j = i + 1; j < image_root_object_array->GetLength(); j++) {
1298 if (value == image_root_object_array->Get(j)) {
1299 run++;
1300 } else {
1301 break;
1302 }
1303 }
1304 if (run == 0) {
1305 indent2_os << StringPrintf("%d: ", i);
1306 } else {
1307 indent2_os << StringPrintf("%d to %zd: ", i, i + run);
1308 i = i + run;
1309 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001310 if (value != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001311 PrettyObjectValue(indent2_os, value->GetClass(), value);
1312 } else {
1313 indent2_os << i << ": null\n";
1314 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001315 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001316 }
1317 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001318 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001319 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001320
Brian Carlstromaded5f72011-10-07 17:15:04 -07001321 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001322 std::string image_filename = image_space_.GetImageFilename();
1323 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001324 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001325 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001326 std::string error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -07001327 const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1328 if (oat_file == nullptr) {
Igor Murashkin46774762014-10-22 11:37:02 -07001329 oat_file = OatFile::Open(oat_location, oat_location, nullptr, nullptr, false, &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001330 if (oat_file == nullptr) {
1331 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001332 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001333 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001334 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001335 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001336
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001337 stats_.oat_file_bytes = oat_file->Size();
1338
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001339 oat_dumper_.reset(new OatDumper(*oat_file, oat_dumper_options_.release()));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001340
Mathieu Chartier02e25112013-08-14 16:14:24 -07001341 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001342 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001343 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1344 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001345 }
1346
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001347 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001348
1349 // Loop through all the image spaces and dump their objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001350 gc::Heap* heap = Runtime::Current()->GetHeap();
1351 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07001352 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001353 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001354 {
1355 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1356 heap->FlushAllocStack();
1357 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001358 // Since FlushAllocStack() above resets the (active) allocation
1359 // stack. Need to revoke the thread-local allocation stacks that
1360 // point into it.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001361 {
1362 self->TransitionFromRunnableToSuspended(kNative);
1363 ThreadList* thread_list = Runtime::Current()->GetThreadList();
1364 thread_list->SuspendAll();
1365 heap->RevokeAllThreadLocalAllocationStacks(self);
1366 thread_list->ResumeAll();
1367 self->TransitionFromSuspendedToRunnable();
1368 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001369 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001370 {
1371 std::ostream* saved_os = os_;
1372 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1373 std::ostream indent_os(&indent_filter);
1374 os_ = &indent_os;
1375 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001376 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001377 if (space->IsImageSpace()) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001378 gc::space::ImageSpace* image_space = space->AsImageSpace();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001379 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1380 indent_os << "\n";
1381 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001382 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001383 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001384 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001385 indent_os << "\n";
1386 os_ = saved_os;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001387 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001388 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001389 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001390 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001391 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001392 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001393 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001394 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001395 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001396 size_t header_bytes = sizeof(ImageHeader);
1397 stats_.header_bytes = header_bytes;
1398 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1399 stats_.alignment_bytes += alignment_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001400 stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
1401 stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001402 stats_.Dump(os);
1403 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001404
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001405 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001406
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001407 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001408 }
1409
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001410 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001411 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001412 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001413 CHECK(type != nullptr);
1414 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001415 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001416 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001417 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001418 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001419 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001420 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001421 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001422 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001423 } else if (type->IsArtFieldClass()) {
1424 mirror::ArtField* field = value->AsArtField();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001425 os << StringPrintf("%p Field: %s\n", field, PrettyField(field).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001426 } else if (type->IsArtMethodClass()) {
1427 mirror::ArtMethod* method = value->AsArtMethod();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001428 os << StringPrintf("%p Method: %s\n", method, PrettyMethod(method).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001429 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001430 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001431 }
1432 }
1433
Brian Carlstromea46f952013-07-30 01:26:50 -07001434 static void PrintField(std::ostream& os, mirror::ArtField* field, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001435 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001436 const char* descriptor = field->GetTypeDescriptor();
1437 os << StringPrintf("%s: ", field->GetName());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001438 if (descriptor[0] != 'L' && descriptor[0] != '[') {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001439 StackHandleScope<1> hs(Thread::Current());
1440 FieldHelper fh(hs.NewHandle(field));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001441 mirror::Class* type = fh.GetType();
Fred Shih37f05ef2014-07-16 18:38:08 -07001442 DCHECK(type->IsPrimitive());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001443 if (type->IsPrimitiveLong()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001444 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -07001445 } else if (type->IsPrimitiveDouble()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001446 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -07001447 } else if (type->IsPrimitiveFloat()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001448 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Fred Shih37f05ef2014-07-16 18:38:08 -07001449 } else if (type->IsPrimitiveInt()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001450 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Fred Shih37f05ef2014-07-16 18:38:08 -07001451 } else if (type->IsPrimitiveChar()) {
1452 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
1453 } else if (type->IsPrimitiveShort()) {
1454 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
1455 } else if (type->IsPrimitiveBoolean()) {
1456 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1457 field->GetBoolean(obj));
1458 } else if (type->IsPrimitiveByte()) {
1459 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
1460 } else {
1461 LOG(FATAL) << "Unknown type: " << PrettyClass(type);
Ian Rogers48efc2b2012-08-27 17:20:31 -07001462 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001463 } else {
Ian Rogers48efc2b2012-08-27 17:20:31 -07001464 // Get the value, don't compute the type unless it is non-null as we don't want
1465 // to cause class loading.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001466 mirror::Object* value = field->GetObj(obj);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001467 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001468 os << StringPrintf("null %s\n", PrettyDescriptor(descriptor).c_str());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001469 } else {
Ian Rogers50239c72013-06-17 14:53:22 -07001470 // Grab the field type without causing resolution.
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001471 StackHandleScope<1> hs(Thread::Current());
1472 FieldHelper fh(hs.NewHandle(field));
Ian Rogers50239c72013-06-17 14:53:22 -07001473 mirror::Class* field_type = fh.GetType(false);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001474 if (field_type != nullptr) {
Ian Rogers50239c72013-06-17 14:53:22 -07001475 PrettyObjectValue(os, field_type, value);
1476 } else {
1477 os << StringPrintf("%p %s\n", value, PrettyDescriptor(descriptor).c_str());
1478 }
Ian Rogers48efc2b2012-08-27 17:20:31 -07001479 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001480 }
1481 }
1482
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001483 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001484 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001485 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001486 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001487 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001488 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001489 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001490 if (fields != nullptr) {
Ian Rogersd5b32602012-02-26 16:40:04 -08001491 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001492 mirror::ArtField* field = fields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001493 PrintField(os, field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001494 }
1495 }
1496 }
1497
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001498 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001499 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001500 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001501
Ian Rogersef7d42f2014-01-06 12:55:46 -08001502 const void* GetQuickOatCodeBegin(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001503 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001504 const void* quick_code = m->GetEntryPointFromQuickCompiledCode();
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001505 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001506 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001507 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001508 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001509 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001510 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001511 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001512 }
1513
Ian Rogersef7d42f2014-01-06 12:55:46 -08001514 uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001515 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001516 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1517 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001518 return 0;
1519 }
1520 return oat_code_begin[-1];
1521 }
1522
Ian Rogersef7d42f2014-01-06 12:55:46 -08001523 const void* GetQuickOatCodeEnd(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001524 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001525 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001526 if (oat_code_begin == nullptr) {
1527 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001528 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001529 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001530 }
1531
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001532 static void Callback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001533 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001534 DCHECK(obj != nullptr);
1535 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001536 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001537 if (!state->InDumpSpace(obj)) {
1538 return;
1539 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001540
1541 size_t object_bytes = obj->SizeOf();
1542 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1543 state->stats_.object_bytes += object_bytes;
1544 state->stats_.alignment_bytes += alignment_bytes;
1545
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001546 std::ostream& os = *state->os_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001547 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001548 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001549 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1550 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001551 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001552 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001553 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1554 << klass->GetStatus() << ")\n";
Brian Carlstromea46f952013-07-30 01:26:50 -07001555 } else if (obj->IsArtField()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001556 os << StringPrintf("%p: java.lang.reflect.ArtField %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001557 PrettyField(obj->AsArtField()).c_str());
1558 } else if (obj->IsArtMethod()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001559 os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001560 PrettyMethod(obj->AsArtMethod()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001561 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001562 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001563 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001564 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001565 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001566 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001567 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1568 std::ostream indent_os(&indent_filter);
1569 DumpFields(indent_os, obj, obj_class);
Ian Rogersd5b32602012-02-26 16:40:04 -08001570 if (obj->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001571 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Ian Rogersd5b32602012-02-26 16:40:04 -08001572 int32_t length = obj_array->GetLength();
1573 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001574 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001575 size_t run = 0;
1576 for (int32_t j = i + 1; j < length; j++) {
1577 if (value == obj_array->Get(j)) {
1578 run++;
1579 } else {
1580 break;
1581 }
1582 }
1583 if (run == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001584 indent_os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001585 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001586 indent_os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001587 i = i + run;
1588 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001589 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001590 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001591 PrettyObjectValue(indent_os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001592 }
1593 } else if (obj->IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001594 mirror::ObjectArray<mirror::ArtField>* sfields = obj->AsClass()->GetSFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001595 if (sfields != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001596 indent_os << "STATICS:\n";
1597 Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
1598 std::ostream indent2_os(&indent2_filter);
Ian Rogersd5b32602012-02-26 16:40:04 -08001599 for (int32_t i = 0; i < sfields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001600 mirror::ArtField* field = sfields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001601 PrintField(indent2_os, field, field->GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001602 }
1603 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001604 } else if (obj->IsArtMethod()) {
1605 mirror::ArtMethod* method = obj->AsArtMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001606 if (method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001607 // TODO: portable dumping.
1608 DCHECK(method->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1609 DCHECK(method->GetMappingTable() == nullptr) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001610 bool first_occurrence;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001611 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1612 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1613 state->ComputeOatSize(quick_oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001614 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001615 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001616 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001617 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCode()) {
1618 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001619 }
Ian Rogers19846512012-02-24 11:42:47 -08001620 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
Jeff Hao88474b42013-10-23 16:24:40 -07001621 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001622 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001623 DCHECK(method->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1624 DCHECK(method->GetMappingTable() == nullptr) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001625 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001626 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -07001627 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001628 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001629
Elliott Hughesa0e18062012-04-13 15:59:59 -07001630 bool first_occurrence;
Ian Rogers0c7abda2012-09-19 13:33:42 -07001631 size_t gc_map_bytes = state->ComputeOatSize(method->GetNativeGcMap(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001632 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001633 state->stats_.gc_map_bytes += gc_map_bytes;
1634 }
1635
1636 size_t pc_mapping_table_bytes =
Ian Rogers1809a722013-08-09 22:05:32 -07001637 state->ComputeOatSize(method->GetMappingTable(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001638 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001639 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1640 }
1641
1642 size_t vmap_table_bytes =
Ian Rogers1809a722013-08-09 22:05:32 -07001643 state->ComputeOatSize(method->GetVmapTable(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001644 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001645 state->stats_.vmap_table_bytes += vmap_table_bytes;
1646 }
1647
Ian Rogersef7d42f2014-01-06 12:55:46 -08001648 // TODO: portable dumping.
1649 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1650 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1651 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1652 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001653 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001654 state->stats_.managed_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001655 if (method->IsConstructor()) {
1656 if (method->IsStatic()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001657 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001658 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001659 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001660 }
1661 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001662 state->stats_.large_method_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001663 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001664 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001665 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001666
Ian Rogersef7d42f2014-01-06 12:55:46 -08001667 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001668 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
1669 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001670
1671 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersef7d42f2014-01-06 12:55:46 -08001672 vmap_table_bytes + quick_oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001673
1674 double expansion =
Ian Rogersef7d42f2014-01-06 12:55:46 -08001675 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001676 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001677 }
1678 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001679 std::string temp;
1680 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001681 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001682
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001683 std::set<const void*> already_seen_;
1684 // Compute the size of the given data within the oat file and whether this is the first time
1685 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001686 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001687 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001688 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001689 already_seen_.insert(oat_data);
1690 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001691 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001692 }
1693 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001694 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001695
1696 public:
1697 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001698 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001699 size_t file_bytes;
1700
1701 size_t header_bytes;
1702 size_t object_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001703 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001704 size_t alignment_bytes;
1705
1706 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001707 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001708 size_t managed_to_native_code_bytes;
1709 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001710 size_t class_initializer_code_bytes;
1711 size_t large_initializer_code_bytes;
1712 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001713
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001714 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001715 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001716 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001717
1718 size_t dex_instruction_bytes;
1719
Brian Carlstromea46f952013-07-30 01:26:50 -07001720 std::vector<mirror::ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001721 std::vector<size_t> method_outlier_size;
1722 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07001723 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001724
1725 explicit Stats()
1726 : oat_file_bytes(0),
1727 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001728 header_bytes(0),
1729 object_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07001730 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001731 alignment_bytes(0),
1732 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001733 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001734 managed_to_native_code_bytes(0),
1735 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001736 class_initializer_code_bytes(0),
1737 large_initializer_code_bytes(0),
1738 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001739 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001740 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001741 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001742 dex_instruction_bytes(0) {}
1743
Elliott Hughesa0e18062012-04-13 15:59:59 -07001744 struct SizeAndCount {
1745 SizeAndCount(size_t bytes, size_t count) : bytes(bytes), count(count) {}
1746 size_t bytes;
1747 size_t count;
1748 };
1749 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1750 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001751
Ian Rogersfc0e94b2013-09-23 23:51:32 -07001752 void Update(const char* descriptor, size_t object_bytes) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001753 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1754 if (it != sizes_and_counts.end()) {
1755 it->second.bytes += object_bytes;
1756 it->second.count += 1;
1757 } else {
1758 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes, 1));
1759 }
1760 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001761
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001762 double PercentOfOatBytes(size_t size) {
1763 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1764 }
1765
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001766 double PercentOfFileBytes(size_t size) {
1767 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1768 }
1769
1770 double PercentOfObjectBytes(size_t size) {
1771 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1772 }
1773
Brian Carlstromea46f952013-07-30 01:26:50 -07001774 void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001775 method_outlier_size.push_back(total_size);
1776 method_outlier_expansion.push_back(expansion);
1777 method_outlier.push_back(method);
1778 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001779
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001780 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001781 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001782 size_t sum_of_sizes = 0;
1783 size_t sum_of_sizes_squared = 0;
1784 size_t sum_of_expansion = 0;
1785 size_t sum_of_expansion_squared = 0;
1786 size_t n = method_outlier_size.size();
1787 for (size_t i = 0; i < n; i++) {
1788 size_t cur_size = method_outlier_size[i];
1789 sum_of_sizes += cur_size;
1790 sum_of_sizes_squared += cur_size * cur_size;
1791 double cur_expansion = method_outlier_expansion[i];
1792 sum_of_expansion += cur_expansion;
1793 sum_of_expansion_squared += cur_expansion * cur_expansion;
1794 }
1795 size_t size_mean = sum_of_sizes / n;
1796 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1797 double expansion_mean = sum_of_expansion / n;
1798 double expansion_variance =
1799 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1800
1801 // Dump methods whose size is a certain number of standard deviations from the mean
1802 size_t dumped_values = 0;
1803 size_t skipped_values = 0;
1804 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1805 size_t cur_size_variance = i * i * size_variance;
1806 bool first = true;
1807 for (size_t j = 0; j < n; j++) {
1808 size_t cur_size = method_outlier_size[j];
1809 if (cur_size > size_mean) {
1810 size_t cur_var = cur_size - size_mean;
1811 cur_var = cur_var * cur_var;
1812 if (cur_var > cur_size_variance) {
1813 if (dumped_values > 20) {
1814 if (i == 1) {
1815 skipped_values++;
1816 } else {
1817 i = 2; // jump to counting for 1 standard deviation
1818 break;
1819 }
1820 } else {
1821 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001822 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001823 first = false;
1824 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001825 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001826 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001827 method_outlier_size[j] = 0; // don't consider this method again
1828 dumped_values++;
1829 }
1830 }
1831 }
1832 }
1833 }
1834 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001835 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001836 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001837 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001838 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001839
1840 // Dump methods whose expansion is a certain number of standard deviations from the mean
1841 dumped_values = 0;
1842 skipped_values = 0;
1843 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1844 double cur_expansion_variance = i * i * expansion_variance;
1845 bool first = true;
1846 for (size_t j = 0; j < n; j++) {
1847 double cur_expansion = method_outlier_expansion[j];
1848 if (cur_expansion > expansion_mean) {
1849 size_t cur_var = cur_expansion - expansion_mean;
1850 cur_var = cur_var * cur_var;
1851 if (cur_var > cur_expansion_variance) {
1852 if (dumped_values > 20) {
1853 if (i == 1) {
1854 skipped_values++;
1855 } else {
1856 i = 2; // jump to counting for 1 standard deviation
1857 break;
1858 }
1859 } else {
1860 if (first) {
1861 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001862 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001863 first = false;
1864 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001865 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001866 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001867 method_outlier_expansion[j] = 0.0; // don't consider this method again
1868 dumped_values++;
1869 }
1870 }
1871 }
1872 }
1873 }
1874 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001875 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001876 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001877 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001878 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001879 }
1880
Ian Rogersb726dcb2012-09-05 08:57:23 -07001881 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001882 {
1883 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1884 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
1885 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1886 std::ostream indent_os(&indent_filter);
1887 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
1888 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier32327092013-08-30 14:04:08 -07001889 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001890 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
1891 header_bytes, PercentOfFileBytes(header_bytes),
1892 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07001893 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001894 alignment_bytes, PercentOfFileBytes(alignment_bytes))
1895 << std::flush;
Mathieu Chartier32327092013-08-30 14:04:08 -07001896 CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001897 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001898
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001899 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001900 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07001901 for (const auto& sizes_and_count : sizes_and_counts) {
1902 const std::string& descriptor(sizes_and_count.first);
1903 double average = static_cast<double>(sizes_and_count.second.bytes) /
1904 static_cast<double>(sizes_and_count.second.count);
1905 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001906 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001907 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001908 descriptor.c_str(), sizes_and_count.second.bytes,
1909 sizes_and_count.second.count, average, percent);
1910 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001911 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001912 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001913 CHECK_EQ(object_bytes, object_bytes_total);
1914
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001915 os << StringPrintf("oat_file_bytes = %8zd\n"
1916 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1917 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1918 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1919 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1920 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1921 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001922 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001923 managed_code_bytes,
1924 PercentOfOatBytes(managed_code_bytes),
1925 managed_to_native_code_bytes,
1926 PercentOfOatBytes(managed_to_native_code_bytes),
1927 native_to_managed_code_bytes,
1928 PercentOfOatBytes(native_to_managed_code_bytes),
1929 class_initializer_code_bytes,
1930 PercentOfOatBytes(class_initializer_code_bytes),
1931 large_initializer_code_bytes,
1932 PercentOfOatBytes(large_initializer_code_bytes),
1933 large_method_code_bytes,
1934 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001935 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07001936 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001937 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001938 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
1939 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07001940 }
1941
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001942 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1943 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1944 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001945 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1946 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1947 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001948 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001949
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001950 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
1951 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001952 static_cast<double>(managed_code_bytes) /
1953 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07001954 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001955 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001956 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001957
1958 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001959 }
1960 } stats_;
1961
1962 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001963 enum {
1964 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1965 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1966 kLargeConstructorDexBytes = 4000,
1967 // Number of bytes for a method to be considered large. Based on the 4000 basic block
1968 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1969 kLargeMethodDexBytes = 16000
1970 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001971 std::ostream* os_;
Ian Rogers1d54e732013-05-02 21:10:01 -07001972 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001973 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001974 std::unique_ptr<OatDumper> oat_dumper_;
1975 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001976
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001977 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001978};
1979
Andreas Gampe00b25f32014-09-17 21:49:05 -07001980static NoopCompilerCallbacks callbacks;
Elliott Hughes72395bf2012-04-24 13:45:26 -07001981
Andreas Gampe00b25f32014-09-17 21:49:05 -07001982static Runtime* StartRuntime(const char* boot_image_location, const char* image_location,
1983 InstructionSet instruction_set) {
Ian Rogerse63db272014-07-15 15:36:11 -07001984 RuntimeOptions options;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001985 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001986 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001987 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001988 std::string boot_oat_option;
Brian Carlstrom6c871802013-07-17 14:25:35 -07001989
1990 // We are more like a compiler than a run-time. We don't want to execute code.
Brian Carlstromc0a1b182014-03-04 23:19:06 -08001991 options.push_back(std::make_pair("compilercallbacks", &callbacks));
Brian Carlstrom6c871802013-07-17 14:25:35 -07001992
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001993 if (boot_image_location != nullptr) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001994 boot_image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001995 boot_image_option += boot_image_location;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001996 options.push_back(std::make_pair(boot_image_option.c_str(), nullptr));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001997 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001998 if (image_location != nullptr) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001999 image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002000 image_option += image_location;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002001 options.push_back(std::make_pair(image_option.c_str(), nullptr));
Brian Carlstromaded5f72011-10-07 17:15:04 -07002002 }
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002003 options.push_back(
2004 std::make_pair("imageinstructionset",
2005 reinterpret_cast<const void*>(GetInstructionSetString(instruction_set))));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07002006
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002007 if (!Runtime::Create(options, false)) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07002008 fprintf(stderr, "Failed to create runtime\n");
Andreas Gampe00b25f32014-09-17 21:49:05 -07002009 return nullptr;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002010 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002011
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002012 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
Ian Rogersb9beb2e2014-05-09 16:57:40 -07002013 // give it away now and then switch to a more manageable ScopedObjectAccess.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002014 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002015
2016 return Runtime::Current();
2017}
2018
2019static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
2020 std::ostream* os) {
2021 // Dumping the image, no explicit class loader.
2022 NullHandle<mirror::ClassLoader> null_class_loader;
2023 options->class_loader_ = &null_class_loader;
2024
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002025 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002026 gc::Heap* heap = runtime->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -07002027 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002028 CHECK(image_space != nullptr);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002029 const ImageHeader& image_header = image_space->GetImageHeader();
2030 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002031 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002032 return EXIT_FAILURE;
2033 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002034 ImageDumper image_dumper(os, *image_space, image_header, options);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002035 bool success = image_dumper.Dump();
2036 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002037}
2038
Andreas Gampe00b25f32014-09-17 21:49:05 -07002039static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2040 std::ostream* os) {
2041 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2042
2043 Thread* self = Thread::Current();
2044 CHECK(self != nullptr);
2045 // Need well-known-classes.
2046 WellKnownClasses::Init(self->GetJniEnv());
2047
2048 // Need to register dex files to get a working dex cache.
2049 ScopedObjectAccess soa(self);
2050 ClassLinker* class_linker = runtime->GetClassLinker();
2051 class_linker->RegisterOatFile(oat_file);
2052 std::vector<const DexFile*> dex_files;
2053 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2054 std::string error_msg;
2055 const DexFile* dex_file = odf->OpenDexFile(&error_msg);
2056 CHECK(dex_file != nullptr) << error_msg;
2057 class_linker->RegisterDexFile(*dex_file);
2058 dex_files.push_back(dex_file);
2059 }
2060
2061 // Need a class loader.
2062 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader);
2063 ScopedLocalRef<jobject> class_loader_local(soa.Env(),
2064 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
2065 jobject class_loader = soa.Env()->NewGlobalRef(class_loader_local.get());
2066 // Fake that we're a compiler.
2067 runtime->SetCompileTimeClassPath(class_loader, dex_files);
2068
2069 // Use the class loader while dumping.
2070 StackHandleScope<1> scope(self);
2071 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2072 soa.Decode<mirror::ClassLoader*>(class_loader));
2073 options->class_loader_ = &loader_handle;
2074
2075 OatDumper oat_dumper(*oat_file, options);
2076 bool success = oat_dumper.Dump(*os);
2077 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2078}
2079
2080static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
2081 // No image = no class loader.
2082 NullHandle<mirror::ClassLoader> null_class_loader;
2083 options->class_loader_ = &null_class_loader;
2084
2085 OatDumper oat_dumper(*oat_file, options);
2086 bool success = oat_dumper.Dump(*os);
2087 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2088}
2089
2090static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2091 std::ostream* os) {
2092 std::string error_msg;
Igor Murashkin46774762014-10-22 11:37:02 -07002093 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002094 if (oat_file == nullptr) {
2095 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2096 return EXIT_FAILURE;
2097 }
2098
2099 if (runtime != nullptr) {
2100 return DumpOatWithRuntime(runtime, oat_file, options, os);
2101 } else {
2102 return DumpOatWithoutRuntime(oat_file, options, os);
2103 }
2104}
2105
2106static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2107 std::string error_msg;
Igor Murashkin46774762014-10-22 11:37:02 -07002108 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002109 if (oat_file == nullptr) {
2110 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2111 return EXIT_FAILURE;
2112 }
2113
2114 OatSymbolizer oat_symbolizer(oat_file, output_name);
2115 if (!oat_symbolizer.Init()) {
2116 fprintf(stderr, "Failed to initialize symbolizer\n");
2117 return EXIT_FAILURE;
2118 }
2119 if (!oat_symbolizer.Symbolize()) {
2120 fprintf(stderr, "Failed to symbolize\n");
2121 return EXIT_FAILURE;
2122 }
2123
2124 return EXIT_SUCCESS;
2125}
2126
2127struct OatdumpArgs {
2128 bool Parse(int argc, char** argv) {
2129 // Skip over argv[0].
2130 argv++;
2131 argc--;
2132
2133 if (argc == 0) {
2134 fprintf(stderr, "No arguments specified\n");
2135 usage();
2136 return false;
2137 }
2138
2139 for (int i = 0; i < argc; i++) {
2140 const StringPiece option(argv[i]);
2141 if (option.starts_with("--oat-file=")) {
2142 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2143 } else if (option.starts_with("--image=")) {
2144 image_location_ = option.substr(strlen("--image=")).data();
2145 } else if (option.starts_with("--boot-image=")) {
2146 boot_image_location_ = option.substr(strlen("--boot-image=")).data();
2147 } else if (option.starts_with("--instruction-set=")) {
2148 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
2149 instruction_set_ = GetInstructionSetFromString(instruction_set_str.data());
2150 if (instruction_set_ == kNone) {
2151 fprintf(stderr, "Unsupported instruction set %s\n", instruction_set_str.data());
2152 usage();
2153 return false;
2154 }
2155 } else if (option =="--dump:raw_mapping_table") {
2156 dump_raw_mapping_table_ = true;
2157 } else if (option == "--dump:raw_gc_map") {
2158 dump_raw_gc_map_ = true;
2159 } else if (option == "--no-dump:vmap") {
2160 dump_vmap_ = false;
2161 } else if (option == "--no-disassemble") {
2162 disassemble_code_ = false;
2163 } else if (option.starts_with("--output=")) {
2164 output_name_ = option.substr(strlen("--output=")).ToString();
2165 const char* filename = output_name_.c_str();
2166 out_.reset(new std::ofstream(filename));
2167 if (!out_->good()) {
2168 fprintf(stderr, "Failed to open output filename %s\n", filename);
2169 usage();
2170 return false;
2171 }
2172 os_ = out_.get();
2173 } else if (option.starts_with("--symbolize=")) {
2174 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2175 symbolize_ = true;
2176 } else {
2177 fprintf(stderr, "Unknown argument %s\n", option.data());
2178 usage();
2179 return false;
2180 }
2181 }
2182
2183 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2184 fprintf(stderr, "Either --image or --oat must be specified\n");
2185 return false;
2186 }
2187
2188 if (image_location_ != nullptr && oat_filename_ != nullptr) {
2189 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
2190 return false;
2191 }
2192
2193 return true;
2194 }
2195
2196 const char* oat_filename_ = nullptr;
2197 const char* image_location_ = nullptr;
2198 const char* boot_image_location_ = nullptr;
2199 InstructionSet instruction_set_ = kRuntimeISA;
2200 std::string elf_filename_prefix_;
2201 std::ostream* os_ = &std::cout;
2202 std::unique_ptr<std::ofstream> out_;
2203 std::string output_name_;
2204 bool dump_raw_mapping_table_ = false;
2205 bool dump_raw_gc_map_ = false;
2206 bool dump_vmap_ = true;
2207 bool disassemble_code_ = true;
2208 bool symbolize_ = false;
2209};
2210
2211static int oatdump(int argc, char** argv) {
2212 InitLogging(argv);
2213
2214 OatdumpArgs args;
2215 if (!args.Parse(argc, argv)) {
2216 return EXIT_FAILURE;
2217 }
2218
2219 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2220 bool absolute_addresses = (args.oat_filename_ == nullptr);
2221
2222 std::unique_ptr<OatDumperOptions> oat_dumper_options(new OatDumperOptions(
2223 args.dump_raw_mapping_table_,
2224 args.dump_raw_gc_map_,
2225 args.dump_vmap_,
2226 args.disassemble_code_,
2227 absolute_addresses,
2228 nullptr));
2229
2230 std::unique_ptr<Runtime> runtime;
2231 if ((args.boot_image_location_ != nullptr || args.image_location_ != nullptr) &&
2232 !args.symbolize_) {
2233 // If we have a boot image option, try to start the runtime; except when just symbolizing.
2234 runtime.reset(StartRuntime(args.boot_image_location_,
2235 args.image_location_,
2236 args.instruction_set_));
Mathieu Chartierd424d082014-10-15 10:31:46 -07002237 } else {
2238 MemMap::Init();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002239 }
2240
2241 if (args.oat_filename_ != nullptr) {
2242 if (args.symbolize_) {
2243 return SymbolizeOat(args.oat_filename_, args.output_name_);
2244 } else {
2245 return DumpOat(runtime.get(), args.oat_filename_, oat_dumper_options.release(), args.os_);
2246 }
2247 }
2248
2249 if (runtime.get() == nullptr) {
2250 // We need the runtime when printing an image.
2251 return EXIT_FAILURE;
2252 }
2253
2254 return DumpImage(runtime.get(), args.image_location_, oat_dumper_options.release(), args.os_);
2255}
2256
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002257} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002258
2259int main(int argc, char** argv) {
2260 return art::oatdump(argc, argv);
2261}