blob: 88dbaaeb034322b5037e18185963513689dbe4ea [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Brian Carlstrom78128a62011-09-15 17:21:19 -070022#include <string>
23#include <vector>
24
25#include "class_linker.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080026#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080027#include "disassembler.h"
Brian Carlstrom916e74e2011-09-23 11:42:01 -070028#include "file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070029#include "image.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070030#include "oat/runtime/context.h" // For VmapTable
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080031#include "object_utils.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080032#include "os.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070033#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070034#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035#include "scoped_thread_state_change.h"
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070036#include "gc/space.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070037#include "stringpiece.h"
Ian Rogers0c7abda2012-09-19 13:33:42 -070038#include "gc_map.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070039
40namespace art {
41
42static void usage() {
43 fprintf(stderr,
44 "Usage: oatdump [options] ...\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080045 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n"
46 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070047 "\n");
48 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080049 " --oat-file=<file.oat>: specifies an input oat filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080050 " Example: --image=/system/framework/boot.oat\n"
Brian Carlstromaded5f72011-10-07 17:15:04 -070051 "\n");
52 fprintf(stderr,
53 " --image=<file.art>: specifies an input image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080054 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070055 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070056 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070057 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080058 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070059 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070060 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070061 " --host-prefix may be used to translate host paths to target paths during\n"
62 " cross compilation.\n"
63 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstromfe487d02012-02-29 18:49:16 -080064 " Default: $ANDROID_PRODUCT_OUT\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070065 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070066 fprintf(stderr,
67 " --output=<file> may be used to send the output to a file.\n"
68 " Example: --output=/tmp/oatdump.txt\n"
69 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070070 exit(EXIT_FAILURE);
71}
72
Ian Rogersff1ed472011-09-20 13:46:24 -070073const char* image_roots_descriptions_[] = {
Brian Carlstrom78128a62011-09-15 17:21:19 -070074 "kJniStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070075 "kAbstractMethodErrorStubArray",
Ian Rogersad25ac52011-10-04 19:13:33 -070076 "kStaticResolutionStubArray",
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070077 "kUnknownMethodResolutionStubArray",
Ian Rogers19846512012-02-24 11:42:47 -080078 "kResolutionMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070079 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070080 "kRefsOnlySaveMethod",
81 "kRefsAndArgsSaveMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070082 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070083 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070084 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070085};
86
Elliott Hughese3c845c2012-02-28 17:23:01 -080087class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -070088 public:
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070089 explicit OatDumper(const std::string& host_prefix, const OatFile& oat_file)
90 : host_prefix_(host_prefix),
91 oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -080092 oat_dex_files_(oat_file.GetOatDexFiles()),
93 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet())) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080094 AddAllOffsets();
95 }
96
97 void Dump(std::ostream& os) {
98 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -070099
100 os << "MAGIC:\n";
101 os << oat_header.GetMagic() << "\n\n";
102
103 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800104 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700105
Elliott Hughesa72ec822012-03-05 17:12:22 -0800106 os << "INSTRUCTION SET:\n";
107 os << oat_header.GetInstructionSet() << "\n\n";
108
Brian Carlstromaded5f72011-10-07 17:15:04 -0700109 os << "DEX FILE COUNT:\n";
110 os << oat_header.GetDexFileCount() << "\n\n";
111
112 os << "EXECUTABLE OFFSET:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800113 os << StringPrintf("0x%08x\n\n", oat_header.GetExecutableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700114
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700115 os << "IMAGE FILE LOCATION CHECKSUM:\n";
116 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationChecksum());
117
118 os << "IMAGE FILE LOCATION:\n";
119 const std::string image_file_location(oat_header.GetImageFileLocation());
120 os << image_file_location;
121 if (!image_file_location.empty() && !host_prefix_.empty()) {
122 os << " (" << host_prefix_ << image_file_location << ")";
123 }
124 os << "\n\n";
125
Ian Rogers30fab402012-01-23 15:43:46 -0800126 os << "BEGIN:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800127 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700128
Ian Rogers30fab402012-01-23 15:43:46 -0800129 os << "END:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800130 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700131
132 os << std::flush;
133
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800134 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
135 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Brian Carlstromaded5f72011-10-07 17:15:04 -0700136 CHECK(oat_dex_file != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800137 DumpOatDexFile(os, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700138 }
139 }
140
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800141 size_t ComputeSize(const void* oat_data) {
142 if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() ||
143 reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) {
144 return 0; // Address not in oat file
145 }
146 uint32_t begin_offset = reinterpret_cast<size_t>(oat_data) -
147 reinterpret_cast<size_t>(oat_file_.Begin());
148 typedef std::set<uint32_t>::iterator It;
149 It it = offsets_.upper_bound(begin_offset);
150 CHECK(it != offsets_.end());
151 uint32_t end_offset = *it;
152 return end_offset - begin_offset;
153 }
154
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700155 InstructionSet GetInstructionSet() {
156 return oat_file_.GetOatHeader().GetInstructionSet();
157 }
158
Mathieu Chartier66f19252012-09-18 08:57:04 -0700159 const void* GetOatCode(AbstractMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800160 MethodHelper mh(m);
161 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
162 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
163 CHECK(oat_dex_file != NULL);
164 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
165 if (dex_file.get() != NULL) {
166 uint32_t class_def_index;
167 bool found = dex_file->FindClassDefIndex(mh.GetDeclaringClassDescriptor(), class_def_index);
168 if (found) {
169 const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_index);
170 CHECK(oat_class != NULL);
171 size_t method_index = m->GetMethodIndex();
172 return oat_class->GetOatMethod(method_index).GetCode();
173 }
174 }
175 }
176 return NULL;
177 }
178
Brian Carlstromaded5f72011-10-07 17:15:04 -0700179 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800180 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800181 // We don't know the length of the code for each method, but we need to know where to stop
182 // when disassembling. What we do know is that a region of code will be followed by some other
183 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
184 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800185 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
186 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800187 CHECK(oat_dex_file != NULL);
188 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
189 if (dex_file.get() == NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800190 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800191 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800192 offsets_.insert(reinterpret_cast<uint32_t>(&dex_file->GetHeader()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800193 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
194 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
195 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index));
196 const byte* class_data = dex_file->GetClassData(class_def);
197 if (class_data != NULL) {
198 ClassDataItemIterator it(*dex_file, class_data);
199 SkipAllFields(it);
200 uint32_t class_method_index = 0;
201 while (it.HasNextDirectMethod()) {
202 AddOffsets(oat_class->GetOatMethod(class_method_index++));
203 it.Next();
204 }
205 while (it.HasNextVirtualMethod()) {
206 AddOffsets(oat_class->GetOatMethod(class_method_index++));
207 it.Next();
208 }
209 }
210 }
211 }
212
213 // If the last thing in the file is code for a method, there won't be an offset for the "next"
214 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
215 // for the end of the file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800216 offsets_.insert(static_cast<uint32_t>(oat_file_.End() - oat_file_.Begin()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800217 }
218
219 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800220 uint32_t code_offset = oat_method.GetCodeOffset();
221 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
222 code_offset &= ~0x1;
223 }
224 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800225 offsets_.insert(oat_method.GetMappingTableOffset());
226 offsets_.insert(oat_method.GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700227 offsets_.insert(oat_method.GetNativeGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800228 offsets_.insert(oat_method.GetInvokeStubOffset());
229 }
230
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800231 void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700232 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800233 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800234 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800235 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile());
236 if (dex_file.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700237 os << "NOT FOUND\n\n";
238 return;
239 }
240 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
241 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
242 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700243 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
244 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800245 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800246 << oat_class->GetStatus() << ")\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800247 DumpOatClass(os, *oat_class.get(), *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700248 }
249
250 os << std::flush;
251 }
252
Elliott Hughese3c845c2012-02-28 17:23:01 -0800253 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700254 while (it.HasNextStaticField()) {
255 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700256 }
Ian Rogers0571d352011-11-03 19:51:38 -0700257 while (it.HasNextInstanceField()) {
258 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700259 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800260 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700261
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800262 void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
263 const DexFile::ClassDef& class_def) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800264 const byte* class_data = dex_file.GetClassData(class_def);
265 if (class_data == NULL) { // empty class such as a marker interface?
266 return;
267 }
268 ClassDataItemIterator it(dex_file, class_data);
269 SkipAllFields(it);
270
271 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700272 while (it.HasNextDirectMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800273 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800274 DumpOatMethod(os, class_method_index, oat_method, dex_file,
275 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800276 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700277 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700278 }
Ian Rogers0571d352011-11-03 19:51:38 -0700279 while (it.HasNextVirtualMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800280 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800281 DumpOatMethod(os, class_method_index, oat_method, dex_file,
282 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800283 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700284 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700285 }
Ian Rogers0571d352011-11-03 19:51:38 -0700286 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700287 os << std::flush;
288 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800289
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800290 void DumpOatMethod(std::ostream& os, uint32_t class_method_index,
Elliott Hughese3c845c2012-02-28 17:23:01 -0800291 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
292 uint32_t dex_method_idx, const DexFile::CodeItem* code_item) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700293 os << StringPrintf("\t%d: %s (dex_method_idx=%d)\n",
294 class_method_index, PrettyMethod(dex_method_idx, dex_file, true).c_str(),
295 dex_method_idx);
Ian Rogersb23a7722012-10-09 16:54:26 -0700296 os << StringPrintf("\t\tframe_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
297 os << StringPrintf("\t\tcore_spill_mask: 0x%08x\n", oat_method.GetCoreSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800298 DumpSpillMask(os, oat_method.GetCoreSpillMask(), false);
Ian Rogersb23a7722012-10-09 16:54:26 -0700299 os << StringPrintf("\n\t\tfp_spill_mask: 0x%08x\n", oat_method.GetFpSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800300 DumpSpillMask(os, oat_method.GetFpSpillMask(), true);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800301 os << StringPrintf("\t\tvmap_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800302 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Ian Rogersb23a7722012-10-09 16:54:26 -0700303 DumpVmap(os, oat_method);
304 const bool kDumpRawMappingTable = false;
305 if (kDumpRawMappingTable) {
306 os << StringPrintf("\t\tmapping_table: %p (offset=0x%08x)\n",
307 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
308 DumpMappingTable(os, oat_method);
309 }
310 const bool kDumpRawGcMap = false;
311 if (kDumpRawGcMap) {
312 os << StringPrintf("\t\tgc_map: %p (offset=0x%08x)\n",
313 oat_method.GetNativeGcMap(), oat_method.GetNativeGcMapOffset());
314 DumpGcMap(os, oat_method, code_item);
315 }
316 os << "\t\tDEX CODE:\n";
317 DumpDexCode(os, dex_file, code_item);
Logan Chien971bf3f2012-05-01 15:47:55 +0800318 os << StringPrintf("\t\tCODE: %p (offset=0x%08x size=%d)%s\n",
Elliott Hughes77405792012-03-15 15:22:12 -0700319 oat_method.GetCode(),
320 oat_method.GetCodeOffset(),
321 oat_method.GetCodeSize(),
322 oat_method.GetCode() != NULL ? "..." : "");
Ian Rogersb23a7722012-10-09 16:54:26 -0700323 DumpCode(os, oat_method, code_item);
Logan Chien971bf3f2012-05-01 15:47:55 +0800324 os << StringPrintf("\t\tINVOKE STUB: %p (offset=0x%08x size=%d)%s\n",
Elliott Hughes77405792012-03-15 15:22:12 -0700325 oat_method.GetInvokeStub(),
326 oat_method.GetInvokeStubOffset(),
327 oat_method.GetInvokeStubSize(),
328 oat_method.GetInvokeStub() != NULL ? "..." : "");
Ian Rogersb23a7722012-10-09 16:54:26 -0700329 DumpInvokeStub(os, oat_method);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700330 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800331
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800332 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
333 if (spill_mask == 0) {
334 return;
335 }
336 os << " (";
337 for (size_t i = 0; i < 32; i++) {
338 if ((spill_mask & (1 << i)) != 0) {
339 if (is_float) {
340 os << "fr" << i;
341 } else {
342 os << "r" << i;
343 }
344 spill_mask ^= 1 << i; // clear bit
345 if (spill_mask != 0) {
346 os << ", ";
347 } else {
348 break;
349 }
350 }
351 }
352 os << ")";
353 }
354
Ian Rogersb23a7722012-10-09 16:54:26 -0700355 void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) {
356 const uint16_t* raw_table = oat_method.GetVmapTable();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800357 if (raw_table == NULL) {
358 return;
359 }
360 const VmapTable vmap_table(raw_table);
361 bool first = true;
362 os << "\t\t\t";
363 for (size_t i = 0; i < vmap_table.size(); i++) {
364 uint16_t dex_reg = vmap_table[i];
365 size_t matches = 0;
366 size_t spill_shifts = 0;
Ian Rogersb23a7722012-10-09 16:54:26 -0700367 uint32_t spill_mask = oat_method.GetCoreSpillMask();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800368 bool processing_fp = false;
369 while (matches != (i + 1)) {
370 if (spill_mask == 0) {
371 CHECK(!processing_fp);
Ian Rogersb23a7722012-10-09 16:54:26 -0700372 spill_mask = oat_method.GetFpSpillMask();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800373 processing_fp = true;
374 }
375 matches += spill_mask & 1; // Add 1 if the low bit is set
376 spill_mask >>= 1;
377 spill_shifts++;
378 }
379 size_t arm_reg = spill_shifts - 1; // wind back one as we want the last match
380 os << (first ? "v" : ", v") << dex_reg;
381 if (arm_reg < 16) {
382 os << "/r" << arm_reg;
383 } else {
384 os << "/fr" << (arm_reg - 16);
385 }
386 if (first) {
387 first = false;
388 }
389 }
Elliott Hughesc073b072012-05-24 19:29:17 -0700390 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800391 }
392
Ian Rogersb23a7722012-10-09 16:54:26 -0700393 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
394 const DexFile::CodeItem* code_item, size_t reg) {
395 const uint16_t* raw_table = oat_method.GetVmapTable();
396 if (raw_table != NULL) {
397 const VmapTable vmap_table(raw_table);
398 uint32_t vmap_offset;
399 if (vmap_table.IsInContext(reg, vmap_offset)) {
400 // Compute the register we need to load from the context
401 uint32_t spill_mask = oat_method.GetCoreSpillMask();
402 CHECK_LT(vmap_offset, static_cast<uint32_t>(__builtin_popcount(spill_mask)));
403 uint32_t matches = 0;
404 uint32_t spill_shifts = 0;
405 while (matches != (vmap_offset + 1)) {
406 DCHECK_NE(spill_mask, 0u);
407 matches += spill_mask & 1; // Add 1 if the low bit is set
408 spill_mask >>= 1;
409 spill_shifts++;
410 }
411 spill_shifts--; // wind back one as we want the last match
412 os << "r" << spill_shifts;
413 } else {
414 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
415 oat_method.GetFpSpillMask(),
416 oat_method.GetFrameSizeInBytes(), reg);
417 os << "[sp + #" << offset << "]";
418 }
419 }
420 }
421
422 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
423 const DexFile::CodeItem* code_item) {
424 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800425 if (gc_map_raw == NULL) {
426 return;
427 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700428 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogersb23a7722012-10-09 16:54:26 -0700429 const void* code = oat_method.GetCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800430 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
Ian Rogersbb6723f2012-09-21 09:49:06 -0700431 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) +
432 map.GetNativePcOffset(entry);
433 os << StringPrintf("\t\t\t%p", native_pc);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800434 size_t num_regs = map.RegWidth() * 8;
435 const uint8_t* reg_bitmap = map.GetBitMap(entry);
436 bool first = true;
437 for (size_t reg = 0; reg < num_regs; reg++) {
438 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
439 if (first) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700440 os << " v" << reg << " (";
441 DescribeVReg(os, oat_method, code_item, reg);
442 os << ")";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800443 first = false;
444 } else {
Ian Rogersb23a7722012-10-09 16:54:26 -0700445 os << ", v" << reg << " (";
446 DescribeVReg(os, oat_method, code_item, reg);
447 os << ")";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800448 }
449 }
450 }
Elliott Hughesc073b072012-05-24 19:29:17 -0700451 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800452 }
453 }
454
455 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800456 const uint32_t* raw_table = oat_method.GetMappingTable();
457 const void* code = oat_method.GetCode();
458 if (raw_table == NULL || code == NULL) {
459 return;
460 }
461
buzbee4d7a1892012-10-03 11:03:37 -0700462 ++raw_table;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800463 uint32_t length = *raw_table;
464 ++raw_table;
Bill Buzbeea5b30242012-09-28 07:19:44 -0700465 uint32_t pc_to_dex_entries = *raw_table;
466 ++raw_table;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800467
Ian Rogersb23a7722012-10-09 16:54:26 -0700468 os << "\t\tsuspend point mappings {";
Elliott Hughese3c845c2012-02-28 17:23:01 -0800469 for (size_t i = 0; i < length; i += 2) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800470 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800471 uint32_t dex_pc = raw_table[i + 1];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800472 os << StringPrintf("%p -> 0x%04x", native_pc, dex_pc);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700473 if (i + 2 == pc_to_dex_entries) {
474 // Separate the pc -> dex from dex -> pc sections
Ian Rogersb23a7722012-10-09 16:54:26 -0700475 os << "}\n\t\tcatch entry mappings {";
Bill Buzbeea5b30242012-09-28 07:19:44 -0700476 } else if (i + 2 < length) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800477 os << ", ";
478 }
479 }
Ian Rogersb23a7722012-10-09 16:54:26 -0700480 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800481 }
482
Ian Rogersb23a7722012-10-09 16:54:26 -0700483 void DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method, size_t offset,
484 bool suspend_point_mapping) {
485 const uint32_t* raw_table = oat_method.GetMappingTable();
486 if (raw_table != NULL) {
487 ++raw_table;
488 uint32_t length = *raw_table;
489 ++raw_table;
490 uint32_t pc_to_dex_entries = *raw_table;
491 ++raw_table;
492 size_t start, end;
493 if (suspend_point_mapping) {
494 start = 0;
495 end = pc_to_dex_entries;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800496 } else {
Ian Rogersb23a7722012-10-09 16:54:26 -0700497 start = pc_to_dex_entries;
498 end = length;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800499 }
Ian Rogersb23a7722012-10-09 16:54:26 -0700500 for (size_t i = start; i < end; i += 2) {
501 if (offset == raw_table[i]) {
502 if (suspend_point_mapping) {
503 os << "\t\t\tsuspend point dex PC: ";
504 } else {
505 os << "\t\t\tcatch entry dex PC: ";
506 }
507 os << raw_table[i + 1] << "\n";
508 return;
509 }
510 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800511 }
Ian Rogersb23a7722012-10-09 16:54:26 -0700512 }
513
514 void DumpGcMapAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
515 const DexFile::CodeItem* code_item, size_t offset) {
516 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
517 if (gc_map_raw != NULL) {
518 NativePcOffsetToReferenceMap map(gc_map_raw);
519 if (map.HasEntry(offset)) {
520 size_t num_regs = map.RegWidth() * 8;
521 const uint8_t* reg_bitmap = map.FindBitMap(offset);
522 bool first = true;
523 for (size_t reg = 0; reg < num_regs; reg++) {
524 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
525 if (first) {
526 os << "\t\t\tGC map objects: v" << reg << " (";
527 DescribeVReg(os, oat_method, code_item, reg);
528 os << ")";
529 first = false;
530 } else {
531 os << ", v" << reg << " (";
532 DescribeVReg(os, oat_method, code_item, reg);
533 os << ")";
534 }
535 }
536 }
537 if (!first) {
538 os << "\n";
539 }
540 }
541 }
542 }
543
544 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
545 if (code_item != NULL) {
546 size_t i = 0;
547 while (i < code_item->insns_size_in_code_units_) {
548 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
549 os << StringPrintf("\t\t\t0x%04x: %s\n", i, instruction->DumpString(&dex_file).c_str());
550 i += instruction->SizeInCodeUnits();
551 }
552 }
553 }
554
555 void DumpCode(std::ostream& os, const OatFile::OatMethod& oat_method,
556 const DexFile::CodeItem* code_item) {
557 const void* code = oat_method.GetCode();
558 size_t code_size = oat_method.GetCodeSize();
559 if (code == NULL || code_size == 0) {
560 os << "\t\t\tNO CODE!\n";
561 return;
562 }
563 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code);
564 size_t offset = 0;
565 while (offset < code_size) {
566 DumpMappingAtOffset(os, oat_method, offset, false);
567 offset += disassembler_->Dump(os, native_pc + offset);
568 DumpMappingAtOffset(os, oat_method, offset, true);
569 DumpGcMapAtOffset(os, oat_method, code_item, offset);
570 }
571 }
572
573 void DumpInvokeStub(std::ostream& os, const OatFile::OatMethod& oat_method) {
574 const uint8_t* begin = reinterpret_cast<const uint8_t*>(oat_method.GetInvokeStub());
575 const uint8_t* end = begin + oat_method.GetInvokeStubSize();
576 disassembler_->Dump(os, begin, end);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800577 }
578
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700579 const std::string host_prefix_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800580 const OatFile& oat_file_;
581 std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800582 std::set<uint32_t> offsets_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800583 UniquePtr<Disassembler> disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700584};
585
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800586class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700587 public:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800588 explicit ImageDumper(std::ostream& os, const std::string& image_filename,
589 const std::string& host_prefix, Space& image_space,
Ian Rogersca190662012-06-26 15:45:57 -0700590 const ImageHeader& image_header)
591 : os_(os), image_filename_(image_filename), host_prefix_(host_prefix),
592 image_space_(image_space), image_header_(image_header) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700593
Ian Rogersb726dcb2012-09-05 08:57:23 -0700594 void Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800595 os_ << "MAGIC:\n";
596 os_ << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700597
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800598 os_ << "IMAGE BEGIN:\n";
599 os_ << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700600
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800601 os_ << "OAT CHECKSUM:\n";
602 os_ << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700603
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800604 os_ << "OAT BEGIN:\n";
605 os_ << reinterpret_cast<void*>(image_header_.GetOatBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700606
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800607 os_ << "OAT END:\n";
608 os_ << reinterpret_cast<void*>(image_header_.GetOatEnd()) << "\n\n";
609
610 os_ << "ROOTS:\n";
611 os_ << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700612 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700613 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
614 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700615 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800616 Object* image_root_object = image_header_.GetImageRoot(image_root);
617 os_ << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700618 if (image_root_object->IsObjectArray()) {
619 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
620 ObjectArray<Object>* image_root_object_array
621 = down_cast<ObjectArray<Object>*>(image_root_object);
622 // = image_root_object->AsObjectArray<Object>();
623 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800624 Object* value = image_root_object_array->Get(i);
625 if (value != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800626 os_ << "\t" << i << ": ";
Ian Rogersd5b32602012-02-26 16:40:04 -0800627 std::string summary;
628 PrettyObjectValue(summary, value->GetClass(), value);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800629 os_ << summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800630 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800631 os_ << StringPrintf("\t%d: null\n", i);
Ian Rogersd5b32602012-02-26 16:40:04 -0800632 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700633 }
634 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700635 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800636 os_ << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700637
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800638 os_ << "OAT LOCATION:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700639 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800640 Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation);
641 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800642 os_ << oat_location;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800643 if (!host_prefix_.empty()) {
644 oat_location = host_prefix_ + oat_location;
645 os_ << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700646 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800647 os_ << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800648 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700649 if (oat_file == NULL) {
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800650 os_ << "NOT FOUND\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700651 return;
652 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800653 os_ << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700654
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800655 stats_.oat_file_bytes = oat_file->Size();
656
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700657 oat_dumper_.reset(new OatDumper(host_prefix_, *oat_file));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800658
659 os_ << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700660
661 // Loop through all the image spaces and dump their objects.
662 Heap* heap = Runtime::Current()->GetHeap();
663 const Spaces& spaces = heap->GetSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -0700664 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700665 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700666 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700667 heap->FlushAllocStack();
668 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700669 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700670 // TODO: C++0x auto
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700671 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
672 Space* space = *it;
673 if (space->IsImageSpace()) {
674 ImageSpace* image_space = space->AsImageSpace();
675 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
676 os_ << "\n";
677 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700678 }
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700679 // Dump the large objects separately.
680 heap->GetLargeObjectsSpace()->GetLiveObjects()->Walk(ImageDumper::Callback, this);
681 os_ << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800682
683 os_ << "STATS:\n" << std::flush;
684 UniquePtr<File> file(OS::OpenFile(image_filename_.c_str(), false));
685 stats_.file_bytes = file->Length();
686 size_t header_bytes = sizeof(ImageHeader);
687 stats_.header_bytes = header_bytes;
688 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
689 stats_.alignment_bytes += alignment_bytes;
690 stats_.Dump(os_);
691 os_ << "\n";
692
693 os_ << std::flush;
694
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800695 oat_dumper_->Dump(os_);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700696 }
697
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700698 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700699 static void PrettyObjectValue(std::string& summary, Class* type, Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700700 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800701 CHECK(type != NULL);
702 if (value == NULL) {
703 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
704 } else if (type->IsStringClass()) {
705 String* string = value->AsString();
706 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
707 } else if (value->IsClass()) {
708 Class* klass = value->AsClass();
709 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
710 } else if (value->IsField()) {
711 Field* field = value->AsField();
712 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
713 } else if (value->IsMethod()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700714 AbstractMethod* method = value->AsMethod();
Ian Rogersd5b32602012-02-26 16:40:04 -0800715 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
716 } else {
717 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
718 }
719 }
720
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700721 static void PrintField(std::string& summary, Field* field, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700722 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800723 FieldHelper fh(field);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700724 const char* descriptor = fh.GetTypeDescriptor();
Ian Rogersd5b32602012-02-26 16:40:04 -0800725 StringAppendF(&summary, "\t%s: ", fh.GetName());
Ian Rogers48efc2b2012-08-27 17:20:31 -0700726 if (descriptor[0] != 'L' && descriptor[0] != '[') {
727 Class* type = fh.GetType();
728 if (type->IsPrimitiveLong()) {
729 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
730 } else if (type->IsPrimitiveDouble()) {
731 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
732 } else if (type->IsPrimitiveFloat()) {
733 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
734 } else {
735 DCHECK(type->IsPrimitive());
736 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
737 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800738 } else {
Ian Rogers48efc2b2012-08-27 17:20:31 -0700739 // Get the value, don't compute the type unless it is non-null as we don't want
740 // to cause class loading.
Ian Rogersd5b32602012-02-26 16:40:04 -0800741 Object* value = field->GetObj(obj);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700742 if (value == NULL) {
743 StringAppendF(&summary, "null %s\n", PrettyDescriptor(descriptor).c_str());
744 } else {
745 PrettyObjectValue(summary, fh.GetType(), value);
746 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800747 }
748 }
749
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700750 static void DumpFields(std::string& summary, Object* obj, Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700751 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800752 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
Mathieu Chartier66f19252012-09-18 08:57:04 -0700769 const void* GetOatCodeBegin(AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700770 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800771 Runtime* runtime = Runtime::Current();
772 const void* code = m->GetCode();
Ian Rogersfb6adba2012-03-04 21:51:51 -0800773 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800774 code = oat_dumper_->GetOatCode(m);
775 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700776 if (oat_dumper_->GetInstructionSet() == kThumb2) {
777 code = reinterpret_cast<void*>(reinterpret_cast<uint32_t>(code) & ~0x1);
778 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800779 return code;
780 }
781
Mathieu Chartier66f19252012-09-18 08:57:04 -0700782 uint32_t GetOatCodeSize(AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700783 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700784 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
Mathieu Chartier66f19252012-09-18 08:57:04 -0700791 const void* GetOatCodeEnd(AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700792 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700793 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetOatCodeBegin(m));
794 if (oat_code_begin == NULL) {
795 return NULL;
796 }
797 return oat_code_begin + GetOatCodeSize(m);
798 }
799
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700800 static void Callback(Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700801 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700802 DCHECK(obj != NULL);
803 DCHECK(arg != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800804 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700805 if (!state->InDumpSpace(obj)) {
806 return;
807 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700808
809 size_t object_bytes = obj->SizeOf();
810 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
811 state->stats_.object_bytes += object_bytes;
812 state->stats_.alignment_bytes += alignment_bytes;
813
Brian Carlstrom78128a62011-09-15 17:21:19 -0700814 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800815 Class* obj_class = obj->GetClass();
816 if (obj_class->IsArrayClass()) {
817 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
818 obj->AsArray()->GetLength());
819 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700820 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800821 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
822 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700823 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800824 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700825 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800826 } else if (obj->IsField()) {
827 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
828 PrettyField(obj->AsField()).c_str());
829 } else if (obj->IsMethod()) {
830 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
831 PrettyMethod(obj->AsMethod()).c_str());
832 } else if (obj_class->IsStringClass()) {
Elliott Hughes82914b62012-04-09 15:56:29 -0700833 StringAppendF(&summary, "%p: java.lang.String %s\n", obj,
834 PrintableString(obj->AsString()->ToModifiedUtf8()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800835 } else {
836 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
837 }
838 DumpFields(summary, obj, obj_class);
839 if (obj->IsObjectArray()) {
840 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
841 int32_t length = obj_array->GetLength();
842 for (int32_t i = 0; i < length; i++) {
843 Object* value = obj_array->Get(i);
844 size_t run = 0;
845 for (int32_t j = i + 1; j < length; j++) {
846 if (value == obj_array->Get(j)) {
847 run++;
848 } else {
849 break;
850 }
851 }
852 if (run == 0) {
853 StringAppendF(&summary, "\t%d: ", i);
854 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800855 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800856 i = i + run;
857 }
858 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
859 PrettyObjectValue(summary, value_class, value);
860 }
861 } else if (obj->IsClass()) {
862 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
863 if (sfields != NULL) {
864 summary += "\t\tSTATICS:\n";
865 for (int32_t i = 0; i < sfields->GetLength(); i++) {
866 Field* field = sfields->Get(i);
867 PrintField(summary, field, NULL);
868 }
869 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700870 } else if (obj->IsMethod()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700871 AbstractMethod* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700872 if (method->IsNative()) {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700873 DCHECK(method->GetNativeGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700874 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800875 bool first_occurrence;
876 size_t invoke_stub_size = state->ComputeOatSize(
877 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
878 if (first_occurrence) {
879 state->stats_.managed_to_native_code_bytes += invoke_stub_size;
880 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700881 const void* oat_code = state->GetOatCodeBegin(method);
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700882 uint32_t oat_code_size = state->GetOatCodeSize(method);
883 state->ComputeOatSize(oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800884 if (first_occurrence) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700885 state->stats_.native_to_managed_code_bytes += oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800886 }
887 if (oat_code != method->GetCode()) {
888 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
889 }
Ian Rogers19846512012-02-24 11:42:47 -0800890 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
891 method->IsResolutionMethod()) {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700892 DCHECK(method->GetNativeGcMap() == NULL) << 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)
Ian Rogers0c7abda2012-09-19 13:33:42 -0700896 DCHECK(method->GetNativeGcMap() != NULL) << PrettyMethod(method);
TDYa1273db52852012-04-01 15:11:43 -0700897#endif
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700898
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800899 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700900 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700901 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800902
Elliott Hughesa0e18062012-04-13 15:59:59 -0700903 bool first_occurrence;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700904 size_t gc_map_bytes = state->ComputeOatSize(method->GetNativeGcMap(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -0700905 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800906 state->stats_.gc_map_bytes += gc_map_bytes;
907 }
908
909 size_t pc_mapping_table_bytes =
Elliott Hughesa0e18062012-04-13 15:59:59 -0700910 state->ComputeOatSize(method->GetMappingTableRaw(), &first_occurrence);
911 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800912 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
913 }
914
915 size_t vmap_table_bytes =
Elliott Hughesa0e18062012-04-13 15:59:59 -0700916 state->ComputeOatSize(method->GetVmapTableRaw(), &first_occurrence);
917 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800918 state->stats_.vmap_table_bytes += vmap_table_bytes;
919 }
920
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700921 // TODO: compute invoke stub using length from oat file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800922 size_t invoke_stub_size = state->ComputeOatSize(
Elliott Hughesa0e18062012-04-13 15:59:59 -0700923 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
924 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800925 state->stats_.native_to_managed_code_bytes += invoke_stub_size;
926 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700927 const void* oat_code_begin = state->GetOatCodeBegin(method);
928 const void* oat_code_end = state->GetOatCodeEnd(method);
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700929 uint32_t oat_code_size = state->GetOatCodeSize(method);
Elliott Hughesa0e18062012-04-13 15:59:59 -0700930 state->ComputeOatSize(oat_code_begin, &first_occurrence);
931 if (first_occurrence) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700932 state->stats_.managed_code_bytes += oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -0700933 if (method->IsConstructor()) {
934 if (method->IsStatic()) {
935 state->stats_.class_initializer_code_bytes += oat_code_size;
936 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
937 state->stats_.large_initializer_code_bytes += oat_code_size;
938 }
939 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
940 state->stats_.large_method_code_bytes += oat_code_size;
941 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800942 }
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700943 state->stats_.managed_code_bytes_ignoring_deduplication += oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800944
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700945 StringAppendF(&summary, "\t\tOAT CODE: %p-%p\n", oat_code_begin, oat_code_end);
Ian Rogersd5b32602012-02-26 16:40:04 -0800946 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800947 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
948
949 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700950 vmap_table_bytes + invoke_stub_size + oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800951
952 double expansion =
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700953 static_cast<double>(oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800954 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700955 }
956 }
Elliott Hughesa0e18062012-04-13 15:59:59 -0700957 state->stats_.Update(ClassHelper(obj_class).GetDescriptor(), object_bytes);
Ian Rogersd5b32602012-02-26 16:40:04 -0800958
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700959 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700960 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700961
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800962 std::set<const void*> already_seen_;
963 // Compute the size of the given data within the oat file and whether this is the first time
964 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -0700965 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800966 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700967 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800968 already_seen_.insert(oat_data);
969 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700970 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800971 }
972 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700973 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700974
975 public:
976 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800977 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700978 size_t file_bytes;
979
980 size_t header_bytes;
981 size_t object_bytes;
982 size_t alignment_bytes;
983
984 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800985 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700986 size_t managed_to_native_code_bytes;
987 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -0700988 size_t class_initializer_code_bytes;
989 size_t large_initializer_code_bytes;
990 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700991
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800992 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700993 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800994 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700995
996 size_t dex_instruction_bytes;
997
Mathieu Chartier66f19252012-09-18 08:57:04 -0700998 std::vector<AbstractMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800999 std::vector<size_t> method_outlier_size;
1000 std::vector<double> method_outlier_expansion;
1001
1002 explicit Stats()
1003 : oat_file_bytes(0),
1004 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001005 header_bytes(0),
1006 object_bytes(0),
1007 alignment_bytes(0),
1008 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001009 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001010 managed_to_native_code_bytes(0),
1011 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001012 class_initializer_code_bytes(0),
1013 large_initializer_code_bytes(0),
1014 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001015 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001016 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001017 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001018 dex_instruction_bytes(0) {}
1019
Elliott Hughesa0e18062012-04-13 15:59:59 -07001020 struct SizeAndCount {
1021 SizeAndCount(size_t bytes, size_t count) : bytes(bytes), count(count) {}
1022 size_t bytes;
1023 size_t count;
1024 };
1025 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1026 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001027
Elliott Hughesa0e18062012-04-13 15:59:59 -07001028 void Update(const std::string& descriptor, size_t object_bytes) {
1029 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1030 if (it != sizes_and_counts.end()) {
1031 it->second.bytes += object_bytes;
1032 it->second.count += 1;
1033 } else {
1034 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes, 1));
1035 }
1036 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001037
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001038 double PercentOfOatBytes(size_t size) {
1039 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1040 }
1041
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001042 double PercentOfFileBytes(size_t size) {
1043 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1044 }
1045
1046 double PercentOfObjectBytes(size_t size) {
1047 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1048 }
1049
Mathieu Chartier66f19252012-09-18 08:57:04 -07001050 void ComputeOutliers(size_t total_size, double expansion, AbstractMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001051 method_outlier_size.push_back(total_size);
1052 method_outlier_expansion.push_back(expansion);
1053 method_outlier.push_back(method);
1054 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001055
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001056 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001057 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001058 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) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001098 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001099 first = false;
1100 }
1101 os << "\t" << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001102 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001103 method_outlier_size[j] = 0; // don't consider this method again
1104 dumped_values++;
1105 }
1106 }
1107 }
1108 }
1109 }
1110 if (skipped_values > 0) {
1111 os << "\t... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001112 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001113 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001114 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001115
1116 // Dump methods whose expansion is a certain number of standard deviations from the mean
1117 dumped_values = 0;
1118 skipped_values = 0;
1119 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1120 double cur_expansion_variance = i * i * expansion_variance;
1121 bool first = true;
1122 for (size_t j = 0; j < n; j++) {
1123 double cur_expansion = method_outlier_expansion[j];
1124 if (cur_expansion > expansion_mean) {
1125 size_t cur_var = cur_expansion - expansion_mean;
1126 cur_var = cur_var * cur_var;
1127 if (cur_var > cur_expansion_variance) {
1128 if (dumped_values > 20) {
1129 if (i == 1) {
1130 skipped_values++;
1131 } else {
1132 i = 2; // jump to counting for 1 standard deviation
1133 break;
1134 }
1135 } else {
1136 if (first) {
1137 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001138 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001139 first = false;
1140 }
1141 os << "\t" << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001142 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001143 method_outlier_expansion[j] = 0.0; // don't consider this method again
1144 dumped_values++;
1145 }
1146 }
1147 }
1148 }
1149 }
1150 if (skipped_values > 0) {
1151 os << "\t... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001152 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001153 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001154 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001155 }
1156
Ian Rogersb726dcb2012-09-05 08:57:23 -07001157 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001158 os << "\tart_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1159 << "\tart_file_bytes = header_bytes + object_bytes + alignment_bytes\n"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001160 << StringPrintf("\theader_bytes = %8zd (%2.0f%% of art file bytes)\n"
1161 "\tobject_bytes = %8zd (%2.0f%% of art file bytes)\n"
Elliott Hughesc073b072012-05-24 19:29:17 -07001162 "\talignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001163 header_bytes, PercentOfFileBytes(header_bytes),
1164 object_bytes, PercentOfFileBytes(object_bytes),
1165 alignment_bytes, PercentOfFileBytes(alignment_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001166 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001167
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001168 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
1169
Elliott Hughesa0e18062012-04-13 15:59:59 -07001170 os << "\tobject_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001171 size_t object_bytes_total = 0;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001172 typedef SizeAndCountTable::const_iterator It; // TODO: C++0x auto
1173 for (It it = sizes_and_counts.begin(), end = sizes_and_counts.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -08001174 const std::string& descriptor(it->first);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001175 double average = static_cast<double>(it->second.bytes) / static_cast<double>(it->second.count);
1176 double percent = PercentOfObjectBytes(it->second.bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001177 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001178 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
1179 descriptor.c_str(), it->second.bytes, it->second.count,
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001180 average, percent);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001181 object_bytes_total += it->second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001182 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001183 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001184 CHECK_EQ(object_bytes, object_bytes_total);
1185
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001186 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1187 "\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers0d2d3782012-04-10 11:09:18 -07001188 "\tnative_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1189 "\tclass_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1190 "\tlarge_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Elliott Hughesc073b072012-05-24 19:29:17 -07001191 "\tlarge_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001192 managed_code_bytes, PercentOfOatBytes(managed_code_bytes),
1193 managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001194 native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes),
1195 class_initializer_code_bytes, PercentOfOatBytes(class_initializer_code_bytes),
1196 large_initializer_code_bytes, PercentOfOatBytes(large_initializer_code_bytes),
1197 large_method_code_bytes, PercentOfOatBytes(large_method_code_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001198 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001199
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001200 os << StringPrintf("\tgc_map_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1201 "\tpc_mapping_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
Elliott Hughesc073b072012-05-24 19:29:17 -07001202 "\tvmap_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001203 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1204 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1205 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001206 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001207
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001208 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes)
Elliott Hughesc073b072012-05-24 19:29:17 -07001209 << StringPrintf("\tmanaged_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
1210 static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes),
1211 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001212 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001213 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001214
1215 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001216 }
1217 } stats_;
1218
1219 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001220 enum {
1221 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1222 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1223 kLargeConstructorDexBytes = 4000,
1224 // Number of bytes for a method to be considered large. Based on the 4000 basic block
1225 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1226 kLargeMethodDexBytes = 16000
1227 };
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001228 UniquePtr<OatDumper> oat_dumper_;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001229 std::ostream& os_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001230 const std::string image_filename_;
1231 const std::string host_prefix_;
1232 Space& image_space_;
1233 const ImageHeader& image_header_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001234
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001235 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001236};
1237
Elliott Hughes72395bf2012-04-24 13:45:26 -07001238static int oatdump(int argc, char** argv) {
Elliott Hughes0d39c122012-06-06 16:41:17 -07001239 InitLogging(argv);
Elliott Hughes72395bf2012-04-24 13:45:26 -07001240
Brian Carlstrom78128a62011-09-15 17:21:19 -07001241 // Skip over argv[0].
1242 argv++;
1243 argc--;
1244
1245 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001246 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001247 usage();
1248 }
1249
Brian Carlstromaded5f72011-10-07 17:15:04 -07001250 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001251 const char* image_filename = NULL;
1252 const char* boot_image_filename = NULL;
Logan Chien0cc6ab62012-03-20 22:57:52 +08001253 std::string elf_filename_prefix;
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001254 UniquePtr<std::string> host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001255 std::ostream* os = &std::cout;
1256 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001257
1258 for (int i = 0; i < argc; i++) {
1259 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001260 if (option.starts_with("--oat-file=")) {
1261 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001262 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001263 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001264 } else if (option.starts_with("--boot-image=")) {
1265 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001266 } else if (option.starts_with("--host-prefix=")) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001267 host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001268 } else if (option.starts_with("--output=")) {
1269 const char* filename = option.substr(strlen("--output=")).data();
1270 out.reset(new std::ofstream(filename));
1271 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001272 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001273 usage();
1274 }
1275 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001276 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001277 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001278 usage();
1279 }
1280 }
1281
Brian Carlstromaded5f72011-10-07 17:15:04 -07001282 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001283 fprintf(stderr, "Either --image or --oat must be specified\n");
1284 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001285 }
1286
Brian Carlstromaded5f72011-10-07 17:15:04 -07001287 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001288 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1289 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001290 }
1291
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001292 if (host_prefix.get() == NULL) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001293 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
1294 if (android_product_out != NULL) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001295 host_prefix.reset(new std::string(android_product_out));
1296 } else {
1297 host_prefix.reset(new std::string(""));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001298 }
1299 }
1300
Brian Carlstromaded5f72011-10-07 17:15:04 -07001301 if (oat_filename != NULL) {
Logan Chien0c717dd2012-03-28 18:31:07 +08001302 OatFile* oat_file =
1303 OatFile::Open(oat_filename, oat_filename, NULL, OatFile::kRelocNone);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001304 if (oat_file == NULL) {
1305 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
1306 return EXIT_FAILURE;
1307 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001308 OatDumper oat_dumper(*host_prefix.get(), *oat_file);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001309 oat_dumper.Dump(*os);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001310 return EXIT_SUCCESS;
1311 }
1312
Brian Carlstrom78128a62011-09-15 17:21:19 -07001313 Runtime::Options options;
1314 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001315 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001316 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001317 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001318 if (boot_image_filename != NULL) {
1319 boot_image_option += "-Ximage:";
1320 boot_image_option += boot_image_filename;
1321 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001322 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001323 if (image_filename != NULL) {
1324 image_option += "-Ximage:";
1325 image_option += image_filename;
1326 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1327 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001328
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001329 if (!host_prefix->empty()) {
1330 options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001331 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001332
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001333 if (!Runtime::Create(options, false)) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001334 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001335 return EXIT_FAILURE;
1336 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001337 UniquePtr<Runtime> runtime(Runtime::Current());
1338 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
1339 // give it away now and then switch to a more managable ScopedObjectAccess.
1340 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
1341 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001342
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001343 Heap* heap = Runtime::Current()->GetHeap();
Brian Carlstromfddf6f62012-03-15 16:56:45 -07001344 ImageSpace* image_space = heap->GetImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001345 CHECK(image_space != NULL);
1346 const ImageHeader& image_header = image_space->GetImageHeader();
1347 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001348 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001349 return EXIT_FAILURE;
1350 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001351 ImageDumper image_dumper(*os, image_filename, *host_prefix.get(), *image_space, image_header);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001352 image_dumper.Dump();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001353 return EXIT_SUCCESS;
1354}
1355
1356} // namespace art
1357
1358int main(int argc, char** argv) {
1359 return art::oatdump(argc, argv);
1360}