blob: 01b83394d971215848f65c132326701082ec9f7a [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>
Elliott Hughese5448b52012-01-18 16:44:06 -080022#include <map>
Brian Carlstrom78128a62011-09-15 17:21:19 -070023#include <string>
24#include <vector>
25
26#include "class_linker.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080027#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080028#include "dex_verifier.h"
29#include "disassembler.h"
Brian Carlstrom916e74e2011-09-23 11:42:01 -070030#include "file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070031#include "image.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070032#include "oat/runtime/context.h" // For VmapTable
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080033#include "object_utils.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080034#include "os.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070035#include "runtime.h"
36#include "space.h"
37#include "stringpiece.h"
38
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);
208 DUMP_ENTRY_POINT(pLadd);
209 DUMP_ENTRY_POINT(pLand);
210 DUMP_ENTRY_POINT(pLdivmod);
211 DUMP_ENTRY_POINT(pLmul);
212 DUMP_ENTRY_POINT(pLor);
213 DUMP_ENTRY_POINT(pLsub);
214 DUMP_ENTRY_POINT(pLxor);
215 DUMP_ENTRY_POINT(pShlLong);
216 DUMP_ENTRY_POINT(pShrLong);
217 DUMP_ENTRY_POINT(pUshrLong);
218 DUMP_ENTRY_POINT(pIndexOf);
219 DUMP_ENTRY_POINT(pMemcmp16);
220 DUMP_ENTRY_POINT(pStringCompareTo);
221 DUMP_ENTRY_POINT(pMemcpy);
222 DUMP_ENTRY_POINT(pFindInterfaceMethodInCache);
223 DUMP_ENTRY_POINT(pUnresolvedDirectMethodTrampolineFromCode);
224 DUMP_ENTRY_POINT(pInvokeDirectTrampolineWithAccessCheck);
225 DUMP_ENTRY_POINT(pInvokeInterfaceTrampoline);
226 DUMP_ENTRY_POINT(pInvokeInterfaceTrampolineWithAccessCheck);
227 DUMP_ENTRY_POINT(pInvokeStaticTrampolineWithAccessCheck);
228 DUMP_ENTRY_POINT(pInvokeSuperTrampolineWithAccessCheck);
229 DUMP_ENTRY_POINT(pInvokeVirtualTrampolineWithAccessCheck);
230 DUMP_ENTRY_POINT(pCheckSuspendFromCode);
231 DUMP_ENTRY_POINT(pTestSuspendFromCode);
232 DUMP_ENTRY_POINT(pDeliverException);
233 DUMP_ENTRY_POINT(pThrowAbstractMethodErrorFromCode);
234 DUMP_ENTRY_POINT(pThrowArrayBoundsFromCode);
235 DUMP_ENTRY_POINT(pThrowDivZeroFromCode);
236 DUMP_ENTRY_POINT(pThrowNoSuchMethodFromCode);
237 DUMP_ENTRY_POINT(pThrowNullPointerFromCode);
238 DUMP_ENTRY_POINT(pThrowStackOverflowFromCode);
239 DUMP_ENTRY_POINT(pThrowVerificationErrorFromCode);
240
241#undef DUMP_ENTRY_POINT
242
243 CHECK_EQ(start_offset + sizeof(EntryPoints), last_offset + sizeof(uintptr_t));
244 os << "\n";
245
Brian Carlstromaded5f72011-10-07 17:15:04 -0700246 os << std::flush;
247
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800248 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
249 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Brian Carlstromaded5f72011-10-07 17:15:04 -0700250 CHECK(oat_dex_file != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800251 DumpOatDexFile(os, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700252 }
253 }
254
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800255 size_t ComputeSize(const void* oat_data) {
256 if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() ||
257 reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) {
258 return 0; // Address not in oat file
259 }
260 uint32_t begin_offset = reinterpret_cast<size_t>(oat_data) -
261 reinterpret_cast<size_t>(oat_file_.Begin());
262 typedef std::set<uint32_t>::iterator It;
263 It it = offsets_.upper_bound(begin_offset);
264 CHECK(it != offsets_.end());
265 uint32_t end_offset = *it;
266 return end_offset - begin_offset;
267 }
268
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700269 InstructionSet GetInstructionSet() {
270 return oat_file_.GetOatHeader().GetInstructionSet();
271 }
272
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800273 const void* GetOatCode(Method* m) {
274 MethodHelper mh(m);
275 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
276 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
277 CHECK(oat_dex_file != NULL);
278 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
279 if (dex_file.get() != NULL) {
280 uint32_t class_def_index;
281 bool found = dex_file->FindClassDefIndex(mh.GetDeclaringClassDescriptor(), class_def_index);
282 if (found) {
283 const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_index);
284 CHECK(oat_class != NULL);
285 size_t method_index = m->GetMethodIndex();
286 return oat_class->GetOatMethod(method_index).GetCode();
287 }
288 }
289 }
290 return NULL;
291 }
292
Brian Carlstromaded5f72011-10-07 17:15:04 -0700293 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800294 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800295 // We don't know the length of the code for each method, but we need to know where to stop
296 // when disassembling. What we do know is that a region of code will be followed by some other
297 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
298 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800299 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
300 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800301 CHECK(oat_dex_file != NULL);
302 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
303 if (dex_file.get() == NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800304 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800305 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800306 offsets_.insert(reinterpret_cast<uint32_t>(&dex_file->GetHeader()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800307 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
308 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
309 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index));
310 const byte* class_data = dex_file->GetClassData(class_def);
311 if (class_data != NULL) {
312 ClassDataItemIterator it(*dex_file, class_data);
313 SkipAllFields(it);
314 uint32_t class_method_index = 0;
315 while (it.HasNextDirectMethod()) {
316 AddOffsets(oat_class->GetOatMethod(class_method_index++));
317 it.Next();
318 }
319 while (it.HasNextVirtualMethod()) {
320 AddOffsets(oat_class->GetOatMethod(class_method_index++));
321 it.Next();
322 }
323 }
324 }
325 }
326
327 // If the last thing in the file is code for a method, there won't be an offset for the "next"
328 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
329 // for the end of the file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800330 offsets_.insert(static_cast<uint32_t>(oat_file_.End() - oat_file_.Begin()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800331 }
332
333 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800334 uint32_t code_offset = oat_method.GetCodeOffset();
335 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
336 code_offset &= ~0x1;
337 }
338 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800339 offsets_.insert(oat_method.GetMappingTableOffset());
340 offsets_.insert(oat_method.GetVmapTableOffset());
341 offsets_.insert(oat_method.GetGcMapOffset());
342 offsets_.insert(oat_method.GetInvokeStubOffset());
343 }
344
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800345 void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700346 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800347 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800348 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800349 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile());
350 if (dex_file.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700351 os << "NOT FOUND\n\n";
352 return;
353 }
354 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
355 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
356 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700357 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
358 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800359 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800360 << oat_class->GetStatus() << ")\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800361 DumpOatClass(os, *oat_class.get(), *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700362 }
363
364 os << std::flush;
365 }
366
Elliott Hughese3c845c2012-02-28 17:23:01 -0800367 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700368 while (it.HasNextStaticField()) {
369 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700370 }
Ian Rogers0571d352011-11-03 19:51:38 -0700371 while (it.HasNextInstanceField()) {
372 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700373 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800374 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700375
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800376 void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
377 const DexFile::ClassDef& class_def) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800378 const byte* class_data = dex_file.GetClassData(class_def);
379 if (class_data == NULL) { // empty class such as a marker interface?
380 return;
381 }
382 ClassDataItemIterator it(dex_file, class_data);
383 SkipAllFields(it);
384
385 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700386 while (it.HasNextDirectMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800387 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800388 DumpOatMethod(os, class_method_index, oat_method, dex_file,
389 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800390 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700391 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700392 }
Ian Rogers0571d352011-11-03 19:51:38 -0700393 while (it.HasNextVirtualMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800394 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800395 DumpOatMethod(os, class_method_index, oat_method, dex_file,
396 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800397 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700398 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700399 }
Ian Rogers0571d352011-11-03 19:51:38 -0700400 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700401 os << std::flush;
402 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800403
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800404 void DumpOatMethod(std::ostream& os, uint32_t class_method_index,
Elliott Hughese3c845c2012-02-28 17:23:01 -0800405 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
406 uint32_t dex_method_idx, const DexFile::CodeItem* code_item) {
407 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700408 const char* name = dex_file.GetMethodName(method_id);
Elliott Hughes95572412011-12-13 18:14:20 -0800409 std::string signature(dex_file.GetMethodSignature(method_id));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800410 os << StringPrintf("\t%d: %s %s (dex_method_idx=%d)\n",
411 class_method_index, name, signature.c_str(), dex_method_idx);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800412 os << StringPrintf("\t\tframe_size_in_bytes: %zd\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800413 oat_method.GetFrameSizeInBytes());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800414 os << StringPrintf("\t\tcore_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800415 oat_method.GetCoreSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800416 DumpSpillMask(os, oat_method.GetCoreSpillMask(), false);
417 os << StringPrintf("\n\t\tfp_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800418 oat_method.GetFpSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800419 DumpSpillMask(os, oat_method.GetFpSpillMask(), true);
420 os << StringPrintf("\n\t\tmapping_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800421 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800422 DumpMappingTable(os, oat_method);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800423 os << StringPrintf("\t\tvmap_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800424 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800425 DumpVmap(os, oat_method.GetVmapTable(), oat_method.GetCoreSpillMask(),
426 oat_method.GetFpSpillMask());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800427 os << StringPrintf("\t\tgc_map: %p (offset=0x%08x)\n",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800428 oat_method.GetGcMap(), oat_method.GetGcMapOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800429 DumpGcMap(os, oat_method.GetGcMap());
Elliott Hughes77405792012-03-15 15:22:12 -0700430 os << StringPrintf("\t\tCODE: %p (offset=0x%08x size=%d)%s\n",
431 oat_method.GetCode(),
432 oat_method.GetCodeOffset(),
433 oat_method.GetCodeSize(),
434 oat_method.GetCode() != NULL ? "..." : "");
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800435 DumpCode(os, oat_method.GetCode(), oat_method.GetMappingTable(), dex_file, code_item);
Elliott Hughes77405792012-03-15 15:22:12 -0700436 os << StringPrintf("\t\tINVOKE STUB: %p (offset=0x%08x size=%d)%s\n",
437 oat_method.GetInvokeStub(),
438 oat_method.GetInvokeStubOffset(),
439 oat_method.GetInvokeStubSize(),
440 oat_method.GetInvokeStub() != NULL ? "..." : "");
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800441 DumpCode(os, reinterpret_cast<const void*>(oat_method.GetInvokeStub()), NULL, dex_file, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700442 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800443
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800444 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
445 if (spill_mask == 0) {
446 return;
447 }
448 os << " (";
449 for (size_t i = 0; i < 32; i++) {
450 if ((spill_mask & (1 << i)) != 0) {
451 if (is_float) {
452 os << "fr" << i;
453 } else {
454 os << "r" << i;
455 }
456 spill_mask ^= 1 << i; // clear bit
457 if (spill_mask != 0) {
458 os << ", ";
459 } else {
460 break;
461 }
462 }
463 }
464 os << ")";
465 }
466
467 void DumpVmap(std::ostream& os, const uint16_t* raw_table, uint32_t core_spill_mask,
468 uint32_t fp_spill_mask) {
469 if (raw_table == NULL) {
470 return;
471 }
472 const VmapTable vmap_table(raw_table);
473 bool first = true;
474 os << "\t\t\t";
475 for (size_t i = 0; i < vmap_table.size(); i++) {
476 uint16_t dex_reg = vmap_table[i];
477 size_t matches = 0;
478 size_t spill_shifts = 0;
479 uint32_t spill_mask = core_spill_mask;
480 bool processing_fp = false;
481 while (matches != (i + 1)) {
482 if (spill_mask == 0) {
483 CHECK(!processing_fp);
484 spill_mask = fp_spill_mask;
485 processing_fp = true;
486 }
487 matches += spill_mask & 1; // Add 1 if the low bit is set
488 spill_mask >>= 1;
489 spill_shifts++;
490 }
491 size_t arm_reg = spill_shifts - 1; // wind back one as we want the last match
492 os << (first ? "v" : ", v") << dex_reg;
493 if (arm_reg < 16) {
494 os << "/r" << arm_reg;
495 } else {
496 os << "/fr" << (arm_reg - 16);
497 }
498 if (first) {
499 first = false;
500 }
501 }
502 os << std::endl;
503 }
504
505 void DumpGcMap(std::ostream& os, const uint8_t* gc_map_raw) {
506 if (gc_map_raw == NULL) {
507 return;
508 }
509 uint32_t gc_map_length = (gc_map_raw[0] << 24) | (gc_map_raw[1] << 16) |
510 (gc_map_raw[2] << 8) | (gc_map_raw[3] << 0);
511 verifier::PcToReferenceMap map(gc_map_raw + sizeof(uint32_t), gc_map_length);
512 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
513 os << StringPrintf("\t\t\t0x%04x", map.GetPC(entry));
514 size_t num_regs = map.RegWidth() * 8;
515 const uint8_t* reg_bitmap = map.GetBitMap(entry);
516 bool first = true;
517 for (size_t reg = 0; reg < num_regs; reg++) {
518 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
519 if (first) {
520 os << " v" << reg;
521 first = false;
522 } else {
523 os << ", v" << reg;
524 }
525 }
526 }
527 os << std::endl;
528 }
529 }
530
531 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800532 const uint32_t* raw_table = oat_method.GetMappingTable();
533 const void* code = oat_method.GetCode();
534 if (raw_table == NULL || code == NULL) {
535 return;
536 }
537
538 uint32_t length = *raw_table;
539 ++raw_table;
540
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800541 os << "\t\t{";
Elliott Hughese3c845c2012-02-28 17:23:01 -0800542 for (size_t i = 0; i < length; i += 2) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800543 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800544 uint32_t dex_pc = raw_table[i + 1];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800545 os << StringPrintf("%p -> 0x%04x", native_pc, dex_pc);
546 if (i + 2 < length) {
547 os << ", ";
548 }
549 }
550 os << "}" << std::endl << std::flush;
551 }
552
TDYa1273db52852012-04-01 15:11:43 -0700553#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800554 void DumpCode(std::ostream& os, const void* code, const uint32_t* raw_mapping_table,
555 const DexFile& dex_file, const DexFile::CodeItem* code_item) {
556 if (code == NULL) {
557 return;
558 }
559
560 if (raw_mapping_table == NULL) {
561 // code but no mapping table is most likely caused by code created by the JNI compiler
562 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code);
563 const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file_.Begin());
564 uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin);
565
566 typedef std::set<uint32_t>::iterator It;
567 It it = offsets_.upper_bound(last_offset);
568 CHECK(it != offsets_.end());
569 const uint8_t* end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it;
570 CHECK(native_pc < end_native_pc);
571
572 disassembler_->Dump(os, native_pc, end_native_pc);
573 return;
574 }
575
576 uint32_t length = *raw_mapping_table;
577 ++raw_mapping_table;
578
579 for (size_t i = 0; i < length; i += 2) {
580 uint32_t dex_pc = raw_mapping_table[i + 1];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800581 const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]);
582 os << StringPrintf("\t\t0x%04x: %s\n", dex_pc, instruction->DumpString(&dex_file).c_str());
583
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800584 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800585 const uint8_t* end_native_pc = NULL;
586 if (i + 2 < length) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800587 end_native_pc = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i + 2];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800588 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800589 const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file_.Begin());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800590 uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin);
591
592 typedef std::set<uint32_t>::iterator It;
Elliott Hughesed2adb62012-02-29 14:41:01 -0800593 It it = offsets_.upper_bound(last_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800594 CHECK(it != offsets_.end());
595 end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it;
596 }
Elliott Hughesed2adb62012-02-29 14:41:01 -0800597 CHECK(native_pc < end_native_pc);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800598 disassembler_->Dump(os, native_pc, end_native_pc);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800599 }
600 }
TDYa1273db52852012-04-01 15:11:43 -0700601#else
602 void DumpCode(std::ostream&, const void*, const uint32_t*,
603 const DexFile&, const DexFile::CodeItem*) {
604 // TODO: Dump code for the LLVM side.
605 }
606#endif
Elliott Hughese3c845c2012-02-28 17:23:01 -0800607
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700608 const std::string host_prefix_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800609 const OatFile& oat_file_;
610 std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800611 std::set<uint32_t> offsets_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800612 UniquePtr<Disassembler> disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700613};
614
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800615class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700616 public:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800617 explicit ImageDumper(std::ostream& os, const std::string& image_filename,
618 const std::string& host_prefix, Space& image_space,
619 const ImageHeader& image_header) : os_(os),
620 image_filename_(image_filename), host_prefix_(host_prefix),
621 image_space_(image_space), image_header_(image_header) {
622 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700623
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800624 void Dump() {
625 os_ << "MAGIC:\n";
626 os_ << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700627
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800628 os_ << "IMAGE BEGIN:\n";
629 os_ << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700630
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800631 os_ << "OAT CHECKSUM:\n";
632 os_ << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700633
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800634 os_ << "OAT BEGIN:\n";
635 os_ << reinterpret_cast<void*>(image_header_.GetOatBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700636
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800637 os_ << "OAT END:\n";
638 os_ << reinterpret_cast<void*>(image_header_.GetOatEnd()) << "\n\n";
639
640 os_ << "ROOTS:\n";
641 os_ << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700642 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700643 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
644 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700645 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800646 Object* image_root_object = image_header_.GetImageRoot(image_root);
647 os_ << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700648 if (image_root_object->IsObjectArray()) {
649 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
650 ObjectArray<Object>* image_root_object_array
651 = down_cast<ObjectArray<Object>*>(image_root_object);
652 // = image_root_object->AsObjectArray<Object>();
653 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800654 Object* value = image_root_object_array->Get(i);
655 if (value != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800656 os_ << "\t" << i << ": ";
Ian Rogersd5b32602012-02-26 16:40:04 -0800657 std::string summary;
658 PrettyObjectValue(summary, value->GetClass(), value);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800659 os_ << summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800660 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800661 os_ << StringPrintf("\t%d: null\n", i);
Ian Rogersd5b32602012-02-26 16:40:04 -0800662 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700663 }
664 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700665 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800666 os_ << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700667
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800668 os_ << "OAT LOCATION:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700669 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800670 Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation);
671 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800672 os_ << oat_location;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800673 if (!host_prefix_.empty()) {
674 oat_location = host_prefix_ + oat_location;
675 os_ << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700676 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800677 os_ << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800678 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700679 if (oat_file == NULL) {
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800680 os_ << "NOT FOUND\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700681 return;
682 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800683 os_ << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700684
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800685 stats_.oat_file_bytes = oat_file->Size();
686
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700687 oat_dumper_.reset(new OatDumper(host_prefix_, *oat_file));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800688
689 os_ << "OBJECTS:\n" << std::flush;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800690 HeapBitmap* heap_bitmap = Runtime::Current()->GetHeap()->GetLiveBits();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800691 DCHECK(heap_bitmap != NULL);
692 heap_bitmap->Walk(ImageDumper::Callback, this);
693 os_ << "\n";
694
695 os_ << "STATS:\n" << std::flush;
696 UniquePtr<File> file(OS::OpenFile(image_filename_.c_str(), false));
697 stats_.file_bytes = file->Length();
698 size_t header_bytes = sizeof(ImageHeader);
699 stats_.header_bytes = header_bytes;
700 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
701 stats_.alignment_bytes += alignment_bytes;
702 stats_.Dump(os_);
703 os_ << "\n";
704
705 os_ << std::flush;
706
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800707 oat_dumper_->Dump(os_);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700708 }
709
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700710 private:
711
Ian Rogersd5b32602012-02-26 16:40:04 -0800712 static void PrettyObjectValue(std::string& summary, Class* type, Object* value) {
713 CHECK(type != NULL);
714 if (value == NULL) {
715 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
716 } else if (type->IsStringClass()) {
717 String* string = value->AsString();
718 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
719 } else if (value->IsClass()) {
720 Class* klass = value->AsClass();
721 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
722 } else if (value->IsField()) {
723 Field* field = value->AsField();
724 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
725 } else if (value->IsMethod()) {
726 Method* method = value->AsMethod();
727 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
728 } else {
729 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
730 }
731 }
732
733 static void PrintField(std::string& summary, Field* field, Object* obj) {
734 FieldHelper fh(field);
735 Class* type = fh.GetType();
736 StringAppendF(&summary, "\t%s: ", fh.GetName());
737 if (type->IsPrimitiveLong()) {
738 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
739 } else if (type->IsPrimitiveDouble()) {
740 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
741 } else if (type->IsPrimitiveFloat()) {
742 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700743 } else if (type->IsPrimitive()) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800744 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
745 } else {
746 Object* value = field->GetObj(obj);
747 PrettyObjectValue(summary, type, value);
748 }
749 }
750
751 static void DumpFields(std::string& summary, Object* obj, Class* klass) {
752 Class* super = klass->GetSuperClass();
753 if (super != NULL) {
754 DumpFields(summary, obj, super);
755 }
756 ObjectArray<Field>* fields = klass->GetIFields();
757 if (fields != NULL) {
758 for (int32_t i = 0; i < fields->GetLength(); i++) {
759 Field* field = fields->Get(i);
760 PrintField(summary, field, obj);
761 }
762 }
763 }
764
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800765 bool InDumpSpace(const Object* object) {
766 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700767 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800768
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700769 const void* GetOatCodeBegin(Method* m) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800770 Runtime* runtime = Runtime::Current();
771 const void* code = m->GetCode();
Ian Rogersfb6adba2012-03-04 21:51:51 -0800772 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800773 code = oat_dumper_->GetOatCode(m);
774 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700775 if (oat_dumper_->GetInstructionSet() == kThumb2) {
776 code = reinterpret_cast<void*>(reinterpret_cast<uint32_t>(code) & ~0x1);
777 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800778 return code;
779 }
780
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700781 uint32_t GetOatCodeSize(Method* m) {
782 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetOatCodeBegin(m));
783 if (oat_code_begin == NULL) {
784 return 0;
785 }
786 return oat_code_begin[-1];
787 }
788
789 const void* GetOatCodeEnd(Method* m) {
790 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetOatCodeBegin(m));
791 if (oat_code_begin == NULL) {
792 return NULL;
793 }
794 return oat_code_begin + GetOatCodeSize(m);
795 }
796
Brian Carlstrom78128a62011-09-15 17:21:19 -0700797 static void Callback(Object* obj, void* arg) {
798 DCHECK(obj != NULL);
799 DCHECK(arg != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800800 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700801 if (!state->InDumpSpace(obj)) {
802 return;
803 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700804
805 size_t object_bytes = obj->SizeOf();
806 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
807 state->stats_.object_bytes += object_bytes;
808 state->stats_.alignment_bytes += alignment_bytes;
809
Brian Carlstrom78128a62011-09-15 17:21:19 -0700810 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800811 Class* obj_class = obj->GetClass();
812 if (obj_class->IsArrayClass()) {
813 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
814 obj->AsArray()->GetLength());
815 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700816 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800817 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
818 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700819 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800820 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700821 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800822 } else if (obj->IsField()) {
823 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
824 PrettyField(obj->AsField()).c_str());
825 } else if (obj->IsMethod()) {
826 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
827 PrettyMethod(obj->AsMethod()).c_str());
828 } else if (obj_class->IsStringClass()) {
829 StringAppendF(&summary, "%p: java.lang.String \"%s\"\n", obj,
830 obj->AsString()->ToModifiedUtf8().c_str());
831 } else {
832 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
833 }
834 DumpFields(summary, obj, obj_class);
835 if (obj->IsObjectArray()) {
836 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
837 int32_t length = obj_array->GetLength();
838 for (int32_t i = 0; i < length; i++) {
839 Object* value = obj_array->Get(i);
840 size_t run = 0;
841 for (int32_t j = i + 1; j < length; j++) {
842 if (value == obj_array->Get(j)) {
843 run++;
844 } else {
845 break;
846 }
847 }
848 if (run == 0) {
849 StringAppendF(&summary, "\t%d: ", i);
850 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800851 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800852 i = i + run;
853 }
854 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
855 PrettyObjectValue(summary, value_class, value);
856 }
857 } else if (obj->IsClass()) {
858 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
859 if (sfields != NULL) {
860 summary += "\t\tSTATICS:\n";
861 for (int32_t i = 0; i < sfields->GetLength(); i++) {
862 Field* field = sfields->Get(i);
863 PrintField(summary, field, NULL);
864 }
865 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700866 } else if (obj->IsMethod()) {
867 Method* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700868 if (method->IsNative()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700869 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800870 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700871 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800872 bool first_occurrence;
873 size_t invoke_stub_size = state->ComputeOatSize(
874 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
875 if (first_occurrence) {
876 state->stats_.managed_to_native_code_bytes += invoke_stub_size;
877 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700878 const void* oat_code = state->GetOatCodeBegin(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800879 size_t code_size = state->ComputeOatSize(oat_code, &first_occurrence);
880 if (first_occurrence) {
881 state->stats_.native_to_managed_code_bytes += code_size;
882 }
883 if (oat_code != method->GetCode()) {
884 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
885 }
Ian Rogers19846512012-02-24 11:42:47 -0800886 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
887 method->IsResolutionMethod()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700888 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800889 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700890 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700891 } else {
TDYa1273db52852012-04-01 15:11:43 -0700892#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800893 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
894 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
TDYa1273db52852012-04-01 15:11:43 -0700895#endif
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700896
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800897 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700898 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700899 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800900
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800901 bool first_occurance;
902 size_t gc_map_bytes = state->ComputeOatSize(method->GetGcMapRaw(), &first_occurance);
903 if (first_occurance) {
904 state->stats_.gc_map_bytes += gc_map_bytes;
905 }
906
907 size_t pc_mapping_table_bytes =
908 state->ComputeOatSize(method->GetMappingTableRaw(), &first_occurance);
909 if (first_occurance) {
910 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
911 }
912
913 size_t vmap_table_bytes =
914 state->ComputeOatSize(method->GetVmapTableRaw(), &first_occurance);
915 if (first_occurance) {
916 state->stats_.vmap_table_bytes += vmap_table_bytes;
917 }
918
919 size_t invoke_stub_size = state->ComputeOatSize(
920 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurance);
921 if (first_occurance) {
922 state->stats_.native_to_managed_code_bytes += invoke_stub_size;
923 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700924 const void* oat_code_begin = state->GetOatCodeBegin(method);
925 const void* oat_code_end = state->GetOatCodeEnd(method);
926 // TODO: use oat_code_size and remove code_size based on offsets
927 // uint32_t oat_code_size = state->GetOatCodeSize(method);
928 size_t code_size = state->ComputeOatSize(oat_code_begin, &first_occurance);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800929 if (first_occurance) {
930 state->stats_.managed_code_bytes += code_size;
931 }
932 state->stats_.managed_code_bytes_ignoring_deduplication += code_size;
933
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700934 StringAppendF(&summary, "\t\tOAT CODE: %p-%p\n", oat_code_begin, oat_code_end);
Ian Rogersd5b32602012-02-26 16:40:04 -0800935 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800936 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
937
938 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
939 vmap_table_bytes + invoke_stub_size + code_size + object_bytes;
940
941 double expansion =
942 static_cast<double>(code_size) / static_cast<double>(dex_instruction_bytes);
943 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700944 }
945 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800946 std::string descriptor(ClassHelper(obj_class).GetDescriptor());
947 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
948 state->stats_.descriptor_to_count[descriptor] += 1;
949
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700950 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700951 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700952
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800953 std::set<const void*> already_seen_;
954 // Compute the size of the given data within the oat file and whether this is the first time
955 // this data has been requested
956 size_t ComputeOatSize(const void* oat_data, bool* first_occurance) {
957 if (already_seen_.count(oat_data) == 0) {
958 *first_occurance = true;
959 already_seen_.insert(oat_data);
960 } else {
961 *first_occurance = false;
962 }
963 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700964 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700965
966 public:
967 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800968 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700969 size_t file_bytes;
970
971 size_t header_bytes;
972 size_t object_bytes;
973 size_t alignment_bytes;
974
975 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800976 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700977 size_t managed_to_native_code_bytes;
978 size_t native_to_managed_code_bytes;
979
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800980 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700981 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800982 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700983
984 size_t dex_instruction_bytes;
985
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800986 std::vector<Method*> method_outlier;
987 std::vector<size_t> method_outlier_size;
988 std::vector<double> method_outlier_expansion;
989
990 explicit Stats()
991 : oat_file_bytes(0),
992 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700993 header_bytes(0),
994 object_bytes(0),
995 alignment_bytes(0),
996 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800997 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700998 managed_to_native_code_bytes(0),
999 native_to_managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001000 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001001 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001002 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001003 dex_instruction_bytes(0) {}
1004
Elliott Hughese5448b52012-01-18 16:44:06 -08001005 typedef std::map<std::string, size_t> TableBytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001006 TableBytes descriptor_to_bytes;
1007
Elliott Hughese5448b52012-01-18 16:44:06 -08001008 typedef std::map<std::string, size_t> TableCount;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001009 TableCount descriptor_to_count;
1010
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001011 double PercentOfOatBytes(size_t size) {
1012 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1013 }
1014
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001015 double PercentOfFileBytes(size_t size) {
1016 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1017 }
1018
1019 double PercentOfObjectBytes(size_t size) {
1020 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1021 }
1022
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001023 void ComputeOutliers(size_t total_size, double expansion, Method* method) {
1024 method_outlier_size.push_back(total_size);
1025 method_outlier_expansion.push_back(expansion);
1026 method_outlier.push_back(method);
1027 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001028
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001029 void DumpOutliers(std::ostream& os) {
1030 size_t sum_of_sizes = 0;
1031 size_t sum_of_sizes_squared = 0;
1032 size_t sum_of_expansion = 0;
1033 size_t sum_of_expansion_squared = 0;
1034 size_t n = method_outlier_size.size();
1035 for (size_t i = 0; i < n; i++) {
1036 size_t cur_size = method_outlier_size[i];
1037 sum_of_sizes += cur_size;
1038 sum_of_sizes_squared += cur_size * cur_size;
1039 double cur_expansion = method_outlier_expansion[i];
1040 sum_of_expansion += cur_expansion;
1041 sum_of_expansion_squared += cur_expansion * cur_expansion;
1042 }
1043 size_t size_mean = sum_of_sizes / n;
1044 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1045 double expansion_mean = sum_of_expansion / n;
1046 double expansion_variance =
1047 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1048
1049 // Dump methods whose size is a certain number of standard deviations from the mean
1050 size_t dumped_values = 0;
1051 size_t skipped_values = 0;
1052 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1053 size_t cur_size_variance = i * i * size_variance;
1054 bool first = true;
1055 for (size_t j = 0; j < n; j++) {
1056 size_t cur_size = method_outlier_size[j];
1057 if (cur_size > size_mean) {
1058 size_t cur_var = cur_size - size_mean;
1059 cur_var = cur_var * cur_var;
1060 if (cur_var > cur_size_variance) {
1061 if (dumped_values > 20) {
1062 if (i == 1) {
1063 skipped_values++;
1064 } else {
1065 i = 2; // jump to counting for 1 standard deviation
1066 break;
1067 }
1068 } else {
1069 if (first) {
1070 os << "\nBig methods (size > " << i << " standard deviations the norm):"
1071 << std::endl;
1072 first = false;
1073 }
1074 os << "\t" << PrettyMethod(method_outlier[j]) << " requires storage of "
1075 << PrettySize(cur_size) << std::endl;
1076 method_outlier_size[j] = 0; // don't consider this method again
1077 dumped_values++;
1078 }
1079 }
1080 }
1081 }
1082 }
1083 if (skipped_values > 0) {
1084 os << "\t... skipped " << skipped_values
1085 << " methods with size > 1 standard deviation from the norm" << std::endl;
1086 }
1087 os << std::endl << std::flush;
1088
1089 // Dump methods whose expansion is a certain number of standard deviations from the mean
1090 dumped_values = 0;
1091 skipped_values = 0;
1092 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1093 double cur_expansion_variance = i * i * expansion_variance;
1094 bool first = true;
1095 for (size_t j = 0; j < n; j++) {
1096 double cur_expansion = method_outlier_expansion[j];
1097 if (cur_expansion > expansion_mean) {
1098 size_t cur_var = cur_expansion - expansion_mean;
1099 cur_var = cur_var * cur_var;
1100 if (cur_var > cur_expansion_variance) {
1101 if (dumped_values > 20) {
1102 if (i == 1) {
1103 skipped_values++;
1104 } else {
1105 i = 2; // jump to counting for 1 standard deviation
1106 break;
1107 }
1108 } else {
1109 if (first) {
1110 os << "\nLarge expansion methods (size > " << i
1111 << " standard deviations the norm):" << std::endl;
1112 first = false;
1113 }
1114 os << "\t" << PrettyMethod(method_outlier[j]) << " expanded code by "
1115 << cur_expansion << std::endl;
1116 method_outlier_expansion[j] = 0.0; // don't consider this method again
1117 dumped_values++;
1118 }
1119 }
1120 }
1121 }
1122 }
1123 if (skipped_values > 0) {
1124 os << "\t... skipped " << skipped_values
1125 << " methods with expansion > 1 standard deviation from the norm" << std::endl;
1126 }
1127 os << std::endl << std::flush;
1128 }
1129
1130 void Dump(std::ostream& os) {
1131 os << "\tart_file_bytes = " << PrettySize(file_bytes) << std::endl << std::endl
1132 << "\tart_file_bytes = header_bytes + object_bytes + alignment_bytes" << std::endl
1133 << StringPrintf("\theader_bytes = %8zd (%2.0f%% of art file bytes)\n"
1134 "\tobject_bytes = %8zd (%2.0f%% of art file bytes)\n"
1135 "\talignment_bytes = %8zd (%2.0f%% of art file bytes)\n",
1136 header_bytes, PercentOfFileBytes(header_bytes),
1137 object_bytes, PercentOfFileBytes(object_bytes),
1138 alignment_bytes, PercentOfFileBytes(alignment_bytes))
1139 << std::endl << std::flush;
1140
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001141 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
1142
1143 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
1144 size_t object_bytes_total = 0;
1145 typedef TableBytes::const_iterator It; // TODO: C++0x auto
1146 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -08001147 const std::string& descriptor(it->first);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001148 size_t bytes = it->second;
1149 size_t count = descriptor_to_count[descriptor];
1150 double average = static_cast<double>(bytes) / static_cast<double>(count);
1151 double percent = PercentOfObjectBytes(bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001152 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001153 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
1154 descriptor.c_str(), bytes, count,
1155 average, percent);
1156
1157 object_bytes_total += bytes;
1158 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001159 os << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001160 CHECK_EQ(object_bytes, object_bytes_total);
1161
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001162 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1163 "\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1164 "\tnative_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n",
1165 managed_code_bytes, PercentOfOatBytes(managed_code_bytes),
1166 managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes),
1167 native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes))
1168 << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001169
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001170 os << StringPrintf("\tgc_map_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1171 "\tpc_mapping_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1172 "\tvmap_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n",
1173 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1174 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1175 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
1176 << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001177
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001178 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001179 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n",
1180 static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes),
1181 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
1182 static_cast<double>(dex_instruction_bytes));
1183 os << std::endl << std::flush;
1184
1185 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001186 }
1187 } stats_;
1188
1189 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001190 UniquePtr<OatDumper> oat_dumper_;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001191 std::ostream& os_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001192 const std::string image_filename_;
1193 const std::string host_prefix_;
1194 Space& image_space_;
1195 const ImageHeader& image_header_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001196
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001197 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001198};
1199
1200int oatdump(int argc, char** argv) {
1201 // Skip over argv[0].
1202 argv++;
1203 argc--;
1204
1205 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001206 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001207 usage();
1208 }
1209
Brian Carlstromaded5f72011-10-07 17:15:04 -07001210 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001211 const char* image_filename = NULL;
1212 const char* boot_image_filename = NULL;
Logan Chien0cc6ab62012-03-20 22:57:52 +08001213 std::string elf_filename_prefix;
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001214 UniquePtr<std::string> host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001215 std::ostream* os = &std::cout;
1216 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001217
1218 for (int i = 0; i < argc; i++) {
1219 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001220 if (option.starts_with("--oat-file=")) {
1221 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001222 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001223 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001224 } else if (option.starts_with("--boot-image=")) {
1225 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Logan Chien0cc6ab62012-03-20 22:57:52 +08001226 } else if (option.starts_with("--extract-elf-to=")) {
1227 elf_filename_prefix = option.substr(strlen("--extract-elf-to=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001228 } else if (option.starts_with("--host-prefix=")) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001229 host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001230 } else if (option.starts_with("--output=")) {
1231 const char* filename = option.substr(strlen("--output=")).data();
1232 out.reset(new std::ofstream(filename));
1233 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001234 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001235 usage();
1236 }
1237 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001238 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001239 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001240 usage();
1241 }
1242 }
1243
Brian Carlstromaded5f72011-10-07 17:15:04 -07001244 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001245 fprintf(stderr, "Either --image or --oat must be specified\n");
1246 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001247 }
1248
Brian Carlstromaded5f72011-10-07 17:15:04 -07001249 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001250 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1251 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001252 }
1253
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001254 if (host_prefix.get() == NULL) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001255 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
1256 if (android_product_out != NULL) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001257 host_prefix.reset(new std::string(android_product_out));
1258 } else {
1259 host_prefix.reset(new std::string(""));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001260 }
1261 }
1262
Brian Carlstromaded5f72011-10-07 17:15:04 -07001263 if (oat_filename != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001264 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001265 if (oat_file == NULL) {
1266 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
1267 return EXIT_FAILURE;
1268 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001269 OatDumper oat_dumper(*host_prefix.get(), *oat_file);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001270 oat_dumper.Dump(*os);
Logan Chien0cc6ab62012-03-20 22:57:52 +08001271
1272 if (!elf_filename_prefix.empty()) {
1273 uint32_t elf_image_count = oat_file->GetOatHeader().GetElfImageCount();
1274 for (uint32_t i = 0; i < elf_image_count; ++i) {
1275 const OatFile::OatElfImage* elf_image = oat_file->GetOatElfImage(i);
1276
1277 std::string elf_filename(
1278 StringPrintf("%s-%u", elf_filename_prefix.c_str(), i));
1279
1280 UniquePtr<File> elf_file(OS::OpenFile(elf_filename.c_str(), true));
1281
1282 if (!elf_file->WriteFully(elf_image->begin(), elf_image->size())) {
1283 fprintf(stderr, "Failed to write ELF image to: %s\n",
1284 elf_filename.c_str());
1285 }
1286 }
1287 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001288 return EXIT_SUCCESS;
1289 }
1290
Brian Carlstrom78128a62011-09-15 17:21:19 -07001291 Runtime::Options options;
1292 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001293 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001294 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001295 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001296 if (boot_image_filename != NULL) {
1297 boot_image_option += "-Ximage:";
1298 boot_image_option += boot_image_filename;
1299 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001300 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001301 if (image_filename != NULL) {
1302 image_option += "-Ximage:";
1303 image_option += image_filename;
1304 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1305 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001306
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001307 if (!host_prefix->empty()) {
1308 options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001309 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001310
1311 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
1312 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001313 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001314 return EXIT_FAILURE;
1315 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001316
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001317 Heap* heap = Runtime::Current()->GetHeap();
Brian Carlstromfddf6f62012-03-15 16:56:45 -07001318 ImageSpace* image_space = heap->GetImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001319 CHECK(image_space != NULL);
1320 const ImageHeader& image_header = image_space->GetImageHeader();
1321 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001322 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001323 return EXIT_FAILURE;
1324 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001325 ImageDumper image_dumper(*os, image_filename, *host_prefix.get(), *image_space, image_header);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001326 image_dumper.Dump();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001327 return EXIT_SUCCESS;
1328}
1329
1330} // namespace art
1331
1332int main(int argc, char** argv) {
1333 return art::oatdump(argc, argv);
1334}