blob: 931ee562f31428660ff2a482b1b618c6775904d6 [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>
23#include <vector>
24
Elliott Hughese222ee02012-12-13 14:41:43 -080025#include "base/stringpiece.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070027#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080030#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080031#include "disassembler.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070032#include "field_helper.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080033#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034#include "gc/space/image_space.h"
35#include "gc/space/large_object_space.h"
36#include "gc/space/space-inl.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070037#include "image.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080038#include "indenter.h"
Ian Rogers1809a722013-08-09 22:05:32 -070039#include "mapping_table.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070040#include "mirror/art_field-inl.h"
41#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "mirror/array-inl.h"
43#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044#include "mirror/object-inl.h"
45#include "mirror/object_array-inl.h"
Brian Carlstromc0a1b182014-03-04 23:19:06 -080046#include "noop_compiler_callbacks.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080047#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010048#include "oat_file-inl.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080049#include "os.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070050#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070051#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070052#include "scoped_thread_state_change.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080053#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080054#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080055#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070056#include "vmap_table.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070057
58namespace art {
59
60static void usage() {
61 fprintf(stderr,
62 "Usage: oatdump [options] ...\n"
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +000063 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080064 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070065 "\n");
66 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080067 " --oat-file=<file.oat>: specifies an input oat filename.\n"
TDYa127a0a2a6c2012-10-16 22:47:38 -070068 " Example: --oat-file=/system/framework/boot.oat\n"
Brian Carlstromaded5f72011-10-07 17:15:04 -070069 "\n");
70 fprintf(stderr,
71 " --image=<file.art>: specifies an input image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080072 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070073 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070074 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070075 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080076 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070077 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070078 fprintf(stderr,
Brian Carlstrom0f5baa02014-05-22 11:54:18 -070079 " --instruction-set=(arm|arm64|mips|x86|x86_64): for locating the image file based on the image location\n"
80 " set.\n"
81 " Example: --instruction-set=x86\n"
82 " Default: %s\n"
83 "\n",
84 GetInstructionSetString(kRuntimeISA));
85 fprintf(stderr,
Brian Carlstrom27ec9612011-09-19 20:20:38 -070086 " --output=<file> may be used to send the output to a file.\n"
87 " Example: --output=/tmp/oatdump.txt\n"
88 "\n");
Dave Allison404f59f2014-02-24 11:10:01 -080089 fprintf(stderr,
90 " --dump:[raw_mapping_table|raw_gc_map]\n"
91 " Example: --dump:raw_gc_map\n"
92 " Default: neither\n"
93 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070094 exit(EXIT_FAILURE);
95}
96
Ian Rogersff1ed472011-09-20 13:46:24 -070097const char* image_roots_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080098 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070099 "kImtConflictMethod",
100 "kDefaultImt",
Brian Carlstrome24fa612011-09-29 00:53:55 -0700101 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -0700102 "kRefsOnlySaveMethod",
103 "kRefsAndArgsSaveMethod",
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700104 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700105 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700106};
107
Elliott Hughese3c845c2012-02-28 17:23:01 -0800108class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700109 public:
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000110 explicit OatDumper(const OatFile& oat_file, bool dump_raw_mapping_table, bool dump_raw_gc_map)
111 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800112 oat_dex_files_(oat_file.GetOatDexFiles()),
Dave Allison404f59f2014-02-24 11:10:01 -0800113 dump_raw_mapping_table_(dump_raw_mapping_table),
114 dump_raw_gc_map_(dump_raw_gc_map),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800115 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet())) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800116 AddAllOffsets();
117 }
118
119 void Dump(std::ostream& os) {
120 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700121
122 os << "MAGIC:\n";
123 os << oat_header.GetMagic() << "\n\n";
124
125 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800126 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700127
Elliott Hughesa72ec822012-03-05 17:12:22 -0800128 os << "INSTRUCTION SET:\n";
129 os << oat_header.GetInstructionSet() << "\n\n";
130
Dave Allison70202782013-10-22 17:52:19 -0700131 os << "INSTRUCTION SET FEATURES:\n";
132 os << oat_header.GetInstructionSetFeatures().GetFeatureString() << "\n\n";
133
Brian Carlstromaded5f72011-10-07 17:15:04 -0700134 os << "DEX FILE COUNT:\n";
135 os << oat_header.GetDexFileCount() << "\n\n";
136
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800137#define DUMP_OAT_HEADER_OFFSET(label, offset) \
138 os << label " OFFSET:\n"; \
139 os << StringPrintf("0x%08x", oat_header.offset()); \
140 if (oat_header.offset() != 0) { \
141 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
142 } \
143 os << StringPrintf("\n\n");
144
145 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
146 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
147 GetInterpreterToInterpreterBridgeOffset);
148 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
149 GetInterpreterToCompiledCodeBridgeOffset);
150 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
151 GetJniDlsymLookupOffset);
152 DUMP_OAT_HEADER_OFFSET("PORTABLE IMT CONFLICT TRAMPOLINE",
153 GetPortableImtConflictTrampolineOffset);
154 DUMP_OAT_HEADER_OFFSET("PORTABLE RESOLUTION TRAMPOLINE",
155 GetPortableResolutionTrampolineOffset);
156 DUMP_OAT_HEADER_OFFSET("PORTABLE TO INTERPRETER BRIDGE",
157 GetPortableToInterpreterBridgeOffset);
158 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
159 GetQuickGenericJniTrampolineOffset);
160 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
161 GetQuickImtConflictTrampolineOffset);
162 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
163 GetQuickResolutionTrampolineOffset);
164 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
165 GetQuickToInterpreterBridgeOffset);
166#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700167
Alex Lighta59dd802014-07-02 16:28:08 -0700168 os << "IMAGE PATCH DELTA:\n" << oat_header.GetImagePatchDelta();
169
Brian Carlstrom28db0122012-10-18 16:20:41 -0700170 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
171 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
172
173 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800174 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700175
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700176 // Print the key-value store.
177 {
178 os << "KEY VALUE STORE:\n";
179 size_t index = 0;
180 const char* key;
181 const char* value;
182 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
183 os << key << " = " << value << "\n";
184 index++;
185 }
186 os << "\n";
187 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700188
Ian Rogers30fab402012-01-23 15:43:46 -0800189 os << "BEGIN:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800190 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700191
Ian Rogers30fab402012-01-23 15:43:46 -0800192 os << "END:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800193 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700194
195 os << std::flush;
196
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800197 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
198 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Brian Carlstromaded5f72011-10-07 17:15:04 -0700199 CHECK(oat_dex_file != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800200 DumpOatDexFile(os, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700201 }
202 }
203
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800204 size_t ComputeSize(const void* oat_data) {
205 if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() ||
206 reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) {
207 return 0; // Address not in oat file
208 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800209 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
210 reinterpret_cast<uintptr_t>(oat_file_.Begin());
211 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800212 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800213 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800214 return end_offset - begin_offset;
215 }
216
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700217 InstructionSet GetInstructionSet() {
218 return oat_file_.GetOatHeader().GetInstructionSet();
219 }
220
Ian Rogersef7d42f2014-01-06 12:55:46 -0800221 const void* GetQuickOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800222 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
223 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700224 CHECK(oat_dex_file != nullptr);
225 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700226 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700227 if (dex_file.get() == nullptr) {
228 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
229 << "': " << error_msg;
230 } else {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700231 const DexFile::ClassDef* class_def =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700232 dex_file->FindClassDef(m->GetDeclaringClassDescriptor());
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700233 if (class_def != NULL) {
234 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100235 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800236 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100237 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800238 }
239 }
240 }
241 return NULL;
242 }
243
Brian Carlstromaded5f72011-10-07 17:15:04 -0700244 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800245 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800246 // We don't know the length of the code for each method, but we need to know where to stop
247 // when disassembling. What we do know is that a region of code will be followed by some other
248 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
249 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800250 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
251 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800252 CHECK(oat_dex_file != NULL);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700253 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700254 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700255 if (dex_file.get() == nullptr) {
256 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
257 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800258 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800259 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800260 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800261 for (size_t class_def_index = 0;
262 class_def_index < dex_file->NumClassDefs();
263 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800264 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100265 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800266 const byte* class_data = dex_file->GetClassData(class_def);
267 if (class_data != NULL) {
268 ClassDataItemIterator it(*dex_file, class_data);
269 SkipAllFields(it);
270 uint32_t class_method_index = 0;
271 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100272 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800273 it.Next();
274 }
275 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100276 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800277 it.Next();
278 }
279 }
280 }
281 }
282
283 // If the last thing in the file is code for a method, there won't be an offset for the "next"
284 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
285 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800286 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800287 }
288
289 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800290 uint32_t code_offset = oat_method.GetCodeOffset();
291 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
292 code_offset &= ~0x1;
293 }
294 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800295 offsets_.insert(oat_method.GetMappingTableOffset());
296 offsets_.insert(oat_method.GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700297 offsets_.insert(oat_method.GetNativeGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800298 }
299
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800300 void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700301 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800302 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800303 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700304
305 // Create the verifier early.
306
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700307 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700308 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800309 if (dex_file.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700310 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700311 return;
312 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800313 for (size_t class_def_index = 0;
314 class_def_index < dex_file->NumClassDefs();
315 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700316 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
317 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100318 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstromba150c32013-08-27 17:31:03 -0700319 os << StringPrintf("%zd: %s (type_idx=%d)", class_def_index, descriptor, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100320 << " (" << oat_class.GetStatus() << ")"
321 << " (" << oat_class.GetType() << ")\n";
322 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800323 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
324 std::ostream indented_os(&indent_filter);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100325 DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700326 }
327
328 os << std::flush;
329 }
330
Elliott Hughese3c845c2012-02-28 17:23:01 -0800331 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700332 while (it.HasNextStaticField()) {
333 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700334 }
Ian Rogers0571d352011-11-03 19:51:38 -0700335 while (it.HasNextInstanceField()) {
336 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700337 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800338 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700339
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800340 void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
341 const DexFile::ClassDef& class_def) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800342 const byte* class_data = dex_file.GetClassData(class_def);
343 if (class_data == NULL) { // empty class such as a marker interface?
344 return;
345 }
346 ClassDataItemIterator it(dex_file, class_data);
347 SkipAllFields(it);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800348 uint32_t class_method_idx = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700349 while (it.HasNextDirectMethod()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800350 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700351 DumpOatMethod(os, class_def, class_method_idx, oat_method, dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800352 it.GetMemberIndex(), it.GetMethodCodeItem(), it.GetMemberAccessFlags());
353 class_method_idx++;
Ian Rogers0571d352011-11-03 19:51:38 -0700354 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700355 }
Ian Rogers0571d352011-11-03 19:51:38 -0700356 while (it.HasNextVirtualMethod()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800357 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700358 DumpOatMethod(os, class_def, class_method_idx, oat_method, dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800359 it.GetMemberIndex(), it.GetMethodCodeItem(), it.GetMemberAccessFlags());
360 class_method_idx++;
Ian Rogers0571d352011-11-03 19:51:38 -0700361 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700362 }
Ian Rogers0571d352011-11-03 19:51:38 -0700363 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700364 os << std::flush;
365 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800366
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700367 void DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
368 uint32_t class_method_index,
Elliott Hughese3c845c2012-02-28 17:23:01 -0800369 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800370 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
371 uint32_t method_access_flags) {
372 os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700373 class_method_index, PrettyMethod(dex_method_idx, dex_file, true).c_str(),
374 dex_method_idx);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800375 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700376 std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter));
377 Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count);
378 std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter));
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800379 {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700380 *indent1_os << "DEX CODE:\n";
381 DumpDexCode(*indent2_os, dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700382 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800383 if (Runtime::Current() != NULL) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700384 *indent1_os << "VERIFIER TYPE ANALYSIS:\n";
385 DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700386 method_access_flags);
Ian Rogersb23a7722012-10-09 16:54:26 -0700387 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800388 {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700389 *indent1_os << "OAT DATA:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800390
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700391 *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
392 *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
393 DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false);
394 *indent2_os << StringPrintf("\nfp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
395 DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true);
396 *indent2_os << StringPrintf("\nvmap_table: %p (offset=0x%08x)\n",
397 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
398 DumpVmap(*indent2_os, oat_method);
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -0700399 DumpVregLocations(*indent2_os, oat_method, code_item);
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700400 *indent2_os << StringPrintf("mapping_table: %p (offset=0x%08x)\n",
401 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
Dave Allison404f59f2014-02-24 11:10:01 -0800402 if (dump_raw_mapping_table_) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700403 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800404 std::ostream indent3_os(&indent3_filter);
405 DumpMappingTable(indent3_os, oat_method);
406 }
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700407 *indent2_os << StringPrintf("gc_map: %p (offset=0x%08x)\n",
408 oat_method.GetNativeGcMap(), oat_method.GetNativeGcMapOffset());
Dave Allison404f59f2014-02-24 11:10:01 -0800409 if (dump_raw_gc_map_) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700410 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800411 std::ostream indent3_os(&indent3_filter);
412 DumpGcMap(indent3_os, oat_method, code_item);
413 }
414 }
415 {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800416 const void* code = oat_method.GetQuickCode();
417 uint32_t code_size = oat_method.GetQuickCodeSize();
418 if (code == nullptr) {
419 code = oat_method.GetPortableCode();
420 code_size = oat_method.GetPortableCodeSize();
421 }
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700422 *indent1_os << StringPrintf("CODE: %p (offset=0x%08x size=%d)%s\n",
Ian Rogersef7d42f2014-01-06 12:55:46 -0800423 code,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800424 oat_method.GetCodeOffset(),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800425 code_size,
426 code != nullptr ? "..." : "");
Mathieu Chartier590fee92013-09-13 13:46:47 -0700427
428 Runtime* runtime = Runtime::Current();
429 if (runtime != nullptr) {
430 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700431 StackHandleScope<1> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700432 Handle<mirror::DexCache> dex_cache(
433 hs.NewHandle(runtime->GetClassLinker()->FindDexCache(dex_file)));
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700434 verifier::MethodVerifier verifier(&dex_file, dex_cache, NullHandle<mirror::ClassLoader>(),
435 &class_def, code_item, dex_method_idx,
436 NullHandle<mirror::ArtMethod>(), method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700437 true, true, true);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700438 verifier.Verify();
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700439 DumpCode(*indent2_os, &verifier, oat_method, code_item);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700440 } else {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700441 DumpCode(*indent2_os, nullptr, oat_method, code_item);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700442 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800443 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700444 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800445
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800446 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
447 if (spill_mask == 0) {
448 return;
449 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800450 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800451 for (size_t i = 0; i < 32; i++) {
452 if ((spill_mask & (1 << i)) != 0) {
453 if (is_float) {
454 os << "fr" << i;
455 } else {
456 os << "r" << i;
457 }
458 spill_mask ^= 1 << i; // clear bit
459 if (spill_mask != 0) {
460 os << ", ";
461 } else {
462 break;
463 }
464 }
465 }
466 os << ")";
467 }
468
Ian Rogersb23a7722012-10-09 16:54:26 -0700469 void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogers1809a722013-08-09 22:05:32 -0700470 const uint8_t* raw_table = oat_method.GetVmapTable();
471 if (raw_table != NULL) {
472 const VmapTable vmap_table(raw_table);
473 bool first = true;
474 bool processing_fp = false;
475 uint32_t spill_mask = oat_method.GetCoreSpillMask();
476 for (size_t i = 0; i < vmap_table.Size(); i++) {
477 uint16_t dex_reg = vmap_table[i];
478 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
479 processing_fp ? kFloatVReg : kIntVReg);
480 os << (first ? "v" : ", v") << dex_reg;
481 if (!processing_fp) {
482 os << "/r" << cpu_reg;
483 } else {
484 os << "/fr" << cpu_reg;
485 }
486 first = false;
487 if (!processing_fp && dex_reg == 0xFFFF) {
488 processing_fp = true;
489 spill_mask = oat_method.GetFpSpillMask();
490 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800491 }
Ian Rogers1809a722013-08-09 22:05:32 -0700492 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800493 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800494 }
495
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -0700496 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
497 const DexFile::CodeItem* code_item) {
498 if (code_item != nullptr) {
499 size_t num_locals_ins = code_item->registers_size_;
500 size_t num_ins = code_item->ins_size_;
501 size_t num_locals = num_locals_ins - num_ins;
502 size_t num_outs = code_item->outs_size_;
503
504 os << "vr_stack_locations:";
505 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
506 // For readability, delimit the different kinds of VRs.
507 if (reg == num_locals_ins) {
508 os << "\n\tmethod*:";
509 } else if (reg == num_locals && num_ins > 0) {
510 os << "\n\tins:";
511 } else if (reg == 0 && num_locals > 0) {
512 os << "\n\tlocals:";
513 }
514
515 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
516 oat_method.GetFpSpillMask(),
517 oat_method.GetFrameSizeInBytes(), reg,
518 GetInstructionSet());
519 os << " v" << reg << "[sp + #" << offset << "]";
520 }
521
522 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
523 if (out_reg == 0) {
524 os << "\n\touts:";
525 }
526
527 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
528 os << " v" << out_reg << "[sp + #" << offset << "]";
529 }
530
531 os << "\n";
532 }
533 }
534
Ian Rogersb23a7722012-10-09 16:54:26 -0700535 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800536 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -0700537 const uint8_t* raw_table = oat_method.GetVmapTable();
Ian Rogersb23a7722012-10-09 16:54:26 -0700538 if (raw_table != NULL) {
539 const VmapTable vmap_table(raw_table);
540 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -0700541 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800542 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
543 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
544 : oat_method.GetCoreSpillMask();
545 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -0700546 } else {
547 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
548 oat_method.GetFpSpillMask(),
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000549 oat_method.GetFrameSizeInBytes(), reg,
550 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -0700551 os << "[sp + #" << offset << "]";
552 }
553 }
554 }
555
Ian Rogersef7d42f2014-01-06 12:55:46 -0800556 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
557 const DexFile::CodeItem* code_item,
558 size_t num_regs, const uint8_t* reg_bitmap) {
559 bool first = true;
560 for (size_t reg = 0; reg < num_regs; reg++) {
561 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
562 if (first) {
563 os << " v" << reg << " (";
564 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
565 os << ")";
566 first = false;
567 } else {
568 os << ", v" << reg << " (";
569 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
570 os << ")";
571 }
572 }
573 }
574 if (first) {
575 os << "No registers in GC map\n";
576 } else {
577 os << "\n";
578 }
579 }
Ian Rogersb23a7722012-10-09 16:54:26 -0700580 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
581 const DexFile::CodeItem* code_item) {
582 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800583 if (gc_map_raw == nullptr) {
584 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800585 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800586 const void* quick_code = oat_method.GetQuickCode();
587 if (quick_code != nullptr) {
588 NativePcOffsetToReferenceMap map(gc_map_raw);
589 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
590 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
591 map.GetNativePcOffset(entry);
592 os << StringPrintf("%p", native_pc);
593 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800594 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800595 } else {
596 const void* portable_code = oat_method.GetPortableCode();
597 CHECK(portable_code != nullptr);
598 verifier::DexPcToReferenceMap map(gc_map_raw);
599 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
600 uint32_t dex_pc = map.GetDexPc(entry);
601 os << StringPrintf("0x%08x", dex_pc);
602 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
603 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800604 }
605 }
606
607 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800608 const void* quick_code = oat_method.GetQuickCode();
609 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800610 return;
611 }
Ian Rogers1809a722013-08-09 22:05:32 -0700612 MappingTable table(oat_method.GetMappingTable());
613 if (table.TotalSize() != 0) {
614 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
615 std::ostream indent_os(&indent_filter);
616 if (table.PcToDexSize() != 0) {
617 typedef MappingTable::PcToDexIterator It;
618 os << "suspend point mappings {\n";
619 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
620 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
621 }
622 os << "}\n";
623 }
624 if (table.DexToPcSize() != 0) {
625 typedef MappingTable::DexToPcIterator It;
626 os << "catch entry mappings {\n";
627 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
628 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
629 }
630 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800631 }
632 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800633 }
634
Ian Rogers1809a722013-08-09 22:05:32 -0700635 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
636 size_t offset, bool suspend_point_mapping) {
637 MappingTable table(oat_method.GetMappingTable());
638 if (suspend_point_mapping && table.PcToDexSize() > 0) {
639 typedef MappingTable::PcToDexIterator It;
640 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
641 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -0700642 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -0700643 return cur.DexPc();
644 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800645 }
Ian Rogers1809a722013-08-09 22:05:32 -0700646 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
647 typedef MappingTable::DexToPcIterator It;
648 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
649 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -0700650 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -0700651 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -0700652 }
653 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800654 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800655 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -0700656 }
657
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800658 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
659 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700660 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
661 if (gc_map_raw != NULL) {
662 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800663 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700664 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800665 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -0700666 bool first = true;
667 for (size_t reg = 0; reg < num_regs; reg++) {
668 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
669 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800670 os << "GC map objects: v" << reg << " (";
671 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -0700672 os << ")";
673 first = false;
674 } else {
675 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800676 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -0700677 os << ")";
678 }
679 }
680 }
681 if (!first) {
682 os << "\n";
683 }
684 }
685 }
686 }
687
Mathieu Chartier590fee92013-09-13 13:46:47 -0700688 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
689 const OatFile::OatMethod& oat_method,
690 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
691 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800692 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800693 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800694 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
695 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
696 if (kind != kUndefined) {
697 if (first) {
698 os << "VRegs: v";
699 first = false;
700 } else {
701 os << ", v";
702 }
703 os << reg << " (";
704 switch (kind) {
705 case kImpreciseConstant:
706 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
707 DescribeVReg(os, oat_method, code_item, reg, kind);
708 break;
709 case kConstant:
710 os << "Constant: " << kinds.at((reg * 2) + 1);
711 break;
712 default:
713 DescribeVReg(os, oat_method, code_item, reg, kind);
714 break;
715 }
716 os << ")";
717 }
718 }
719 if (!first) {
720 os << "\n";
721 }
722 }
723
724
Ian Rogersb23a7722012-10-09 16:54:26 -0700725 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
726 if (code_item != NULL) {
727 size_t i = 0;
728 while (i < code_item->insns_size_in_code_units_) {
729 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800730 os << StringPrintf("0x%04zx: %s\n", i, instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -0700731 i += instruction->SizeInCodeUnits();
732 }
733 }
734 }
735
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800736 void DumpVerifier(std::ostream& os, uint32_t dex_method_idx, const DexFile* dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700737 const DexFile::ClassDef& class_def, const DexFile::CodeItem* code_item,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800738 uint32_t method_access_flags) {
739 if ((method_access_flags & kAccNative) == 0) {
740 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700741 StackHandleScope<1> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700742 Handle<mirror::DexCache> dex_cache(
743 hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800744 verifier::MethodVerifier::VerifyMethodAndDump(os, dex_method_idx, dex_file, dex_cache,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700745 NullHandle<mirror::ClassLoader>(), &class_def,
746 code_item, NullHandle<mirror::ArtMethod>(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800747 method_access_flags);
748 }
749 }
750
Mathieu Chartier590fee92013-09-13 13:46:47 -0700751 void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
752 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800753 const void* portable_code = oat_method.GetPortableCode();
754 const void* quick_code = oat_method.GetQuickCode();
755
756 size_t code_size = oat_method.GetQuickCodeSize();
757 if ((code_size == 0) || ((portable_code == nullptr) && (quick_code == nullptr))) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800758 os << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -0700759 return;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800760 } else if (quick_code != nullptr) {
761 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
762 size_t offset = 0;
763 while (offset < code_size) {
764 DumpMappingAtOffset(os, oat_method, offset, false);
765 offset += disassembler_->Dump(os, quick_native_pc + offset);
766 uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
767 if (dex_pc != DexFile::kDexNoIndex) {
768 DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
769 if (verifier != nullptr) {
770 DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
771 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800772 }
773 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800774 } else {
775 CHECK(portable_code != nullptr);
776 CHECK_EQ(code_size, 0U); // TODO: disassembly of portable is currently not supported.
Ian Rogersb23a7722012-10-09 16:54:26 -0700777 }
778 }
779
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800780 const OatFile& oat_file_;
781 std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Dave Allison404f59f2014-02-24 11:10:01 -0800782 bool dump_raw_mapping_table_;
783 bool dump_raw_gc_map_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800784 std::set<uintptr_t> offsets_;
Ian Rogers700a4022014-05-19 16:49:03 -0700785 std::unique_ptr<Disassembler> disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700786};
787
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800788class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700789 public:
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800790 explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
Dave Allison404f59f2014-02-24 11:10:01 -0800791 const ImageHeader& image_header, bool dump_raw_mapping_table,
792 bool dump_raw_gc_map)
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800793 : os_(os), image_space_(image_space), image_header_(image_header),
Dave Allison404f59f2014-02-24 11:10:01 -0800794 dump_raw_mapping_table_(dump_raw_mapping_table),
795 dump_raw_gc_map_(dump_raw_gc_map) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700796
Ian Rogersb726dcb2012-09-05 08:57:23 -0700797 void Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800798 std::ostream& os = *os_;
799 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700800
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800801 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700802
Mathieu Chartier31e89252013-08-28 11:29:12 -0700803 os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
804 << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
805
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800806 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700807
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800808 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700809
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800810 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
811
812 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
813
814 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800815
Alex Lighta59dd802014-07-02 16:28:08 -0700816 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
817
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800818 {
819 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
820 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
821 std::ostream indent1_os(&indent1_filter);
822 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
823 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
824 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
825 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800826 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800827 indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
828 if (image_root_object->IsObjectArray()) {
829 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
830 std::ostream indent2_os(&indent2_filter);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800831 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -0800832 = image_root_object->AsObjectArray<mirror::Object>();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800833 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800834 mirror::Object* value = image_root_object_array->Get(i);
Ian Rogersfa824272013-11-05 16:12:57 -0800835 size_t run = 0;
836 for (int32_t j = i + 1; j < image_root_object_array->GetLength(); j++) {
837 if (value == image_root_object_array->Get(j)) {
838 run++;
839 } else {
840 break;
841 }
842 }
843 if (run == 0) {
844 indent2_os << StringPrintf("%d: ", i);
845 } else {
846 indent2_os << StringPrintf("%d to %zd: ", i, i + run);
847 i = i + run;
848 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800849 if (value != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800850 PrettyObjectValue(indent2_os, value->GetClass(), value);
851 } else {
852 indent2_os << i << ": null\n";
853 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800854 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700855 }
856 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700857 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800858 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700859
Brian Carlstromaded5f72011-10-07 17:15:04 -0700860 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800861 std::string image_filename = image_space_.GetImageFilename();
862 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800863 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800864 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700865 std::string error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -0700866 const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
867 if (oat_file == nullptr) {
868 oat_file = OatFile::Open(oat_location, oat_location, NULL, false, &error_msg);
869 if (oat_file == nullptr) {
870 os << "NOT FOUND: " << error_msg << "\n";
871 return;
872 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700873 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800874 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700875
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800876 stats_.oat_file_bytes = oat_file->Size();
877
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000878 oat_dumper_.reset(new OatDumper(*oat_file, dump_raw_mapping_table_,
Dave Allison404f59f2014-02-24 11:10:01 -0800879 dump_raw_gc_map_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800880
Mathieu Chartier02e25112013-08-14 16:14:24 -0700881 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Ian Rogers05f28c62012-10-23 18:12:13 -0700882 CHECK(oat_dex_file != NULL);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700883 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
884 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -0700885 }
886
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800887 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700888
889 // Loop through all the image spaces and dump their objects.
Ian Rogers1d54e732013-05-02 21:10:01 -0700890 gc::Heap* heap = Runtime::Current()->GetHeap();
891 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -0700892 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700893 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -0800894 {
895 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
896 heap->FlushAllocStack();
897 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -0800898 // Since FlushAllocStack() above resets the (active) allocation
899 // stack. Need to revoke the thread-local allocation stacks that
900 // point into it.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -0800901 {
902 self->TransitionFromRunnableToSuspended(kNative);
903 ThreadList* thread_list = Runtime::Current()->GetThreadList();
904 thread_list->SuspendAll();
905 heap->RevokeAllThreadLocalAllocationStacks(self);
906 thread_list->ResumeAll();
907 self->TransitionFromSuspendedToRunnable();
908 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700909 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800910 {
911 std::ostream* saved_os = os_;
912 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
913 std::ostream indent_os(&indent_filter);
914 os_ = &indent_os;
915 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700916 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800917 if (space->IsImageSpace()) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700918 gc::space::ImageSpace* image_space = space->AsImageSpace();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800919 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
920 indent_os << "\n";
921 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700922 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800923 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700924 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800925 indent_os << "\n";
926 os_ = saved_os;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700927 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800928 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -0700929 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Brian Carlstrom6f277752013-09-30 17:56:45 -0700930 if (file.get() == NULL) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800931 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -0700932 }
933 if (file.get() != NULL) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800934 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -0700935 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800936 size_t header_bytes = sizeof(ImageHeader);
937 stats_.header_bytes = header_bytes;
938 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
939 stats_.alignment_bytes += alignment_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -0700940 stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
941 stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800942 stats_.Dump(os);
943 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800944
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800945 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800946
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800947 oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700948 }
949
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700950 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800951 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700952 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800953 CHECK(type != NULL);
954 if (value == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800955 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800956 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800957 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800958 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -0700959 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -0700960 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800961 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800962 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -0700963 } else if (type->IsArtFieldClass()) {
964 mirror::ArtField* field = value->AsArtField();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800965 os << StringPrintf("%p Field: %s\n", field, PrettyField(field).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -0700966 } else if (type->IsArtMethodClass()) {
967 mirror::ArtMethod* method = value->AsArtMethod();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800968 os << StringPrintf("%p Method: %s\n", method, PrettyMethod(method).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800969 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800970 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800971 }
972 }
973
Brian Carlstromea46f952013-07-30 01:26:50 -0700974 static void PrintField(std::ostream& os, mirror::ArtField* field, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700975 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700976 const char* descriptor = field->GetTypeDescriptor();
977 os << StringPrintf("%s: ", field->GetName());
Ian Rogers48efc2b2012-08-27 17:20:31 -0700978 if (descriptor[0] != 'L' && descriptor[0] != '[') {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700979 StackHandleScope<1> hs(Thread::Current());
980 FieldHelper fh(hs.NewHandle(field));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800981 mirror::Class* type = fh.GetType();
Fred Shih37f05ef2014-07-16 18:38:08 -0700982 DCHECK(type->IsPrimitive());
Ian Rogers48efc2b2012-08-27 17:20:31 -0700983 if (type->IsPrimitiveLong()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800984 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -0700985 } else if (type->IsPrimitiveDouble()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800986 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -0700987 } else if (type->IsPrimitiveFloat()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800988 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Fred Shih37f05ef2014-07-16 18:38:08 -0700989 } else if (type->IsPrimitiveInt()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800990 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Fred Shih37f05ef2014-07-16 18:38:08 -0700991 } else if (type->IsPrimitiveChar()) {
992 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
993 } else if (type->IsPrimitiveShort()) {
994 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
995 } else if (type->IsPrimitiveBoolean()) {
996 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
997 field->GetBoolean(obj));
998 } else if (type->IsPrimitiveByte()) {
999 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
1000 } else {
1001 LOG(FATAL) << "Unknown type: " << PrettyClass(type);
Ian Rogers48efc2b2012-08-27 17:20:31 -07001002 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001003 } else {
Ian Rogers48efc2b2012-08-27 17:20:31 -07001004 // Get the value, don't compute the type unless it is non-null as we don't want
1005 // to cause class loading.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001006 mirror::Object* value = field->GetObj(obj);
Ian Rogers48efc2b2012-08-27 17:20:31 -07001007 if (value == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001008 os << StringPrintf("null %s\n", PrettyDescriptor(descriptor).c_str());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001009 } else {
Ian Rogers50239c72013-06-17 14:53:22 -07001010 // Grab the field type without causing resolution.
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001011 StackHandleScope<1> hs(Thread::Current());
1012 FieldHelper fh(hs.NewHandle(field));
Ian Rogers50239c72013-06-17 14:53:22 -07001013 mirror::Class* field_type = fh.GetType(false);
1014 if (field_type != NULL) {
1015 PrettyObjectValue(os, field_type, value);
1016 } else {
1017 os << StringPrintf("%p %s\n", value, PrettyDescriptor(descriptor).c_str());
1018 }
Ian Rogers48efc2b2012-08-27 17:20:31 -07001019 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001020 }
1021 }
1022
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001023 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001024 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001025 mirror::Class* super = klass->GetSuperClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001026 if (super != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001027 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001028 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001029 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
Ian Rogersd5b32602012-02-26 16:40:04 -08001030 if (fields != NULL) {
1031 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001032 mirror::ArtField* field = fields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001033 PrintField(os, field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001034 }
1035 }
1036 }
1037
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001038 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001039 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001040 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001041
Ian Rogersef7d42f2014-01-06 12:55:46 -08001042 const void* GetQuickOatCodeBegin(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001043 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001044 const void* quick_code = m->GetEntryPointFromQuickCompiledCode();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001045 if (quick_code == Runtime::Current()->GetClassLinker()->GetQuickResolutionTrampoline()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001046 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001047 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001048 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001049 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001050 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001051 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001052 }
1053
Ian Rogersef7d42f2014-01-06 12:55:46 -08001054 uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001055 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001056 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1057 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001058 return 0;
1059 }
1060 return oat_code_begin[-1];
1061 }
1062
Ian Rogersef7d42f2014-01-06 12:55:46 -08001063 const void* GetQuickOatCodeEnd(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001064 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001065 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001066 if (oat_code_begin == NULL) {
1067 return NULL;
1068 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001069 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001070 }
1071
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001072 static void Callback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001073 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001074 DCHECK(obj != NULL);
1075 DCHECK(arg != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001076 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001077 if (!state->InDumpSpace(obj)) {
1078 return;
1079 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001080
1081 size_t object_bytes = obj->SizeOf();
1082 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1083 state->stats_.object_bytes += object_bytes;
1084 state->stats_.alignment_bytes += alignment_bytes;
1085
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001086 std::ostream& os = *state->os_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001087 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001088 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001089 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1090 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001091 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001092 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001093 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1094 << klass->GetStatus() << ")\n";
Brian Carlstromea46f952013-07-30 01:26:50 -07001095 } else if (obj->IsArtField()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001096 os << StringPrintf("%p: java.lang.reflect.ArtField %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001097 PrettyField(obj->AsArtField()).c_str());
1098 } else if (obj->IsArtMethod()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001099 os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001100 PrettyMethod(obj->AsArtMethod()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001101 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001102 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001103 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001104 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001105 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001106 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001107 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1108 std::ostream indent_os(&indent_filter);
1109 DumpFields(indent_os, obj, obj_class);
Ian Rogersd5b32602012-02-26 16:40:04 -08001110 if (obj->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001111 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Ian Rogersd5b32602012-02-26 16:40:04 -08001112 int32_t length = obj_array->GetLength();
1113 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001114 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001115 size_t run = 0;
1116 for (int32_t j = i + 1; j < length; j++) {
1117 if (value == obj_array->Get(j)) {
1118 run++;
1119 } else {
1120 break;
1121 }
1122 }
1123 if (run == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001124 indent_os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001125 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001126 indent_os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001127 i = i + run;
1128 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001129 mirror::Class* value_class =
1130 (value == NULL) ? obj_class->GetComponentType() : value->GetClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001131 PrettyObjectValue(indent_os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001132 }
1133 } else if (obj->IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001134 mirror::ObjectArray<mirror::ArtField>* sfields = obj->AsClass()->GetSFields();
Ian Rogersd5b32602012-02-26 16:40:04 -08001135 if (sfields != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001136 indent_os << "STATICS:\n";
1137 Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
1138 std::ostream indent2_os(&indent2_filter);
Ian Rogersd5b32602012-02-26 16:40:04 -08001139 for (int32_t i = 0; i < sfields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001140 mirror::ArtField* field = sfields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001141 PrintField(indent2_os, field, field->GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001142 }
1143 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001144 } else if (obj->IsArtMethod()) {
1145 mirror::ArtMethod* method = obj->AsArtMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001146 if (method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001147 // TODO: portable dumping.
1148 DCHECK(method->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1149 DCHECK(method->GetMappingTable() == nullptr) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001150 bool first_occurrence;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001151 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1152 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1153 state->ComputeOatSize(quick_oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001154 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001155 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001156 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001157 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCode()) {
1158 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001159 }
Ian Rogers19846512012-02-24 11:42:47 -08001160 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
Jeff Hao88474b42013-10-23 16:24:40 -07001161 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001162 method->IsClassInitializer()) {
Ian Rogers0c7abda2012-09-19 13:33:42 -07001163 DCHECK(method->GetNativeGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001164 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001165 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001166 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -07001167 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001168 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001169
Elliott Hughesa0e18062012-04-13 15:59:59 -07001170 bool first_occurrence;
Ian Rogers0c7abda2012-09-19 13:33:42 -07001171 size_t gc_map_bytes = state->ComputeOatSize(method->GetNativeGcMap(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001172 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001173 state->stats_.gc_map_bytes += gc_map_bytes;
1174 }
1175
1176 size_t pc_mapping_table_bytes =
Ian Rogers1809a722013-08-09 22:05:32 -07001177 state->ComputeOatSize(method->GetMappingTable(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001178 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001179 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1180 }
1181
1182 size_t vmap_table_bytes =
Ian Rogers1809a722013-08-09 22:05:32 -07001183 state->ComputeOatSize(method->GetVmapTable(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001184 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001185 state->stats_.vmap_table_bytes += vmap_table_bytes;
1186 }
1187
Ian Rogersef7d42f2014-01-06 12:55:46 -08001188 // TODO: portable dumping.
1189 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1190 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1191 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1192 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001193 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001194 state->stats_.managed_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001195 if (method->IsConstructor()) {
1196 if (method->IsStatic()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001197 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001198 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001199 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001200 }
1201 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001202 state->stats_.large_method_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001203 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001204 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001205 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001206
Ian Rogersef7d42f2014-01-06 12:55:46 -08001207 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001208 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
1209 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001210
1211 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersef7d42f2014-01-06 12:55:46 -08001212 vmap_table_bytes + quick_oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001213
1214 double expansion =
Ian Rogersef7d42f2014-01-06 12:55:46 -08001215 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001216 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001217 }
1218 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001219 std::string temp;
1220 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001221 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001222
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001223 std::set<const void*> already_seen_;
1224 // Compute the size of the given data within the oat file and whether this is the first time
1225 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001226 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001227 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001228 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001229 already_seen_.insert(oat_data);
1230 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001231 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001232 }
1233 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001234 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001235
1236 public:
1237 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001238 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001239 size_t file_bytes;
1240
1241 size_t header_bytes;
1242 size_t object_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001243 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001244 size_t alignment_bytes;
1245
1246 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001247 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001248 size_t managed_to_native_code_bytes;
1249 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001250 size_t class_initializer_code_bytes;
1251 size_t large_initializer_code_bytes;
1252 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001253
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001254 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001255 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001256 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001257
1258 size_t dex_instruction_bytes;
1259
Brian Carlstromea46f952013-07-30 01:26:50 -07001260 std::vector<mirror::ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001261 std::vector<size_t> method_outlier_size;
1262 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07001263 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001264
1265 explicit Stats()
1266 : oat_file_bytes(0),
1267 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001268 header_bytes(0),
1269 object_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07001270 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001271 alignment_bytes(0),
1272 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001273 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001274 managed_to_native_code_bytes(0),
1275 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001276 class_initializer_code_bytes(0),
1277 large_initializer_code_bytes(0),
1278 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001279 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001280 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001281 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001282 dex_instruction_bytes(0) {}
1283
Elliott Hughesa0e18062012-04-13 15:59:59 -07001284 struct SizeAndCount {
1285 SizeAndCount(size_t bytes, size_t count) : bytes(bytes), count(count) {}
1286 size_t bytes;
1287 size_t count;
1288 };
1289 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1290 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001291
Ian Rogersfc0e94b2013-09-23 23:51:32 -07001292 void Update(const char* descriptor, size_t object_bytes) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001293 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1294 if (it != sizes_and_counts.end()) {
1295 it->second.bytes += object_bytes;
1296 it->second.count += 1;
1297 } else {
1298 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes, 1));
1299 }
1300 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001301
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001302 double PercentOfOatBytes(size_t size) {
1303 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1304 }
1305
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001306 double PercentOfFileBytes(size_t size) {
1307 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1308 }
1309
1310 double PercentOfObjectBytes(size_t size) {
1311 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1312 }
1313
Brian Carlstromea46f952013-07-30 01:26:50 -07001314 void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001315 method_outlier_size.push_back(total_size);
1316 method_outlier_expansion.push_back(expansion);
1317 method_outlier.push_back(method);
1318 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001319
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001320 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001321 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001322 size_t sum_of_sizes = 0;
1323 size_t sum_of_sizes_squared = 0;
1324 size_t sum_of_expansion = 0;
1325 size_t sum_of_expansion_squared = 0;
1326 size_t n = method_outlier_size.size();
1327 for (size_t i = 0; i < n; i++) {
1328 size_t cur_size = method_outlier_size[i];
1329 sum_of_sizes += cur_size;
1330 sum_of_sizes_squared += cur_size * cur_size;
1331 double cur_expansion = method_outlier_expansion[i];
1332 sum_of_expansion += cur_expansion;
1333 sum_of_expansion_squared += cur_expansion * cur_expansion;
1334 }
1335 size_t size_mean = sum_of_sizes / n;
1336 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1337 double expansion_mean = sum_of_expansion / n;
1338 double expansion_variance =
1339 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1340
1341 // Dump methods whose size is a certain number of standard deviations from the mean
1342 size_t dumped_values = 0;
1343 size_t skipped_values = 0;
1344 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1345 size_t cur_size_variance = i * i * size_variance;
1346 bool first = true;
1347 for (size_t j = 0; j < n; j++) {
1348 size_t cur_size = method_outlier_size[j];
1349 if (cur_size > size_mean) {
1350 size_t cur_var = cur_size - size_mean;
1351 cur_var = cur_var * cur_var;
1352 if (cur_var > cur_size_variance) {
1353 if (dumped_values > 20) {
1354 if (i == 1) {
1355 skipped_values++;
1356 } else {
1357 i = 2; // jump to counting for 1 standard deviation
1358 break;
1359 }
1360 } else {
1361 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001362 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001363 first = false;
1364 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001365 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001366 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001367 method_outlier_size[j] = 0; // don't consider this method again
1368 dumped_values++;
1369 }
1370 }
1371 }
1372 }
1373 }
1374 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001375 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001376 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001377 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001378 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001379
1380 // Dump methods whose expansion is a certain number of standard deviations from the mean
1381 dumped_values = 0;
1382 skipped_values = 0;
1383 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1384 double cur_expansion_variance = i * i * expansion_variance;
1385 bool first = true;
1386 for (size_t j = 0; j < n; j++) {
1387 double cur_expansion = method_outlier_expansion[j];
1388 if (cur_expansion > expansion_mean) {
1389 size_t cur_var = cur_expansion - expansion_mean;
1390 cur_var = cur_var * cur_var;
1391 if (cur_var > cur_expansion_variance) {
1392 if (dumped_values > 20) {
1393 if (i == 1) {
1394 skipped_values++;
1395 } else {
1396 i = 2; // jump to counting for 1 standard deviation
1397 break;
1398 }
1399 } else {
1400 if (first) {
1401 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001402 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001403 first = false;
1404 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001405 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001406 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001407 method_outlier_expansion[j] = 0.0; // don't consider this method again
1408 dumped_values++;
1409 }
1410 }
1411 }
1412 }
1413 }
1414 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001415 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001416 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001417 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001418 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001419 }
1420
Ian Rogersb726dcb2012-09-05 08:57:23 -07001421 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001422 {
1423 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1424 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
1425 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1426 std::ostream indent_os(&indent_filter);
1427 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
1428 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier32327092013-08-30 14:04:08 -07001429 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001430 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
1431 header_bytes, PercentOfFileBytes(header_bytes),
1432 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07001433 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001434 alignment_bytes, PercentOfFileBytes(alignment_bytes))
1435 << std::flush;
Mathieu Chartier32327092013-08-30 14:04:08 -07001436 CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001437 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001438
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001439 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001440 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07001441 for (const auto& sizes_and_count : sizes_and_counts) {
1442 const std::string& descriptor(sizes_and_count.first);
1443 double average = static_cast<double>(sizes_and_count.second.bytes) /
1444 static_cast<double>(sizes_and_count.second.count);
1445 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001446 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001447 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001448 descriptor.c_str(), sizes_and_count.second.bytes,
1449 sizes_and_count.second.count, average, percent);
1450 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001451 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001452 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001453 CHECK_EQ(object_bytes, object_bytes_total);
1454
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001455 os << StringPrintf("oat_file_bytes = %8zd\n"
1456 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1457 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1458 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1459 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1460 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1461 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001462 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001463 managed_code_bytes,
1464 PercentOfOatBytes(managed_code_bytes),
1465 managed_to_native_code_bytes,
1466 PercentOfOatBytes(managed_to_native_code_bytes),
1467 native_to_managed_code_bytes,
1468 PercentOfOatBytes(native_to_managed_code_bytes),
1469 class_initializer_code_bytes,
1470 PercentOfOatBytes(class_initializer_code_bytes),
1471 large_initializer_code_bytes,
1472 PercentOfOatBytes(large_initializer_code_bytes),
1473 large_method_code_bytes,
1474 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001475 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07001476 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001477 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001478 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
1479 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07001480 }
1481
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001482 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1483 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1484 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001485 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1486 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1487 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001488 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001489
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001490 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
1491 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001492 static_cast<double>(managed_code_bytes) /
1493 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07001494 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001495 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001496 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001497
1498 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001499 }
1500 } stats_;
1501
1502 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001503 enum {
1504 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1505 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1506 kLargeConstructorDexBytes = 4000,
1507 // Number of bytes for a method to be considered large. Based on the 4000 basic block
1508 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1509 kLargeMethodDexBytes = 16000
1510 };
Ian Rogers700a4022014-05-19 16:49:03 -07001511 std::unique_ptr<OatDumper> oat_dumper_;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001512 std::ostream* os_;
Ian Rogers1d54e732013-05-02 21:10:01 -07001513 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001514 const ImageHeader& image_header_;
Dave Allison404f59f2014-02-24 11:10:01 -08001515 bool dump_raw_mapping_table_;
1516 bool dump_raw_gc_map_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001517
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001518 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001519};
1520
Elliott Hughes72395bf2012-04-24 13:45:26 -07001521static int oatdump(int argc, char** argv) {
Elliott Hughes0d39c122012-06-06 16:41:17 -07001522 InitLogging(argv);
Elliott Hughes72395bf2012-04-24 13:45:26 -07001523
Brian Carlstrom78128a62011-09-15 17:21:19 -07001524 // Skip over argv[0].
1525 argv++;
1526 argc--;
1527
1528 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001529 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001530 usage();
1531 }
1532
Brian Carlstromaded5f72011-10-07 17:15:04 -07001533 const char* oat_filename = NULL;
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001534 const char* image_location = NULL;
1535 const char* boot_image_location = NULL;
1536 InstructionSet instruction_set = kRuntimeISA;
Logan Chien0cc6ab62012-03-20 22:57:52 +08001537 std::string elf_filename_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001538 std::ostream* os = &std::cout;
Ian Rogers700a4022014-05-19 16:49:03 -07001539 std::unique_ptr<std::ofstream> out;
Dave Allison404f59f2014-02-24 11:10:01 -08001540 bool dump_raw_mapping_table = false;
1541 bool dump_raw_gc_map = false;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001542
1543 for (int i = 0; i < argc; i++) {
1544 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001545 if (option.starts_with("--oat-file=")) {
1546 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001547 } else if (option.starts_with("--image=")) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001548 image_location = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001549 } else if (option.starts_with("--boot-image=")) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001550 boot_image_location = option.substr(strlen("--boot-image=")).data();
1551 } else if (option.starts_with("--instruction-set=")) {
1552 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
1553 if (instruction_set_str == "arm") {
1554 instruction_set = kThumb2;
1555 } else if (instruction_set_str == "arm64") {
1556 instruction_set = kArm64;
1557 } else if (instruction_set_str == "mips") {
1558 instruction_set = kMips;
1559 } else if (instruction_set_str == "x86") {
1560 instruction_set = kX86;
1561 } else if (instruction_set_str == "x86_64") {
1562 instruction_set = kX86_64;
1563 }
Dave Allison404f59f2014-02-24 11:10:01 -08001564 } else if (option.starts_with("--dump:")) {
1565 if (option == "--dump:raw_mapping_table") {
1566 dump_raw_mapping_table = true;
1567 } else if (option == "--dump:raw_gc_map") {
1568 dump_raw_gc_map = true;
1569 } else {
1570 fprintf(stderr, "Unknown argument %s\n", option.data());
1571 usage();
1572 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001573 } else if (option.starts_with("--output=")) {
1574 const char* filename = option.substr(strlen("--output=")).data();
1575 out.reset(new std::ofstream(filename));
1576 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001577 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001578 usage();
1579 }
1580 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001581 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001582 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001583 usage();
1584 }
1585 }
1586
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001587 if (image_location == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001588 fprintf(stderr, "Either --image or --oat must be specified\n");
1589 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001590 }
1591
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001592 if (image_location != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001593 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1594 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001595 }
1596
1597 if (oat_filename != NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001598 std::string error_msg;
Logan Chien0c717dd2012-03-28 18:31:07 +08001599 OatFile* oat_file =
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001600 OatFile::Open(oat_filename, oat_filename, NULL, false, &error_msg);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001601 if (oat_file == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001602 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
Brian Carlstromaded5f72011-10-07 17:15:04 -07001603 return EXIT_FAILURE;
1604 }
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +00001605 OatDumper oat_dumper(*oat_file, dump_raw_mapping_table, dump_raw_gc_map);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001606 oat_dumper.Dump(*os);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001607 return EXIT_SUCCESS;
1608 }
1609
Ian Rogerse63db272014-07-15 15:36:11 -07001610 RuntimeOptions options;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001611 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001612 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001613 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001614 std::string boot_oat_option;
Brian Carlstrom6c871802013-07-17 14:25:35 -07001615
1616 // We are more like a compiler than a run-time. We don't want to execute code.
Brian Carlstromc0a1b182014-03-04 23:19:06 -08001617 NoopCompilerCallbacks callbacks;
1618 options.push_back(std::make_pair("compilercallbacks", &callbacks));
Brian Carlstrom6c871802013-07-17 14:25:35 -07001619
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001620 if (boot_image_location != NULL) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001621 boot_image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001622 boot_image_option += boot_image_location;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001623 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001624 }
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001625 if (image_location != NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001626 image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001627 image_option += image_location;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001628 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1629 }
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001630 options.push_back(
1631 std::make_pair("imageinstructionset",
1632 reinterpret_cast<const void*>(GetInstructionSetString(instruction_set))));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001633
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001634 if (!Runtime::Create(options, false)) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001635 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001636 return EXIT_FAILURE;
1637 }
Ian Rogers700a4022014-05-19 16:49:03 -07001638 std::unique_ptr<Runtime> runtime(Runtime::Current());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001639 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
Ian Rogersb9beb2e2014-05-09 16:57:40 -07001640 // give it away now and then switch to a more manageable ScopedObjectAccess.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001641 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
1642 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1d54e732013-05-02 21:10:01 -07001643 gc::Heap* heap = Runtime::Current()->GetHeap();
1644 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001645 CHECK(image_space != NULL);
1646 const ImageHeader& image_header = image_space->GetImageHeader();
1647 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001648 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001649 return EXIT_FAILURE;
1650 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001651 ImageDumper image_dumper(os, *image_space, image_header,
1652 dump_raw_mapping_table, dump_raw_gc_map);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001653 image_dumper.Dump();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001654 return EXIT_SUCCESS;
1655}
1656
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001657} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07001658
1659int main(int argc, char** argv) {
1660 return art::oatdump(argc, argv);
1661}