blob: b1aa47e987d5800d884570144f5a4f080bbba050 [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"
Brian Carlstrom78128a62011-09-15 17:21:19 -070029#include "image.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070030#include "oat/runtime/context.h" // For VmapTable
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080031#include "object_utils.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080032#include "os.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070033#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070034#include "safe_map.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070035#include "space.h"
36#include "stringpiece.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070037#include "verifier/gc_map.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070038
39namespace art {
40
41static void usage() {
42 fprintf(stderr,
43 "Usage: oatdump [options] ...\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080044 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n"
45 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070046 "\n");
47 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080048 " --oat-file=<file.oat>: specifies an input oat filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080049 " Example: --image=/system/framework/boot.oat\n"
Brian Carlstromaded5f72011-10-07 17:15:04 -070050 "\n");
51 fprintf(stderr,
52 " --image=<file.art>: specifies an input image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080053 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070054 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070055 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070056 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080057 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070058 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070059 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070060 " --host-prefix may be used to translate host paths to target paths during\n"
61 " cross compilation.\n"
62 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstromfe487d02012-02-29 18:49:16 -080063 " Default: $ANDROID_PRODUCT_OUT\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070064 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070065 fprintf(stderr,
66 " --output=<file> may be used to send the output to a file.\n"
67 " Example: --output=/tmp/oatdump.txt\n"
68 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070069 exit(EXIT_FAILURE);
70}
71
Ian Rogersff1ed472011-09-20 13:46:24 -070072const char* image_roots_descriptions_[] = {
Brian Carlstrom78128a62011-09-15 17:21:19 -070073 "kJniStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070074 "kAbstractMethodErrorStubArray",
Ian Rogersad25ac52011-10-04 19:13:33 -070075 "kStaticResolutionStubArray",
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070076 "kUnknownMethodResolutionStubArray",
Ian Rogers19846512012-02-24 11:42:47 -080077 "kResolutionMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070078 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070079 "kRefsOnlySaveMethod",
80 "kRefsAndArgsSaveMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070081 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070082 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070083 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070084};
85
Elliott Hughese3c845c2012-02-28 17:23:01 -080086class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -070087 public:
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070088 explicit OatDumper(const std::string& host_prefix, const OatFile& oat_file)
89 : host_prefix_(host_prefix),
90 oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -080091 oat_dex_files_(oat_file.GetOatDexFiles()),
92 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet())) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080093 AddAllOffsets();
94 }
95
96 void Dump(std::ostream& os) {
97 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -070098
99 os << "MAGIC:\n";
100 os << oat_header.GetMagic() << "\n\n";
101
102 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800103 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700104
Elliott Hughesa72ec822012-03-05 17:12:22 -0800105 os << "INSTRUCTION SET:\n";
106 os << oat_header.GetInstructionSet() << "\n\n";
107
Brian Carlstromaded5f72011-10-07 17:15:04 -0700108 os << "DEX FILE COUNT:\n";
109 os << oat_header.GetDexFileCount() << "\n\n";
110
111 os << "EXECUTABLE OFFSET:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800112 os << StringPrintf("0x%08x\n\n", oat_header.GetExecutableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700113
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700114 os << "IMAGE FILE LOCATION CHECKSUM:\n";
115 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationChecksum());
116
117 os << "IMAGE FILE LOCATION:\n";
118 const std::string image_file_location(oat_header.GetImageFileLocation());
119 os << image_file_location;
120 if (!image_file_location.empty() && !host_prefix_.empty()) {
121 os << " (" << host_prefix_ << image_file_location << ")";
122 }
123 os << "\n\n";
124
Ian Rogers30fab402012-01-23 15:43:46 -0800125 os << "BEGIN:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800126 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700127
Ian Rogers30fab402012-01-23 15:43:46 -0800128 os << "END:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800129 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700130
131 os << std::flush;
132
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800133 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
134 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Brian Carlstromaded5f72011-10-07 17:15:04 -0700135 CHECK(oat_dex_file != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800136 DumpOatDexFile(os, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700137 }
138 }
139
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800140 size_t ComputeSize(const void* oat_data) {
141 if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() ||
142 reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) {
143 return 0; // Address not in oat file
144 }
145 uint32_t begin_offset = reinterpret_cast<size_t>(oat_data) -
146 reinterpret_cast<size_t>(oat_file_.Begin());
147 typedef std::set<uint32_t>::iterator It;
148 It it = offsets_.upper_bound(begin_offset);
149 CHECK(it != offsets_.end());
150 uint32_t end_offset = *it;
151 return end_offset - begin_offset;
152 }
153
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700154 InstructionSet GetInstructionSet() {
155 return oat_file_.GetOatHeader().GetInstructionSet();
156 }
157
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800158 const void* GetOatCode(Method* m) {
159 MethodHelper mh(m);
160 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
161 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
162 CHECK(oat_dex_file != NULL);
163 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
164 if (dex_file.get() != NULL) {
165 uint32_t class_def_index;
166 bool found = dex_file->FindClassDefIndex(mh.GetDeclaringClassDescriptor(), class_def_index);
167 if (found) {
168 const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_index);
169 CHECK(oat_class != NULL);
170 size_t method_index = m->GetMethodIndex();
171 return oat_class->GetOatMethod(method_index).GetCode();
172 }
173 }
174 }
175 return NULL;
176 }
177
Brian Carlstromaded5f72011-10-07 17:15:04 -0700178 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800179 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800180 // We don't know the length of the code for each method, but we need to know where to stop
181 // when disassembling. What we do know is that a region of code will be followed by some other
182 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
183 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800184 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
185 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800186 CHECK(oat_dex_file != NULL);
187 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
188 if (dex_file.get() == NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800189 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800190 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800191 offsets_.insert(reinterpret_cast<uint32_t>(&dex_file->GetHeader()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800192 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
193 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
194 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index));
195 const byte* class_data = dex_file->GetClassData(class_def);
196 if (class_data != NULL) {
197 ClassDataItemIterator it(*dex_file, class_data);
198 SkipAllFields(it);
199 uint32_t class_method_index = 0;
200 while (it.HasNextDirectMethod()) {
201 AddOffsets(oat_class->GetOatMethod(class_method_index++));
202 it.Next();
203 }
204 while (it.HasNextVirtualMethod()) {
205 AddOffsets(oat_class->GetOatMethod(class_method_index++));
206 it.Next();
207 }
208 }
209 }
210 }
211
212 // If the last thing in the file is code for a method, there won't be an offset for the "next"
213 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
214 // for the end of the file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800215 offsets_.insert(static_cast<uint32_t>(oat_file_.End() - oat_file_.Begin()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800216 }
217
218 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800219 uint32_t code_offset = oat_method.GetCodeOffset();
220 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
221 code_offset &= ~0x1;
222 }
223 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800224 offsets_.insert(oat_method.GetMappingTableOffset());
225 offsets_.insert(oat_method.GetVmapTableOffset());
226 offsets_.insert(oat_method.GetGcMapOffset());
227 offsets_.insert(oat_method.GetInvokeStubOffset());
228 }
229
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800230 void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700231 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800232 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800233 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800234 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile());
235 if (dex_file.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700236 os << "NOT FOUND\n\n";
237 return;
238 }
239 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
240 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
241 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700242 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
243 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800244 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800245 << oat_class->GetStatus() << ")\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800246 DumpOatClass(os, *oat_class.get(), *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700247 }
248
249 os << std::flush;
250 }
251
Elliott Hughese3c845c2012-02-28 17:23:01 -0800252 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700253 while (it.HasNextStaticField()) {
254 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700255 }
Ian Rogers0571d352011-11-03 19:51:38 -0700256 while (it.HasNextInstanceField()) {
257 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700258 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800259 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700260
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800261 void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
262 const DexFile::ClassDef& class_def) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800263 const byte* class_data = dex_file.GetClassData(class_def);
264 if (class_data == NULL) { // empty class such as a marker interface?
265 return;
266 }
267 ClassDataItemIterator it(dex_file, class_data);
268 SkipAllFields(it);
269
270 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700271 while (it.HasNextDirectMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800272 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800273 DumpOatMethod(os, class_method_index, oat_method, dex_file,
274 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800275 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700276 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700277 }
Ian Rogers0571d352011-11-03 19:51:38 -0700278 while (it.HasNextVirtualMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800279 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800280 DumpOatMethod(os, class_method_index, oat_method, dex_file,
281 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800282 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700283 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700284 }
Ian Rogers0571d352011-11-03 19:51:38 -0700285 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700286 os << std::flush;
287 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800288
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800289 void DumpOatMethod(std::ostream& os, uint32_t class_method_index,
Elliott Hughese3c845c2012-02-28 17:23:01 -0800290 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
291 uint32_t dex_method_idx, const DexFile::CodeItem* code_item) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700292 os << StringPrintf("\t%d: %s (dex_method_idx=%d)\n",
293 class_method_index, PrettyMethod(dex_method_idx, dex_file, true).c_str(),
294 dex_method_idx);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800295 os << StringPrintf("\t\tframe_size_in_bytes: %zd\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800296 oat_method.GetFrameSizeInBytes());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800297 os << StringPrintf("\t\tcore_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800298 oat_method.GetCoreSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800299 DumpSpillMask(os, oat_method.GetCoreSpillMask(), false);
300 os << StringPrintf("\n\t\tfp_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800301 oat_method.GetFpSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800302 DumpSpillMask(os, oat_method.GetFpSpillMask(), true);
303 os << StringPrintf("\n\t\tmapping_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800304 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800305 DumpMappingTable(os, oat_method);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800306 os << StringPrintf("\t\tvmap_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800307 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800308 DumpVmap(os, oat_method.GetVmapTable(), oat_method.GetCoreSpillMask(),
309 oat_method.GetFpSpillMask());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800310 os << StringPrintf("\t\tgc_map: %p (offset=0x%08x)\n",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800311 oat_method.GetGcMap(), oat_method.GetGcMapOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800312 DumpGcMap(os, oat_method.GetGcMap());
Logan Chien971bf3f2012-05-01 15:47:55 +0800313 os << StringPrintf("\t\tCODE: %p (offset=0x%08x size=%d)%s\n",
Elliott Hughes77405792012-03-15 15:22:12 -0700314 oat_method.GetCode(),
315 oat_method.GetCodeOffset(),
316 oat_method.GetCodeSize(),
317 oat_method.GetCode() != NULL ? "..." : "");
Ian Rogersd3fb5692012-04-09 21:56:13 -0700318 DumpCode(os, oat_method.GetCode(), oat_method.GetCodeSize(), oat_method.GetMappingTable(),
319 dex_file, code_item);
Logan Chien971bf3f2012-05-01 15:47:55 +0800320 os << StringPrintf("\t\tINVOKE STUB: %p (offset=0x%08x size=%d)%s\n",
Elliott Hughes77405792012-03-15 15:22:12 -0700321 oat_method.GetInvokeStub(),
322 oat_method.GetInvokeStubOffset(),
323 oat_method.GetInvokeStubSize(),
324 oat_method.GetInvokeStub() != NULL ? "..." : "");
Ian Rogersd3fb5692012-04-09 21:56:13 -0700325 DumpCode(os, reinterpret_cast<const void*>(oat_method.GetInvokeStub()),
326 oat_method.GetInvokeStubSize(), NULL, dex_file, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700327 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800328
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800329 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
330 if (spill_mask == 0) {
331 return;
332 }
333 os << " (";
334 for (size_t i = 0; i < 32; i++) {
335 if ((spill_mask & (1 << i)) != 0) {
336 if (is_float) {
337 os << "fr" << i;
338 } else {
339 os << "r" << i;
340 }
341 spill_mask ^= 1 << i; // clear bit
342 if (spill_mask != 0) {
343 os << ", ";
344 } else {
345 break;
346 }
347 }
348 }
349 os << ")";
350 }
351
352 void DumpVmap(std::ostream& os, const uint16_t* raw_table, uint32_t core_spill_mask,
353 uint32_t fp_spill_mask) {
354 if (raw_table == NULL) {
355 return;
356 }
357 const VmapTable vmap_table(raw_table);
358 bool first = true;
359 os << "\t\t\t";
360 for (size_t i = 0; i < vmap_table.size(); i++) {
361 uint16_t dex_reg = vmap_table[i];
362 size_t matches = 0;
363 size_t spill_shifts = 0;
364 uint32_t spill_mask = core_spill_mask;
365 bool processing_fp = false;
366 while (matches != (i + 1)) {
367 if (spill_mask == 0) {
368 CHECK(!processing_fp);
369 spill_mask = fp_spill_mask;
370 processing_fp = true;
371 }
372 matches += spill_mask & 1; // Add 1 if the low bit is set
373 spill_mask >>= 1;
374 spill_shifts++;
375 }
376 size_t arm_reg = spill_shifts - 1; // wind back one as we want the last match
377 os << (first ? "v" : ", v") << dex_reg;
378 if (arm_reg < 16) {
379 os << "/r" << arm_reg;
380 } else {
381 os << "/fr" << (arm_reg - 16);
382 }
383 if (first) {
384 first = false;
385 }
386 }
Elliott Hughesc073b072012-05-24 19:29:17 -0700387 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800388 }
389
390 void DumpGcMap(std::ostream& os, const uint8_t* gc_map_raw) {
391 if (gc_map_raw == NULL) {
392 return;
393 }
394 uint32_t gc_map_length = (gc_map_raw[0] << 24) | (gc_map_raw[1] << 16) |
395 (gc_map_raw[2] << 8) | (gc_map_raw[3] << 0);
396 verifier::PcToReferenceMap map(gc_map_raw + sizeof(uint32_t), gc_map_length);
397 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
398 os << StringPrintf("\t\t\t0x%04x", map.GetPC(entry));
399 size_t num_regs = map.RegWidth() * 8;
400 const uint8_t* reg_bitmap = map.GetBitMap(entry);
401 bool first = true;
402 for (size_t reg = 0; reg < num_regs; reg++) {
403 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
404 if (first) {
405 os << " v" << reg;
406 first = false;
407 } else {
408 os << ", v" << reg;
409 }
410 }
411 }
Elliott Hughesc073b072012-05-24 19:29:17 -0700412 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800413 }
414 }
415
416 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800417 const uint32_t* raw_table = oat_method.GetMappingTable();
418 const void* code = oat_method.GetCode();
419 if (raw_table == NULL || code == NULL) {
420 return;
421 }
422
423 uint32_t length = *raw_table;
424 ++raw_table;
425
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800426 os << "\t\t{";
Elliott Hughese3c845c2012-02-28 17:23:01 -0800427 for (size_t i = 0; i < length; i += 2) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800428 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800429 uint32_t dex_pc = raw_table[i + 1];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800430 os << StringPrintf("%p -> 0x%04x", native_pc, dex_pc);
431 if (i + 2 < length) {
432 os << ", ";
433 }
434 }
Elliott Hughesc073b072012-05-24 19:29:17 -0700435 os << "}\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800436 }
437
Ian Rogersd3fb5692012-04-09 21:56:13 -0700438 void DumpCode(std::ostream& os, const void* code, int code_size,
439 const uint32_t* raw_mapping_table,
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800440 const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Ian Rogersd3fb5692012-04-09 21:56:13 -0700441 if (code == NULL || code_size == 0) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800442 return;
443 }
444
Ian Rogersd3fb5692012-04-09 21:56:13 -0700445 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code);
446 const uint8_t* end_native_pc = native_pc + code_size;
447
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800448 if (raw_mapping_table == NULL) {
449 // code but no mapping table is most likely caused by code created by the JNI compiler
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800450 disassembler_->Dump(os, native_pc, end_native_pc);
451 return;
452 }
453
454 uint32_t length = *raw_mapping_table;
455 ++raw_mapping_table;
456
457 for (size_t i = 0; i < length; i += 2) {
458 uint32_t dex_pc = raw_mapping_table[i + 1];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800459 const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]);
460 os << StringPrintf("\t\t0x%04x: %s\n", dex_pc, instruction->DumpString(&dex_file).c_str());
461
Ian Rogersd3fb5692012-04-09 21:56:13 -0700462 const uint8_t* cur_pc = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i];
463 const uint8_t* cur_pc_end = NULL;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800464 if (i + 2 < length) {
Ian Rogersd3fb5692012-04-09 21:56:13 -0700465 cur_pc_end = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i + 2];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800466 } else {
Ian Rogersd3fb5692012-04-09 21:56:13 -0700467 cur_pc_end = end_native_pc;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800468 }
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700469 CHECK(cur_pc < cur_pc_end);
470 disassembler_->Dump(os, cur_pc, cur_pc_end);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800471 }
472 }
473
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700474 const std::string host_prefix_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800475 const OatFile& oat_file_;
476 std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800477 std::set<uint32_t> offsets_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800478 UniquePtr<Disassembler> disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700479};
480
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800481class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700482 public:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800483 explicit ImageDumper(std::ostream& os, const std::string& image_filename,
484 const std::string& host_prefix, Space& image_space,
Ian Rogersca190662012-06-26 15:45:57 -0700485 const ImageHeader& image_header)
486 : os_(os), image_filename_(image_filename), host_prefix_(host_prefix),
487 image_space_(image_space), image_header_(image_header) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700488
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800489 void Dump() {
490 os_ << "MAGIC:\n";
491 os_ << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700492
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800493 os_ << "IMAGE BEGIN:\n";
494 os_ << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700495
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800496 os_ << "OAT CHECKSUM:\n";
497 os_ << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700498
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800499 os_ << "OAT BEGIN:\n";
500 os_ << reinterpret_cast<void*>(image_header_.GetOatBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700501
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800502 os_ << "OAT END:\n";
503 os_ << reinterpret_cast<void*>(image_header_.GetOatEnd()) << "\n\n";
504
505 os_ << "ROOTS:\n";
506 os_ << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700507 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700508 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
509 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700510 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800511 Object* image_root_object = image_header_.GetImageRoot(image_root);
512 os_ << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700513 if (image_root_object->IsObjectArray()) {
514 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
515 ObjectArray<Object>* image_root_object_array
516 = down_cast<ObjectArray<Object>*>(image_root_object);
517 // = image_root_object->AsObjectArray<Object>();
518 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800519 Object* value = image_root_object_array->Get(i);
520 if (value != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800521 os_ << "\t" << i << ": ";
Ian Rogersd5b32602012-02-26 16:40:04 -0800522 std::string summary;
523 PrettyObjectValue(summary, value->GetClass(), value);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800524 os_ << summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800525 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800526 os_ << StringPrintf("\t%d: null\n", i);
Ian Rogersd5b32602012-02-26 16:40:04 -0800527 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700528 }
529 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700530 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800531 os_ << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700532
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800533 os_ << "OAT LOCATION:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700534 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800535 Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation);
536 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800537 os_ << oat_location;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800538 if (!host_prefix_.empty()) {
539 oat_location = host_prefix_ + oat_location;
540 os_ << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700541 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800542 os_ << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800543 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700544 if (oat_file == NULL) {
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800545 os_ << "NOT FOUND\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700546 return;
547 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800548 os_ << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700549
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800550 stats_.oat_file_bytes = oat_file->Size();
551
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700552 oat_dumper_.reset(new OatDumper(host_prefix_, *oat_file));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800553
554 os_ << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700555
556 // Loop through all the image spaces and dump their objects.
557 Heap* heap = Runtime::Current()->GetHeap();
558 const Spaces& spaces = heap->GetSpaces();
559 // TODO: C++0x auto
560 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
561 (*cur)->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
562 os_ << "\n";
563 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800564
565 os_ << "STATS:\n" << std::flush;
566 UniquePtr<File> file(OS::OpenFile(image_filename_.c_str(), false));
567 stats_.file_bytes = file->Length();
568 size_t header_bytes = sizeof(ImageHeader);
569 stats_.header_bytes = header_bytes;
570 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
571 stats_.alignment_bytes += alignment_bytes;
572 stats_.Dump(os_);
573 os_ << "\n";
574
575 os_ << std::flush;
576
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800577 oat_dumper_->Dump(os_);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700578 }
579
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700580 private:
Ian Rogersd5b32602012-02-26 16:40:04 -0800581 static void PrettyObjectValue(std::string& summary, Class* type, Object* value) {
582 CHECK(type != NULL);
583 if (value == NULL) {
584 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
585 } else if (type->IsStringClass()) {
586 String* string = value->AsString();
587 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
588 } else if (value->IsClass()) {
589 Class* klass = value->AsClass();
590 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
591 } else if (value->IsField()) {
592 Field* field = value->AsField();
593 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
594 } else if (value->IsMethod()) {
595 Method* method = value->AsMethod();
596 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
597 } else {
598 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
599 }
600 }
601
602 static void PrintField(std::string& summary, Field* field, Object* obj) {
603 FieldHelper fh(field);
604 Class* type = fh.GetType();
605 StringAppendF(&summary, "\t%s: ", fh.GetName());
606 if (type->IsPrimitiveLong()) {
607 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
608 } else if (type->IsPrimitiveDouble()) {
609 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
610 } else if (type->IsPrimitiveFloat()) {
611 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700612 } else if (type->IsPrimitive()) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800613 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
614 } else {
615 Object* value = field->GetObj(obj);
616 PrettyObjectValue(summary, type, value);
617 }
618 }
619
620 static void DumpFields(std::string& summary, Object* obj, Class* klass) {
621 Class* super = klass->GetSuperClass();
622 if (super != NULL) {
623 DumpFields(summary, obj, super);
624 }
625 ObjectArray<Field>* fields = klass->GetIFields();
626 if (fields != NULL) {
627 for (int32_t i = 0; i < fields->GetLength(); i++) {
628 Field* field = fields->Get(i);
629 PrintField(summary, field, obj);
630 }
631 }
632 }
633
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800634 bool InDumpSpace(const Object* object) {
635 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700636 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800637
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700638 const void* GetOatCodeBegin(Method* m) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800639 Runtime* runtime = Runtime::Current();
640 const void* code = m->GetCode();
Ian Rogersfb6adba2012-03-04 21:51:51 -0800641 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800642 code = oat_dumper_->GetOatCode(m);
643 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700644 if (oat_dumper_->GetInstructionSet() == kThumb2) {
645 code = reinterpret_cast<void*>(reinterpret_cast<uint32_t>(code) & ~0x1);
646 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800647 return code;
648 }
649
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700650 uint32_t GetOatCodeSize(Method* m) {
651 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetOatCodeBegin(m));
652 if (oat_code_begin == NULL) {
653 return 0;
654 }
655 return oat_code_begin[-1];
656 }
657
658 const void* GetOatCodeEnd(Method* m) {
659 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetOatCodeBegin(m));
660 if (oat_code_begin == NULL) {
661 return NULL;
662 }
663 return oat_code_begin + GetOatCodeSize(m);
664 }
665
Brian Carlstrom78128a62011-09-15 17:21:19 -0700666 static void Callback(Object* obj, void* arg) {
667 DCHECK(obj != NULL);
668 DCHECK(arg != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800669 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700670 if (!state->InDumpSpace(obj)) {
671 return;
672 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700673
674 size_t object_bytes = obj->SizeOf();
675 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
676 state->stats_.object_bytes += object_bytes;
677 state->stats_.alignment_bytes += alignment_bytes;
678
Brian Carlstrom78128a62011-09-15 17:21:19 -0700679 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800680 Class* obj_class = obj->GetClass();
681 if (obj_class->IsArrayClass()) {
682 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
683 obj->AsArray()->GetLength());
684 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700685 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800686 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
687 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700688 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800689 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700690 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800691 } else if (obj->IsField()) {
692 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
693 PrettyField(obj->AsField()).c_str());
694 } else if (obj->IsMethod()) {
695 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
696 PrettyMethod(obj->AsMethod()).c_str());
697 } else if (obj_class->IsStringClass()) {
Elliott Hughes82914b62012-04-09 15:56:29 -0700698 StringAppendF(&summary, "%p: java.lang.String %s\n", obj,
699 PrintableString(obj->AsString()->ToModifiedUtf8()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800700 } else {
701 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
702 }
703 DumpFields(summary, obj, obj_class);
704 if (obj->IsObjectArray()) {
705 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
706 int32_t length = obj_array->GetLength();
707 for (int32_t i = 0; i < length; i++) {
708 Object* value = obj_array->Get(i);
709 size_t run = 0;
710 for (int32_t j = i + 1; j < length; j++) {
711 if (value == obj_array->Get(j)) {
712 run++;
713 } else {
714 break;
715 }
716 }
717 if (run == 0) {
718 StringAppendF(&summary, "\t%d: ", i);
719 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800720 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800721 i = i + run;
722 }
723 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
724 PrettyObjectValue(summary, value_class, value);
725 }
726 } else if (obj->IsClass()) {
727 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
728 if (sfields != NULL) {
729 summary += "\t\tSTATICS:\n";
730 for (int32_t i = 0; i < sfields->GetLength(); i++) {
731 Field* field = sfields->Get(i);
732 PrintField(summary, field, NULL);
733 }
734 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700735 } else if (obj->IsMethod()) {
736 Method* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700737 if (method->IsNative()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700738 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800739 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700740 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800741 bool first_occurrence;
742 size_t invoke_stub_size = state->ComputeOatSize(
743 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
744 if (first_occurrence) {
745 state->stats_.managed_to_native_code_bytes += invoke_stub_size;
746 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700747 const void* oat_code = state->GetOatCodeBegin(method);
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700748 uint32_t oat_code_size = state->GetOatCodeSize(method);
749 state->ComputeOatSize(oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800750 if (first_occurrence) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700751 state->stats_.native_to_managed_code_bytes += oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800752 }
753 if (oat_code != method->GetCode()) {
754 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
755 }
Ian Rogers19846512012-02-24 11:42:47 -0800756 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
757 method->IsResolutionMethod()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700758 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800759 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700760 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700761 } else {
TDYa1273db52852012-04-01 15:11:43 -0700762#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800763 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
764 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
TDYa1273db52852012-04-01 15:11:43 -0700765#endif
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700766
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800767 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700768 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700769 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800770
Elliott Hughesa0e18062012-04-13 15:59:59 -0700771 bool first_occurrence;
772 size_t gc_map_bytes = state->ComputeOatSize(method->GetGcMapRaw(), &first_occurrence);
773 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800774 state->stats_.gc_map_bytes += gc_map_bytes;
775 }
776
777 size_t pc_mapping_table_bytes =
Elliott Hughesa0e18062012-04-13 15:59:59 -0700778 state->ComputeOatSize(method->GetMappingTableRaw(), &first_occurrence);
779 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800780 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
781 }
782
783 size_t vmap_table_bytes =
Elliott Hughesa0e18062012-04-13 15:59:59 -0700784 state->ComputeOatSize(method->GetVmapTableRaw(), &first_occurrence);
785 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800786 state->stats_.vmap_table_bytes += vmap_table_bytes;
787 }
788
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700789 // TODO: compute invoke stub using length from oat file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800790 size_t invoke_stub_size = state->ComputeOatSize(
Elliott Hughesa0e18062012-04-13 15:59:59 -0700791 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
792 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800793 state->stats_.native_to_managed_code_bytes += invoke_stub_size;
794 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700795 const void* oat_code_begin = state->GetOatCodeBegin(method);
796 const void* oat_code_end = state->GetOatCodeEnd(method);
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700797 uint32_t oat_code_size = state->GetOatCodeSize(method);
Elliott Hughesa0e18062012-04-13 15:59:59 -0700798 state->ComputeOatSize(oat_code_begin, &first_occurrence);
799 if (first_occurrence) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700800 state->stats_.managed_code_bytes += oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -0700801 if (method->IsConstructor()) {
802 if (method->IsStatic()) {
803 state->stats_.class_initializer_code_bytes += oat_code_size;
804 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
805 state->stats_.large_initializer_code_bytes += oat_code_size;
806 }
807 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
808 state->stats_.large_method_code_bytes += oat_code_size;
809 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800810 }
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700811 state->stats_.managed_code_bytes_ignoring_deduplication += oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800812
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700813 StringAppendF(&summary, "\t\tOAT CODE: %p-%p\n", oat_code_begin, oat_code_end);
Ian Rogersd5b32602012-02-26 16:40:04 -0800814 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800815 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
816
817 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700818 vmap_table_bytes + invoke_stub_size + oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800819
820 double expansion =
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700821 static_cast<double>(oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800822 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700823 }
824 }
Elliott Hughesa0e18062012-04-13 15:59:59 -0700825 state->stats_.Update(ClassHelper(obj_class).GetDescriptor(), object_bytes);
Ian Rogersd5b32602012-02-26 16:40:04 -0800826
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700827 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700828 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700829
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800830 std::set<const void*> already_seen_;
831 // Compute the size of the given data within the oat file and whether this is the first time
832 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -0700833 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800834 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700835 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800836 already_seen_.insert(oat_data);
837 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700838 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800839 }
840 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700841 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700842
843 public:
844 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800845 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700846 size_t file_bytes;
847
848 size_t header_bytes;
849 size_t object_bytes;
850 size_t alignment_bytes;
851
852 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800853 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700854 size_t managed_to_native_code_bytes;
855 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -0700856 size_t class_initializer_code_bytes;
857 size_t large_initializer_code_bytes;
858 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700859
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800860 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700861 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800862 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700863
864 size_t dex_instruction_bytes;
865
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800866 std::vector<Method*> method_outlier;
867 std::vector<size_t> method_outlier_size;
868 std::vector<double> method_outlier_expansion;
869
870 explicit Stats()
871 : oat_file_bytes(0),
872 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700873 header_bytes(0),
874 object_bytes(0),
875 alignment_bytes(0),
876 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800877 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700878 managed_to_native_code_bytes(0),
879 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -0700880 class_initializer_code_bytes(0),
881 large_initializer_code_bytes(0),
882 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800883 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700884 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800885 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700886 dex_instruction_bytes(0) {}
887
Elliott Hughesa0e18062012-04-13 15:59:59 -0700888 struct SizeAndCount {
889 SizeAndCount(size_t bytes, size_t count) : bytes(bytes), count(count) {}
890 size_t bytes;
891 size_t count;
892 };
893 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
894 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700895
Elliott Hughesa0e18062012-04-13 15:59:59 -0700896 void Update(const std::string& descriptor, size_t object_bytes) {
897 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
898 if (it != sizes_and_counts.end()) {
899 it->second.bytes += object_bytes;
900 it->second.count += 1;
901 } else {
902 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes, 1));
903 }
904 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700905
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800906 double PercentOfOatBytes(size_t size) {
907 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
908 }
909
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700910 double PercentOfFileBytes(size_t size) {
911 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
912 }
913
914 double PercentOfObjectBytes(size_t size) {
915 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
916 }
917
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800918 void ComputeOutliers(size_t total_size, double expansion, Method* method) {
919 method_outlier_size.push_back(total_size);
920 method_outlier_expansion.push_back(expansion);
921 method_outlier.push_back(method);
922 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700923
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800924 void DumpOutliers(std::ostream& os) {
925 size_t sum_of_sizes = 0;
926 size_t sum_of_sizes_squared = 0;
927 size_t sum_of_expansion = 0;
928 size_t sum_of_expansion_squared = 0;
929 size_t n = method_outlier_size.size();
930 for (size_t i = 0; i < n; i++) {
931 size_t cur_size = method_outlier_size[i];
932 sum_of_sizes += cur_size;
933 sum_of_sizes_squared += cur_size * cur_size;
934 double cur_expansion = method_outlier_expansion[i];
935 sum_of_expansion += cur_expansion;
936 sum_of_expansion_squared += cur_expansion * cur_expansion;
937 }
938 size_t size_mean = sum_of_sizes / n;
939 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
940 double expansion_mean = sum_of_expansion / n;
941 double expansion_variance =
942 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
943
944 // Dump methods whose size is a certain number of standard deviations from the mean
945 size_t dumped_values = 0;
946 size_t skipped_values = 0;
947 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
948 size_t cur_size_variance = i * i * size_variance;
949 bool first = true;
950 for (size_t j = 0; j < n; j++) {
951 size_t cur_size = method_outlier_size[j];
952 if (cur_size > size_mean) {
953 size_t cur_var = cur_size - size_mean;
954 cur_var = cur_var * cur_var;
955 if (cur_var > cur_size_variance) {
956 if (dumped_values > 20) {
957 if (i == 1) {
958 skipped_values++;
959 } else {
960 i = 2; // jump to counting for 1 standard deviation
961 break;
962 }
963 } else {
964 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700965 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800966 first = false;
967 }
968 os << "\t" << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -0700969 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800970 method_outlier_size[j] = 0; // don't consider this method again
971 dumped_values++;
972 }
973 }
974 }
975 }
976 }
977 if (skipped_values > 0) {
978 os << "\t... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -0700979 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800980 }
Elliott Hughesc073b072012-05-24 19:29:17 -0700981 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800982
983 // Dump methods whose expansion is a certain number of standard deviations from the mean
984 dumped_values = 0;
985 skipped_values = 0;
986 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
987 double cur_expansion_variance = i * i * expansion_variance;
988 bool first = true;
989 for (size_t j = 0; j < n; j++) {
990 double cur_expansion = method_outlier_expansion[j];
991 if (cur_expansion > expansion_mean) {
992 size_t cur_var = cur_expansion - expansion_mean;
993 cur_var = cur_var * cur_var;
994 if (cur_var > cur_expansion_variance) {
995 if (dumped_values > 20) {
996 if (i == 1) {
997 skipped_values++;
998 } else {
999 i = 2; // jump to counting for 1 standard deviation
1000 break;
1001 }
1002 } else {
1003 if (first) {
1004 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001005 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001006 first = false;
1007 }
1008 os << "\t" << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001009 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001010 method_outlier_expansion[j] = 0.0; // don't consider this method again
1011 dumped_values++;
1012 }
1013 }
1014 }
1015 }
1016 }
1017 if (skipped_values > 0) {
1018 os << "\t... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001019 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001020 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001021 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001022 }
1023
1024 void Dump(std::ostream& os) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001025 os << "\tart_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1026 << "\tart_file_bytes = header_bytes + object_bytes + alignment_bytes\n"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001027 << StringPrintf("\theader_bytes = %8zd (%2.0f%% of art file bytes)\n"
1028 "\tobject_bytes = %8zd (%2.0f%% of art file bytes)\n"
Elliott Hughesc073b072012-05-24 19:29:17 -07001029 "\talignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001030 header_bytes, PercentOfFileBytes(header_bytes),
1031 object_bytes, PercentOfFileBytes(object_bytes),
1032 alignment_bytes, PercentOfFileBytes(alignment_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001033 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001034
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001035 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
1036
Elliott Hughesa0e18062012-04-13 15:59:59 -07001037 os << "\tobject_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001038 size_t object_bytes_total = 0;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001039 typedef SizeAndCountTable::const_iterator It; // TODO: C++0x auto
1040 for (It it = sizes_and_counts.begin(), end = sizes_and_counts.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -08001041 const std::string& descriptor(it->first);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001042 double average = static_cast<double>(it->second.bytes) / static_cast<double>(it->second.count);
1043 double percent = PercentOfObjectBytes(it->second.bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001044 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001045 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
1046 descriptor.c_str(), it->second.bytes, it->second.count,
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001047 average, percent);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001048 object_bytes_total += it->second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001049 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001050 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001051 CHECK_EQ(object_bytes, object_bytes_total);
1052
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001053 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1054 "\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers0d2d3782012-04-10 11:09:18 -07001055 "\tnative_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1056 "\tclass_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1057 "\tlarge_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Elliott Hughesc073b072012-05-24 19:29:17 -07001058 "\tlarge_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001059 managed_code_bytes, PercentOfOatBytes(managed_code_bytes),
1060 managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001061 native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes),
1062 class_initializer_code_bytes, PercentOfOatBytes(class_initializer_code_bytes),
1063 large_initializer_code_bytes, PercentOfOatBytes(large_initializer_code_bytes),
1064 large_method_code_bytes, PercentOfOatBytes(large_method_code_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001065 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001066
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001067 os << StringPrintf("\tgc_map_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1068 "\tpc_mapping_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
Elliott Hughesc073b072012-05-24 19:29:17 -07001069 "\tvmap_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001070 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1071 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1072 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001073 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001074
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001075 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes)
Elliott Hughesc073b072012-05-24 19:29:17 -07001076 << StringPrintf("\tmanaged_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
1077 static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes),
1078 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001079 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001080 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001081
1082 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001083 }
1084 } stats_;
1085
1086 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001087 enum {
1088 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1089 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1090 kLargeConstructorDexBytes = 4000,
1091 // Number of bytes for a method to be considered large. Based on the 4000 basic block
1092 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1093 kLargeMethodDexBytes = 16000
1094 };
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001095 UniquePtr<OatDumper> oat_dumper_;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001096 std::ostream& os_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001097 const std::string image_filename_;
1098 const std::string host_prefix_;
1099 Space& image_space_;
1100 const ImageHeader& image_header_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001101
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001102 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001103};
1104
Elliott Hughes72395bf2012-04-24 13:45:26 -07001105static int oatdump(int argc, char** argv) {
Elliott Hughes0d39c122012-06-06 16:41:17 -07001106 InitLogging(argv);
Elliott Hughes72395bf2012-04-24 13:45:26 -07001107
Brian Carlstrom78128a62011-09-15 17:21:19 -07001108 // Skip over argv[0].
1109 argv++;
1110 argc--;
1111
1112 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001113 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001114 usage();
1115 }
1116
Brian Carlstromaded5f72011-10-07 17:15:04 -07001117 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001118 const char* image_filename = NULL;
1119 const char* boot_image_filename = NULL;
Logan Chien0cc6ab62012-03-20 22:57:52 +08001120 std::string elf_filename_prefix;
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001121 UniquePtr<std::string> host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001122 std::ostream* os = &std::cout;
1123 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001124
1125 for (int i = 0; i < argc; i++) {
1126 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001127 if (option.starts_with("--oat-file=")) {
1128 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001129 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001130 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001131 } else if (option.starts_with("--boot-image=")) {
1132 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001133 } else if (option.starts_with("--host-prefix=")) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001134 host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001135 } else if (option.starts_with("--output=")) {
1136 const char* filename = option.substr(strlen("--output=")).data();
1137 out.reset(new std::ofstream(filename));
1138 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001139 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001140 usage();
1141 }
1142 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001143 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001144 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001145 usage();
1146 }
1147 }
1148
Brian Carlstromaded5f72011-10-07 17:15:04 -07001149 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001150 fprintf(stderr, "Either --image or --oat must be specified\n");
1151 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001152 }
1153
Brian Carlstromaded5f72011-10-07 17:15:04 -07001154 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001155 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1156 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001157 }
1158
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001159 if (host_prefix.get() == NULL) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001160 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
1161 if (android_product_out != NULL) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001162 host_prefix.reset(new std::string(android_product_out));
1163 } else {
1164 host_prefix.reset(new std::string(""));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001165 }
1166 }
1167
Brian Carlstromaded5f72011-10-07 17:15:04 -07001168 if (oat_filename != NULL) {
Logan Chien0c717dd2012-03-28 18:31:07 +08001169 OatFile* oat_file =
1170 OatFile::Open(oat_filename, oat_filename, NULL, OatFile::kRelocNone);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001171 if (oat_file == NULL) {
1172 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
1173 return EXIT_FAILURE;
1174 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001175 OatDumper oat_dumper(*host_prefix.get(), *oat_file);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001176 oat_dumper.Dump(*os);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001177 return EXIT_SUCCESS;
1178 }
1179
Brian Carlstrom78128a62011-09-15 17:21:19 -07001180 Runtime::Options options;
1181 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001182 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001183 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001184 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001185 if (boot_image_filename != NULL) {
1186 boot_image_option += "-Ximage:";
1187 boot_image_option += boot_image_filename;
1188 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001189 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001190 if (image_filename != NULL) {
1191 image_option += "-Ximage:";
1192 image_option += image_filename;
1193 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1194 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001195
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001196 if (!host_prefix->empty()) {
1197 options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001198 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001199
1200 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
1201 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001202 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001203 return EXIT_FAILURE;
1204 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001205
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001206 Heap* heap = Runtime::Current()->GetHeap();
Brian Carlstromfddf6f62012-03-15 16:56:45 -07001207 ImageSpace* image_space = heap->GetImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001208 CHECK(image_space != NULL);
1209 const ImageHeader& image_header = image_space->GetImageHeader();
1210 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001211 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001212 return EXIT_FAILURE;
1213 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001214 ImageDumper image_dumper(*os, image_filename, *host_prefix.get(), *image_space, image_header);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001215 image_dumper.Dump();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001216 return EXIT_SUCCESS;
1217}
1218
1219} // namespace art
1220
1221int main(int argc, char** argv) {
1222 return art::oatdump(argc, argv);
1223}