blob: e0ee14843c76b21338c727757dbcf67dfe3aefe2 [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"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035#include "scoped_thread_state_change.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070036#include "space.h"
37#include "stringpiece.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070038#include "verifier/gc_map.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070039
40namespace art {
41
42static void usage() {
43 fprintf(stderr,
44 "Usage: oatdump [options] ...\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080045 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n"
46 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070047 "\n");
48 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080049 " --oat-file=<file.oat>: specifies an input oat filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080050 " Example: --image=/system/framework/boot.oat\n"
Brian Carlstromaded5f72011-10-07 17:15:04 -070051 "\n");
52 fprintf(stderr,
53 " --image=<file.art>: specifies an input image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080054 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070055 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070056 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070057 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080058 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070059 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070060 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070061 " --host-prefix may be used to translate host paths to target paths during\n"
62 " cross compilation.\n"
63 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstromfe487d02012-02-29 18:49:16 -080064 " Default: $ANDROID_PRODUCT_OUT\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070065 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070066 fprintf(stderr,
67 " --output=<file> may be used to send the output to a file.\n"
68 " Example: --output=/tmp/oatdump.txt\n"
69 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070070 exit(EXIT_FAILURE);
71}
72
Ian Rogersff1ed472011-09-20 13:46:24 -070073const char* image_roots_descriptions_[] = {
Brian Carlstrom78128a62011-09-15 17:21:19 -070074 "kJniStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070075 "kAbstractMethodErrorStubArray",
Ian Rogersad25ac52011-10-04 19:13:33 -070076 "kStaticResolutionStubArray",
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070077 "kUnknownMethodResolutionStubArray",
Ian Rogers19846512012-02-24 11:42:47 -080078 "kResolutionMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070079 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070080 "kRefsOnlySaveMethod",
81 "kRefsAndArgsSaveMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070082 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070083 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070084 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070085};
86
Elliott Hughese3c845c2012-02-28 17:23:01 -080087class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -070088 public:
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070089 explicit OatDumper(const std::string& host_prefix, const OatFile& oat_file)
90 : host_prefix_(host_prefix),
91 oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -080092 oat_dex_files_(oat_file.GetOatDexFiles()),
93 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet())) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080094 AddAllOffsets();
95 }
96
97 void Dump(std::ostream& os) {
98 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -070099
100 os << "MAGIC:\n";
101 os << oat_header.GetMagic() << "\n\n";
102
103 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800104 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700105
Elliott Hughesa72ec822012-03-05 17:12:22 -0800106 os << "INSTRUCTION SET:\n";
107 os << oat_header.GetInstructionSet() << "\n\n";
108
Brian Carlstromaded5f72011-10-07 17:15:04 -0700109 os << "DEX FILE COUNT:\n";
110 os << oat_header.GetDexFileCount() << "\n\n";
111
112 os << "EXECUTABLE OFFSET:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800113 os << StringPrintf("0x%08x\n\n", oat_header.GetExecutableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700114
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700115 os << "IMAGE FILE LOCATION CHECKSUM:\n";
116 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationChecksum());
117
118 os << "IMAGE FILE LOCATION:\n";
119 const std::string image_file_location(oat_header.GetImageFileLocation());
120 os << image_file_location;
121 if (!image_file_location.empty() && !host_prefix_.empty()) {
122 os << " (" << host_prefix_ << image_file_location << ")";
123 }
124 os << "\n\n";
125
Ian Rogers30fab402012-01-23 15:43:46 -0800126 os << "BEGIN:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800127 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700128
Ian Rogers30fab402012-01-23 15:43:46 -0800129 os << "END:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800130 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700131
132 os << std::flush;
133
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800134 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
135 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Brian Carlstromaded5f72011-10-07 17:15:04 -0700136 CHECK(oat_dex_file != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800137 DumpOatDexFile(os, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700138 }
139 }
140
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800141 size_t ComputeSize(const void* oat_data) {
142 if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() ||
143 reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) {
144 return 0; // Address not in oat file
145 }
146 uint32_t begin_offset = reinterpret_cast<size_t>(oat_data) -
147 reinterpret_cast<size_t>(oat_file_.Begin());
148 typedef std::set<uint32_t>::iterator It;
149 It it = offsets_.upper_bound(begin_offset);
150 CHECK(it != offsets_.end());
151 uint32_t end_offset = *it;
152 return end_offset - begin_offset;
153 }
154
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700155 InstructionSet GetInstructionSet() {
156 return oat_file_.GetOatHeader().GetInstructionSet();
157 }
158
Ian Rogersb726dcb2012-09-05 08:57:23 -0700159 const void* GetOatCode(Method* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800160 MethodHelper mh(m);
161 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
162 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
163 CHECK(oat_dex_file != NULL);
164 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
165 if (dex_file.get() != NULL) {
166 uint32_t class_def_index;
167 bool found = dex_file->FindClassDefIndex(mh.GetDeclaringClassDescriptor(), class_def_index);
168 if (found) {
169 const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_index);
170 CHECK(oat_class != NULL);
171 size_t method_index = m->GetMethodIndex();
172 return oat_class->GetOatMethod(method_index).GetCode();
173 }
174 }
175 }
176 return NULL;
177 }
178
Brian Carlstromaded5f72011-10-07 17:15:04 -0700179 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800180 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800181 // We don't know the length of the code for each method, but we need to know where to stop
182 // when disassembling. What we do know is that a region of code will be followed by some other
183 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
184 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800185 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
186 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800187 CHECK(oat_dex_file != NULL);
188 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
189 if (dex_file.get() == NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800190 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800191 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800192 offsets_.insert(reinterpret_cast<uint32_t>(&dex_file->GetHeader()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800193 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
194 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
195 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index));
196 const byte* class_data = dex_file->GetClassData(class_def);
197 if (class_data != NULL) {
198 ClassDataItemIterator it(*dex_file, class_data);
199 SkipAllFields(it);
200 uint32_t class_method_index = 0;
201 while (it.HasNextDirectMethod()) {
202 AddOffsets(oat_class->GetOatMethod(class_method_index++));
203 it.Next();
204 }
205 while (it.HasNextVirtualMethod()) {
206 AddOffsets(oat_class->GetOatMethod(class_method_index++));
207 it.Next();
208 }
209 }
210 }
211 }
212
213 // If the last thing in the file is code for a method, there won't be an offset for the "next"
214 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
215 // for the end of the file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800216 offsets_.insert(static_cast<uint32_t>(oat_file_.End() - oat_file_.Begin()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800217 }
218
219 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800220 uint32_t code_offset = oat_method.GetCodeOffset();
221 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
222 code_offset &= ~0x1;
223 }
224 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800225 offsets_.insert(oat_method.GetMappingTableOffset());
226 offsets_.insert(oat_method.GetVmapTableOffset());
227 offsets_.insert(oat_method.GetGcMapOffset());
228 offsets_.insert(oat_method.GetInvokeStubOffset());
229 }
230
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800231 void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700232 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800233 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800234 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800235 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile());
236 if (dex_file.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700237 os << "NOT FOUND\n\n";
238 return;
239 }
240 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
241 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
242 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700243 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
244 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800245 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800246 << oat_class->GetStatus() << ")\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800247 DumpOatClass(os, *oat_class.get(), *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700248 }
249
250 os << std::flush;
251 }
252
Elliott Hughese3c845c2012-02-28 17:23:01 -0800253 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700254 while (it.HasNextStaticField()) {
255 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700256 }
Ian Rogers0571d352011-11-03 19:51:38 -0700257 while (it.HasNextInstanceField()) {
258 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700259 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800260 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700261
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800262 void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
263 const DexFile::ClassDef& class_def) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800264 const byte* class_data = dex_file.GetClassData(class_def);
265 if (class_data == NULL) { // empty class such as a marker interface?
266 return;
267 }
268 ClassDataItemIterator it(dex_file, class_data);
269 SkipAllFields(it);
270
271 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700272 while (it.HasNextDirectMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800273 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800274 DumpOatMethod(os, class_method_index, oat_method, dex_file,
275 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800276 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700277 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700278 }
Ian Rogers0571d352011-11-03 19:51:38 -0700279 while (it.HasNextVirtualMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800280 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800281 DumpOatMethod(os, class_method_index, oat_method, dex_file,
282 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800283 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700284 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700285 }
Ian Rogers0571d352011-11-03 19:51:38 -0700286 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700287 os << std::flush;
288 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800289
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800290 void DumpOatMethod(std::ostream& os, uint32_t class_method_index,
Elliott Hughese3c845c2012-02-28 17:23:01 -0800291 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
292 uint32_t dex_method_idx, const DexFile::CodeItem* code_item) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700293 os << StringPrintf("\t%d: %s (dex_method_idx=%d)\n",
294 class_method_index, PrettyMethod(dex_method_idx, dex_file, true).c_str(),
295 dex_method_idx);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800296 os << StringPrintf("\t\tframe_size_in_bytes: %zd\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800297 oat_method.GetFrameSizeInBytes());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800298 os << StringPrintf("\t\tcore_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800299 oat_method.GetCoreSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800300 DumpSpillMask(os, oat_method.GetCoreSpillMask(), false);
301 os << StringPrintf("\n\t\tfp_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800302 oat_method.GetFpSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800303 DumpSpillMask(os, oat_method.GetFpSpillMask(), true);
304 os << StringPrintf("\n\t\tmapping_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800305 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800306 DumpMappingTable(os, oat_method);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800307 os << StringPrintf("\t\tvmap_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800308 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800309 DumpVmap(os, oat_method.GetVmapTable(), oat_method.GetCoreSpillMask(),
310 oat_method.GetFpSpillMask());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800311 os << StringPrintf("\t\tgc_map: %p (offset=0x%08x)\n",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800312 oat_method.GetGcMap(), oat_method.GetGcMapOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800313 DumpGcMap(os, oat_method.GetGcMap());
Logan Chien971bf3f2012-05-01 15:47:55 +0800314 os << StringPrintf("\t\tCODE: %p (offset=0x%08x size=%d)%s\n",
Elliott Hughes77405792012-03-15 15:22:12 -0700315 oat_method.GetCode(),
316 oat_method.GetCodeOffset(),
317 oat_method.GetCodeSize(),
318 oat_method.GetCode() != NULL ? "..." : "");
Ian Rogersd3fb5692012-04-09 21:56:13 -0700319 DumpCode(os, oat_method.GetCode(), oat_method.GetCodeSize(), oat_method.GetMappingTable(),
320 dex_file, code_item);
Logan Chien971bf3f2012-05-01 15:47:55 +0800321 os << StringPrintf("\t\tINVOKE STUB: %p (offset=0x%08x size=%d)%s\n",
Elliott Hughes77405792012-03-15 15:22:12 -0700322 oat_method.GetInvokeStub(),
323 oat_method.GetInvokeStubOffset(),
324 oat_method.GetInvokeStubSize(),
325 oat_method.GetInvokeStub() != NULL ? "..." : "");
Ian Rogersd3fb5692012-04-09 21:56:13 -0700326 DumpCode(os, reinterpret_cast<const void*>(oat_method.GetInvokeStub()),
327 oat_method.GetInvokeStubSize(), NULL, dex_file, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700328 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800329
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800330 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
331 if (spill_mask == 0) {
332 return;
333 }
334 os << " (";
335 for (size_t i = 0; i < 32; i++) {
336 if ((spill_mask & (1 << i)) != 0) {
337 if (is_float) {
338 os << "fr" << i;
339 } else {
340 os << "r" << i;
341 }
342 spill_mask ^= 1 << i; // clear bit
343 if (spill_mask != 0) {
344 os << ", ";
345 } else {
346 break;
347 }
348 }
349 }
350 os << ")";
351 }
352
353 void DumpVmap(std::ostream& os, const uint16_t* raw_table, uint32_t core_spill_mask,
354 uint32_t fp_spill_mask) {
355 if (raw_table == NULL) {
356 return;
357 }
358 const VmapTable vmap_table(raw_table);
359 bool first = true;
360 os << "\t\t\t";
361 for (size_t i = 0; i < vmap_table.size(); i++) {
362 uint16_t dex_reg = vmap_table[i];
363 size_t matches = 0;
364 size_t spill_shifts = 0;
365 uint32_t spill_mask = core_spill_mask;
366 bool processing_fp = false;
367 while (matches != (i + 1)) {
368 if (spill_mask == 0) {
369 CHECK(!processing_fp);
370 spill_mask = fp_spill_mask;
371 processing_fp = true;
372 }
373 matches += spill_mask & 1; // Add 1 if the low bit is set
374 spill_mask >>= 1;
375 spill_shifts++;
376 }
377 size_t arm_reg = spill_shifts - 1; // wind back one as we want the last match
378 os << (first ? "v" : ", v") << dex_reg;
379 if (arm_reg < 16) {
380 os << "/r" << arm_reg;
381 } else {
382 os << "/fr" << (arm_reg - 16);
383 }
384 if (first) {
385 first = false;
386 }
387 }
Elliott Hughesc073b072012-05-24 19:29:17 -0700388 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800389 }
390
391 void DumpGcMap(std::ostream& os, const uint8_t* gc_map_raw) {
392 if (gc_map_raw == NULL) {
393 return;
394 }
395 uint32_t gc_map_length = (gc_map_raw[0] << 24) | (gc_map_raw[1] << 16) |
396 (gc_map_raw[2] << 8) | (gc_map_raw[3] << 0);
397 verifier::PcToReferenceMap map(gc_map_raw + sizeof(uint32_t), gc_map_length);
398 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
399 os << StringPrintf("\t\t\t0x%04x", map.GetPC(entry));
400 size_t num_regs = map.RegWidth() * 8;
401 const uint8_t* reg_bitmap = map.GetBitMap(entry);
402 bool first = true;
403 for (size_t reg = 0; reg < num_regs; reg++) {
404 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
405 if (first) {
406 os << " v" << reg;
407 first = false;
408 } else {
409 os << ", v" << reg;
410 }
411 }
412 }
Elliott Hughesc073b072012-05-24 19:29:17 -0700413 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800414 }
415 }
416
417 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800418 const uint32_t* raw_table = oat_method.GetMappingTable();
419 const void* code = oat_method.GetCode();
420 if (raw_table == NULL || code == NULL) {
421 return;
422 }
423
424 uint32_t length = *raw_table;
425 ++raw_table;
426
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800427 os << "\t\t{";
Elliott Hughese3c845c2012-02-28 17:23:01 -0800428 for (size_t i = 0; i < length; i += 2) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800429 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800430 uint32_t dex_pc = raw_table[i + 1];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800431 os << StringPrintf("%p -> 0x%04x", native_pc, dex_pc);
432 if (i + 2 < length) {
433 os << ", ";
434 }
435 }
Elliott Hughesc073b072012-05-24 19:29:17 -0700436 os << "}\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800437 }
438
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 }
474
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700475 const std::string host_prefix_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800476 const OatFile& oat_file_;
477 std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800478 std::set<uint32_t> offsets_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800479 UniquePtr<Disassembler> disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700480};
481
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800482class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700483 public:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800484 explicit ImageDumper(std::ostream& os, const std::string& image_filename,
485 const std::string& host_prefix, Space& image_space,
Ian Rogersca190662012-06-26 15:45:57 -0700486 const ImageHeader& image_header)
487 : os_(os), image_filename_(image_filename), host_prefix_(host_prefix),
488 image_space_(image_space), image_header_(image_header) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700489
Ian Rogersb726dcb2012-09-05 08:57:23 -0700490 void Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800491 os_ << "MAGIC:\n";
492 os_ << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700493
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800494 os_ << "IMAGE BEGIN:\n";
495 os_ << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700496
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800497 os_ << "OAT CHECKSUM:\n";
498 os_ << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700499
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800500 os_ << "OAT BEGIN:\n";
501 os_ << reinterpret_cast<void*>(image_header_.GetOatBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700502
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800503 os_ << "OAT END:\n";
504 os_ << reinterpret_cast<void*>(image_header_.GetOatEnd()) << "\n\n";
505
506 os_ << "ROOTS:\n";
507 os_ << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700508 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700509 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
510 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700511 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800512 Object* image_root_object = image_header_.GetImageRoot(image_root);
513 os_ << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700514 if (image_root_object->IsObjectArray()) {
515 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
516 ObjectArray<Object>* image_root_object_array
517 = down_cast<ObjectArray<Object>*>(image_root_object);
518 // = image_root_object->AsObjectArray<Object>();
519 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800520 Object* value = image_root_object_array->Get(i);
521 if (value != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800522 os_ << "\t" << i << ": ";
Ian Rogersd5b32602012-02-26 16:40:04 -0800523 std::string summary;
524 PrettyObjectValue(summary, value->GetClass(), value);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800525 os_ << summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800526 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800527 os_ << StringPrintf("\t%d: null\n", i);
Ian Rogersd5b32602012-02-26 16:40:04 -0800528 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700529 }
530 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700531 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800532 os_ << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700533
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800534 os_ << "OAT LOCATION:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700535 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800536 Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation);
537 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800538 os_ << oat_location;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800539 if (!host_prefix_.empty()) {
540 oat_location = host_prefix_ + oat_location;
541 os_ << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700542 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800543 os_ << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800544 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700545 if (oat_file == NULL) {
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800546 os_ << "NOT FOUND\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700547 return;
548 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800549 os_ << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700550
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800551 stats_.oat_file_bytes = oat_file->Size();
552
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700553 oat_dumper_.reset(new OatDumper(host_prefix_, *oat_file));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800554
555 os_ << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700556
557 // Loop through all the image spaces and dump their objects.
558 Heap* heap = Runtime::Current()->GetHeap();
559 const Spaces& spaces = heap->GetSpaces();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700560 {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700561 WriterMutexLock mu(*Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700562 heap->FlushAllocStack();
563 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700564 ReaderMutexLock mu(*Locks::heap_bitmap_lock_);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700565 // TODO: C++0x auto
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700566 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
567 (*cur)->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
568 os_ << "\n";
569 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800570
571 os_ << "STATS:\n" << std::flush;
572 UniquePtr<File> file(OS::OpenFile(image_filename_.c_str(), false));
573 stats_.file_bytes = file->Length();
574 size_t header_bytes = sizeof(ImageHeader);
575 stats_.header_bytes = header_bytes;
576 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
577 stats_.alignment_bytes += alignment_bytes;
578 stats_.Dump(os_);
579 os_ << "\n";
580
581 os_ << std::flush;
582
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800583 oat_dumper_->Dump(os_);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700584 }
585
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700586 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700587 static void PrettyObjectValue(std::string& summary, Class* type, Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700588 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800589 CHECK(type != NULL);
590 if (value == NULL) {
591 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
592 } else if (type->IsStringClass()) {
593 String* string = value->AsString();
594 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
595 } else if (value->IsClass()) {
596 Class* klass = value->AsClass();
597 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
598 } else if (value->IsField()) {
599 Field* field = value->AsField();
600 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
601 } else if (value->IsMethod()) {
602 Method* method = value->AsMethod();
603 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
604 } else {
605 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
606 }
607 }
608
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700609 static void PrintField(std::string& summary, Field* field, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700610 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800611 FieldHelper fh(field);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700612 const char* descriptor = fh.GetTypeDescriptor();
Ian Rogersd5b32602012-02-26 16:40:04 -0800613 StringAppendF(&summary, "\t%s: ", fh.GetName());
Ian Rogers48efc2b2012-08-27 17:20:31 -0700614 if (descriptor[0] != 'L' && descriptor[0] != '[') {
615 Class* type = fh.GetType();
616 if (type->IsPrimitiveLong()) {
617 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
618 } else if (type->IsPrimitiveDouble()) {
619 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
620 } else if (type->IsPrimitiveFloat()) {
621 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
622 } else {
623 DCHECK(type->IsPrimitive());
624 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
625 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800626 } else {
Ian Rogers48efc2b2012-08-27 17:20:31 -0700627 // Get the value, don't compute the type unless it is non-null as we don't want
628 // to cause class loading.
Ian Rogersd5b32602012-02-26 16:40:04 -0800629 Object* value = field->GetObj(obj);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700630 if (value == NULL) {
631 StringAppendF(&summary, "null %s\n", PrettyDescriptor(descriptor).c_str());
632 } else {
633 PrettyObjectValue(summary, fh.GetType(), value);
634 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800635 }
636 }
637
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700638 static void DumpFields(std::string& summary, Object* obj, Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700639 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800640 Class* super = klass->GetSuperClass();
641 if (super != NULL) {
642 DumpFields(summary, obj, super);
643 }
644 ObjectArray<Field>* fields = klass->GetIFields();
645 if (fields != NULL) {
646 for (int32_t i = 0; i < fields->GetLength(); i++) {
647 Field* field = fields->Get(i);
648 PrintField(summary, field, obj);
649 }
650 }
651 }
652
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800653 bool InDumpSpace(const Object* object) {
654 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700655 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800656
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700657 const void* GetOatCodeBegin(Method* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700658 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800659 Runtime* runtime = Runtime::Current();
660 const void* code = m->GetCode();
Ian Rogersfb6adba2012-03-04 21:51:51 -0800661 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800662 code = oat_dumper_->GetOatCode(m);
663 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700664 if (oat_dumper_->GetInstructionSet() == kThumb2) {
665 code = reinterpret_cast<void*>(reinterpret_cast<uint32_t>(code) & ~0x1);
666 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800667 return code;
668 }
669
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700670 uint32_t GetOatCodeSize(Method* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700671 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700672 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetOatCodeBegin(m));
673 if (oat_code_begin == NULL) {
674 return 0;
675 }
676 return oat_code_begin[-1];
677 }
678
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700679 const void* GetOatCodeEnd(Method* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700680 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700681 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetOatCodeBegin(m));
682 if (oat_code_begin == NULL) {
683 return NULL;
684 }
685 return oat_code_begin + GetOatCodeSize(m);
686 }
687
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700688 static void Callback(Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700689 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700690 DCHECK(obj != NULL);
691 DCHECK(arg != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800692 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700693 if (!state->InDumpSpace(obj)) {
694 return;
695 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700696
697 size_t object_bytes = obj->SizeOf();
698 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
699 state->stats_.object_bytes += object_bytes;
700 state->stats_.alignment_bytes += alignment_bytes;
701
Brian Carlstrom78128a62011-09-15 17:21:19 -0700702 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800703 Class* obj_class = obj->GetClass();
704 if (obj_class->IsArrayClass()) {
705 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
706 obj->AsArray()->GetLength());
707 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700708 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800709 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
710 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700711 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800712 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700713 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800714 } else if (obj->IsField()) {
715 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
716 PrettyField(obj->AsField()).c_str());
717 } else if (obj->IsMethod()) {
718 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
719 PrettyMethod(obj->AsMethod()).c_str());
720 } else if (obj_class->IsStringClass()) {
Elliott Hughes82914b62012-04-09 15:56:29 -0700721 StringAppendF(&summary, "%p: java.lang.String %s\n", obj,
722 PrintableString(obj->AsString()->ToModifiedUtf8()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800723 } else {
724 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
725 }
726 DumpFields(summary, obj, obj_class);
727 if (obj->IsObjectArray()) {
728 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
729 int32_t length = obj_array->GetLength();
730 for (int32_t i = 0; i < length; i++) {
731 Object* value = obj_array->Get(i);
732 size_t run = 0;
733 for (int32_t j = i + 1; j < length; j++) {
734 if (value == obj_array->Get(j)) {
735 run++;
736 } else {
737 break;
738 }
739 }
740 if (run == 0) {
741 StringAppendF(&summary, "\t%d: ", i);
742 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800743 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800744 i = i + run;
745 }
746 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
747 PrettyObjectValue(summary, value_class, value);
748 }
749 } else if (obj->IsClass()) {
750 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
751 if (sfields != NULL) {
752 summary += "\t\tSTATICS:\n";
753 for (int32_t i = 0; i < sfields->GetLength(); i++) {
754 Field* field = sfields->Get(i);
755 PrintField(summary, field, NULL);
756 }
757 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700758 } else if (obj->IsMethod()) {
759 Method* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700760 if (method->IsNative()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700761 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800762 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700763 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800764 bool first_occurrence;
765 size_t invoke_stub_size = state->ComputeOatSize(
766 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
767 if (first_occurrence) {
768 state->stats_.managed_to_native_code_bytes += invoke_stub_size;
769 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700770 const void* oat_code = state->GetOatCodeBegin(method);
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700771 uint32_t oat_code_size = state->GetOatCodeSize(method);
772 state->ComputeOatSize(oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800773 if (first_occurrence) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700774 state->stats_.native_to_managed_code_bytes += oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800775 }
776 if (oat_code != method->GetCode()) {
777 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
778 }
Ian Rogers19846512012-02-24 11:42:47 -0800779 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
780 method->IsResolutionMethod()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700781 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800782 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700783 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700784 } else {
TDYa1273db52852012-04-01 15:11:43 -0700785#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800786 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
787 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
TDYa1273db52852012-04-01 15:11:43 -0700788#endif
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700789
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800790 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700791 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700792 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800793
Elliott Hughesa0e18062012-04-13 15:59:59 -0700794 bool first_occurrence;
795 size_t gc_map_bytes = state->ComputeOatSize(method->GetGcMapRaw(), &first_occurrence);
796 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800797 state->stats_.gc_map_bytes += gc_map_bytes;
798 }
799
800 size_t pc_mapping_table_bytes =
Elliott Hughesa0e18062012-04-13 15:59:59 -0700801 state->ComputeOatSize(method->GetMappingTableRaw(), &first_occurrence);
802 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800803 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
804 }
805
806 size_t vmap_table_bytes =
Elliott Hughesa0e18062012-04-13 15:59:59 -0700807 state->ComputeOatSize(method->GetVmapTableRaw(), &first_occurrence);
808 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800809 state->stats_.vmap_table_bytes += vmap_table_bytes;
810 }
811
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700812 // TODO: compute invoke stub using length from oat file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800813 size_t invoke_stub_size = state->ComputeOatSize(
Elliott Hughesa0e18062012-04-13 15:59:59 -0700814 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
815 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800816 state->stats_.native_to_managed_code_bytes += invoke_stub_size;
817 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700818 const void* oat_code_begin = state->GetOatCodeBegin(method);
819 const void* oat_code_end = state->GetOatCodeEnd(method);
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700820 uint32_t oat_code_size = state->GetOatCodeSize(method);
Elliott Hughesa0e18062012-04-13 15:59:59 -0700821 state->ComputeOatSize(oat_code_begin, &first_occurrence);
822 if (first_occurrence) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700823 state->stats_.managed_code_bytes += oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -0700824 if (method->IsConstructor()) {
825 if (method->IsStatic()) {
826 state->stats_.class_initializer_code_bytes += oat_code_size;
827 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
828 state->stats_.large_initializer_code_bytes += oat_code_size;
829 }
830 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
831 state->stats_.large_method_code_bytes += oat_code_size;
832 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800833 }
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700834 state->stats_.managed_code_bytes_ignoring_deduplication += oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800835
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700836 StringAppendF(&summary, "\t\tOAT CODE: %p-%p\n", oat_code_begin, oat_code_end);
Ian Rogersd5b32602012-02-26 16:40:04 -0800837 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800838 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
839
840 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700841 vmap_table_bytes + invoke_stub_size + oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800842
843 double expansion =
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700844 static_cast<double>(oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800845 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700846 }
847 }
Elliott Hughesa0e18062012-04-13 15:59:59 -0700848 state->stats_.Update(ClassHelper(obj_class).GetDescriptor(), object_bytes);
Ian Rogersd5b32602012-02-26 16:40:04 -0800849
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700850 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700851 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700852
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800853 std::set<const void*> already_seen_;
854 // Compute the size of the given data within the oat file and whether this is the first time
855 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -0700856 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800857 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700858 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800859 already_seen_.insert(oat_data);
860 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700861 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800862 }
863 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700864 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700865
866 public:
867 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800868 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700869 size_t file_bytes;
870
871 size_t header_bytes;
872 size_t object_bytes;
873 size_t alignment_bytes;
874
875 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800876 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700877 size_t managed_to_native_code_bytes;
878 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -0700879 size_t class_initializer_code_bytes;
880 size_t large_initializer_code_bytes;
881 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700882
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800883 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700884 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800885 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700886
887 size_t dex_instruction_bytes;
888
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800889 std::vector<Method*> method_outlier;
890 std::vector<size_t> method_outlier_size;
891 std::vector<double> method_outlier_expansion;
892
893 explicit Stats()
894 : oat_file_bytes(0),
895 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700896 header_bytes(0),
897 object_bytes(0),
898 alignment_bytes(0),
899 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800900 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700901 managed_to_native_code_bytes(0),
902 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -0700903 class_initializer_code_bytes(0),
904 large_initializer_code_bytes(0),
905 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800906 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700907 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800908 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700909 dex_instruction_bytes(0) {}
910
Elliott Hughesa0e18062012-04-13 15:59:59 -0700911 struct SizeAndCount {
912 SizeAndCount(size_t bytes, size_t count) : bytes(bytes), count(count) {}
913 size_t bytes;
914 size_t count;
915 };
916 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
917 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700918
Elliott Hughesa0e18062012-04-13 15:59:59 -0700919 void Update(const std::string& descriptor, size_t object_bytes) {
920 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
921 if (it != sizes_and_counts.end()) {
922 it->second.bytes += object_bytes;
923 it->second.count += 1;
924 } else {
925 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes, 1));
926 }
927 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700928
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800929 double PercentOfOatBytes(size_t size) {
930 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
931 }
932
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700933 double PercentOfFileBytes(size_t size) {
934 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
935 }
936
937 double PercentOfObjectBytes(size_t size) {
938 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
939 }
940
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800941 void ComputeOutliers(size_t total_size, double expansion, Method* method) {
942 method_outlier_size.push_back(total_size);
943 method_outlier_expansion.push_back(expansion);
944 method_outlier.push_back(method);
945 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700946
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700947 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700948 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800949 size_t sum_of_sizes = 0;
950 size_t sum_of_sizes_squared = 0;
951 size_t sum_of_expansion = 0;
952 size_t sum_of_expansion_squared = 0;
953 size_t n = method_outlier_size.size();
954 for (size_t i = 0; i < n; i++) {
955 size_t cur_size = method_outlier_size[i];
956 sum_of_sizes += cur_size;
957 sum_of_sizes_squared += cur_size * cur_size;
958 double cur_expansion = method_outlier_expansion[i];
959 sum_of_expansion += cur_expansion;
960 sum_of_expansion_squared += cur_expansion * cur_expansion;
961 }
962 size_t size_mean = sum_of_sizes / n;
963 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
964 double expansion_mean = sum_of_expansion / n;
965 double expansion_variance =
966 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
967
968 // Dump methods whose size is a certain number of standard deviations from the mean
969 size_t dumped_values = 0;
970 size_t skipped_values = 0;
971 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
972 size_t cur_size_variance = i * i * size_variance;
973 bool first = true;
974 for (size_t j = 0; j < n; j++) {
975 size_t cur_size = method_outlier_size[j];
976 if (cur_size > size_mean) {
977 size_t cur_var = cur_size - size_mean;
978 cur_var = cur_var * cur_var;
979 if (cur_var > cur_size_variance) {
980 if (dumped_values > 20) {
981 if (i == 1) {
982 skipped_values++;
983 } else {
984 i = 2; // jump to counting for 1 standard deviation
985 break;
986 }
987 } else {
988 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700989 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800990 first = false;
991 }
992 os << "\t" << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -0700993 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800994 method_outlier_size[j] = 0; // don't consider this method again
995 dumped_values++;
996 }
997 }
998 }
999 }
1000 }
1001 if (skipped_values > 0) {
1002 os << "\t... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001003 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001004 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001005 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001006
1007 // Dump methods whose expansion is a certain number of standard deviations from the mean
1008 dumped_values = 0;
1009 skipped_values = 0;
1010 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1011 double cur_expansion_variance = i * i * expansion_variance;
1012 bool first = true;
1013 for (size_t j = 0; j < n; j++) {
1014 double cur_expansion = method_outlier_expansion[j];
1015 if (cur_expansion > expansion_mean) {
1016 size_t cur_var = cur_expansion - expansion_mean;
1017 cur_var = cur_var * cur_var;
1018 if (cur_var > cur_expansion_variance) {
1019 if (dumped_values > 20) {
1020 if (i == 1) {
1021 skipped_values++;
1022 } else {
1023 i = 2; // jump to counting for 1 standard deviation
1024 break;
1025 }
1026 } else {
1027 if (first) {
1028 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001029 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001030 first = false;
1031 }
1032 os << "\t" << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001033 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001034 method_outlier_expansion[j] = 0.0; // don't consider this method again
1035 dumped_values++;
1036 }
1037 }
1038 }
1039 }
1040 }
1041 if (skipped_values > 0) {
1042 os << "\t... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001043 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001044 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001045 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001046 }
1047
Ian Rogersb726dcb2012-09-05 08:57:23 -07001048 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001049 os << "\tart_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1050 << "\tart_file_bytes = header_bytes + object_bytes + alignment_bytes\n"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001051 << StringPrintf("\theader_bytes = %8zd (%2.0f%% of art file bytes)\n"
1052 "\tobject_bytes = %8zd (%2.0f%% of art file bytes)\n"
Elliott Hughesc073b072012-05-24 19:29:17 -07001053 "\talignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001054 header_bytes, PercentOfFileBytes(header_bytes),
1055 object_bytes, PercentOfFileBytes(object_bytes),
1056 alignment_bytes, PercentOfFileBytes(alignment_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001057 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001058
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001059 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
1060
Elliott Hughesa0e18062012-04-13 15:59:59 -07001061 os << "\tobject_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001062 size_t object_bytes_total = 0;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001063 typedef SizeAndCountTable::const_iterator It; // TODO: C++0x auto
1064 for (It it = sizes_and_counts.begin(), end = sizes_and_counts.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -08001065 const std::string& descriptor(it->first);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001066 double average = static_cast<double>(it->second.bytes) / static_cast<double>(it->second.count);
1067 double percent = PercentOfObjectBytes(it->second.bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001068 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001069 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
1070 descriptor.c_str(), it->second.bytes, it->second.count,
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001071 average, percent);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001072 object_bytes_total += it->second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001073 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001074 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001075 CHECK_EQ(object_bytes, object_bytes_total);
1076
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001077 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1078 "\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers0d2d3782012-04-10 11:09:18 -07001079 "\tnative_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1080 "\tclass_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1081 "\tlarge_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Elliott Hughesc073b072012-05-24 19:29:17 -07001082 "\tlarge_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001083 managed_code_bytes, PercentOfOatBytes(managed_code_bytes),
1084 managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001085 native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes),
1086 class_initializer_code_bytes, PercentOfOatBytes(class_initializer_code_bytes),
1087 large_initializer_code_bytes, PercentOfOatBytes(large_initializer_code_bytes),
1088 large_method_code_bytes, PercentOfOatBytes(large_method_code_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001089 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001090
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001091 os << StringPrintf("\tgc_map_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1092 "\tpc_mapping_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
Elliott Hughesc073b072012-05-24 19:29:17 -07001093 "\tvmap_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001094 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1095 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1096 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001097 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001098
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001099 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes)
Elliott Hughesc073b072012-05-24 19:29:17 -07001100 << StringPrintf("\tmanaged_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
1101 static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes),
1102 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001103 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001104 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001105
1106 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001107 }
1108 } stats_;
1109
1110 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001111 enum {
1112 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1113 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1114 kLargeConstructorDexBytes = 4000,
1115 // Number of bytes for a method to be considered large. Based on the 4000 basic block
1116 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1117 kLargeMethodDexBytes = 16000
1118 };
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001119 UniquePtr<OatDumper> oat_dumper_;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001120 std::ostream& os_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001121 const std::string image_filename_;
1122 const std::string host_prefix_;
1123 Space& image_space_;
1124 const ImageHeader& image_header_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001125
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001126 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001127};
1128
Elliott Hughes72395bf2012-04-24 13:45:26 -07001129static int oatdump(int argc, char** argv) {
Elliott Hughes0d39c122012-06-06 16:41:17 -07001130 InitLogging(argv);
Elliott Hughes72395bf2012-04-24 13:45:26 -07001131
Brian Carlstrom78128a62011-09-15 17:21:19 -07001132 // Skip over argv[0].
1133 argv++;
1134 argc--;
1135
1136 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001137 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001138 usage();
1139 }
1140
Brian Carlstromaded5f72011-10-07 17:15:04 -07001141 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001142 const char* image_filename = NULL;
1143 const char* boot_image_filename = NULL;
Logan Chien0cc6ab62012-03-20 22:57:52 +08001144 std::string elf_filename_prefix;
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001145 UniquePtr<std::string> host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001146 std::ostream* os = &std::cout;
1147 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001148
1149 for (int i = 0; i < argc; i++) {
1150 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001151 if (option.starts_with("--oat-file=")) {
1152 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001153 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001154 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001155 } else if (option.starts_with("--boot-image=")) {
1156 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001157 } else if (option.starts_with("--host-prefix=")) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001158 host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001159 } else if (option.starts_with("--output=")) {
1160 const char* filename = option.substr(strlen("--output=")).data();
1161 out.reset(new std::ofstream(filename));
1162 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001163 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001164 usage();
1165 }
1166 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001167 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001168 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001169 usage();
1170 }
1171 }
1172
Brian Carlstromaded5f72011-10-07 17:15:04 -07001173 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001174 fprintf(stderr, "Either --image or --oat must be specified\n");
1175 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001176 }
1177
Brian Carlstromaded5f72011-10-07 17:15:04 -07001178 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001179 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1180 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001181 }
1182
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001183 if (host_prefix.get() == NULL) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001184 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
1185 if (android_product_out != NULL) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001186 host_prefix.reset(new std::string(android_product_out));
1187 } else {
1188 host_prefix.reset(new std::string(""));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001189 }
1190 }
1191
Brian Carlstromaded5f72011-10-07 17:15:04 -07001192 if (oat_filename != NULL) {
Logan Chien0c717dd2012-03-28 18:31:07 +08001193 OatFile* oat_file =
1194 OatFile::Open(oat_filename, oat_filename, NULL, OatFile::kRelocNone);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001195 if (oat_file == NULL) {
1196 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
1197 return EXIT_FAILURE;
1198 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001199 OatDumper oat_dumper(*host_prefix.get(), *oat_file);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001200 oat_dumper.Dump(*os);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001201 return EXIT_SUCCESS;
1202 }
1203
Brian Carlstrom78128a62011-09-15 17:21:19 -07001204 Runtime::Options options;
1205 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001206 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001207 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001208 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001209 if (boot_image_filename != NULL) {
1210 boot_image_option += "-Ximage:";
1211 boot_image_option += boot_image_filename;
1212 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001213 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001214 if (image_filename != NULL) {
1215 image_option += "-Ximage:";
1216 image_option += image_filename;
1217 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1218 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001219
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001220 if (!host_prefix->empty()) {
1221 options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001222 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001223
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001224 if (!Runtime::Create(options, false)) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001225 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001226 return EXIT_FAILURE;
1227 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001228 UniquePtr<Runtime> runtime(Runtime::Current());
1229 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
1230 // give it away now and then switch to a more managable ScopedObjectAccess.
1231 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
1232 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001233
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001234 Heap* heap = Runtime::Current()->GetHeap();
Brian Carlstromfddf6f62012-03-15 16:56:45 -07001235 ImageSpace* image_space = heap->GetImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001236 CHECK(image_space != NULL);
1237 const ImageHeader& image_header = image_space->GetImageHeader();
1238 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001239 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001240 return EXIT_FAILURE;
1241 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001242 ImageDumper image_dumper(*os, image_filename, *host_prefix.get(), *image_space, image_header);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001243 image_dumper.Dump();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001244 return EXIT_SUCCESS;
1245}
1246
1247} // namespace art
1248
1249int main(int argc, char** argv) {
1250 return art::oatdump(argc, argv);
1251}