blob: 5bcb92786aafcf173aa8f020b49215755d090787 [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,
Logan Chien0cc6ab62012-03-20 22:57:52 +080060 " --extract-elf-to=<file.elf>: provide the prefix of the filename for\n"
61 " the output ELF files.\n"
62 " Example: --extract-elf-to=output.elf\n"
63 "\n");
64 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070065 " --host-prefix may be used to translate host paths to target paths during\n"
66 " cross compilation.\n"
67 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstromfe487d02012-02-29 18:49:16 -080068 " Default: $ANDROID_PRODUCT_OUT\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070069 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070070 fprintf(stderr,
71 " --output=<file> may be used to send the output to a file.\n"
72 " Example: --output=/tmp/oatdump.txt\n"
73 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070074 exit(EXIT_FAILURE);
75}
76
Ian Rogersff1ed472011-09-20 13:46:24 -070077const char* image_roots_descriptions_[] = {
Brian Carlstrom78128a62011-09-15 17:21:19 -070078 "kJniStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070079 "kAbstractMethodErrorStubArray",
Ian Rogersad25ac52011-10-04 19:13:33 -070080 "kStaticResolutionStubArray",
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070081 "kUnknownMethodResolutionStubArray",
Ian Rogers19846512012-02-24 11:42:47 -080082 "kResolutionMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070083 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070084 "kRefsOnlySaveMethod",
85 "kRefsAndArgsSaveMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070086 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070087 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070088 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070089};
90
Elliott Hughese3c845c2012-02-28 17:23:01 -080091class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -070092 public:
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070093 explicit OatDumper(const std::string& host_prefix, const OatFile& oat_file)
94 : host_prefix_(host_prefix),
95 oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -080096 oat_dex_files_(oat_file.GetOatDexFiles()),
97 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet())) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080098 AddAllOffsets();
99 }
100
101 void Dump(std::ostream& os) {
102 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700103
104 os << "MAGIC:\n";
105 os << oat_header.GetMagic() << "\n\n";
106
107 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800108 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700109
Elliott Hughesa72ec822012-03-05 17:12:22 -0800110 os << "INSTRUCTION SET:\n";
111 os << oat_header.GetInstructionSet() << "\n\n";
112
Brian Carlstromaded5f72011-10-07 17:15:04 -0700113 os << "DEX FILE COUNT:\n";
114 os << oat_header.GetDexFileCount() << "\n\n";
115
Logan Chien0cc6ab62012-03-20 22:57:52 +0800116 os << "ELF IMAGE COUNT:\n";
117 os << oat_header.GetElfImageCount() << "\n\n";
118
Brian Carlstromaded5f72011-10-07 17:15:04 -0700119 os << "EXECUTABLE OFFSET:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800120 os << StringPrintf("0x%08x\n\n", oat_header.GetExecutableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700121
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700122 os << "IMAGE FILE LOCATION CHECKSUM:\n";
123 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationChecksum());
124
125 os << "IMAGE FILE LOCATION:\n";
126 const std::string image_file_location(oat_header.GetImageFileLocation());
127 os << image_file_location;
128 if (!image_file_location.empty() && !host_prefix_.empty()) {
129 os << " (" << host_prefix_ << image_file_location << ")";
130 }
131 os << "\n\n";
132
Ian Rogers30fab402012-01-23 15:43:46 -0800133 os << "BEGIN:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800134 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700135
Ian Rogers30fab402012-01-23 15:43:46 -0800136 os << "END:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800137 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700138
Brian Carlstrom33412de2012-04-01 16:55:06 -0700139 os << "THREAD ENTRY POINTS:\n";
140 uintptr_t start_offset = OFFSETOF_MEMBER(Thread, entrypoints_);
141 uintptr_t last_offset = start_offset - sizeof(uintptr_t);
142
143#define DUMP_ENTRY_POINT(x) { \
144 uintptr_t offset = ENTRYPOINT_OFFSET(x); \
145 CHECK_EQ(last_offset + sizeof(uintptr_t), offset); \
146 os << #x << ": " << offset << "\n"; \
147 last_offset = offset; \
148 }
149
150 DUMP_ENTRY_POINT(pAllocArrayFromCode);
151 DUMP_ENTRY_POINT(pAllocArrayFromCodeWithAccessCheck);
152 DUMP_ENTRY_POINT(pAllocObjectFromCode);
153 DUMP_ENTRY_POINT(pAllocObjectFromCodeWithAccessCheck);
154 DUMP_ENTRY_POINT(pCheckAndAllocArrayFromCode);
155 DUMP_ENTRY_POINT(pCheckAndAllocArrayFromCodeWithAccessCheck);
156 DUMP_ENTRY_POINT(pInstanceofNonTrivialFromCode);
157 DUMP_ENTRY_POINT(pCanPutArrayElementFromCode);
158 DUMP_ENTRY_POINT(pCheckCastFromCode);
159 DUMP_ENTRY_POINT(pDebugMe);
160 DUMP_ENTRY_POINT(pUpdateDebuggerFromCode);
161 DUMP_ENTRY_POINT(pInitializeStaticStorage);
162 DUMP_ENTRY_POINT(pInitializeTypeAndVerifyAccessFromCode);
163 DUMP_ENTRY_POINT(pInitializeTypeFromCode);
164 DUMP_ENTRY_POINT(pResolveStringFromCode);
165 DUMP_ENTRY_POINT(pSet32Instance);
166 DUMP_ENTRY_POINT(pSet32Static);
167 DUMP_ENTRY_POINT(pSet64Instance);
168 DUMP_ENTRY_POINT(pSet64Static);
169 DUMP_ENTRY_POINT(pSetObjInstance);
170 DUMP_ENTRY_POINT(pSetObjStatic);
171 DUMP_ENTRY_POINT(pGet32Instance);
172 DUMP_ENTRY_POINT(pGet32Static);
173 DUMP_ENTRY_POINT(pGet64Instance);
174 DUMP_ENTRY_POINT(pGet64Static);
175 DUMP_ENTRY_POINT(pGetObjInstance);
176 DUMP_ENTRY_POINT(pGetObjStatic);
177 DUMP_ENTRY_POINT(pHandleFillArrayDataFromCode);
178 DUMP_ENTRY_POINT(pDecodeJObjectInThread);
179 DUMP_ENTRY_POINT(pFindNativeMethod);
180 DUMP_ENTRY_POINT(pLockObjectFromCode);
181 DUMP_ENTRY_POINT(pUnlockObjectFromCode);
182 DUMP_ENTRY_POINT(pCmpgDouble);
183 DUMP_ENTRY_POINT(pCmpgFloat);
184 DUMP_ENTRY_POINT(pCmplDouble);
185 DUMP_ENTRY_POINT(pCmplFloat);
186 DUMP_ENTRY_POINT(pDadd);
187 DUMP_ENTRY_POINT(pDdiv);
188 DUMP_ENTRY_POINT(pDmul);
189 DUMP_ENTRY_POINT(pDsub);
190 DUMP_ENTRY_POINT(pF2d);
191 DUMP_ENTRY_POINT(pFmod);
192 DUMP_ENTRY_POINT(pI2d);
193 DUMP_ENTRY_POINT(pL2d);
194 DUMP_ENTRY_POINT(pD2f);
195 DUMP_ENTRY_POINT(pFadd);
196 DUMP_ENTRY_POINT(pFdiv);
197 DUMP_ENTRY_POINT(pFmodf);
198 DUMP_ENTRY_POINT(pFmul);
199 DUMP_ENTRY_POINT(pFsub);
200 DUMP_ENTRY_POINT(pI2f);
201 DUMP_ENTRY_POINT(pL2f);
202 DUMP_ENTRY_POINT(pD2iz);
203 DUMP_ENTRY_POINT(pF2iz);
204 DUMP_ENTRY_POINT(pIdiv);
205 DUMP_ENTRY_POINT(pIdivmod);
206 DUMP_ENTRY_POINT(pD2l);
207 DUMP_ENTRY_POINT(pF2l);
Brian Carlstroma7302082012-04-05 15:43:40 -0700208 DUMP_ENTRY_POINT(pLdiv);
Brian Carlstrom33412de2012-04-01 16:55:06 -0700209 DUMP_ENTRY_POINT(pLdivmod);
210 DUMP_ENTRY_POINT(pLmul);
Brian Carlstrom33412de2012-04-01 16:55:06 -0700211 DUMP_ENTRY_POINT(pShlLong);
212 DUMP_ENTRY_POINT(pShrLong);
213 DUMP_ENTRY_POINT(pUshrLong);
214 DUMP_ENTRY_POINT(pIndexOf);
215 DUMP_ENTRY_POINT(pMemcmp16);
216 DUMP_ENTRY_POINT(pStringCompareTo);
217 DUMP_ENTRY_POINT(pMemcpy);
218 DUMP_ENTRY_POINT(pFindInterfaceMethodInCache);
219 DUMP_ENTRY_POINT(pUnresolvedDirectMethodTrampolineFromCode);
220 DUMP_ENTRY_POINT(pInvokeDirectTrampolineWithAccessCheck);
221 DUMP_ENTRY_POINT(pInvokeInterfaceTrampoline);
222 DUMP_ENTRY_POINT(pInvokeInterfaceTrampolineWithAccessCheck);
223 DUMP_ENTRY_POINT(pInvokeStaticTrampolineWithAccessCheck);
224 DUMP_ENTRY_POINT(pInvokeSuperTrampolineWithAccessCheck);
225 DUMP_ENTRY_POINT(pInvokeVirtualTrampolineWithAccessCheck);
226 DUMP_ENTRY_POINT(pCheckSuspendFromCode);
227 DUMP_ENTRY_POINT(pTestSuspendFromCode);
228 DUMP_ENTRY_POINT(pDeliverException);
229 DUMP_ENTRY_POINT(pThrowAbstractMethodErrorFromCode);
230 DUMP_ENTRY_POINT(pThrowArrayBoundsFromCode);
231 DUMP_ENTRY_POINT(pThrowDivZeroFromCode);
232 DUMP_ENTRY_POINT(pThrowNoSuchMethodFromCode);
233 DUMP_ENTRY_POINT(pThrowNullPointerFromCode);
234 DUMP_ENTRY_POINT(pThrowStackOverflowFromCode);
235 DUMP_ENTRY_POINT(pThrowVerificationErrorFromCode);
236
237#undef DUMP_ENTRY_POINT
238
239 CHECK_EQ(start_offset + sizeof(EntryPoints), last_offset + sizeof(uintptr_t));
240 os << "\n";
241
Brian Carlstromaded5f72011-10-07 17:15:04 -0700242 os << std::flush;
243
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800244 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
245 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Brian Carlstromaded5f72011-10-07 17:15:04 -0700246 CHECK(oat_dex_file != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800247 DumpOatDexFile(os, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700248 }
249 }
250
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800251 size_t ComputeSize(const void* oat_data) {
252 if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() ||
253 reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) {
254 return 0; // Address not in oat file
255 }
256 uint32_t begin_offset = reinterpret_cast<size_t>(oat_data) -
257 reinterpret_cast<size_t>(oat_file_.Begin());
258 typedef std::set<uint32_t>::iterator It;
259 It it = offsets_.upper_bound(begin_offset);
260 CHECK(it != offsets_.end());
261 uint32_t end_offset = *it;
262 return end_offset - begin_offset;
263 }
264
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700265 InstructionSet GetInstructionSet() {
266 return oat_file_.GetOatHeader().GetInstructionSet();
267 }
268
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800269 const void* GetOatCode(Method* m) {
270 MethodHelper mh(m);
271 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
272 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
273 CHECK(oat_dex_file != NULL);
274 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
275 if (dex_file.get() != NULL) {
276 uint32_t class_def_index;
277 bool found = dex_file->FindClassDefIndex(mh.GetDeclaringClassDescriptor(), class_def_index);
278 if (found) {
279 const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_index);
280 CHECK(oat_class != NULL);
281 size_t method_index = m->GetMethodIndex();
282 return oat_class->GetOatMethod(method_index).GetCode();
283 }
284 }
285 }
286 return NULL;
287 }
288
Brian Carlstromaded5f72011-10-07 17:15:04 -0700289 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800290 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800291 // We don't know the length of the code for each method, but we need to know where to stop
292 // when disassembling. What we do know is that a region of code will be followed by some other
293 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
294 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800295 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
296 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800297 CHECK(oat_dex_file != NULL);
298 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
299 if (dex_file.get() == NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800300 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800301 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800302 offsets_.insert(reinterpret_cast<uint32_t>(&dex_file->GetHeader()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800303 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
304 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
305 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index));
306 const byte* class_data = dex_file->GetClassData(class_def);
307 if (class_data != NULL) {
308 ClassDataItemIterator it(*dex_file, class_data);
309 SkipAllFields(it);
310 uint32_t class_method_index = 0;
311 while (it.HasNextDirectMethod()) {
312 AddOffsets(oat_class->GetOatMethod(class_method_index++));
313 it.Next();
314 }
315 while (it.HasNextVirtualMethod()) {
316 AddOffsets(oat_class->GetOatMethod(class_method_index++));
317 it.Next();
318 }
319 }
320 }
321 }
322
323 // If the last thing in the file is code for a method, there won't be an offset for the "next"
324 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
325 // for the end of the file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800326 offsets_.insert(static_cast<uint32_t>(oat_file_.End() - oat_file_.Begin()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800327 }
328
329 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800330 uint32_t code_offset = oat_method.GetCodeOffset();
331 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
332 code_offset &= ~0x1;
333 }
334 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800335 offsets_.insert(oat_method.GetMappingTableOffset());
336 offsets_.insert(oat_method.GetVmapTableOffset());
337 offsets_.insert(oat_method.GetGcMapOffset());
338 offsets_.insert(oat_method.GetInvokeStubOffset());
339 }
340
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800341 void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700342 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800343 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800344 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800345 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile());
346 if (dex_file.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700347 os << "NOT FOUND\n\n";
348 return;
349 }
350 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
351 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
352 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700353 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
354 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800355 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800356 << oat_class->GetStatus() << ")\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800357 DumpOatClass(os, *oat_class.get(), *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700358 }
359
360 os << std::flush;
361 }
362
Elliott Hughese3c845c2012-02-28 17:23:01 -0800363 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700364 while (it.HasNextStaticField()) {
365 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700366 }
Ian Rogers0571d352011-11-03 19:51:38 -0700367 while (it.HasNextInstanceField()) {
368 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700369 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800370 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700371
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800372 void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
373 const DexFile::ClassDef& class_def) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800374 const byte* class_data = dex_file.GetClassData(class_def);
375 if (class_data == NULL) { // empty class such as a marker interface?
376 return;
377 }
378 ClassDataItemIterator it(dex_file, class_data);
379 SkipAllFields(it);
380
381 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700382 while (it.HasNextDirectMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800383 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800384 DumpOatMethod(os, class_method_index, oat_method, dex_file,
385 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800386 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700387 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700388 }
Ian Rogers0571d352011-11-03 19:51:38 -0700389 while (it.HasNextVirtualMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800390 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800391 DumpOatMethod(os, class_method_index, oat_method, dex_file,
392 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800393 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700394 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700395 }
Ian Rogers0571d352011-11-03 19:51:38 -0700396 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700397 os << std::flush;
398 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800399
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800400 void DumpOatMethod(std::ostream& os, uint32_t class_method_index,
Elliott Hughese3c845c2012-02-28 17:23:01 -0800401 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
402 uint32_t dex_method_idx, const DexFile::CodeItem* code_item) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700403 os << StringPrintf("\t%d: %s (dex_method_idx=%d)\n",
404 class_method_index, PrettyMethod(dex_method_idx, dex_file, true).c_str(),
405 dex_method_idx);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800406 os << StringPrintf("\t\tframe_size_in_bytes: %zd\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800407 oat_method.GetFrameSizeInBytes());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800408 os << StringPrintf("\t\tcore_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800409 oat_method.GetCoreSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800410 DumpSpillMask(os, oat_method.GetCoreSpillMask(), false);
411 os << StringPrintf("\n\t\tfp_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800412 oat_method.GetFpSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800413 DumpSpillMask(os, oat_method.GetFpSpillMask(), true);
414 os << StringPrintf("\n\t\tmapping_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800415 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800416 DumpMappingTable(os, oat_method);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800417 os << StringPrintf("\t\tvmap_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800418 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800419 DumpVmap(os, oat_method.GetVmapTable(), oat_method.GetCoreSpillMask(),
420 oat_method.GetFpSpillMask());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800421 os << StringPrintf("\t\tgc_map: %p (offset=0x%08x)\n",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800422 oat_method.GetGcMap(), oat_method.GetGcMapOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800423 DumpGcMap(os, oat_method.GetGcMap());
Logan Chien14924fe2012-04-03 18:33:37 +0800424 os << StringPrintf(
425#if defined(ART_USE_LLVM_COMPILER)
426 "\t\tCODE: %p (offset=0x%08x size=%d elf_idx=%d elf_func_idx=%d)%s\n",
427#else
428 "\t\tCODE: %p (offset=0x%08x size=%d)%s\n",
429#endif
Elliott Hughes77405792012-03-15 15:22:12 -0700430 oat_method.GetCode(),
431 oat_method.GetCodeOffset(),
432 oat_method.GetCodeSize(),
Logan Chien14924fe2012-04-03 18:33:37 +0800433#if defined(ART_USE_LLVM_COMPILER)
434 static_cast<int>(oat_method.GetCodeElfIndex()),
435 static_cast<int>(oat_method.GetCodeElfFuncIndex()),
436#endif
Elliott Hughes77405792012-03-15 15:22:12 -0700437 oat_method.GetCode() != NULL ? "..." : "");
Ian Rogersd3fb5692012-04-09 21:56:13 -0700438 DumpCode(os, oat_method.GetCode(), oat_method.GetCodeSize(), oat_method.GetMappingTable(),
439 dex_file, code_item);
Logan Chien14924fe2012-04-03 18:33:37 +0800440 os << StringPrintf(
441#if defined(ART_USE_LLVM_COMPILER)
442 "\t\tINVOKE STUB: %p (offset=0x%08x size=%d elf_idx=%d elf_func_idx=%d)%s\n",
443#else
444 "\t\tINVOKE STUB: %p (offset=0x%08x size=%d)%s\n",
445#endif
Elliott Hughes77405792012-03-15 15:22:12 -0700446 oat_method.GetInvokeStub(),
447 oat_method.GetInvokeStubOffset(),
448 oat_method.GetInvokeStubSize(),
Logan Chien14924fe2012-04-03 18:33:37 +0800449#if defined(ART_USE_LLVM_COMPILER)
450 static_cast<int>(oat_method.GetInvokeStubElfIndex()),
451 static_cast<int>(oat_method.GetInvokeStubElfFuncIndex()),
452#endif
Elliott Hughes77405792012-03-15 15:22:12 -0700453 oat_method.GetInvokeStub() != NULL ? "..." : "");
Ian Rogersd3fb5692012-04-09 21:56:13 -0700454 DumpCode(os, reinterpret_cast<const void*>(oat_method.GetInvokeStub()),
455 oat_method.GetInvokeStubSize(), NULL, dex_file, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700456 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800457
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800458 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
459 if (spill_mask == 0) {
460 return;
461 }
462 os << " (";
463 for (size_t i = 0; i < 32; i++) {
464 if ((spill_mask & (1 << i)) != 0) {
465 if (is_float) {
466 os << "fr" << i;
467 } else {
468 os << "r" << i;
469 }
470 spill_mask ^= 1 << i; // clear bit
471 if (spill_mask != 0) {
472 os << ", ";
473 } else {
474 break;
475 }
476 }
477 }
478 os << ")";
479 }
480
481 void DumpVmap(std::ostream& os, const uint16_t* raw_table, uint32_t core_spill_mask,
482 uint32_t fp_spill_mask) {
483 if (raw_table == NULL) {
484 return;
485 }
486 const VmapTable vmap_table(raw_table);
487 bool first = true;
488 os << "\t\t\t";
489 for (size_t i = 0; i < vmap_table.size(); i++) {
490 uint16_t dex_reg = vmap_table[i];
491 size_t matches = 0;
492 size_t spill_shifts = 0;
493 uint32_t spill_mask = core_spill_mask;
494 bool processing_fp = false;
495 while (matches != (i + 1)) {
496 if (spill_mask == 0) {
497 CHECK(!processing_fp);
498 spill_mask = fp_spill_mask;
499 processing_fp = true;
500 }
501 matches += spill_mask & 1; // Add 1 if the low bit is set
502 spill_mask >>= 1;
503 spill_shifts++;
504 }
505 size_t arm_reg = spill_shifts - 1; // wind back one as we want the last match
506 os << (first ? "v" : ", v") << dex_reg;
507 if (arm_reg < 16) {
508 os << "/r" << arm_reg;
509 } else {
510 os << "/fr" << (arm_reg - 16);
511 }
512 if (first) {
513 first = false;
514 }
515 }
516 os << std::endl;
517 }
518
519 void DumpGcMap(std::ostream& os, const uint8_t* gc_map_raw) {
520 if (gc_map_raw == NULL) {
521 return;
522 }
523 uint32_t gc_map_length = (gc_map_raw[0] << 24) | (gc_map_raw[1] << 16) |
524 (gc_map_raw[2] << 8) | (gc_map_raw[3] << 0);
525 verifier::PcToReferenceMap map(gc_map_raw + sizeof(uint32_t), gc_map_length);
526 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
527 os << StringPrintf("\t\t\t0x%04x", map.GetPC(entry));
528 size_t num_regs = map.RegWidth() * 8;
529 const uint8_t* reg_bitmap = map.GetBitMap(entry);
530 bool first = true;
531 for (size_t reg = 0; reg < num_regs; reg++) {
532 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
533 if (first) {
534 os << " v" << reg;
535 first = false;
536 } else {
537 os << ", v" << reg;
538 }
539 }
540 }
541 os << std::endl;
542 }
543 }
544
545 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800546 const uint32_t* raw_table = oat_method.GetMappingTable();
547 const void* code = oat_method.GetCode();
548 if (raw_table == NULL || code == NULL) {
549 return;
550 }
551
552 uint32_t length = *raw_table;
553 ++raw_table;
554
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800555 os << "\t\t{";
Elliott Hughese3c845c2012-02-28 17:23:01 -0800556 for (size_t i = 0; i < length; i += 2) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800557 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800558 uint32_t dex_pc = raw_table[i + 1];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800559 os << StringPrintf("%p -> 0x%04x", native_pc, dex_pc);
560 if (i + 2 < length) {
561 os << ", ";
562 }
563 }
564 os << "}" << std::endl << std::flush;
565 }
566
TDYa1273db52852012-04-01 15:11:43 -0700567#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogersd3fb5692012-04-09 21:56:13 -0700568 void DumpCode(std::ostream& os, const void* code, int code_size,
569 const uint32_t* raw_mapping_table,
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800570 const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Ian Rogersd3fb5692012-04-09 21:56:13 -0700571 if (code == NULL || code_size == 0) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800572 return;
573 }
574
Ian Rogersd3fb5692012-04-09 21:56:13 -0700575 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code);
576 const uint8_t* end_native_pc = native_pc + code_size;
577
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800578 if (raw_mapping_table == NULL) {
579 // code but no mapping table is most likely caused by code created by the JNI compiler
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800580 disassembler_->Dump(os, native_pc, end_native_pc);
581 return;
582 }
583
584 uint32_t length = *raw_mapping_table;
585 ++raw_mapping_table;
586
587 for (size_t i = 0; i < length; i += 2) {
588 uint32_t dex_pc = raw_mapping_table[i + 1];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800589 const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]);
590 os << StringPrintf("\t\t0x%04x: %s\n", dex_pc, instruction->DumpString(&dex_file).c_str());
591
Ian Rogersd3fb5692012-04-09 21:56:13 -0700592 const uint8_t* cur_pc = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i];
593 const uint8_t* cur_pc_end = NULL;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800594 if (i + 2 < length) {
Ian Rogersd3fb5692012-04-09 21:56:13 -0700595 cur_pc_end = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i + 2];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800596 } else {
Ian Rogersd3fb5692012-04-09 21:56:13 -0700597 cur_pc_end = end_native_pc;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800598 }
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700599 CHECK(cur_pc < cur_pc_end);
600 disassembler_->Dump(os, cur_pc, cur_pc_end);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800601 }
602 }
TDYa1273db52852012-04-01 15:11:43 -0700603#else
Logan Chiende85cc12012-04-10 19:55:34 +0800604 void DumpCode(std::ostream&, const void*, int, const uint32_t*,
605 const DexFile&, const DexFile::CodeItem*) {
TDYa1273db52852012-04-01 15:11:43 -0700606 // TODO: Dump code for the LLVM side.
607 }
608#endif
Elliott Hughese3c845c2012-02-28 17:23:01 -0800609
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700610 const std::string host_prefix_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800611 const OatFile& oat_file_;
612 std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800613 std::set<uint32_t> offsets_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800614 UniquePtr<Disassembler> disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700615};
616
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800617class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700618 public:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800619 explicit ImageDumper(std::ostream& os, const std::string& image_filename,
620 const std::string& host_prefix, Space& image_space,
621 const ImageHeader& image_header) : os_(os),
622 image_filename_(image_filename), host_prefix_(host_prefix),
623 image_space_(image_space), image_header_(image_header) {
624 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700625
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800626 void Dump() {
627 os_ << "MAGIC:\n";
628 os_ << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700629
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800630 os_ << "IMAGE BEGIN:\n";
631 os_ << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700632
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800633 os_ << "OAT CHECKSUM:\n";
634 os_ << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700635
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800636 os_ << "OAT BEGIN:\n";
637 os_ << reinterpret_cast<void*>(image_header_.GetOatBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700638
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800639 os_ << "OAT END:\n";
640 os_ << reinterpret_cast<void*>(image_header_.GetOatEnd()) << "\n\n";
641
642 os_ << "ROOTS:\n";
643 os_ << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700644 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700645 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
646 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700647 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800648 Object* image_root_object = image_header_.GetImageRoot(image_root);
649 os_ << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700650 if (image_root_object->IsObjectArray()) {
651 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
652 ObjectArray<Object>* image_root_object_array
653 = down_cast<ObjectArray<Object>*>(image_root_object);
654 // = image_root_object->AsObjectArray<Object>();
655 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800656 Object* value = image_root_object_array->Get(i);
657 if (value != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800658 os_ << "\t" << i << ": ";
Ian Rogersd5b32602012-02-26 16:40:04 -0800659 std::string summary;
660 PrettyObjectValue(summary, value->GetClass(), value);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800661 os_ << summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800662 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800663 os_ << StringPrintf("\t%d: null\n", i);
Ian Rogersd5b32602012-02-26 16:40:04 -0800664 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700665 }
666 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700667 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800668 os_ << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700669
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800670 os_ << "OAT LOCATION:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700671 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800672 Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation);
673 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800674 os_ << oat_location;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800675 if (!host_prefix_.empty()) {
676 oat_location = host_prefix_ + oat_location;
677 os_ << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700678 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800679 os_ << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800680 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700681 if (oat_file == NULL) {
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800682 os_ << "NOT FOUND\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700683 return;
684 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800685 os_ << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700686
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800687 stats_.oat_file_bytes = oat_file->Size();
688
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700689 oat_dumper_.reset(new OatDumper(host_prefix_, *oat_file));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800690
691 os_ << "OBJECTS:\n" << std::flush;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800692 HeapBitmap* heap_bitmap = Runtime::Current()->GetHeap()->GetLiveBits();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800693 DCHECK(heap_bitmap != NULL);
694 heap_bitmap->Walk(ImageDumper::Callback, this);
695 os_ << "\n";
696
697 os_ << "STATS:\n" << std::flush;
698 UniquePtr<File> file(OS::OpenFile(image_filename_.c_str(), false));
699 stats_.file_bytes = file->Length();
700 size_t header_bytes = sizeof(ImageHeader);
701 stats_.header_bytes = header_bytes;
702 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
703 stats_.alignment_bytes += alignment_bytes;
704 stats_.Dump(os_);
705 os_ << "\n";
706
707 os_ << std::flush;
708
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800709 oat_dumper_->Dump(os_);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700710 }
711
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700712 private:
713
Ian Rogersd5b32602012-02-26 16:40:04 -0800714 static void PrettyObjectValue(std::string& summary, Class* type, Object* value) {
715 CHECK(type != NULL);
716 if (value == NULL) {
717 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
718 } else if (type->IsStringClass()) {
719 String* string = value->AsString();
720 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
721 } else if (value->IsClass()) {
722 Class* klass = value->AsClass();
723 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
724 } else if (value->IsField()) {
725 Field* field = value->AsField();
726 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
727 } else if (value->IsMethod()) {
728 Method* method = value->AsMethod();
729 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
730 } else {
731 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
732 }
733 }
734
735 static void PrintField(std::string& summary, Field* field, Object* obj) {
736 FieldHelper fh(field);
737 Class* type = fh.GetType();
738 StringAppendF(&summary, "\t%s: ", fh.GetName());
739 if (type->IsPrimitiveLong()) {
740 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
741 } else if (type->IsPrimitiveDouble()) {
742 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
743 } else if (type->IsPrimitiveFloat()) {
744 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700745 } else if (type->IsPrimitive()) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800746 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
747 } else {
748 Object* value = field->GetObj(obj);
749 PrettyObjectValue(summary, type, value);
750 }
751 }
752
753 static void DumpFields(std::string& summary, Object* obj, Class* klass) {
754 Class* super = klass->GetSuperClass();
755 if (super != NULL) {
756 DumpFields(summary, obj, super);
757 }
758 ObjectArray<Field>* fields = klass->GetIFields();
759 if (fields != NULL) {
760 for (int32_t i = 0; i < fields->GetLength(); i++) {
761 Field* field = fields->Get(i);
762 PrintField(summary, field, obj);
763 }
764 }
765 }
766
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800767 bool InDumpSpace(const Object* object) {
768 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700769 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800770
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700771 const void* GetOatCodeBegin(Method* m) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800772 Runtime* runtime = Runtime::Current();
773 const void* code = m->GetCode();
Ian Rogersfb6adba2012-03-04 21:51:51 -0800774 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800775 code = oat_dumper_->GetOatCode(m);
776 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700777 if (oat_dumper_->GetInstructionSet() == kThumb2) {
778 code = reinterpret_cast<void*>(reinterpret_cast<uint32_t>(code) & ~0x1);
779 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800780 return code;
781 }
782
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700783 uint32_t GetOatCodeSize(Method* m) {
784 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetOatCodeBegin(m));
785 if (oat_code_begin == NULL) {
786 return 0;
787 }
788 return oat_code_begin[-1];
789 }
790
791 const void* GetOatCodeEnd(Method* m) {
792 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetOatCodeBegin(m));
793 if (oat_code_begin == NULL) {
794 return NULL;
795 }
796 return oat_code_begin + GetOatCodeSize(m);
797 }
798
Brian Carlstrom78128a62011-09-15 17:21:19 -0700799 static void Callback(Object* obj, void* arg) {
800 DCHECK(obj != NULL);
801 DCHECK(arg != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800802 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700803 if (!state->InDumpSpace(obj)) {
804 return;
805 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700806
807 size_t object_bytes = obj->SizeOf();
808 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
809 state->stats_.object_bytes += object_bytes;
810 state->stats_.alignment_bytes += alignment_bytes;
811
Brian Carlstrom78128a62011-09-15 17:21:19 -0700812 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800813 Class* obj_class = obj->GetClass();
814 if (obj_class->IsArrayClass()) {
815 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
816 obj->AsArray()->GetLength());
817 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700818 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800819 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
820 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700821 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800822 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700823 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800824 } else if (obj->IsField()) {
825 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
826 PrettyField(obj->AsField()).c_str());
827 } else if (obj->IsMethod()) {
828 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
829 PrettyMethod(obj->AsMethod()).c_str());
830 } else if (obj_class->IsStringClass()) {
Elliott Hughes82914b62012-04-09 15:56:29 -0700831 StringAppendF(&summary, "%p: java.lang.String %s\n", obj,
832 PrintableString(obj->AsString()->ToModifiedUtf8()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800833 } else {
834 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
835 }
836 DumpFields(summary, obj, obj_class);
837 if (obj->IsObjectArray()) {
838 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
839 int32_t length = obj_array->GetLength();
840 for (int32_t i = 0; i < length; i++) {
841 Object* value = obj_array->Get(i);
842 size_t run = 0;
843 for (int32_t j = i + 1; j < length; j++) {
844 if (value == obj_array->Get(j)) {
845 run++;
846 } else {
847 break;
848 }
849 }
850 if (run == 0) {
851 StringAppendF(&summary, "\t%d: ", i);
852 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800853 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800854 i = i + run;
855 }
856 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
857 PrettyObjectValue(summary, value_class, value);
858 }
859 } else if (obj->IsClass()) {
860 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
861 if (sfields != NULL) {
862 summary += "\t\tSTATICS:\n";
863 for (int32_t i = 0; i < sfields->GetLength(); i++) {
864 Field* field = sfields->Get(i);
865 PrintField(summary, field, NULL);
866 }
867 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700868 } else if (obj->IsMethod()) {
869 Method* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700870 if (method->IsNative()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700871 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800872 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700873 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800874 bool first_occurrence;
875 size_t invoke_stub_size = state->ComputeOatSize(
876 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
877 if (first_occurrence) {
878 state->stats_.managed_to_native_code_bytes += invoke_stub_size;
879 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700880 const void* oat_code = state->GetOatCodeBegin(method);
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700881 uint32_t oat_code_size = state->GetOatCodeSize(method);
882 state->ComputeOatSize(oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800883 if (first_occurrence) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700884 state->stats_.native_to_managed_code_bytes += oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800885 }
886 if (oat_code != method->GetCode()) {
887 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
888 }
Ian Rogers19846512012-02-24 11:42:47 -0800889 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
890 method->IsResolutionMethod()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700891 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800892 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700893 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700894 } else {
TDYa1273db52852012-04-01 15:11:43 -0700895#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800896 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
897 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
TDYa1273db52852012-04-01 15:11:43 -0700898#endif
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700899
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800900 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700901 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700902 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800903
Elliott Hughesa0e18062012-04-13 15:59:59 -0700904 bool first_occurrence;
905 size_t gc_map_bytes = state->ComputeOatSize(method->GetGcMapRaw(), &first_occurrence);
906 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800907 state->stats_.gc_map_bytes += gc_map_bytes;
908 }
909
910 size_t pc_mapping_table_bytes =
Elliott Hughesa0e18062012-04-13 15:59:59 -0700911 state->ComputeOatSize(method->GetMappingTableRaw(), &first_occurrence);
912 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800913 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
914 }
915
916 size_t vmap_table_bytes =
Elliott Hughesa0e18062012-04-13 15:59:59 -0700917 state->ComputeOatSize(method->GetVmapTableRaw(), &first_occurrence);
918 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800919 state->stats_.vmap_table_bytes += vmap_table_bytes;
920 }
921
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700922 // TODO: compute invoke stub using length from oat file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800923 size_t invoke_stub_size = state->ComputeOatSize(
Elliott Hughesa0e18062012-04-13 15:59:59 -0700924 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
925 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800926 state->stats_.native_to_managed_code_bytes += invoke_stub_size;
927 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700928 const void* oat_code_begin = state->GetOatCodeBegin(method);
929 const void* oat_code_end = state->GetOatCodeEnd(method);
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700930 uint32_t oat_code_size = state->GetOatCodeSize(method);
Elliott Hughesa0e18062012-04-13 15:59:59 -0700931 state->ComputeOatSize(oat_code_begin, &first_occurrence);
932 if (first_occurrence) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700933 state->stats_.managed_code_bytes += oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -0700934 if (method->IsConstructor()) {
935 if (method->IsStatic()) {
936 state->stats_.class_initializer_code_bytes += oat_code_size;
937 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
938 state->stats_.large_initializer_code_bytes += oat_code_size;
939 }
940 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
941 state->stats_.large_method_code_bytes += oat_code_size;
942 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800943 }
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700944 state->stats_.managed_code_bytes_ignoring_deduplication += oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800945
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700946 StringAppendF(&summary, "\t\tOAT CODE: %p-%p\n", oat_code_begin, oat_code_end);
Ian Rogersd5b32602012-02-26 16:40:04 -0800947 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800948 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
949
950 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700951 vmap_table_bytes + invoke_stub_size + oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800952
953 double expansion =
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700954 static_cast<double>(oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800955 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700956 }
957 }
Elliott Hughesa0e18062012-04-13 15:59:59 -0700958 state->stats_.Update(ClassHelper(obj_class).GetDescriptor(), object_bytes);
Ian Rogersd5b32602012-02-26 16:40:04 -0800959
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700960 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700961 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700962
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800963 std::set<const void*> already_seen_;
964 // Compute the size of the given data within the oat file and whether this is the first time
965 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -0700966 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800967 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700968 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800969 already_seen_.insert(oat_data);
970 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700971 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800972 }
973 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700974 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700975
976 public:
977 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800978 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700979 size_t file_bytes;
980
981 size_t header_bytes;
982 size_t object_bytes;
983 size_t alignment_bytes;
984
985 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800986 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700987 size_t managed_to_native_code_bytes;
988 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -0700989 size_t class_initializer_code_bytes;
990 size_t large_initializer_code_bytes;
991 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700992
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800993 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700994 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800995 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700996
997 size_t dex_instruction_bytes;
998
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800999 std::vector<Method*> method_outlier;
1000 std::vector<size_t> method_outlier_size;
1001 std::vector<double> method_outlier_expansion;
1002
1003 explicit Stats()
1004 : oat_file_bytes(0),
1005 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001006 header_bytes(0),
1007 object_bytes(0),
1008 alignment_bytes(0),
1009 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001010 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001011 managed_to_native_code_bytes(0),
1012 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001013 class_initializer_code_bytes(0),
1014 large_initializer_code_bytes(0),
1015 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001016 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001017 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001018 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001019 dex_instruction_bytes(0) {}
1020
Elliott Hughesa0e18062012-04-13 15:59:59 -07001021 struct SizeAndCount {
1022 SizeAndCount(size_t bytes, size_t count) : bytes(bytes), count(count) {}
1023 size_t bytes;
1024 size_t count;
1025 };
1026 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1027 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001028
Elliott Hughesa0e18062012-04-13 15:59:59 -07001029 void Update(const std::string& descriptor, size_t object_bytes) {
1030 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1031 if (it != sizes_and_counts.end()) {
1032 it->second.bytes += object_bytes;
1033 it->second.count += 1;
1034 } else {
1035 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes, 1));
1036 }
1037 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001038
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001039 double PercentOfOatBytes(size_t size) {
1040 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1041 }
1042
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001043 double PercentOfFileBytes(size_t size) {
1044 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1045 }
1046
1047 double PercentOfObjectBytes(size_t size) {
1048 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1049 }
1050
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001051 void ComputeOutliers(size_t total_size, double expansion, Method* method) {
1052 method_outlier_size.push_back(total_size);
1053 method_outlier_expansion.push_back(expansion);
1054 method_outlier.push_back(method);
1055 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001056
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001057 void DumpOutliers(std::ostream& os) {
1058 size_t sum_of_sizes = 0;
1059 size_t sum_of_sizes_squared = 0;
1060 size_t sum_of_expansion = 0;
1061 size_t sum_of_expansion_squared = 0;
1062 size_t n = method_outlier_size.size();
1063 for (size_t i = 0; i < n; i++) {
1064 size_t cur_size = method_outlier_size[i];
1065 sum_of_sizes += cur_size;
1066 sum_of_sizes_squared += cur_size * cur_size;
1067 double cur_expansion = method_outlier_expansion[i];
1068 sum_of_expansion += cur_expansion;
1069 sum_of_expansion_squared += cur_expansion * cur_expansion;
1070 }
1071 size_t size_mean = sum_of_sizes / n;
1072 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1073 double expansion_mean = sum_of_expansion / n;
1074 double expansion_variance =
1075 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1076
1077 // Dump methods whose size is a certain number of standard deviations from the mean
1078 size_t dumped_values = 0;
1079 size_t skipped_values = 0;
1080 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1081 size_t cur_size_variance = i * i * size_variance;
1082 bool first = true;
1083 for (size_t j = 0; j < n; j++) {
1084 size_t cur_size = method_outlier_size[j];
1085 if (cur_size > size_mean) {
1086 size_t cur_var = cur_size - size_mean;
1087 cur_var = cur_var * cur_var;
1088 if (cur_var > cur_size_variance) {
1089 if (dumped_values > 20) {
1090 if (i == 1) {
1091 skipped_values++;
1092 } else {
1093 i = 2; // jump to counting for 1 standard deviation
1094 break;
1095 }
1096 } else {
1097 if (first) {
1098 os << "\nBig methods (size > " << i << " standard deviations the norm):"
1099 << std::endl;
1100 first = false;
1101 }
1102 os << "\t" << PrettyMethod(method_outlier[j]) << " requires storage of "
1103 << PrettySize(cur_size) << std::endl;
1104 method_outlier_size[j] = 0; // don't consider this method again
1105 dumped_values++;
1106 }
1107 }
1108 }
1109 }
1110 }
1111 if (skipped_values > 0) {
1112 os << "\t... skipped " << skipped_values
1113 << " methods with size > 1 standard deviation from the norm" << std::endl;
1114 }
1115 os << std::endl << std::flush;
1116
1117 // Dump methods whose expansion is a certain number of standard deviations from the mean
1118 dumped_values = 0;
1119 skipped_values = 0;
1120 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1121 double cur_expansion_variance = i * i * expansion_variance;
1122 bool first = true;
1123 for (size_t j = 0; j < n; j++) {
1124 double cur_expansion = method_outlier_expansion[j];
1125 if (cur_expansion > expansion_mean) {
1126 size_t cur_var = cur_expansion - expansion_mean;
1127 cur_var = cur_var * cur_var;
1128 if (cur_var > cur_expansion_variance) {
1129 if (dumped_values > 20) {
1130 if (i == 1) {
1131 skipped_values++;
1132 } else {
1133 i = 2; // jump to counting for 1 standard deviation
1134 break;
1135 }
1136 } else {
1137 if (first) {
1138 os << "\nLarge expansion methods (size > " << i
1139 << " standard deviations the norm):" << std::endl;
1140 first = false;
1141 }
1142 os << "\t" << PrettyMethod(method_outlier[j]) << " expanded code by "
1143 << cur_expansion << std::endl;
1144 method_outlier_expansion[j] = 0.0; // don't consider this method again
1145 dumped_values++;
1146 }
1147 }
1148 }
1149 }
1150 }
1151 if (skipped_values > 0) {
1152 os << "\t... skipped " << skipped_values
1153 << " methods with expansion > 1 standard deviation from the norm" << std::endl;
1154 }
1155 os << std::endl << std::flush;
1156 }
1157
1158 void Dump(std::ostream& os) {
1159 os << "\tart_file_bytes = " << PrettySize(file_bytes) << std::endl << std::endl
1160 << "\tart_file_bytes = header_bytes + object_bytes + alignment_bytes" << std::endl
1161 << StringPrintf("\theader_bytes = %8zd (%2.0f%% of art file bytes)\n"
1162 "\tobject_bytes = %8zd (%2.0f%% of art file bytes)\n"
1163 "\talignment_bytes = %8zd (%2.0f%% of art file bytes)\n",
1164 header_bytes, PercentOfFileBytes(header_bytes),
1165 object_bytes, PercentOfFileBytes(object_bytes),
1166 alignment_bytes, PercentOfFileBytes(alignment_bytes))
1167 << std::endl << std::flush;
1168
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001169 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
1170
Elliott Hughesa0e18062012-04-13 15:59:59 -07001171 os << "\tobject_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001172 size_t object_bytes_total = 0;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001173 typedef SizeAndCountTable::const_iterator It; // TODO: C++0x auto
1174 for (It it = sizes_and_counts.begin(), end = sizes_and_counts.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -08001175 const std::string& descriptor(it->first);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001176 double average = static_cast<double>(it->second.bytes) / static_cast<double>(it->second.count);
1177 double percent = PercentOfObjectBytes(it->second.bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001178 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001179 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
1180 descriptor.c_str(), it->second.bytes, it->second.count,
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001181 average, percent);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001182 object_bytes_total += it->second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001183 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001184 os << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001185 CHECK_EQ(object_bytes, object_bytes_total);
1186
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001187 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1188 "\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers0d2d3782012-04-10 11:09:18 -07001189 "\tnative_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1190 "\tclass_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1191 "\tlarge_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1192 "\tlarge_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001193 managed_code_bytes, PercentOfOatBytes(managed_code_bytes),
1194 managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001195 native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes),
1196 class_initializer_code_bytes, PercentOfOatBytes(class_initializer_code_bytes),
1197 large_initializer_code_bytes, PercentOfOatBytes(large_initializer_code_bytes),
1198 large_method_code_bytes, PercentOfOatBytes(large_method_code_bytes))
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001199 << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001200
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001201 os << StringPrintf("\tgc_map_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1202 "\tpc_mapping_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1203 "\tvmap_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n",
1204 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1205 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1206 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
1207 << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001208
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001209 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001210 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n",
1211 static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes),
1212 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
1213 static_cast<double>(dex_instruction_bytes));
1214 os << std::endl << std::flush;
1215
1216 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001217 }
1218 } stats_;
1219
1220 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001221 enum {
1222 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1223 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1224 kLargeConstructorDexBytes = 4000,
1225 // Number of bytes for a method to be considered large. Based on the 4000 basic block
1226 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1227 kLargeMethodDexBytes = 16000
1228 };
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001229 UniquePtr<OatDumper> oat_dumper_;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001230 std::ostream& os_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001231 const std::string image_filename_;
1232 const std::string host_prefix_;
1233 Space& image_space_;
1234 const ImageHeader& image_header_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001235
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001236 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001237};
1238
1239int oatdump(int argc, char** argv) {
1240 // Skip over argv[0].
1241 argv++;
1242 argc--;
1243
1244 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001245 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001246 usage();
1247 }
1248
Brian Carlstromaded5f72011-10-07 17:15:04 -07001249 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001250 const char* image_filename = NULL;
1251 const char* boot_image_filename = NULL;
Logan Chien0cc6ab62012-03-20 22:57:52 +08001252 std::string elf_filename_prefix;
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001253 UniquePtr<std::string> host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001254 std::ostream* os = &std::cout;
1255 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001256
1257 for (int i = 0; i < argc; i++) {
1258 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001259 if (option.starts_with("--oat-file=")) {
1260 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001261 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001262 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001263 } else if (option.starts_with("--boot-image=")) {
1264 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Logan Chien0cc6ab62012-03-20 22:57:52 +08001265 } else if (option.starts_with("--extract-elf-to=")) {
1266 elf_filename_prefix = option.substr(strlen("--extract-elf-to=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001267 } else if (option.starts_with("--host-prefix=")) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001268 host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001269 } else if (option.starts_with("--output=")) {
1270 const char* filename = option.substr(strlen("--output=")).data();
1271 out.reset(new std::ofstream(filename));
1272 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001273 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001274 usage();
1275 }
1276 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001277 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001278 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001279 usage();
1280 }
1281 }
1282
Brian Carlstromaded5f72011-10-07 17:15:04 -07001283 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001284 fprintf(stderr, "Either --image or --oat must be specified\n");
1285 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001286 }
1287
Brian Carlstromaded5f72011-10-07 17:15:04 -07001288 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001289 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1290 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001291 }
1292
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001293 if (host_prefix.get() == NULL) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001294 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
1295 if (android_product_out != NULL) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001296 host_prefix.reset(new std::string(android_product_out));
1297 } else {
1298 host_prefix.reset(new std::string(""));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001299 }
1300 }
1301
Brian Carlstromaded5f72011-10-07 17:15:04 -07001302 if (oat_filename != NULL) {
Logan Chien0c717dd2012-03-28 18:31:07 +08001303 OatFile* oat_file =
1304 OatFile::Open(oat_filename, oat_filename, NULL, OatFile::kRelocNone);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001305 if (oat_file == NULL) {
1306 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
1307 return EXIT_FAILURE;
1308 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001309 OatDumper oat_dumper(*host_prefix.get(), *oat_file);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001310 oat_dumper.Dump(*os);
Logan Chien0cc6ab62012-03-20 22:57:52 +08001311
Logan Chien0c717dd2012-03-28 18:31:07 +08001312#if defined(ART_USE_LLVM_COMPILER)
Logan Chien0cc6ab62012-03-20 22:57:52 +08001313 if (!elf_filename_prefix.empty()) {
1314 uint32_t elf_image_count = oat_file->GetOatHeader().GetElfImageCount();
1315 for (uint32_t i = 0; i < elf_image_count; ++i) {
1316 const OatFile::OatElfImage* elf_image = oat_file->GetOatElfImage(i);
1317
1318 std::string elf_filename(
1319 StringPrintf("%s-%u", elf_filename_prefix.c_str(), i));
1320
1321 UniquePtr<File> elf_file(OS::OpenFile(elf_filename.c_str(), true));
1322
1323 if (!elf_file->WriteFully(elf_image->begin(), elf_image->size())) {
1324 fprintf(stderr, "Failed to write ELF image to: %s\n",
1325 elf_filename.c_str());
1326 }
1327 }
1328 }
Logan Chien0c717dd2012-03-28 18:31:07 +08001329#endif
Brian Carlstromaded5f72011-10-07 17:15:04 -07001330 return EXIT_SUCCESS;
1331 }
1332
Brian Carlstrom78128a62011-09-15 17:21:19 -07001333 Runtime::Options options;
1334 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001335 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001336 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001337 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001338 if (boot_image_filename != NULL) {
1339 boot_image_option += "-Ximage:";
1340 boot_image_option += boot_image_filename;
1341 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001342 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001343 if (image_filename != NULL) {
1344 image_option += "-Ximage:";
1345 image_option += image_filename;
1346 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1347 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001348
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001349 if (!host_prefix->empty()) {
1350 options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001351 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001352
1353 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
1354 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001355 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001356 return EXIT_FAILURE;
1357 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001358
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001359 Heap* heap = Runtime::Current()->GetHeap();
Brian Carlstromfddf6f62012-03-15 16:56:45 -07001360 ImageSpace* image_space = heap->GetImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001361 CHECK(image_space != NULL);
1362 const ImageHeader& image_header = image_space->GetImageHeader();
1363 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001364 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001365 return EXIT_FAILURE;
1366 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001367 ImageDumper image_dumper(*os, image_filename, *host_prefix.get(), *image_space, image_header);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001368 image_dumper.Dump();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001369 return EXIT_SUCCESS;
1370}
1371
1372} // namespace art
1373
1374int main(int argc, char** argv) {
1375 return art::oatdump(argc, argv);
1376}