blob: 295c74f746bda7730f4f4a05146d08334c2f2292 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Elliott Hughese5448b52012-01-18 16:44:06 -080022#include <map>
Brian Carlstrom78128a62011-09-15 17:21:19 -070023#include <string>
24#include <vector>
25
26#include "class_linker.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080027#include "context.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080028#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080029#include "dex_verifier.h"
30#include "disassembler.h"
Brian Carlstrom916e74e2011-09-23 11:42:01 -070031#include "file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070032#include "image.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080033#include "object_utils.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080034#include "os.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070035#include "runtime.h"
36#include "space.h"
37#include "stringpiece.h"
38
39namespace art {
40
41static void usage() {
42 fprintf(stderr,
43 "Usage: oatdump [options] ...\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080044 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n"
45 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070046 "\n");
47 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080048 " --oat-file=<file.oat>: specifies an input oat filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080049 " Example: --image=/system/framework/boot.oat\n"
Brian Carlstromaded5f72011-10-07 17:15:04 -070050 "\n");
51 fprintf(stderr,
52 " --image=<file.art>: specifies an input image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080053 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070054 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070055 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070056 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080057 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070058 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070059 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070060 " --host-prefix may be used to translate host paths to target paths during\n"
61 " cross compilation.\n"
62 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstromfe487d02012-02-29 18:49:16 -080063 " Default: $ANDROID_PRODUCT_OUT\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070064 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070065 fprintf(stderr,
66 " --output=<file> may be used to send the output to a file.\n"
67 " Example: --output=/tmp/oatdump.txt\n"
68 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070069 exit(EXIT_FAILURE);
70}
71
Ian Rogersff1ed472011-09-20 13:46:24 -070072const char* image_roots_descriptions_[] = {
Brian Carlstrom78128a62011-09-15 17:21:19 -070073 "kJniStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070074 "kAbstractMethodErrorStubArray",
Ian Rogersad25ac52011-10-04 19:13:33 -070075 "kStaticResolutionStubArray",
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070076 "kUnknownMethodResolutionStubArray",
Ian Rogers19846512012-02-24 11:42:47 -080077 "kResolutionMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070078 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070079 "kRefsOnlySaveMethod",
80 "kRefsAndArgsSaveMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070081 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070082 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070083 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070084};
85
Elliott Hughese3c845c2012-02-28 17:23:01 -080086class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -070087 public:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080088 explicit OatDumper(const OatFile& oat_file) : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -080089 oat_dex_files_(oat_file.GetOatDexFiles()),
90 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet())) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080091 AddAllOffsets();
92 }
93
94 void Dump(std::ostream& os) {
95 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -070096
97 os << "MAGIC:\n";
98 os << oat_header.GetMagic() << "\n\n";
99
100 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800101 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700102
Elliott Hughesa72ec822012-03-05 17:12:22 -0800103 os << "INSTRUCTION SET:\n";
104 os << oat_header.GetInstructionSet() << "\n\n";
105
Brian Carlstromaded5f72011-10-07 17:15:04 -0700106 os << "DEX FILE COUNT:\n";
107 os << oat_header.GetDexFileCount() << "\n\n";
108
109 os << "EXECUTABLE OFFSET:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800110 os << StringPrintf("0x%08x\n\n", oat_header.GetExecutableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700111
Ian Rogers30fab402012-01-23 15:43:46 -0800112 os << "BEGIN:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800113 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700114
Ian Rogers30fab402012-01-23 15:43:46 -0800115 os << "END:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800116 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700117
118 os << std::flush;
119
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800120 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
121 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Brian Carlstromaded5f72011-10-07 17:15:04 -0700122 CHECK(oat_dex_file != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800123 DumpOatDexFile(os, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700124 }
125 }
126
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800127 size_t ComputeSize(const void* oat_data) {
128 if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() ||
129 reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) {
130 return 0; // Address not in oat file
131 }
132 uint32_t begin_offset = reinterpret_cast<size_t>(oat_data) -
133 reinterpret_cast<size_t>(oat_file_.Begin());
134 typedef std::set<uint32_t>::iterator It;
135 It it = offsets_.upper_bound(begin_offset);
136 CHECK(it != offsets_.end());
137 uint32_t end_offset = *it;
138 return end_offset - begin_offset;
139 }
140
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700141 InstructionSet GetInstructionSet() {
142 return oat_file_.GetOatHeader().GetInstructionSet();
143 }
144
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800145 const void* GetOatCode(Method* m) {
146 MethodHelper mh(m);
147 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
148 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
149 CHECK(oat_dex_file != NULL);
150 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
151 if (dex_file.get() != NULL) {
152 uint32_t class_def_index;
153 bool found = dex_file->FindClassDefIndex(mh.GetDeclaringClassDescriptor(), class_def_index);
154 if (found) {
155 const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_index);
156 CHECK(oat_class != NULL);
157 size_t method_index = m->GetMethodIndex();
158 return oat_class->GetOatMethod(method_index).GetCode();
159 }
160 }
161 }
162 return NULL;
163 }
164
Brian Carlstromaded5f72011-10-07 17:15:04 -0700165 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800166 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800167 // We don't know the length of the code for each method, but we need to know where to stop
168 // when disassembling. What we do know is that a region of code will be followed by some other
169 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
170 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800171 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
172 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800173 CHECK(oat_dex_file != NULL);
174 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
175 if (dex_file.get() == NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800176 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800177 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800178 offsets_.insert(reinterpret_cast<uint32_t>(&dex_file->GetHeader()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800179 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
180 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
181 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index));
182 const byte* class_data = dex_file->GetClassData(class_def);
183 if (class_data != NULL) {
184 ClassDataItemIterator it(*dex_file, class_data);
185 SkipAllFields(it);
186 uint32_t class_method_index = 0;
187 while (it.HasNextDirectMethod()) {
188 AddOffsets(oat_class->GetOatMethod(class_method_index++));
189 it.Next();
190 }
191 while (it.HasNextVirtualMethod()) {
192 AddOffsets(oat_class->GetOatMethod(class_method_index++));
193 it.Next();
194 }
195 }
196 }
197 }
198
199 // If the last thing in the file is code for a method, there won't be an offset for the "next"
200 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
201 // for the end of the file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800202 offsets_.insert(static_cast<uint32_t>(oat_file_.End() - oat_file_.Begin()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800203 }
204
205 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800206 uint32_t code_offset = oat_method.GetCodeOffset();
207 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
208 code_offset &= ~0x1;
209 }
210 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800211 offsets_.insert(oat_method.GetMappingTableOffset());
212 offsets_.insert(oat_method.GetVmapTableOffset());
213 offsets_.insert(oat_method.GetGcMapOffset());
214 offsets_.insert(oat_method.GetInvokeStubOffset());
215 }
216
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800217 void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700218 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800219 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800220 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800221 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile());
222 if (dex_file.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700223 os << "NOT FOUND\n\n";
224 return;
225 }
226 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
227 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
228 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700229 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
230 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800231 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800232 << oat_class->GetStatus() << ")\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800233 DumpOatClass(os, *oat_class.get(), *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700234 }
235
236 os << std::flush;
237 }
238
Elliott Hughese3c845c2012-02-28 17:23:01 -0800239 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700240 while (it.HasNextStaticField()) {
241 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700242 }
Ian Rogers0571d352011-11-03 19:51:38 -0700243 while (it.HasNextInstanceField()) {
244 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700245 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800246 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700247
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800248 void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
249 const DexFile::ClassDef& class_def) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800250 const byte* class_data = dex_file.GetClassData(class_def);
251 if (class_data == NULL) { // empty class such as a marker interface?
252 return;
253 }
254 ClassDataItemIterator it(dex_file, class_data);
255 SkipAllFields(it);
256
257 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700258 while (it.HasNextDirectMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800259 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800260 DumpOatMethod(os, class_method_index, oat_method, dex_file,
261 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800262 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700263 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700264 }
Ian Rogers0571d352011-11-03 19:51:38 -0700265 while (it.HasNextVirtualMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800266 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800267 DumpOatMethod(os, class_method_index, oat_method, dex_file,
268 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800269 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700270 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700271 }
Ian Rogers0571d352011-11-03 19:51:38 -0700272 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700273 os << std::flush;
274 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800275
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800276 void DumpOatMethod(std::ostream& os, uint32_t class_method_index,
Elliott Hughese3c845c2012-02-28 17:23:01 -0800277 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
278 uint32_t dex_method_idx, const DexFile::CodeItem* code_item) {
279 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700280 const char* name = dex_file.GetMethodName(method_id);
Elliott Hughes95572412011-12-13 18:14:20 -0800281 std::string signature(dex_file.GetMethodSignature(method_id));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800282 os << StringPrintf("\t%d: %s %s (dex_method_idx=%d)\n",
283 class_method_index, name, signature.c_str(), dex_method_idx);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800284 os << StringPrintf("\t\tframe_size_in_bytes: %zd\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800285 oat_method.GetFrameSizeInBytes());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800286 os << StringPrintf("\t\tcore_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800287 oat_method.GetCoreSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800288 DumpSpillMask(os, oat_method.GetCoreSpillMask(), false);
289 os << StringPrintf("\n\t\tfp_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800290 oat_method.GetFpSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800291 DumpSpillMask(os, oat_method.GetFpSpillMask(), true);
292 os << StringPrintf("\n\t\tmapping_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800293 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800294 DumpMappingTable(os, oat_method);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800295 os << StringPrintf("\t\tvmap_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800296 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800297 DumpVmap(os, oat_method.GetVmapTable(), oat_method.GetCoreSpillMask(),
298 oat_method.GetFpSpillMask());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800299 os << StringPrintf("\t\tgc_map: %p (offset=0x%08x)\n",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800300 oat_method.GetGcMap(), oat_method.GetGcMapOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800301 DumpGcMap(os, oat_method.GetGcMap());
Elliott Hughes77405792012-03-15 15:22:12 -0700302 os << StringPrintf("\t\tCODE: %p (offset=0x%08x size=%d)%s\n",
303 oat_method.GetCode(),
304 oat_method.GetCodeOffset(),
305 oat_method.GetCodeSize(),
306 oat_method.GetCode() != NULL ? "..." : "");
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800307 DumpCode(os, oat_method.GetCode(), oat_method.GetMappingTable(), dex_file, code_item);
Elliott Hughes77405792012-03-15 15:22:12 -0700308 os << StringPrintf("\t\tINVOKE STUB: %p (offset=0x%08x size=%d)%s\n",
309 oat_method.GetInvokeStub(),
310 oat_method.GetInvokeStubOffset(),
311 oat_method.GetInvokeStubSize(),
312 oat_method.GetInvokeStub() != NULL ? "..." : "");
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800313 DumpCode(os, reinterpret_cast<const void*>(oat_method.GetInvokeStub()), NULL, dex_file, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700314 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800315
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800316 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
317 if (spill_mask == 0) {
318 return;
319 }
320 os << " (";
321 for (size_t i = 0; i < 32; i++) {
322 if ((spill_mask & (1 << i)) != 0) {
323 if (is_float) {
324 os << "fr" << i;
325 } else {
326 os << "r" << i;
327 }
328 spill_mask ^= 1 << i; // clear bit
329 if (spill_mask != 0) {
330 os << ", ";
331 } else {
332 break;
333 }
334 }
335 }
336 os << ")";
337 }
338
339 void DumpVmap(std::ostream& os, const uint16_t* raw_table, uint32_t core_spill_mask,
340 uint32_t fp_spill_mask) {
341 if (raw_table == NULL) {
342 return;
343 }
344 const VmapTable vmap_table(raw_table);
345 bool first = true;
346 os << "\t\t\t";
347 for (size_t i = 0; i < vmap_table.size(); i++) {
348 uint16_t dex_reg = vmap_table[i];
349 size_t matches = 0;
350 size_t spill_shifts = 0;
351 uint32_t spill_mask = core_spill_mask;
352 bool processing_fp = false;
353 while (matches != (i + 1)) {
354 if (spill_mask == 0) {
355 CHECK(!processing_fp);
356 spill_mask = fp_spill_mask;
357 processing_fp = true;
358 }
359 matches += spill_mask & 1; // Add 1 if the low bit is set
360 spill_mask >>= 1;
361 spill_shifts++;
362 }
363 size_t arm_reg = spill_shifts - 1; // wind back one as we want the last match
364 os << (first ? "v" : ", v") << dex_reg;
365 if (arm_reg < 16) {
366 os << "/r" << arm_reg;
367 } else {
368 os << "/fr" << (arm_reg - 16);
369 }
370 if (first) {
371 first = false;
372 }
373 }
374 os << std::endl;
375 }
376
377 void DumpGcMap(std::ostream& os, const uint8_t* gc_map_raw) {
378 if (gc_map_raw == NULL) {
379 return;
380 }
381 uint32_t gc_map_length = (gc_map_raw[0] << 24) | (gc_map_raw[1] << 16) |
382 (gc_map_raw[2] << 8) | (gc_map_raw[3] << 0);
383 verifier::PcToReferenceMap map(gc_map_raw + sizeof(uint32_t), gc_map_length);
384 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
385 os << StringPrintf("\t\t\t0x%04x", map.GetPC(entry));
386 size_t num_regs = map.RegWidth() * 8;
387 const uint8_t* reg_bitmap = map.GetBitMap(entry);
388 bool first = true;
389 for (size_t reg = 0; reg < num_regs; reg++) {
390 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
391 if (first) {
392 os << " v" << reg;
393 first = false;
394 } else {
395 os << ", v" << reg;
396 }
397 }
398 }
399 os << std::endl;
400 }
401 }
402
403 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800404 const uint32_t* raw_table = oat_method.GetMappingTable();
405 const void* code = oat_method.GetCode();
406 if (raw_table == NULL || code == NULL) {
407 return;
408 }
409
410 uint32_t length = *raw_table;
411 ++raw_table;
412
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800413 os << "\t\t{";
Elliott Hughese3c845c2012-02-28 17:23:01 -0800414 for (size_t i = 0; i < length; i += 2) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800415 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800416 uint32_t dex_pc = raw_table[i + 1];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800417 os << StringPrintf("%p -> 0x%04x", native_pc, dex_pc);
418 if (i + 2 < length) {
419 os << ", ";
420 }
421 }
422 os << "}" << std::endl << std::flush;
423 }
424
425 void DumpCode(std::ostream& os, const void* code, const uint32_t* raw_mapping_table,
426 const DexFile& dex_file, const DexFile::CodeItem* code_item) {
427 if (code == NULL) {
428 return;
429 }
430
431 if (raw_mapping_table == NULL) {
432 // code but no mapping table is most likely caused by code created by the JNI compiler
433 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code);
434 const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file_.Begin());
435 uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin);
436
437 typedef std::set<uint32_t>::iterator It;
438 It it = offsets_.upper_bound(last_offset);
439 CHECK(it != offsets_.end());
440 const uint8_t* end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it;
441 CHECK(native_pc < end_native_pc);
442
443 disassembler_->Dump(os, native_pc, end_native_pc);
444 return;
445 }
446
447 uint32_t length = *raw_mapping_table;
448 ++raw_mapping_table;
449
450 for (size_t i = 0; i < length; i += 2) {
451 uint32_t dex_pc = raw_mapping_table[i + 1];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800452 const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]);
453 os << StringPrintf("\t\t0x%04x: %s\n", dex_pc, instruction->DumpString(&dex_file).c_str());
454
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800455 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800456 const uint8_t* end_native_pc = NULL;
457 if (i + 2 < length) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800458 end_native_pc = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i + 2];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800459 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800460 const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file_.Begin());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800461 uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin);
462
463 typedef std::set<uint32_t>::iterator It;
Elliott Hughesed2adb62012-02-29 14:41:01 -0800464 It it = offsets_.upper_bound(last_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800465 CHECK(it != offsets_.end());
466 end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it;
467 }
Elliott Hughesed2adb62012-02-29 14:41:01 -0800468 CHECK(native_pc < end_native_pc);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800469 disassembler_->Dump(os, native_pc, end_native_pc);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800470 }
471 }
472
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800473 const OatFile& oat_file_;
474 std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800475 std::set<uint32_t> offsets_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800476 UniquePtr<Disassembler> disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700477};
478
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800479class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700480 public:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800481 explicit ImageDumper(std::ostream& os, const std::string& image_filename,
482 const std::string& host_prefix, Space& image_space,
483 const ImageHeader& image_header) : os_(os),
484 image_filename_(image_filename), host_prefix_(host_prefix),
485 image_space_(image_space), image_header_(image_header) {
486 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700487
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800488 void Dump() {
489 os_ << "MAGIC:\n";
490 os_ << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700491
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800492 os_ << "IMAGE BEGIN:\n";
493 os_ << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700494
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800495 os_ << "OAT CHECKSUM:\n";
496 os_ << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700497
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800498 os_ << "OAT BEGIN:\n";
499 os_ << reinterpret_cast<void*>(image_header_.GetOatBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700500
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800501 os_ << "OAT END:\n";
502 os_ << reinterpret_cast<void*>(image_header_.GetOatEnd()) << "\n\n";
503
504 os_ << "ROOTS:\n";
505 os_ << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700506 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700507 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
508 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700509 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800510 Object* image_root_object = image_header_.GetImageRoot(image_root);
511 os_ << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700512 if (image_root_object->IsObjectArray()) {
513 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
514 ObjectArray<Object>* image_root_object_array
515 = down_cast<ObjectArray<Object>*>(image_root_object);
516 // = image_root_object->AsObjectArray<Object>();
517 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800518 Object* value = image_root_object_array->Get(i);
519 if (value != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800520 os_ << "\t" << i << ": ";
Ian Rogersd5b32602012-02-26 16:40:04 -0800521 std::string summary;
522 PrettyObjectValue(summary, value->GetClass(), value);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800523 os_ << summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800524 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800525 os_ << StringPrintf("\t%d: null\n", i);
Ian Rogersd5b32602012-02-26 16:40:04 -0800526 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700527 }
528 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700529 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800530 os_ << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700531
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800532 os_ << "OAT LOCATION:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700533 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800534 Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation);
535 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800536 os_ << oat_location;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800537 if (!host_prefix_.empty()) {
538 oat_location = host_prefix_ + oat_location;
539 os_ << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700540 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800541 os_ << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800542 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700543 if (oat_file == NULL) {
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800544 os_ << "NOT FOUND\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700545 return;
546 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800547 os_ << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700548
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800549 stats_.oat_file_bytes = oat_file->Size();
550
551 oat_dumper_.reset(new OatDumper(*oat_file));
552
553 os_ << "OBJECTS:\n" << std::flush;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800554 HeapBitmap* heap_bitmap = Runtime::Current()->GetHeap()->GetLiveBits();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800555 DCHECK(heap_bitmap != NULL);
556 heap_bitmap->Walk(ImageDumper::Callback, this);
557 os_ << "\n";
558
559 os_ << "STATS:\n" << std::flush;
560 UniquePtr<File> file(OS::OpenFile(image_filename_.c_str(), false));
561 stats_.file_bytes = file->Length();
562 size_t header_bytes = sizeof(ImageHeader);
563 stats_.header_bytes = header_bytes;
564 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
565 stats_.alignment_bytes += alignment_bytes;
566 stats_.Dump(os_);
567 os_ << "\n";
568
569 os_ << std::flush;
570
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800571 oat_dumper_->Dump(os_);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700572 }
573
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700574 private:
575
Ian Rogersd5b32602012-02-26 16:40:04 -0800576 static void PrettyObjectValue(std::string& summary, Class* type, Object* value) {
577 CHECK(type != NULL);
578 if (value == NULL) {
579 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
580 } else if (type->IsStringClass()) {
581 String* string = value->AsString();
582 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
583 } else if (value->IsClass()) {
584 Class* klass = value->AsClass();
585 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
586 } else if (value->IsField()) {
587 Field* field = value->AsField();
588 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
589 } else if (value->IsMethod()) {
590 Method* method = value->AsMethod();
591 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
592 } else {
593 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
594 }
595 }
596
597 static void PrintField(std::string& summary, Field* field, Object* obj) {
598 FieldHelper fh(field);
599 Class* type = fh.GetType();
600 StringAppendF(&summary, "\t%s: ", fh.GetName());
601 if (type->IsPrimitiveLong()) {
602 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
603 } else if (type->IsPrimitiveDouble()) {
604 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
605 } else if (type->IsPrimitiveFloat()) {
606 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
607 } else if (type->IsPrimitive()){
608 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
609 } else {
610 Object* value = field->GetObj(obj);
611 PrettyObjectValue(summary, type, value);
612 }
613 }
614
615 static void DumpFields(std::string& summary, Object* obj, Class* klass) {
616 Class* super = klass->GetSuperClass();
617 if (super != NULL) {
618 DumpFields(summary, obj, super);
619 }
620 ObjectArray<Field>* fields = klass->GetIFields();
621 if (fields != NULL) {
622 for (int32_t i = 0; i < fields->GetLength(); i++) {
623 Field* field = fields->Get(i);
624 PrintField(summary, field, obj);
625 }
626 }
627 }
628
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800629 bool InDumpSpace(const Object* object) {
630 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700631 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800632
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700633 const void* GetOatCodeBegin(Method* m) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800634 Runtime* runtime = Runtime::Current();
635 const void* code = m->GetCode();
Ian Rogersfb6adba2012-03-04 21:51:51 -0800636 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800637 code = oat_dumper_->GetOatCode(m);
638 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700639 if (oat_dumper_->GetInstructionSet() == kThumb2) {
640 code = reinterpret_cast<void*>(reinterpret_cast<uint32_t>(code) & ~0x1);
641 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800642 return code;
643 }
644
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700645 uint32_t GetOatCodeSize(Method* m) {
646 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetOatCodeBegin(m));
647 if (oat_code_begin == NULL) {
648 return 0;
649 }
650 return oat_code_begin[-1];
651 }
652
653 const void* GetOatCodeEnd(Method* m) {
654 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetOatCodeBegin(m));
655 if (oat_code_begin == NULL) {
656 return NULL;
657 }
658 return oat_code_begin + GetOatCodeSize(m);
659 }
660
Brian Carlstrom78128a62011-09-15 17:21:19 -0700661 static void Callback(Object* obj, void* arg) {
662 DCHECK(obj != NULL);
663 DCHECK(arg != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800664 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700665 if (!state->InDumpSpace(obj)) {
666 return;
667 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700668
669 size_t object_bytes = obj->SizeOf();
670 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
671 state->stats_.object_bytes += object_bytes;
672 state->stats_.alignment_bytes += alignment_bytes;
673
Brian Carlstrom78128a62011-09-15 17:21:19 -0700674 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800675 Class* obj_class = obj->GetClass();
676 if (obj_class->IsArrayClass()) {
677 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
678 obj->AsArray()->GetLength());
679 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700680 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800681 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
682 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700683 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800684 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700685 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800686 } else if (obj->IsField()) {
687 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
688 PrettyField(obj->AsField()).c_str());
689 } else if (obj->IsMethod()) {
690 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
691 PrettyMethod(obj->AsMethod()).c_str());
692 } else if (obj_class->IsStringClass()) {
693 StringAppendF(&summary, "%p: java.lang.String \"%s\"\n", obj,
694 obj->AsString()->ToModifiedUtf8().c_str());
695 } else {
696 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
697 }
698 DumpFields(summary, obj, obj_class);
699 if (obj->IsObjectArray()) {
700 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
701 int32_t length = obj_array->GetLength();
702 for (int32_t i = 0; i < length; i++) {
703 Object* value = obj_array->Get(i);
704 size_t run = 0;
705 for (int32_t j = i + 1; j < length; j++) {
706 if (value == obj_array->Get(j)) {
707 run++;
708 } else {
709 break;
710 }
711 }
712 if (run == 0) {
713 StringAppendF(&summary, "\t%d: ", i);
714 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800715 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800716 i = i + run;
717 }
718 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
719 PrettyObjectValue(summary, value_class, value);
720 }
721 } else if (obj->IsClass()) {
722 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
723 if (sfields != NULL) {
724 summary += "\t\tSTATICS:\n";
725 for (int32_t i = 0; i < sfields->GetLength(); i++) {
726 Field* field = sfields->Get(i);
727 PrintField(summary, field, NULL);
728 }
729 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700730 } else if (obj->IsMethod()) {
731 Method* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700732 if (method->IsNative()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700733 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800734 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700735 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800736 bool first_occurrence;
737 size_t invoke_stub_size = state->ComputeOatSize(
738 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
739 if (first_occurrence) {
740 state->stats_.managed_to_native_code_bytes += invoke_stub_size;
741 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700742 const void* oat_code = state->GetOatCodeBegin(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800743 size_t code_size = state->ComputeOatSize(oat_code, &first_occurrence);
744 if (first_occurrence) {
745 state->stats_.native_to_managed_code_bytes += code_size;
746 }
747 if (oat_code != method->GetCode()) {
748 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
749 }
Ian Rogers19846512012-02-24 11:42:47 -0800750 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
751 method->IsResolutionMethod()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700752 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800753 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700754 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700755 } else {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800756 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
757 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700758
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800759 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700760 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700761 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800762
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800763 bool first_occurance;
764 size_t gc_map_bytes = state->ComputeOatSize(method->GetGcMapRaw(), &first_occurance);
765 if (first_occurance) {
766 state->stats_.gc_map_bytes += gc_map_bytes;
767 }
768
769 size_t pc_mapping_table_bytes =
770 state->ComputeOatSize(method->GetMappingTableRaw(), &first_occurance);
771 if (first_occurance) {
772 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
773 }
774
775 size_t vmap_table_bytes =
776 state->ComputeOatSize(method->GetVmapTableRaw(), &first_occurance);
777 if (first_occurance) {
778 state->stats_.vmap_table_bytes += vmap_table_bytes;
779 }
780
781 size_t invoke_stub_size = state->ComputeOatSize(
782 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurance);
783 if (first_occurance) {
784 state->stats_.native_to_managed_code_bytes += invoke_stub_size;
785 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700786 const void* oat_code_begin = state->GetOatCodeBegin(method);
787 const void* oat_code_end = state->GetOatCodeEnd(method);
788 // TODO: use oat_code_size and remove code_size based on offsets
789 // uint32_t oat_code_size = state->GetOatCodeSize(method);
790 size_t code_size = state->ComputeOatSize(oat_code_begin, &first_occurance);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800791 if (first_occurance) {
792 state->stats_.managed_code_bytes += code_size;
793 }
794 state->stats_.managed_code_bytes_ignoring_deduplication += code_size;
795
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700796 StringAppendF(&summary, "\t\tOAT CODE: %p-%p\n", oat_code_begin, oat_code_end);
Ian Rogersd5b32602012-02-26 16:40:04 -0800797 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800798 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
799
800 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
801 vmap_table_bytes + invoke_stub_size + code_size + object_bytes;
802
803 double expansion =
804 static_cast<double>(code_size) / static_cast<double>(dex_instruction_bytes);
805 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700806 }
807 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800808 std::string descriptor(ClassHelper(obj_class).GetDescriptor());
809 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
810 state->stats_.descriptor_to_count[descriptor] += 1;
811
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700812 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700813 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700814
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800815 std::set<const void*> already_seen_;
816 // Compute the size of the given data within the oat file and whether this is the first time
817 // this data has been requested
818 size_t ComputeOatSize(const void* oat_data, bool* first_occurance) {
819 if (already_seen_.count(oat_data) == 0) {
820 *first_occurance = true;
821 already_seen_.insert(oat_data);
822 } else {
823 *first_occurance = false;
824 }
825 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700826 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700827
828 public:
829 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800830 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700831 size_t file_bytes;
832
833 size_t header_bytes;
834 size_t object_bytes;
835 size_t alignment_bytes;
836
837 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800838 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700839 size_t managed_to_native_code_bytes;
840 size_t native_to_managed_code_bytes;
841
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800842 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700843 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800844 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700845
846 size_t dex_instruction_bytes;
847
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800848 std::vector<Method*> method_outlier;
849 std::vector<size_t> method_outlier_size;
850 std::vector<double> method_outlier_expansion;
851
852 explicit Stats()
853 : oat_file_bytes(0),
854 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700855 header_bytes(0),
856 object_bytes(0),
857 alignment_bytes(0),
858 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800859 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700860 managed_to_native_code_bytes(0),
861 native_to_managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800862 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700863 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800864 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700865 dex_instruction_bytes(0) {}
866
Elliott Hughese5448b52012-01-18 16:44:06 -0800867 typedef std::map<std::string, size_t> TableBytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700868 TableBytes descriptor_to_bytes;
869
Elliott Hughese5448b52012-01-18 16:44:06 -0800870 typedef std::map<std::string, size_t> TableCount;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700871 TableCount descriptor_to_count;
872
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800873 double PercentOfOatBytes(size_t size) {
874 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
875 }
876
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700877 double PercentOfFileBytes(size_t size) {
878 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
879 }
880
881 double PercentOfObjectBytes(size_t size) {
882 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
883 }
884
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800885 void ComputeOutliers(size_t total_size, double expansion, Method* method) {
886 method_outlier_size.push_back(total_size);
887 method_outlier_expansion.push_back(expansion);
888 method_outlier.push_back(method);
889 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700890
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800891 void DumpOutliers(std::ostream& os) {
892 size_t sum_of_sizes = 0;
893 size_t sum_of_sizes_squared = 0;
894 size_t sum_of_expansion = 0;
895 size_t sum_of_expansion_squared = 0;
896 size_t n = method_outlier_size.size();
897 for (size_t i = 0; i < n; i++) {
898 size_t cur_size = method_outlier_size[i];
899 sum_of_sizes += cur_size;
900 sum_of_sizes_squared += cur_size * cur_size;
901 double cur_expansion = method_outlier_expansion[i];
902 sum_of_expansion += cur_expansion;
903 sum_of_expansion_squared += cur_expansion * cur_expansion;
904 }
905 size_t size_mean = sum_of_sizes / n;
906 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
907 double expansion_mean = sum_of_expansion / n;
908 double expansion_variance =
909 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
910
911 // Dump methods whose size is a certain number of standard deviations from the mean
912 size_t dumped_values = 0;
913 size_t skipped_values = 0;
914 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
915 size_t cur_size_variance = i * i * size_variance;
916 bool first = true;
917 for (size_t j = 0; j < n; j++) {
918 size_t cur_size = method_outlier_size[j];
919 if (cur_size > size_mean) {
920 size_t cur_var = cur_size - size_mean;
921 cur_var = cur_var * cur_var;
922 if (cur_var > cur_size_variance) {
923 if (dumped_values > 20) {
924 if (i == 1) {
925 skipped_values++;
926 } else {
927 i = 2; // jump to counting for 1 standard deviation
928 break;
929 }
930 } else {
931 if (first) {
932 os << "\nBig methods (size > " << i << " standard deviations the norm):"
933 << std::endl;
934 first = false;
935 }
936 os << "\t" << PrettyMethod(method_outlier[j]) << " requires storage of "
937 << PrettySize(cur_size) << std::endl;
938 method_outlier_size[j] = 0; // don't consider this method again
939 dumped_values++;
940 }
941 }
942 }
943 }
944 }
945 if (skipped_values > 0) {
946 os << "\t... skipped " << skipped_values
947 << " methods with size > 1 standard deviation from the norm" << std::endl;
948 }
949 os << std::endl << std::flush;
950
951 // Dump methods whose expansion is a certain number of standard deviations from the mean
952 dumped_values = 0;
953 skipped_values = 0;
954 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
955 double cur_expansion_variance = i * i * expansion_variance;
956 bool first = true;
957 for (size_t j = 0; j < n; j++) {
958 double cur_expansion = method_outlier_expansion[j];
959 if (cur_expansion > expansion_mean) {
960 size_t cur_var = cur_expansion - expansion_mean;
961 cur_var = cur_var * cur_var;
962 if (cur_var > cur_expansion_variance) {
963 if (dumped_values > 20) {
964 if (i == 1) {
965 skipped_values++;
966 } else {
967 i = 2; // jump to counting for 1 standard deviation
968 break;
969 }
970 } else {
971 if (first) {
972 os << "\nLarge expansion methods (size > " << i
973 << " standard deviations the norm):" << std::endl;
974 first = false;
975 }
976 os << "\t" << PrettyMethod(method_outlier[j]) << " expanded code by "
977 << cur_expansion << std::endl;
978 method_outlier_expansion[j] = 0.0; // don't consider this method again
979 dumped_values++;
980 }
981 }
982 }
983 }
984 }
985 if (skipped_values > 0) {
986 os << "\t... skipped " << skipped_values
987 << " methods with expansion > 1 standard deviation from the norm" << std::endl;
988 }
989 os << std::endl << std::flush;
990 }
991
992 void Dump(std::ostream& os) {
993 os << "\tart_file_bytes = " << PrettySize(file_bytes) << std::endl << std::endl
994 << "\tart_file_bytes = header_bytes + object_bytes + alignment_bytes" << std::endl
995 << StringPrintf("\theader_bytes = %8zd (%2.0f%% of art file bytes)\n"
996 "\tobject_bytes = %8zd (%2.0f%% of art file bytes)\n"
997 "\talignment_bytes = %8zd (%2.0f%% of art file bytes)\n",
998 header_bytes, PercentOfFileBytes(header_bytes),
999 object_bytes, PercentOfFileBytes(object_bytes),
1000 alignment_bytes, PercentOfFileBytes(alignment_bytes))
1001 << std::endl << std::flush;
1002
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001003 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
1004
1005 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
1006 size_t object_bytes_total = 0;
1007 typedef TableBytes::const_iterator It; // TODO: C++0x auto
1008 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -08001009 const std::string& descriptor(it->first);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001010 size_t bytes = it->second;
1011 size_t count = descriptor_to_count[descriptor];
1012 double average = static_cast<double>(bytes) / static_cast<double>(count);
1013 double percent = PercentOfObjectBytes(bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001014 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001015 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
1016 descriptor.c_str(), bytes, count,
1017 average, percent);
1018
1019 object_bytes_total += bytes;
1020 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001021 os << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001022 CHECK_EQ(object_bytes, object_bytes_total);
1023
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001024 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1025 "\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1026 "\tnative_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n",
1027 managed_code_bytes, PercentOfOatBytes(managed_code_bytes),
1028 managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes),
1029 native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes))
1030 << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001031
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001032 os << StringPrintf("\tgc_map_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1033 "\tpc_mapping_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1034 "\tvmap_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n",
1035 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1036 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1037 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
1038 << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001039
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001040 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001041 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n",
1042 static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes),
1043 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
1044 static_cast<double>(dex_instruction_bytes));
1045 os << std::endl << std::flush;
1046
1047 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001048 }
1049 } stats_;
1050
1051 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001052 UniquePtr<OatDumper> oat_dumper_;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001053 std::ostream& os_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001054 const std::string image_filename_;
1055 const std::string host_prefix_;
1056 Space& image_space_;
1057 const ImageHeader& image_header_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001058
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001059 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001060};
1061
1062int oatdump(int argc, char** argv) {
1063 // Skip over argv[0].
1064 argv++;
1065 argc--;
1066
1067 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001068 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001069 usage();
1070 }
1071
Brian Carlstromaded5f72011-10-07 17:15:04 -07001072 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001073 const char* image_filename = NULL;
1074 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001075 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001076 std::ostream* os = &std::cout;
1077 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001078
1079 for (int i = 0; i < argc; i++) {
1080 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001081 if (option.starts_with("--oat-file=")) {
1082 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001083 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001084 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001085 } else if (option.starts_with("--boot-image=")) {
1086 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001087 } else if (option.starts_with("--host-prefix=")) {
1088 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001089 } else if (option.starts_with("--output=")) {
1090 const char* filename = option.substr(strlen("--output=")).data();
1091 out.reset(new std::ofstream(filename));
1092 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001093 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001094 usage();
1095 }
1096 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001097 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001098 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001099 usage();
1100 }
1101 }
1102
Brian Carlstromaded5f72011-10-07 17:15:04 -07001103 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001104 fprintf(stderr, "Either --image or --oat must be specified\n");
1105 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001106 }
1107
Brian Carlstromaded5f72011-10-07 17:15:04 -07001108 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001109 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1110 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001111 }
1112
1113 if (oat_filename != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001114 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001115 if (oat_file == NULL) {
1116 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
1117 return EXIT_FAILURE;
1118 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001119 OatDumper oat_dumper(*oat_file);
1120 oat_dumper.Dump(*os);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001121 return EXIT_SUCCESS;
1122 }
1123
Brian Carlstrom78128a62011-09-15 17:21:19 -07001124 Runtime::Options options;
1125 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001126 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001127 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001128 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001129 if (boot_image_filename != NULL) {
1130 boot_image_option += "-Ximage:";
1131 boot_image_option += boot_image_filename;
1132 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001133 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001134 if (image_filename != NULL) {
1135 image_option += "-Ximage:";
1136 image_option += image_filename;
1137 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1138 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001139
Brian Carlstromfe487d02012-02-29 18:49:16 -08001140 if (host_prefix.empty()) {
1141 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
1142 if (android_product_out != NULL) {
1143 host_prefix = android_product_out;
1144 }
1145 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001146 if (!host_prefix.empty()) {
1147 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
1148 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001149
1150 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
1151 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001152 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001153 return EXIT_FAILURE;
1154 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001155
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001156 Heap* heap = Runtime::Current()->GetHeap();
Brian Carlstromfddf6f62012-03-15 16:56:45 -07001157 ImageSpace* image_space = heap->GetImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001158 CHECK(image_space != NULL);
1159 const ImageHeader& image_header = image_space->GetImageHeader();
1160 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001161 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001162 return EXIT_FAILURE;
1163 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001164 ImageDumper image_dumper(*os, image_filename, host_prefix, *image_space, image_header);
1165 image_dumper.Dump();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001166 return EXIT_SUCCESS;
1167}
1168
1169} // namespace art
1170
1171int main(int argc, char** argv) {
1172 return art::oatdump(argc, argv);
1173}