blob: 0dde41fc09b78f9ac08496eed75e18f3defe5cc8 [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 Carlstromfe487d02012-02-29 18:49:16 -080060 " Default: $ANDROID_PRODUCT_OUT\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070061 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070062 fprintf(stderr,
63 " --output=<file> may be used to send the output to a file.\n"
64 " Example: --output=/tmp/oatdump.txt\n"
65 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070066 exit(EXIT_FAILURE);
67}
68
Ian Rogersff1ed472011-09-20 13:46:24 -070069const char* image_roots_descriptions_[] = {
Brian Carlstrom78128a62011-09-15 17:21:19 -070070 "kJniStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070071 "kAbstractMethodErrorStubArray",
Ian Rogersad25ac52011-10-04 19:13:33 -070072 "kInstanceResolutionStubArray",
73 "kStaticResolutionStubArray",
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070074 "kUnknownMethodResolutionStubArray",
Ian Rogers19846512012-02-24 11:42:47 -080075 "kResolutionMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070076 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070077 "kRefsOnlySaveMethod",
78 "kRefsAndArgsSaveMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070079 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070080 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070081 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070082};
83
Elliott Hughese3c845c2012-02-28 17:23:01 -080084class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -070085 public:
Elliott Hughese3c845c2012-02-28 17:23:01 -080086 void Dump(const std::string& oat_filename, std::ostream& os, const OatFile& oat_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -070087 const OatHeader& oat_header = oat_file.GetOatHeader();
88
89 os << "MAGIC:\n";
90 os << oat_header.GetMagic() << "\n\n";
91
92 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -080093 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -070094
95 os << "DEX FILE COUNT:\n";
96 os << oat_header.GetDexFileCount() << "\n\n";
97
98 os << "EXECUTABLE OFFSET:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -080099 os << StringPrintf("0x%08x\n\n", oat_header.GetExecutableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700100
Ian Rogers30fab402012-01-23 15:43:46 -0800101 os << "BEGIN:\n";
102 os << reinterpret_cast<const void*>(oat_file.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700103
Ian Rogers30fab402012-01-23 15:43:46 -0800104 os << "END:\n";
105 os << reinterpret_cast<const void*>(oat_file.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700106
107 os << std::flush;
108
Elliott Hughesba8eee12012-01-24 20:25:24 -0800109 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file.GetOatDexFiles();
Elliott Hughese3c845c2012-02-28 17:23:01 -0800110 AddAllOffsets(oat_file, oat_dex_files);
111
Brian Carlstromaded5f72011-10-07 17:15:04 -0700112 for (size_t i = 0; i < oat_dex_files.size(); i++) {
113 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
114 CHECK(oat_dex_file != NULL);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800115 DumpOatDexFile(os, oat_file, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700116 }
117 }
118
119 private:
Elliott Hughese3c845c2012-02-28 17:23:01 -0800120 void AddAllOffsets(const OatFile& oat_file, std::vector<const OatFile::OatDexFile*>& oat_dex_files) {
121 // We don't know the length of the code for each method, but we need to know where to stop
122 // when disassembling. What we do know is that a region of code will be followed by some other
123 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
124 // of a piece of code by using upper_bound to find the start of the next region.
125 for (size_t i = 0; i < oat_dex_files.size(); i++) {
126 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
127 CHECK(oat_dex_file != NULL);
128 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
129 if (dex_file.get() == NULL) {
130 return;
131 }
132 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
133 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
134 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index));
135 const byte* class_data = dex_file->GetClassData(class_def);
136 if (class_data != NULL) {
137 ClassDataItemIterator it(*dex_file, class_data);
138 SkipAllFields(it);
139 uint32_t class_method_index = 0;
140 while (it.HasNextDirectMethod()) {
141 AddOffsets(oat_class->GetOatMethod(class_method_index++));
142 it.Next();
143 }
144 while (it.HasNextVirtualMethod()) {
145 AddOffsets(oat_class->GetOatMethod(class_method_index++));
146 it.Next();
147 }
148 }
149 }
150 }
151
152 // If the last thing in the file is code for a method, there won't be an offset for the "next"
153 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
154 // for the end of the file.
155 offsets_.insert(static_cast<uint32_t>(oat_file.End() - oat_file.Begin()));
156 }
157
158 void AddOffsets(const OatFile::OatMethod& oat_method) {
159 offsets_.insert(oat_method.GetCodeOffset());
160 offsets_.insert(oat_method.GetMappingTableOffset());
161 offsets_.insert(oat_method.GetVmapTableOffset());
162 offsets_.insert(oat_method.GetGcMapOffset());
163 offsets_.insert(oat_method.GetInvokeStubOffset());
164 }
165
166 void DumpOatDexFile(std::ostream& os, const OatFile& oat_file,
167 const OatFile::OatDexFile& oat_dex_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700168 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800169 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800170 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800171 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile());
172 if (dex_file.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700173 os << "NOT FOUND\n\n";
174 return;
175 }
176 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
177 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
178 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700179 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
180 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800181 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800182 << oat_class->GetStatus() << ")\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800183 DumpOatClass(os, oat_file, *oat_class.get(), *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700184 }
185
186 os << std::flush;
187 }
188
Elliott Hughese3c845c2012-02-28 17:23:01 -0800189 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700190 while (it.HasNextStaticField()) {
191 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700192 }
Ian Rogers0571d352011-11-03 19:51:38 -0700193 while (it.HasNextInstanceField()) {
194 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700195 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800196 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700197
Elliott Hughese3c845c2012-02-28 17:23:01 -0800198 void DumpOatClass(std::ostream& os, const OatFile& oat_file, const OatFile::OatClass& oat_class,
199 const DexFile& dex_file, const DexFile::ClassDef& class_def) {
200 const byte* class_data = dex_file.GetClassData(class_def);
201 if (class_data == NULL) { // empty class such as a marker interface?
202 return;
203 }
204 ClassDataItemIterator it(dex_file, class_data);
205 SkipAllFields(it);
206
207 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700208 while (it.HasNextDirectMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800209 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
210 DumpOatMethod(os, class_method_index, oat_file, oat_method, dex_file,
211 it.GetMemberIndex(), it.GetMethodCodeItem());
212 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700213 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700214 }
Ian Rogers0571d352011-11-03 19:51:38 -0700215 while (it.HasNextVirtualMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800216 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
217 DumpOatMethod(os, class_method_index, oat_file, oat_method, dex_file,
218 it.GetMemberIndex(), it.GetMethodCodeItem());
219 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700220 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700221 }
Ian Rogers0571d352011-11-03 19:51:38 -0700222 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700223 os << std::flush;
224 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800225
226 void DumpOatMethod(std::ostream& os, uint32_t class_method_index, const OatFile& oat_file,
227 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
228 uint32_t dex_method_idx, const DexFile::CodeItem* code_item) {
229 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700230 const char* name = dex_file.GetMethodName(method_id);
Elliott Hughes95572412011-12-13 18:14:20 -0800231 std::string signature(dex_file.GetMethodSignature(method_id));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800232 os << StringPrintf("\t%d: %s %s (dex_method_idx=%d)\n",
233 class_method_index, name, signature.c_str(), dex_method_idx);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800234 os << StringPrintf("\t\tcode: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800235 oat_method.GetCode(), oat_method.GetCodeOffset());
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800236 os << StringPrintf("\t\tframe_size_in_bytes: %zd\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800237 oat_method.GetFrameSizeInBytes());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800238 os << StringPrintf("\t\tcore_spill_mask: 0x%08x\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800239 oat_method.GetCoreSpillMask());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800240 os << StringPrintf("\t\tfp_spill_mask: 0x%08x\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800241 oat_method.GetFpSpillMask());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800242 os << StringPrintf("\t\tmapping_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800243 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800244 DumpMappingTable(os, oat_file, oat_method, dex_file, code_item);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800245 os << StringPrintf("\t\tvmap_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800246 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800247 os << StringPrintf("\t\tgc_map: %p (offset=0x%08x)\n",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800248 oat_method.GetGcMap(), oat_method.GetGcMapOffset());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800249 os << StringPrintf("\t\tinvoke_stub: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800250 oat_method.GetInvokeStub(), oat_method.GetInvokeStubOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700251 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800252
253 void DumpMappingTable(std::ostream& os,
254 const OatFile& oat_file, const OatFile::OatMethod& oat_method,
255 const DexFile& dex_file, const DexFile::CodeItem* code_item) {
256 const uint32_t* raw_table = oat_method.GetMappingTable();
257 const void* code = oat_method.GetCode();
258 if (raw_table == NULL || code == NULL) {
259 return;
260 }
261
262 uint32_t length = *raw_table;
263 ++raw_table;
264
265 for (size_t i = 0; i < length; i += 2) {
266 uint32_t dex_pc = raw_table[i + 1];
267 const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]);
268 os << StringPrintf("\t\t0x%04x: %s\n", dex_pc, instruction->DumpString(&dex_file).c_str());
269
Elliott Hughese3c845c2012-02-28 17:23:01 -0800270 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;
Elliott Hughesed2adb62012-02-29 14:41:01 -0800279 It it = offsets_.upper_bound(last_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800280 CHECK(it != offsets_.end());
281 end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it;
282 }
283
284 // TODO: insert disassembler here.
Elliott Hughesed2adb62012-02-29 14:41:01 -0800285 CHECK(native_pc < end_native_pc);
286 os << StringPrintf("\t\t\t%p:", native_pc);
287 for (; native_pc < end_native_pc; ++native_pc) {
288 os << StringPrintf(" 0x%02x", *native_pc);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800289 }
Elliott Hughesed2adb62012-02-29 14:41:01 -0800290 os << "\n";
Elliott Hughese3c845c2012-02-28 17:23:01 -0800291 }
292 }
293
294 std::set<uint32_t> offsets_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700295};
296
297class ImageDump {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700298 public:
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700299 static void Dump(const std::string& image_filename,
Brian Carlstromaded5f72011-10-07 17:15:04 -0700300 const std::string& host_prefix,
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700301 std::ostream& os,
302 Space& image_space,
303 const ImageHeader& image_header) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700304 os << "MAGIC:\n";
305 os << image_header.GetMagic() << "\n\n";
306
Ian Rogers30fab402012-01-23 15:43:46 -0800307 os << "IMAGE BEGIN:\n";
308 os << reinterpret_cast<void*>(image_header.GetImageBegin()) << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700309
Brian Carlstromaded5f72011-10-07 17:15:04 -0700310 os << "OAT CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800311 os << StringPrintf("0x%08x\n\n", image_header.GetOatChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700312
Ian Rogers30fab402012-01-23 15:43:46 -0800313 os << "OAT BEGIN:\n";
314 os << reinterpret_cast<void*>(image_header.GetOatBegin()) << "\n\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700315
Ian Rogers30fab402012-01-23 15:43:46 -0800316 os << "OAT END:\n";
317 os << reinterpret_cast<void*>(image_header.GetOatEnd()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700318
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700319 os << "ROOTS:\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700320 os << reinterpret_cast<void*>(image_header.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700321 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700322 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
323 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700324 const char* image_root_description = image_roots_descriptions_[i];
325 Object* image_root_object = image_header.GetImageRoot(image_root);
326 os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
327 if (image_root_object->IsObjectArray()) {
328 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
329 ObjectArray<Object>* image_root_object_array
330 = down_cast<ObjectArray<Object>*>(image_root_object);
331 // = image_root_object->AsObjectArray<Object>();
332 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800333 Object* value = image_root_object_array->Get(i);
334 if (value != NULL) {
335 os << "\t" << i << ": ";
336 std::string summary;
337 PrettyObjectValue(summary, value->GetClass(), value);
338 os << summary;
339 } else {
340 os << StringPrintf("\t%d: null\n", i);
341 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700342 }
343 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700344 }
345 os << "\n";
346
347 os << "OBJECTS:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700348 ImageDump state(image_space, os);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700349 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
350 DCHECK(heap_bitmap != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700351 heap_bitmap->Walk(ImageDump::Callback, &state);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700352 os << "\n";
353
354 os << "STATS:\n" << std::flush;
355 UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), false));
356 state.stats_.file_bytes = file->Length();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700357 size_t header_bytes = sizeof(ImageHeader);
358 state.stats_.header_bytes = header_bytes;
359 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
360 state.stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700361 state.stats_.Dump(os);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700362 os << "\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700363
364 os << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700365
366 os << "OAT LOCATION:\n" << std::flush;
367 Object* oat_location_object = image_header.GetImageRoot(ImageHeader::kOatLocation);
Elliott Hughes95572412011-12-13 18:14:20 -0800368 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700369 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
370 os << oat_location;
371 if (!host_prefix.empty()) {
372 oat_location = host_prefix + oat_location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700373 os << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700374 }
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700375 os << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800376 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700377 if (oat_file == NULL) {
378 os << "NOT FOUND\n";
379 os << std::flush;
380 return;
381 }
382 os << "\n";
383 os << std::flush;
384
Elliott Hughese3c845c2012-02-28 17:23:01 -0800385 OatDumper oat_dumper;
386 oat_dumper.Dump(oat_location, os, *oat_file);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700387 }
388
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700389 private:
390
Brian Carlstromaded5f72011-10-07 17:15:04 -0700391 ImageDump(const Space& dump_space, std::ostream& os) : dump_space_(dump_space), os_(os) {}
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700392
Brian Carlstromaded5f72011-10-07 17:15:04 -0700393 ~ImageDump() {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700394
Ian Rogersd5b32602012-02-26 16:40:04 -0800395 static void PrettyObjectValue(std::string& summary, Class* type, Object* value) {
396 CHECK(type != NULL);
397 if (value == NULL) {
398 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
399 } else if (type->IsStringClass()) {
400 String* string = value->AsString();
401 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
402 } else if (value->IsClass()) {
403 Class* klass = value->AsClass();
404 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
405 } else if (value->IsField()) {
406 Field* field = value->AsField();
407 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
408 } else if (value->IsMethod()) {
409 Method* method = value->AsMethod();
410 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
411 } else {
412 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
413 }
414 }
415
416 static void PrintField(std::string& summary, Field* field, Object* obj) {
417 FieldHelper fh(field);
418 Class* type = fh.GetType();
419 StringAppendF(&summary, "\t%s: ", fh.GetName());
420 if (type->IsPrimitiveLong()) {
421 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
422 } else if (type->IsPrimitiveDouble()) {
423 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
424 } else if (type->IsPrimitiveFloat()) {
425 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
426 } else if (type->IsPrimitive()){
427 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
428 } else {
429 Object* value = field->GetObj(obj);
430 PrettyObjectValue(summary, type, value);
431 }
432 }
433
434 static void DumpFields(std::string& summary, Object* obj, Class* klass) {
435 Class* super = klass->GetSuperClass();
436 if (super != NULL) {
437 DumpFields(summary, obj, super);
438 }
439 ObjectArray<Field>* fields = klass->GetIFields();
440 if (fields != NULL) {
441 for (int32_t i = 0; i < fields->GetLength(); i++) {
442 Field* field = fields->Get(i);
443 PrintField(summary, field, obj);
444 }
445 }
446 }
447
Brian Carlstrom78128a62011-09-15 17:21:19 -0700448 static void Callback(Object* obj, void* arg) {
449 DCHECK(obj != NULL);
450 DCHECK(arg != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700451 ImageDump* state = reinterpret_cast<ImageDump*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700452 if (!state->InDumpSpace(obj)) {
453 return;
454 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700455
456 size_t object_bytes = obj->SizeOf();
457 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
458 state->stats_.object_bytes += object_bytes;
459 state->stats_.alignment_bytes += alignment_bytes;
460
Brian Carlstrom78128a62011-09-15 17:21:19 -0700461 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800462 Class* obj_class = obj->GetClass();
463 if (obj_class->IsArrayClass()) {
464 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
465 obj->AsArray()->GetLength());
466 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700467 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800468 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
469 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700470 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800471 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700472 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800473 } else if (obj->IsField()) {
474 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
475 PrettyField(obj->AsField()).c_str());
476 } else if (obj->IsMethod()) {
477 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
478 PrettyMethod(obj->AsMethod()).c_str());
479 } else if (obj_class->IsStringClass()) {
480 StringAppendF(&summary, "%p: java.lang.String \"%s\"\n", obj,
481 obj->AsString()->ToModifiedUtf8().c_str());
482 } else {
483 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
484 }
485 DumpFields(summary, obj, obj_class);
486 if (obj->IsObjectArray()) {
487 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
488 int32_t length = obj_array->GetLength();
489 for (int32_t i = 0; i < length; i++) {
490 Object* value = obj_array->Get(i);
491 size_t run = 0;
492 for (int32_t j = i + 1; j < length; j++) {
493 if (value == obj_array->Get(j)) {
494 run++;
495 } else {
496 break;
497 }
498 }
499 if (run == 0) {
500 StringAppendF(&summary, "\t%d: ", i);
501 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800502 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800503 i = i + run;
504 }
505 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
506 PrettyObjectValue(summary, value_class, value);
507 }
508 } else if (obj->IsClass()) {
509 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
510 if (sfields != NULL) {
511 summary += "\t\tSTATICS:\n";
512 for (int32_t i = 0; i < sfields->GetLength(); i++) {
513 Field* field = sfields->Get(i);
514 PrintField(summary, field, NULL);
515 }
516 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700517 } else if (obj->IsMethod()) {
518 Method* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700519 if (method->IsNative()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700520 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800521 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700522 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers19846512012-02-24 11:42:47 -0800523 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
524 method->IsResolutionMethod()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700525 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800526 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700527 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700528 } else {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800529 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
530 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700531
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800532 size_t register_map_bytes = method->GetGcMapLength();
533 state->stats_.register_map_bytes += register_map_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800534
535 size_t pc_mapping_table_bytes = method->GetMappingTableLength();
536 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700537
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800538 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700539 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700540 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800541
Ian Rogersd5b32602012-02-26 16:40:04 -0800542 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800543 dex_instruction_bytes, register_map_bytes, pc_mapping_table_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700544 }
545 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800546 std::string descriptor(ClassHelper(obj_class).GetDescriptor());
547 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
548 state->stats_.descriptor_to_count[descriptor] += 1;
549
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700550 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700551 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700552
553 bool InDumpSpace(const Object* object) {
Ian Rogers30fab402012-01-23 15:43:46 -0800554 return dump_space_.Contains(object);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700555 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700556
557 public:
558 struct Stats {
559 size_t file_bytes;
560
561 size_t header_bytes;
562 size_t object_bytes;
563 size_t alignment_bytes;
564
565 size_t managed_code_bytes;
566 size_t managed_to_native_code_bytes;
567 size_t native_to_managed_code_bytes;
568
569 size_t register_map_bytes;
570 size_t pc_mapping_table_bytes;
571
572 size_t dex_instruction_bytes;
573
574 Stats()
575 : file_bytes(0),
576 header_bytes(0),
577 object_bytes(0),
578 alignment_bytes(0),
579 managed_code_bytes(0),
580 managed_to_native_code_bytes(0),
581 native_to_managed_code_bytes(0),
582 register_map_bytes(0),
583 pc_mapping_table_bytes(0),
584 dex_instruction_bytes(0) {}
585
Elliott Hughese5448b52012-01-18 16:44:06 -0800586 typedef std::map<std::string, size_t> TableBytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700587 TableBytes descriptor_to_bytes;
588
Elliott Hughese5448b52012-01-18 16:44:06 -0800589 typedef std::map<std::string, size_t> TableCount;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700590 TableCount descriptor_to_count;
591
592 double PercentOfFileBytes(size_t size) {
593 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
594 }
595
596 double PercentOfObjectBytes(size_t size) {
597 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
598 }
599
600 void Dump(std::ostream& os) {
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800601 os << StringPrintf("\tfile_bytes = %zd\n", file_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700602 os << "\n";
603
604 os << "\tfile_bytes = header_bytes + object_bytes + alignment_bytes\n";
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800605 os << StringPrintf("\theader_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700606 header_bytes, PercentOfFileBytes(header_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800607 os << StringPrintf("\tobject_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700608 object_bytes, PercentOfFileBytes(object_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800609 os << StringPrintf("\talignment_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700610 alignment_bytes, PercentOfFileBytes(alignment_bytes));
611 os << "\n";
612 os << std::flush;
613 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
614
615 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
616 size_t object_bytes_total = 0;
617 typedef TableBytes::const_iterator It; // TODO: C++0x auto
618 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -0800619 const std::string& descriptor(it->first);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700620 size_t bytes = it->second;
621 size_t count = descriptor_to_count[descriptor];
622 double average = static_cast<double>(bytes) / static_cast<double>(count);
623 double percent = PercentOfObjectBytes(bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800624 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700625 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
626 descriptor.c_str(), bytes, count,
627 average, percent);
628
629 object_bytes_total += bytes;
630 }
631 os << "\n";
632 os << std::flush;
633 CHECK_EQ(object_bytes, object_bytes_total);
634
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800635 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700636 managed_code_bytes, PercentOfObjectBytes(managed_code_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800637 os << StringPrintf("\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700638 managed_to_native_code_bytes,
639 PercentOfObjectBytes(managed_to_native_code_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800640 os << StringPrintf("\tnative_to_managed_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700641 native_to_managed_code_bytes,
642 PercentOfObjectBytes(native_to_managed_code_bytes));
643 os << "\n";
644 os << std::flush;
645
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800646 os << StringPrintf("\tregister_map_bytes = %7zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700647 register_map_bytes, PercentOfObjectBytes(register_map_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800648 os << StringPrintf("\tpc_mapping_table_bytes = %7zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700649 pc_mapping_table_bytes, PercentOfObjectBytes(pc_mapping_table_bytes));
650 os << "\n";
651 os << std::flush;
652
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800653 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700654 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f\n",
655 static_cast<double>(managed_code_bytes)
656 / static_cast<double>(dex_instruction_bytes));
657 os << "\n";
658 os << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700659 }
660 } stats_;
661
662 private:
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700663 const Space& dump_space_;
664 std::ostream& os_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700665
Brian Carlstromaded5f72011-10-07 17:15:04 -0700666 DISALLOW_COPY_AND_ASSIGN(ImageDump);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700667};
668
669int oatdump(int argc, char** argv) {
670 // Skip over argv[0].
671 argv++;
672 argc--;
673
674 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700675 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700676 usage();
677 }
678
Brian Carlstromaded5f72011-10-07 17:15:04 -0700679 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700680 const char* image_filename = NULL;
681 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700682 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700683 std::ostream* os = &std::cout;
684 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700685
686 for (int i = 0; i < argc; i++) {
687 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800688 if (option.starts_with("--oat-file=")) {
689 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700690 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700691 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700692 } else if (option.starts_with("--boot-image=")) {
693 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700694 } else if (option.starts_with("--host-prefix=")) {
695 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700696 } else if (option.starts_with("--output=")) {
697 const char* filename = option.substr(strlen("--output=")).data();
698 out.reset(new std::ofstream(filename));
699 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700700 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700701 usage();
702 }
703 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700704 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700705 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700706 usage();
707 }
708 }
709
Brian Carlstromaded5f72011-10-07 17:15:04 -0700710 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700711 fprintf(stderr, "Either --image or --oat must be specified\n");
712 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700713 }
714
Brian Carlstromaded5f72011-10-07 17:15:04 -0700715 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700716 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
717 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700718 }
719
720 if (oat_filename != NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800721 const OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700722 if (oat_file == NULL) {
723 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
724 return EXIT_FAILURE;
725 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800726 OatDumper oat_dumper;
727 oat_dumper.Dump(oat_filename, *os, *oat_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700728 return EXIT_SUCCESS;
729 }
730
Brian Carlstrom78128a62011-09-15 17:21:19 -0700731 Runtime::Options options;
732 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700733 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700734 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700735 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700736 if (boot_image_filename != NULL) {
737 boot_image_option += "-Ximage:";
738 boot_image_option += boot_image_filename;
739 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -0700740 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700741 if (image_filename != NULL) {
742 image_option += "-Ximage:";
743 image_option += image_filename;
744 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
745 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700746
Brian Carlstromfe487d02012-02-29 18:49:16 -0800747 if (host_prefix.empty()) {
748 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
749 if (android_product_out != NULL) {
750 host_prefix = android_product_out;
751 }
752 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700753 if (!host_prefix.empty()) {
754 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
755 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700756
757 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
758 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700759 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700760 return EXIT_FAILURE;
761 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700762
Ian Rogers30fab402012-01-23 15:43:46 -0800763 ImageSpace* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]->AsImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700764 CHECK(image_space != NULL);
765 const ImageHeader& image_header = image_space->GetImageHeader();
766 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700767 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700768 return EXIT_FAILURE;
769 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700770 ImageDump::Dump(image_filename, host_prefix, *os, *image_space, image_header);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700771 return EXIT_SUCCESS;
772}
773
774} // namespace art
775
776int main(int argc, char** argv) {
777 return art::oatdump(argc, argv);
778}