blob: 44632c0263cc71724336ad54a8af8ea4d6a29437 [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"
Elliott Hughese3c845c2012-02-28 17:23:01 -080027#include "dex_instruction.h"
Brian Carlstrom916e74e2011-09-23 11:42:01 -070028#include "file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070029#include "image.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080030#include "object_utils.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080031#include "os.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070032#include "runtime.h"
33#include "space.h"
34#include "stringpiece.h"
35
36namespace art {
37
38static void usage() {
39 fprintf(stderr,
40 "Usage: oatdump [options] ...\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080041 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n"
42 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070043 "\n");
44 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080045 " --oat-file=<file.oat>: specifies an input oat filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080046 " Example: --image=/system/framework/boot.oat\n"
Brian Carlstromaded5f72011-10-07 17:15:04 -070047 "\n");
48 fprintf(stderr,
49 " --image=<file.art>: specifies an input image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080050 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070051 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070052 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070053 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080054 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070055 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070056 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070057 " --host-prefix may be used to translate host paths to target paths during\n"
58 " cross compilation.\n"
59 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070060 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070061 fprintf(stderr,
62 " --output=<file> may be used to send the output to a file.\n"
63 " Example: --output=/tmp/oatdump.txt\n"
64 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070065 exit(EXIT_FAILURE);
66}
67
Ian Rogersff1ed472011-09-20 13:46:24 -070068const char* image_roots_descriptions_[] = {
Brian Carlstrom78128a62011-09-15 17:21:19 -070069 "kJniStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070070 "kAbstractMethodErrorStubArray",
Ian Rogersad25ac52011-10-04 19:13:33 -070071 "kInstanceResolutionStubArray",
72 "kStaticResolutionStubArray",
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070073 "kUnknownMethodResolutionStubArray",
Ian Rogers19846512012-02-24 11:42:47 -080074 "kResolutionMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070075 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070076 "kRefsOnlySaveMethod",
77 "kRefsAndArgsSaveMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070078 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070079 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070080 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070081};
82
Elliott Hughese3c845c2012-02-28 17:23:01 -080083class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -070084 public:
Elliott Hughese3c845c2012-02-28 17:23:01 -080085 void Dump(const std::string& oat_filename, std::ostream& os, const OatFile& oat_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -070086 const OatHeader& oat_header = oat_file.GetOatHeader();
87
88 os << "MAGIC:\n";
89 os << oat_header.GetMagic() << "\n\n";
90
91 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -080092 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -070093
94 os << "DEX FILE COUNT:\n";
95 os << oat_header.GetDexFileCount() << "\n\n";
96
97 os << "EXECUTABLE OFFSET:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -080098 os << StringPrintf("0x%08x\n\n", oat_header.GetExecutableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -070099
Ian Rogers30fab402012-01-23 15:43:46 -0800100 os << "BEGIN:\n";
101 os << reinterpret_cast<const void*>(oat_file.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700102
Ian Rogers30fab402012-01-23 15:43:46 -0800103 os << "END:\n";
104 os << reinterpret_cast<const void*>(oat_file.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700105
106 os << std::flush;
107
Elliott Hughesba8eee12012-01-24 20:25:24 -0800108 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file.GetOatDexFiles();
Elliott Hughese3c845c2012-02-28 17:23:01 -0800109 AddAllOffsets(oat_file, oat_dex_files);
110
Brian Carlstromaded5f72011-10-07 17:15:04 -0700111 for (size_t i = 0; i < oat_dex_files.size(); i++) {
112 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
113 CHECK(oat_dex_file != NULL);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800114 DumpOatDexFile(os, oat_file, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700115 }
116 }
117
118 private:
Elliott Hughese3c845c2012-02-28 17:23:01 -0800119 void AddAllOffsets(const OatFile& oat_file, std::vector<const OatFile::OatDexFile*>& oat_dex_files) {
120 // We don't know the length of the code for each method, but we need to know where to stop
121 // when disassembling. What we do know is that a region of code will be followed by some other
122 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
123 // of a piece of code by using upper_bound to find the start of the next region.
124 for (size_t i = 0; i < oat_dex_files.size(); i++) {
125 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
126 CHECK(oat_dex_file != NULL);
127 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
128 if (dex_file.get() == NULL) {
129 return;
130 }
131 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
132 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
133 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index));
134 const byte* class_data = dex_file->GetClassData(class_def);
135 if (class_data != NULL) {
136 ClassDataItemIterator it(*dex_file, class_data);
137 SkipAllFields(it);
138 uint32_t class_method_index = 0;
139 while (it.HasNextDirectMethod()) {
140 AddOffsets(oat_class->GetOatMethod(class_method_index++));
141 it.Next();
142 }
143 while (it.HasNextVirtualMethod()) {
144 AddOffsets(oat_class->GetOatMethod(class_method_index++));
145 it.Next();
146 }
147 }
148 }
149 }
150
151 // If the last thing in the file is code for a method, there won't be an offset for the "next"
152 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
153 // for the end of the file.
154 offsets_.insert(static_cast<uint32_t>(oat_file.End() - oat_file.Begin()));
155 }
156
157 void AddOffsets(const OatFile::OatMethod& oat_method) {
158 offsets_.insert(oat_method.GetCodeOffset());
159 offsets_.insert(oat_method.GetMappingTableOffset());
160 offsets_.insert(oat_method.GetVmapTableOffset());
161 offsets_.insert(oat_method.GetGcMapOffset());
162 offsets_.insert(oat_method.GetInvokeStubOffset());
163 }
164
165 void DumpOatDexFile(std::ostream& os, const OatFile& oat_file,
166 const OatFile::OatDexFile& oat_dex_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700167 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800168 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800169 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800170 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile());
171 if (dex_file.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700172 os << "NOT FOUND\n\n";
173 return;
174 }
175 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
176 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
177 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700178 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
179 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800180 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800181 << oat_class->GetStatus() << ")\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800182 DumpOatClass(os, oat_file, *oat_class.get(), *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700183 }
184
185 os << std::flush;
186 }
187
Elliott Hughese3c845c2012-02-28 17:23:01 -0800188 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700189 while (it.HasNextStaticField()) {
190 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700191 }
Ian Rogers0571d352011-11-03 19:51:38 -0700192 while (it.HasNextInstanceField()) {
193 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700194 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800195 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700196
Elliott Hughese3c845c2012-02-28 17:23:01 -0800197 void DumpOatClass(std::ostream& os, const OatFile& oat_file, const OatFile::OatClass& oat_class,
198 const DexFile& dex_file, const DexFile::ClassDef& class_def) {
199 const byte* class_data = dex_file.GetClassData(class_def);
200 if (class_data == NULL) { // empty class such as a marker interface?
201 return;
202 }
203 ClassDataItemIterator it(dex_file, class_data);
204 SkipAllFields(it);
205
206 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700207 while (it.HasNextDirectMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800208 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
209 DumpOatMethod(os, class_method_index, oat_file, oat_method, dex_file,
210 it.GetMemberIndex(), it.GetMethodCodeItem());
211 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700212 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700213 }
Ian Rogers0571d352011-11-03 19:51:38 -0700214 while (it.HasNextVirtualMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800215 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
216 DumpOatMethod(os, class_method_index, oat_file, oat_method, dex_file,
217 it.GetMemberIndex(), it.GetMethodCodeItem());
218 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700219 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700220 }
Ian Rogers0571d352011-11-03 19:51:38 -0700221 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700222 os << std::flush;
223 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800224
225 void DumpOatMethod(std::ostream& os, uint32_t class_method_index, const OatFile& oat_file,
226 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
227 uint32_t dex_method_idx, const DexFile::CodeItem* code_item) {
228 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700229 const char* name = dex_file.GetMethodName(method_id);
Elliott Hughes95572412011-12-13 18:14:20 -0800230 std::string signature(dex_file.GetMethodSignature(method_id));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800231 os << StringPrintf("\t%d: %s %s (dex_method_idx=%d)\n",
232 class_method_index, name, signature.c_str(), dex_method_idx);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800233 os << StringPrintf("\t\tcode: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800234 oat_method.GetCode(), oat_method.GetCodeOffset());
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800235 os << StringPrintf("\t\tframe_size_in_bytes: %zd\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800236 oat_method.GetFrameSizeInBytes());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800237 os << StringPrintf("\t\tcore_spill_mask: 0x%08x\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800238 oat_method.GetCoreSpillMask());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800239 os << StringPrintf("\t\tfp_spill_mask: 0x%08x\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800240 oat_method.GetFpSpillMask());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800241 os << StringPrintf("\t\tmapping_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800242 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800243 DumpMappingTable(os, oat_file, oat_method, dex_file, code_item);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800244 os << StringPrintf("\t\tvmap_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800245 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800246 os << StringPrintf("\t\tgc_map: %p (offset=0x%08x)\n",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800247 oat_method.GetGcMap(), oat_method.GetGcMapOffset());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800248 os << StringPrintf("\t\tinvoke_stub: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800249 oat_method.GetInvokeStub(), oat_method.GetInvokeStubOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700250 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800251
252 void DumpMappingTable(std::ostream& os,
253 const OatFile& oat_file, const OatFile::OatMethod& oat_method,
254 const DexFile& dex_file, const DexFile::CodeItem* code_item) {
255 const uint32_t* raw_table = oat_method.GetMappingTable();
256 const void* code = oat_method.GetCode();
257 if (raw_table == NULL || code == NULL) {
258 return;
259 }
260
261 uint32_t length = *raw_table;
262 ++raw_table;
263
264 for (size_t i = 0; i < length; i += 2) {
265 uint32_t dex_pc = raw_table[i + 1];
266 const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]);
267 os << StringPrintf("\t\t0x%04x: %s\n", dex_pc, instruction->DumpString(&dex_file).c_str());
268
Elliott Hughese3c845c2012-02-28 17:23:01 -0800269 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i];
270 const uint8_t* end_native_pc = NULL;
271 if (i + 2 < length) {
272 end_native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i + 2];
273 } else {
274 const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file.Begin());
275 uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin);
276
277 typedef std::set<uint32_t>::iterator It;
Elliott Hughesed2adb62012-02-29 14:41:01 -0800278 It it = offsets_.upper_bound(last_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800279 CHECK(it != offsets_.end());
280 end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it;
281 }
282
283 // TODO: insert disassembler here.
Elliott Hughesed2adb62012-02-29 14:41:01 -0800284 CHECK(native_pc < end_native_pc);
285 os << StringPrintf("\t\t\t%p:", native_pc);
286 for (; native_pc < end_native_pc; ++native_pc) {
287 os << StringPrintf(" 0x%02x", *native_pc);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800288 }
Elliott Hughesed2adb62012-02-29 14:41:01 -0800289 os << "\n";
Elliott Hughese3c845c2012-02-28 17:23:01 -0800290 }
291 }
292
293 std::set<uint32_t> offsets_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700294};
295
296class ImageDump {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700297 public:
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700298 static void Dump(const std::string& image_filename,
Brian Carlstromaded5f72011-10-07 17:15:04 -0700299 const std::string& host_prefix,
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700300 std::ostream& os,
301 Space& image_space,
302 const ImageHeader& image_header) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700303 os << "MAGIC:\n";
304 os << image_header.GetMagic() << "\n\n";
305
Ian Rogers30fab402012-01-23 15:43:46 -0800306 os << "IMAGE BEGIN:\n";
307 os << reinterpret_cast<void*>(image_header.GetImageBegin()) << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700308
Brian Carlstromaded5f72011-10-07 17:15:04 -0700309 os << "OAT CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800310 os << StringPrintf("0x%08x\n\n", image_header.GetOatChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700311
Ian Rogers30fab402012-01-23 15:43:46 -0800312 os << "OAT BEGIN:\n";
313 os << reinterpret_cast<void*>(image_header.GetOatBegin()) << "\n\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700314
Ian Rogers30fab402012-01-23 15:43:46 -0800315 os << "OAT END:\n";
316 os << reinterpret_cast<void*>(image_header.GetOatEnd()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700317
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700318 os << "ROOTS:\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700319 os << reinterpret_cast<void*>(image_header.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700320 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700321 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
322 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700323 const char* image_root_description = image_roots_descriptions_[i];
324 Object* image_root_object = image_header.GetImageRoot(image_root);
325 os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
326 if (image_root_object->IsObjectArray()) {
327 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
328 ObjectArray<Object>* image_root_object_array
329 = down_cast<ObjectArray<Object>*>(image_root_object);
330 // = image_root_object->AsObjectArray<Object>();
331 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800332 Object* value = image_root_object_array->Get(i);
333 if (value != NULL) {
334 os << "\t" << i << ": ";
335 std::string summary;
336 PrettyObjectValue(summary, value->GetClass(), value);
337 os << summary;
338 } else {
339 os << StringPrintf("\t%d: null\n", i);
340 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700341 }
342 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700343 }
344 os << "\n";
345
346 os << "OBJECTS:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700347 ImageDump state(image_space, os);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700348 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
349 DCHECK(heap_bitmap != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700350 heap_bitmap->Walk(ImageDump::Callback, &state);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700351 os << "\n";
352
353 os << "STATS:\n" << std::flush;
354 UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), false));
355 state.stats_.file_bytes = file->Length();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700356 size_t header_bytes = sizeof(ImageHeader);
357 state.stats_.header_bytes = header_bytes;
358 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
359 state.stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700360 state.stats_.Dump(os);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700361 os << "\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700362
363 os << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700364
365 os << "OAT LOCATION:\n" << std::flush;
366 Object* oat_location_object = image_header.GetImageRoot(ImageHeader::kOatLocation);
Elliott Hughes95572412011-12-13 18:14:20 -0800367 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700368 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
369 os << oat_location;
370 if (!host_prefix.empty()) {
371 oat_location = host_prefix + oat_location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700372 os << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700373 }
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700374 os << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800375 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700376 if (oat_file == NULL) {
377 os << "NOT FOUND\n";
378 os << std::flush;
379 return;
380 }
381 os << "\n";
382 os << std::flush;
383
Elliott Hughese3c845c2012-02-28 17:23:01 -0800384 OatDumper oat_dumper;
385 oat_dumper.Dump(oat_location, os, *oat_file);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700386 }
387
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700388 private:
389
Brian Carlstromaded5f72011-10-07 17:15:04 -0700390 ImageDump(const Space& dump_space, std::ostream& os) : dump_space_(dump_space), os_(os) {}
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700391
Brian Carlstromaded5f72011-10-07 17:15:04 -0700392 ~ImageDump() {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700393
Ian Rogersd5b32602012-02-26 16:40:04 -0800394 static void PrettyObjectValue(std::string& summary, Class* type, Object* value) {
395 CHECK(type != NULL);
396 if (value == NULL) {
397 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
398 } else if (type->IsStringClass()) {
399 String* string = value->AsString();
400 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
401 } else if (value->IsClass()) {
402 Class* klass = value->AsClass();
403 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
404 } else if (value->IsField()) {
405 Field* field = value->AsField();
406 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
407 } else if (value->IsMethod()) {
408 Method* method = value->AsMethod();
409 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
410 } else {
411 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
412 }
413 }
414
415 static void PrintField(std::string& summary, Field* field, Object* obj) {
416 FieldHelper fh(field);
417 Class* type = fh.GetType();
418 StringAppendF(&summary, "\t%s: ", fh.GetName());
419 if (type->IsPrimitiveLong()) {
420 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
421 } else if (type->IsPrimitiveDouble()) {
422 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
423 } else if (type->IsPrimitiveFloat()) {
424 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
425 } else if (type->IsPrimitive()){
426 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
427 } else {
428 Object* value = field->GetObj(obj);
429 PrettyObjectValue(summary, type, value);
430 }
431 }
432
433 static void DumpFields(std::string& summary, Object* obj, Class* klass) {
434 Class* super = klass->GetSuperClass();
435 if (super != NULL) {
436 DumpFields(summary, obj, super);
437 }
438 ObjectArray<Field>* fields = klass->GetIFields();
439 if (fields != NULL) {
440 for (int32_t i = 0; i < fields->GetLength(); i++) {
441 Field* field = fields->Get(i);
442 PrintField(summary, field, obj);
443 }
444 }
445 }
446
Brian Carlstrom78128a62011-09-15 17:21:19 -0700447 static void Callback(Object* obj, void* arg) {
448 DCHECK(obj != NULL);
449 DCHECK(arg != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700450 ImageDump* state = reinterpret_cast<ImageDump*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700451 if (!state->InDumpSpace(obj)) {
452 return;
453 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700454
455 size_t object_bytes = obj->SizeOf();
456 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
457 state->stats_.object_bytes += object_bytes;
458 state->stats_.alignment_bytes += alignment_bytes;
459
Brian Carlstrom78128a62011-09-15 17:21:19 -0700460 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800461 Class* obj_class = obj->GetClass();
462 if (obj_class->IsArrayClass()) {
463 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
464 obj->AsArray()->GetLength());
465 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700466 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800467 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
468 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700469 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800470 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700471 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800472 } else if (obj->IsField()) {
473 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
474 PrettyField(obj->AsField()).c_str());
475 } else if (obj->IsMethod()) {
476 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
477 PrettyMethod(obj->AsMethod()).c_str());
478 } else if (obj_class->IsStringClass()) {
479 StringAppendF(&summary, "%p: java.lang.String \"%s\"\n", obj,
480 obj->AsString()->ToModifiedUtf8().c_str());
481 } else {
482 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
483 }
484 DumpFields(summary, obj, obj_class);
485 if (obj->IsObjectArray()) {
486 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
487 int32_t length = obj_array->GetLength();
488 for (int32_t i = 0; i < length; i++) {
489 Object* value = obj_array->Get(i);
490 size_t run = 0;
491 for (int32_t j = i + 1; j < length; j++) {
492 if (value == obj_array->Get(j)) {
493 run++;
494 } else {
495 break;
496 }
497 }
498 if (run == 0) {
499 StringAppendF(&summary, "\t%d: ", i);
500 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800501 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800502 i = i + run;
503 }
504 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
505 PrettyObjectValue(summary, value_class, value);
506 }
507 } else if (obj->IsClass()) {
508 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
509 if (sfields != NULL) {
510 summary += "\t\tSTATICS:\n";
511 for (int32_t i = 0; i < sfields->GetLength(); i++) {
512 Field* field = sfields->Get(i);
513 PrintField(summary, field, NULL);
514 }
515 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700516 } else if (obj->IsMethod()) {
517 Method* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700518 if (method->IsNative()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700519 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800520 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700521 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers19846512012-02-24 11:42:47 -0800522 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
523 method->IsResolutionMethod()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700524 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800525 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700526 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700527 } else {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800528 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
529 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700530
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800531 size_t register_map_bytes = method->GetGcMapLength();
532 state->stats_.register_map_bytes += register_map_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800533
534 size_t pc_mapping_table_bytes = method->GetMappingTableLength();
535 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700536
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800537 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700538 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700539 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800540
Ian Rogersd5b32602012-02-26 16:40:04 -0800541 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800542 dex_instruction_bytes, register_map_bytes, pc_mapping_table_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700543 }
544 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800545 std::string descriptor(ClassHelper(obj_class).GetDescriptor());
546 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
547 state->stats_.descriptor_to_count[descriptor] += 1;
548
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700549 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700550 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700551
552 bool InDumpSpace(const Object* object) {
Ian Rogers30fab402012-01-23 15:43:46 -0800553 return dump_space_.Contains(object);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700554 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700555
556 public:
557 struct Stats {
558 size_t file_bytes;
559
560 size_t header_bytes;
561 size_t object_bytes;
562 size_t alignment_bytes;
563
564 size_t managed_code_bytes;
565 size_t managed_to_native_code_bytes;
566 size_t native_to_managed_code_bytes;
567
568 size_t register_map_bytes;
569 size_t pc_mapping_table_bytes;
570
571 size_t dex_instruction_bytes;
572
573 Stats()
574 : file_bytes(0),
575 header_bytes(0),
576 object_bytes(0),
577 alignment_bytes(0),
578 managed_code_bytes(0),
579 managed_to_native_code_bytes(0),
580 native_to_managed_code_bytes(0),
581 register_map_bytes(0),
582 pc_mapping_table_bytes(0),
583 dex_instruction_bytes(0) {}
584
Elliott Hughese5448b52012-01-18 16:44:06 -0800585 typedef std::map<std::string, size_t> TableBytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700586 TableBytes descriptor_to_bytes;
587
Elliott Hughese5448b52012-01-18 16:44:06 -0800588 typedef std::map<std::string, size_t> TableCount;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700589 TableCount descriptor_to_count;
590
591 double PercentOfFileBytes(size_t size) {
592 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
593 }
594
595 double PercentOfObjectBytes(size_t size) {
596 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
597 }
598
599 void Dump(std::ostream& os) {
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800600 os << StringPrintf("\tfile_bytes = %zd\n", file_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700601 os << "\n";
602
603 os << "\tfile_bytes = header_bytes + object_bytes + alignment_bytes\n";
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800604 os << StringPrintf("\theader_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700605 header_bytes, PercentOfFileBytes(header_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800606 os << StringPrintf("\tobject_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700607 object_bytes, PercentOfFileBytes(object_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800608 os << StringPrintf("\talignment_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700609 alignment_bytes, PercentOfFileBytes(alignment_bytes));
610 os << "\n";
611 os << std::flush;
612 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
613
614 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
615 size_t object_bytes_total = 0;
616 typedef TableBytes::const_iterator It; // TODO: C++0x auto
617 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -0800618 const std::string& descriptor(it->first);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700619 size_t bytes = it->second;
620 size_t count = descriptor_to_count[descriptor];
621 double average = static_cast<double>(bytes) / static_cast<double>(count);
622 double percent = PercentOfObjectBytes(bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800623 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700624 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
625 descriptor.c_str(), bytes, count,
626 average, percent);
627
628 object_bytes_total += bytes;
629 }
630 os << "\n";
631 os << std::flush;
632 CHECK_EQ(object_bytes, object_bytes_total);
633
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800634 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700635 managed_code_bytes, PercentOfObjectBytes(managed_code_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800636 os << StringPrintf("\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700637 managed_to_native_code_bytes,
638 PercentOfObjectBytes(managed_to_native_code_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800639 os << StringPrintf("\tnative_to_managed_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700640 native_to_managed_code_bytes,
641 PercentOfObjectBytes(native_to_managed_code_bytes));
642 os << "\n";
643 os << std::flush;
644
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800645 os << StringPrintf("\tregister_map_bytes = %7zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700646 register_map_bytes, PercentOfObjectBytes(register_map_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800647 os << StringPrintf("\tpc_mapping_table_bytes = %7zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700648 pc_mapping_table_bytes, PercentOfObjectBytes(pc_mapping_table_bytes));
649 os << "\n";
650 os << std::flush;
651
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800652 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700653 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f\n",
654 static_cast<double>(managed_code_bytes)
655 / static_cast<double>(dex_instruction_bytes));
656 os << "\n";
657 os << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700658 }
659 } stats_;
660
661 private:
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700662 const Space& dump_space_;
663 std::ostream& os_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700664
Brian Carlstromaded5f72011-10-07 17:15:04 -0700665 DISALLOW_COPY_AND_ASSIGN(ImageDump);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700666};
667
668int oatdump(int argc, char** argv) {
669 // Skip over argv[0].
670 argv++;
671 argc--;
672
673 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700674 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700675 usage();
676 }
677
Brian Carlstromaded5f72011-10-07 17:15:04 -0700678 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700679 const char* image_filename = NULL;
680 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700681 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700682 std::ostream* os = &std::cout;
683 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700684
685 for (int i = 0; i < argc; i++) {
686 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800687 if (option.starts_with("--oat-file=")) {
688 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700689 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700690 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700691 } else if (option.starts_with("--boot-image=")) {
692 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700693 } else if (option.starts_with("--host-prefix=")) {
694 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700695 } else if (option.starts_with("--output=")) {
696 const char* filename = option.substr(strlen("--output=")).data();
697 out.reset(new std::ofstream(filename));
698 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700699 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700700 usage();
701 }
702 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700703 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700704 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700705 usage();
706 }
707 }
708
Brian Carlstromaded5f72011-10-07 17:15:04 -0700709 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700710 fprintf(stderr, "Either --image or --oat must be specified\n");
711 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700712 }
713
Brian Carlstromaded5f72011-10-07 17:15:04 -0700714 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700715 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
716 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700717 }
718
719 if (oat_filename != NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800720 const OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700721 if (oat_file == NULL) {
722 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
723 return EXIT_FAILURE;
724 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800725 OatDumper oat_dumper;
726 oat_dumper.Dump(oat_filename, *os, *oat_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700727 return EXIT_SUCCESS;
728 }
729
Brian Carlstrom78128a62011-09-15 17:21:19 -0700730 Runtime::Options options;
731 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700732 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700733 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700734 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700735 if (boot_image_filename != NULL) {
736 boot_image_option += "-Ximage:";
737 boot_image_option += boot_image_filename;
738 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -0700739 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700740 if (image_filename != NULL) {
741 image_option += "-Ximage:";
742 image_option += image_filename;
743 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
744 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700745
746 if (!host_prefix.empty()) {
747 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
748 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700749
750 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
751 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700752 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700753 return EXIT_FAILURE;
754 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700755
Ian Rogers30fab402012-01-23 15:43:46 -0800756 ImageSpace* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]->AsImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700757 CHECK(image_space != NULL);
758 const ImageHeader& image_header = image_space->GetImageHeader();
759 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700760 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700761 return EXIT_FAILURE;
762 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700763 ImageDump::Dump(image_filename, host_prefix, *os, *image_space, image_header);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700764 return EXIT_SUCCESS;
765}
766
767} // namespace art
768
769int main(int argc, char** argv) {
770 return art::oatdump(argc, argv);
771}