blob: 866c180cc5093b01c012a2ed7383d87380574dc4 [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"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070029#include "gc/large_object_space.h"
30#include "gc/space.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"
Elliott Hughesa0e18062012-04-13 15:59:59 -070036#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070037#include "scoped_thread_state_change.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070038#include "stringpiece.h"
Ian Rogers0c7abda2012-09-19 13:33:42 -070039#include "gc_map.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070040
41namespace art {
42
43static void usage() {
44 fprintf(stderr,
45 "Usage: oatdump [options] ...\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080046 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n"
47 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070048 "\n");
49 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080050 " --oat-file=<file.oat>: specifies an input oat filename.\n"
TDYa127a0a2a6c2012-10-16 22:47:38 -070051 " Example: --oat-file=/system/framework/boot.oat\n"
Brian Carlstromaded5f72011-10-07 17:15:04 -070052 "\n");
53 fprintf(stderr,
54 " --image=<file.art>: specifies an input image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080055 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070056 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070057 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070058 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080059 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070060 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070061 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070062 " --host-prefix may be used to translate host paths to target paths during\n"
63 " cross compilation.\n"
64 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstromfe487d02012-02-29 18:49:16 -080065 " Default: $ANDROID_PRODUCT_OUT\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070066 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070067 fprintf(stderr,
68 " --output=<file> may be used to send the output to a file.\n"
69 " Example: --output=/tmp/oatdump.txt\n"
70 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070071 exit(EXIT_FAILURE);
72}
73
Ian Rogersff1ed472011-09-20 13:46:24 -070074const char* image_roots_descriptions_[] = {
Brian Carlstrom78128a62011-09-15 17:21:19 -070075 "kJniStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070076 "kAbstractMethodErrorStubArray",
Ian Rogersad25ac52011-10-04 19:13:33 -070077 "kStaticResolutionStubArray",
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070078 "kUnknownMethodResolutionStubArray",
Ian Rogers19846512012-02-24 11:42:47 -080079 "kResolutionMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070080 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070081 "kRefsOnlySaveMethod",
82 "kRefsAndArgsSaveMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070083 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070084 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070085 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070086};
87
Elliott Hughese3c845c2012-02-28 17:23:01 -080088class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -070089 public:
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070090 explicit OatDumper(const std::string& host_prefix, const OatFile& oat_file)
91 : host_prefix_(host_prefix),
92 oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -080093 oat_dex_files_(oat_file.GetOatDexFiles()),
94 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet())) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080095 AddAllOffsets();
96 }
97
98 void Dump(std::ostream& os) {
99 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700100
101 os << "MAGIC:\n";
102 os << oat_header.GetMagic() << "\n\n";
103
104 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800105 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700106
Elliott Hughesa72ec822012-03-05 17:12:22 -0800107 os << "INSTRUCTION SET:\n";
108 os << oat_header.GetInstructionSet() << "\n\n";
109
Brian Carlstromaded5f72011-10-07 17:15:04 -0700110 os << "DEX FILE COUNT:\n";
111 os << oat_header.GetDexFileCount() << "\n\n";
112
113 os << "EXECUTABLE OFFSET:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800114 os << StringPrintf("0x%08x\n\n", oat_header.GetExecutableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700115
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700116 os << "IMAGE FILE LOCATION CHECKSUM:\n";
117 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationChecksum());
118
119 os << "IMAGE FILE LOCATION:\n";
120 const std::string image_file_location(oat_header.GetImageFileLocation());
121 os << image_file_location;
122 if (!image_file_location.empty() && !host_prefix_.empty()) {
123 os << " (" << host_prefix_ << image_file_location << ")";
124 }
125 os << "\n\n";
126
Ian Rogers30fab402012-01-23 15:43:46 -0800127 os << "BEGIN:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800128 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700129
Ian Rogers30fab402012-01-23 15:43:46 -0800130 os << "END:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800131 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700132
133 os << std::flush;
134
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800135 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
136 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Brian Carlstromaded5f72011-10-07 17:15:04 -0700137 CHECK(oat_dex_file != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800138 DumpOatDexFile(os, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700139 }
140 }
141
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800142 size_t ComputeSize(const void* oat_data) {
143 if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() ||
144 reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) {
145 return 0; // Address not in oat file
146 }
147 uint32_t begin_offset = reinterpret_cast<size_t>(oat_data) -
148 reinterpret_cast<size_t>(oat_file_.Begin());
149 typedef std::set<uint32_t>::iterator It;
150 It it = offsets_.upper_bound(begin_offset);
151 CHECK(it != offsets_.end());
152 uint32_t end_offset = *it;
153 return end_offset - begin_offset;
154 }
155
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700156 InstructionSet GetInstructionSet() {
157 return oat_file_.GetOatHeader().GetInstructionSet();
158 }
159
Mathieu Chartier66f19252012-09-18 08:57:04 -0700160 const void* GetOatCode(AbstractMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800161 MethodHelper mh(m);
162 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
163 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
164 CHECK(oat_dex_file != NULL);
165 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
166 if (dex_file.get() != NULL) {
167 uint32_t class_def_index;
168 bool found = dex_file->FindClassDefIndex(mh.GetDeclaringClassDescriptor(), class_def_index);
169 if (found) {
170 const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_index);
171 CHECK(oat_class != NULL);
172 size_t method_index = m->GetMethodIndex();
173 return oat_class->GetOatMethod(method_index).GetCode();
174 }
175 }
176 }
177 return NULL;
178 }
179
Brian Carlstromaded5f72011-10-07 17:15:04 -0700180 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800181 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800182 // We don't know the length of the code for each method, but we need to know where to stop
183 // when disassembling. What we do know is that a region of code will be followed by some other
184 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
185 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800186 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
187 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800188 CHECK(oat_dex_file != NULL);
189 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
190 if (dex_file.get() == NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800191 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800192 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800193 offsets_.insert(reinterpret_cast<uint32_t>(&dex_file->GetHeader()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800194 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
195 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
196 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index));
197 const byte* class_data = dex_file->GetClassData(class_def);
198 if (class_data != NULL) {
199 ClassDataItemIterator it(*dex_file, class_data);
200 SkipAllFields(it);
201 uint32_t class_method_index = 0;
202 while (it.HasNextDirectMethod()) {
203 AddOffsets(oat_class->GetOatMethod(class_method_index++));
204 it.Next();
205 }
206 while (it.HasNextVirtualMethod()) {
207 AddOffsets(oat_class->GetOatMethod(class_method_index++));
208 it.Next();
209 }
210 }
211 }
212 }
213
214 // If the last thing in the file is code for a method, there won't be an offset for the "next"
215 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
216 // for the end of the file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800217 offsets_.insert(static_cast<uint32_t>(oat_file_.End() - oat_file_.Begin()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800218 }
219
220 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800221 uint32_t code_offset = oat_method.GetCodeOffset();
222 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
223 code_offset &= ~0x1;
224 }
225 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800226 offsets_.insert(oat_method.GetMappingTableOffset());
227 offsets_.insert(oat_method.GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700228 offsets_.insert(oat_method.GetNativeGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800229 offsets_.insert(oat_method.GetInvokeStubOffset());
230 }
231
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800232 void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700233 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800234 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800235 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800236 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile());
237 if (dex_file.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700238 os << "NOT FOUND\n\n";
239 return;
240 }
241 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
242 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
243 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700244 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
245 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800246 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800247 << oat_class->GetStatus() << ")\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800248 DumpOatClass(os, *oat_class.get(), *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700249 }
250
251 os << std::flush;
252 }
253
Elliott Hughese3c845c2012-02-28 17:23:01 -0800254 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700255 while (it.HasNextStaticField()) {
256 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700257 }
Ian Rogers0571d352011-11-03 19:51:38 -0700258 while (it.HasNextInstanceField()) {
259 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700260 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800261 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700262
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800263 void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
264 const DexFile::ClassDef& class_def) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800265 const byte* class_data = dex_file.GetClassData(class_def);
266 if (class_data == NULL) { // empty class such as a marker interface?
267 return;
268 }
269 ClassDataItemIterator it(dex_file, class_data);
270 SkipAllFields(it);
271
272 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700273 while (it.HasNextDirectMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800274 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800275 DumpOatMethod(os, class_method_index, oat_method, dex_file,
276 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800277 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700278 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700279 }
Ian Rogers0571d352011-11-03 19:51:38 -0700280 while (it.HasNextVirtualMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800281 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800282 DumpOatMethod(os, class_method_index, oat_method, dex_file,
283 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800284 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700285 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700286 }
Ian Rogers0571d352011-11-03 19:51:38 -0700287 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700288 os << std::flush;
289 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800290
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800291 void DumpOatMethod(std::ostream& os, uint32_t class_method_index,
Elliott Hughese3c845c2012-02-28 17:23:01 -0800292 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
293 uint32_t dex_method_idx, const DexFile::CodeItem* code_item) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700294 os << StringPrintf("\t%d: %s (dex_method_idx=%d)\n",
295 class_method_index, PrettyMethod(dex_method_idx, dex_file, true).c_str(),
296 dex_method_idx);
Ian Rogersb23a7722012-10-09 16:54:26 -0700297 os << StringPrintf("\t\tframe_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
298 os << StringPrintf("\t\tcore_spill_mask: 0x%08x\n", oat_method.GetCoreSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800299 DumpSpillMask(os, oat_method.GetCoreSpillMask(), false);
Ian Rogersb23a7722012-10-09 16:54:26 -0700300 os << StringPrintf("\n\t\tfp_spill_mask: 0x%08x\n", oat_method.GetFpSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800301 DumpSpillMask(os, oat_method.GetFpSpillMask(), true);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800302 os << StringPrintf("\t\tvmap_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800303 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Ian Rogersb23a7722012-10-09 16:54:26 -0700304 DumpVmap(os, oat_method);
305 const bool kDumpRawMappingTable = false;
306 if (kDumpRawMappingTable) {
307 os << StringPrintf("\t\tmapping_table: %p (offset=0x%08x)\n",
308 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
309 DumpMappingTable(os, oat_method);
310 }
311 const bool kDumpRawGcMap = false;
312 if (kDumpRawGcMap) {
313 os << StringPrintf("\t\tgc_map: %p (offset=0x%08x)\n",
314 oat_method.GetNativeGcMap(), oat_method.GetNativeGcMapOffset());
315 DumpGcMap(os, oat_method, code_item);
316 }
317 os << "\t\tDEX CODE:\n";
318 DumpDexCode(os, dex_file, code_item);
Logan Chien971bf3f2012-05-01 15:47:55 +0800319 os << StringPrintf("\t\tCODE: %p (offset=0x%08x size=%d)%s\n",
Elliott Hughes77405792012-03-15 15:22:12 -0700320 oat_method.GetCode(),
321 oat_method.GetCodeOffset(),
322 oat_method.GetCodeSize(),
323 oat_method.GetCode() != NULL ? "..." : "");
Ian Rogersb23a7722012-10-09 16:54:26 -0700324 DumpCode(os, oat_method, code_item);
Logan Chien971bf3f2012-05-01 15:47:55 +0800325 os << StringPrintf("\t\tINVOKE STUB: %p (offset=0x%08x size=%d)%s\n",
Elliott Hughes77405792012-03-15 15:22:12 -0700326 oat_method.GetInvokeStub(),
327 oat_method.GetInvokeStubOffset(),
328 oat_method.GetInvokeStubSize(),
329 oat_method.GetInvokeStub() != NULL ? "..." : "");
Ian Rogersb23a7722012-10-09 16:54:26 -0700330 DumpInvokeStub(os, oat_method);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700331 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800332
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800333 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
334 if (spill_mask == 0) {
335 return;
336 }
337 os << " (";
338 for (size_t i = 0; i < 32; i++) {
339 if ((spill_mask & (1 << i)) != 0) {
340 if (is_float) {
341 os << "fr" << i;
342 } else {
343 os << "r" << i;
344 }
345 spill_mask ^= 1 << i; // clear bit
346 if (spill_mask != 0) {
347 os << ", ";
348 } else {
349 break;
350 }
351 }
352 }
353 os << ")";
354 }
355
Ian Rogersb23a7722012-10-09 16:54:26 -0700356 void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) {
357 const uint16_t* raw_table = oat_method.GetVmapTable();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800358 if (raw_table == NULL) {
359 return;
360 }
361 const VmapTable vmap_table(raw_table);
362 bool first = true;
363 os << "\t\t\t";
364 for (size_t i = 0; i < vmap_table.size(); i++) {
365 uint16_t dex_reg = vmap_table[i];
366 size_t matches = 0;
367 size_t spill_shifts = 0;
Ian Rogersb23a7722012-10-09 16:54:26 -0700368 uint32_t spill_mask = oat_method.GetCoreSpillMask();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800369 bool processing_fp = false;
370 while (matches != (i + 1)) {
371 if (spill_mask == 0) {
372 CHECK(!processing_fp);
Ian Rogersb23a7722012-10-09 16:54:26 -0700373 spill_mask = oat_method.GetFpSpillMask();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800374 processing_fp = true;
375 }
376 matches += spill_mask & 1; // Add 1 if the low bit is set
377 spill_mask >>= 1;
378 spill_shifts++;
379 }
380 size_t arm_reg = spill_shifts - 1; // wind back one as we want the last match
381 os << (first ? "v" : ", v") << dex_reg;
382 if (arm_reg < 16) {
383 os << "/r" << arm_reg;
384 } else {
385 os << "/fr" << (arm_reg - 16);
386 }
387 if (first) {
388 first = false;
389 }
390 }
Elliott Hughesc073b072012-05-24 19:29:17 -0700391 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800392 }
393
Ian Rogersb23a7722012-10-09 16:54:26 -0700394 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
395 const DexFile::CodeItem* code_item, size_t reg) {
396 const uint16_t* raw_table = oat_method.GetVmapTable();
397 if (raw_table != NULL) {
398 const VmapTable vmap_table(raw_table);
399 uint32_t vmap_offset;
400 if (vmap_table.IsInContext(reg, vmap_offset)) {
401 // Compute the register we need to load from the context
402 uint32_t spill_mask = oat_method.GetCoreSpillMask();
403 CHECK_LT(vmap_offset, static_cast<uint32_t>(__builtin_popcount(spill_mask)));
404 uint32_t matches = 0;
405 uint32_t spill_shifts = 0;
406 while (matches != (vmap_offset + 1)) {
407 DCHECK_NE(spill_mask, 0u);
408 matches += spill_mask & 1; // Add 1 if the low bit is set
409 spill_mask >>= 1;
410 spill_shifts++;
411 }
412 spill_shifts--; // wind back one as we want the last match
413 os << "r" << spill_shifts;
414 } else {
415 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
416 oat_method.GetFpSpillMask(),
417 oat_method.GetFrameSizeInBytes(), reg);
418 os << "[sp + #" << offset << "]";
419 }
420 }
421 }
422
423 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
424 const DexFile::CodeItem* code_item) {
425 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800426 if (gc_map_raw == NULL) {
427 return;
428 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700429 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogersb23a7722012-10-09 16:54:26 -0700430 const void* code = oat_method.GetCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800431 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
Ian Rogersbb6723f2012-09-21 09:49:06 -0700432 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) +
433 map.GetNativePcOffset(entry);
434 os << StringPrintf("\t\t\t%p", native_pc);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800435 size_t num_regs = map.RegWidth() * 8;
436 const uint8_t* reg_bitmap = map.GetBitMap(entry);
437 bool first = true;
438 for (size_t reg = 0; reg < num_regs; reg++) {
439 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
440 if (first) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700441 os << " v" << reg << " (";
442 DescribeVReg(os, oat_method, code_item, reg);
443 os << ")";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800444 first = false;
445 } else {
Ian Rogersb23a7722012-10-09 16:54:26 -0700446 os << ", v" << reg << " (";
447 DescribeVReg(os, oat_method, code_item, reg);
448 os << ")";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800449 }
450 }
451 }
Elliott Hughesc073b072012-05-24 19:29:17 -0700452 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800453 }
454 }
455
456 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800457 const uint32_t* raw_table = oat_method.GetMappingTable();
458 const void* code = oat_method.GetCode();
459 if (raw_table == NULL || code == NULL) {
460 return;
461 }
462
buzbee4d7a1892012-10-03 11:03:37 -0700463 ++raw_table;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800464 uint32_t length = *raw_table;
465 ++raw_table;
Bill Buzbeea5b30242012-09-28 07:19:44 -0700466 uint32_t pc_to_dex_entries = *raw_table;
467 ++raw_table;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800468
Ian Rogersb23a7722012-10-09 16:54:26 -0700469 os << "\t\tsuspend point mappings {";
Elliott Hughese3c845c2012-02-28 17:23:01 -0800470 for (size_t i = 0; i < length; i += 2) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800471 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800472 uint32_t dex_pc = raw_table[i + 1];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800473 os << StringPrintf("%p -> 0x%04x", native_pc, dex_pc);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700474 if (i + 2 == pc_to_dex_entries) {
475 // Separate the pc -> dex from dex -> pc sections
Ian Rogersb23a7722012-10-09 16:54:26 -0700476 os << "}\n\t\tcatch entry mappings {";
Bill Buzbeea5b30242012-09-28 07:19:44 -0700477 } else if (i + 2 < length) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800478 os << ", ";
479 }
480 }
Ian Rogersb23a7722012-10-09 16:54:26 -0700481 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800482 }
483
Ian Rogersb23a7722012-10-09 16:54:26 -0700484 void DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method, size_t offset,
485 bool suspend_point_mapping) {
486 const uint32_t* raw_table = oat_method.GetMappingTable();
487 if (raw_table != NULL) {
488 ++raw_table;
489 uint32_t length = *raw_table;
490 ++raw_table;
491 uint32_t pc_to_dex_entries = *raw_table;
492 ++raw_table;
493 size_t start, end;
494 if (suspend_point_mapping) {
495 start = 0;
496 end = pc_to_dex_entries;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800497 } else {
Ian Rogersb23a7722012-10-09 16:54:26 -0700498 start = pc_to_dex_entries;
499 end = length;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800500 }
Ian Rogersb23a7722012-10-09 16:54:26 -0700501 for (size_t i = start; i < end; i += 2) {
502 if (offset == raw_table[i]) {
503 if (suspend_point_mapping) {
Ian Rogersae4b0a12012-10-09 17:32:36 -0700504 os << "\t\t\tsuspend point dex PC: 0x";
Ian Rogersb23a7722012-10-09 16:54:26 -0700505 } else {
Ian Rogersae4b0a12012-10-09 17:32:36 -0700506 os << "\t\t\tcatch entry dex PC: 0x";
Ian Rogersb23a7722012-10-09 16:54:26 -0700507 }
Ian Rogers9958daa2012-10-12 00:23:38 -0700508 os << std::hex << raw_table[i + 1] << std::dec << "\n";
Ian Rogersb23a7722012-10-09 16:54:26 -0700509 return;
510 }
511 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800512 }
Ian Rogersb23a7722012-10-09 16:54:26 -0700513 }
514
515 void DumpGcMapAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
516 const DexFile::CodeItem* code_item, size_t offset) {
517 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
518 if (gc_map_raw != NULL) {
519 NativePcOffsetToReferenceMap map(gc_map_raw);
520 if (map.HasEntry(offset)) {
521 size_t num_regs = map.RegWidth() * 8;
522 const uint8_t* reg_bitmap = map.FindBitMap(offset);
523 bool first = true;
524 for (size_t reg = 0; reg < num_regs; reg++) {
525 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
526 if (first) {
527 os << "\t\t\tGC map objects: v" << reg << " (";
528 DescribeVReg(os, oat_method, code_item, reg);
529 os << ")";
530 first = false;
531 } else {
532 os << ", v" << reg << " (";
533 DescribeVReg(os, oat_method, code_item, reg);
534 os << ")";
535 }
536 }
537 }
538 if (!first) {
539 os << "\n";
540 }
541 }
542 }
543 }
544
545 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
546 if (code_item != NULL) {
547 size_t i = 0;
548 while (i < code_item->insns_size_in_code_units_) {
549 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Ian Rogersae4b0a12012-10-09 17:32:36 -0700550 os << StringPrintf("\t\t\t0x%04zx: %s\n", i, instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -0700551 i += instruction->SizeInCodeUnits();
552 }
553 }
554 }
555
556 void DumpCode(std::ostream& os, const OatFile::OatMethod& oat_method,
557 const DexFile::CodeItem* code_item) {
558 const void* code = oat_method.GetCode();
559 size_t code_size = oat_method.GetCodeSize();
560 if (code == NULL || code_size == 0) {
561 os << "\t\t\tNO CODE!\n";
562 return;
563 }
564 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code);
565 size_t offset = 0;
566 while (offset < code_size) {
567 DumpMappingAtOffset(os, oat_method, offset, false);
568 offset += disassembler_->Dump(os, native_pc + offset);
569 DumpMappingAtOffset(os, oat_method, offset, true);
570 DumpGcMapAtOffset(os, oat_method, code_item, offset);
571 }
572 }
573
574 void DumpInvokeStub(std::ostream& os, const OatFile::OatMethod& oat_method) {
575 const uint8_t* begin = reinterpret_cast<const uint8_t*>(oat_method.GetInvokeStub());
576 const uint8_t* end = begin + oat_method.GetInvokeStubSize();
577 disassembler_->Dump(os, begin, end);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800578 }
579
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700580 const std::string host_prefix_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800581 const OatFile& oat_file_;
582 std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800583 std::set<uint32_t> offsets_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800584 UniquePtr<Disassembler> disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700585};
586
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800587class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700588 public:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800589 explicit ImageDumper(std::ostream& os, const std::string& image_filename,
590 const std::string& host_prefix, Space& image_space,
Ian Rogersca190662012-06-26 15:45:57 -0700591 const ImageHeader& image_header)
592 : os_(os), image_filename_(image_filename), host_prefix_(host_prefix),
593 image_space_(image_space), image_header_(image_header) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700594
Ian Rogersb726dcb2012-09-05 08:57:23 -0700595 void Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800596 os_ << "MAGIC:\n";
597 os_ << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700598
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800599 os_ << "IMAGE BEGIN:\n";
600 os_ << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700601
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800602 os_ << "OAT CHECKSUM:\n";
603 os_ << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700604
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800605 os_ << "OAT BEGIN:\n";
606 os_ << reinterpret_cast<void*>(image_header_.GetOatBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700607
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800608 os_ << "OAT END:\n";
609 os_ << reinterpret_cast<void*>(image_header_.GetOatEnd()) << "\n\n";
610
611 os_ << "ROOTS:\n";
612 os_ << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700613 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700614 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
615 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700616 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800617 Object* image_root_object = image_header_.GetImageRoot(image_root);
618 os_ << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700619 if (image_root_object->IsObjectArray()) {
620 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
621 ObjectArray<Object>* image_root_object_array
622 = down_cast<ObjectArray<Object>*>(image_root_object);
623 // = image_root_object->AsObjectArray<Object>();
624 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800625 Object* value = image_root_object_array->Get(i);
626 if (value != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800627 os_ << "\t" << i << ": ";
Ian Rogersd5b32602012-02-26 16:40:04 -0800628 std::string summary;
629 PrettyObjectValue(summary, value->GetClass(), value);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800630 os_ << summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800631 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800632 os_ << StringPrintf("\t%d: null\n", i);
Ian Rogersd5b32602012-02-26 16:40:04 -0800633 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700634 }
635 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700636 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800637 os_ << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700638
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800639 os_ << "OAT LOCATION:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700640 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800641 Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation);
642 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800643 os_ << oat_location;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800644 if (!host_prefix_.empty()) {
645 oat_location = host_prefix_ + oat_location;
646 os_ << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700647 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800648 os_ << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800649 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700650 if (oat_file == NULL) {
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800651 os_ << "NOT FOUND\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700652 return;
653 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800654 os_ << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700655
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800656 stats_.oat_file_bytes = oat_file->Size();
657
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700658 oat_dumper_.reset(new OatDumper(host_prefix_, *oat_file));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800659
660 os_ << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700661
662 // Loop through all the image spaces and dump their objects.
663 Heap* heap = Runtime::Current()->GetHeap();
664 const Spaces& spaces = heap->GetSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -0700665 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700666 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700667 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700668 heap->FlushAllocStack();
669 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700670 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700671 // TODO: C++0x auto
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700672 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
673 Space* space = *it;
674 if (space->IsImageSpace()) {
675 ImageSpace* image_space = space->AsImageSpace();
676 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
677 os_ << "\n";
678 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700679 }
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700680 // Dump the large objects separately.
681 heap->GetLargeObjectsSpace()->GetLiveObjects()->Walk(ImageDumper::Callback, this);
682 os_ << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800683
684 os_ << "STATS:\n" << std::flush;
685 UniquePtr<File> file(OS::OpenFile(image_filename_.c_str(), false));
686 stats_.file_bytes = file->Length();
687 size_t header_bytes = sizeof(ImageHeader);
688 stats_.header_bytes = header_bytes;
689 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
690 stats_.alignment_bytes += alignment_bytes;
691 stats_.Dump(os_);
692 os_ << "\n";
693
694 os_ << std::flush;
695
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800696 oat_dumper_->Dump(os_);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700697 }
698
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700699 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700700 static void PrettyObjectValue(std::string& summary, Class* type, Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700701 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800702 CHECK(type != NULL);
703 if (value == NULL) {
704 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
705 } else if (type->IsStringClass()) {
706 String* string = value->AsString();
707 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
708 } else if (value->IsClass()) {
709 Class* klass = value->AsClass();
710 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
711 } else if (value->IsField()) {
712 Field* field = value->AsField();
713 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
714 } else if (value->IsMethod()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700715 AbstractMethod* method = value->AsMethod();
Ian Rogersd5b32602012-02-26 16:40:04 -0800716 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
717 } else {
718 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
719 }
720 }
721
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700722 static void PrintField(std::string& summary, Field* field, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700723 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800724 FieldHelper fh(field);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700725 const char* descriptor = fh.GetTypeDescriptor();
Ian Rogersd5b32602012-02-26 16:40:04 -0800726 StringAppendF(&summary, "\t%s: ", fh.GetName());
Ian Rogers48efc2b2012-08-27 17:20:31 -0700727 if (descriptor[0] != 'L' && descriptor[0] != '[') {
728 Class* type = fh.GetType();
729 if (type->IsPrimitiveLong()) {
730 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
731 } else if (type->IsPrimitiveDouble()) {
732 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
733 } else if (type->IsPrimitiveFloat()) {
734 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
735 } else {
736 DCHECK(type->IsPrimitive());
737 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
738 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800739 } else {
Ian Rogers48efc2b2012-08-27 17:20:31 -0700740 // Get the value, don't compute the type unless it is non-null as we don't want
741 // to cause class loading.
Ian Rogersd5b32602012-02-26 16:40:04 -0800742 Object* value = field->GetObj(obj);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700743 if (value == NULL) {
744 StringAppendF(&summary, "null %s\n", PrettyDescriptor(descriptor).c_str());
745 } else {
746 PrettyObjectValue(summary, fh.GetType(), value);
747 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800748 }
749 }
750
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700751 static void DumpFields(std::string& summary, Object* obj, Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700752 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800753 Class* super = klass->GetSuperClass();
754 if (super != NULL) {
755 DumpFields(summary, obj, super);
756 }
757 ObjectArray<Field>* fields = klass->GetIFields();
758 if (fields != NULL) {
759 for (int32_t i = 0; i < fields->GetLength(); i++) {
760 Field* field = fields->Get(i);
761 PrintField(summary, field, obj);
762 }
763 }
764 }
765
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800766 bool InDumpSpace(const Object* object) {
767 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700768 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800769
Mathieu Chartier66f19252012-09-18 08:57:04 -0700770 const void* GetOatCodeBegin(AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700771 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800772 Runtime* runtime = Runtime::Current();
773 const void* code = m->GetCode();
Ian Rogersfb6adba2012-03-04 21:51:51 -0800774 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800775 code = oat_dumper_->GetOatCode(m);
776 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700777 if (oat_dumper_->GetInstructionSet() == kThumb2) {
778 code = reinterpret_cast<void*>(reinterpret_cast<uint32_t>(code) & ~0x1);
779 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800780 return code;
781 }
782
Mathieu Chartier66f19252012-09-18 08:57:04 -0700783 uint32_t GetOatCodeSize(AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700784 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700785 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetOatCodeBegin(m));
786 if (oat_code_begin == NULL) {
787 return 0;
788 }
789 return oat_code_begin[-1];
790 }
791
Mathieu Chartier66f19252012-09-18 08:57:04 -0700792 const void* GetOatCodeEnd(AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700793 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700794 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetOatCodeBegin(m));
795 if (oat_code_begin == NULL) {
796 return NULL;
797 }
798 return oat_code_begin + GetOatCodeSize(m);
799 }
800
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700801 static void Callback(Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700802 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700803 DCHECK(obj != NULL);
804 DCHECK(arg != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800805 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700806 if (!state->InDumpSpace(obj)) {
807 return;
808 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700809
810 size_t object_bytes = obj->SizeOf();
811 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
812 state->stats_.object_bytes += object_bytes;
813 state->stats_.alignment_bytes += alignment_bytes;
814
Brian Carlstrom78128a62011-09-15 17:21:19 -0700815 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800816 Class* obj_class = obj->GetClass();
817 if (obj_class->IsArrayClass()) {
818 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
819 obj->AsArray()->GetLength());
820 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700821 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800822 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
823 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700824 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800825 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700826 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800827 } else if (obj->IsField()) {
828 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
829 PrettyField(obj->AsField()).c_str());
830 } else if (obj->IsMethod()) {
831 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
832 PrettyMethod(obj->AsMethod()).c_str());
833 } else if (obj_class->IsStringClass()) {
Elliott Hughes82914b62012-04-09 15:56:29 -0700834 StringAppendF(&summary, "%p: java.lang.String %s\n", obj,
835 PrintableString(obj->AsString()->ToModifiedUtf8()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800836 } else {
837 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
838 }
839 DumpFields(summary, obj, obj_class);
840 if (obj->IsObjectArray()) {
841 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
842 int32_t length = obj_array->GetLength();
843 for (int32_t i = 0; i < length; i++) {
844 Object* value = obj_array->Get(i);
845 size_t run = 0;
846 for (int32_t j = i + 1; j < length; j++) {
847 if (value == obj_array->Get(j)) {
848 run++;
849 } else {
850 break;
851 }
852 }
853 if (run == 0) {
854 StringAppendF(&summary, "\t%d: ", i);
855 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800856 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800857 i = i + run;
858 }
859 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
860 PrettyObjectValue(summary, value_class, value);
861 }
862 } else if (obj->IsClass()) {
863 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
864 if (sfields != NULL) {
865 summary += "\t\tSTATICS:\n";
866 for (int32_t i = 0; i < sfields->GetLength(); i++) {
867 Field* field = sfields->Get(i);
868 PrintField(summary, field, NULL);
869 }
870 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700871 } else if (obj->IsMethod()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700872 AbstractMethod* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700873 if (method->IsNative()) {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700874 DCHECK(method->GetNativeGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700875 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800876 bool first_occurrence;
877 size_t invoke_stub_size = state->ComputeOatSize(
878 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
879 if (first_occurrence) {
880 state->stats_.managed_to_native_code_bytes += invoke_stub_size;
881 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700882 const void* oat_code = state->GetOatCodeBegin(method);
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700883 uint32_t oat_code_size = state->GetOatCodeSize(method);
884 state->ComputeOatSize(oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800885 if (first_occurrence) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700886 state->stats_.native_to_managed_code_bytes += oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800887 }
888 if (oat_code != method->GetCode()) {
889 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
890 }
Ian Rogers19846512012-02-24 11:42:47 -0800891 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
892 method->IsResolutionMethod()) {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700893 DCHECK(method->GetNativeGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700894 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700895 } else {
TDYa1273db52852012-04-01 15:11:43 -0700896#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogers0c7abda2012-09-19 13:33:42 -0700897 DCHECK(method->GetNativeGcMap() != NULL) << PrettyMethod(method);
TDYa1273db52852012-04-01 15:11:43 -0700898#endif
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700899
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800900 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700901 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700902 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800903
Elliott Hughesa0e18062012-04-13 15:59:59 -0700904 bool first_occurrence;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700905 size_t gc_map_bytes = state->ComputeOatSize(method->GetNativeGcMap(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -0700906 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800907 state->stats_.gc_map_bytes += gc_map_bytes;
908 }
909
910 size_t pc_mapping_table_bytes =
Elliott Hughesa0e18062012-04-13 15:59:59 -0700911 state->ComputeOatSize(method->GetMappingTableRaw(), &first_occurrence);
912 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800913 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
914 }
915
916 size_t vmap_table_bytes =
Elliott Hughesa0e18062012-04-13 15:59:59 -0700917 state->ComputeOatSize(method->GetVmapTableRaw(), &first_occurrence);
918 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800919 state->stats_.vmap_table_bytes += vmap_table_bytes;
920 }
921
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700922 // TODO: compute invoke stub using length from oat file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800923 size_t invoke_stub_size = state->ComputeOatSize(
Elliott Hughesa0e18062012-04-13 15:59:59 -0700924 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
925 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800926 state->stats_.native_to_managed_code_bytes += invoke_stub_size;
927 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700928 const void* oat_code_begin = state->GetOatCodeBegin(method);
929 const void* oat_code_end = state->GetOatCodeEnd(method);
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700930 uint32_t oat_code_size = state->GetOatCodeSize(method);
Elliott Hughesa0e18062012-04-13 15:59:59 -0700931 state->ComputeOatSize(oat_code_begin, &first_occurrence);
932 if (first_occurrence) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700933 state->stats_.managed_code_bytes += oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -0700934 if (method->IsConstructor()) {
935 if (method->IsStatic()) {
936 state->stats_.class_initializer_code_bytes += oat_code_size;
937 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
938 state->stats_.large_initializer_code_bytes += oat_code_size;
939 }
940 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
941 state->stats_.large_method_code_bytes += oat_code_size;
942 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800943 }
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700944 state->stats_.managed_code_bytes_ignoring_deduplication += oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800945
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700946 StringAppendF(&summary, "\t\tOAT CODE: %p-%p\n", oat_code_begin, oat_code_end);
Ian Rogersd5b32602012-02-26 16:40:04 -0800947 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800948 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
949
950 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700951 vmap_table_bytes + invoke_stub_size + oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800952
953 double expansion =
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700954 static_cast<double>(oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800955 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700956 }
957 }
Elliott Hughesa0e18062012-04-13 15:59:59 -0700958 state->stats_.Update(ClassHelper(obj_class).GetDescriptor(), object_bytes);
Ian Rogersd5b32602012-02-26 16:40:04 -0800959
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700960 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700961 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700962
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800963 std::set<const void*> already_seen_;
964 // Compute the size of the given data within the oat file and whether this is the first time
965 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -0700966 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800967 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700968 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800969 already_seen_.insert(oat_data);
970 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700971 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800972 }
973 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700974 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700975
976 public:
977 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800978 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700979 size_t file_bytes;
980
981 size_t header_bytes;
982 size_t object_bytes;
983 size_t alignment_bytes;
984
985 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800986 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700987 size_t managed_to_native_code_bytes;
988 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -0700989 size_t class_initializer_code_bytes;
990 size_t large_initializer_code_bytes;
991 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700992
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800993 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700994 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800995 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700996
997 size_t dex_instruction_bytes;
998
Mathieu Chartier66f19252012-09-18 08:57:04 -0700999 std::vector<AbstractMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001000 std::vector<size_t> method_outlier_size;
1001 std::vector<double> method_outlier_expansion;
1002
1003 explicit Stats()
1004 : oat_file_bytes(0),
1005 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001006 header_bytes(0),
1007 object_bytes(0),
1008 alignment_bytes(0),
1009 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001010 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001011 managed_to_native_code_bytes(0),
1012 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001013 class_initializer_code_bytes(0),
1014 large_initializer_code_bytes(0),
1015 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001016 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001017 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001018 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001019 dex_instruction_bytes(0) {}
1020
Elliott Hughesa0e18062012-04-13 15:59:59 -07001021 struct SizeAndCount {
1022 SizeAndCount(size_t bytes, size_t count) : bytes(bytes), count(count) {}
1023 size_t bytes;
1024 size_t count;
1025 };
1026 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1027 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001028
Elliott Hughesa0e18062012-04-13 15:59:59 -07001029 void Update(const std::string& descriptor, size_t object_bytes) {
1030 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1031 if (it != sizes_and_counts.end()) {
1032 it->second.bytes += object_bytes;
1033 it->second.count += 1;
1034 } else {
1035 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes, 1));
1036 }
1037 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001038
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001039 double PercentOfOatBytes(size_t size) {
1040 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1041 }
1042
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001043 double PercentOfFileBytes(size_t size) {
1044 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1045 }
1046
1047 double PercentOfObjectBytes(size_t size) {
1048 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1049 }
1050
Mathieu Chartier66f19252012-09-18 08:57:04 -07001051 void ComputeOutliers(size_t total_size, double expansion, AbstractMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001052 method_outlier_size.push_back(total_size);
1053 method_outlier_expansion.push_back(expansion);
1054 method_outlier.push_back(method);
1055 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001056
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001057 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001058 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001059 size_t sum_of_sizes = 0;
1060 size_t sum_of_sizes_squared = 0;
1061 size_t sum_of_expansion = 0;
1062 size_t sum_of_expansion_squared = 0;
1063 size_t n = method_outlier_size.size();
1064 for (size_t i = 0; i < n; i++) {
1065 size_t cur_size = method_outlier_size[i];
1066 sum_of_sizes += cur_size;
1067 sum_of_sizes_squared += cur_size * cur_size;
1068 double cur_expansion = method_outlier_expansion[i];
1069 sum_of_expansion += cur_expansion;
1070 sum_of_expansion_squared += cur_expansion * cur_expansion;
1071 }
1072 size_t size_mean = sum_of_sizes / n;
1073 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1074 double expansion_mean = sum_of_expansion / n;
1075 double expansion_variance =
1076 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1077
1078 // Dump methods whose size is a certain number of standard deviations from the mean
1079 size_t dumped_values = 0;
1080 size_t skipped_values = 0;
1081 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1082 size_t cur_size_variance = i * i * size_variance;
1083 bool first = true;
1084 for (size_t j = 0; j < n; j++) {
1085 size_t cur_size = method_outlier_size[j];
1086 if (cur_size > size_mean) {
1087 size_t cur_var = cur_size - size_mean;
1088 cur_var = cur_var * cur_var;
1089 if (cur_var > cur_size_variance) {
1090 if (dumped_values > 20) {
1091 if (i == 1) {
1092 skipped_values++;
1093 } else {
1094 i = 2; // jump to counting for 1 standard deviation
1095 break;
1096 }
1097 } else {
1098 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001099 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001100 first = false;
1101 }
1102 os << "\t" << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001103 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001104 method_outlier_size[j] = 0; // don't consider this method again
1105 dumped_values++;
1106 }
1107 }
1108 }
1109 }
1110 }
1111 if (skipped_values > 0) {
1112 os << "\t... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001113 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001114 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001115 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001116
1117 // Dump methods whose expansion is a certain number of standard deviations from the mean
1118 dumped_values = 0;
1119 skipped_values = 0;
1120 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1121 double cur_expansion_variance = i * i * expansion_variance;
1122 bool first = true;
1123 for (size_t j = 0; j < n; j++) {
1124 double cur_expansion = method_outlier_expansion[j];
1125 if (cur_expansion > expansion_mean) {
1126 size_t cur_var = cur_expansion - expansion_mean;
1127 cur_var = cur_var * cur_var;
1128 if (cur_var > cur_expansion_variance) {
1129 if (dumped_values > 20) {
1130 if (i == 1) {
1131 skipped_values++;
1132 } else {
1133 i = 2; // jump to counting for 1 standard deviation
1134 break;
1135 }
1136 } else {
1137 if (first) {
1138 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001139 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001140 first = false;
1141 }
1142 os << "\t" << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001143 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001144 method_outlier_expansion[j] = 0.0; // don't consider this method again
1145 dumped_values++;
1146 }
1147 }
1148 }
1149 }
1150 }
1151 if (skipped_values > 0) {
1152 os << "\t... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001153 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001154 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001155 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001156 }
1157
Ian Rogersb726dcb2012-09-05 08:57:23 -07001158 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001159 os << "\tart_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1160 << "\tart_file_bytes = header_bytes + object_bytes + alignment_bytes\n"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001161 << StringPrintf("\theader_bytes = %8zd (%2.0f%% of art file bytes)\n"
1162 "\tobject_bytes = %8zd (%2.0f%% of art file bytes)\n"
Elliott Hughesc073b072012-05-24 19:29:17 -07001163 "\talignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001164 header_bytes, PercentOfFileBytes(header_bytes),
1165 object_bytes, PercentOfFileBytes(object_bytes),
1166 alignment_bytes, PercentOfFileBytes(alignment_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001167 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001168
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001169 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
1170
Elliott Hughesa0e18062012-04-13 15:59:59 -07001171 os << "\tobject_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001172 size_t object_bytes_total = 0;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001173 typedef SizeAndCountTable::const_iterator It; // TODO: C++0x auto
1174 for (It it = sizes_and_counts.begin(), end = sizes_and_counts.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -08001175 const std::string& descriptor(it->first);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001176 double average = static_cast<double>(it->second.bytes) / static_cast<double>(it->second.count);
1177 double percent = PercentOfObjectBytes(it->second.bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001178 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001179 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
1180 descriptor.c_str(), it->second.bytes, it->second.count,
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001181 average, percent);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001182 object_bytes_total += it->second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001183 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001184 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001185 CHECK_EQ(object_bytes, object_bytes_total);
1186
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001187 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1188 "\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers0d2d3782012-04-10 11:09:18 -07001189 "\tnative_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1190 "\tclass_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1191 "\tlarge_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Elliott Hughesc073b072012-05-24 19:29:17 -07001192 "\tlarge_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001193 managed_code_bytes, PercentOfOatBytes(managed_code_bytes),
1194 managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001195 native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes),
1196 class_initializer_code_bytes, PercentOfOatBytes(class_initializer_code_bytes),
1197 large_initializer_code_bytes, PercentOfOatBytes(large_initializer_code_bytes),
1198 large_method_code_bytes, PercentOfOatBytes(large_method_code_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001199 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001200
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001201 os << StringPrintf("\tgc_map_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1202 "\tpc_mapping_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
Elliott Hughesc073b072012-05-24 19:29:17 -07001203 "\tvmap_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001204 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1205 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1206 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001207 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001208
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001209 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes)
Elliott Hughesc073b072012-05-24 19:29:17 -07001210 << StringPrintf("\tmanaged_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
1211 static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes),
1212 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001213 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001214 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001215
1216 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001217 }
1218 } stats_;
1219
1220 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001221 enum {
1222 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1223 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1224 kLargeConstructorDexBytes = 4000,
1225 // Number of bytes for a method to be considered large. Based on the 4000 basic block
1226 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1227 kLargeMethodDexBytes = 16000
1228 };
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001229 UniquePtr<OatDumper> oat_dumper_;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001230 std::ostream& os_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001231 const std::string image_filename_;
1232 const std::string host_prefix_;
1233 Space& image_space_;
1234 const ImageHeader& image_header_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001235
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001236 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001237};
1238
Elliott Hughes72395bf2012-04-24 13:45:26 -07001239static int oatdump(int argc, char** argv) {
Elliott Hughes0d39c122012-06-06 16:41:17 -07001240 InitLogging(argv);
Elliott Hughes72395bf2012-04-24 13:45:26 -07001241
Brian Carlstrom78128a62011-09-15 17:21:19 -07001242 // Skip over argv[0].
1243 argv++;
1244 argc--;
1245
1246 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001247 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001248 usage();
1249 }
1250
Brian Carlstromaded5f72011-10-07 17:15:04 -07001251 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001252 const char* image_filename = NULL;
1253 const char* boot_image_filename = NULL;
Logan Chien0cc6ab62012-03-20 22:57:52 +08001254 std::string elf_filename_prefix;
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001255 UniquePtr<std::string> host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001256 std::ostream* os = &std::cout;
1257 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001258
1259 for (int i = 0; i < argc; i++) {
1260 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001261 if (option.starts_with("--oat-file=")) {
1262 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001263 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001264 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001265 } else if (option.starts_with("--boot-image=")) {
1266 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001267 } else if (option.starts_with("--host-prefix=")) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001268 host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001269 } else if (option.starts_with("--output=")) {
1270 const char* filename = option.substr(strlen("--output=")).data();
1271 out.reset(new std::ofstream(filename));
1272 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001273 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001274 usage();
1275 }
1276 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001277 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001278 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001279 usage();
1280 }
1281 }
1282
Brian Carlstromaded5f72011-10-07 17:15:04 -07001283 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001284 fprintf(stderr, "Either --image or --oat must be specified\n");
1285 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001286 }
1287
Brian Carlstromaded5f72011-10-07 17:15:04 -07001288 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001289 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1290 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001291 }
1292
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001293 if (host_prefix.get() == NULL) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001294 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
1295 if (android_product_out != NULL) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001296 host_prefix.reset(new std::string(android_product_out));
1297 } else {
1298 host_prefix.reset(new std::string(""));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001299 }
1300 }
1301
Brian Carlstromaded5f72011-10-07 17:15:04 -07001302 if (oat_filename != NULL) {
Logan Chien0c717dd2012-03-28 18:31:07 +08001303 OatFile* oat_file =
1304 OatFile::Open(oat_filename, oat_filename, NULL, OatFile::kRelocNone);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001305 if (oat_file == NULL) {
1306 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
1307 return EXIT_FAILURE;
1308 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001309 OatDumper oat_dumper(*host_prefix.get(), *oat_file);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001310 oat_dumper.Dump(*os);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001311 return EXIT_SUCCESS;
1312 }
1313
Brian Carlstrom78128a62011-09-15 17:21:19 -07001314 Runtime::Options options;
1315 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001316 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001317 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001318 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001319 if (boot_image_filename != NULL) {
1320 boot_image_option += "-Ximage:";
1321 boot_image_option += boot_image_filename;
1322 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001323 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001324 if (image_filename != NULL) {
1325 image_option += "-Ximage:";
1326 image_option += image_filename;
1327 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1328 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001329
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001330 if (!host_prefix->empty()) {
1331 options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001332 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001333
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001334 if (!Runtime::Create(options, false)) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001335 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001336 return EXIT_FAILURE;
1337 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001338 UniquePtr<Runtime> runtime(Runtime::Current());
1339 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
1340 // give it away now and then switch to a more managable ScopedObjectAccess.
1341 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
1342 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001343
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001344 Heap* heap = Runtime::Current()->GetHeap();
Brian Carlstromfddf6f62012-03-15 16:56:45 -07001345 ImageSpace* image_space = heap->GetImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001346 CHECK(image_space != NULL);
1347 const ImageHeader& image_header = image_space->GetImageHeader();
1348 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001349 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001350 return EXIT_FAILURE;
1351 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001352 ImageDumper image_dumper(*os, image_filename, *host_prefix.get(), *image_space, image_header);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001353 image_dumper.Dump();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001354 return EXIT_SUCCESS;
1355}
1356
1357} // namespace art
1358
1359int main(int argc, char** argv) {
1360 return art::oatdump(argc, argv);
1361}