blob: 5574bb7b5bba9968d13ab2266fff6fcb33543245 [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 Carlstrom4dcbffb2012-03-07 00:21:18 -0800524 os_ << "OAT LOCATION:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700525 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800526 Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation);
527 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800528 os_ << oat_location;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800529 if (!host_prefix_.empty()) {
530 oat_location = host_prefix_ + oat_location;
531 os_ << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700532 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800533 os_ << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800534 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700535 if (oat_file == NULL) {
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800536 os_ << "NOT FOUND\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700537 return;
538 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800539 os_ << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700540
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800541 stats_.oat_file_bytes = oat_file->Size();
542
543 oat_dumper_.reset(new OatDumper(*oat_file));
544
545 os_ << "OBJECTS:\n" << std::flush;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800546 HeapBitmap* heap_bitmap = Runtime::Current()->GetHeap()->GetLiveBits();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800547 DCHECK(heap_bitmap != NULL);
548 heap_bitmap->Walk(ImageDumper::Callback, this);
549 os_ << "\n";
550
551 os_ << "STATS:\n" << std::flush;
552 UniquePtr<File> file(OS::OpenFile(image_filename_.c_str(), false));
553 stats_.file_bytes = file->Length();
554 size_t header_bytes = sizeof(ImageHeader);
555 stats_.header_bytes = header_bytes;
556 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
557 stats_.alignment_bytes += alignment_bytes;
558 stats_.Dump(os_);
559 os_ << "\n";
560
561 os_ << std::flush;
562
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800563 oat_dumper_->Dump(os_);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700564 }
565
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700566 private:
567
Ian Rogersd5b32602012-02-26 16:40:04 -0800568 static void PrettyObjectValue(std::string& summary, Class* type, Object* value) {
569 CHECK(type != NULL);
570 if (value == NULL) {
571 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
572 } else if (type->IsStringClass()) {
573 String* string = value->AsString();
574 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
575 } else if (value->IsClass()) {
576 Class* klass = value->AsClass();
577 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
578 } else if (value->IsField()) {
579 Field* field = value->AsField();
580 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
581 } else if (value->IsMethod()) {
582 Method* method = value->AsMethod();
583 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
584 } else {
585 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
586 }
587 }
588
589 static void PrintField(std::string& summary, Field* field, Object* obj) {
590 FieldHelper fh(field);
591 Class* type = fh.GetType();
592 StringAppendF(&summary, "\t%s: ", fh.GetName());
593 if (type->IsPrimitiveLong()) {
594 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
595 } else if (type->IsPrimitiveDouble()) {
596 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
597 } else if (type->IsPrimitiveFloat()) {
598 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
599 } else if (type->IsPrimitive()){
600 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
601 } else {
602 Object* value = field->GetObj(obj);
603 PrettyObjectValue(summary, type, value);
604 }
605 }
606
607 static void DumpFields(std::string& summary, Object* obj, Class* klass) {
608 Class* super = klass->GetSuperClass();
609 if (super != NULL) {
610 DumpFields(summary, obj, super);
611 }
612 ObjectArray<Field>* fields = klass->GetIFields();
613 if (fields != NULL) {
614 for (int32_t i = 0; i < fields->GetLength(); i++) {
615 Field* field = fields->Get(i);
616 PrintField(summary, field, obj);
617 }
618 }
619 }
620
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800621 bool InDumpSpace(const Object* object) {
622 return image_space_.Contains(object);
623 }
624
625 const void* GetOatCode(Method* m) {
626 Runtime* runtime = Runtime::Current();
627 const void* code = m->GetCode();
Ian Rogersfb6adba2012-03-04 21:51:51 -0800628 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800629 code = oat_dumper_->GetOatCode(m);
630 }
631 return code;
632 }
633
Brian Carlstrom78128a62011-09-15 17:21:19 -0700634 static void Callback(Object* obj, void* arg) {
635 DCHECK(obj != NULL);
636 DCHECK(arg != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800637 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700638 if (!state->InDumpSpace(obj)) {
639 return;
640 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700641
642 size_t object_bytes = obj->SizeOf();
643 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
644 state->stats_.object_bytes += object_bytes;
645 state->stats_.alignment_bytes += alignment_bytes;
646
Brian Carlstrom78128a62011-09-15 17:21:19 -0700647 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800648 Class* obj_class = obj->GetClass();
649 if (obj_class->IsArrayClass()) {
650 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
651 obj->AsArray()->GetLength());
652 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700653 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800654 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
655 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700656 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800657 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700658 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800659 } else if (obj->IsField()) {
660 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
661 PrettyField(obj->AsField()).c_str());
662 } else if (obj->IsMethod()) {
663 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
664 PrettyMethod(obj->AsMethod()).c_str());
665 } else if (obj_class->IsStringClass()) {
666 StringAppendF(&summary, "%p: java.lang.String \"%s\"\n", obj,
667 obj->AsString()->ToModifiedUtf8().c_str());
668 } else {
669 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
670 }
671 DumpFields(summary, obj, obj_class);
672 if (obj->IsObjectArray()) {
673 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
674 int32_t length = obj_array->GetLength();
675 for (int32_t i = 0; i < length; i++) {
676 Object* value = obj_array->Get(i);
677 size_t run = 0;
678 for (int32_t j = i + 1; j < length; j++) {
679 if (value == obj_array->Get(j)) {
680 run++;
681 } else {
682 break;
683 }
684 }
685 if (run == 0) {
686 StringAppendF(&summary, "\t%d: ", i);
687 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800688 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800689 i = i + run;
690 }
691 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
692 PrettyObjectValue(summary, value_class, value);
693 }
694 } else if (obj->IsClass()) {
695 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
696 if (sfields != NULL) {
697 summary += "\t\tSTATICS:\n";
698 for (int32_t i = 0; i < sfields->GetLength(); i++) {
699 Field* field = sfields->Get(i);
700 PrintField(summary, field, NULL);
701 }
702 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700703 } else if (obj->IsMethod()) {
704 Method* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700705 if (method->IsNative()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700706 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800707 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700708 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800709 bool first_occurrence;
710 size_t invoke_stub_size = state->ComputeOatSize(
711 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
712 if (first_occurrence) {
713 state->stats_.managed_to_native_code_bytes += invoke_stub_size;
714 }
715 const void* oat_code = state->GetOatCode(method);
716 size_t code_size = state->ComputeOatSize(oat_code, &first_occurrence);
717 if (first_occurrence) {
718 state->stats_.native_to_managed_code_bytes += code_size;
719 }
720 if (oat_code != method->GetCode()) {
721 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
722 }
Ian Rogers19846512012-02-24 11:42:47 -0800723 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
724 method->IsResolutionMethod()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700725 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800726 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700727 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700728 } else {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800729 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
730 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700731
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800732 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700733 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700734 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800735
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800736 bool first_occurance;
737 size_t gc_map_bytes = state->ComputeOatSize(method->GetGcMapRaw(), &first_occurance);
738 if (first_occurance) {
739 state->stats_.gc_map_bytes += gc_map_bytes;
740 }
741
742 size_t pc_mapping_table_bytes =
743 state->ComputeOatSize(method->GetMappingTableRaw(), &first_occurance);
744 if (first_occurance) {
745 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
746 }
747
748 size_t vmap_table_bytes =
749 state->ComputeOatSize(method->GetVmapTableRaw(), &first_occurance);
750 if (first_occurance) {
751 state->stats_.vmap_table_bytes += vmap_table_bytes;
752 }
753
754 size_t invoke_stub_size = state->ComputeOatSize(
755 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurance);
756 if (first_occurance) {
757 state->stats_.native_to_managed_code_bytes += invoke_stub_size;
758 }
759 const void* oat_code = state->GetOatCode(method);
760 size_t code_size = state->ComputeOatSize(oat_code, &first_occurance);
761 if (first_occurance) {
762 state->stats_.managed_code_bytes += code_size;
763 }
764 state->stats_.managed_code_bytes_ignoring_deduplication += code_size;
765
766 if (oat_code != method->GetCode()) {
767 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
768 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800769 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800770 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
771
772 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
773 vmap_table_bytes + invoke_stub_size + code_size + object_bytes;
774
775 double expansion =
776 static_cast<double>(code_size) / static_cast<double>(dex_instruction_bytes);
777 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700778 }
779 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800780 std::string descriptor(ClassHelper(obj_class).GetDescriptor());
781 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
782 state->stats_.descriptor_to_count[descriptor] += 1;
783
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700784 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700785 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700786
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800787 std::set<const void*> already_seen_;
788 // Compute the size of the given data within the oat file and whether this is the first time
789 // this data has been requested
790 size_t ComputeOatSize(const void* oat_data, bool* first_occurance) {
791 if (already_seen_.count(oat_data) == 0) {
792 *first_occurance = true;
793 already_seen_.insert(oat_data);
794 } else {
795 *first_occurance = false;
796 }
797 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700798 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700799
800 public:
801 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800802 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700803 size_t file_bytes;
804
805 size_t header_bytes;
806 size_t object_bytes;
807 size_t alignment_bytes;
808
809 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800810 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700811 size_t managed_to_native_code_bytes;
812 size_t native_to_managed_code_bytes;
813
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800814 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700815 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800816 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700817
818 size_t dex_instruction_bytes;
819
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800820 std::vector<Method*> method_outlier;
821 std::vector<size_t> method_outlier_size;
822 std::vector<double> method_outlier_expansion;
823
824 explicit Stats()
825 : oat_file_bytes(0),
826 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700827 header_bytes(0),
828 object_bytes(0),
829 alignment_bytes(0),
830 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800831 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700832 managed_to_native_code_bytes(0),
833 native_to_managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800834 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700835 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800836 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700837 dex_instruction_bytes(0) {}
838
Elliott Hughese5448b52012-01-18 16:44:06 -0800839 typedef std::map<std::string, size_t> TableBytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700840 TableBytes descriptor_to_bytes;
841
Elliott Hughese5448b52012-01-18 16:44:06 -0800842 typedef std::map<std::string, size_t> TableCount;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700843 TableCount descriptor_to_count;
844
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800845 double PercentOfOatBytes(size_t size) {
846 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
847 }
848
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700849 double PercentOfFileBytes(size_t size) {
850 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
851 }
852
853 double PercentOfObjectBytes(size_t size) {
854 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
855 }
856
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800857 void ComputeOutliers(size_t total_size, double expansion, Method* method) {
858 method_outlier_size.push_back(total_size);
859 method_outlier_expansion.push_back(expansion);
860 method_outlier.push_back(method);
861 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700862
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800863 void DumpOutliers(std::ostream& os) {
864 size_t sum_of_sizes = 0;
865 size_t sum_of_sizes_squared = 0;
866 size_t sum_of_expansion = 0;
867 size_t sum_of_expansion_squared = 0;
868 size_t n = method_outlier_size.size();
869 for (size_t i = 0; i < n; i++) {
870 size_t cur_size = method_outlier_size[i];
871 sum_of_sizes += cur_size;
872 sum_of_sizes_squared += cur_size * cur_size;
873 double cur_expansion = method_outlier_expansion[i];
874 sum_of_expansion += cur_expansion;
875 sum_of_expansion_squared += cur_expansion * cur_expansion;
876 }
877 size_t size_mean = sum_of_sizes / n;
878 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
879 double expansion_mean = sum_of_expansion / n;
880 double expansion_variance =
881 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
882
883 // Dump methods whose size is a certain number of standard deviations from the mean
884 size_t dumped_values = 0;
885 size_t skipped_values = 0;
886 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
887 size_t cur_size_variance = i * i * size_variance;
888 bool first = true;
889 for (size_t j = 0; j < n; j++) {
890 size_t cur_size = method_outlier_size[j];
891 if (cur_size > size_mean) {
892 size_t cur_var = cur_size - size_mean;
893 cur_var = cur_var * cur_var;
894 if (cur_var > cur_size_variance) {
895 if (dumped_values > 20) {
896 if (i == 1) {
897 skipped_values++;
898 } else {
899 i = 2; // jump to counting for 1 standard deviation
900 break;
901 }
902 } else {
903 if (first) {
904 os << "\nBig methods (size > " << i << " standard deviations the norm):"
905 << std::endl;
906 first = false;
907 }
908 os << "\t" << PrettyMethod(method_outlier[j]) << " requires storage of "
909 << PrettySize(cur_size) << std::endl;
910 method_outlier_size[j] = 0; // don't consider this method again
911 dumped_values++;
912 }
913 }
914 }
915 }
916 }
917 if (skipped_values > 0) {
918 os << "\t... skipped " << skipped_values
919 << " methods with size > 1 standard deviation from the norm" << std::endl;
920 }
921 os << std::endl << std::flush;
922
923 // Dump methods whose expansion is a certain number of standard deviations from the mean
924 dumped_values = 0;
925 skipped_values = 0;
926 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
927 double cur_expansion_variance = i * i * expansion_variance;
928 bool first = true;
929 for (size_t j = 0; j < n; j++) {
930 double cur_expansion = method_outlier_expansion[j];
931 if (cur_expansion > expansion_mean) {
932 size_t cur_var = cur_expansion - expansion_mean;
933 cur_var = cur_var * cur_var;
934 if (cur_var > cur_expansion_variance) {
935 if (dumped_values > 20) {
936 if (i == 1) {
937 skipped_values++;
938 } else {
939 i = 2; // jump to counting for 1 standard deviation
940 break;
941 }
942 } else {
943 if (first) {
944 os << "\nLarge expansion methods (size > " << i
945 << " standard deviations the norm):" << std::endl;
946 first = false;
947 }
948 os << "\t" << PrettyMethod(method_outlier[j]) << " expanded code by "
949 << cur_expansion << std::endl;
950 method_outlier_expansion[j] = 0.0; // don't consider this method again
951 dumped_values++;
952 }
953 }
954 }
955 }
956 }
957 if (skipped_values > 0) {
958 os << "\t... skipped " << skipped_values
959 << " methods with expansion > 1 standard deviation from the norm" << std::endl;
960 }
961 os << std::endl << std::flush;
962 }
963
964 void Dump(std::ostream& os) {
965 os << "\tart_file_bytes = " << PrettySize(file_bytes) << std::endl << std::endl
966 << "\tart_file_bytes = header_bytes + object_bytes + alignment_bytes" << std::endl
967 << StringPrintf("\theader_bytes = %8zd (%2.0f%% of art file bytes)\n"
968 "\tobject_bytes = %8zd (%2.0f%% of art file bytes)\n"
969 "\talignment_bytes = %8zd (%2.0f%% of art file bytes)\n",
970 header_bytes, PercentOfFileBytes(header_bytes),
971 object_bytes, PercentOfFileBytes(object_bytes),
972 alignment_bytes, PercentOfFileBytes(alignment_bytes))
973 << std::endl << std::flush;
974
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700975 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
976
977 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
978 size_t object_bytes_total = 0;
979 typedef TableBytes::const_iterator It; // TODO: C++0x auto
980 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -0800981 const std::string& descriptor(it->first);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700982 size_t bytes = it->second;
983 size_t count = descriptor_to_count[descriptor];
984 double average = static_cast<double>(bytes) / static_cast<double>(count);
985 double percent = PercentOfObjectBytes(bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800986 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700987 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
988 descriptor.c_str(), bytes, count,
989 average, percent);
990
991 object_bytes_total += bytes;
992 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800993 os << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700994 CHECK_EQ(object_bytes, object_bytes_total);
995
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800996 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
997 "\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
998 "\tnative_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n",
999 managed_code_bytes, PercentOfOatBytes(managed_code_bytes),
1000 managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes),
1001 native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes))
1002 << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001003
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001004 os << StringPrintf("\tgc_map_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1005 "\tpc_mapping_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1006 "\tvmap_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n",
1007 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1008 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1009 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
1010 << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001011
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001012 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001013 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n",
1014 static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes),
1015 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
1016 static_cast<double>(dex_instruction_bytes));
1017 os << std::endl << std::flush;
1018
1019 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001020 }
1021 } stats_;
1022
1023 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001024 UniquePtr<OatDumper> oat_dumper_;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001025 std::ostream& os_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001026 const std::string image_filename_;
1027 const std::string host_prefix_;
1028 Space& image_space_;
1029 const ImageHeader& image_header_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001030
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001031 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001032};
1033
1034int oatdump(int argc, char** argv) {
1035 // Skip over argv[0].
1036 argv++;
1037 argc--;
1038
1039 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001040 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001041 usage();
1042 }
1043
Brian Carlstromaded5f72011-10-07 17:15:04 -07001044 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001045 const char* image_filename = NULL;
1046 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001047 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001048 std::ostream* os = &std::cout;
1049 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001050
1051 for (int i = 0; i < argc; i++) {
1052 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001053 if (option.starts_with("--oat-file=")) {
1054 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001055 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001056 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001057 } else if (option.starts_with("--boot-image=")) {
1058 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001059 } else if (option.starts_with("--host-prefix=")) {
1060 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001061 } else if (option.starts_with("--output=")) {
1062 const char* filename = option.substr(strlen("--output=")).data();
1063 out.reset(new std::ofstream(filename));
1064 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001065 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001066 usage();
1067 }
1068 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001069 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001070 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001071 usage();
1072 }
1073 }
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\n");
1077 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001078 }
1079
Brian Carlstromaded5f72011-10-07 17:15:04 -07001080 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001081 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1082 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001083 }
1084
1085 if (oat_filename != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001086 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001087 if (oat_file == NULL) {
1088 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
1089 return EXIT_FAILURE;
1090 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001091 OatDumper oat_dumper(*oat_file);
1092 oat_dumper.Dump(*os);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001093 return EXIT_SUCCESS;
1094 }
1095
Brian Carlstrom78128a62011-09-15 17:21:19 -07001096 Runtime::Options options;
1097 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001098 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001099 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001100 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001101 if (boot_image_filename != NULL) {
1102 boot_image_option += "-Ximage:";
1103 boot_image_option += boot_image_filename;
1104 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001105 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001106 if (image_filename != NULL) {
1107 image_option += "-Ximage:";
1108 image_option += image_filename;
1109 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1110 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001111
Brian Carlstromfe487d02012-02-29 18:49:16 -08001112 if (host_prefix.empty()) {
1113 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
1114 if (android_product_out != NULL) {
1115 host_prefix = android_product_out;
1116 }
1117 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001118 if (!host_prefix.empty()) {
1119 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
1120 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001121
1122 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
1123 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001124 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001125 return EXIT_FAILURE;
1126 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001127
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001128 Heap* heap = Runtime::Current()->GetHeap();
1129 ImageSpace* image_space = heap->GetSpaces()[heap->GetSpaces().size()-2]->AsImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001130 CHECK(image_space != NULL);
1131 const ImageHeader& image_header = image_space->GetImageHeader();
1132 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001133 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001134 return EXIT_FAILURE;
1135 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001136 ImageDumper image_dumper(*os, image_filename, host_prefix, *image_space, image_header);
1137 image_dumper.Dump();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001138 return EXIT_SUCCESS;
1139}
1140
1141} // namespace art
1142
1143int main(int argc, char** argv) {
1144 return art::oatdump(argc, argv);
1145}