blob: a33cd0967e773682c9e834352f692758b2fe337b [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
TDYa1273db52852012-04-01 15:11:43 -0700438#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogersd3fb5692012-04-09 21:56:13 -0700439 void DumpCode(std::ostream& os, const void* code, int code_size,
440 const uint32_t* raw_mapping_table,
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800441 const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Ian Rogersd3fb5692012-04-09 21:56:13 -0700442 if (code == NULL || code_size == 0) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800443 return;
444 }
445
Ian Rogersd3fb5692012-04-09 21:56:13 -0700446 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code);
447 const uint8_t* end_native_pc = native_pc + code_size;
448
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800449 if (raw_mapping_table == NULL) {
450 // code but no mapping table is most likely caused by code created by the JNI compiler
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800451 disassembler_->Dump(os, native_pc, end_native_pc);
452 return;
453 }
454
455 uint32_t length = *raw_mapping_table;
456 ++raw_mapping_table;
457
458 for (size_t i = 0; i < length; i += 2) {
459 uint32_t dex_pc = raw_mapping_table[i + 1];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800460 const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]);
461 os << StringPrintf("\t\t0x%04x: %s\n", dex_pc, instruction->DumpString(&dex_file).c_str());
462
Ian Rogersd3fb5692012-04-09 21:56:13 -0700463 const uint8_t* cur_pc = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i];
464 const uint8_t* cur_pc_end = NULL;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800465 if (i + 2 < length) {
Ian Rogersd3fb5692012-04-09 21:56:13 -0700466 cur_pc_end = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i + 2];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800467 } else {
Ian Rogersd3fb5692012-04-09 21:56:13 -0700468 cur_pc_end = end_native_pc;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800469 }
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700470 CHECK(cur_pc < cur_pc_end);
471 disassembler_->Dump(os, cur_pc, cur_pc_end);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800472 }
473 }
TDYa1273db52852012-04-01 15:11:43 -0700474#else
Logan Chiende85cc12012-04-10 19:55:34 +0800475 void DumpCode(std::ostream&, const void*, int, const uint32_t*,
476 const DexFile&, const DexFile::CodeItem*) {
TDYa1273db52852012-04-01 15:11:43 -0700477 // TODO: Dump code for the LLVM side.
478 }
479#endif
Elliott Hughese3c845c2012-02-28 17:23:01 -0800480
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700481 const std::string host_prefix_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800482 const OatFile& oat_file_;
483 std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800484 std::set<uint32_t> offsets_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800485 UniquePtr<Disassembler> disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700486};
487
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800488class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700489 public:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800490 explicit ImageDumper(std::ostream& os, const std::string& image_filename,
491 const std::string& host_prefix, Space& image_space,
Ian Rogersca190662012-06-26 15:45:57 -0700492 const ImageHeader& image_header)
493 : os_(os), image_filename_(image_filename), host_prefix_(host_prefix),
494 image_space_(image_space), image_header_(image_header) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700495
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800496 void Dump() {
497 os_ << "MAGIC:\n";
498 os_ << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700499
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800500 os_ << "IMAGE BEGIN:\n";
501 os_ << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700502
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800503 os_ << "OAT CHECKSUM:\n";
504 os_ << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700505
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800506 os_ << "OAT BEGIN:\n";
507 os_ << reinterpret_cast<void*>(image_header_.GetOatBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700508
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800509 os_ << "OAT END:\n";
510 os_ << reinterpret_cast<void*>(image_header_.GetOatEnd()) << "\n\n";
511
512 os_ << "ROOTS:\n";
513 os_ << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700514 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700515 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
516 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700517 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800518 Object* image_root_object = image_header_.GetImageRoot(image_root);
519 os_ << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700520 if (image_root_object->IsObjectArray()) {
521 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
522 ObjectArray<Object>* image_root_object_array
523 = down_cast<ObjectArray<Object>*>(image_root_object);
524 // = image_root_object->AsObjectArray<Object>();
525 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800526 Object* value = image_root_object_array->Get(i);
527 if (value != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800528 os_ << "\t" << i << ": ";
Ian Rogersd5b32602012-02-26 16:40:04 -0800529 std::string summary;
530 PrettyObjectValue(summary, value->GetClass(), value);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800531 os_ << summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800532 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800533 os_ << StringPrintf("\t%d: null\n", i);
Ian Rogersd5b32602012-02-26 16:40:04 -0800534 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700535 }
536 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700537 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800538 os_ << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700539
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800540 os_ << "OAT LOCATION:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700541 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800542 Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation);
543 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800544 os_ << oat_location;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800545 if (!host_prefix_.empty()) {
546 oat_location = host_prefix_ + oat_location;
547 os_ << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700548 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800549 os_ << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800550 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700551 if (oat_file == NULL) {
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800552 os_ << "NOT FOUND\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700553 return;
554 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800555 os_ << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700556
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800557 stats_.oat_file_bytes = oat_file->Size();
558
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700559 oat_dumper_.reset(new OatDumper(host_prefix_, *oat_file));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800560
561 os_ << "OBJECTS:\n" << std::flush;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800562 HeapBitmap* heap_bitmap = Runtime::Current()->GetHeap()->GetLiveBits();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800563 DCHECK(heap_bitmap != NULL);
564 heap_bitmap->Walk(ImageDumper::Callback, this);
565 os_ << "\n";
566
567 os_ << "STATS:\n" << std::flush;
568 UniquePtr<File> file(OS::OpenFile(image_filename_.c_str(), false));
569 stats_.file_bytes = file->Length();
570 size_t header_bytes = sizeof(ImageHeader);
571 stats_.header_bytes = header_bytes;
572 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
573 stats_.alignment_bytes += alignment_bytes;
574 stats_.Dump(os_);
575 os_ << "\n";
576
577 os_ << std::flush;
578
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800579 oat_dumper_->Dump(os_);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700580 }
581
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700582 private:
Ian Rogersd5b32602012-02-26 16:40:04 -0800583 static void PrettyObjectValue(std::string& summary, Class* type, Object* value) {
584 CHECK(type != NULL);
585 if (value == NULL) {
586 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
587 } else if (type->IsStringClass()) {
588 String* string = value->AsString();
589 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
590 } else if (value->IsClass()) {
591 Class* klass = value->AsClass();
592 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
593 } else if (value->IsField()) {
594 Field* field = value->AsField();
595 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
596 } else if (value->IsMethod()) {
597 Method* method = value->AsMethod();
598 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
599 } else {
600 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
601 }
602 }
603
604 static void PrintField(std::string& summary, Field* field, Object* obj) {
605 FieldHelper fh(field);
606 Class* type = fh.GetType();
607 StringAppendF(&summary, "\t%s: ", fh.GetName());
608 if (type->IsPrimitiveLong()) {
609 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
610 } else if (type->IsPrimitiveDouble()) {
611 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
612 } else if (type->IsPrimitiveFloat()) {
613 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700614 } else if (type->IsPrimitive()) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800615 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
616 } else {
617 Object* value = field->GetObj(obj);
618 PrettyObjectValue(summary, type, value);
619 }
620 }
621
622 static void DumpFields(std::string& summary, Object* obj, Class* klass) {
623 Class* super = klass->GetSuperClass();
624 if (super != NULL) {
625 DumpFields(summary, obj, super);
626 }
627 ObjectArray<Field>* fields = klass->GetIFields();
628 if (fields != NULL) {
629 for (int32_t i = 0; i < fields->GetLength(); i++) {
630 Field* field = fields->Get(i);
631 PrintField(summary, field, obj);
632 }
633 }
634 }
635
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800636 bool InDumpSpace(const Object* object) {
637 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700638 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800639
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700640 const void* GetOatCodeBegin(Method* m) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800641 Runtime* runtime = Runtime::Current();
642 const void* code = m->GetCode();
Ian Rogersfb6adba2012-03-04 21:51:51 -0800643 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800644 code = oat_dumper_->GetOatCode(m);
645 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700646 if (oat_dumper_->GetInstructionSet() == kThumb2) {
647 code = reinterpret_cast<void*>(reinterpret_cast<uint32_t>(code) & ~0x1);
648 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800649 return code;
650 }
651
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700652 uint32_t GetOatCodeSize(Method* m) {
653 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetOatCodeBegin(m));
654 if (oat_code_begin == NULL) {
655 return 0;
656 }
657 return oat_code_begin[-1];
658 }
659
660 const void* GetOatCodeEnd(Method* m) {
661 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetOatCodeBegin(m));
662 if (oat_code_begin == NULL) {
663 return NULL;
664 }
665 return oat_code_begin + GetOatCodeSize(m);
666 }
667
Brian Carlstrom78128a62011-09-15 17:21:19 -0700668 static void Callback(Object* obj, void* arg) {
669 DCHECK(obj != NULL);
670 DCHECK(arg != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800671 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700672 if (!state->InDumpSpace(obj)) {
673 return;
674 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700675
676 size_t object_bytes = obj->SizeOf();
677 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
678 state->stats_.object_bytes += object_bytes;
679 state->stats_.alignment_bytes += alignment_bytes;
680
Brian Carlstrom78128a62011-09-15 17:21:19 -0700681 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800682 Class* obj_class = obj->GetClass();
683 if (obj_class->IsArrayClass()) {
684 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
685 obj->AsArray()->GetLength());
686 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700687 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800688 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
689 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700690 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800691 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700692 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800693 } else if (obj->IsField()) {
694 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
695 PrettyField(obj->AsField()).c_str());
696 } else if (obj->IsMethod()) {
697 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
698 PrettyMethod(obj->AsMethod()).c_str());
699 } else if (obj_class->IsStringClass()) {
Elliott Hughes82914b62012-04-09 15:56:29 -0700700 StringAppendF(&summary, "%p: java.lang.String %s\n", obj,
701 PrintableString(obj->AsString()->ToModifiedUtf8()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800702 } else {
703 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
704 }
705 DumpFields(summary, obj, obj_class);
706 if (obj->IsObjectArray()) {
707 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
708 int32_t length = obj_array->GetLength();
709 for (int32_t i = 0; i < length; i++) {
710 Object* value = obj_array->Get(i);
711 size_t run = 0;
712 for (int32_t j = i + 1; j < length; j++) {
713 if (value == obj_array->Get(j)) {
714 run++;
715 } else {
716 break;
717 }
718 }
719 if (run == 0) {
720 StringAppendF(&summary, "\t%d: ", i);
721 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800722 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800723 i = i + run;
724 }
725 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
726 PrettyObjectValue(summary, value_class, value);
727 }
728 } else if (obj->IsClass()) {
729 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
730 if (sfields != NULL) {
731 summary += "\t\tSTATICS:\n";
732 for (int32_t i = 0; i < sfields->GetLength(); i++) {
733 Field* field = sfields->Get(i);
734 PrintField(summary, field, NULL);
735 }
736 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700737 } else if (obj->IsMethod()) {
738 Method* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700739 if (method->IsNative()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700740 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800741 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700742 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800743 bool first_occurrence;
744 size_t invoke_stub_size = state->ComputeOatSize(
745 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
746 if (first_occurrence) {
747 state->stats_.managed_to_native_code_bytes += invoke_stub_size;
748 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700749 const void* oat_code = state->GetOatCodeBegin(method);
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700750 uint32_t oat_code_size = state->GetOatCodeSize(method);
751 state->ComputeOatSize(oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800752 if (first_occurrence) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700753 state->stats_.native_to_managed_code_bytes += oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800754 }
755 if (oat_code != method->GetCode()) {
756 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
757 }
Ian Rogers19846512012-02-24 11:42:47 -0800758 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
759 method->IsResolutionMethod()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700760 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800761 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700762 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700763 } else {
TDYa1273db52852012-04-01 15:11:43 -0700764#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800765 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
766 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
TDYa1273db52852012-04-01 15:11:43 -0700767#endif
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700768
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800769 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700770 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700771 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800772
Elliott Hughesa0e18062012-04-13 15:59:59 -0700773 bool first_occurrence;
774 size_t gc_map_bytes = state->ComputeOatSize(method->GetGcMapRaw(), &first_occurrence);
775 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800776 state->stats_.gc_map_bytes += gc_map_bytes;
777 }
778
779 size_t pc_mapping_table_bytes =
Elliott Hughesa0e18062012-04-13 15:59:59 -0700780 state->ComputeOatSize(method->GetMappingTableRaw(), &first_occurrence);
781 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800782 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
783 }
784
785 size_t vmap_table_bytes =
Elliott Hughesa0e18062012-04-13 15:59:59 -0700786 state->ComputeOatSize(method->GetVmapTableRaw(), &first_occurrence);
787 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800788 state->stats_.vmap_table_bytes += vmap_table_bytes;
789 }
790
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700791 // TODO: compute invoke stub using length from oat file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800792 size_t invoke_stub_size = state->ComputeOatSize(
Elliott Hughesa0e18062012-04-13 15:59:59 -0700793 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
794 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800795 state->stats_.native_to_managed_code_bytes += invoke_stub_size;
796 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700797 const void* oat_code_begin = state->GetOatCodeBegin(method);
798 const void* oat_code_end = state->GetOatCodeEnd(method);
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700799 uint32_t oat_code_size = state->GetOatCodeSize(method);
Elliott Hughesa0e18062012-04-13 15:59:59 -0700800 state->ComputeOatSize(oat_code_begin, &first_occurrence);
801 if (first_occurrence) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700802 state->stats_.managed_code_bytes += oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -0700803 if (method->IsConstructor()) {
804 if (method->IsStatic()) {
805 state->stats_.class_initializer_code_bytes += oat_code_size;
806 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
807 state->stats_.large_initializer_code_bytes += oat_code_size;
808 }
809 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
810 state->stats_.large_method_code_bytes += oat_code_size;
811 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800812 }
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700813 state->stats_.managed_code_bytes_ignoring_deduplication += oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800814
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700815 StringAppendF(&summary, "\t\tOAT CODE: %p-%p\n", oat_code_begin, oat_code_end);
Ian Rogersd5b32602012-02-26 16:40:04 -0800816 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800817 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
818
819 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700820 vmap_table_bytes + invoke_stub_size + oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800821
822 double expansion =
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700823 static_cast<double>(oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800824 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700825 }
826 }
Elliott Hughesa0e18062012-04-13 15:59:59 -0700827 state->stats_.Update(ClassHelper(obj_class).GetDescriptor(), object_bytes);
Ian Rogersd5b32602012-02-26 16:40:04 -0800828
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700829 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700830 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700831
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800832 std::set<const void*> already_seen_;
833 // Compute the size of the given data within the oat file and whether this is the first time
834 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -0700835 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800836 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700837 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800838 already_seen_.insert(oat_data);
839 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700840 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800841 }
842 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700843 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700844
845 public:
846 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800847 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700848 size_t file_bytes;
849
850 size_t header_bytes;
851 size_t object_bytes;
852 size_t alignment_bytes;
853
854 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800855 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700856 size_t managed_to_native_code_bytes;
857 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -0700858 size_t class_initializer_code_bytes;
859 size_t large_initializer_code_bytes;
860 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700861
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800862 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700863 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800864 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700865
866 size_t dex_instruction_bytes;
867
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800868 std::vector<Method*> method_outlier;
869 std::vector<size_t> method_outlier_size;
870 std::vector<double> method_outlier_expansion;
871
872 explicit Stats()
873 : oat_file_bytes(0),
874 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700875 header_bytes(0),
876 object_bytes(0),
877 alignment_bytes(0),
878 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800879 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700880 managed_to_native_code_bytes(0),
881 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -0700882 class_initializer_code_bytes(0),
883 large_initializer_code_bytes(0),
884 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800885 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700886 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800887 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700888 dex_instruction_bytes(0) {}
889
Elliott Hughesa0e18062012-04-13 15:59:59 -0700890 struct SizeAndCount {
891 SizeAndCount(size_t bytes, size_t count) : bytes(bytes), count(count) {}
892 size_t bytes;
893 size_t count;
894 };
895 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
896 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700897
Elliott Hughesa0e18062012-04-13 15:59:59 -0700898 void Update(const std::string& descriptor, size_t object_bytes) {
899 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
900 if (it != sizes_and_counts.end()) {
901 it->second.bytes += object_bytes;
902 it->second.count += 1;
903 } else {
904 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes, 1));
905 }
906 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700907
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800908 double PercentOfOatBytes(size_t size) {
909 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
910 }
911
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700912 double PercentOfFileBytes(size_t size) {
913 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
914 }
915
916 double PercentOfObjectBytes(size_t size) {
917 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
918 }
919
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800920 void ComputeOutliers(size_t total_size, double expansion, Method* method) {
921 method_outlier_size.push_back(total_size);
922 method_outlier_expansion.push_back(expansion);
923 method_outlier.push_back(method);
924 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700925
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800926 void DumpOutliers(std::ostream& os) {
927 size_t sum_of_sizes = 0;
928 size_t sum_of_sizes_squared = 0;
929 size_t sum_of_expansion = 0;
930 size_t sum_of_expansion_squared = 0;
931 size_t n = method_outlier_size.size();
932 for (size_t i = 0; i < n; i++) {
933 size_t cur_size = method_outlier_size[i];
934 sum_of_sizes += cur_size;
935 sum_of_sizes_squared += cur_size * cur_size;
936 double cur_expansion = method_outlier_expansion[i];
937 sum_of_expansion += cur_expansion;
938 sum_of_expansion_squared += cur_expansion * cur_expansion;
939 }
940 size_t size_mean = sum_of_sizes / n;
941 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
942 double expansion_mean = sum_of_expansion / n;
943 double expansion_variance =
944 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
945
946 // Dump methods whose size is a certain number of standard deviations from the mean
947 size_t dumped_values = 0;
948 size_t skipped_values = 0;
949 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
950 size_t cur_size_variance = i * i * size_variance;
951 bool first = true;
952 for (size_t j = 0; j < n; j++) {
953 size_t cur_size = method_outlier_size[j];
954 if (cur_size > size_mean) {
955 size_t cur_var = cur_size - size_mean;
956 cur_var = cur_var * cur_var;
957 if (cur_var > cur_size_variance) {
958 if (dumped_values > 20) {
959 if (i == 1) {
960 skipped_values++;
961 } else {
962 i = 2; // jump to counting for 1 standard deviation
963 break;
964 }
965 } else {
966 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700967 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800968 first = false;
969 }
970 os << "\t" << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -0700971 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800972 method_outlier_size[j] = 0; // don't consider this method again
973 dumped_values++;
974 }
975 }
976 }
977 }
978 }
979 if (skipped_values > 0) {
980 os << "\t... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -0700981 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800982 }
Elliott Hughesc073b072012-05-24 19:29:17 -0700983 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800984
985 // Dump methods whose expansion is a certain number of standard deviations from the mean
986 dumped_values = 0;
987 skipped_values = 0;
988 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
989 double cur_expansion_variance = i * i * expansion_variance;
990 bool first = true;
991 for (size_t j = 0; j < n; j++) {
992 double cur_expansion = method_outlier_expansion[j];
993 if (cur_expansion > expansion_mean) {
994 size_t cur_var = cur_expansion - expansion_mean;
995 cur_var = cur_var * cur_var;
996 if (cur_var > cur_expansion_variance) {
997 if (dumped_values > 20) {
998 if (i == 1) {
999 skipped_values++;
1000 } else {
1001 i = 2; // jump to counting for 1 standard deviation
1002 break;
1003 }
1004 } else {
1005 if (first) {
1006 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001007 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001008 first = false;
1009 }
1010 os << "\t" << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001011 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001012 method_outlier_expansion[j] = 0.0; // don't consider this method again
1013 dumped_values++;
1014 }
1015 }
1016 }
1017 }
1018 }
1019 if (skipped_values > 0) {
1020 os << "\t... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001021 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001022 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001023 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001024 }
1025
1026 void Dump(std::ostream& os) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001027 os << "\tart_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1028 << "\tart_file_bytes = header_bytes + object_bytes + alignment_bytes\n"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001029 << StringPrintf("\theader_bytes = %8zd (%2.0f%% of art file bytes)\n"
1030 "\tobject_bytes = %8zd (%2.0f%% of art file bytes)\n"
Elliott Hughesc073b072012-05-24 19:29:17 -07001031 "\talignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001032 header_bytes, PercentOfFileBytes(header_bytes),
1033 object_bytes, PercentOfFileBytes(object_bytes),
1034 alignment_bytes, PercentOfFileBytes(alignment_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001035 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001036
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001037 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
1038
Elliott Hughesa0e18062012-04-13 15:59:59 -07001039 os << "\tobject_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001040 size_t object_bytes_total = 0;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001041 typedef SizeAndCountTable::const_iterator It; // TODO: C++0x auto
1042 for (It it = sizes_and_counts.begin(), end = sizes_and_counts.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -08001043 const std::string& descriptor(it->first);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001044 double average = static_cast<double>(it->second.bytes) / static_cast<double>(it->second.count);
1045 double percent = PercentOfObjectBytes(it->second.bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001046 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001047 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
1048 descriptor.c_str(), it->second.bytes, it->second.count,
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001049 average, percent);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001050 object_bytes_total += it->second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001051 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001052 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001053 CHECK_EQ(object_bytes, object_bytes_total);
1054
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001055 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1056 "\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers0d2d3782012-04-10 11:09:18 -07001057 "\tnative_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1058 "\tclass_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1059 "\tlarge_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Elliott Hughesc073b072012-05-24 19:29:17 -07001060 "\tlarge_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001061 managed_code_bytes, PercentOfOatBytes(managed_code_bytes),
1062 managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001063 native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes),
1064 class_initializer_code_bytes, PercentOfOatBytes(class_initializer_code_bytes),
1065 large_initializer_code_bytes, PercentOfOatBytes(large_initializer_code_bytes),
1066 large_method_code_bytes, PercentOfOatBytes(large_method_code_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001067 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001068
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001069 os << StringPrintf("\tgc_map_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1070 "\tpc_mapping_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
Elliott Hughesc073b072012-05-24 19:29:17 -07001071 "\tvmap_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001072 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1073 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1074 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001075 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001076
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001077 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes)
Elliott Hughesc073b072012-05-24 19:29:17 -07001078 << StringPrintf("\tmanaged_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
1079 static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes),
1080 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001081 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001082 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001083
1084 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001085 }
1086 } stats_;
1087
1088 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001089 enum {
1090 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1091 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1092 kLargeConstructorDexBytes = 4000,
1093 // Number of bytes for a method to be considered large. Based on the 4000 basic block
1094 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1095 kLargeMethodDexBytes = 16000
1096 };
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001097 UniquePtr<OatDumper> oat_dumper_;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001098 std::ostream& os_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001099 const std::string image_filename_;
1100 const std::string host_prefix_;
1101 Space& image_space_;
1102 const ImageHeader& image_header_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001103
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001104 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001105};
1106
Elliott Hughes72395bf2012-04-24 13:45:26 -07001107static int oatdump(int argc, char** argv) {
Elliott Hughes0d39c122012-06-06 16:41:17 -07001108 InitLogging(argv);
Elliott Hughes72395bf2012-04-24 13:45:26 -07001109
Brian Carlstrom78128a62011-09-15 17:21:19 -07001110 // Skip over argv[0].
1111 argv++;
1112 argc--;
1113
1114 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001115 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001116 usage();
1117 }
1118
Brian Carlstromaded5f72011-10-07 17:15:04 -07001119 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001120 const char* image_filename = NULL;
1121 const char* boot_image_filename = NULL;
Logan Chien0cc6ab62012-03-20 22:57:52 +08001122 std::string elf_filename_prefix;
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001123 UniquePtr<std::string> host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001124 std::ostream* os = &std::cout;
1125 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001126
1127 for (int i = 0; i < argc; i++) {
1128 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001129 if (option.starts_with("--oat-file=")) {
1130 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001131 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001132 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001133 } else if (option.starts_with("--boot-image=")) {
1134 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001135 } else if (option.starts_with("--host-prefix=")) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001136 host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001137 } else if (option.starts_with("--output=")) {
1138 const char* filename = option.substr(strlen("--output=")).data();
1139 out.reset(new std::ofstream(filename));
1140 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001141 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001142 usage();
1143 }
1144 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001145 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001146 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001147 usage();
1148 }
1149 }
1150
Brian Carlstromaded5f72011-10-07 17:15:04 -07001151 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001152 fprintf(stderr, "Either --image or --oat must be specified\n");
1153 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001154 }
1155
Brian Carlstromaded5f72011-10-07 17:15:04 -07001156 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001157 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1158 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001159 }
1160
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001161 if (host_prefix.get() == NULL) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001162 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
1163 if (android_product_out != NULL) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001164 host_prefix.reset(new std::string(android_product_out));
1165 } else {
1166 host_prefix.reset(new std::string(""));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001167 }
1168 }
1169
Brian Carlstromaded5f72011-10-07 17:15:04 -07001170 if (oat_filename != NULL) {
Logan Chien0c717dd2012-03-28 18:31:07 +08001171 OatFile* oat_file =
1172 OatFile::Open(oat_filename, oat_filename, NULL, OatFile::kRelocNone);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001173 if (oat_file == NULL) {
1174 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
1175 return EXIT_FAILURE;
1176 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001177 OatDumper oat_dumper(*host_prefix.get(), *oat_file);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001178 oat_dumper.Dump(*os);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001179 return EXIT_SUCCESS;
1180 }
1181
Brian Carlstrom78128a62011-09-15 17:21:19 -07001182 Runtime::Options options;
1183 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001184 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001185 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001186 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001187 if (boot_image_filename != NULL) {
1188 boot_image_option += "-Ximage:";
1189 boot_image_option += boot_image_filename;
1190 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001191 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001192 if (image_filename != NULL) {
1193 image_option += "-Ximage:";
1194 image_option += image_filename;
1195 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1196 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001197
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001198 if (!host_prefix->empty()) {
1199 options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001200 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001201
1202 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
1203 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001204 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001205 return EXIT_FAILURE;
1206 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001207
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001208 Heap* heap = Runtime::Current()->GetHeap();
Brian Carlstromfddf6f62012-03-15 16:56:45 -07001209 ImageSpace* image_space = heap->GetImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001210 CHECK(image_space != NULL);
1211 const ImageHeader& image_header = image_space->GetImageHeader();
1212 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001213 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001214 return EXIT_FAILURE;
1215 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001216 ImageDumper image_dumper(*os, image_filename, *host_prefix.get(), *image_space, image_header);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001217 image_dumper.Dump();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001218 return EXIT_SUCCESS;
1219}
1220
1221} // namespace art
1222
1223int main(int argc, char** argv) {
1224 return art::oatdump(argc, argv);
1225}