blob: 6c7e2ef07ca22ab729675ac9c87d4f70b23c2d96 [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
25#include "class_linker.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080026#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080027#include "disassembler.h"
Brian Carlstrom916e74e2011-09-23 11:42:01 -070028#include "file.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070029#include "gc/large_object_space.h"
30#include "gc/space.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070031#include "image.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070032#include "oat/runtime/context.h" // For VmapTable
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080033#include "object_utils.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080034#include "os.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070035#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070036#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070037#include "scoped_thread_state_change.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070038#include "stringpiece.h"
Ian Rogers0c7abda2012-09-19 13:33:42 -070039#include "gc_map.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070040
41namespace art {
42
43static void usage() {
44 fprintf(stderr,
45 "Usage: oatdump [options] ...\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080046 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n"
47 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070048 "\n");
49 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080050 " --oat-file=<file.oat>: specifies an input oat filename.\n"
TDYa127a0a2a6c2012-10-16 22:47:38 -070051 " Example: --oat-file=/system/framework/boot.oat\n"
Brian Carlstromaded5f72011-10-07 17:15:04 -070052 "\n");
53 fprintf(stderr,
54 " --image=<file.art>: specifies an input image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080055 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070056 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070057 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070058 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080059 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070060 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070061 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070062 " --host-prefix may be used to translate host paths to target paths during\n"
63 " cross compilation.\n"
64 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstromfe487d02012-02-29 18:49:16 -080065 " Default: $ANDROID_PRODUCT_OUT\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070066 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070067 fprintf(stderr,
68 " --output=<file> may be used to send the output to a file.\n"
69 " Example: --output=/tmp/oatdump.txt\n"
70 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070071 exit(EXIT_FAILURE);
72}
73
Ian Rogersff1ed472011-09-20 13:46:24 -070074const char* image_roots_descriptions_[] = {
Brian Carlstrom78128a62011-09-15 17:21:19 -070075 "kJniStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070076 "kAbstractMethodErrorStubArray",
Ian Rogersad25ac52011-10-04 19:13:33 -070077 "kStaticResolutionStubArray",
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070078 "kUnknownMethodResolutionStubArray",
Ian Rogers19846512012-02-24 11:42:47 -080079 "kResolutionMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070080 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070081 "kRefsOnlySaveMethod",
82 "kRefsAndArgsSaveMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070083 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070084 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070085 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070086};
87
Elliott Hughese3c845c2012-02-28 17:23:01 -080088class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -070089 public:
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070090 explicit OatDumper(const std::string& host_prefix, const OatFile& oat_file)
91 : host_prefix_(host_prefix),
92 oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -080093 oat_dex_files_(oat_file.GetOatDexFiles()),
94 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet())) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080095 AddAllOffsets();
96 }
97
98 void Dump(std::ostream& os) {
99 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700100
101 os << "MAGIC:\n";
102 os << oat_header.GetMagic() << "\n\n";
103
104 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800105 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700106
Elliott Hughesa72ec822012-03-05 17:12:22 -0800107 os << "INSTRUCTION SET:\n";
108 os << oat_header.GetInstructionSet() << "\n\n";
109
Brian Carlstromaded5f72011-10-07 17:15:04 -0700110 os << "DEX FILE COUNT:\n";
111 os << oat_header.GetDexFileCount() << "\n\n";
112
113 os << "EXECUTABLE OFFSET:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800114 os << StringPrintf("0x%08x\n\n", oat_header.GetExecutableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700115
Brian Carlstrom28db0122012-10-18 16:20:41 -0700116 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
117 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
118
119 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
120 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700121
122 os << "IMAGE FILE LOCATION:\n";
123 const std::string image_file_location(oat_header.GetImageFileLocation());
124 os << image_file_location;
125 if (!image_file_location.empty() && !host_prefix_.empty()) {
126 os << " (" << host_prefix_ << image_file_location << ")";
127 }
128 os << "\n\n";
129
Ian Rogers30fab402012-01-23 15:43:46 -0800130 os << "BEGIN:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800131 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700132
Ian Rogers30fab402012-01-23 15:43:46 -0800133 os << "END:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800134 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700135
136 os << std::flush;
137
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800138 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
139 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Brian Carlstromaded5f72011-10-07 17:15:04 -0700140 CHECK(oat_dex_file != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800141 DumpOatDexFile(os, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700142 }
143 }
144
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800145 size_t ComputeSize(const void* oat_data) {
146 if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() ||
147 reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) {
148 return 0; // Address not in oat file
149 }
150 uint32_t begin_offset = reinterpret_cast<size_t>(oat_data) -
151 reinterpret_cast<size_t>(oat_file_.Begin());
152 typedef std::set<uint32_t>::iterator It;
153 It it = offsets_.upper_bound(begin_offset);
154 CHECK(it != offsets_.end());
155 uint32_t end_offset = *it;
156 return end_offset - begin_offset;
157 }
158
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700159 InstructionSet GetInstructionSet() {
160 return oat_file_.GetOatHeader().GetInstructionSet();
161 }
162
Mathieu Chartier66f19252012-09-18 08:57:04 -0700163 const void* GetOatCode(AbstractMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800164 MethodHelper mh(m);
165 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
166 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
167 CHECK(oat_dex_file != NULL);
168 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
169 if (dex_file.get() != NULL) {
170 uint32_t class_def_index;
171 bool found = dex_file->FindClassDefIndex(mh.GetDeclaringClassDescriptor(), class_def_index);
172 if (found) {
173 const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_index);
174 CHECK(oat_class != NULL);
175 size_t method_index = m->GetMethodIndex();
176 return oat_class->GetOatMethod(method_index).GetCode();
177 }
178 }
179 }
180 return NULL;
181 }
182
Brian Carlstromaded5f72011-10-07 17:15:04 -0700183 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800184 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800185 // We don't know the length of the code for each method, but we need to know where to stop
186 // when disassembling. What we do know is that a region of code will be followed by some other
187 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
188 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800189 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
190 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800191 CHECK(oat_dex_file != NULL);
192 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
193 if (dex_file.get() == NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800194 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800195 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800196 offsets_.insert(reinterpret_cast<uint32_t>(&dex_file->GetHeader()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800197 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
198 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
199 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index));
200 const byte* class_data = dex_file->GetClassData(class_def);
201 if (class_data != NULL) {
202 ClassDataItemIterator it(*dex_file, class_data);
203 SkipAllFields(it);
204 uint32_t class_method_index = 0;
205 while (it.HasNextDirectMethod()) {
206 AddOffsets(oat_class->GetOatMethod(class_method_index++));
207 it.Next();
208 }
209 while (it.HasNextVirtualMethod()) {
210 AddOffsets(oat_class->GetOatMethod(class_method_index++));
211 it.Next();
212 }
213 }
214 }
215 }
216
217 // If the last thing in the file is code for a method, there won't be an offset for the "next"
218 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
219 // for the end of the file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800220 offsets_.insert(static_cast<uint32_t>(oat_file_.End() - oat_file_.Begin()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800221 }
222
223 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800224 uint32_t code_offset = oat_method.GetCodeOffset();
225 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
226 code_offset &= ~0x1;
227 }
228 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800229 offsets_.insert(oat_method.GetMappingTableOffset());
230 offsets_.insert(oat_method.GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700231 offsets_.insert(oat_method.GetNativeGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800232 offsets_.insert(oat_method.GetInvokeStubOffset());
233 }
234
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800235 void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700236 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800237 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800238 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800239 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile());
240 if (dex_file.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700241 os << "NOT FOUND\n\n";
242 return;
243 }
244 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
245 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
246 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700247 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
248 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800249 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800250 << oat_class->GetStatus() << ")\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800251 DumpOatClass(os, *oat_class.get(), *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700252 }
253
254 os << std::flush;
255 }
256
Elliott Hughese3c845c2012-02-28 17:23:01 -0800257 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700258 while (it.HasNextStaticField()) {
259 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700260 }
Ian Rogers0571d352011-11-03 19:51:38 -0700261 while (it.HasNextInstanceField()) {
262 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700263 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800264 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700265
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800266 void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
267 const DexFile::ClassDef& class_def) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800268 const byte* class_data = dex_file.GetClassData(class_def);
269 if (class_data == NULL) { // empty class such as a marker interface?
270 return;
271 }
272 ClassDataItemIterator it(dex_file, class_data);
273 SkipAllFields(it);
274
275 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700276 while (it.HasNextDirectMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800277 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800278 DumpOatMethod(os, class_method_index, oat_method, dex_file,
279 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800280 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700281 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700282 }
Ian Rogers0571d352011-11-03 19:51:38 -0700283 while (it.HasNextVirtualMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800284 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800285 DumpOatMethod(os, class_method_index, oat_method, dex_file,
286 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800287 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700288 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700289 }
Ian Rogers0571d352011-11-03 19:51:38 -0700290 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700291 os << std::flush;
292 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800293
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800294 void DumpOatMethod(std::ostream& os, uint32_t class_method_index,
Elliott Hughese3c845c2012-02-28 17:23:01 -0800295 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
296 uint32_t dex_method_idx, const DexFile::CodeItem* code_item) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700297 os << StringPrintf("\t%d: %s (dex_method_idx=%d)\n",
298 class_method_index, PrettyMethod(dex_method_idx, dex_file, true).c_str(),
299 dex_method_idx);
Ian Rogersb23a7722012-10-09 16:54:26 -0700300 os << StringPrintf("\t\tframe_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
301 os << StringPrintf("\t\tcore_spill_mask: 0x%08x\n", oat_method.GetCoreSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800302 DumpSpillMask(os, oat_method.GetCoreSpillMask(), false);
Ian Rogersb23a7722012-10-09 16:54:26 -0700303 os << StringPrintf("\n\t\tfp_spill_mask: 0x%08x\n", oat_method.GetFpSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800304 DumpSpillMask(os, oat_method.GetFpSpillMask(), true);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800305 os << StringPrintf("\t\tvmap_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800306 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Ian Rogersb23a7722012-10-09 16:54:26 -0700307 DumpVmap(os, oat_method);
308 const bool kDumpRawMappingTable = false;
309 if (kDumpRawMappingTable) {
310 os << StringPrintf("\t\tmapping_table: %p (offset=0x%08x)\n",
311 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
312 DumpMappingTable(os, oat_method);
313 }
314 const bool kDumpRawGcMap = false;
315 if (kDumpRawGcMap) {
316 os << StringPrintf("\t\tgc_map: %p (offset=0x%08x)\n",
317 oat_method.GetNativeGcMap(), oat_method.GetNativeGcMapOffset());
318 DumpGcMap(os, oat_method, code_item);
319 }
320 os << "\t\tDEX CODE:\n";
321 DumpDexCode(os, dex_file, code_item);
Logan Chien971bf3f2012-05-01 15:47:55 +0800322 os << StringPrintf("\t\tCODE: %p (offset=0x%08x size=%d)%s\n",
Elliott Hughes77405792012-03-15 15:22:12 -0700323 oat_method.GetCode(),
324 oat_method.GetCodeOffset(),
325 oat_method.GetCodeSize(),
326 oat_method.GetCode() != NULL ? "..." : "");
Ian Rogersb23a7722012-10-09 16:54:26 -0700327 DumpCode(os, oat_method, code_item);
Logan Chien971bf3f2012-05-01 15:47:55 +0800328 os << StringPrintf("\t\tINVOKE STUB: %p (offset=0x%08x size=%d)%s\n",
Elliott Hughes77405792012-03-15 15:22:12 -0700329 oat_method.GetInvokeStub(),
330 oat_method.GetInvokeStubOffset(),
331 oat_method.GetInvokeStubSize(),
332 oat_method.GetInvokeStub() != NULL ? "..." : "");
Ian Rogersb23a7722012-10-09 16:54:26 -0700333 DumpInvokeStub(os, oat_method);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700334 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800335
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800336 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
337 if (spill_mask == 0) {
338 return;
339 }
340 os << " (";
341 for (size_t i = 0; i < 32; i++) {
342 if ((spill_mask & (1 << i)) != 0) {
343 if (is_float) {
344 os << "fr" << i;
345 } else {
346 os << "r" << i;
347 }
348 spill_mask ^= 1 << i; // clear bit
349 if (spill_mask != 0) {
350 os << ", ";
351 } else {
352 break;
353 }
354 }
355 }
356 os << ")";
357 }
358
Ian Rogersb23a7722012-10-09 16:54:26 -0700359 void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) {
360 const uint16_t* raw_table = oat_method.GetVmapTable();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800361 if (raw_table == NULL) {
362 return;
363 }
364 const VmapTable vmap_table(raw_table);
365 bool first = true;
366 os << "\t\t\t";
367 for (size_t i = 0; i < vmap_table.size(); i++) {
368 uint16_t dex_reg = vmap_table[i];
369 size_t matches = 0;
370 size_t spill_shifts = 0;
Ian Rogersb23a7722012-10-09 16:54:26 -0700371 uint32_t spill_mask = oat_method.GetCoreSpillMask();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800372 bool processing_fp = false;
373 while (matches != (i + 1)) {
374 if (spill_mask == 0) {
375 CHECK(!processing_fp);
Ian Rogersb23a7722012-10-09 16:54:26 -0700376 spill_mask = oat_method.GetFpSpillMask();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800377 processing_fp = true;
378 }
379 matches += spill_mask & 1; // Add 1 if the low bit is set
380 spill_mask >>= 1;
381 spill_shifts++;
382 }
383 size_t arm_reg = spill_shifts - 1; // wind back one as we want the last match
384 os << (first ? "v" : ", v") << dex_reg;
385 if (arm_reg < 16) {
386 os << "/r" << arm_reg;
387 } else {
388 os << "/fr" << (arm_reg - 16);
389 }
390 if (first) {
391 first = false;
392 }
393 }
Elliott Hughesc073b072012-05-24 19:29:17 -0700394 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800395 }
396
Ian Rogersb23a7722012-10-09 16:54:26 -0700397 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
398 const DexFile::CodeItem* code_item, size_t reg) {
399 const uint16_t* raw_table = oat_method.GetVmapTable();
400 if (raw_table != NULL) {
401 const VmapTable vmap_table(raw_table);
402 uint32_t vmap_offset;
403 if (vmap_table.IsInContext(reg, vmap_offset)) {
404 // Compute the register we need to load from the context
405 uint32_t spill_mask = oat_method.GetCoreSpillMask();
406 CHECK_LT(vmap_offset, static_cast<uint32_t>(__builtin_popcount(spill_mask)));
407 uint32_t matches = 0;
408 uint32_t spill_shifts = 0;
409 while (matches != (vmap_offset + 1)) {
410 DCHECK_NE(spill_mask, 0u);
411 matches += spill_mask & 1; // Add 1 if the low bit is set
412 spill_mask >>= 1;
413 spill_shifts++;
414 }
415 spill_shifts--; // wind back one as we want the last match
416 os << "r" << spill_shifts;
417 } else {
418 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
419 oat_method.GetFpSpillMask(),
420 oat_method.GetFrameSizeInBytes(), reg);
421 os << "[sp + #" << offset << "]";
422 }
423 }
424 }
425
426 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
427 const DexFile::CodeItem* code_item) {
428 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800429 if (gc_map_raw == NULL) {
430 return;
431 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700432 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogersb23a7722012-10-09 16:54:26 -0700433 const void* code = oat_method.GetCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800434 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
Ian Rogersbb6723f2012-09-21 09:49:06 -0700435 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) +
436 map.GetNativePcOffset(entry);
437 os << StringPrintf("\t\t\t%p", native_pc);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800438 size_t num_regs = map.RegWidth() * 8;
439 const uint8_t* reg_bitmap = map.GetBitMap(entry);
440 bool first = true;
441 for (size_t reg = 0; reg < num_regs; reg++) {
442 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
443 if (first) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700444 os << " v" << reg << " (";
445 DescribeVReg(os, oat_method, code_item, reg);
446 os << ")";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800447 first = false;
448 } else {
Ian Rogersb23a7722012-10-09 16:54:26 -0700449 os << ", v" << reg << " (";
450 DescribeVReg(os, oat_method, code_item, reg);
451 os << ")";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800452 }
453 }
454 }
Elliott Hughesc073b072012-05-24 19:29:17 -0700455 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800456 }
457 }
458
459 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800460 const uint32_t* raw_table = oat_method.GetMappingTable();
461 const void* code = oat_method.GetCode();
462 if (raw_table == NULL || code == NULL) {
463 return;
464 }
465
buzbee4d7a1892012-10-03 11:03:37 -0700466 ++raw_table;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800467 uint32_t length = *raw_table;
468 ++raw_table;
Bill Buzbeea5b30242012-09-28 07:19:44 -0700469 uint32_t pc_to_dex_entries = *raw_table;
470 ++raw_table;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800471
Ian Rogersb23a7722012-10-09 16:54:26 -0700472 os << "\t\tsuspend point mappings {";
Elliott Hughese3c845c2012-02-28 17:23:01 -0800473 for (size_t i = 0; i < length; i += 2) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800474 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800475 uint32_t dex_pc = raw_table[i + 1];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800476 os << StringPrintf("%p -> 0x%04x", native_pc, dex_pc);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700477 if (i + 2 == pc_to_dex_entries) {
478 // Separate the pc -> dex from dex -> pc sections
Ian Rogersb23a7722012-10-09 16:54:26 -0700479 os << "}\n\t\tcatch entry mappings {";
Bill Buzbeea5b30242012-09-28 07:19:44 -0700480 } else if (i + 2 < length) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800481 os << ", ";
482 }
483 }
Ian Rogersb23a7722012-10-09 16:54:26 -0700484 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800485 }
486
Ian Rogersb23a7722012-10-09 16:54:26 -0700487 void DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method, size_t offset,
488 bool suspend_point_mapping) {
489 const uint32_t* raw_table = oat_method.GetMappingTable();
490 if (raw_table != NULL) {
491 ++raw_table;
492 uint32_t length = *raw_table;
493 ++raw_table;
494 uint32_t pc_to_dex_entries = *raw_table;
495 ++raw_table;
496 size_t start, end;
497 if (suspend_point_mapping) {
498 start = 0;
499 end = pc_to_dex_entries;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800500 } else {
Ian Rogersb23a7722012-10-09 16:54:26 -0700501 start = pc_to_dex_entries;
502 end = length;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800503 }
Ian Rogersb23a7722012-10-09 16:54:26 -0700504 for (size_t i = start; i < end; i += 2) {
505 if (offset == raw_table[i]) {
506 if (suspend_point_mapping) {
Ian Rogersae4b0a12012-10-09 17:32:36 -0700507 os << "\t\t\tsuspend point dex PC: 0x";
Ian Rogersb23a7722012-10-09 16:54:26 -0700508 } else {
Ian Rogersae4b0a12012-10-09 17:32:36 -0700509 os << "\t\t\tcatch entry dex PC: 0x";
Ian Rogersb23a7722012-10-09 16:54:26 -0700510 }
Ian Rogers9958daa2012-10-12 00:23:38 -0700511 os << std::hex << raw_table[i + 1] << std::dec << "\n";
Ian Rogersb23a7722012-10-09 16:54:26 -0700512 return;
513 }
514 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800515 }
Ian Rogersb23a7722012-10-09 16:54:26 -0700516 }
517
518 void DumpGcMapAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
519 const DexFile::CodeItem* code_item, size_t offset) {
520 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
521 if (gc_map_raw != NULL) {
522 NativePcOffsetToReferenceMap map(gc_map_raw);
523 if (map.HasEntry(offset)) {
524 size_t num_regs = map.RegWidth() * 8;
525 const uint8_t* reg_bitmap = map.FindBitMap(offset);
526 bool first = true;
527 for (size_t reg = 0; reg < num_regs; reg++) {
528 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
529 if (first) {
530 os << "\t\t\tGC map objects: v" << reg << " (";
531 DescribeVReg(os, oat_method, code_item, reg);
532 os << ")";
533 first = false;
534 } else {
535 os << ", v" << reg << " (";
536 DescribeVReg(os, oat_method, code_item, reg);
537 os << ")";
538 }
539 }
540 }
541 if (!first) {
542 os << "\n";
543 }
544 }
545 }
546 }
547
548 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
549 if (code_item != NULL) {
550 size_t i = 0;
551 while (i < code_item->insns_size_in_code_units_) {
552 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Ian Rogersae4b0a12012-10-09 17:32:36 -0700553 os << StringPrintf("\t\t\t0x%04zx: %s\n", i, instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -0700554 i += instruction->SizeInCodeUnits();
555 }
556 }
557 }
558
559 void DumpCode(std::ostream& os, const OatFile::OatMethod& oat_method,
560 const DexFile::CodeItem* code_item) {
561 const void* code = oat_method.GetCode();
562 size_t code_size = oat_method.GetCodeSize();
563 if (code == NULL || code_size == 0) {
564 os << "\t\t\tNO CODE!\n";
565 return;
566 }
567 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code);
568 size_t offset = 0;
569 while (offset < code_size) {
570 DumpMappingAtOffset(os, oat_method, offset, false);
571 offset += disassembler_->Dump(os, native_pc + offset);
572 DumpMappingAtOffset(os, oat_method, offset, true);
573 DumpGcMapAtOffset(os, oat_method, code_item, offset);
574 }
575 }
576
577 void DumpInvokeStub(std::ostream& os, const OatFile::OatMethod& oat_method) {
578 const uint8_t* begin = reinterpret_cast<const uint8_t*>(oat_method.GetInvokeStub());
579 const uint8_t* end = begin + oat_method.GetInvokeStubSize();
580 disassembler_->Dump(os, begin, end);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800581 }
582
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700583 const std::string host_prefix_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800584 const OatFile& oat_file_;
585 std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800586 std::set<uint32_t> offsets_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800587 UniquePtr<Disassembler> disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700588};
589
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800590class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700591 public:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800592 explicit ImageDumper(std::ostream& os, const std::string& image_filename,
593 const std::string& host_prefix, Space& image_space,
Ian Rogersca190662012-06-26 15:45:57 -0700594 const ImageHeader& image_header)
595 : os_(os), image_filename_(image_filename), host_prefix_(host_prefix),
596 image_space_(image_space), image_header_(image_header) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700597
Ian Rogersb726dcb2012-09-05 08:57:23 -0700598 void Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800599 os_ << "MAGIC:\n";
600 os_ << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700601
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800602 os_ << "IMAGE BEGIN:\n";
603 os_ << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700604
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800605 os_ << "OAT CHECKSUM:\n";
606 os_ << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700607
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800608 os_ << "OAT BEGIN:\n";
609 os_ << reinterpret_cast<void*>(image_header_.GetOatBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700610
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800611 os_ << "OAT END:\n";
612 os_ << reinterpret_cast<void*>(image_header_.GetOatEnd()) << "\n\n";
613
614 os_ << "ROOTS:\n";
615 os_ << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700616 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700617 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
618 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700619 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800620 Object* image_root_object = image_header_.GetImageRoot(image_root);
621 os_ << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700622 if (image_root_object->IsObjectArray()) {
623 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
624 ObjectArray<Object>* image_root_object_array
625 = down_cast<ObjectArray<Object>*>(image_root_object);
626 // = image_root_object->AsObjectArray<Object>();
627 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800628 Object* value = image_root_object_array->Get(i);
629 if (value != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800630 os_ << "\t" << i << ": ";
Ian Rogersd5b32602012-02-26 16:40:04 -0800631 std::string summary;
632 PrettyObjectValue(summary, value->GetClass(), value);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800633 os_ << summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800634 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800635 os_ << StringPrintf("\t%d: null\n", i);
Ian Rogersd5b32602012-02-26 16:40:04 -0800636 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700637 }
638 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700639 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800640 os_ << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700641
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800642 os_ << "OAT LOCATION:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700643 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800644 Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation);
645 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800646 os_ << oat_location;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800647 if (!host_prefix_.empty()) {
648 oat_location = host_prefix_ + oat_location;
649 os_ << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700650 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800651 os_ << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800652 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700653 if (oat_file == NULL) {
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800654 os_ << "NOT FOUND\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700655 return;
656 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800657 os_ << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700658
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800659 stats_.oat_file_bytes = oat_file->Size();
660
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700661 oat_dumper_.reset(new OatDumper(host_prefix_, *oat_file));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800662
663 os_ << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700664
665 // Loop through all the image spaces and dump their objects.
666 Heap* heap = Runtime::Current()->GetHeap();
667 const Spaces& spaces = heap->GetSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -0700668 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700669 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700670 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700671 heap->FlushAllocStack();
672 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700673 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700674 // TODO: C++0x auto
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700675 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
676 Space* space = *it;
677 if (space->IsImageSpace()) {
678 ImageSpace* image_space = space->AsImageSpace();
679 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
680 os_ << "\n";
681 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700682 }
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700683 // Dump the large objects separately.
684 heap->GetLargeObjectsSpace()->GetLiveObjects()->Walk(ImageDumper::Callback, this);
685 os_ << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800686
687 os_ << "STATS:\n" << std::flush;
688 UniquePtr<File> file(OS::OpenFile(image_filename_.c_str(), false));
689 stats_.file_bytes = file->Length();
690 size_t header_bytes = sizeof(ImageHeader);
691 stats_.header_bytes = header_bytes;
692 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
693 stats_.alignment_bytes += alignment_bytes;
694 stats_.Dump(os_);
695 os_ << "\n";
696
697 os_ << std::flush;
698
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800699 oat_dumper_->Dump(os_);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700700 }
701
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700702 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700703 static void PrettyObjectValue(std::string& summary, Class* type, Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700704 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800705 CHECK(type != NULL);
706 if (value == NULL) {
707 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
708 } else if (type->IsStringClass()) {
709 String* string = value->AsString();
710 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
711 } else if (value->IsClass()) {
712 Class* klass = value->AsClass();
713 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
714 } else if (value->IsField()) {
715 Field* field = value->AsField();
716 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
717 } else if (value->IsMethod()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700718 AbstractMethod* method = value->AsMethod();
Ian Rogersd5b32602012-02-26 16:40:04 -0800719 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
720 } else {
721 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
722 }
723 }
724
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700725 static void PrintField(std::string& summary, Field* field, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700726 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800727 FieldHelper fh(field);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700728 const char* descriptor = fh.GetTypeDescriptor();
Ian Rogersd5b32602012-02-26 16:40:04 -0800729 StringAppendF(&summary, "\t%s: ", fh.GetName());
Ian Rogers48efc2b2012-08-27 17:20:31 -0700730 if (descriptor[0] != 'L' && descriptor[0] != '[') {
731 Class* type = fh.GetType();
732 if (type->IsPrimitiveLong()) {
733 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
734 } else if (type->IsPrimitiveDouble()) {
735 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
736 } else if (type->IsPrimitiveFloat()) {
737 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
738 } else {
739 DCHECK(type->IsPrimitive());
740 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
741 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800742 } else {
Ian Rogers48efc2b2012-08-27 17:20:31 -0700743 // Get the value, don't compute the type unless it is non-null as we don't want
744 // to cause class loading.
Ian Rogersd5b32602012-02-26 16:40:04 -0800745 Object* value = field->GetObj(obj);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700746 if (value == NULL) {
747 StringAppendF(&summary, "null %s\n", PrettyDescriptor(descriptor).c_str());
748 } else {
749 PrettyObjectValue(summary, fh.GetType(), value);
750 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800751 }
752 }
753
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700754 static void DumpFields(std::string& summary, Object* obj, Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700755 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800756 Class* super = klass->GetSuperClass();
757 if (super != NULL) {
758 DumpFields(summary, obj, super);
759 }
760 ObjectArray<Field>* fields = klass->GetIFields();
761 if (fields != NULL) {
762 for (int32_t i = 0; i < fields->GetLength(); i++) {
763 Field* field = fields->Get(i);
764 PrintField(summary, field, obj);
765 }
766 }
767 }
768
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800769 bool InDumpSpace(const Object* object) {
770 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700771 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800772
Mathieu Chartier66f19252012-09-18 08:57:04 -0700773 const void* GetOatCodeBegin(AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700774 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800775 Runtime* runtime = Runtime::Current();
776 const void* code = m->GetCode();
Ian Rogersfb6adba2012-03-04 21:51:51 -0800777 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800778 code = oat_dumper_->GetOatCode(m);
779 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700780 if (oat_dumper_->GetInstructionSet() == kThumb2) {
781 code = reinterpret_cast<void*>(reinterpret_cast<uint32_t>(code) & ~0x1);
782 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800783 return code;
784 }
785
Mathieu Chartier66f19252012-09-18 08:57:04 -0700786 uint32_t GetOatCodeSize(AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700787 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700788 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetOatCodeBegin(m));
789 if (oat_code_begin == NULL) {
790 return 0;
791 }
792 return oat_code_begin[-1];
793 }
794
Mathieu Chartier66f19252012-09-18 08:57:04 -0700795 const void* GetOatCodeEnd(AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700796 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700797 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetOatCodeBegin(m));
798 if (oat_code_begin == NULL) {
799 return NULL;
800 }
801 return oat_code_begin + GetOatCodeSize(m);
802 }
803
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700804 static void Callback(Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700805 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700806 DCHECK(obj != NULL);
807 DCHECK(arg != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800808 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700809 if (!state->InDumpSpace(obj)) {
810 return;
811 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700812
813 size_t object_bytes = obj->SizeOf();
814 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
815 state->stats_.object_bytes += object_bytes;
816 state->stats_.alignment_bytes += alignment_bytes;
817
Brian Carlstrom78128a62011-09-15 17:21:19 -0700818 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800819 Class* obj_class = obj->GetClass();
820 if (obj_class->IsArrayClass()) {
821 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
822 obj->AsArray()->GetLength());
823 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700824 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800825 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
826 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700827 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800828 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700829 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800830 } else if (obj->IsField()) {
831 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
832 PrettyField(obj->AsField()).c_str());
833 } else if (obj->IsMethod()) {
834 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
835 PrettyMethod(obj->AsMethod()).c_str());
836 } else if (obj_class->IsStringClass()) {
Elliott Hughes82914b62012-04-09 15:56:29 -0700837 StringAppendF(&summary, "%p: java.lang.String %s\n", obj,
838 PrintableString(obj->AsString()->ToModifiedUtf8()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800839 } else {
840 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
841 }
842 DumpFields(summary, obj, obj_class);
843 if (obj->IsObjectArray()) {
844 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
845 int32_t length = obj_array->GetLength();
846 for (int32_t i = 0; i < length; i++) {
847 Object* value = obj_array->Get(i);
848 size_t run = 0;
849 for (int32_t j = i + 1; j < length; j++) {
850 if (value == obj_array->Get(j)) {
851 run++;
852 } else {
853 break;
854 }
855 }
856 if (run == 0) {
857 StringAppendF(&summary, "\t%d: ", i);
858 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800859 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800860 i = i + run;
861 }
862 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
863 PrettyObjectValue(summary, value_class, value);
864 }
865 } else if (obj->IsClass()) {
866 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
867 if (sfields != NULL) {
868 summary += "\t\tSTATICS:\n";
869 for (int32_t i = 0; i < sfields->GetLength(); i++) {
870 Field* field = sfields->Get(i);
871 PrintField(summary, field, NULL);
872 }
873 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700874 } else if (obj->IsMethod()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700875 AbstractMethod* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700876 if (method->IsNative()) {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700877 DCHECK(method->GetNativeGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700878 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800879 bool first_occurrence;
880 size_t invoke_stub_size = state->ComputeOatSize(
881 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
882 if (first_occurrence) {
883 state->stats_.managed_to_native_code_bytes += invoke_stub_size;
884 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700885 const void* oat_code = state->GetOatCodeBegin(method);
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700886 uint32_t oat_code_size = state->GetOatCodeSize(method);
887 state->ComputeOatSize(oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800888 if (first_occurrence) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700889 state->stats_.native_to_managed_code_bytes += oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800890 }
891 if (oat_code != method->GetCode()) {
892 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
893 }
Ian Rogers19846512012-02-24 11:42:47 -0800894 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
895 method->IsResolutionMethod()) {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700896 DCHECK(method->GetNativeGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700897 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700898 } else {
TDYa1273db52852012-04-01 15:11:43 -0700899#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogers0c7abda2012-09-19 13:33:42 -0700900 DCHECK(method->GetNativeGcMap() != NULL) << PrettyMethod(method);
TDYa1273db52852012-04-01 15:11:43 -0700901#endif
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700902
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800903 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700904 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700905 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800906
Elliott Hughesa0e18062012-04-13 15:59:59 -0700907 bool first_occurrence;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700908 size_t gc_map_bytes = state->ComputeOatSize(method->GetNativeGcMap(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -0700909 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800910 state->stats_.gc_map_bytes += gc_map_bytes;
911 }
912
913 size_t pc_mapping_table_bytes =
Elliott Hughesa0e18062012-04-13 15:59:59 -0700914 state->ComputeOatSize(method->GetMappingTableRaw(), &first_occurrence);
915 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800916 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
917 }
918
919 size_t vmap_table_bytes =
Elliott Hughesa0e18062012-04-13 15:59:59 -0700920 state->ComputeOatSize(method->GetVmapTableRaw(), &first_occurrence);
921 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800922 state->stats_.vmap_table_bytes += vmap_table_bytes;
923 }
924
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700925 // TODO: compute invoke stub using length from oat file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800926 size_t invoke_stub_size = state->ComputeOatSize(
Elliott Hughesa0e18062012-04-13 15:59:59 -0700927 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
928 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800929 state->stats_.native_to_managed_code_bytes += invoke_stub_size;
930 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700931 const void* oat_code_begin = state->GetOatCodeBegin(method);
932 const void* oat_code_end = state->GetOatCodeEnd(method);
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700933 uint32_t oat_code_size = state->GetOatCodeSize(method);
Elliott Hughesa0e18062012-04-13 15:59:59 -0700934 state->ComputeOatSize(oat_code_begin, &first_occurrence);
935 if (first_occurrence) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700936 state->stats_.managed_code_bytes += oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -0700937 if (method->IsConstructor()) {
938 if (method->IsStatic()) {
939 state->stats_.class_initializer_code_bytes += oat_code_size;
940 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
941 state->stats_.large_initializer_code_bytes += oat_code_size;
942 }
943 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
944 state->stats_.large_method_code_bytes += oat_code_size;
945 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800946 }
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700947 state->stats_.managed_code_bytes_ignoring_deduplication += oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800948
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700949 StringAppendF(&summary, "\t\tOAT CODE: %p-%p\n", oat_code_begin, oat_code_end);
Ian Rogersd5b32602012-02-26 16:40:04 -0800950 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800951 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
952
953 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700954 vmap_table_bytes + invoke_stub_size + oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800955
956 double expansion =
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700957 static_cast<double>(oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800958 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700959 }
960 }
Elliott Hughesa0e18062012-04-13 15:59:59 -0700961 state->stats_.Update(ClassHelper(obj_class).GetDescriptor(), object_bytes);
Ian Rogersd5b32602012-02-26 16:40:04 -0800962
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700963 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700964 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700965
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800966 std::set<const void*> already_seen_;
967 // Compute the size of the given data within the oat file and whether this is the first time
968 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -0700969 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800970 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700971 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800972 already_seen_.insert(oat_data);
973 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700974 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800975 }
976 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700977 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700978
979 public:
980 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800981 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700982 size_t file_bytes;
983
984 size_t header_bytes;
985 size_t object_bytes;
986 size_t alignment_bytes;
987
988 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800989 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700990 size_t managed_to_native_code_bytes;
991 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -0700992 size_t class_initializer_code_bytes;
993 size_t large_initializer_code_bytes;
994 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700995
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800996 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700997 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800998 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700999
1000 size_t dex_instruction_bytes;
1001
Mathieu Chartier66f19252012-09-18 08:57:04 -07001002 std::vector<AbstractMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001003 std::vector<size_t> method_outlier_size;
1004 std::vector<double> method_outlier_expansion;
1005
1006 explicit Stats()
1007 : oat_file_bytes(0),
1008 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001009 header_bytes(0),
1010 object_bytes(0),
1011 alignment_bytes(0),
1012 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001013 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001014 managed_to_native_code_bytes(0),
1015 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001016 class_initializer_code_bytes(0),
1017 large_initializer_code_bytes(0),
1018 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001019 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001020 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001021 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001022 dex_instruction_bytes(0) {}
1023
Elliott Hughesa0e18062012-04-13 15:59:59 -07001024 struct SizeAndCount {
1025 SizeAndCount(size_t bytes, size_t count) : bytes(bytes), count(count) {}
1026 size_t bytes;
1027 size_t count;
1028 };
1029 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1030 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001031
Elliott Hughesa0e18062012-04-13 15:59:59 -07001032 void Update(const std::string& descriptor, size_t object_bytes) {
1033 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1034 if (it != sizes_and_counts.end()) {
1035 it->second.bytes += object_bytes;
1036 it->second.count += 1;
1037 } else {
1038 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes, 1));
1039 }
1040 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001041
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001042 double PercentOfOatBytes(size_t size) {
1043 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1044 }
1045
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001046 double PercentOfFileBytes(size_t size) {
1047 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1048 }
1049
1050 double PercentOfObjectBytes(size_t size) {
1051 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1052 }
1053
Mathieu Chartier66f19252012-09-18 08:57:04 -07001054 void ComputeOutliers(size_t total_size, double expansion, AbstractMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001055 method_outlier_size.push_back(total_size);
1056 method_outlier_expansion.push_back(expansion);
1057 method_outlier.push_back(method);
1058 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001059
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001060 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001061 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001062 size_t sum_of_sizes = 0;
1063 size_t sum_of_sizes_squared = 0;
1064 size_t sum_of_expansion = 0;
1065 size_t sum_of_expansion_squared = 0;
1066 size_t n = method_outlier_size.size();
1067 for (size_t i = 0; i < n; i++) {
1068 size_t cur_size = method_outlier_size[i];
1069 sum_of_sizes += cur_size;
1070 sum_of_sizes_squared += cur_size * cur_size;
1071 double cur_expansion = method_outlier_expansion[i];
1072 sum_of_expansion += cur_expansion;
1073 sum_of_expansion_squared += cur_expansion * cur_expansion;
1074 }
1075 size_t size_mean = sum_of_sizes / n;
1076 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1077 double expansion_mean = sum_of_expansion / n;
1078 double expansion_variance =
1079 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1080
1081 // Dump methods whose size is a certain number of standard deviations from the mean
1082 size_t dumped_values = 0;
1083 size_t skipped_values = 0;
1084 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1085 size_t cur_size_variance = i * i * size_variance;
1086 bool first = true;
1087 for (size_t j = 0; j < n; j++) {
1088 size_t cur_size = method_outlier_size[j];
1089 if (cur_size > size_mean) {
1090 size_t cur_var = cur_size - size_mean;
1091 cur_var = cur_var * cur_var;
1092 if (cur_var > cur_size_variance) {
1093 if (dumped_values > 20) {
1094 if (i == 1) {
1095 skipped_values++;
1096 } else {
1097 i = 2; // jump to counting for 1 standard deviation
1098 break;
1099 }
1100 } else {
1101 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001102 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001103 first = false;
1104 }
1105 os << "\t" << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001106 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001107 method_outlier_size[j] = 0; // don't consider this method again
1108 dumped_values++;
1109 }
1110 }
1111 }
1112 }
1113 }
1114 if (skipped_values > 0) {
1115 os << "\t... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001116 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001117 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001118 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001119
1120 // Dump methods whose expansion is a certain number of standard deviations from the mean
1121 dumped_values = 0;
1122 skipped_values = 0;
1123 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1124 double cur_expansion_variance = i * i * expansion_variance;
1125 bool first = true;
1126 for (size_t j = 0; j < n; j++) {
1127 double cur_expansion = method_outlier_expansion[j];
1128 if (cur_expansion > expansion_mean) {
1129 size_t cur_var = cur_expansion - expansion_mean;
1130 cur_var = cur_var * cur_var;
1131 if (cur_var > cur_expansion_variance) {
1132 if (dumped_values > 20) {
1133 if (i == 1) {
1134 skipped_values++;
1135 } else {
1136 i = 2; // jump to counting for 1 standard deviation
1137 break;
1138 }
1139 } else {
1140 if (first) {
1141 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001142 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001143 first = false;
1144 }
1145 os << "\t" << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001146 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001147 method_outlier_expansion[j] = 0.0; // don't consider this method again
1148 dumped_values++;
1149 }
1150 }
1151 }
1152 }
1153 }
1154 if (skipped_values > 0) {
1155 os << "\t... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001156 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001157 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001158 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001159 }
1160
Ian Rogersb726dcb2012-09-05 08:57:23 -07001161 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001162 os << "\tart_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1163 << "\tart_file_bytes = header_bytes + object_bytes + alignment_bytes\n"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001164 << StringPrintf("\theader_bytes = %8zd (%2.0f%% of art file bytes)\n"
1165 "\tobject_bytes = %8zd (%2.0f%% of art file bytes)\n"
Elliott Hughesc073b072012-05-24 19:29:17 -07001166 "\talignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001167 header_bytes, PercentOfFileBytes(header_bytes),
1168 object_bytes, PercentOfFileBytes(object_bytes),
1169 alignment_bytes, PercentOfFileBytes(alignment_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001170 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001171
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001172 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
1173
Elliott Hughesa0e18062012-04-13 15:59:59 -07001174 os << "\tobject_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001175 size_t object_bytes_total = 0;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001176 typedef SizeAndCountTable::const_iterator It; // TODO: C++0x auto
1177 for (It it = sizes_and_counts.begin(), end = sizes_and_counts.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -08001178 const std::string& descriptor(it->first);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001179 double average = static_cast<double>(it->second.bytes) / static_cast<double>(it->second.count);
1180 double percent = PercentOfObjectBytes(it->second.bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001181 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001182 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
1183 descriptor.c_str(), it->second.bytes, it->second.count,
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001184 average, percent);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001185 object_bytes_total += it->second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001186 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001187 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001188 CHECK_EQ(object_bytes, object_bytes_total);
1189
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001190 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1191 "\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers0d2d3782012-04-10 11:09:18 -07001192 "\tnative_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1193 "\tclass_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1194 "\tlarge_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Elliott Hughesc073b072012-05-24 19:29:17 -07001195 "\tlarge_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001196 managed_code_bytes, PercentOfOatBytes(managed_code_bytes),
1197 managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001198 native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes),
1199 class_initializer_code_bytes, PercentOfOatBytes(class_initializer_code_bytes),
1200 large_initializer_code_bytes, PercentOfOatBytes(large_initializer_code_bytes),
1201 large_method_code_bytes, PercentOfOatBytes(large_method_code_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001202 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001203
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001204 os << StringPrintf("\tgc_map_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1205 "\tpc_mapping_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
Elliott Hughesc073b072012-05-24 19:29:17 -07001206 "\tvmap_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001207 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1208 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1209 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001210 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001211
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001212 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes)
Elliott Hughesc073b072012-05-24 19:29:17 -07001213 << StringPrintf("\tmanaged_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
1214 static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes),
1215 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001216 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001217 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001218
1219 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001220 }
1221 } stats_;
1222
1223 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001224 enum {
1225 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1226 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1227 kLargeConstructorDexBytes = 4000,
1228 // Number of bytes for a method to be considered large. Based on the 4000 basic block
1229 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1230 kLargeMethodDexBytes = 16000
1231 };
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001232 UniquePtr<OatDumper> oat_dumper_;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001233 std::ostream& os_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001234 const std::string image_filename_;
1235 const std::string host_prefix_;
1236 Space& image_space_;
1237 const ImageHeader& image_header_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001238
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001239 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001240};
1241
Elliott Hughes72395bf2012-04-24 13:45:26 -07001242static int oatdump(int argc, char** argv) {
Elliott Hughes0d39c122012-06-06 16:41:17 -07001243 InitLogging(argv);
Elliott Hughes72395bf2012-04-24 13:45:26 -07001244
Brian Carlstrom78128a62011-09-15 17:21:19 -07001245 // Skip over argv[0].
1246 argv++;
1247 argc--;
1248
1249 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001250 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001251 usage();
1252 }
1253
Brian Carlstromaded5f72011-10-07 17:15:04 -07001254 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001255 const char* image_filename = NULL;
1256 const char* boot_image_filename = NULL;
Logan Chien0cc6ab62012-03-20 22:57:52 +08001257 std::string elf_filename_prefix;
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001258 UniquePtr<std::string> host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001259 std::ostream* os = &std::cout;
1260 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001261
1262 for (int i = 0; i < argc; i++) {
1263 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001264 if (option.starts_with("--oat-file=")) {
1265 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001266 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001267 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001268 } else if (option.starts_with("--boot-image=")) {
1269 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001270 } else if (option.starts_with("--host-prefix=")) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001271 host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001272 } else if (option.starts_with("--output=")) {
1273 const char* filename = option.substr(strlen("--output=")).data();
1274 out.reset(new std::ofstream(filename));
1275 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001276 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001277 usage();
1278 }
1279 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001280 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001281 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001282 usage();
1283 }
1284 }
1285
Brian Carlstromaded5f72011-10-07 17:15:04 -07001286 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001287 fprintf(stderr, "Either --image or --oat must be specified\n");
1288 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001289 }
1290
Brian Carlstromaded5f72011-10-07 17:15:04 -07001291 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001292 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1293 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001294 }
1295
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001296 if (host_prefix.get() == NULL) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001297 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
1298 if (android_product_out != NULL) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001299 host_prefix.reset(new std::string(android_product_out));
1300 } else {
1301 host_prefix.reset(new std::string(""));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001302 }
1303 }
1304
Brian Carlstromaded5f72011-10-07 17:15:04 -07001305 if (oat_filename != NULL) {
Logan Chien0c717dd2012-03-28 18:31:07 +08001306 OatFile* oat_file =
1307 OatFile::Open(oat_filename, oat_filename, NULL, OatFile::kRelocNone);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001308 if (oat_file == NULL) {
1309 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
1310 return EXIT_FAILURE;
1311 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001312 OatDumper oat_dumper(*host_prefix.get(), *oat_file);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001313 oat_dumper.Dump(*os);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001314 return EXIT_SUCCESS;
1315 }
1316
Brian Carlstrom78128a62011-09-15 17:21:19 -07001317 Runtime::Options options;
1318 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001319 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001320 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001321 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001322 if (boot_image_filename != NULL) {
1323 boot_image_option += "-Ximage:";
1324 boot_image_option += boot_image_filename;
1325 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001326 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001327 if (image_filename != NULL) {
1328 image_option += "-Ximage:";
1329 image_option += image_filename;
1330 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1331 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001332
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001333 if (!host_prefix->empty()) {
1334 options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001335 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001336
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001337 if (!Runtime::Create(options, false)) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001338 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001339 return EXIT_FAILURE;
1340 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001341 UniquePtr<Runtime> runtime(Runtime::Current());
1342 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
1343 // give it away now and then switch to a more managable ScopedObjectAccess.
1344 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
1345 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001346
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001347 Heap* heap = Runtime::Current()->GetHeap();
Brian Carlstromfddf6f62012-03-15 16:56:45 -07001348 ImageSpace* image_space = heap->GetImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001349 CHECK(image_space != NULL);
1350 const ImageHeader& image_header = image_space->GetImageHeader();
1351 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001352 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001353 return EXIT_FAILURE;
1354 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001355 ImageDumper image_dumper(*os, image_filename, *host_prefix.get(), *image_space, image_header);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001356 image_dumper.Dump();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001357 return EXIT_SUCCESS;
1358}
1359
1360} // namespace art
1361
1362int main(int argc, char** argv) {
1363 return art::oatdump(argc, argv);
1364}