blob: d2ceb5efa0e0fda77501db446ee06dbd7f312b66 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Elliott Hughese5448b52012-01-18 16:44:06 -080022#include <map>
Brian Carlstrom78128a62011-09-15 17:21:19 -070023#include <string>
24#include <vector>
25
26#include "class_linker.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080027#include "context.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080028#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080029#include "dex_verifier.h"
30#include "disassembler.h"
Brian Carlstrom916e74e2011-09-23 11:42:01 -070031#include "file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070032#include "image.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080033#include "object_utils.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080034#include "os.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070035#include "runtime.h"
36#include "space.h"
37#include "stringpiece.h"
38
39namespace art {
40
41static void usage() {
42 fprintf(stderr,
43 "Usage: oatdump [options] ...\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080044 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n"
45 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070046 "\n");
47 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080048 " --oat-file=<file.oat>: specifies an input oat filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080049 " Example: --image=/system/framework/boot.oat\n"
Brian Carlstromaded5f72011-10-07 17:15:04 -070050 "\n");
51 fprintf(stderr,
52 " --image=<file.art>: specifies an input image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080053 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070054 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070055 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070056 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080057 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070058 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070059 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070060 " --host-prefix may be used to translate host paths to target paths during\n"
61 " cross compilation.\n"
62 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstromfe487d02012-02-29 18:49:16 -080063 " Default: $ANDROID_PRODUCT_OUT\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070064 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070065 fprintf(stderr,
66 " --output=<file> may be used to send the output to a file.\n"
67 " Example: --output=/tmp/oatdump.txt\n"
68 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070069 exit(EXIT_FAILURE);
70}
71
Ian Rogersff1ed472011-09-20 13:46:24 -070072const char* image_roots_descriptions_[] = {
Brian Carlstrom78128a62011-09-15 17:21:19 -070073 "kJniStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070074 "kAbstractMethodErrorStubArray",
Ian Rogersad25ac52011-10-04 19:13:33 -070075 "kStaticResolutionStubArray",
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070076 "kUnknownMethodResolutionStubArray",
Ian Rogers19846512012-02-24 11:42:47 -080077 "kResolutionMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070078 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070079 "kRefsOnlySaveMethod",
80 "kRefsAndArgsSaveMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070081 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070082 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070083 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070084};
85
Elliott Hughese3c845c2012-02-28 17:23:01 -080086class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -070087 public:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080088 explicit OatDumper(const OatFile& oat_file) : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -080089 oat_dex_files_(oat_file.GetOatDexFiles()),
90 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet())) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080091 AddAllOffsets();
92 }
93
94 void Dump(std::ostream& os) {
95 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -070096
97 os << "MAGIC:\n";
98 os << oat_header.GetMagic() << "\n\n";
99
100 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800101 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700102
Elliott Hughesa72ec822012-03-05 17:12:22 -0800103 os << "INSTRUCTION SET:\n";
104 os << oat_header.GetInstructionSet() << "\n\n";
105
Brian Carlstromaded5f72011-10-07 17:15:04 -0700106 os << "DEX FILE COUNT:\n";
107 os << oat_header.GetDexFileCount() << "\n\n";
108
109 os << "EXECUTABLE OFFSET:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800110 os << StringPrintf("0x%08x\n\n", oat_header.GetExecutableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700111
Ian Rogers30fab402012-01-23 15:43:46 -0800112 os << "BEGIN:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800113 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700114
Ian Rogers30fab402012-01-23 15:43:46 -0800115 os << "END:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800116 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700117
118 os << std::flush;
119
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800120 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
121 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Brian Carlstromaded5f72011-10-07 17:15:04 -0700122 CHECK(oat_dex_file != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800123 DumpOatDexFile(os, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700124 }
125 }
126
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800127 size_t ComputeSize(const void* oat_data) {
128 if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() ||
129 reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) {
130 return 0; // Address not in oat file
131 }
132 uint32_t begin_offset = reinterpret_cast<size_t>(oat_data) -
133 reinterpret_cast<size_t>(oat_file_.Begin());
134 typedef std::set<uint32_t>::iterator It;
135 It it = offsets_.upper_bound(begin_offset);
136 CHECK(it != offsets_.end());
137 uint32_t end_offset = *it;
138 return end_offset - begin_offset;
139 }
140
141 const void* GetOatCode(Method* m) {
142 MethodHelper mh(m);
143 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
144 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
145 CHECK(oat_dex_file != NULL);
146 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
147 if (dex_file.get() != NULL) {
148 uint32_t class_def_index;
149 bool found = dex_file->FindClassDefIndex(mh.GetDeclaringClassDescriptor(), class_def_index);
150 if (found) {
151 const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_index);
152 CHECK(oat_class != NULL);
153 size_t method_index = m->GetMethodIndex();
154 return oat_class->GetOatMethod(method_index).GetCode();
155 }
156 }
157 }
158 return NULL;
159 }
160
Brian Carlstromaded5f72011-10-07 17:15:04 -0700161 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800162 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800163 // We don't know the length of the code for each method, but we need to know where to stop
164 // when disassembling. What we do know is that a region of code will be followed by some other
165 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
166 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800167 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
168 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800169 CHECK(oat_dex_file != NULL);
170 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
171 if (dex_file.get() == NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800172 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800173 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800174 offsets_.insert(reinterpret_cast<uint32_t>(&dex_file->GetHeader()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800175 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
176 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
177 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index));
178 const byte* class_data = dex_file->GetClassData(class_def);
179 if (class_data != NULL) {
180 ClassDataItemIterator it(*dex_file, class_data);
181 SkipAllFields(it);
182 uint32_t class_method_index = 0;
183 while (it.HasNextDirectMethod()) {
184 AddOffsets(oat_class->GetOatMethod(class_method_index++));
185 it.Next();
186 }
187 while (it.HasNextVirtualMethod()) {
188 AddOffsets(oat_class->GetOatMethod(class_method_index++));
189 it.Next();
190 }
191 }
192 }
193 }
194
195 // If the last thing in the file is code for a method, there won't be an offset for the "next"
196 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
197 // for the end of the file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800198 offsets_.insert(static_cast<uint32_t>(oat_file_.End() - oat_file_.Begin()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800199 }
200
201 void AddOffsets(const OatFile::OatMethod& oat_method) {
202 offsets_.insert(oat_method.GetCodeOffset());
203 offsets_.insert(oat_method.GetMappingTableOffset());
204 offsets_.insert(oat_method.GetVmapTableOffset());
205 offsets_.insert(oat_method.GetGcMapOffset());
206 offsets_.insert(oat_method.GetInvokeStubOffset());
207 }
208
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800209 void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700210 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800211 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800212 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800213 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile());
214 if (dex_file.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700215 os << "NOT FOUND\n\n";
216 return;
217 }
218 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
219 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
220 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700221 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
222 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800223 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800224 << oat_class->GetStatus() << ")\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800225 DumpOatClass(os, *oat_class.get(), *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700226 }
227
228 os << std::flush;
229 }
230
Elliott Hughese3c845c2012-02-28 17:23:01 -0800231 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700232 while (it.HasNextStaticField()) {
233 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700234 }
Ian Rogers0571d352011-11-03 19:51:38 -0700235 while (it.HasNextInstanceField()) {
236 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700237 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800238 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700239
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800240 void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
241 const DexFile::ClassDef& class_def) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800242 const byte* class_data = dex_file.GetClassData(class_def);
243 if (class_data == NULL) { // empty class such as a marker interface?
244 return;
245 }
246 ClassDataItemIterator it(dex_file, class_data);
247 SkipAllFields(it);
248
249 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700250 while (it.HasNextDirectMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800251 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800252 DumpOatMethod(os, class_method_index, oat_method, dex_file,
253 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800254 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700255 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700256 }
Ian Rogers0571d352011-11-03 19:51:38 -0700257 while (it.HasNextVirtualMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800258 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800259 DumpOatMethod(os, class_method_index, oat_method, dex_file,
260 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800261 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700262 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700263 }
Ian Rogers0571d352011-11-03 19:51:38 -0700264 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700265 os << std::flush;
266 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800267
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800268 void DumpOatMethod(std::ostream& os, uint32_t class_method_index,
Elliott Hughese3c845c2012-02-28 17:23:01 -0800269 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
270 uint32_t dex_method_idx, const DexFile::CodeItem* code_item) {
271 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700272 const char* name = dex_file.GetMethodName(method_id);
Elliott Hughes95572412011-12-13 18:14:20 -0800273 std::string signature(dex_file.GetMethodSignature(method_id));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800274 os << StringPrintf("\t%d: %s %s (dex_method_idx=%d)\n",
275 class_method_index, name, signature.c_str(), dex_method_idx);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800276 os << StringPrintf("\t\tcode: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800277 oat_method.GetCode(), oat_method.GetCodeOffset());
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800278 os << StringPrintf("\t\tframe_size_in_bytes: %zd\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800279 oat_method.GetFrameSizeInBytes());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800280 os << StringPrintf("\t\tcore_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800281 oat_method.GetCoreSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800282 DumpSpillMask(os, oat_method.GetCoreSpillMask(), false);
283 os << StringPrintf("\n\t\tfp_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800284 oat_method.GetFpSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800285 DumpSpillMask(os, oat_method.GetFpSpillMask(), true);
286 os << StringPrintf("\n\t\tmapping_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800287 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800288 DumpMappingTable(os, oat_method);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800289 os << StringPrintf("\t\tvmap_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800290 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800291 DumpVmap(os, oat_method.GetVmapTable(), oat_method.GetCoreSpillMask(),
292 oat_method.GetFpSpillMask());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800293 os << StringPrintf("\t\tgc_map: %p (offset=0x%08x)\n",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800294 oat_method.GetGcMap(), oat_method.GetGcMapOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800295 DumpGcMap(os, oat_method.GetGcMap());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800296 os << StringPrintf("\t\tinvoke_stub: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800297 oat_method.GetInvokeStub(), oat_method.GetInvokeStubOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800298 os << "\t\tCODE:\n";
299 DumpCode(os, oat_method.GetCode(), oat_method.GetMappingTable(), dex_file, code_item);
300 os << "\t\tINVOKE STUB:\n";
301 DumpCode(os, reinterpret_cast<const void*>(oat_method.GetInvokeStub()), NULL, dex_file, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700302 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800303
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800304 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
305 if (spill_mask == 0) {
306 return;
307 }
308 os << " (";
309 for (size_t i = 0; i < 32; i++) {
310 if ((spill_mask & (1 << i)) != 0) {
311 if (is_float) {
312 os << "fr" << i;
313 } else {
314 os << "r" << i;
315 }
316 spill_mask ^= 1 << i; // clear bit
317 if (spill_mask != 0) {
318 os << ", ";
319 } else {
320 break;
321 }
322 }
323 }
324 os << ")";
325 }
326
327 void DumpVmap(std::ostream& os, const uint16_t* raw_table, uint32_t core_spill_mask,
328 uint32_t fp_spill_mask) {
329 if (raw_table == NULL) {
330 return;
331 }
332 const VmapTable vmap_table(raw_table);
333 bool first = true;
334 os << "\t\t\t";
335 for (size_t i = 0; i < vmap_table.size(); i++) {
336 uint16_t dex_reg = vmap_table[i];
337 size_t matches = 0;
338 size_t spill_shifts = 0;
339 uint32_t spill_mask = core_spill_mask;
340 bool processing_fp = false;
341 while (matches != (i + 1)) {
342 if (spill_mask == 0) {
343 CHECK(!processing_fp);
344 spill_mask = fp_spill_mask;
345 processing_fp = true;
346 }
347 matches += spill_mask & 1; // Add 1 if the low bit is set
348 spill_mask >>= 1;
349 spill_shifts++;
350 }
351 size_t arm_reg = spill_shifts - 1; // wind back one as we want the last match
352 os << (first ? "v" : ", v") << dex_reg;
353 if (arm_reg < 16) {
354 os << "/r" << arm_reg;
355 } else {
356 os << "/fr" << (arm_reg - 16);
357 }
358 if (first) {
359 first = false;
360 }
361 }
362 os << std::endl;
363 }
364
365 void DumpGcMap(std::ostream& os, const uint8_t* gc_map_raw) {
366 if (gc_map_raw == NULL) {
367 return;
368 }
369 uint32_t gc_map_length = (gc_map_raw[0] << 24) | (gc_map_raw[1] << 16) |
370 (gc_map_raw[2] << 8) | (gc_map_raw[3] << 0);
371 verifier::PcToReferenceMap map(gc_map_raw + sizeof(uint32_t), gc_map_length);
372 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
373 os << StringPrintf("\t\t\t0x%04x", map.GetPC(entry));
374 size_t num_regs = map.RegWidth() * 8;
375 const uint8_t* reg_bitmap = map.GetBitMap(entry);
376 bool first = true;
377 for (size_t reg = 0; reg < num_regs; reg++) {
378 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
379 if (first) {
380 os << " v" << reg;
381 first = false;
382 } else {
383 os << ", v" << reg;
384 }
385 }
386 }
387 os << std::endl;
388 }
389 }
390
391 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800392 const uint32_t* raw_table = oat_method.GetMappingTable();
393 const void* code = oat_method.GetCode();
394 if (raw_table == NULL || code == NULL) {
395 return;
396 }
397
398 uint32_t length = *raw_table;
399 ++raw_table;
400
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800401 os << "\t\t{";
Elliott Hughese3c845c2012-02-28 17:23:01 -0800402 for (size_t i = 0; i < length; i += 2) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800403 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800404 uint32_t dex_pc = raw_table[i + 1];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800405 os << StringPrintf("%p -> 0x%04x", native_pc, dex_pc);
406 if (i + 2 < length) {
407 os << ", ";
408 }
409 }
410 os << "}" << std::endl << std::flush;
411 }
412
413 void DumpCode(std::ostream& os, const void* code, const uint32_t* raw_mapping_table,
414 const DexFile& dex_file, const DexFile::CodeItem* code_item) {
415 if (code == NULL) {
416 return;
417 }
418
419 if (raw_mapping_table == NULL) {
420 // code but no mapping table is most likely caused by code created by the JNI compiler
421 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code);
422 const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file_.Begin());
423 uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin);
424
425 typedef std::set<uint32_t>::iterator It;
426 It it = offsets_.upper_bound(last_offset);
427 CHECK(it != offsets_.end());
428 const uint8_t* end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it;
429 CHECK(native_pc < end_native_pc);
430
431 disassembler_->Dump(os, native_pc, end_native_pc);
432 return;
433 }
434
435 uint32_t length = *raw_mapping_table;
436 ++raw_mapping_table;
437
438 for (size_t i = 0; i < length; i += 2) {
439 uint32_t dex_pc = raw_mapping_table[i + 1];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800440 const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]);
441 os << StringPrintf("\t\t0x%04x: %s\n", dex_pc, instruction->DumpString(&dex_file).c_str());
442
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800443 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800444 const uint8_t* end_native_pc = NULL;
445 if (i + 2 < length) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800446 end_native_pc = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i + 2];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800447 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800448 const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file_.Begin());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800449 uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin);
450
451 typedef std::set<uint32_t>::iterator It;
Elliott Hughesed2adb62012-02-29 14:41:01 -0800452 It it = offsets_.upper_bound(last_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800453 CHECK(it != offsets_.end());
454 end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it;
455 }
Elliott Hughesed2adb62012-02-29 14:41:01 -0800456 CHECK(native_pc < end_native_pc);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800457 disassembler_->Dump(os, native_pc, end_native_pc);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800458 }
459 }
460
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800461 const OatFile& oat_file_;
462 std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800463 std::set<uint32_t> offsets_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800464 UniquePtr<Disassembler> disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700465};
466
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800467class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700468 public:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800469 explicit ImageDumper(std::ostream& os, const std::string& image_filename,
470 const std::string& host_prefix, Space& image_space,
471 const ImageHeader& image_header) : os_(os),
472 image_filename_(image_filename), host_prefix_(host_prefix),
473 image_space_(image_space), image_header_(image_header) {
474 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700475
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800476 void Dump() {
477 os_ << "MAGIC:\n";
478 os_ << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700479
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800480 os_ << "IMAGE BEGIN:\n";
481 os_ << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700482
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800483 os_ << "OAT CHECKSUM:\n";
484 os_ << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700485
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800486 os_ << "OAT BEGIN:\n";
487 os_ << reinterpret_cast<void*>(image_header_.GetOatBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700488
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800489 os_ << "OAT END:\n";
490 os_ << reinterpret_cast<void*>(image_header_.GetOatEnd()) << "\n\n";
491
492 os_ << "ROOTS:\n";
493 os_ << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700494 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700495 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
496 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700497 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800498 Object* image_root_object = image_header_.GetImageRoot(image_root);
499 os_ << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700500 if (image_root_object->IsObjectArray()) {
501 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
502 ObjectArray<Object>* image_root_object_array
503 = down_cast<ObjectArray<Object>*>(image_root_object);
504 // = image_root_object->AsObjectArray<Object>();
505 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800506 Object* value = image_root_object_array->Get(i);
507 if (value != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800508 os_ << "\t" << i << ": ";
Ian Rogersd5b32602012-02-26 16:40:04 -0800509 std::string summary;
510 PrettyObjectValue(summary, value->GetClass(), value);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800511 os_ << summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800512 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800513 os_ << StringPrintf("\t%d: null\n", i);
Ian Rogersd5b32602012-02-26 16:40:04 -0800514 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700515 }
516 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700517 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800518 os_ << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700519
Brian Carlstromaded5f72011-10-07 17:15:04 -0700520 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800521 Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation);
522 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
523 if (!host_prefix_.empty()) {
524 oat_location = host_prefix_ + oat_location;
525 os_ << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700526 }
Brian Carlstromae826982011-11-09 01:33:42 -0800527 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700528 if (oat_file == NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800529 os_ << "OAT FILE NOT FOUND: " << oat_location << std::endl << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700530 return;
531 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700532
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800533 stats_.oat_file_bytes = oat_file->Size();
534
535 oat_dumper_.reset(new OatDumper(*oat_file));
536
537 os_ << "OBJECTS:\n" << std::flush;
538 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
539 DCHECK(heap_bitmap != NULL);
540 heap_bitmap->Walk(ImageDumper::Callback, this);
541 os_ << "\n";
542
543 os_ << "STATS:\n" << std::flush;
544 UniquePtr<File> file(OS::OpenFile(image_filename_.c_str(), false));
545 stats_.file_bytes = file->Length();
546 size_t header_bytes = sizeof(ImageHeader);
547 stats_.header_bytes = header_bytes;
548 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
549 stats_.alignment_bytes += alignment_bytes;
550 stats_.Dump(os_);
551 os_ << "\n";
552
553 os_ << std::flush;
554
555 os_ << "OAT LOCATION:\n" << std::flush;
556 os_ << oat_location;
557 os_ << "\n";
558 oat_dumper_->Dump(os_);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700559 }
560
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700561 private:
562
Ian Rogersd5b32602012-02-26 16:40:04 -0800563 static void PrettyObjectValue(std::string& summary, Class* type, Object* value) {
564 CHECK(type != NULL);
565 if (value == NULL) {
566 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
567 } else if (type->IsStringClass()) {
568 String* string = value->AsString();
569 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
570 } else if (value->IsClass()) {
571 Class* klass = value->AsClass();
572 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
573 } else if (value->IsField()) {
574 Field* field = value->AsField();
575 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
576 } else if (value->IsMethod()) {
577 Method* method = value->AsMethod();
578 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
579 } else {
580 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
581 }
582 }
583
584 static void PrintField(std::string& summary, Field* field, Object* obj) {
585 FieldHelper fh(field);
586 Class* type = fh.GetType();
587 StringAppendF(&summary, "\t%s: ", fh.GetName());
588 if (type->IsPrimitiveLong()) {
589 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
590 } else if (type->IsPrimitiveDouble()) {
591 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
592 } else if (type->IsPrimitiveFloat()) {
593 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
594 } else if (type->IsPrimitive()){
595 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
596 } else {
597 Object* value = field->GetObj(obj);
598 PrettyObjectValue(summary, type, value);
599 }
600 }
601
602 static void DumpFields(std::string& summary, Object* obj, Class* klass) {
603 Class* super = klass->GetSuperClass();
604 if (super != NULL) {
605 DumpFields(summary, obj, super);
606 }
607 ObjectArray<Field>* fields = klass->GetIFields();
608 if (fields != NULL) {
609 for (int32_t i = 0; i < fields->GetLength(); i++) {
610 Field* field = fields->Get(i);
611 PrintField(summary, field, obj);
612 }
613 }
614 }
615
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800616 bool InDumpSpace(const Object* object) {
617 return image_space_.Contains(object);
618 }
619
620 const void* GetOatCode(Method* m) {
621 Runtime* runtime = Runtime::Current();
622 const void* code = m->GetCode();
Ian Rogersfb6adba2012-03-04 21:51:51 -0800623 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800624 code = oat_dumper_->GetOatCode(m);
625 }
626 return code;
627 }
628
Brian Carlstrom78128a62011-09-15 17:21:19 -0700629 static void Callback(Object* obj, void* arg) {
630 DCHECK(obj != NULL);
631 DCHECK(arg != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800632 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700633 if (!state->InDumpSpace(obj)) {
634 return;
635 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700636
637 size_t object_bytes = obj->SizeOf();
638 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
639 state->stats_.object_bytes += object_bytes;
640 state->stats_.alignment_bytes += alignment_bytes;
641
Brian Carlstrom78128a62011-09-15 17:21:19 -0700642 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800643 Class* obj_class = obj->GetClass();
644 if (obj_class->IsArrayClass()) {
645 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
646 obj->AsArray()->GetLength());
647 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700648 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800649 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
650 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700651 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800652 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700653 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800654 } else if (obj->IsField()) {
655 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
656 PrettyField(obj->AsField()).c_str());
657 } else if (obj->IsMethod()) {
658 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
659 PrettyMethod(obj->AsMethod()).c_str());
660 } else if (obj_class->IsStringClass()) {
661 StringAppendF(&summary, "%p: java.lang.String \"%s\"\n", obj,
662 obj->AsString()->ToModifiedUtf8().c_str());
663 } else {
664 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
665 }
666 DumpFields(summary, obj, obj_class);
667 if (obj->IsObjectArray()) {
668 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
669 int32_t length = obj_array->GetLength();
670 for (int32_t i = 0; i < length; i++) {
671 Object* value = obj_array->Get(i);
672 size_t run = 0;
673 for (int32_t j = i + 1; j < length; j++) {
674 if (value == obj_array->Get(j)) {
675 run++;
676 } else {
677 break;
678 }
679 }
680 if (run == 0) {
681 StringAppendF(&summary, "\t%d: ", i);
682 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800683 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800684 i = i + run;
685 }
686 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
687 PrettyObjectValue(summary, value_class, value);
688 }
689 } else if (obj->IsClass()) {
690 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
691 if (sfields != NULL) {
692 summary += "\t\tSTATICS:\n";
693 for (int32_t i = 0; i < sfields->GetLength(); i++) {
694 Field* field = sfields->Get(i);
695 PrintField(summary, field, NULL);
696 }
697 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700698 } else if (obj->IsMethod()) {
699 Method* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700700 if (method->IsNative()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700701 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800702 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700703 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800704 bool first_occurrence;
705 size_t invoke_stub_size = state->ComputeOatSize(
706 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
707 if (first_occurrence) {
708 state->stats_.managed_to_native_code_bytes += invoke_stub_size;
709 }
710 const void* oat_code = state->GetOatCode(method);
711 size_t code_size = state->ComputeOatSize(oat_code, &first_occurrence);
712 if (first_occurrence) {
713 state->stats_.native_to_managed_code_bytes += code_size;
714 }
715 if (oat_code != method->GetCode()) {
716 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
717 }
Ian Rogers19846512012-02-24 11:42:47 -0800718 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
719 method->IsResolutionMethod()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800721 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700722 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700723 } else {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800724 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
725 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700726
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800727 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700728 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700729 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800730
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800731 bool first_occurance;
732 size_t gc_map_bytes = state->ComputeOatSize(method->GetGcMapRaw(), &first_occurance);
733 if (first_occurance) {
734 state->stats_.gc_map_bytes += gc_map_bytes;
735 }
736
737 size_t pc_mapping_table_bytes =
738 state->ComputeOatSize(method->GetMappingTableRaw(), &first_occurance);
739 if (first_occurance) {
740 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
741 }
742
743 size_t vmap_table_bytes =
744 state->ComputeOatSize(method->GetVmapTableRaw(), &first_occurance);
745 if (first_occurance) {
746 state->stats_.vmap_table_bytes += vmap_table_bytes;
747 }
748
749 size_t invoke_stub_size = state->ComputeOatSize(
750 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurance);
751 if (first_occurance) {
752 state->stats_.native_to_managed_code_bytes += invoke_stub_size;
753 }
754 const void* oat_code = state->GetOatCode(method);
755 size_t code_size = state->ComputeOatSize(oat_code, &first_occurance);
756 if (first_occurance) {
757 state->stats_.managed_code_bytes += code_size;
758 }
759 state->stats_.managed_code_bytes_ignoring_deduplication += code_size;
760
761 if (oat_code != method->GetCode()) {
762 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
763 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800764 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800765 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
766
767 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
768 vmap_table_bytes + invoke_stub_size + code_size + object_bytes;
769
770 double expansion =
771 static_cast<double>(code_size) / static_cast<double>(dex_instruction_bytes);
772 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700773 }
774 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800775 std::string descriptor(ClassHelper(obj_class).GetDescriptor());
776 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
777 state->stats_.descriptor_to_count[descriptor] += 1;
778
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700779 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700780 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700781
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800782 std::set<const void*> already_seen_;
783 // Compute the size of the given data within the oat file and whether this is the first time
784 // this data has been requested
785 size_t ComputeOatSize(const void* oat_data, bool* first_occurance) {
786 if (already_seen_.count(oat_data) == 0) {
787 *first_occurance = true;
788 already_seen_.insert(oat_data);
789 } else {
790 *first_occurance = false;
791 }
792 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700793 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700794
795 public:
796 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800797 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700798 size_t file_bytes;
799
800 size_t header_bytes;
801 size_t object_bytes;
802 size_t alignment_bytes;
803
804 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800805 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700806 size_t managed_to_native_code_bytes;
807 size_t native_to_managed_code_bytes;
808
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800809 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700810 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800811 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700812
813 size_t dex_instruction_bytes;
814
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800815 std::vector<Method*> method_outlier;
816 std::vector<size_t> method_outlier_size;
817 std::vector<double> method_outlier_expansion;
818
819 explicit Stats()
820 : oat_file_bytes(0),
821 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700822 header_bytes(0),
823 object_bytes(0),
824 alignment_bytes(0),
825 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800826 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700827 managed_to_native_code_bytes(0),
828 native_to_managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800829 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700830 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800831 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700832 dex_instruction_bytes(0) {}
833
Elliott Hughese5448b52012-01-18 16:44:06 -0800834 typedef std::map<std::string, size_t> TableBytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700835 TableBytes descriptor_to_bytes;
836
Elliott Hughese5448b52012-01-18 16:44:06 -0800837 typedef std::map<std::string, size_t> TableCount;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700838 TableCount descriptor_to_count;
839
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800840 double PercentOfOatBytes(size_t size) {
841 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
842 }
843
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700844 double PercentOfFileBytes(size_t size) {
845 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
846 }
847
848 double PercentOfObjectBytes(size_t size) {
849 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
850 }
851
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800852 void ComputeOutliers(size_t total_size, double expansion, Method* method) {
853 method_outlier_size.push_back(total_size);
854 method_outlier_expansion.push_back(expansion);
855 method_outlier.push_back(method);
856 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700857
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800858 void DumpOutliers(std::ostream& os) {
859 size_t sum_of_sizes = 0;
860 size_t sum_of_sizes_squared = 0;
861 size_t sum_of_expansion = 0;
862 size_t sum_of_expansion_squared = 0;
863 size_t n = method_outlier_size.size();
864 for (size_t i = 0; i < n; i++) {
865 size_t cur_size = method_outlier_size[i];
866 sum_of_sizes += cur_size;
867 sum_of_sizes_squared += cur_size * cur_size;
868 double cur_expansion = method_outlier_expansion[i];
869 sum_of_expansion += cur_expansion;
870 sum_of_expansion_squared += cur_expansion * cur_expansion;
871 }
872 size_t size_mean = sum_of_sizes / n;
873 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
874 double expansion_mean = sum_of_expansion / n;
875 double expansion_variance =
876 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
877
878 // Dump methods whose size is a certain number of standard deviations from the mean
879 size_t dumped_values = 0;
880 size_t skipped_values = 0;
881 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
882 size_t cur_size_variance = i * i * size_variance;
883 bool first = true;
884 for (size_t j = 0; j < n; j++) {
885 size_t cur_size = method_outlier_size[j];
886 if (cur_size > size_mean) {
887 size_t cur_var = cur_size - size_mean;
888 cur_var = cur_var * cur_var;
889 if (cur_var > cur_size_variance) {
890 if (dumped_values > 20) {
891 if (i == 1) {
892 skipped_values++;
893 } else {
894 i = 2; // jump to counting for 1 standard deviation
895 break;
896 }
897 } else {
898 if (first) {
899 os << "\nBig methods (size > " << i << " standard deviations the norm):"
900 << std::endl;
901 first = false;
902 }
903 os << "\t" << PrettyMethod(method_outlier[j]) << " requires storage of "
904 << PrettySize(cur_size) << std::endl;
905 method_outlier_size[j] = 0; // don't consider this method again
906 dumped_values++;
907 }
908 }
909 }
910 }
911 }
912 if (skipped_values > 0) {
913 os << "\t... skipped " << skipped_values
914 << " methods with size > 1 standard deviation from the norm" << std::endl;
915 }
916 os << std::endl << std::flush;
917
918 // Dump methods whose expansion is a certain number of standard deviations from the mean
919 dumped_values = 0;
920 skipped_values = 0;
921 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
922 double cur_expansion_variance = i * i * expansion_variance;
923 bool first = true;
924 for (size_t j = 0; j < n; j++) {
925 double cur_expansion = method_outlier_expansion[j];
926 if (cur_expansion > expansion_mean) {
927 size_t cur_var = cur_expansion - expansion_mean;
928 cur_var = cur_var * cur_var;
929 if (cur_var > cur_expansion_variance) {
930 if (dumped_values > 20) {
931 if (i == 1) {
932 skipped_values++;
933 } else {
934 i = 2; // jump to counting for 1 standard deviation
935 break;
936 }
937 } else {
938 if (first) {
939 os << "\nLarge expansion methods (size > " << i
940 << " standard deviations the norm):" << std::endl;
941 first = false;
942 }
943 os << "\t" << PrettyMethod(method_outlier[j]) << " expanded code by "
944 << cur_expansion << std::endl;
945 method_outlier_expansion[j] = 0.0; // don't consider this method again
946 dumped_values++;
947 }
948 }
949 }
950 }
951 }
952 if (skipped_values > 0) {
953 os << "\t... skipped " << skipped_values
954 << " methods with expansion > 1 standard deviation from the norm" << std::endl;
955 }
956 os << std::endl << std::flush;
957 }
958
959 void Dump(std::ostream& os) {
960 os << "\tart_file_bytes = " << PrettySize(file_bytes) << std::endl << std::endl
961 << "\tart_file_bytes = header_bytes + object_bytes + alignment_bytes" << std::endl
962 << StringPrintf("\theader_bytes = %8zd (%2.0f%% of art file bytes)\n"
963 "\tobject_bytes = %8zd (%2.0f%% of art file bytes)\n"
964 "\talignment_bytes = %8zd (%2.0f%% of art file bytes)\n",
965 header_bytes, PercentOfFileBytes(header_bytes),
966 object_bytes, PercentOfFileBytes(object_bytes),
967 alignment_bytes, PercentOfFileBytes(alignment_bytes))
968 << std::endl << std::flush;
969
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700970 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
971
972 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
973 size_t object_bytes_total = 0;
974 typedef TableBytes::const_iterator It; // TODO: C++0x auto
975 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -0800976 const std::string& descriptor(it->first);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700977 size_t bytes = it->second;
978 size_t count = descriptor_to_count[descriptor];
979 double average = static_cast<double>(bytes) / static_cast<double>(count);
980 double percent = PercentOfObjectBytes(bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800981 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700982 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
983 descriptor.c_str(), bytes, count,
984 average, percent);
985
986 object_bytes_total += bytes;
987 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800988 os << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700989 CHECK_EQ(object_bytes, object_bytes_total);
990
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800991 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
992 "\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
993 "\tnative_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n",
994 managed_code_bytes, PercentOfOatBytes(managed_code_bytes),
995 managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes),
996 native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes))
997 << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700998
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800999 os << StringPrintf("\tgc_map_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1000 "\tpc_mapping_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1001 "\tvmap_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n",
1002 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1003 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1004 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
1005 << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001006
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001007 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001008 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n",
1009 static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes),
1010 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
1011 static_cast<double>(dex_instruction_bytes));
1012 os << std::endl << std::flush;
1013
1014 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001015 }
1016 } stats_;
1017
1018 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001019 UniquePtr<OatDumper> oat_dumper_;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001020 std::ostream& os_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001021 const std::string image_filename_;
1022 const std::string host_prefix_;
1023 Space& image_space_;
1024 const ImageHeader& image_header_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001025
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001026 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001027};
1028
1029int oatdump(int argc, char** argv) {
1030 // Skip over argv[0].
1031 argv++;
1032 argc--;
1033
1034 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001035 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001036 usage();
1037 }
1038
Brian Carlstromaded5f72011-10-07 17:15:04 -07001039 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001040 const char* image_filename = NULL;
1041 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001042 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001043 std::ostream* os = &std::cout;
1044 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001045
1046 for (int i = 0; i < argc; i++) {
1047 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001048 if (option.starts_with("--oat-file=")) {
1049 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001050 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001051 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001052 } else if (option.starts_with("--boot-image=")) {
1053 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001054 } else if (option.starts_with("--host-prefix=")) {
1055 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001056 } else if (option.starts_with("--output=")) {
1057 const char* filename = option.substr(strlen("--output=")).data();
1058 out.reset(new std::ofstream(filename));
1059 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001060 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001061 usage();
1062 }
1063 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001064 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001065 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001066 usage();
1067 }
1068 }
1069
Brian Carlstromaded5f72011-10-07 17:15:04 -07001070 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001071 fprintf(stderr, "Either --image or --oat must be specified\n");
1072 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001073 }
1074
Brian Carlstromaded5f72011-10-07 17:15:04 -07001075 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001076 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1077 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001078 }
1079
1080 if (oat_filename != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001081 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001082 if (oat_file == NULL) {
1083 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
1084 return EXIT_FAILURE;
1085 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001086 OatDumper oat_dumper(*oat_file);
1087 oat_dumper.Dump(*os);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001088 return EXIT_SUCCESS;
1089 }
1090
Brian Carlstrom78128a62011-09-15 17:21:19 -07001091 Runtime::Options options;
1092 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001093 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001094 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001095 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001096 if (boot_image_filename != NULL) {
1097 boot_image_option += "-Ximage:";
1098 boot_image_option += boot_image_filename;
1099 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001100 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001101 if (image_filename != NULL) {
1102 image_option += "-Ximage:";
1103 image_option += image_filename;
1104 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1105 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001106
Brian Carlstromfe487d02012-02-29 18:49:16 -08001107 if (host_prefix.empty()) {
1108 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
1109 if (android_product_out != NULL) {
1110 host_prefix = android_product_out;
1111 }
1112 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001113 if (!host_prefix.empty()) {
1114 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
1115 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001116
1117 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
1118 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001119 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001120 return EXIT_FAILURE;
1121 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001122
Ian Rogers30fab402012-01-23 15:43:46 -08001123 ImageSpace* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]->AsImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001124 CHECK(image_space != NULL);
1125 const ImageHeader& image_header = image_space->GetImageHeader();
1126 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001127 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001128 return EXIT_FAILURE;
1129 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001130 ImageDumper image_dumper(*os, image_filename, host_prefix, *image_space, image_header);
1131 image_dumper.Dump();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001132 return EXIT_SUCCESS;
1133}
1134
1135} // namespace art
1136
1137int main(int argc, char** argv) {
1138 return art::oatdump(argc, argv);
1139}