blob: c7b81512fa3c843e26a9b66b1757427d7871a3c0 [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),
89 oat_dex_files_(oat_file.GetOatDexFiles()), disassembler_(Disassembler::Create(kArm)) {
90 // TODO: the disassembler should find the oat file instruction set from the oat header
91 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
103 os << "DEX FILE COUNT:\n";
104 os << oat_header.GetDexFileCount() << "\n\n";
105
106 os << "EXECUTABLE OFFSET:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800107 os << StringPrintf("0x%08x\n\n", oat_header.GetExecutableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700108
Ian Rogers30fab402012-01-23 15:43:46 -0800109 os << "BEGIN:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800110 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700111
Ian Rogers30fab402012-01-23 15:43:46 -0800112 os << "END:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800113 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700114
115 os << std::flush;
116
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800117 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
118 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Brian Carlstromaded5f72011-10-07 17:15:04 -0700119 CHECK(oat_dex_file != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800120 DumpOatDexFile(os, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700121 }
122 }
123
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800124 size_t ComputeSize(const void* oat_data) {
125 if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() ||
126 reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) {
127 return 0; // Address not in oat file
128 }
129 uint32_t begin_offset = reinterpret_cast<size_t>(oat_data) -
130 reinterpret_cast<size_t>(oat_file_.Begin());
131 typedef std::set<uint32_t>::iterator It;
132 It it = offsets_.upper_bound(begin_offset);
133 CHECK(it != offsets_.end());
134 uint32_t end_offset = *it;
135 return end_offset - begin_offset;
136 }
137
138 const void* GetOatCode(Method* m) {
139 MethodHelper mh(m);
140 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
141 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
142 CHECK(oat_dex_file != NULL);
143 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
144 if (dex_file.get() != NULL) {
145 uint32_t class_def_index;
146 bool found = dex_file->FindClassDefIndex(mh.GetDeclaringClassDescriptor(), class_def_index);
147 if (found) {
148 const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_index);
149 CHECK(oat_class != NULL);
150 size_t method_index = m->GetMethodIndex();
151 return oat_class->GetOatMethod(method_index).GetCode();
152 }
153 }
154 }
155 return NULL;
156 }
157
Brian Carlstromaded5f72011-10-07 17:15:04 -0700158 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800159 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800160 // We don't know the length of the code for each method, but we need to know where to stop
161 // when disassembling. What we do know is that a region of code will be followed by some other
162 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
163 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800164 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
165 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800166 CHECK(oat_dex_file != NULL);
167 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
168 if (dex_file.get() == NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800169 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800170 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800171 offsets_.insert(reinterpret_cast<uint32_t>(&dex_file->GetHeader()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800172 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
173 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
174 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index));
175 const byte* class_data = dex_file->GetClassData(class_def);
176 if (class_data != NULL) {
177 ClassDataItemIterator it(*dex_file, class_data);
178 SkipAllFields(it);
179 uint32_t class_method_index = 0;
180 while (it.HasNextDirectMethod()) {
181 AddOffsets(oat_class->GetOatMethod(class_method_index++));
182 it.Next();
183 }
184 while (it.HasNextVirtualMethod()) {
185 AddOffsets(oat_class->GetOatMethod(class_method_index++));
186 it.Next();
187 }
188 }
189 }
190 }
191
192 // If the last thing in the file is code for a method, there won't be an offset for the "next"
193 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
194 // for the end of the file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800195 offsets_.insert(static_cast<uint32_t>(oat_file_.End() - oat_file_.Begin()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800196 }
197
198 void AddOffsets(const OatFile::OatMethod& oat_method) {
199 offsets_.insert(oat_method.GetCodeOffset());
200 offsets_.insert(oat_method.GetMappingTableOffset());
201 offsets_.insert(oat_method.GetVmapTableOffset());
202 offsets_.insert(oat_method.GetGcMapOffset());
203 offsets_.insert(oat_method.GetInvokeStubOffset());
204 }
205
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800206 void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700207 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800208 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800209 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800210 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile());
211 if (dex_file.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700212 os << "NOT FOUND\n\n";
213 return;
214 }
215 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
216 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
217 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700218 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
219 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800220 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800221 << oat_class->GetStatus() << ")\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800222 DumpOatClass(os, *oat_class.get(), *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700223 }
224
225 os << std::flush;
226 }
227
Elliott Hughese3c845c2012-02-28 17:23:01 -0800228 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700229 while (it.HasNextStaticField()) {
230 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700231 }
Ian Rogers0571d352011-11-03 19:51:38 -0700232 while (it.HasNextInstanceField()) {
233 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700234 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800235 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700236
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800237 void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
238 const DexFile::ClassDef& class_def) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800239 const byte* class_data = dex_file.GetClassData(class_def);
240 if (class_data == NULL) { // empty class such as a marker interface?
241 return;
242 }
243 ClassDataItemIterator it(dex_file, class_data);
244 SkipAllFields(it);
245
246 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700247 while (it.HasNextDirectMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800248 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800249 DumpOatMethod(os, class_method_index, oat_method, dex_file,
250 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800251 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700252 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700253 }
Ian Rogers0571d352011-11-03 19:51:38 -0700254 while (it.HasNextVirtualMethod()) {
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 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700262 os << std::flush;
263 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800264
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800265 void DumpOatMethod(std::ostream& os, uint32_t class_method_index,
Elliott Hughese3c845c2012-02-28 17:23:01 -0800266 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
267 uint32_t dex_method_idx, const DexFile::CodeItem* code_item) {
268 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700269 const char* name = dex_file.GetMethodName(method_id);
Elliott Hughes95572412011-12-13 18:14:20 -0800270 std::string signature(dex_file.GetMethodSignature(method_id));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800271 os << StringPrintf("\t%d: %s %s (dex_method_idx=%d)\n",
272 class_method_index, name, signature.c_str(), dex_method_idx);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800273 os << StringPrintf("\t\tcode: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800274 oat_method.GetCode(), oat_method.GetCodeOffset());
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800275 os << StringPrintf("\t\tframe_size_in_bytes: %zd\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800276 oat_method.GetFrameSizeInBytes());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800277 os << StringPrintf("\t\tcore_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800278 oat_method.GetCoreSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800279 DumpSpillMask(os, oat_method.GetCoreSpillMask(), false);
280 os << StringPrintf("\n\t\tfp_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800281 oat_method.GetFpSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800282 DumpSpillMask(os, oat_method.GetFpSpillMask(), true);
283 os << StringPrintf("\n\t\tmapping_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800284 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800285 DumpMappingTable(os, oat_method);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800286 os << StringPrintf("\t\tvmap_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800287 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800288 DumpVmap(os, oat_method.GetVmapTable(), oat_method.GetCoreSpillMask(),
289 oat_method.GetFpSpillMask());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800290 os << StringPrintf("\t\tgc_map: %p (offset=0x%08x)\n",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800291 oat_method.GetGcMap(), oat_method.GetGcMapOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800292 DumpGcMap(os, oat_method.GetGcMap());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800293 os << StringPrintf("\t\tinvoke_stub: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800294 oat_method.GetInvokeStub(), oat_method.GetInvokeStubOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800295 os << "\t\tCODE:\n";
296 DumpCode(os, oat_method.GetCode(), oat_method.GetMappingTable(), dex_file, code_item);
297 os << "\t\tINVOKE STUB:\n";
298 DumpCode(os, reinterpret_cast<const void*>(oat_method.GetInvokeStub()), NULL, dex_file, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700299 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800300
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800301 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
302 if (spill_mask == 0) {
303 return;
304 }
305 os << " (";
306 for (size_t i = 0; i < 32; i++) {
307 if ((spill_mask & (1 << i)) != 0) {
308 if (is_float) {
309 os << "fr" << i;
310 } else {
311 os << "r" << i;
312 }
313 spill_mask ^= 1 << i; // clear bit
314 if (spill_mask != 0) {
315 os << ", ";
316 } else {
317 break;
318 }
319 }
320 }
321 os << ")";
322 }
323
324 void DumpVmap(std::ostream& os, const uint16_t* raw_table, uint32_t core_spill_mask,
325 uint32_t fp_spill_mask) {
326 if (raw_table == NULL) {
327 return;
328 }
329 const VmapTable vmap_table(raw_table);
330 bool first = true;
331 os << "\t\t\t";
332 for (size_t i = 0; i < vmap_table.size(); i++) {
333 uint16_t dex_reg = vmap_table[i];
334 size_t matches = 0;
335 size_t spill_shifts = 0;
336 uint32_t spill_mask = core_spill_mask;
337 bool processing_fp = false;
338 while (matches != (i + 1)) {
339 if (spill_mask == 0) {
340 CHECK(!processing_fp);
341 spill_mask = fp_spill_mask;
342 processing_fp = true;
343 }
344 matches += spill_mask & 1; // Add 1 if the low bit is set
345 spill_mask >>= 1;
346 spill_shifts++;
347 }
348 size_t arm_reg = spill_shifts - 1; // wind back one as we want the last match
349 os << (first ? "v" : ", v") << dex_reg;
350 if (arm_reg < 16) {
351 os << "/r" << arm_reg;
352 } else {
353 os << "/fr" << (arm_reg - 16);
354 }
355 if (first) {
356 first = false;
357 }
358 }
359 os << std::endl;
360 }
361
362 void DumpGcMap(std::ostream& os, const uint8_t* gc_map_raw) {
363 if (gc_map_raw == NULL) {
364 return;
365 }
366 uint32_t gc_map_length = (gc_map_raw[0] << 24) | (gc_map_raw[1] << 16) |
367 (gc_map_raw[2] << 8) | (gc_map_raw[3] << 0);
368 verifier::PcToReferenceMap map(gc_map_raw + sizeof(uint32_t), gc_map_length);
369 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
370 os << StringPrintf("\t\t\t0x%04x", map.GetPC(entry));
371 size_t num_regs = map.RegWidth() * 8;
372 const uint8_t* reg_bitmap = map.GetBitMap(entry);
373 bool first = true;
374 for (size_t reg = 0; reg < num_regs; reg++) {
375 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
376 if (first) {
377 os << " v" << reg;
378 first = false;
379 } else {
380 os << ", v" << reg;
381 }
382 }
383 }
384 os << std::endl;
385 }
386 }
387
388 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800389 const uint32_t* raw_table = oat_method.GetMappingTable();
390 const void* code = oat_method.GetCode();
391 if (raw_table == NULL || code == NULL) {
392 return;
393 }
394
395 uint32_t length = *raw_table;
396 ++raw_table;
397
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800398 os << "\t\t{";
Elliott Hughese3c845c2012-02-28 17:23:01 -0800399 for (size_t i = 0; i < length; i += 2) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800400 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800401 uint32_t dex_pc = raw_table[i + 1];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800402 os << StringPrintf("%p -> 0x%04x", native_pc, dex_pc);
403 if (i + 2 < length) {
404 os << ", ";
405 }
406 }
407 os << "}" << std::endl << std::flush;
408 }
409
410 void DumpCode(std::ostream& os, const void* code, const uint32_t* raw_mapping_table,
411 const DexFile& dex_file, const DexFile::CodeItem* code_item) {
412 if (code == NULL) {
413 return;
414 }
415
416 if (raw_mapping_table == NULL) {
417 // code but no mapping table is most likely caused by code created by the JNI compiler
418 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code);
419 const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file_.Begin());
420 uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin);
421
422 typedef std::set<uint32_t>::iterator It;
423 It it = offsets_.upper_bound(last_offset);
424 CHECK(it != offsets_.end());
425 const uint8_t* end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it;
426 CHECK(native_pc < end_native_pc);
427
428 disassembler_->Dump(os, native_pc, end_native_pc);
429 return;
430 }
431
432 uint32_t length = *raw_mapping_table;
433 ++raw_mapping_table;
434
435 for (size_t i = 0; i < length; i += 2) {
436 uint32_t dex_pc = raw_mapping_table[i + 1];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800437 const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]);
438 os << StringPrintf("\t\t0x%04x: %s\n", dex_pc, instruction->DumpString(&dex_file).c_str());
439
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800440 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800441 const uint8_t* end_native_pc = NULL;
442 if (i + 2 < length) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800443 end_native_pc = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i + 2];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800444 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800445 const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file_.Begin());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800446 uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin);
447
448 typedef std::set<uint32_t>::iterator It;
Elliott Hughesed2adb62012-02-29 14:41:01 -0800449 It it = offsets_.upper_bound(last_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800450 CHECK(it != offsets_.end());
451 end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it;
452 }
Elliott Hughesed2adb62012-02-29 14:41:01 -0800453 CHECK(native_pc < end_native_pc);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800454 disassembler_->Dump(os, native_pc, end_native_pc);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800455 }
456 }
457
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800458 const OatFile& oat_file_;
459 std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800460 std::set<uint32_t> offsets_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800461 UniquePtr<Disassembler> disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700462};
463
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800464class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700465 public:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800466 explicit ImageDumper(std::ostream& os, const std::string& image_filename,
467 const std::string& host_prefix, Space& image_space,
468 const ImageHeader& image_header) : os_(os),
469 image_filename_(image_filename), host_prefix_(host_prefix),
470 image_space_(image_space), image_header_(image_header) {
471 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700472
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800473 void Dump() {
474 os_ << "MAGIC:\n";
475 os_ << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700476
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800477 os_ << "IMAGE BEGIN:\n";
478 os_ << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700479
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800480 os_ << "OAT CHECKSUM:\n";
481 os_ << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700482
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800483 os_ << "OAT BEGIN:\n";
484 os_ << reinterpret_cast<void*>(image_header_.GetOatBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700485
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800486 os_ << "OAT END:\n";
487 os_ << reinterpret_cast<void*>(image_header_.GetOatEnd()) << "\n\n";
488
489 os_ << "ROOTS:\n";
490 os_ << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700491 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700492 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
493 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700494 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800495 Object* image_root_object = image_header_.GetImageRoot(image_root);
496 os_ << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700497 if (image_root_object->IsObjectArray()) {
498 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
499 ObjectArray<Object>* image_root_object_array
500 = down_cast<ObjectArray<Object>*>(image_root_object);
501 // = image_root_object->AsObjectArray<Object>();
502 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800503 Object* value = image_root_object_array->Get(i);
504 if (value != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800505 os_ << "\t" << i << ": ";
Ian Rogersd5b32602012-02-26 16:40:04 -0800506 std::string summary;
507 PrettyObjectValue(summary, value->GetClass(), value);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800508 os_ << summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800509 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800510 os_ << StringPrintf("\t%d: null\n", i);
Ian Rogersd5b32602012-02-26 16:40:04 -0800511 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700512 }
513 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700514 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800515 os_ << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700516
Brian Carlstromaded5f72011-10-07 17:15:04 -0700517 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800518 Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation);
519 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
520 if (!host_prefix_.empty()) {
521 oat_location = host_prefix_ + oat_location;
522 os_ << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700523 }
Brian Carlstromae826982011-11-09 01:33:42 -0800524 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700525 if (oat_file == NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800526 os_ << "OAT FILE NOT FOUND: " << oat_location << std::endl << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700527 return;
528 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700529
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800530 stats_.oat_file_bytes = oat_file->Size();
531
532 oat_dumper_.reset(new OatDumper(*oat_file));
533
534 os_ << "OBJECTS:\n" << std::flush;
535 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
536 DCHECK(heap_bitmap != NULL);
537 heap_bitmap->Walk(ImageDumper::Callback, this);
538 os_ << "\n";
539
540 os_ << "STATS:\n" << std::flush;
541 UniquePtr<File> file(OS::OpenFile(image_filename_.c_str(), false));
542 stats_.file_bytes = file->Length();
543 size_t header_bytes = sizeof(ImageHeader);
544 stats_.header_bytes = header_bytes;
545 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
546 stats_.alignment_bytes += alignment_bytes;
547 stats_.Dump(os_);
548 os_ << "\n";
549
550 os_ << std::flush;
551
552 os_ << "OAT LOCATION:\n" << std::flush;
553 os_ << oat_location;
554 os_ << "\n";
555 oat_dumper_->Dump(os_);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700556 }
557
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700558 private:
559
Ian Rogersd5b32602012-02-26 16:40:04 -0800560 static void PrettyObjectValue(std::string& summary, Class* type, Object* value) {
561 CHECK(type != NULL);
562 if (value == NULL) {
563 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
564 } else if (type->IsStringClass()) {
565 String* string = value->AsString();
566 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
567 } else if (value->IsClass()) {
568 Class* klass = value->AsClass();
569 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
570 } else if (value->IsField()) {
571 Field* field = value->AsField();
572 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
573 } else if (value->IsMethod()) {
574 Method* method = value->AsMethod();
575 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
576 } else {
577 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
578 }
579 }
580
581 static void PrintField(std::string& summary, Field* field, Object* obj) {
582 FieldHelper fh(field);
583 Class* type = fh.GetType();
584 StringAppendF(&summary, "\t%s: ", fh.GetName());
585 if (type->IsPrimitiveLong()) {
586 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
587 } else if (type->IsPrimitiveDouble()) {
588 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
589 } else if (type->IsPrimitiveFloat()) {
590 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
591 } else if (type->IsPrimitive()){
592 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
593 } else {
594 Object* value = field->GetObj(obj);
595 PrettyObjectValue(summary, type, value);
596 }
597 }
598
599 static void DumpFields(std::string& summary, Object* obj, Class* klass) {
600 Class* super = klass->GetSuperClass();
601 if (super != NULL) {
602 DumpFields(summary, obj, super);
603 }
604 ObjectArray<Field>* fields = klass->GetIFields();
605 if (fields != NULL) {
606 for (int32_t i = 0; i < fields->GetLength(); i++) {
607 Field* field = fields->Get(i);
608 PrintField(summary, field, obj);
609 }
610 }
611 }
612
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800613 bool InDumpSpace(const Object* object) {
614 return image_space_.Contains(object);
615 }
616
617 const void* GetOatCode(Method* m) {
618 Runtime* runtime = Runtime::Current();
619 const void* code = m->GetCode();
Ian Rogersfb6adba2012-03-04 21:51:51 -0800620 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800621 code = oat_dumper_->GetOatCode(m);
622 }
623 return code;
624 }
625
Brian Carlstrom78128a62011-09-15 17:21:19 -0700626 static void Callback(Object* obj, void* arg) {
627 DCHECK(obj != NULL);
628 DCHECK(arg != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800629 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700630 if (!state->InDumpSpace(obj)) {
631 return;
632 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700633
634 size_t object_bytes = obj->SizeOf();
635 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
636 state->stats_.object_bytes += object_bytes;
637 state->stats_.alignment_bytes += alignment_bytes;
638
Brian Carlstrom78128a62011-09-15 17:21:19 -0700639 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800640 Class* obj_class = obj->GetClass();
641 if (obj_class->IsArrayClass()) {
642 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
643 obj->AsArray()->GetLength());
644 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700645 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800646 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
647 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700648 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800649 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700650 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800651 } else if (obj->IsField()) {
652 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
653 PrettyField(obj->AsField()).c_str());
654 } else if (obj->IsMethod()) {
655 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
656 PrettyMethod(obj->AsMethod()).c_str());
657 } else if (obj_class->IsStringClass()) {
658 StringAppendF(&summary, "%p: java.lang.String \"%s\"\n", obj,
659 obj->AsString()->ToModifiedUtf8().c_str());
660 } else {
661 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
662 }
663 DumpFields(summary, obj, obj_class);
664 if (obj->IsObjectArray()) {
665 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
666 int32_t length = obj_array->GetLength();
667 for (int32_t i = 0; i < length; i++) {
668 Object* value = obj_array->Get(i);
669 size_t run = 0;
670 for (int32_t j = i + 1; j < length; j++) {
671 if (value == obj_array->Get(j)) {
672 run++;
673 } else {
674 break;
675 }
676 }
677 if (run == 0) {
678 StringAppendF(&summary, "\t%d: ", i);
679 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800680 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800681 i = i + run;
682 }
683 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
684 PrettyObjectValue(summary, value_class, value);
685 }
686 } else if (obj->IsClass()) {
687 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
688 if (sfields != NULL) {
689 summary += "\t\tSTATICS:\n";
690 for (int32_t i = 0; i < sfields->GetLength(); i++) {
691 Field* field = sfields->Get(i);
692 PrintField(summary, field, NULL);
693 }
694 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700695 } else if (obj->IsMethod()) {
696 Method* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700697 if (method->IsNative()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700698 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800699 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700700 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800701 bool first_occurrence;
702 size_t invoke_stub_size = state->ComputeOatSize(
703 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
704 if (first_occurrence) {
705 state->stats_.managed_to_native_code_bytes += invoke_stub_size;
706 }
707 const void* oat_code = state->GetOatCode(method);
708 size_t code_size = state->ComputeOatSize(oat_code, &first_occurrence);
709 if (first_occurrence) {
710 state->stats_.native_to_managed_code_bytes += code_size;
711 }
712 if (oat_code != method->GetCode()) {
713 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
714 }
Ian Rogers19846512012-02-24 11:42:47 -0800715 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
716 method->IsResolutionMethod()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800718 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700719 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700720 } else {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800721 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
722 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700723
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800724 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700725 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700726 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800727
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800728 bool first_occurance;
729 size_t gc_map_bytes = state->ComputeOatSize(method->GetGcMapRaw(), &first_occurance);
730 if (first_occurance) {
731 state->stats_.gc_map_bytes += gc_map_bytes;
732 }
733
734 size_t pc_mapping_table_bytes =
735 state->ComputeOatSize(method->GetMappingTableRaw(), &first_occurance);
736 if (first_occurance) {
737 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
738 }
739
740 size_t vmap_table_bytes =
741 state->ComputeOatSize(method->GetVmapTableRaw(), &first_occurance);
742 if (first_occurance) {
743 state->stats_.vmap_table_bytes += vmap_table_bytes;
744 }
745
746 size_t invoke_stub_size = state->ComputeOatSize(
747 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurance);
748 if (first_occurance) {
749 state->stats_.native_to_managed_code_bytes += invoke_stub_size;
750 }
751 const void* oat_code = state->GetOatCode(method);
752 size_t code_size = state->ComputeOatSize(oat_code, &first_occurance);
753 if (first_occurance) {
754 state->stats_.managed_code_bytes += code_size;
755 }
756 state->stats_.managed_code_bytes_ignoring_deduplication += code_size;
757
758 if (oat_code != method->GetCode()) {
759 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
760 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800761 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800762 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
763
764 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
765 vmap_table_bytes + invoke_stub_size + code_size + object_bytes;
766
767 double expansion =
768 static_cast<double>(code_size) / static_cast<double>(dex_instruction_bytes);
769 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700770 }
771 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800772 std::string descriptor(ClassHelper(obj_class).GetDescriptor());
773 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
774 state->stats_.descriptor_to_count[descriptor] += 1;
775
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700776 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700777 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700778
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800779 std::set<const void*> already_seen_;
780 // Compute the size of the given data within the oat file and whether this is the first time
781 // this data has been requested
782 size_t ComputeOatSize(const void* oat_data, bool* first_occurance) {
783 if (already_seen_.count(oat_data) == 0) {
784 *first_occurance = true;
785 already_seen_.insert(oat_data);
786 } else {
787 *first_occurance = false;
788 }
789 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700790 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700791
792 public:
793 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800794 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700795 size_t file_bytes;
796
797 size_t header_bytes;
798 size_t object_bytes;
799 size_t alignment_bytes;
800
801 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800802 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700803 size_t managed_to_native_code_bytes;
804 size_t native_to_managed_code_bytes;
805
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800806 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700807 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800808 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700809
810 size_t dex_instruction_bytes;
811
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800812 std::vector<Method*> method_outlier;
813 std::vector<size_t> method_outlier_size;
814 std::vector<double> method_outlier_expansion;
815
816 explicit Stats()
817 : oat_file_bytes(0),
818 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700819 header_bytes(0),
820 object_bytes(0),
821 alignment_bytes(0),
822 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800823 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700824 managed_to_native_code_bytes(0),
825 native_to_managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800826 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700827 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800828 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700829 dex_instruction_bytes(0) {}
830
Elliott Hughese5448b52012-01-18 16:44:06 -0800831 typedef std::map<std::string, size_t> TableBytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700832 TableBytes descriptor_to_bytes;
833
Elliott Hughese5448b52012-01-18 16:44:06 -0800834 typedef std::map<std::string, size_t> TableCount;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700835 TableCount descriptor_to_count;
836
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800837 double PercentOfOatBytes(size_t size) {
838 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
839 }
840
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700841 double PercentOfFileBytes(size_t size) {
842 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
843 }
844
845 double PercentOfObjectBytes(size_t size) {
846 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
847 }
848
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800849 void ComputeOutliers(size_t total_size, double expansion, Method* method) {
850 method_outlier_size.push_back(total_size);
851 method_outlier_expansion.push_back(expansion);
852 method_outlier.push_back(method);
853 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700854
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800855 void DumpOutliers(std::ostream& os) {
856 size_t sum_of_sizes = 0;
857 size_t sum_of_sizes_squared = 0;
858 size_t sum_of_expansion = 0;
859 size_t sum_of_expansion_squared = 0;
860 size_t n = method_outlier_size.size();
861 for (size_t i = 0; i < n; i++) {
862 size_t cur_size = method_outlier_size[i];
863 sum_of_sizes += cur_size;
864 sum_of_sizes_squared += cur_size * cur_size;
865 double cur_expansion = method_outlier_expansion[i];
866 sum_of_expansion += cur_expansion;
867 sum_of_expansion_squared += cur_expansion * cur_expansion;
868 }
869 size_t size_mean = sum_of_sizes / n;
870 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
871 double expansion_mean = sum_of_expansion / n;
872 double expansion_variance =
873 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
874
875 // Dump methods whose size is a certain number of standard deviations from the mean
876 size_t dumped_values = 0;
877 size_t skipped_values = 0;
878 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
879 size_t cur_size_variance = i * i * size_variance;
880 bool first = true;
881 for (size_t j = 0; j < n; j++) {
882 size_t cur_size = method_outlier_size[j];
883 if (cur_size > size_mean) {
884 size_t cur_var = cur_size - size_mean;
885 cur_var = cur_var * cur_var;
886 if (cur_var > cur_size_variance) {
887 if (dumped_values > 20) {
888 if (i == 1) {
889 skipped_values++;
890 } else {
891 i = 2; // jump to counting for 1 standard deviation
892 break;
893 }
894 } else {
895 if (first) {
896 os << "\nBig methods (size > " << i << " standard deviations the norm):"
897 << std::endl;
898 first = false;
899 }
900 os << "\t" << PrettyMethod(method_outlier[j]) << " requires storage of "
901 << PrettySize(cur_size) << std::endl;
902 method_outlier_size[j] = 0; // don't consider this method again
903 dumped_values++;
904 }
905 }
906 }
907 }
908 }
909 if (skipped_values > 0) {
910 os << "\t... skipped " << skipped_values
911 << " methods with size > 1 standard deviation from the norm" << std::endl;
912 }
913 os << std::endl << std::flush;
914
915 // Dump methods whose expansion is a certain number of standard deviations from the mean
916 dumped_values = 0;
917 skipped_values = 0;
918 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
919 double cur_expansion_variance = i * i * expansion_variance;
920 bool first = true;
921 for (size_t j = 0; j < n; j++) {
922 double cur_expansion = method_outlier_expansion[j];
923 if (cur_expansion > expansion_mean) {
924 size_t cur_var = cur_expansion - expansion_mean;
925 cur_var = cur_var * cur_var;
926 if (cur_var > cur_expansion_variance) {
927 if (dumped_values > 20) {
928 if (i == 1) {
929 skipped_values++;
930 } else {
931 i = 2; // jump to counting for 1 standard deviation
932 break;
933 }
934 } else {
935 if (first) {
936 os << "\nLarge expansion methods (size > " << i
937 << " standard deviations the norm):" << std::endl;
938 first = false;
939 }
940 os << "\t" << PrettyMethod(method_outlier[j]) << " expanded code by "
941 << cur_expansion << std::endl;
942 method_outlier_expansion[j] = 0.0; // don't consider this method again
943 dumped_values++;
944 }
945 }
946 }
947 }
948 }
949 if (skipped_values > 0) {
950 os << "\t... skipped " << skipped_values
951 << " methods with expansion > 1 standard deviation from the norm" << std::endl;
952 }
953 os << std::endl << std::flush;
954 }
955
956 void Dump(std::ostream& os) {
957 os << "\tart_file_bytes = " << PrettySize(file_bytes) << std::endl << std::endl
958 << "\tart_file_bytes = header_bytes + object_bytes + alignment_bytes" << std::endl
959 << StringPrintf("\theader_bytes = %8zd (%2.0f%% of art file bytes)\n"
960 "\tobject_bytes = %8zd (%2.0f%% of art file bytes)\n"
961 "\talignment_bytes = %8zd (%2.0f%% of art file bytes)\n",
962 header_bytes, PercentOfFileBytes(header_bytes),
963 object_bytes, PercentOfFileBytes(object_bytes),
964 alignment_bytes, PercentOfFileBytes(alignment_bytes))
965 << std::endl << std::flush;
966
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700967 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
968
969 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
970 size_t object_bytes_total = 0;
971 typedef TableBytes::const_iterator It; // TODO: C++0x auto
972 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -0800973 const std::string& descriptor(it->first);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700974 size_t bytes = it->second;
975 size_t count = descriptor_to_count[descriptor];
976 double average = static_cast<double>(bytes) / static_cast<double>(count);
977 double percent = PercentOfObjectBytes(bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800978 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700979 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
980 descriptor.c_str(), bytes, count,
981 average, percent);
982
983 object_bytes_total += bytes;
984 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800985 os << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700986 CHECK_EQ(object_bytes, object_bytes_total);
987
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800988 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
989 "\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
990 "\tnative_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n",
991 managed_code_bytes, PercentOfOatBytes(managed_code_bytes),
992 managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes),
993 native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes))
994 << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700995
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800996 os << StringPrintf("\tgc_map_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
997 "\tpc_mapping_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
998 "\tvmap_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n",
999 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1000 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1001 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
1002 << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001003
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001004 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001005 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n",
1006 static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes),
1007 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
1008 static_cast<double>(dex_instruction_bytes));
1009 os << std::endl << std::flush;
1010
1011 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001012 }
1013 } stats_;
1014
1015 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001016 UniquePtr<OatDumper> oat_dumper_;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001017 std::ostream& os_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001018 const std::string image_filename_;
1019 const std::string host_prefix_;
1020 Space& image_space_;
1021 const ImageHeader& image_header_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001022
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001023 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001024};
1025
1026int oatdump(int argc, char** argv) {
1027 // Skip over argv[0].
1028 argv++;
1029 argc--;
1030
1031 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001032 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001033 usage();
1034 }
1035
Brian Carlstromaded5f72011-10-07 17:15:04 -07001036 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001037 const char* image_filename = NULL;
1038 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001039 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001040 std::ostream* os = &std::cout;
1041 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001042
1043 for (int i = 0; i < argc; i++) {
1044 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001045 if (option.starts_with("--oat-file=")) {
1046 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001047 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001048 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001049 } else if (option.starts_with("--boot-image=")) {
1050 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001051 } else if (option.starts_with("--host-prefix=")) {
1052 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001053 } else if (option.starts_with("--output=")) {
1054 const char* filename = option.substr(strlen("--output=")).data();
1055 out.reset(new std::ofstream(filename));
1056 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001057 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001058 usage();
1059 }
1060 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001061 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001062 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001063 usage();
1064 }
1065 }
1066
Brian Carlstromaded5f72011-10-07 17:15:04 -07001067 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001068 fprintf(stderr, "Either --image or --oat must be specified\n");
1069 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001070 }
1071
Brian Carlstromaded5f72011-10-07 17:15:04 -07001072 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001073 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1074 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001075 }
1076
1077 if (oat_filename != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001078 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001079 if (oat_file == NULL) {
1080 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
1081 return EXIT_FAILURE;
1082 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001083 OatDumper oat_dumper(*oat_file);
1084 oat_dumper.Dump(*os);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001085 return EXIT_SUCCESS;
1086 }
1087
Brian Carlstrom78128a62011-09-15 17:21:19 -07001088 Runtime::Options options;
1089 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001090 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001091 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001092 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001093 if (boot_image_filename != NULL) {
1094 boot_image_option += "-Ximage:";
1095 boot_image_option += boot_image_filename;
1096 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001097 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001098 if (image_filename != NULL) {
1099 image_option += "-Ximage:";
1100 image_option += image_filename;
1101 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1102 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001103
Brian Carlstromfe487d02012-02-29 18:49:16 -08001104 if (host_prefix.empty()) {
1105 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
1106 if (android_product_out != NULL) {
1107 host_prefix = android_product_out;
1108 }
1109 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001110 if (!host_prefix.empty()) {
1111 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
1112 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001113
1114 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
1115 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001116 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001117 return EXIT_FAILURE;
1118 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001119
Ian Rogers30fab402012-01-23 15:43:46 -08001120 ImageSpace* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]->AsImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001121 CHECK(image_space != NULL);
1122 const ImageHeader& image_header = image_space->GetImageHeader();
1123 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001124 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001125 return EXIT_FAILURE;
1126 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001127 ImageDumper image_dumper(*os, image_filename, host_prefix, *image_space, image_header);
1128 image_dumper.Dump();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001129 return EXIT_SUCCESS;
1130}
1131
1132} // namespace art
1133
1134int main(int argc, char** argv) {
1135 return art::oatdump(argc, argv);
1136}