blob: 7333bc3b5cf187fc2861c35d626eff2f59ad5b36 [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) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800202 uint32_t code_offset = oat_method.GetCodeOffset();
203 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
204 code_offset &= ~0x1;
205 }
206 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800207 offsets_.insert(oat_method.GetMappingTableOffset());
208 offsets_.insert(oat_method.GetVmapTableOffset());
209 offsets_.insert(oat_method.GetGcMapOffset());
210 offsets_.insert(oat_method.GetInvokeStubOffset());
211 }
212
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800213 void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700214 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800215 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800216 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800217 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile());
218 if (dex_file.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700219 os << "NOT FOUND\n\n";
220 return;
221 }
222 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
223 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
224 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700225 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
226 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800227 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800228 << oat_class->GetStatus() << ")\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800229 DumpOatClass(os, *oat_class.get(), *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700230 }
231
232 os << std::flush;
233 }
234
Elliott Hughese3c845c2012-02-28 17:23:01 -0800235 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700236 while (it.HasNextStaticField()) {
237 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700238 }
Ian Rogers0571d352011-11-03 19:51:38 -0700239 while (it.HasNextInstanceField()) {
240 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700241 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800242 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700243
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800244 void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
245 const DexFile::ClassDef& class_def) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800246 const byte* class_data = dex_file.GetClassData(class_def);
247 if (class_data == NULL) { // empty class such as a marker interface?
248 return;
249 }
250 ClassDataItemIterator it(dex_file, class_data);
251 SkipAllFields(it);
252
253 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700254 while (it.HasNextDirectMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800255 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800256 DumpOatMethod(os, class_method_index, oat_method, dex_file,
257 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800258 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700259 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700260 }
Ian Rogers0571d352011-11-03 19:51:38 -0700261 while (it.HasNextVirtualMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800262 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800263 DumpOatMethod(os, class_method_index, oat_method, dex_file,
264 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800265 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700266 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700267 }
Ian Rogers0571d352011-11-03 19:51:38 -0700268 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700269 os << std::flush;
270 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800271
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800272 void DumpOatMethod(std::ostream& os, uint32_t class_method_index,
Elliott Hughese3c845c2012-02-28 17:23:01 -0800273 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
274 uint32_t dex_method_idx, const DexFile::CodeItem* code_item) {
275 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700276 const char* name = dex_file.GetMethodName(method_id);
Elliott Hughes95572412011-12-13 18:14:20 -0800277 std::string signature(dex_file.GetMethodSignature(method_id));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800278 os << StringPrintf("\t%d: %s %s (dex_method_idx=%d)\n",
279 class_method_index, name, signature.c_str(), dex_method_idx);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800280 os << StringPrintf("\t\tcode: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800281 oat_method.GetCode(), oat_method.GetCodeOffset());
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800282 os << StringPrintf("\t\tframe_size_in_bytes: %zd\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800283 oat_method.GetFrameSizeInBytes());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800284 os << StringPrintf("\t\tcore_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800285 oat_method.GetCoreSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800286 DumpSpillMask(os, oat_method.GetCoreSpillMask(), false);
287 os << StringPrintf("\n\t\tfp_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800288 oat_method.GetFpSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800289 DumpSpillMask(os, oat_method.GetFpSpillMask(), true);
290 os << StringPrintf("\n\t\tmapping_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800291 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800292 DumpMappingTable(os, oat_method);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800293 os << StringPrintf("\t\tvmap_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800294 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800295 DumpVmap(os, oat_method.GetVmapTable(), oat_method.GetCoreSpillMask(),
296 oat_method.GetFpSpillMask());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800297 os << StringPrintf("\t\tgc_map: %p (offset=0x%08x)\n",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800298 oat_method.GetGcMap(), oat_method.GetGcMapOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800299 DumpGcMap(os, oat_method.GetGcMap());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800300 os << StringPrintf("\t\tinvoke_stub: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800301 oat_method.GetInvokeStub(), oat_method.GetInvokeStubOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800302 os << "\t\tCODE:\n";
303 DumpCode(os, oat_method.GetCode(), oat_method.GetMappingTable(), dex_file, code_item);
304 os << "\t\tINVOKE STUB:\n";
305 DumpCode(os, reinterpret_cast<const void*>(oat_method.GetInvokeStub()), NULL, dex_file, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700306 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800307
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800308 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
309 if (spill_mask == 0) {
310 return;
311 }
312 os << " (";
313 for (size_t i = 0; i < 32; i++) {
314 if ((spill_mask & (1 << i)) != 0) {
315 if (is_float) {
316 os << "fr" << i;
317 } else {
318 os << "r" << i;
319 }
320 spill_mask ^= 1 << i; // clear bit
321 if (spill_mask != 0) {
322 os << ", ";
323 } else {
324 break;
325 }
326 }
327 }
328 os << ")";
329 }
330
331 void DumpVmap(std::ostream& os, const uint16_t* raw_table, uint32_t core_spill_mask,
332 uint32_t fp_spill_mask) {
333 if (raw_table == NULL) {
334 return;
335 }
336 const VmapTable vmap_table(raw_table);
337 bool first = true;
338 os << "\t\t\t";
339 for (size_t i = 0; i < vmap_table.size(); i++) {
340 uint16_t dex_reg = vmap_table[i];
341 size_t matches = 0;
342 size_t spill_shifts = 0;
343 uint32_t spill_mask = core_spill_mask;
344 bool processing_fp = false;
345 while (matches != (i + 1)) {
346 if (spill_mask == 0) {
347 CHECK(!processing_fp);
348 spill_mask = fp_spill_mask;
349 processing_fp = true;
350 }
351 matches += spill_mask & 1; // Add 1 if the low bit is set
352 spill_mask >>= 1;
353 spill_shifts++;
354 }
355 size_t arm_reg = spill_shifts - 1; // wind back one as we want the last match
356 os << (first ? "v" : ", v") << dex_reg;
357 if (arm_reg < 16) {
358 os << "/r" << arm_reg;
359 } else {
360 os << "/fr" << (arm_reg - 16);
361 }
362 if (first) {
363 first = false;
364 }
365 }
366 os << std::endl;
367 }
368
369 void DumpGcMap(std::ostream& os, const uint8_t* gc_map_raw) {
370 if (gc_map_raw == NULL) {
371 return;
372 }
373 uint32_t gc_map_length = (gc_map_raw[0] << 24) | (gc_map_raw[1] << 16) |
374 (gc_map_raw[2] << 8) | (gc_map_raw[3] << 0);
375 verifier::PcToReferenceMap map(gc_map_raw + sizeof(uint32_t), gc_map_length);
376 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
377 os << StringPrintf("\t\t\t0x%04x", map.GetPC(entry));
378 size_t num_regs = map.RegWidth() * 8;
379 const uint8_t* reg_bitmap = map.GetBitMap(entry);
380 bool first = true;
381 for (size_t reg = 0; reg < num_regs; reg++) {
382 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
383 if (first) {
384 os << " v" << reg;
385 first = false;
386 } else {
387 os << ", v" << reg;
388 }
389 }
390 }
391 os << std::endl;
392 }
393 }
394
395 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800396 const uint32_t* raw_table = oat_method.GetMappingTable();
397 const void* code = oat_method.GetCode();
398 if (raw_table == NULL || code == NULL) {
399 return;
400 }
401
402 uint32_t length = *raw_table;
403 ++raw_table;
404
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800405 os << "\t\t{";
Elliott Hughese3c845c2012-02-28 17:23:01 -0800406 for (size_t i = 0; i < length; i += 2) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800407 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800408 uint32_t dex_pc = raw_table[i + 1];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800409 os << StringPrintf("%p -> 0x%04x", native_pc, dex_pc);
410 if (i + 2 < length) {
411 os << ", ";
412 }
413 }
414 os << "}" << std::endl << std::flush;
415 }
416
417 void DumpCode(std::ostream& os, const void* code, const uint32_t* raw_mapping_table,
418 const DexFile& dex_file, const DexFile::CodeItem* code_item) {
419 if (code == NULL) {
420 return;
421 }
422
423 if (raw_mapping_table == NULL) {
424 // code but no mapping table is most likely caused by code created by the JNI compiler
425 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code);
426 const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file_.Begin());
427 uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin);
428
429 typedef std::set<uint32_t>::iterator It;
430 It it = offsets_.upper_bound(last_offset);
431 CHECK(it != offsets_.end());
432 const uint8_t* end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it;
433 CHECK(native_pc < end_native_pc);
434
435 disassembler_->Dump(os, native_pc, end_native_pc);
436 return;
437 }
438
439 uint32_t length = *raw_mapping_table;
440 ++raw_mapping_table;
441
442 for (size_t i = 0; i < length; i += 2) {
443 uint32_t dex_pc = raw_mapping_table[i + 1];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800444 const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]);
445 os << StringPrintf("\t\t0x%04x: %s\n", dex_pc, instruction->DumpString(&dex_file).c_str());
446
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800447 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800448 const uint8_t* end_native_pc = NULL;
449 if (i + 2 < length) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800450 end_native_pc = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i + 2];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800451 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800452 const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file_.Begin());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800453 uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin);
454
455 typedef std::set<uint32_t>::iterator It;
Elliott Hughesed2adb62012-02-29 14:41:01 -0800456 It it = offsets_.upper_bound(last_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800457 CHECK(it != offsets_.end());
458 end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it;
459 }
Elliott Hughesed2adb62012-02-29 14:41:01 -0800460 CHECK(native_pc < end_native_pc);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800461 disassembler_->Dump(os, native_pc, end_native_pc);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800462 }
463 }
464
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800465 const OatFile& oat_file_;
466 std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800467 std::set<uint32_t> offsets_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800468 UniquePtr<Disassembler> disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700469};
470
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800471class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700472 public:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800473 explicit ImageDumper(std::ostream& os, const std::string& image_filename,
474 const std::string& host_prefix, Space& image_space,
475 const ImageHeader& image_header) : os_(os),
476 image_filename_(image_filename), host_prefix_(host_prefix),
477 image_space_(image_space), image_header_(image_header) {
478 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700479
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800480 void Dump() {
481 os_ << "MAGIC:\n";
482 os_ << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700483
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800484 os_ << "IMAGE BEGIN:\n";
485 os_ << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700486
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800487 os_ << "OAT CHECKSUM:\n";
488 os_ << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700489
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800490 os_ << "OAT BEGIN:\n";
491 os_ << reinterpret_cast<void*>(image_header_.GetOatBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700492
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800493 os_ << "OAT END:\n";
494 os_ << reinterpret_cast<void*>(image_header_.GetOatEnd()) << "\n\n";
495
496 os_ << "ROOTS:\n";
497 os_ << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700498 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700499 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
500 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700501 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800502 Object* image_root_object = image_header_.GetImageRoot(image_root);
503 os_ << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700504 if (image_root_object->IsObjectArray()) {
505 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
506 ObjectArray<Object>* image_root_object_array
507 = down_cast<ObjectArray<Object>*>(image_root_object);
508 // = image_root_object->AsObjectArray<Object>();
509 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800510 Object* value = image_root_object_array->Get(i);
511 if (value != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800512 os_ << "\t" << i << ": ";
Ian Rogersd5b32602012-02-26 16:40:04 -0800513 std::string summary;
514 PrettyObjectValue(summary, value->GetClass(), value);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800515 os_ << summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800516 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800517 os_ << StringPrintf("\t%d: null\n", i);
Ian Rogersd5b32602012-02-26 16:40:04 -0800518 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700519 }
520 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700521 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800522 os_ << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700523
Brian Carlstromaded5f72011-10-07 17:15:04 -0700524 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800525 Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation);
526 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
527 if (!host_prefix_.empty()) {
528 oat_location = host_prefix_ + oat_location;
529 os_ << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700530 }
Brian Carlstromae826982011-11-09 01:33:42 -0800531 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700532 if (oat_file == NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800533 os_ << "OAT FILE NOT FOUND: " << oat_location << std::endl << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700534 return;
535 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700536
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800537 stats_.oat_file_bytes = oat_file->Size();
538
539 oat_dumper_.reset(new OatDumper(*oat_file));
540
541 os_ << "OBJECTS:\n" << std::flush;
542 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
543 DCHECK(heap_bitmap != NULL);
544 heap_bitmap->Walk(ImageDumper::Callback, this);
545 os_ << "\n";
546
547 os_ << "STATS:\n" << std::flush;
548 UniquePtr<File> file(OS::OpenFile(image_filename_.c_str(), false));
549 stats_.file_bytes = file->Length();
550 size_t header_bytes = sizeof(ImageHeader);
551 stats_.header_bytes = header_bytes;
552 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
553 stats_.alignment_bytes += alignment_bytes;
554 stats_.Dump(os_);
555 os_ << "\n";
556
557 os_ << std::flush;
558
559 os_ << "OAT LOCATION:\n" << std::flush;
560 os_ << oat_location;
561 os_ << "\n";
562 oat_dumper_->Dump(os_);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700563 }
564
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700565 private:
566
Ian Rogersd5b32602012-02-26 16:40:04 -0800567 static void PrettyObjectValue(std::string& summary, Class* type, Object* value) {
568 CHECK(type != NULL);
569 if (value == NULL) {
570 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
571 } else if (type->IsStringClass()) {
572 String* string = value->AsString();
573 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
574 } else if (value->IsClass()) {
575 Class* klass = value->AsClass();
576 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
577 } else if (value->IsField()) {
578 Field* field = value->AsField();
579 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
580 } else if (value->IsMethod()) {
581 Method* method = value->AsMethod();
582 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
583 } else {
584 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
585 }
586 }
587
588 static void PrintField(std::string& summary, Field* field, Object* obj) {
589 FieldHelper fh(field);
590 Class* type = fh.GetType();
591 StringAppendF(&summary, "\t%s: ", fh.GetName());
592 if (type->IsPrimitiveLong()) {
593 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
594 } else if (type->IsPrimitiveDouble()) {
595 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
596 } else if (type->IsPrimitiveFloat()) {
597 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
598 } else if (type->IsPrimitive()){
599 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
600 } else {
601 Object* value = field->GetObj(obj);
602 PrettyObjectValue(summary, type, value);
603 }
604 }
605
606 static void DumpFields(std::string& summary, Object* obj, Class* klass) {
607 Class* super = klass->GetSuperClass();
608 if (super != NULL) {
609 DumpFields(summary, obj, super);
610 }
611 ObjectArray<Field>* fields = klass->GetIFields();
612 if (fields != NULL) {
613 for (int32_t i = 0; i < fields->GetLength(); i++) {
614 Field* field = fields->Get(i);
615 PrintField(summary, field, obj);
616 }
617 }
618 }
619
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800620 bool InDumpSpace(const Object* object) {
621 return image_space_.Contains(object);
622 }
623
624 const void* GetOatCode(Method* m) {
625 Runtime* runtime = Runtime::Current();
626 const void* code = m->GetCode();
Ian Rogersfb6adba2012-03-04 21:51:51 -0800627 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800628 code = oat_dumper_->GetOatCode(m);
629 }
630 return code;
631 }
632
Brian Carlstrom78128a62011-09-15 17:21:19 -0700633 static void Callback(Object* obj, void* arg) {
634 DCHECK(obj != NULL);
635 DCHECK(arg != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800636 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700637 if (!state->InDumpSpace(obj)) {
638 return;
639 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700640
641 size_t object_bytes = obj->SizeOf();
642 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
643 state->stats_.object_bytes += object_bytes;
644 state->stats_.alignment_bytes += alignment_bytes;
645
Brian Carlstrom78128a62011-09-15 17:21:19 -0700646 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800647 Class* obj_class = obj->GetClass();
648 if (obj_class->IsArrayClass()) {
649 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
650 obj->AsArray()->GetLength());
651 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700652 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800653 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
654 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700655 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800656 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700657 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800658 } else if (obj->IsField()) {
659 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
660 PrettyField(obj->AsField()).c_str());
661 } else if (obj->IsMethod()) {
662 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
663 PrettyMethod(obj->AsMethod()).c_str());
664 } else if (obj_class->IsStringClass()) {
665 StringAppendF(&summary, "%p: java.lang.String \"%s\"\n", obj,
666 obj->AsString()->ToModifiedUtf8().c_str());
667 } else {
668 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
669 }
670 DumpFields(summary, obj, obj_class);
671 if (obj->IsObjectArray()) {
672 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
673 int32_t length = obj_array->GetLength();
674 for (int32_t i = 0; i < length; i++) {
675 Object* value = obj_array->Get(i);
676 size_t run = 0;
677 for (int32_t j = i + 1; j < length; j++) {
678 if (value == obj_array->Get(j)) {
679 run++;
680 } else {
681 break;
682 }
683 }
684 if (run == 0) {
685 StringAppendF(&summary, "\t%d: ", i);
686 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800687 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800688 i = i + run;
689 }
690 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
691 PrettyObjectValue(summary, value_class, value);
692 }
693 } else if (obj->IsClass()) {
694 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
695 if (sfields != NULL) {
696 summary += "\t\tSTATICS:\n";
697 for (int32_t i = 0; i < sfields->GetLength(); i++) {
698 Field* field = sfields->Get(i);
699 PrintField(summary, field, NULL);
700 }
701 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700702 } else if (obj->IsMethod()) {
703 Method* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700704 if (method->IsNative()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700705 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800706 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700707 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800708 bool first_occurrence;
709 size_t invoke_stub_size = state->ComputeOatSize(
710 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
711 if (first_occurrence) {
712 state->stats_.managed_to_native_code_bytes += invoke_stub_size;
713 }
714 const void* oat_code = state->GetOatCode(method);
715 size_t code_size = state->ComputeOatSize(oat_code, &first_occurrence);
716 if (first_occurrence) {
717 state->stats_.native_to_managed_code_bytes += code_size;
718 }
719 if (oat_code != method->GetCode()) {
720 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
721 }
Ian Rogers19846512012-02-24 11:42:47 -0800722 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
723 method->IsResolutionMethod()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800725 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700726 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700727 } else {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800728 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
729 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700730
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800731 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700732 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700733 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800734
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800735 bool first_occurance;
736 size_t gc_map_bytes = state->ComputeOatSize(method->GetGcMapRaw(), &first_occurance);
737 if (first_occurance) {
738 state->stats_.gc_map_bytes += gc_map_bytes;
739 }
740
741 size_t pc_mapping_table_bytes =
742 state->ComputeOatSize(method->GetMappingTableRaw(), &first_occurance);
743 if (first_occurance) {
744 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
745 }
746
747 size_t vmap_table_bytes =
748 state->ComputeOatSize(method->GetVmapTableRaw(), &first_occurance);
749 if (first_occurance) {
750 state->stats_.vmap_table_bytes += vmap_table_bytes;
751 }
752
753 size_t invoke_stub_size = state->ComputeOatSize(
754 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurance);
755 if (first_occurance) {
756 state->stats_.native_to_managed_code_bytes += invoke_stub_size;
757 }
758 const void* oat_code = state->GetOatCode(method);
759 size_t code_size = state->ComputeOatSize(oat_code, &first_occurance);
760 if (first_occurance) {
761 state->stats_.managed_code_bytes += code_size;
762 }
763 state->stats_.managed_code_bytes_ignoring_deduplication += code_size;
764
765 if (oat_code != method->GetCode()) {
766 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
767 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800768 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800769 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
770
771 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
772 vmap_table_bytes + invoke_stub_size + code_size + object_bytes;
773
774 double expansion =
775 static_cast<double>(code_size) / static_cast<double>(dex_instruction_bytes);
776 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700777 }
778 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800779 std::string descriptor(ClassHelper(obj_class).GetDescriptor());
780 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
781 state->stats_.descriptor_to_count[descriptor] += 1;
782
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700783 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700784 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700785
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800786 std::set<const void*> already_seen_;
787 // Compute the size of the given data within the oat file and whether this is the first time
788 // this data has been requested
789 size_t ComputeOatSize(const void* oat_data, bool* first_occurance) {
790 if (already_seen_.count(oat_data) == 0) {
791 *first_occurance = true;
792 already_seen_.insert(oat_data);
793 } else {
794 *first_occurance = false;
795 }
796 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700797 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700798
799 public:
800 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800801 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700802 size_t file_bytes;
803
804 size_t header_bytes;
805 size_t object_bytes;
806 size_t alignment_bytes;
807
808 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800809 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700810 size_t managed_to_native_code_bytes;
811 size_t native_to_managed_code_bytes;
812
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800813 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700814 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800815 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700816
817 size_t dex_instruction_bytes;
818
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800819 std::vector<Method*> method_outlier;
820 std::vector<size_t> method_outlier_size;
821 std::vector<double> method_outlier_expansion;
822
823 explicit Stats()
824 : oat_file_bytes(0),
825 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700826 header_bytes(0),
827 object_bytes(0),
828 alignment_bytes(0),
829 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800830 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700831 managed_to_native_code_bytes(0),
832 native_to_managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800833 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700834 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800835 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700836 dex_instruction_bytes(0) {}
837
Elliott Hughese5448b52012-01-18 16:44:06 -0800838 typedef std::map<std::string, size_t> TableBytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700839 TableBytes descriptor_to_bytes;
840
Elliott Hughese5448b52012-01-18 16:44:06 -0800841 typedef std::map<std::string, size_t> TableCount;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700842 TableCount descriptor_to_count;
843
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800844 double PercentOfOatBytes(size_t size) {
845 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
846 }
847
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700848 double PercentOfFileBytes(size_t size) {
849 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
850 }
851
852 double PercentOfObjectBytes(size_t size) {
853 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
854 }
855
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800856 void ComputeOutliers(size_t total_size, double expansion, Method* method) {
857 method_outlier_size.push_back(total_size);
858 method_outlier_expansion.push_back(expansion);
859 method_outlier.push_back(method);
860 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700861
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800862 void DumpOutliers(std::ostream& os) {
863 size_t sum_of_sizes = 0;
864 size_t sum_of_sizes_squared = 0;
865 size_t sum_of_expansion = 0;
866 size_t sum_of_expansion_squared = 0;
867 size_t n = method_outlier_size.size();
868 for (size_t i = 0; i < n; i++) {
869 size_t cur_size = method_outlier_size[i];
870 sum_of_sizes += cur_size;
871 sum_of_sizes_squared += cur_size * cur_size;
872 double cur_expansion = method_outlier_expansion[i];
873 sum_of_expansion += cur_expansion;
874 sum_of_expansion_squared += cur_expansion * cur_expansion;
875 }
876 size_t size_mean = sum_of_sizes / n;
877 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
878 double expansion_mean = sum_of_expansion / n;
879 double expansion_variance =
880 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
881
882 // Dump methods whose size is a certain number of standard deviations from the mean
883 size_t dumped_values = 0;
884 size_t skipped_values = 0;
885 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
886 size_t cur_size_variance = i * i * size_variance;
887 bool first = true;
888 for (size_t j = 0; j < n; j++) {
889 size_t cur_size = method_outlier_size[j];
890 if (cur_size > size_mean) {
891 size_t cur_var = cur_size - size_mean;
892 cur_var = cur_var * cur_var;
893 if (cur_var > cur_size_variance) {
894 if (dumped_values > 20) {
895 if (i == 1) {
896 skipped_values++;
897 } else {
898 i = 2; // jump to counting for 1 standard deviation
899 break;
900 }
901 } else {
902 if (first) {
903 os << "\nBig methods (size > " << i << " standard deviations the norm):"
904 << std::endl;
905 first = false;
906 }
907 os << "\t" << PrettyMethod(method_outlier[j]) << " requires storage of "
908 << PrettySize(cur_size) << std::endl;
909 method_outlier_size[j] = 0; // don't consider this method again
910 dumped_values++;
911 }
912 }
913 }
914 }
915 }
916 if (skipped_values > 0) {
917 os << "\t... skipped " << skipped_values
918 << " methods with size > 1 standard deviation from the norm" << std::endl;
919 }
920 os << std::endl << std::flush;
921
922 // Dump methods whose expansion is a certain number of standard deviations from the mean
923 dumped_values = 0;
924 skipped_values = 0;
925 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
926 double cur_expansion_variance = i * i * expansion_variance;
927 bool first = true;
928 for (size_t j = 0; j < n; j++) {
929 double cur_expansion = method_outlier_expansion[j];
930 if (cur_expansion > expansion_mean) {
931 size_t cur_var = cur_expansion - expansion_mean;
932 cur_var = cur_var * cur_var;
933 if (cur_var > cur_expansion_variance) {
934 if (dumped_values > 20) {
935 if (i == 1) {
936 skipped_values++;
937 } else {
938 i = 2; // jump to counting for 1 standard deviation
939 break;
940 }
941 } else {
942 if (first) {
943 os << "\nLarge expansion methods (size > " << i
944 << " standard deviations the norm):" << std::endl;
945 first = false;
946 }
947 os << "\t" << PrettyMethod(method_outlier[j]) << " expanded code by "
948 << cur_expansion << std::endl;
949 method_outlier_expansion[j] = 0.0; // don't consider this method again
950 dumped_values++;
951 }
952 }
953 }
954 }
955 }
956 if (skipped_values > 0) {
957 os << "\t... skipped " << skipped_values
958 << " methods with expansion > 1 standard deviation from the norm" << std::endl;
959 }
960 os << std::endl << std::flush;
961 }
962
963 void Dump(std::ostream& os) {
964 os << "\tart_file_bytes = " << PrettySize(file_bytes) << std::endl << std::endl
965 << "\tart_file_bytes = header_bytes + object_bytes + alignment_bytes" << std::endl
966 << StringPrintf("\theader_bytes = %8zd (%2.0f%% of art file bytes)\n"
967 "\tobject_bytes = %8zd (%2.0f%% of art file bytes)\n"
968 "\talignment_bytes = %8zd (%2.0f%% of art file bytes)\n",
969 header_bytes, PercentOfFileBytes(header_bytes),
970 object_bytes, PercentOfFileBytes(object_bytes),
971 alignment_bytes, PercentOfFileBytes(alignment_bytes))
972 << std::endl << std::flush;
973
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700974 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
975
976 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
977 size_t object_bytes_total = 0;
978 typedef TableBytes::const_iterator It; // TODO: C++0x auto
979 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -0800980 const std::string& descriptor(it->first);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700981 size_t bytes = it->second;
982 size_t count = descriptor_to_count[descriptor];
983 double average = static_cast<double>(bytes) / static_cast<double>(count);
984 double percent = PercentOfObjectBytes(bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800985 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700986 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
987 descriptor.c_str(), bytes, count,
988 average, percent);
989
990 object_bytes_total += bytes;
991 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800992 os << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700993 CHECK_EQ(object_bytes, object_bytes_total);
994
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800995 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
996 "\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
997 "\tnative_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n",
998 managed_code_bytes, PercentOfOatBytes(managed_code_bytes),
999 managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes),
1000 native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes))
1001 << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001002
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001003 os << StringPrintf("\tgc_map_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1004 "\tpc_mapping_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1005 "\tvmap_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n",
1006 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1007 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1008 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
1009 << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001010
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001011 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001012 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n",
1013 static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes),
1014 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
1015 static_cast<double>(dex_instruction_bytes));
1016 os << std::endl << std::flush;
1017
1018 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001019 }
1020 } stats_;
1021
1022 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001023 UniquePtr<OatDumper> oat_dumper_;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001024 std::ostream& os_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001025 const std::string image_filename_;
1026 const std::string host_prefix_;
1027 Space& image_space_;
1028 const ImageHeader& image_header_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001029
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001030 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001031};
1032
1033int oatdump(int argc, char** argv) {
1034 // Skip over argv[0].
1035 argv++;
1036 argc--;
1037
1038 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001039 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001040 usage();
1041 }
1042
Brian Carlstromaded5f72011-10-07 17:15:04 -07001043 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001044 const char* image_filename = NULL;
1045 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001046 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001047 std::ostream* os = &std::cout;
1048 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001049
1050 for (int i = 0; i < argc; i++) {
1051 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001052 if (option.starts_with("--oat-file=")) {
1053 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001054 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001055 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001056 } else if (option.starts_with("--boot-image=")) {
1057 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001058 } else if (option.starts_with("--host-prefix=")) {
1059 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001060 } else if (option.starts_with("--output=")) {
1061 const char* filename = option.substr(strlen("--output=")).data();
1062 out.reset(new std::ofstream(filename));
1063 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001064 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001065 usage();
1066 }
1067 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001068 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001069 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001070 usage();
1071 }
1072 }
1073
Brian Carlstromaded5f72011-10-07 17:15:04 -07001074 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001075 fprintf(stderr, "Either --image or --oat must be specified\n");
1076 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001077 }
1078
Brian Carlstromaded5f72011-10-07 17:15:04 -07001079 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001080 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1081 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001082 }
1083
1084 if (oat_filename != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001085 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001086 if (oat_file == NULL) {
1087 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
1088 return EXIT_FAILURE;
1089 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001090 OatDumper oat_dumper(*oat_file);
1091 oat_dumper.Dump(*os);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001092 return EXIT_SUCCESS;
1093 }
1094
Brian Carlstrom78128a62011-09-15 17:21:19 -07001095 Runtime::Options options;
1096 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001097 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001098 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001099 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001100 if (boot_image_filename != NULL) {
1101 boot_image_option += "-Ximage:";
1102 boot_image_option += boot_image_filename;
1103 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001104 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001105 if (image_filename != NULL) {
1106 image_option += "-Ximage:";
1107 image_option += image_filename;
1108 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1109 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001110
Brian Carlstromfe487d02012-02-29 18:49:16 -08001111 if (host_prefix.empty()) {
1112 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
1113 if (android_product_out != NULL) {
1114 host_prefix = android_product_out;
1115 }
1116 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001117 if (!host_prefix.empty()) {
1118 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
1119 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001120
1121 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
1122 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001123 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001124 return EXIT_FAILURE;
1125 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001126
Ian Rogers30fab402012-01-23 15:43:46 -08001127 ImageSpace* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]->AsImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001128 CHECK(image_space != NULL);
1129 const ImageHeader& image_header = image_space->GetImageHeader();
1130 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001131 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001132 return EXIT_FAILURE;
1133 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001134 ImageDumper image_dumper(*os, image_filename, host_prefix, *image_space, image_header);
1135 image_dumper.Dump();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001136 return EXIT_SUCCESS;
1137}
1138
1139} // namespace art
1140
1141int main(int argc, char** argv) {
1142 return art::oatdump(argc, argv);
1143}