blob: 337c907a9bdc852e61bb5ecb746d1c3b71681164 [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";
92 os << StringPrintf("%08x\n\n", oat_header.GetChecksum());
93
94 os << "DEX FILE COUNT:\n";
95 os << oat_header.GetDexFileCount() << "\n\n";
96
97 os << "EXECUTABLE OFFSET:\n";
98 os << StringPrintf("%08x\n\n", oat_header.GetExecutableOffset());
99
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());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800169 os << StringPrintf("checksum: %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);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700233 os << StringPrintf("\t\tcode: %p (offset=%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());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700237 os << StringPrintf("\t\tcore_spill_mask: %08x\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800238 oat_method.GetCoreSpillMask());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700239 os << StringPrintf("\t\tfp_spill_mask: %08x\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800240 oat_method.GetFpSpillMask());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700241 os << StringPrintf("\t\tmapping_table: %p (offset=%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);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700244 os << StringPrintf("\t\tvmap_table: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800245 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800246 os << StringPrintf("\t\tgc_map: %p (offset=%08x)\n",
247 oat_method.GetGcMap(), oat_method.GetGcMapOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700248 os << StringPrintf("\t\tinvoke_stub: %p (offset=%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
269 // TODO: this is thumb2-specific.
270 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i];
271 const uint8_t* end_native_pc = NULL;
272 if (i + 2 < length) {
273 end_native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i + 2];
274 } else {
275 const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file.Begin());
276 uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin);
277
278 typedef std::set<uint32_t>::iterator It;
279 It it = offsets_.lower_bound(last_offset);
280 CHECK(it != offsets_.end());
281 end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it;
282 }
283
284 // TODO: insert disassembler here.
285 CHECK(native_pc <= end_native_pc);
286 for (; native_pc < end_native_pc; native_pc += 2) {
287 os << StringPrintf("\t\t\t%p: 0x%04x\n", native_pc, *reinterpret_cast<const uint16_t*>(native_pc));
288 }
289 }
290 }
291
292 std::set<uint32_t> offsets_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700293};
294
295class ImageDump {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700296 public:
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700297 static void Dump(const std::string& image_filename,
Brian Carlstromaded5f72011-10-07 17:15:04 -0700298 const std::string& host_prefix,
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700299 std::ostream& os,
300 Space& image_space,
301 const ImageHeader& image_header) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700302 os << "MAGIC:\n";
303 os << image_header.GetMagic() << "\n\n";
304
Ian Rogers30fab402012-01-23 15:43:46 -0800305 os << "IMAGE BEGIN:\n";
306 os << reinterpret_cast<void*>(image_header.GetImageBegin()) << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700307
Brian Carlstromaded5f72011-10-07 17:15:04 -0700308 os << "OAT CHECKSUM:\n";
309 os << StringPrintf("%08x\n\n", image_header.GetOatChecksum());
310
Ian Rogers30fab402012-01-23 15:43:46 -0800311 os << "OAT BEGIN:\n";
312 os << reinterpret_cast<void*>(image_header.GetOatBegin()) << "\n\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700313
Ian Rogers30fab402012-01-23 15:43:46 -0800314 os << "OAT END:\n";
315 os << reinterpret_cast<void*>(image_header.GetOatEnd()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700316
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700317 os << "ROOTS:\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700318 os << reinterpret_cast<void*>(image_header.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700319 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700320 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
321 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700322 const char* image_root_description = image_roots_descriptions_[i];
323 Object* image_root_object = image_header.GetImageRoot(image_root);
324 os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
325 if (image_root_object->IsObjectArray()) {
326 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
327 ObjectArray<Object>* image_root_object_array
328 = down_cast<ObjectArray<Object>*>(image_root_object);
329 // = image_root_object->AsObjectArray<Object>();
330 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800331 Object* value = image_root_object_array->Get(i);
332 if (value != NULL) {
333 os << "\t" << i << ": ";
334 std::string summary;
335 PrettyObjectValue(summary, value->GetClass(), value);
336 os << summary;
337 } else {
338 os << StringPrintf("\t%d: null\n", i);
339 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700340 }
341 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700342 }
343 os << "\n";
344
345 os << "OBJECTS:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700346 ImageDump state(image_space, os);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700347 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
348 DCHECK(heap_bitmap != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700349 heap_bitmap->Walk(ImageDump::Callback, &state);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700350 os << "\n";
351
352 os << "STATS:\n" << std::flush;
353 UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), false));
354 state.stats_.file_bytes = file->Length();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700355 size_t header_bytes = sizeof(ImageHeader);
356 state.stats_.header_bytes = header_bytes;
357 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
358 state.stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700359 state.stats_.Dump(os);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700360 os << "\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700361
362 os << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700363
364 os << "OAT LOCATION:\n" << std::flush;
365 Object* oat_location_object = image_header.GetImageRoot(ImageHeader::kOatLocation);
Elliott Hughes95572412011-12-13 18:14:20 -0800366 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700367 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
368 os << oat_location;
369 if (!host_prefix.empty()) {
370 oat_location = host_prefix + oat_location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700371 os << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700372 }
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700373 os << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800374 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700375 if (oat_file == NULL) {
376 os << "NOT FOUND\n";
377 os << std::flush;
378 return;
379 }
380 os << "\n";
381 os << std::flush;
382
Elliott Hughese3c845c2012-02-28 17:23:01 -0800383 OatDumper oat_dumper;
384 oat_dumper.Dump(oat_location, os, *oat_file);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700385 }
386
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700387 private:
388
Brian Carlstromaded5f72011-10-07 17:15:04 -0700389 ImageDump(const Space& dump_space, std::ostream& os) : dump_space_(dump_space), os_(os) {}
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700390
Brian Carlstromaded5f72011-10-07 17:15:04 -0700391 ~ImageDump() {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700392
Ian Rogersd5b32602012-02-26 16:40:04 -0800393 static void PrettyObjectValue(std::string& summary, Class* type, Object* value) {
394 CHECK(type != NULL);
395 if (value == NULL) {
396 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
397 } else if (type->IsStringClass()) {
398 String* string = value->AsString();
399 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
400 } else if (value->IsClass()) {
401 Class* klass = value->AsClass();
402 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
403 } else if (value->IsField()) {
404 Field* field = value->AsField();
405 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
406 } else if (value->IsMethod()) {
407 Method* method = value->AsMethod();
408 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
409 } else {
410 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
411 }
412 }
413
414 static void PrintField(std::string& summary, Field* field, Object* obj) {
415 FieldHelper fh(field);
416 Class* type = fh.GetType();
417 StringAppendF(&summary, "\t%s: ", fh.GetName());
418 if (type->IsPrimitiveLong()) {
419 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
420 } else if (type->IsPrimitiveDouble()) {
421 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
422 } else if (type->IsPrimitiveFloat()) {
423 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
424 } else if (type->IsPrimitive()){
425 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
426 } else {
427 Object* value = field->GetObj(obj);
428 PrettyObjectValue(summary, type, value);
429 }
430 }
431
432 static void DumpFields(std::string& summary, Object* obj, Class* klass) {
433 Class* super = klass->GetSuperClass();
434 if (super != NULL) {
435 DumpFields(summary, obj, super);
436 }
437 ObjectArray<Field>* fields = klass->GetIFields();
438 if (fields != NULL) {
439 for (int32_t i = 0; i < fields->GetLength(); i++) {
440 Field* field = fields->Get(i);
441 PrintField(summary, field, obj);
442 }
443 }
444 }
445
Brian Carlstrom78128a62011-09-15 17:21:19 -0700446 static void Callback(Object* obj, void* arg) {
447 DCHECK(obj != NULL);
448 DCHECK(arg != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700449 ImageDump* state = reinterpret_cast<ImageDump*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700450 if (!state->InDumpSpace(obj)) {
451 return;
452 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700453
454 size_t object_bytes = obj->SizeOf();
455 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
456 state->stats_.object_bytes += object_bytes;
457 state->stats_.alignment_bytes += alignment_bytes;
458
Brian Carlstrom78128a62011-09-15 17:21:19 -0700459 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800460 Class* obj_class = obj->GetClass();
461 if (obj_class->IsArrayClass()) {
462 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
463 obj->AsArray()->GetLength());
464 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700465 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800466 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
467 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700468 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800469 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700470 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800471 } else if (obj->IsField()) {
472 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
473 PrettyField(obj->AsField()).c_str());
474 } else if (obj->IsMethod()) {
475 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
476 PrettyMethod(obj->AsMethod()).c_str());
477 } else if (obj_class->IsStringClass()) {
478 StringAppendF(&summary, "%p: java.lang.String \"%s\"\n", obj,
479 obj->AsString()->ToModifiedUtf8().c_str());
480 } else {
481 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
482 }
483 DumpFields(summary, obj, obj_class);
484 if (obj->IsObjectArray()) {
485 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
486 int32_t length = obj_array->GetLength();
487 for (int32_t i = 0; i < length; i++) {
488 Object* value = obj_array->Get(i);
489 size_t run = 0;
490 for (int32_t j = i + 1; j < length; j++) {
491 if (value == obj_array->Get(j)) {
492 run++;
493 } else {
494 break;
495 }
496 }
497 if (run == 0) {
498 StringAppendF(&summary, "\t%d: ", i);
499 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800500 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800501 i = i + run;
502 }
503 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
504 PrettyObjectValue(summary, value_class, value);
505 }
506 } else if (obj->IsClass()) {
507 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
508 if (sfields != NULL) {
509 summary += "\t\tSTATICS:\n";
510 for (int32_t i = 0; i < sfields->GetLength(); i++) {
511 Field* field = sfields->Get(i);
512 PrintField(summary, field, NULL);
513 }
514 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700515 } else if (obj->IsMethod()) {
516 Method* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700517 if (method->IsNative()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700518 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800519 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700520 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers19846512012-02-24 11:42:47 -0800521 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
522 method->IsResolutionMethod()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700523 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800524 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700525 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700526 } else {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800527 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
528 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700529
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800530 size_t register_map_bytes = method->GetGcMapLength();
531 state->stats_.register_map_bytes += register_map_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800532
533 size_t pc_mapping_table_bytes = method->GetMappingTableLength();
534 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700535
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800536 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700537 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700538 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800539
Ian Rogersd5b32602012-02-26 16:40:04 -0800540 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800541 dex_instruction_bytes, register_map_bytes, pc_mapping_table_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700542 }
543 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800544 std::string descriptor(ClassHelper(obj_class).GetDescriptor());
545 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
546 state->stats_.descriptor_to_count[descriptor] += 1;
547
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700548 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700549 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700550
551 bool InDumpSpace(const Object* object) {
Ian Rogers30fab402012-01-23 15:43:46 -0800552 return dump_space_.Contains(object);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700553 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700554
555 public:
556 struct Stats {
557 size_t file_bytes;
558
559 size_t header_bytes;
560 size_t object_bytes;
561 size_t alignment_bytes;
562
563 size_t managed_code_bytes;
564 size_t managed_to_native_code_bytes;
565 size_t native_to_managed_code_bytes;
566
567 size_t register_map_bytes;
568 size_t pc_mapping_table_bytes;
569
570 size_t dex_instruction_bytes;
571
572 Stats()
573 : file_bytes(0),
574 header_bytes(0),
575 object_bytes(0),
576 alignment_bytes(0),
577 managed_code_bytes(0),
578 managed_to_native_code_bytes(0),
579 native_to_managed_code_bytes(0),
580 register_map_bytes(0),
581 pc_mapping_table_bytes(0),
582 dex_instruction_bytes(0) {}
583
Elliott Hughese5448b52012-01-18 16:44:06 -0800584 typedef std::map<std::string, size_t> TableBytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700585 TableBytes descriptor_to_bytes;
586
Elliott Hughese5448b52012-01-18 16:44:06 -0800587 typedef std::map<std::string, size_t> TableCount;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700588 TableCount descriptor_to_count;
589
590 double PercentOfFileBytes(size_t size) {
591 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
592 }
593
594 double PercentOfObjectBytes(size_t size) {
595 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
596 }
597
598 void Dump(std::ostream& os) {
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800599 os << StringPrintf("\tfile_bytes = %zd\n", file_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700600 os << "\n";
601
602 os << "\tfile_bytes = header_bytes + object_bytes + alignment_bytes\n";
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800603 os << StringPrintf("\theader_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700604 header_bytes, PercentOfFileBytes(header_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800605 os << StringPrintf("\tobject_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700606 object_bytes, PercentOfFileBytes(object_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800607 os << StringPrintf("\talignment_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700608 alignment_bytes, PercentOfFileBytes(alignment_bytes));
609 os << "\n";
610 os << std::flush;
611 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
612
613 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
614 size_t object_bytes_total = 0;
615 typedef TableBytes::const_iterator It; // TODO: C++0x auto
616 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -0800617 const std::string& descriptor(it->first);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700618 size_t bytes = it->second;
619 size_t count = descriptor_to_count[descriptor];
620 double average = static_cast<double>(bytes) / static_cast<double>(count);
621 double percent = PercentOfObjectBytes(bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800622 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700623 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
624 descriptor.c_str(), bytes, count,
625 average, percent);
626
627 object_bytes_total += bytes;
628 }
629 os << "\n";
630 os << std::flush;
631 CHECK_EQ(object_bytes, object_bytes_total);
632
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800633 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700634 managed_code_bytes, PercentOfObjectBytes(managed_code_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800635 os << StringPrintf("\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700636 managed_to_native_code_bytes,
637 PercentOfObjectBytes(managed_to_native_code_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800638 os << StringPrintf("\tnative_to_managed_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700639 native_to_managed_code_bytes,
640 PercentOfObjectBytes(native_to_managed_code_bytes));
641 os << "\n";
642 os << std::flush;
643
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800644 os << StringPrintf("\tregister_map_bytes = %7zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700645 register_map_bytes, PercentOfObjectBytes(register_map_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800646 os << StringPrintf("\tpc_mapping_table_bytes = %7zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700647 pc_mapping_table_bytes, PercentOfObjectBytes(pc_mapping_table_bytes));
648 os << "\n";
649 os << std::flush;
650
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800651 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700652 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f\n",
653 static_cast<double>(managed_code_bytes)
654 / static_cast<double>(dex_instruction_bytes));
655 os << "\n";
656 os << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700657 }
658 } stats_;
659
660 private:
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700661 const Space& dump_space_;
662 std::ostream& os_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700663
Brian Carlstromaded5f72011-10-07 17:15:04 -0700664 DISALLOW_COPY_AND_ASSIGN(ImageDump);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700665};
666
667int oatdump(int argc, char** argv) {
668 // Skip over argv[0].
669 argv++;
670 argc--;
671
672 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700673 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700674 usage();
675 }
676
Brian Carlstromaded5f72011-10-07 17:15:04 -0700677 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700678 const char* image_filename = NULL;
679 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700680 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700681 std::ostream* os = &std::cout;
682 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700683
684 for (int i = 0; i < argc; i++) {
685 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800686 if (option.starts_with("--oat-file=")) {
687 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700688 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700689 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700690 } else if (option.starts_with("--boot-image=")) {
691 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700692 } else if (option.starts_with("--host-prefix=")) {
693 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700694 } else if (option.starts_with("--output=")) {
695 const char* filename = option.substr(strlen("--output=")).data();
696 out.reset(new std::ofstream(filename));
697 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700698 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700699 usage();
700 }
701 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700702 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700703 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700704 usage();
705 }
706 }
707
Brian Carlstromaded5f72011-10-07 17:15:04 -0700708 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700709 fprintf(stderr, "Either --image or --oat must be specified\n");
710 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700711 }
712
Brian Carlstromaded5f72011-10-07 17:15:04 -0700713 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700714 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
715 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700716 }
717
718 if (oat_filename != NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800719 const OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700720 if (oat_file == NULL) {
721 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
722 return EXIT_FAILURE;
723 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800724 OatDumper oat_dumper;
725 oat_dumper.Dump(oat_filename, *os, *oat_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700726 return EXIT_SUCCESS;
727 }
728
Brian Carlstrom78128a62011-09-15 17:21:19 -0700729 Runtime::Options options;
730 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700731 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700732 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700733 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700734 if (boot_image_filename != NULL) {
735 boot_image_option += "-Ximage:";
736 boot_image_option += boot_image_filename;
737 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -0700738 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700739 if (image_filename != NULL) {
740 image_option += "-Ximage:";
741 image_option += image_filename;
742 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
743 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700744
745 if (!host_prefix.empty()) {
746 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
747 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700748
749 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
750 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700751 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700752 return EXIT_FAILURE;
753 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700754
Ian Rogers30fab402012-01-23 15:43:46 -0800755 ImageSpace* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]->AsImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700756 CHECK(image_space != NULL);
757 const ImageHeader& image_header = image_space->GetImageHeader();
758 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700759 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700760 return EXIT_FAILURE;
761 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700762 ImageDump::Dump(image_filename, host_prefix, *os, *image_space, image_header);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700763 return EXIT_SUCCESS;
764}
765
766} // namespace art
767
768int main(int argc, char** argv) {
769 return art::oatdump(argc, argv);
770}