blob: 3c07f56cb695450ba8586012533a46463286cab6 [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 Rogers2bcb4a42012-11-08 10:39:18 -080032#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070033#include "gc/space/image_space.h"
34#include "gc/space/large_object_space.h"
35#include "gc/space/space-inl.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070036#include "image.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080037#include "indenter.h"
Ian Rogers1809a722013-08-09 22:05:32 -070038#include "mapping_table.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070039#include "mirror/art_field-inl.h"
40#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/array-inl.h"
42#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043#include "mirror/object-inl.h"
44#include "mirror/object_array-inl.h"
Brian Carlstromc0a1b182014-03-04 23:19:06 -080045#include "noop_compiler_callbacks.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080046#include "oat.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080047#include "object_utils.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080048#include "os.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070049#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070050#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070051#include "scoped_thread_state_change.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080052#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080053#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080054#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070055#include "vmap_table.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070056
57namespace art {
58
59static void usage() {
60 fprintf(stderr,
61 "Usage: oatdump [options] ...\n"
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +000062 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080063 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070064 "\n");
65 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080066 " --oat-file=<file.oat>: specifies an input oat filename.\n"
TDYa127a0a2a6c2012-10-16 22:47:38 -070067 " Example: --oat-file=/system/framework/boot.oat\n"
Brian Carlstromaded5f72011-10-07 17:15:04 -070068 "\n");
69 fprintf(stderr,
70 " --image=<file.art>: specifies an input image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080071 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070072 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070073 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070074 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080075 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070076 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070077 fprintf(stderr,
Brian Carlstrom27ec9612011-09-19 20:20:38 -070078 " --output=<file> may be used to send the output to a file.\n"
79 " Example: --output=/tmp/oatdump.txt\n"
80 "\n");
Dave Allison404f59f2014-02-24 11:10:01 -080081 fprintf(stderr,
82 " --dump:[raw_mapping_table|raw_gc_map]\n"
83 " Example: --dump:raw_gc_map\n"
84 " Default: neither\n"
85 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070086 exit(EXIT_FAILURE);
87}
88
Ian Rogersff1ed472011-09-20 13:46:24 -070089const char* image_roots_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080090 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070091 "kImtConflictMethod",
92 "kDefaultImt",
Brian Carlstrome24fa612011-09-29 00:53:55 -070093 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070094 "kRefsOnlySaveMethod",
95 "kRefsAndArgsSaveMethod",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070096 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070097 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070098};
99
Elliott Hughese3c845c2012-02-28 17:23:01 -0800100class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700101 public:
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000102 explicit OatDumper(const OatFile& oat_file, bool dump_raw_mapping_table, bool dump_raw_gc_map)
103 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800104 oat_dex_files_(oat_file.GetOatDexFiles()),
Dave Allison404f59f2014-02-24 11:10:01 -0800105 dump_raw_mapping_table_(dump_raw_mapping_table),
106 dump_raw_gc_map_(dump_raw_gc_map),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800107 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet())) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800108 AddAllOffsets();
109 }
110
111 void Dump(std::ostream& os) {
112 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700113
114 os << "MAGIC:\n";
115 os << oat_header.GetMagic() << "\n\n";
116
117 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800118 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700119
Elliott Hughesa72ec822012-03-05 17:12:22 -0800120 os << "INSTRUCTION SET:\n";
121 os << oat_header.GetInstructionSet() << "\n\n";
122
Dave Allison70202782013-10-22 17:52:19 -0700123 os << "INSTRUCTION SET FEATURES:\n";
124 os << oat_header.GetInstructionSetFeatures().GetFeatureString() << "\n\n";
125
Brian Carlstromaded5f72011-10-07 17:15:04 -0700126 os << "DEX FILE COUNT:\n";
127 os << oat_header.GetDexFileCount() << "\n\n";
128
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800129#define DUMP_OAT_HEADER_OFFSET(label, offset) \
130 os << label " OFFSET:\n"; \
131 os << StringPrintf("0x%08x", oat_header.offset()); \
132 if (oat_header.offset() != 0) { \
133 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
134 } \
135 os << StringPrintf("\n\n");
136
137 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
138 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
139 GetInterpreterToInterpreterBridgeOffset);
140 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
141 GetInterpreterToCompiledCodeBridgeOffset);
142 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
143 GetJniDlsymLookupOffset);
144 DUMP_OAT_HEADER_OFFSET("PORTABLE IMT CONFLICT TRAMPOLINE",
145 GetPortableImtConflictTrampolineOffset);
146 DUMP_OAT_HEADER_OFFSET("PORTABLE RESOLUTION TRAMPOLINE",
147 GetPortableResolutionTrampolineOffset);
148 DUMP_OAT_HEADER_OFFSET("PORTABLE TO INTERPRETER BRIDGE",
149 GetPortableToInterpreterBridgeOffset);
150 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
151 GetQuickGenericJniTrampolineOffset);
152 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
153 GetQuickImtConflictTrampolineOffset);
154 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
155 GetQuickResolutionTrampolineOffset);
156 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
157 GetQuickToInterpreterBridgeOffset);
158#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700159
Brian Carlstrom28db0122012-10-18 16:20:41 -0700160 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
161 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
162
163 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800164 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700165
166 os << "IMAGE FILE LOCATION:\n";
167 const std::string image_file_location(oat_header.GetImageFileLocation());
168 os << image_file_location;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700169 os << "\n\n";
170
Ian Rogers30fab402012-01-23 15:43:46 -0800171 os << "BEGIN:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800172 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700173
Ian Rogers30fab402012-01-23 15:43:46 -0800174 os << "END:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800175 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700176
177 os << std::flush;
178
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800179 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
180 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Brian Carlstromaded5f72011-10-07 17:15:04 -0700181 CHECK(oat_dex_file != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800182 DumpOatDexFile(os, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700183 }
184 }
185
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800186 size_t ComputeSize(const void* oat_data) {
187 if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() ||
188 reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) {
189 return 0; // Address not in oat file
190 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800191 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
192 reinterpret_cast<uintptr_t>(oat_file_.Begin());
193 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800194 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800195 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800196 return end_offset - begin_offset;
197 }
198
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700199 InstructionSet GetInstructionSet() {
200 return oat_file_.GetOatHeader().GetInstructionSet();
201 }
202
Ian Rogersef7d42f2014-01-06 12:55:46 -0800203 const void* GetQuickOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800204 MethodHelper mh(m);
205 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
206 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700207 CHECK(oat_dex_file != nullptr);
208 std::string error_msg;
209 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
210 if (dex_file.get() == nullptr) {
211 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
212 << "': " << error_msg;
213 } else {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700214 const DexFile::ClassDef* class_def =
215 dex_file->FindClassDef(mh.GetDeclaringClassDescriptor());
216 if (class_def != NULL) {
217 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100218 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800219 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100220 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800221 }
222 }
223 }
224 return NULL;
225 }
226
Brian Carlstromaded5f72011-10-07 17:15:04 -0700227 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800228 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800229 // We don't know the length of the code for each method, but we need to know where to stop
230 // when disassembling. What we do know is that a region of code will be followed by some other
231 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
232 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800233 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
234 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800235 CHECK(oat_dex_file != NULL);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700236 std::string error_msg;
237 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
238 if (dex_file.get() == nullptr) {
239 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
240 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800241 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800242 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800243 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800244 for (size_t class_def_index = 0;
245 class_def_index < dex_file->NumClassDefs();
246 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800247 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100248 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800249 const byte* class_data = dex_file->GetClassData(class_def);
250 if (class_data != NULL) {
251 ClassDataItemIterator it(*dex_file, class_data);
252 SkipAllFields(it);
253 uint32_t class_method_index = 0;
254 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100255 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800256 it.Next();
257 }
258 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100259 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800260 it.Next();
261 }
262 }
263 }
264 }
265
266 // If the last thing in the file is code for a method, there won't be an offset for the "next"
267 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
268 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800269 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800270 }
271
272 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800273 uint32_t code_offset = oat_method.GetCodeOffset();
274 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
275 code_offset &= ~0x1;
276 }
277 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800278 offsets_.insert(oat_method.GetMappingTableOffset());
279 offsets_.insert(oat_method.GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700280 offsets_.insert(oat_method.GetNativeGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800281 }
282
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800283 void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700284 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800285 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800286 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700287
288 // Create the verifier early.
289
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700290 std::string error_msg;
291 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800292 if (dex_file.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700293 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700294 return;
295 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800296 for (size_t class_def_index = 0;
297 class_def_index < dex_file->NumClassDefs();
298 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700299 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
300 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100301 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstromba150c32013-08-27 17:31:03 -0700302 os << StringPrintf("%zd: %s (type_idx=%d)", class_def_index, descriptor, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100303 << " (" << oat_class.GetStatus() << ")"
304 << " (" << oat_class.GetType() << ")\n";
305 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800306 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
307 std::ostream indented_os(&indent_filter);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100308 DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700309 }
310
311 os << std::flush;
312 }
313
Elliott Hughese3c845c2012-02-28 17:23:01 -0800314 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700315 while (it.HasNextStaticField()) {
316 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700317 }
Ian Rogers0571d352011-11-03 19:51:38 -0700318 while (it.HasNextInstanceField()) {
319 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700320 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800321 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700322
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800323 void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
324 const DexFile::ClassDef& class_def) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800325 const byte* class_data = dex_file.GetClassData(class_def);
326 if (class_data == NULL) { // empty class such as a marker interface?
327 return;
328 }
329 ClassDataItemIterator it(dex_file, class_data);
330 SkipAllFields(it);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800331 uint32_t class_method_idx = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700332 while (it.HasNextDirectMethod()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800333 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700334 DumpOatMethod(os, class_def, class_method_idx, oat_method, dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800335 it.GetMemberIndex(), it.GetMethodCodeItem(), it.GetMemberAccessFlags());
336 class_method_idx++;
Ian Rogers0571d352011-11-03 19:51:38 -0700337 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700338 }
Ian Rogers0571d352011-11-03 19:51:38 -0700339 while (it.HasNextVirtualMethod()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800340 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700341 DumpOatMethod(os, class_def, class_method_idx, oat_method, dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800342 it.GetMemberIndex(), it.GetMethodCodeItem(), it.GetMemberAccessFlags());
343 class_method_idx++;
Ian Rogers0571d352011-11-03 19:51:38 -0700344 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700345 }
Ian Rogers0571d352011-11-03 19:51:38 -0700346 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700347 os << std::flush;
348 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800349
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700350 void DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
351 uint32_t class_method_index,
Elliott Hughese3c845c2012-02-28 17:23:01 -0800352 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800353 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
354 uint32_t method_access_flags) {
355 os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700356 class_method_index, PrettyMethod(dex_method_idx, dex_file, true).c_str(),
357 dex_method_idx);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800358 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
359 std::ostream indent1_os(&indent1_filter);
360 {
361 indent1_os << "DEX CODE:\n";
362 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
363 std::ostream indent2_os(&indent2_filter);
364 DumpDexCode(indent2_os, dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700365 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800366 if (Runtime::Current() != NULL) {
367 indent1_os << "VERIFIER TYPE ANALYSIS:\n";
368 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
369 std::ostream indent2_os(&indent2_filter);
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700370 DumpVerifier(indent2_os, dex_method_idx, &dex_file, class_def, code_item,
371 method_access_flags);
Ian Rogersb23a7722012-10-09 16:54:26 -0700372 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800373 {
374 indent1_os << "OAT DATA:\n";
375 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
376 std::ostream indent2_os(&indent2_filter);
377
378 indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
379 indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
380 DumpSpillMask(indent2_os, oat_method.GetCoreSpillMask(), false);
381 indent2_os << StringPrintf("\nfp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
382 DumpSpillMask(indent2_os, oat_method.GetFpSpillMask(), true);
383 indent2_os << StringPrintf("\nvmap_table: %p (offset=0x%08x)\n",
384 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
385 DumpVmap(indent2_os, oat_method);
386 indent2_os << StringPrintf("mapping_table: %p (offset=0x%08x)\n",
387 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
Dave Allison404f59f2014-02-24 11:10:01 -0800388 if (dump_raw_mapping_table_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800389 Indenter indent3_filter(indent2_os.rdbuf(), kIndentChar, kIndentBy1Count);
390 std::ostream indent3_os(&indent3_filter);
391 DumpMappingTable(indent3_os, oat_method);
392 }
393 indent2_os << StringPrintf("gc_map: %p (offset=0x%08x)\n",
394 oat_method.GetNativeGcMap(), oat_method.GetNativeGcMapOffset());
Dave Allison404f59f2014-02-24 11:10:01 -0800395 if (dump_raw_gc_map_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800396 Indenter indent3_filter(indent2_os.rdbuf(), kIndentChar, kIndentBy1Count);
397 std::ostream indent3_os(&indent3_filter);
398 DumpGcMap(indent3_os, oat_method, code_item);
399 }
400 }
401 {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800402 const void* code = oat_method.GetQuickCode();
403 uint32_t code_size = oat_method.GetQuickCodeSize();
404 if (code == nullptr) {
405 code = oat_method.GetPortableCode();
406 code_size = oat_method.GetPortableCodeSize();
407 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800408 indent1_os << StringPrintf("CODE: %p (offset=0x%08x size=%d)%s\n",
Ian Rogersef7d42f2014-01-06 12:55:46 -0800409 code,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800410 oat_method.GetCodeOffset(),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800411 code_size,
412 code != nullptr ? "..." : "");
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800413 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
414 std::ostream indent2_os(&indent2_filter);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700415
416 Runtime* runtime = Runtime::Current();
417 if (runtime != nullptr) {
418 ScopedObjectAccess soa(Thread::Current());
419 SirtRef<mirror::DexCache> dex_cache(
420 soa.Self(), runtime->GetClassLinker()->FindDexCache(dex_file));
421 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), nullptr);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800422 verifier::MethodVerifier verifier(&dex_file, &dex_cache, &class_loader, &class_def,
423 code_item, dex_method_idx, nullptr, method_access_flags,
424 true, true);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700425 verifier.Verify();
426 DumpCode(indent2_os, &verifier, oat_method, code_item);
427 } else {
428 DumpCode(indent2_os, nullptr, oat_method, code_item);
429 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800430 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700431 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800432
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800433 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
434 if (spill_mask == 0) {
435 return;
436 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800437 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800438 for (size_t i = 0; i < 32; i++) {
439 if ((spill_mask & (1 << i)) != 0) {
440 if (is_float) {
441 os << "fr" << i;
442 } else {
443 os << "r" << i;
444 }
445 spill_mask ^= 1 << i; // clear bit
446 if (spill_mask != 0) {
447 os << ", ";
448 } else {
449 break;
450 }
451 }
452 }
453 os << ")";
454 }
455
Ian Rogersb23a7722012-10-09 16:54:26 -0700456 void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogers1809a722013-08-09 22:05:32 -0700457 const uint8_t* raw_table = oat_method.GetVmapTable();
458 if (raw_table != NULL) {
459 const VmapTable vmap_table(raw_table);
460 bool first = true;
461 bool processing_fp = false;
462 uint32_t spill_mask = oat_method.GetCoreSpillMask();
463 for (size_t i = 0; i < vmap_table.Size(); i++) {
464 uint16_t dex_reg = vmap_table[i];
465 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
466 processing_fp ? kFloatVReg : kIntVReg);
467 os << (first ? "v" : ", v") << dex_reg;
468 if (!processing_fp) {
469 os << "/r" << cpu_reg;
470 } else {
471 os << "/fr" << cpu_reg;
472 }
473 first = false;
474 if (!processing_fp && dex_reg == 0xFFFF) {
475 processing_fp = true;
476 spill_mask = oat_method.GetFpSpillMask();
477 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800478 }
Ian Rogers1809a722013-08-09 22:05:32 -0700479 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800480 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800481 }
482
Ian Rogersb23a7722012-10-09 16:54:26 -0700483 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800484 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -0700485 const uint8_t* raw_table = oat_method.GetVmapTable();
Ian Rogersb23a7722012-10-09 16:54:26 -0700486 if (raw_table != NULL) {
487 const VmapTable vmap_table(raw_table);
488 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -0700489 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800490 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
491 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
492 : oat_method.GetCoreSpillMask();
493 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -0700494 } else {
495 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
496 oat_method.GetFpSpillMask(),
497 oat_method.GetFrameSizeInBytes(), reg);
498 os << "[sp + #" << offset << "]";
499 }
500 }
501 }
502
Ian Rogersef7d42f2014-01-06 12:55:46 -0800503 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
504 const DexFile::CodeItem* code_item,
505 size_t num_regs, const uint8_t* reg_bitmap) {
506 bool first = true;
507 for (size_t reg = 0; reg < num_regs; reg++) {
508 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
509 if (first) {
510 os << " v" << reg << " (";
511 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
512 os << ")";
513 first = false;
514 } else {
515 os << ", v" << reg << " (";
516 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
517 os << ")";
518 }
519 }
520 }
521 if (first) {
522 os << "No registers in GC map\n";
523 } else {
524 os << "\n";
525 }
526 }
Ian Rogersb23a7722012-10-09 16:54:26 -0700527 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
528 const DexFile::CodeItem* code_item) {
529 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800530 if (gc_map_raw == nullptr) {
531 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800532 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800533 const void* quick_code = oat_method.GetQuickCode();
534 if (quick_code != nullptr) {
535 NativePcOffsetToReferenceMap map(gc_map_raw);
536 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
537 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
538 map.GetNativePcOffset(entry);
539 os << StringPrintf("%p", native_pc);
540 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800541 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800542 } else {
543 const void* portable_code = oat_method.GetPortableCode();
544 CHECK(portable_code != nullptr);
545 verifier::DexPcToReferenceMap map(gc_map_raw);
546 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
547 uint32_t dex_pc = map.GetDexPc(entry);
548 os << StringPrintf("0x%08x", dex_pc);
549 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
550 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800551 }
552 }
553
554 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800555 const void* quick_code = oat_method.GetQuickCode();
556 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800557 return;
558 }
Ian Rogers1809a722013-08-09 22:05:32 -0700559 MappingTable table(oat_method.GetMappingTable());
560 if (table.TotalSize() != 0) {
561 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
562 std::ostream indent_os(&indent_filter);
563 if (table.PcToDexSize() != 0) {
564 typedef MappingTable::PcToDexIterator It;
565 os << "suspend point mappings {\n";
566 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
567 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
568 }
569 os << "}\n";
570 }
571 if (table.DexToPcSize() != 0) {
572 typedef MappingTable::DexToPcIterator It;
573 os << "catch entry mappings {\n";
574 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
575 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
576 }
577 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800578 }
579 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800580 }
581
Ian Rogers1809a722013-08-09 22:05:32 -0700582 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
583 size_t offset, bool suspend_point_mapping) {
584 MappingTable table(oat_method.GetMappingTable());
585 if (suspend_point_mapping && table.PcToDexSize() > 0) {
586 typedef MappingTable::PcToDexIterator It;
587 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
588 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -0700589 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -0700590 return cur.DexPc();
591 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800592 }
Ian Rogers1809a722013-08-09 22:05:32 -0700593 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
594 typedef MappingTable::DexToPcIterator It;
595 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
596 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -0700597 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -0700598 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -0700599 }
600 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800601 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800602 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -0700603 }
604
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800605 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
606 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700607 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
608 if (gc_map_raw != NULL) {
609 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800610 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700611 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800612 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -0700613 bool first = true;
614 for (size_t reg = 0; reg < num_regs; reg++) {
615 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
616 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800617 os << "GC map objects: v" << reg << " (";
618 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -0700619 os << ")";
620 first = false;
621 } else {
622 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800623 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -0700624 os << ")";
625 }
626 }
627 }
628 if (!first) {
629 os << "\n";
630 }
631 }
632 }
633 }
634
Mathieu Chartier590fee92013-09-13 13:46:47 -0700635 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
636 const OatFile::OatMethod& oat_method,
637 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
638 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800639 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800640 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800641 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
642 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
643 if (kind != kUndefined) {
644 if (first) {
645 os << "VRegs: v";
646 first = false;
647 } else {
648 os << ", v";
649 }
650 os << reg << " (";
651 switch (kind) {
652 case kImpreciseConstant:
653 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
654 DescribeVReg(os, oat_method, code_item, reg, kind);
655 break;
656 case kConstant:
657 os << "Constant: " << kinds.at((reg * 2) + 1);
658 break;
659 default:
660 DescribeVReg(os, oat_method, code_item, reg, kind);
661 break;
662 }
663 os << ")";
664 }
665 }
666 if (!first) {
667 os << "\n";
668 }
669 }
670
671
Ian Rogersb23a7722012-10-09 16:54:26 -0700672 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
673 if (code_item != NULL) {
674 size_t i = 0;
675 while (i < code_item->insns_size_in_code_units_) {
676 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800677 os << StringPrintf("0x%04zx: %s\n", i, instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -0700678 i += instruction->SizeInCodeUnits();
679 }
680 }
681 }
682
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800683 void DumpVerifier(std::ostream& os, uint32_t dex_method_idx, const DexFile* dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700684 const DexFile::ClassDef& class_def, const DexFile::CodeItem* code_item,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800685 uint32_t method_access_flags) {
686 if ((method_access_flags & kAccNative) == 0) {
687 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800688 SirtRef<mirror::DexCache> dex_cache(
689 soa.Self(), Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700690 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), nullptr);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800691 verifier::MethodVerifier::VerifyMethodAndDump(os, dex_method_idx, dex_file, dex_cache,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700692 class_loader, &class_def, code_item, NULL,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800693 method_access_flags);
694 }
695 }
696
Mathieu Chartier590fee92013-09-13 13:46:47 -0700697 void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
698 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800699 const void* portable_code = oat_method.GetPortableCode();
700 const void* quick_code = oat_method.GetQuickCode();
701
702 size_t code_size = oat_method.GetQuickCodeSize();
703 if ((code_size == 0) || ((portable_code == nullptr) && (quick_code == nullptr))) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800704 os << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -0700705 return;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800706 } else if (quick_code != nullptr) {
707 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
708 size_t offset = 0;
709 while (offset < code_size) {
710 DumpMappingAtOffset(os, oat_method, offset, false);
711 offset += disassembler_->Dump(os, quick_native_pc + offset);
712 uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
713 if (dex_pc != DexFile::kDexNoIndex) {
714 DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
715 if (verifier != nullptr) {
716 DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
717 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800718 }
719 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800720 } else {
721 CHECK(portable_code != nullptr);
722 CHECK_EQ(code_size, 0U); // TODO: disassembly of portable is currently not supported.
Ian Rogersb23a7722012-10-09 16:54:26 -0700723 }
724 }
725
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800726 const OatFile& oat_file_;
727 std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Dave Allison404f59f2014-02-24 11:10:01 -0800728 bool dump_raw_mapping_table_;
729 bool dump_raw_gc_map_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800730 std::set<uintptr_t> offsets_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800731 UniquePtr<Disassembler> disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700732};
733
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800734class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700735 public:
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800736 explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
Dave Allison404f59f2014-02-24 11:10:01 -0800737 const ImageHeader& image_header, bool dump_raw_mapping_table,
738 bool dump_raw_gc_map)
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800739 : os_(os), image_space_(image_space), image_header_(image_header),
Dave Allison404f59f2014-02-24 11:10:01 -0800740 dump_raw_mapping_table_(dump_raw_mapping_table),
741 dump_raw_gc_map_(dump_raw_gc_map) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700742
Ian Rogersb726dcb2012-09-05 08:57:23 -0700743 void Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800744 std::ostream& os = *os_;
745 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700746
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800747 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700748
Mathieu Chartier31e89252013-08-28 11:29:12 -0700749 os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
750 << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
751
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800752 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700753
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800754 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700755
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800756 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
757
758 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
759
760 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800761
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800762 {
763 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
764 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
765 std::ostream indent1_os(&indent1_filter);
766 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
767 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
768 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
769 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800770 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800771 indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
772 if (image_root_object->IsObjectArray()) {
773 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
774 std::ostream indent2_os(&indent2_filter);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800775 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -0800776 = image_root_object->AsObjectArray<mirror::Object>();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800777 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800778 mirror::Object* value = image_root_object_array->Get(i);
Ian Rogersfa824272013-11-05 16:12:57 -0800779 size_t run = 0;
780 for (int32_t j = i + 1; j < image_root_object_array->GetLength(); j++) {
781 if (value == image_root_object_array->Get(j)) {
782 run++;
783 } else {
784 break;
785 }
786 }
787 if (run == 0) {
788 indent2_os << StringPrintf("%d: ", i);
789 } else {
790 indent2_os << StringPrintf("%d to %zd: ", i, i + run);
791 i = i + run;
792 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800793 if (value != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800794 PrettyObjectValue(indent2_os, value->GetClass(), value);
795 } else {
796 indent2_os << i << ": null\n";
797 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800798 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700799 }
800 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700801 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800802 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700803
Brian Carlstromaded5f72011-10-07 17:15:04 -0700804 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800805 std::string image_filename = image_space_.GetImageFilename();
806 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800807 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800808 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700809 std::string error_msg;
810 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location, &error_msg);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700811 if (oat_file == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700812 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700813 return;
814 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800815 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700816
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800817 stats_.oat_file_bytes = oat_file->Size();
818
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000819 oat_dumper_.reset(new OatDumper(*oat_file, dump_raw_mapping_table_,
Dave Allison404f59f2014-02-24 11:10:01 -0800820 dump_raw_gc_map_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800821
Mathieu Chartier02e25112013-08-14 16:14:24 -0700822 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Ian Rogers05f28c62012-10-23 18:12:13 -0700823 CHECK(oat_dex_file != NULL);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700824 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
825 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -0700826 }
827
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800828 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700829
830 // Loop through all the image spaces and dump their objects.
Ian Rogers1d54e732013-05-02 21:10:01 -0700831 gc::Heap* heap = Runtime::Current()->GetHeap();
832 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -0700833 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700834 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -0800835 {
836 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
837 heap->FlushAllocStack();
838 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -0800839 // Since FlushAllocStack() above resets the (active) allocation
840 // stack. Need to revoke the thread-local allocation stacks that
841 // point into it.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -0800842 {
843 self->TransitionFromRunnableToSuspended(kNative);
844 ThreadList* thread_list = Runtime::Current()->GetThreadList();
845 thread_list->SuspendAll();
846 heap->RevokeAllThreadLocalAllocationStacks(self);
847 thread_list->ResumeAll();
848 self->TransitionFromSuspendedToRunnable();
849 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700850 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800851 {
852 std::ostream* saved_os = os_;
853 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
854 std::ostream indent_os(&indent_filter);
855 os_ = &indent_os;
856 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700857 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800858 if (space->IsImageSpace()) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700859 gc::space::ImageSpace* image_space = space->AsImageSpace();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800860 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
861 indent_os << "\n";
862 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700863 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800864 // Dump the large objects separately.
865 heap->GetLargeObjectsSpace()->GetLiveObjects()->Walk(ImageDumper::Callback, this);
866 indent_os << "\n";
867 os_ = saved_os;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700868 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800869 os << "STATS:\n" << std::flush;
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800870 UniquePtr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Brian Carlstrom6f277752013-09-30 17:56:45 -0700871 if (file.get() == NULL) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800872 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -0700873 }
874 if (file.get() != NULL) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800875 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -0700876 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800877 size_t header_bytes = sizeof(ImageHeader);
878 stats_.header_bytes = header_bytes;
879 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
880 stats_.alignment_bytes += alignment_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -0700881 stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
882 stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800883 stats_.Dump(os);
884 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800885
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800886 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800887
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800888 oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700889 }
890
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700891 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800892 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700893 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800894 CHECK(type != NULL);
895 if (value == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800896 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800897 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800898 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800899 os << StringPrintf("%p String: %s\n", string,
900 PrintableString(string->ToModifiedUtf8()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -0700901 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800902 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800903 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -0700904 } else if (type->IsArtFieldClass()) {
905 mirror::ArtField* field = value->AsArtField();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800906 os << StringPrintf("%p Field: %s\n", field, PrettyField(field).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -0700907 } else if (type->IsArtMethodClass()) {
908 mirror::ArtMethod* method = value->AsArtMethod();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800909 os << StringPrintf("%p Method: %s\n", method, PrettyMethod(method).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800910 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800911 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800912 }
913 }
914
Brian Carlstromea46f952013-07-30 01:26:50 -0700915 static void PrintField(std::ostream& os, mirror::ArtField* field, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700916 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800917 FieldHelper fh(field);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700918 const char* descriptor = fh.GetTypeDescriptor();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800919 os << StringPrintf("%s: ", fh.GetName());
Ian Rogers48efc2b2012-08-27 17:20:31 -0700920 if (descriptor[0] != 'L' && descriptor[0] != '[') {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800921 mirror::Class* type = fh.GetType();
Ian Rogers48efc2b2012-08-27 17:20:31 -0700922 if (type->IsPrimitiveLong()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800923 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -0700924 } else if (type->IsPrimitiveDouble()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800925 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -0700926 } else if (type->IsPrimitiveFloat()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800927 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -0700928 } else {
929 DCHECK(type->IsPrimitive());
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800930 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -0700931 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800932 } else {
Ian Rogers48efc2b2012-08-27 17:20:31 -0700933 // Get the value, don't compute the type unless it is non-null as we don't want
934 // to cause class loading.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800935 mirror::Object* value = field->GetObj(obj);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700936 if (value == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800937 os << StringPrintf("null %s\n", PrettyDescriptor(descriptor).c_str());
Ian Rogers48efc2b2012-08-27 17:20:31 -0700938 } else {
Ian Rogers50239c72013-06-17 14:53:22 -0700939 // Grab the field type without causing resolution.
940 mirror::Class* field_type = fh.GetType(false);
941 if (field_type != NULL) {
942 PrettyObjectValue(os, field_type, value);
943 } else {
944 os << StringPrintf("%p %s\n", value, PrettyDescriptor(descriptor).c_str());
945 }
Ian Rogers48efc2b2012-08-27 17:20:31 -0700946 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800947 }
948 }
949
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800950 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700951 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800952 mirror::Class* super = klass->GetSuperClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800953 if (super != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800954 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -0800955 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700956 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
Ian Rogersd5b32602012-02-26 16:40:04 -0800957 if (fields != NULL) {
958 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700959 mirror::ArtField* field = fields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800960 PrintField(os, field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -0800961 }
962 }
963 }
964
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800965 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800966 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700967 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800968
Ian Rogersef7d42f2014-01-06 12:55:46 -0800969 const void* GetQuickOatCodeBegin(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700970 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800971 const void* quick_code = m->GetEntryPointFromQuickCompiledCode();
972 if (quick_code == GetQuickResolutionTrampoline(Runtime::Current()->GetClassLinker())) {
973 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800974 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700975 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800976 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700977 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800978 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800979 }
980
Ian Rogersef7d42f2014-01-06 12:55:46 -0800981 uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700982 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800983 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
984 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700985 return 0;
986 }
987 return oat_code_begin[-1];
988 }
989
Ian Rogersef7d42f2014-01-06 12:55:46 -0800990 const void* GetQuickOatCodeEnd(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700991 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800992 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700993 if (oat_code_begin == NULL) {
994 return NULL;
995 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800996 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700997 }
998
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800999 static void Callback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001000 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001001 DCHECK(obj != NULL);
1002 DCHECK(arg != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001003 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001004 if (!state->InDumpSpace(obj)) {
1005 return;
1006 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001007
1008 size_t object_bytes = obj->SizeOf();
1009 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1010 state->stats_.object_bytes += object_bytes;
1011 state->stats_.alignment_bytes += alignment_bytes;
1012
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001013 std::ostream& os = *state->os_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001014 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001015 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001016 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1017 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001018 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001019 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001020 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1021 << klass->GetStatus() << ")\n";
Brian Carlstromea46f952013-07-30 01:26:50 -07001022 } else if (obj->IsArtField()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001023 os << StringPrintf("%p: java.lang.reflect.ArtField %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001024 PrettyField(obj->AsArtField()).c_str());
1025 } else if (obj->IsArtMethod()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001026 os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001027 PrettyMethod(obj->AsArtMethod()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001028 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001029 os << StringPrintf("%p: java.lang.String %s\n", obj,
1030 PrintableString(obj->AsString()->ToModifiedUtf8()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001031 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001032 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001033 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001034 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1035 std::ostream indent_os(&indent_filter);
1036 DumpFields(indent_os, obj, obj_class);
Ian Rogersd5b32602012-02-26 16:40:04 -08001037 if (obj->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001038 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Ian Rogersd5b32602012-02-26 16:40:04 -08001039 int32_t length = obj_array->GetLength();
1040 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001041 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001042 size_t run = 0;
1043 for (int32_t j = i + 1; j < length; j++) {
1044 if (value == obj_array->Get(j)) {
1045 run++;
1046 } else {
1047 break;
1048 }
1049 }
1050 if (run == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001051 indent_os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001052 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001053 indent_os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001054 i = i + run;
1055 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001056 mirror::Class* value_class =
1057 (value == NULL) ? obj_class->GetComponentType() : value->GetClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001058 PrettyObjectValue(indent_os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001059 }
1060 } else if (obj->IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001061 mirror::ObjectArray<mirror::ArtField>* sfields = obj->AsClass()->GetSFields();
Ian Rogersd5b32602012-02-26 16:40:04 -08001062 if (sfields != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001063 indent_os << "STATICS:\n";
1064 Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
1065 std::ostream indent2_os(&indent2_filter);
Ian Rogersd5b32602012-02-26 16:40:04 -08001066 for (int32_t i = 0; i < sfields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001067 mirror::ArtField* field = sfields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001068 PrintField(indent2_os, field, field->GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001069 }
1070 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001071 } else if (obj->IsArtMethod()) {
1072 mirror::ArtMethod* method = obj->AsArtMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001073 if (method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001074 // TODO: portable dumping.
1075 DCHECK(method->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1076 DCHECK(method->GetMappingTable() == nullptr) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001077 bool first_occurrence;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001078 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1079 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1080 state->ComputeOatSize(quick_oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001081 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001082 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001083 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001084 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCode()) {
1085 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001086 }
Ian Rogers19846512012-02-24 11:42:47 -08001087 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
Jeff Hao88474b42013-10-23 16:24:40 -07001088 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
1089 MethodHelper(method).IsClassInitializer()) {
Ian Rogers0c7abda2012-09-19 13:33:42 -07001090 DCHECK(method->GetNativeGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001091 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001092 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001093 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -07001094 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001095 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001096
Elliott Hughesa0e18062012-04-13 15:59:59 -07001097 bool first_occurrence;
Ian Rogers0c7abda2012-09-19 13:33:42 -07001098 size_t gc_map_bytes = state->ComputeOatSize(method->GetNativeGcMap(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001099 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001100 state->stats_.gc_map_bytes += gc_map_bytes;
1101 }
1102
1103 size_t pc_mapping_table_bytes =
Ian Rogers1809a722013-08-09 22:05:32 -07001104 state->ComputeOatSize(method->GetMappingTable(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001105 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001106 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1107 }
1108
1109 size_t vmap_table_bytes =
Ian Rogers1809a722013-08-09 22:05:32 -07001110 state->ComputeOatSize(method->GetVmapTable(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001111 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001112 state->stats_.vmap_table_bytes += vmap_table_bytes;
1113 }
1114
Ian Rogersef7d42f2014-01-06 12:55:46 -08001115 // TODO: portable dumping.
1116 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1117 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1118 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1119 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001120 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001121 state->stats_.managed_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001122 if (method->IsConstructor()) {
1123 if (method->IsStatic()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001124 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001125 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001126 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001127 }
1128 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001129 state->stats_.large_method_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001130 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001131 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001132 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001133
Ian Rogersef7d42f2014-01-06 12:55:46 -08001134 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001135 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
1136 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001137
1138 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersef7d42f2014-01-06 12:55:46 -08001139 vmap_table_bytes + quick_oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001140
1141 double expansion =
Ian Rogersef7d42f2014-01-06 12:55:46 -08001142 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001143 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001144 }
1145 }
Elliott Hughesa0e18062012-04-13 15:59:59 -07001146 state->stats_.Update(ClassHelper(obj_class).GetDescriptor(), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001147 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001148
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001149 std::set<const void*> already_seen_;
1150 // Compute the size of the given data within the oat file and whether this is the first time
1151 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001152 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001153 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001154 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001155 already_seen_.insert(oat_data);
1156 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001157 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001158 }
1159 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001160 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001161
1162 public:
1163 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001164 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001165 size_t file_bytes;
1166
1167 size_t header_bytes;
1168 size_t object_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001169 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001170 size_t alignment_bytes;
1171
1172 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001173 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001174 size_t managed_to_native_code_bytes;
1175 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001176 size_t class_initializer_code_bytes;
1177 size_t large_initializer_code_bytes;
1178 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001179
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001180 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001181 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001182 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001183
1184 size_t dex_instruction_bytes;
1185
Brian Carlstromea46f952013-07-30 01:26:50 -07001186 std::vector<mirror::ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001187 std::vector<size_t> method_outlier_size;
1188 std::vector<double> method_outlier_expansion;
Ian Rogers05f28c62012-10-23 18:12:13 -07001189 std::vector<std::pair<std::string, size_t> > oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001190
1191 explicit Stats()
1192 : oat_file_bytes(0),
1193 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001194 header_bytes(0),
1195 object_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07001196 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001197 alignment_bytes(0),
1198 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001199 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001200 managed_to_native_code_bytes(0),
1201 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001202 class_initializer_code_bytes(0),
1203 large_initializer_code_bytes(0),
1204 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001205 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001206 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001207 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001208 dex_instruction_bytes(0) {}
1209
Elliott Hughesa0e18062012-04-13 15:59:59 -07001210 struct SizeAndCount {
1211 SizeAndCount(size_t bytes, size_t count) : bytes(bytes), count(count) {}
1212 size_t bytes;
1213 size_t count;
1214 };
1215 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1216 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001217
Ian Rogersfc0e94b2013-09-23 23:51:32 -07001218 void Update(const char* descriptor, size_t object_bytes) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001219 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1220 if (it != sizes_and_counts.end()) {
1221 it->second.bytes += object_bytes;
1222 it->second.count += 1;
1223 } else {
1224 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes, 1));
1225 }
1226 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001227
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001228 double PercentOfOatBytes(size_t size) {
1229 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1230 }
1231
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001232 double PercentOfFileBytes(size_t size) {
1233 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1234 }
1235
1236 double PercentOfObjectBytes(size_t size) {
1237 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1238 }
1239
Brian Carlstromea46f952013-07-30 01:26:50 -07001240 void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001241 method_outlier_size.push_back(total_size);
1242 method_outlier_expansion.push_back(expansion);
1243 method_outlier.push_back(method);
1244 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001245
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001246 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001247 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001248 size_t sum_of_sizes = 0;
1249 size_t sum_of_sizes_squared = 0;
1250 size_t sum_of_expansion = 0;
1251 size_t sum_of_expansion_squared = 0;
1252 size_t n = method_outlier_size.size();
1253 for (size_t i = 0; i < n; i++) {
1254 size_t cur_size = method_outlier_size[i];
1255 sum_of_sizes += cur_size;
1256 sum_of_sizes_squared += cur_size * cur_size;
1257 double cur_expansion = method_outlier_expansion[i];
1258 sum_of_expansion += cur_expansion;
1259 sum_of_expansion_squared += cur_expansion * cur_expansion;
1260 }
1261 size_t size_mean = sum_of_sizes / n;
1262 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1263 double expansion_mean = sum_of_expansion / n;
1264 double expansion_variance =
1265 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1266
1267 // Dump methods whose size is a certain number of standard deviations from the mean
1268 size_t dumped_values = 0;
1269 size_t skipped_values = 0;
1270 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1271 size_t cur_size_variance = i * i * size_variance;
1272 bool first = true;
1273 for (size_t j = 0; j < n; j++) {
1274 size_t cur_size = method_outlier_size[j];
1275 if (cur_size > size_mean) {
1276 size_t cur_var = cur_size - size_mean;
1277 cur_var = cur_var * cur_var;
1278 if (cur_var > cur_size_variance) {
1279 if (dumped_values > 20) {
1280 if (i == 1) {
1281 skipped_values++;
1282 } else {
1283 i = 2; // jump to counting for 1 standard deviation
1284 break;
1285 }
1286 } else {
1287 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001288 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001289 first = false;
1290 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001291 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001292 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001293 method_outlier_size[j] = 0; // don't consider this method again
1294 dumped_values++;
1295 }
1296 }
1297 }
1298 }
1299 }
1300 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001301 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001302 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001303 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001304 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001305
1306 // Dump methods whose expansion is a certain number of standard deviations from the mean
1307 dumped_values = 0;
1308 skipped_values = 0;
1309 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1310 double cur_expansion_variance = i * i * expansion_variance;
1311 bool first = true;
1312 for (size_t j = 0; j < n; j++) {
1313 double cur_expansion = method_outlier_expansion[j];
1314 if (cur_expansion > expansion_mean) {
1315 size_t cur_var = cur_expansion - expansion_mean;
1316 cur_var = cur_var * cur_var;
1317 if (cur_var > cur_expansion_variance) {
1318 if (dumped_values > 20) {
1319 if (i == 1) {
1320 skipped_values++;
1321 } else {
1322 i = 2; // jump to counting for 1 standard deviation
1323 break;
1324 }
1325 } else {
1326 if (first) {
1327 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001328 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001329 first = false;
1330 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001331 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001332 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001333 method_outlier_expansion[j] = 0.0; // don't consider this method again
1334 dumped_values++;
1335 }
1336 }
1337 }
1338 }
1339 }
1340 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001341 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001342 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001343 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001344 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001345 }
1346
Ian Rogersb726dcb2012-09-05 08:57:23 -07001347 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001348 {
1349 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1350 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
1351 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1352 std::ostream indent_os(&indent_filter);
1353 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
1354 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier32327092013-08-30 14:04:08 -07001355 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001356 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
1357 header_bytes, PercentOfFileBytes(header_bytes),
1358 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07001359 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001360 alignment_bytes, PercentOfFileBytes(alignment_bytes))
1361 << std::flush;
Mathieu Chartier32327092013-08-30 14:04:08 -07001362 CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001363 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001364
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001365 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001366 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07001367 for (const auto& sizes_and_count : sizes_and_counts) {
1368 const std::string& descriptor(sizes_and_count.first);
1369 double average = static_cast<double>(sizes_and_count.second.bytes) /
1370 static_cast<double>(sizes_and_count.second.count);
1371 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001372 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001373 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001374 descriptor.c_str(), sizes_and_count.second.bytes,
1375 sizes_and_count.second.count, average, percent);
1376 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001377 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001378 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001379 CHECK_EQ(object_bytes, object_bytes_total);
1380
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001381 os << StringPrintf("oat_file_bytes = %8zd\n"
1382 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1383 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1384 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1385 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1386 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1387 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001388 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001389 managed_code_bytes,
1390 PercentOfOatBytes(managed_code_bytes),
1391 managed_to_native_code_bytes,
1392 PercentOfOatBytes(managed_to_native_code_bytes),
1393 native_to_managed_code_bytes,
1394 PercentOfOatBytes(native_to_managed_code_bytes),
1395 class_initializer_code_bytes,
1396 PercentOfOatBytes(class_initializer_code_bytes),
1397 large_initializer_code_bytes,
1398 PercentOfOatBytes(large_initializer_code_bytes),
1399 large_method_code_bytes,
1400 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001401 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07001402 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001403 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001404 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
1405 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07001406 }
1407
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001408 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1409 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1410 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001411 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1412 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1413 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001414 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001415
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001416 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
1417 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001418 static_cast<double>(managed_code_bytes) /
1419 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07001420 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001421 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001422 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001423
1424 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001425 }
1426 } stats_;
1427
1428 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001429 enum {
1430 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1431 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1432 kLargeConstructorDexBytes = 4000,
1433 // Number of bytes for a method to be considered large. Based on the 4000 basic block
1434 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1435 kLargeMethodDexBytes = 16000
1436 };
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001437 UniquePtr<OatDumper> oat_dumper_;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001438 std::ostream* os_;
Ian Rogers1d54e732013-05-02 21:10:01 -07001439 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001440 const ImageHeader& image_header_;
Dave Allison404f59f2014-02-24 11:10:01 -08001441 bool dump_raw_mapping_table_;
1442 bool dump_raw_gc_map_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001443
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001444 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001445};
1446
Elliott Hughes72395bf2012-04-24 13:45:26 -07001447static int oatdump(int argc, char** argv) {
Elliott Hughes0d39c122012-06-06 16:41:17 -07001448 InitLogging(argv);
Elliott Hughes72395bf2012-04-24 13:45:26 -07001449
Brian Carlstrom78128a62011-09-15 17:21:19 -07001450 // Skip over argv[0].
1451 argv++;
1452 argc--;
1453
1454 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001455 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001456 usage();
1457 }
1458
Brian Carlstromaded5f72011-10-07 17:15:04 -07001459 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001460 const char* image_filename = NULL;
1461 const char* boot_image_filename = NULL;
Logan Chien0cc6ab62012-03-20 22:57:52 +08001462 std::string elf_filename_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001463 std::ostream* os = &std::cout;
1464 UniquePtr<std::ofstream> out;
Dave Allison404f59f2014-02-24 11:10:01 -08001465 bool dump_raw_mapping_table = false;
1466 bool dump_raw_gc_map = false;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001467
1468 for (int i = 0; i < argc; i++) {
1469 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001470 if (option.starts_with("--oat-file=")) {
1471 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001472 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001473 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001474 } else if (option.starts_with("--boot-image=")) {
1475 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Dave Allison404f59f2014-02-24 11:10:01 -08001476 } else if (option.starts_with("--dump:")) {
1477 if (option == "--dump:raw_mapping_table") {
1478 dump_raw_mapping_table = true;
1479 } else if (option == "--dump:raw_gc_map") {
1480 dump_raw_gc_map = true;
1481 } else {
1482 fprintf(stderr, "Unknown argument %s\n", option.data());
1483 usage();
1484 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001485 } else if (option.starts_with("--output=")) {
1486 const char* filename = option.substr(strlen("--output=")).data();
1487 out.reset(new std::ofstream(filename));
1488 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001489 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001490 usage();
1491 }
1492 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001493 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001494 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001495 usage();
1496 }
1497 }
1498
Brian Carlstromaded5f72011-10-07 17:15:04 -07001499 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001500 fprintf(stderr, "Either --image or --oat must be specified\n");
1501 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001502 }
1503
Brian Carlstromaded5f72011-10-07 17:15:04 -07001504 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001505 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1506 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001507 }
1508
1509 if (oat_filename != NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001510 std::string error_msg;
Logan Chien0c717dd2012-03-28 18:31:07 +08001511 OatFile* oat_file =
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001512 OatFile::Open(oat_filename, oat_filename, NULL, false, &error_msg);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001513 if (oat_file == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001514 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
Brian Carlstromaded5f72011-10-07 17:15:04 -07001515 return EXIT_FAILURE;
1516 }
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +00001517 OatDumper oat_dumper(*oat_file, dump_raw_mapping_table, dump_raw_gc_map);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001518 oat_dumper.Dump(*os);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001519 return EXIT_SUCCESS;
1520 }
1521
Brian Carlstrom78128a62011-09-15 17:21:19 -07001522 Runtime::Options options;
1523 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001524 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001525 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001526 std::string boot_oat_option;
Brian Carlstrom6c871802013-07-17 14:25:35 -07001527
1528 // We are more like a compiler than a run-time. We don't want to execute code.
Brian Carlstromc0a1b182014-03-04 23:19:06 -08001529 NoopCompilerCallbacks callbacks;
1530 options.push_back(std::make_pair("compilercallbacks", &callbacks));
Brian Carlstrom6c871802013-07-17 14:25:35 -07001531
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001532 if (boot_image_filename != NULL) {
1533 boot_image_option += "-Ximage:";
1534 boot_image_option += boot_image_filename;
1535 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001536 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001537 if (image_filename != NULL) {
1538 image_option += "-Ximage:";
1539 image_option += image_filename;
1540 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1541 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001542
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001543 if (!Runtime::Create(options, false)) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001544 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001545 return EXIT_FAILURE;
1546 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001547 UniquePtr<Runtime> runtime(Runtime::Current());
1548 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
1549 // give it away now and then switch to a more managable ScopedObjectAccess.
1550 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
1551 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1d54e732013-05-02 21:10:01 -07001552 gc::Heap* heap = Runtime::Current()->GetHeap();
1553 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001554 CHECK(image_space != NULL);
1555 const ImageHeader& image_header = image_space->GetImageHeader();
1556 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001557 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001558 return EXIT_FAILURE;
1559 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001560 ImageDumper image_dumper(os, *image_space, image_header,
1561 dump_raw_mapping_table, dump_raw_gc_map);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001562 image_dumper.Dump();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001563 return EXIT_SUCCESS;
1564}
1565
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001566} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07001567
1568int main(int argc, char** argv) {
1569 return art::oatdump(argc, argv);
1570}