blob: cf4dc2d4dfbd298e38e4049015d6d1820ce9dc3a [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 "kInstanceResolutionStubArray",
76 "kStaticResolutionStubArray",
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070077 "kUnknownMethodResolutionStubArray",
Ian Rogers19846512012-02-24 11:42:47 -080078 "kResolutionMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070079 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070080 "kRefsOnlySaveMethod",
81 "kRefsAndArgsSaveMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070082 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070083 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070084 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070085};
86
Elliott Hughese3c845c2012-02-28 17:23:01 -080087class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -070088 public:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080089 explicit OatDumper(const OatFile& oat_file) : oat_file_(oat_file),
90 oat_dex_files_(oat_file.GetOatDexFiles()), disassembler_(Disassembler::Create(kArm)) {
91 // TODO: the disassembler should find the oat file instruction set from the oat header
92 AddAllOffsets();
93 }
94
95 void Dump(std::ostream& os) {
96 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -070097
98 os << "MAGIC:\n";
99 os << oat_header.GetMagic() << "\n\n";
100
101 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800102 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700103
104 os << "DEX FILE COUNT:\n";
105 os << oat_header.GetDexFileCount() << "\n\n";
106
107 os << "EXECUTABLE OFFSET:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800108 os << StringPrintf("0x%08x\n\n", oat_header.GetExecutableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700109
Ian Rogers30fab402012-01-23 15:43:46 -0800110 os << "BEGIN:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800111 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700112
Ian Rogers30fab402012-01-23 15:43:46 -0800113 os << "END:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800114 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700115
116 os << std::flush;
117
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800118 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
119 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Brian Carlstromaded5f72011-10-07 17:15:04 -0700120 CHECK(oat_dex_file != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800121 DumpOatDexFile(os, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700122 }
123 }
124
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800125 size_t ComputeSize(const void* oat_data) {
126 if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() ||
127 reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) {
128 return 0; // Address not in oat file
129 }
130 uint32_t begin_offset = reinterpret_cast<size_t>(oat_data) -
131 reinterpret_cast<size_t>(oat_file_.Begin());
132 typedef std::set<uint32_t>::iterator It;
133 It it = offsets_.upper_bound(begin_offset);
134 CHECK(it != offsets_.end());
135 uint32_t end_offset = *it;
136 return end_offset - begin_offset;
137 }
138
139 const void* GetOatCode(Method* m) {
140 MethodHelper mh(m);
141 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
142 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
143 CHECK(oat_dex_file != NULL);
144 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
145 if (dex_file.get() != NULL) {
146 uint32_t class_def_index;
147 bool found = dex_file->FindClassDefIndex(mh.GetDeclaringClassDescriptor(), class_def_index);
148 if (found) {
149 const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_index);
150 CHECK(oat_class != NULL);
151 size_t method_index = m->GetMethodIndex();
152 return oat_class->GetOatMethod(method_index).GetCode();
153 }
154 }
155 }
156 return NULL;
157 }
158
Brian Carlstromaded5f72011-10-07 17:15:04 -0700159 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800160 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800161 // We don't know the length of the code for each method, but we need to know where to stop
162 // when disassembling. What we do know is that a region of code will be followed by some other
163 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
164 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800165 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
166 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800167 CHECK(oat_dex_file != NULL);
168 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
169 if (dex_file.get() == NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800170 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800171 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800172 offsets_.insert(reinterpret_cast<uint32_t>(&dex_file->GetHeader()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800173 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
174 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
175 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index));
176 const byte* class_data = dex_file->GetClassData(class_def);
177 if (class_data != NULL) {
178 ClassDataItemIterator it(*dex_file, class_data);
179 SkipAllFields(it);
180 uint32_t class_method_index = 0;
181 while (it.HasNextDirectMethod()) {
182 AddOffsets(oat_class->GetOatMethod(class_method_index++));
183 it.Next();
184 }
185 while (it.HasNextVirtualMethod()) {
186 AddOffsets(oat_class->GetOatMethod(class_method_index++));
187 it.Next();
188 }
189 }
190 }
191 }
192
193 // If the last thing in the file is code for a method, there won't be an offset for the "next"
194 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
195 // for the end of the file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800196 offsets_.insert(static_cast<uint32_t>(oat_file_.End() - oat_file_.Begin()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800197 }
198
199 void AddOffsets(const OatFile::OatMethod& oat_method) {
200 offsets_.insert(oat_method.GetCodeOffset());
201 offsets_.insert(oat_method.GetMappingTableOffset());
202 offsets_.insert(oat_method.GetVmapTableOffset());
203 offsets_.insert(oat_method.GetGcMapOffset());
204 offsets_.insert(oat_method.GetInvokeStubOffset());
205 }
206
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800207 void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700208 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800209 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800210 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800211 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile());
212 if (dex_file.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700213 os << "NOT FOUND\n\n";
214 return;
215 }
216 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
217 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
218 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700219 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
220 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800221 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800222 << oat_class->GetStatus() << ")\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800223 DumpOatClass(os, *oat_class.get(), *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700224 }
225
226 os << std::flush;
227 }
228
Elliott Hughese3c845c2012-02-28 17:23:01 -0800229 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700230 while (it.HasNextStaticField()) {
231 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700232 }
Ian Rogers0571d352011-11-03 19:51:38 -0700233 while (it.HasNextInstanceField()) {
234 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700235 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800236 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700237
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800238 void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
239 const DexFile::ClassDef& class_def) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800240 const byte* class_data = dex_file.GetClassData(class_def);
241 if (class_data == NULL) { // empty class such as a marker interface?
242 return;
243 }
244 ClassDataItemIterator it(dex_file, class_data);
245 SkipAllFields(it);
246
247 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700248 while (it.HasNextDirectMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800249 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800250 DumpOatMethod(os, class_method_index, oat_method, dex_file,
251 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800252 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700253 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700254 }
Ian Rogers0571d352011-11-03 19:51:38 -0700255 while (it.HasNextVirtualMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800256 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800257 DumpOatMethod(os, class_method_index, oat_method, dex_file,
258 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800259 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700260 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700261 }
Ian Rogers0571d352011-11-03 19:51:38 -0700262 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700263 os << std::flush;
264 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800265
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800266 void DumpOatMethod(std::ostream& os, uint32_t class_method_index,
Elliott Hughese3c845c2012-02-28 17:23:01 -0800267 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
268 uint32_t dex_method_idx, const DexFile::CodeItem* code_item) {
269 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700270 const char* name = dex_file.GetMethodName(method_id);
Elliott Hughes95572412011-12-13 18:14:20 -0800271 std::string signature(dex_file.GetMethodSignature(method_id));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800272 os << StringPrintf("\t%d: %s %s (dex_method_idx=%d)\n",
273 class_method_index, name, signature.c_str(), dex_method_idx);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800274 os << StringPrintf("\t\tcode: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800275 oat_method.GetCode(), oat_method.GetCodeOffset());
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800276 os << StringPrintf("\t\tframe_size_in_bytes: %zd\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800277 oat_method.GetFrameSizeInBytes());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800278 os << StringPrintf("\t\tcore_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800279 oat_method.GetCoreSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800280 DumpSpillMask(os, oat_method.GetCoreSpillMask(), false);
281 os << StringPrintf("\n\t\tfp_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800282 oat_method.GetFpSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800283 DumpSpillMask(os, oat_method.GetFpSpillMask(), true);
284 os << StringPrintf("\n\t\tmapping_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800285 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800286 DumpMappingTable(os, oat_method);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800287 os << StringPrintf("\t\tvmap_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800288 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800289 DumpVmap(os, oat_method.GetVmapTable(), oat_method.GetCoreSpillMask(),
290 oat_method.GetFpSpillMask());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800291 os << StringPrintf("\t\tgc_map: %p (offset=0x%08x)\n",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800292 oat_method.GetGcMap(), oat_method.GetGcMapOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800293 DumpGcMap(os, oat_method.GetGcMap());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800294 os << StringPrintf("\t\tinvoke_stub: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800295 oat_method.GetInvokeStub(), oat_method.GetInvokeStubOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800296 os << "\t\tCODE:\n";
297 DumpCode(os, oat_method.GetCode(), oat_method.GetMappingTable(), dex_file, code_item);
298 os << "\t\tINVOKE STUB:\n";
299 DumpCode(os, reinterpret_cast<const void*>(oat_method.GetInvokeStub()), NULL, dex_file, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700300 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800301
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800302 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
303 if (spill_mask == 0) {
304 return;
305 }
306 os << " (";
307 for (size_t i = 0; i < 32; i++) {
308 if ((spill_mask & (1 << i)) != 0) {
309 if (is_float) {
310 os << "fr" << i;
311 } else {
312 os << "r" << i;
313 }
314 spill_mask ^= 1 << i; // clear bit
315 if (spill_mask != 0) {
316 os << ", ";
317 } else {
318 break;
319 }
320 }
321 }
322 os << ")";
323 }
324
325 void DumpVmap(std::ostream& os, const uint16_t* raw_table, uint32_t core_spill_mask,
326 uint32_t fp_spill_mask) {
327 if (raw_table == NULL) {
328 return;
329 }
330 const VmapTable vmap_table(raw_table);
331 bool first = true;
332 os << "\t\t\t";
333 for (size_t i = 0; i < vmap_table.size(); i++) {
334 uint16_t dex_reg = vmap_table[i];
335 size_t matches = 0;
336 size_t spill_shifts = 0;
337 uint32_t spill_mask = core_spill_mask;
338 bool processing_fp = false;
339 while (matches != (i + 1)) {
340 if (spill_mask == 0) {
341 CHECK(!processing_fp);
342 spill_mask = fp_spill_mask;
343 processing_fp = true;
344 }
345 matches += spill_mask & 1; // Add 1 if the low bit is set
346 spill_mask >>= 1;
347 spill_shifts++;
348 }
349 size_t arm_reg = spill_shifts - 1; // wind back one as we want the last match
350 os << (first ? "v" : ", v") << dex_reg;
351 if (arm_reg < 16) {
352 os << "/r" << arm_reg;
353 } else {
354 os << "/fr" << (arm_reg - 16);
355 }
356 if (first) {
357 first = false;
358 }
359 }
360 os << std::endl;
361 }
362
363 void DumpGcMap(std::ostream& os, const uint8_t* gc_map_raw) {
364 if (gc_map_raw == NULL) {
365 return;
366 }
367 uint32_t gc_map_length = (gc_map_raw[0] << 24) | (gc_map_raw[1] << 16) |
368 (gc_map_raw[2] << 8) | (gc_map_raw[3] << 0);
369 verifier::PcToReferenceMap map(gc_map_raw + sizeof(uint32_t), gc_map_length);
370 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
371 os << StringPrintf("\t\t\t0x%04x", map.GetPC(entry));
372 size_t num_regs = map.RegWidth() * 8;
373 const uint8_t* reg_bitmap = map.GetBitMap(entry);
374 bool first = true;
375 for (size_t reg = 0; reg < num_regs; reg++) {
376 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
377 if (first) {
378 os << " v" << reg;
379 first = false;
380 } else {
381 os << ", v" << reg;
382 }
383 }
384 }
385 os << std::endl;
386 }
387 }
388
389 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800390 const uint32_t* raw_table = oat_method.GetMappingTable();
391 const void* code = oat_method.GetCode();
392 if (raw_table == NULL || code == NULL) {
393 return;
394 }
395
396 uint32_t length = *raw_table;
397 ++raw_table;
398
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800399 os << "\t\t{";
Elliott Hughese3c845c2012-02-28 17:23:01 -0800400 for (size_t i = 0; i < length; i += 2) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800401 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800402 uint32_t dex_pc = raw_table[i + 1];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800403 os << StringPrintf("%p -> 0x%04x", native_pc, dex_pc);
404 if (i + 2 < length) {
405 os << ", ";
406 }
407 }
408 os << "}" << std::endl << std::flush;
409 }
410
411 void DumpCode(std::ostream& os, const void* code, const uint32_t* raw_mapping_table,
412 const DexFile& dex_file, const DexFile::CodeItem* code_item) {
413 if (code == NULL) {
414 return;
415 }
416
417 if (raw_mapping_table == NULL) {
418 // code but no mapping table is most likely caused by code created by the JNI compiler
419 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code);
420 const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file_.Begin());
421 uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin);
422
423 typedef std::set<uint32_t>::iterator It;
424 It it = offsets_.upper_bound(last_offset);
425 CHECK(it != offsets_.end());
426 const uint8_t* end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it;
427 CHECK(native_pc < end_native_pc);
428
429 disassembler_->Dump(os, native_pc, end_native_pc);
430 return;
431 }
432
433 uint32_t length = *raw_mapping_table;
434 ++raw_mapping_table;
435
436 for (size_t i = 0; i < length; i += 2) {
437 uint32_t dex_pc = raw_mapping_table[i + 1];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800438 const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]);
439 os << StringPrintf("\t\t0x%04x: %s\n", dex_pc, instruction->DumpString(&dex_file).c_str());
440
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800441 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800442 const uint8_t* end_native_pc = NULL;
443 if (i + 2 < length) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800444 end_native_pc = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i + 2];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800445 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800446 const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file_.Begin());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800447 uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin);
448
449 typedef std::set<uint32_t>::iterator It;
Elliott Hughesed2adb62012-02-29 14:41:01 -0800450 It it = offsets_.upper_bound(last_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800451 CHECK(it != offsets_.end());
452 end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it;
453 }
Elliott Hughesed2adb62012-02-29 14:41:01 -0800454 CHECK(native_pc < end_native_pc);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800455 disassembler_->Dump(os, native_pc, end_native_pc);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800456 }
457 }
458
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800459 const OatFile& oat_file_;
460 std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800461 std::set<uint32_t> offsets_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800462 UniquePtr<Disassembler> disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700463};
464
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800465class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700466 public:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800467 explicit ImageDumper(std::ostream& os, const std::string& image_filename,
468 const std::string& host_prefix, Space& image_space,
469 const ImageHeader& image_header) : os_(os),
470 image_filename_(image_filename), host_prefix_(host_prefix),
471 image_space_(image_space), image_header_(image_header) {
472 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700473
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800474 void Dump() {
475 os_ << "MAGIC:\n";
476 os_ << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700477
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800478 os_ << "IMAGE BEGIN:\n";
479 os_ << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700480
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800481 os_ << "OAT CHECKSUM:\n";
482 os_ << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700483
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800484 os_ << "OAT BEGIN:\n";
485 os_ << reinterpret_cast<void*>(image_header_.GetOatBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700486
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800487 os_ << "OAT END:\n";
488 os_ << reinterpret_cast<void*>(image_header_.GetOatEnd()) << "\n\n";
489
490 os_ << "ROOTS:\n";
491 os_ << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700492 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700493 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
494 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700495 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800496 Object* image_root_object = image_header_.GetImageRoot(image_root);
497 os_ << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700498 if (image_root_object->IsObjectArray()) {
499 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
500 ObjectArray<Object>* image_root_object_array
501 = down_cast<ObjectArray<Object>*>(image_root_object);
502 // = image_root_object->AsObjectArray<Object>();
503 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800504 Object* value = image_root_object_array->Get(i);
505 if (value != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800506 os_ << "\t" << i << ": ";
Ian Rogersd5b32602012-02-26 16:40:04 -0800507 std::string summary;
508 PrettyObjectValue(summary, value->GetClass(), value);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800509 os_ << summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800510 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800511 os_ << StringPrintf("\t%d: null\n", i);
Ian Rogersd5b32602012-02-26 16:40:04 -0800512 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700513 }
514 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700515 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800516 os_ << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700517
Brian Carlstromaded5f72011-10-07 17:15:04 -0700518 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800519 Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation);
520 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
521 if (!host_prefix_.empty()) {
522 oat_location = host_prefix_ + oat_location;
523 os_ << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700524 }
Brian Carlstromae826982011-11-09 01:33:42 -0800525 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700526 if (oat_file == NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800527 os_ << "OAT FILE NOT FOUND: " << oat_location << std::endl << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700528 return;
529 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700530
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800531 stats_.oat_file_bytes = oat_file->Size();
532
533 oat_dumper_.reset(new OatDumper(*oat_file));
534
535 os_ << "OBJECTS:\n" << std::flush;
536 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
537 DCHECK(heap_bitmap != NULL);
538 heap_bitmap->Walk(ImageDumper::Callback, this);
539 os_ << "\n";
540
541 os_ << "STATS:\n" << std::flush;
542 UniquePtr<File> file(OS::OpenFile(image_filename_.c_str(), false));
543 stats_.file_bytes = file->Length();
544 size_t header_bytes = sizeof(ImageHeader);
545 stats_.header_bytes = header_bytes;
546 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
547 stats_.alignment_bytes += alignment_bytes;
548 stats_.Dump(os_);
549 os_ << "\n";
550
551 os_ << std::flush;
552
553 os_ << "OAT LOCATION:\n" << std::flush;
554 os_ << oat_location;
555 os_ << "\n";
556 oat_dumper_->Dump(os_);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700557 }
558
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700559 private:
560
Ian Rogersd5b32602012-02-26 16:40:04 -0800561 static void PrettyObjectValue(std::string& summary, Class* type, Object* value) {
562 CHECK(type != NULL);
563 if (value == NULL) {
564 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
565 } else if (type->IsStringClass()) {
566 String* string = value->AsString();
567 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
568 } else if (value->IsClass()) {
569 Class* klass = value->AsClass();
570 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
571 } else if (value->IsField()) {
572 Field* field = value->AsField();
573 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
574 } else if (value->IsMethod()) {
575 Method* method = value->AsMethod();
576 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
577 } else {
578 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
579 }
580 }
581
582 static void PrintField(std::string& summary, Field* field, Object* obj) {
583 FieldHelper fh(field);
584 Class* type = fh.GetType();
585 StringAppendF(&summary, "\t%s: ", fh.GetName());
586 if (type->IsPrimitiveLong()) {
587 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
588 } else if (type->IsPrimitiveDouble()) {
589 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
590 } else if (type->IsPrimitiveFloat()) {
591 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
592 } else if (type->IsPrimitive()){
593 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
594 } else {
595 Object* value = field->GetObj(obj);
596 PrettyObjectValue(summary, type, value);
597 }
598 }
599
600 static void DumpFields(std::string& summary, Object* obj, Class* klass) {
601 Class* super = klass->GetSuperClass();
602 if (super != NULL) {
603 DumpFields(summary, obj, super);
604 }
605 ObjectArray<Field>* fields = klass->GetIFields();
606 if (fields != NULL) {
607 for (int32_t i = 0; i < fields->GetLength(); i++) {
608 Field* field = fields->Get(i);
609 PrintField(summary, field, obj);
610 }
611 }
612 }
613
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800614 bool InDumpSpace(const Object* object) {
615 return image_space_.Contains(object);
616 }
617
618 const void* GetOatCode(Method* m) {
619 Runtime* runtime = Runtime::Current();
620 const void* code = m->GetCode();
621 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData() ||
622 code == runtime->GetResolutionStubArray(Runtime::kInstanceMethod)->GetData()) {
623 code = oat_dumper_->GetOatCode(m);
624 }
625 return code;
626 }
627
Brian Carlstrom78128a62011-09-15 17:21:19 -0700628 static void Callback(Object* obj, void* arg) {
629 DCHECK(obj != NULL);
630 DCHECK(arg != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800631 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700632 if (!state->InDumpSpace(obj)) {
633 return;
634 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700635
636 size_t object_bytes = obj->SizeOf();
637 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
638 state->stats_.object_bytes += object_bytes;
639 state->stats_.alignment_bytes += alignment_bytes;
640
Brian Carlstrom78128a62011-09-15 17:21:19 -0700641 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800642 Class* obj_class = obj->GetClass();
643 if (obj_class->IsArrayClass()) {
644 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
645 obj->AsArray()->GetLength());
646 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700647 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800648 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
649 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700650 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800651 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700652 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800653 } else if (obj->IsField()) {
654 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
655 PrettyField(obj->AsField()).c_str());
656 } else if (obj->IsMethod()) {
657 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
658 PrettyMethod(obj->AsMethod()).c_str());
659 } else if (obj_class->IsStringClass()) {
660 StringAppendF(&summary, "%p: java.lang.String \"%s\"\n", obj,
661 obj->AsString()->ToModifiedUtf8().c_str());
662 } else {
663 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
664 }
665 DumpFields(summary, obj, obj_class);
666 if (obj->IsObjectArray()) {
667 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
668 int32_t length = obj_array->GetLength();
669 for (int32_t i = 0; i < length; i++) {
670 Object* value = obj_array->Get(i);
671 size_t run = 0;
672 for (int32_t j = i + 1; j < length; j++) {
673 if (value == obj_array->Get(j)) {
674 run++;
675 } else {
676 break;
677 }
678 }
679 if (run == 0) {
680 StringAppendF(&summary, "\t%d: ", i);
681 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800682 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800683 i = i + run;
684 }
685 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
686 PrettyObjectValue(summary, value_class, value);
687 }
688 } else if (obj->IsClass()) {
689 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
690 if (sfields != NULL) {
691 summary += "\t\tSTATICS:\n";
692 for (int32_t i = 0; i < sfields->GetLength(); i++) {
693 Field* field = sfields->Get(i);
694 PrintField(summary, field, NULL);
695 }
696 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700697 } else if (obj->IsMethod()) {
698 Method* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700699 if (method->IsNative()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700700 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800701 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700702 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800703 bool first_occurrence;
704 size_t invoke_stub_size = state->ComputeOatSize(
705 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
706 if (first_occurrence) {
707 state->stats_.managed_to_native_code_bytes += invoke_stub_size;
708 }
709 const void* oat_code = state->GetOatCode(method);
710 size_t code_size = state->ComputeOatSize(oat_code, &first_occurrence);
711 if (first_occurrence) {
712 state->stats_.native_to_managed_code_bytes += code_size;
713 }
714 if (oat_code != method->GetCode()) {
715 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
716 }
Ian Rogers19846512012-02-24 11:42:47 -0800717 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
718 method->IsResolutionMethod()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700719 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800720 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700721 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700722 } else {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800723 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
724 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700725
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800726 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700727 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700728 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800729
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800730 bool first_occurance;
731 size_t gc_map_bytes = state->ComputeOatSize(method->GetGcMapRaw(), &first_occurance);
732 if (first_occurance) {
733 state->stats_.gc_map_bytes += gc_map_bytes;
734 }
735
736 size_t pc_mapping_table_bytes =
737 state->ComputeOatSize(method->GetMappingTableRaw(), &first_occurance);
738 if (first_occurance) {
739 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
740 }
741
742 size_t vmap_table_bytes =
743 state->ComputeOatSize(method->GetVmapTableRaw(), &first_occurance);
744 if (first_occurance) {
745 state->stats_.vmap_table_bytes += vmap_table_bytes;
746 }
747
748 size_t invoke_stub_size = state->ComputeOatSize(
749 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurance);
750 if (first_occurance) {
751 state->stats_.native_to_managed_code_bytes += invoke_stub_size;
752 }
753 const void* oat_code = state->GetOatCode(method);
754 size_t code_size = state->ComputeOatSize(oat_code, &first_occurance);
755 if (first_occurance) {
756 state->stats_.managed_code_bytes += code_size;
757 }
758 state->stats_.managed_code_bytes_ignoring_deduplication += code_size;
759
760 if (oat_code != method->GetCode()) {
761 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
762 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800763 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800764 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
765
766 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
767 vmap_table_bytes + invoke_stub_size + code_size + object_bytes;
768
769 double expansion =
770 static_cast<double>(code_size) / static_cast<double>(dex_instruction_bytes);
771 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700772 }
773 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800774 std::string descriptor(ClassHelper(obj_class).GetDescriptor());
775 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
776 state->stats_.descriptor_to_count[descriptor] += 1;
777
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700778 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700779 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700780
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800781 std::set<const void*> already_seen_;
782 // Compute the size of the given data within the oat file and whether this is the first time
783 // this data has been requested
784 size_t ComputeOatSize(const void* oat_data, bool* first_occurance) {
785 if (already_seen_.count(oat_data) == 0) {
786 *first_occurance = true;
787 already_seen_.insert(oat_data);
788 } else {
789 *first_occurance = false;
790 }
791 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700792 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700793
794 public:
795 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800796 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700797 size_t file_bytes;
798
799 size_t header_bytes;
800 size_t object_bytes;
801 size_t alignment_bytes;
802
803 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800804 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700805 size_t managed_to_native_code_bytes;
806 size_t native_to_managed_code_bytes;
807
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800808 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700809 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800810 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700811
812 size_t dex_instruction_bytes;
813
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800814 std::vector<Method*> method_outlier;
815 std::vector<size_t> method_outlier_size;
816 std::vector<double> method_outlier_expansion;
817
818 explicit Stats()
819 : oat_file_bytes(0),
820 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700821 header_bytes(0),
822 object_bytes(0),
823 alignment_bytes(0),
824 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800825 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700826 managed_to_native_code_bytes(0),
827 native_to_managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800828 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700829 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800830 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700831 dex_instruction_bytes(0) {}
832
Elliott Hughese5448b52012-01-18 16:44:06 -0800833 typedef std::map<std::string, size_t> TableBytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700834 TableBytes descriptor_to_bytes;
835
Elliott Hughese5448b52012-01-18 16:44:06 -0800836 typedef std::map<std::string, size_t> TableCount;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700837 TableCount descriptor_to_count;
838
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800839 double PercentOfOatBytes(size_t size) {
840 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
841 }
842
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700843 double PercentOfFileBytes(size_t size) {
844 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
845 }
846
847 double PercentOfObjectBytes(size_t size) {
848 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
849 }
850
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800851 void ComputeOutliers(size_t total_size, double expansion, Method* method) {
852 method_outlier_size.push_back(total_size);
853 method_outlier_expansion.push_back(expansion);
854 method_outlier.push_back(method);
855 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700856
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800857 void DumpOutliers(std::ostream& os) {
858 size_t sum_of_sizes = 0;
859 size_t sum_of_sizes_squared = 0;
860 size_t sum_of_expansion = 0;
861 size_t sum_of_expansion_squared = 0;
862 size_t n = method_outlier_size.size();
863 for (size_t i = 0; i < n; i++) {
864 size_t cur_size = method_outlier_size[i];
865 sum_of_sizes += cur_size;
866 sum_of_sizes_squared += cur_size * cur_size;
867 double cur_expansion = method_outlier_expansion[i];
868 sum_of_expansion += cur_expansion;
869 sum_of_expansion_squared += cur_expansion * cur_expansion;
870 }
871 size_t size_mean = sum_of_sizes / n;
872 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
873 double expansion_mean = sum_of_expansion / n;
874 double expansion_variance =
875 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
876
877 // Dump methods whose size is a certain number of standard deviations from the mean
878 size_t dumped_values = 0;
879 size_t skipped_values = 0;
880 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
881 size_t cur_size_variance = i * i * size_variance;
882 bool first = true;
883 for (size_t j = 0; j < n; j++) {
884 size_t cur_size = method_outlier_size[j];
885 if (cur_size > size_mean) {
886 size_t cur_var = cur_size - size_mean;
887 cur_var = cur_var * cur_var;
888 if (cur_var > cur_size_variance) {
889 if (dumped_values > 20) {
890 if (i == 1) {
891 skipped_values++;
892 } else {
893 i = 2; // jump to counting for 1 standard deviation
894 break;
895 }
896 } else {
897 if (first) {
898 os << "\nBig methods (size > " << i << " standard deviations the norm):"
899 << std::endl;
900 first = false;
901 }
902 os << "\t" << PrettyMethod(method_outlier[j]) << " requires storage of "
903 << PrettySize(cur_size) << std::endl;
904 method_outlier_size[j] = 0; // don't consider this method again
905 dumped_values++;
906 }
907 }
908 }
909 }
910 }
911 if (skipped_values > 0) {
912 os << "\t... skipped " << skipped_values
913 << " methods with size > 1 standard deviation from the norm" << std::endl;
914 }
915 os << std::endl << std::flush;
916
917 // Dump methods whose expansion is a certain number of standard deviations from the mean
918 dumped_values = 0;
919 skipped_values = 0;
920 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
921 double cur_expansion_variance = i * i * expansion_variance;
922 bool first = true;
923 for (size_t j = 0; j < n; j++) {
924 double cur_expansion = method_outlier_expansion[j];
925 if (cur_expansion > expansion_mean) {
926 size_t cur_var = cur_expansion - expansion_mean;
927 cur_var = cur_var * cur_var;
928 if (cur_var > cur_expansion_variance) {
929 if (dumped_values > 20) {
930 if (i == 1) {
931 skipped_values++;
932 } else {
933 i = 2; // jump to counting for 1 standard deviation
934 break;
935 }
936 } else {
937 if (first) {
938 os << "\nLarge expansion methods (size > " << i
939 << " standard deviations the norm):" << std::endl;
940 first = false;
941 }
942 os << "\t" << PrettyMethod(method_outlier[j]) << " expanded code by "
943 << cur_expansion << std::endl;
944 method_outlier_expansion[j] = 0.0; // don't consider this method again
945 dumped_values++;
946 }
947 }
948 }
949 }
950 }
951 if (skipped_values > 0) {
952 os << "\t... skipped " << skipped_values
953 << " methods with expansion > 1 standard deviation from the norm" << std::endl;
954 }
955 os << std::endl << std::flush;
956 }
957
958 void Dump(std::ostream& os) {
959 os << "\tart_file_bytes = " << PrettySize(file_bytes) << std::endl << std::endl
960 << "\tart_file_bytes = header_bytes + object_bytes + alignment_bytes" << std::endl
961 << StringPrintf("\theader_bytes = %8zd (%2.0f%% of art file bytes)\n"
962 "\tobject_bytes = %8zd (%2.0f%% of art file bytes)\n"
963 "\talignment_bytes = %8zd (%2.0f%% of art file bytes)\n",
964 header_bytes, PercentOfFileBytes(header_bytes),
965 object_bytes, PercentOfFileBytes(object_bytes),
966 alignment_bytes, PercentOfFileBytes(alignment_bytes))
967 << std::endl << std::flush;
968
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700969 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
970
971 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
972 size_t object_bytes_total = 0;
973 typedef TableBytes::const_iterator It; // TODO: C++0x auto
974 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -0800975 const std::string& descriptor(it->first);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700976 size_t bytes = it->second;
977 size_t count = descriptor_to_count[descriptor];
978 double average = static_cast<double>(bytes) / static_cast<double>(count);
979 double percent = PercentOfObjectBytes(bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800980 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700981 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
982 descriptor.c_str(), bytes, count,
983 average, percent);
984
985 object_bytes_total += bytes;
986 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800987 os << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700988 CHECK_EQ(object_bytes, object_bytes_total);
989
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800990 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
991 "\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
992 "\tnative_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n",
993 managed_code_bytes, PercentOfOatBytes(managed_code_bytes),
994 managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes),
995 native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes))
996 << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700997
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800998 os << StringPrintf("\tgc_map_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
999 "\tpc_mapping_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1000 "\tvmap_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n",
1001 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1002 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1003 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
1004 << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001005
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001006 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001007 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n",
1008 static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes),
1009 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
1010 static_cast<double>(dex_instruction_bytes));
1011 os << std::endl << std::flush;
1012
1013 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001014 }
1015 } stats_;
1016
1017 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001018 UniquePtr<OatDumper> oat_dumper_;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001019 std::ostream& os_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001020 const std::string image_filename_;
1021 const std::string host_prefix_;
1022 Space& image_space_;
1023 const ImageHeader& image_header_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001024
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001025 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001026};
1027
1028int oatdump(int argc, char** argv) {
1029 // Skip over argv[0].
1030 argv++;
1031 argc--;
1032
1033 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001034 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001035 usage();
1036 }
1037
Brian Carlstromaded5f72011-10-07 17:15:04 -07001038 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001039 const char* image_filename = NULL;
1040 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001041 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001042 std::ostream* os = &std::cout;
1043 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001044
1045 for (int i = 0; i < argc; i++) {
1046 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001047 if (option.starts_with("--oat-file=")) {
1048 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001049 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001050 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001051 } else if (option.starts_with("--boot-image=")) {
1052 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001053 } else if (option.starts_with("--host-prefix=")) {
1054 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001055 } else if (option.starts_with("--output=")) {
1056 const char* filename = option.substr(strlen("--output=")).data();
1057 out.reset(new std::ofstream(filename));
1058 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001059 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001060 usage();
1061 }
1062 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001063 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001064 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001065 usage();
1066 }
1067 }
1068
Brian Carlstromaded5f72011-10-07 17:15:04 -07001069 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001070 fprintf(stderr, "Either --image or --oat must be specified\n");
1071 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001072 }
1073
Brian Carlstromaded5f72011-10-07 17:15:04 -07001074 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001075 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1076 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001077 }
1078
1079 if (oat_filename != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001080 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001081 if (oat_file == NULL) {
1082 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
1083 return EXIT_FAILURE;
1084 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001085 OatDumper oat_dumper(*oat_file);
1086 oat_dumper.Dump(*os);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001087 return EXIT_SUCCESS;
1088 }
1089
Brian Carlstrom78128a62011-09-15 17:21:19 -07001090 Runtime::Options options;
1091 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001092 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001093 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001094 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001095 if (boot_image_filename != NULL) {
1096 boot_image_option += "-Ximage:";
1097 boot_image_option += boot_image_filename;
1098 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001099 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001100 if (image_filename != NULL) {
1101 image_option += "-Ximage:";
1102 image_option += image_filename;
1103 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1104 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001105
Brian Carlstromfe487d02012-02-29 18:49:16 -08001106 if (host_prefix.empty()) {
1107 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
1108 if (android_product_out != NULL) {
1109 host_prefix = android_product_out;
1110 }
1111 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001112 if (!host_prefix.empty()) {
1113 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
1114 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001115
1116 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
1117 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001118 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001119 return EXIT_FAILURE;
1120 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001121
Ian Rogers30fab402012-01-23 15:43:46 -08001122 ImageSpace* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]->AsImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001123 CHECK(image_space != NULL);
1124 const ImageHeader& image_header = image_space->GetImageHeader();
1125 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001126 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001127 return EXIT_FAILURE;
1128 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001129 ImageDumper image_dumper(*os, image_filename, host_prefix, *image_space, image_header);
1130 image_dumper.Dump();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001131 return EXIT_SUCCESS;
1132}
1133
1134} // namespace art
1135
1136int main(int argc, char** argv) {
1137 return art::oatdump(argc, argv);
1138}