blob: f38f8782212cc81a2aa1af7ab74baf36f586b3ef [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Elliott Hughese5448b52012-01-18 16:44:06 -080022#include <map>
Brian Carlstrom78128a62011-09-15 17:21:19 -070023#include <string>
24#include <vector>
25
26#include "class_linker.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080027#include "context.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080028#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080029#include "dex_verifier.h"
30#include "disassembler.h"
Brian Carlstrom916e74e2011-09-23 11:42:01 -070031#include "file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070032#include "image.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080033#include "object_utils.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080034#include "os.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070035#include "runtime.h"
36#include "space.h"
37#include "stringpiece.h"
38
39namespace art {
40
41static void usage() {
42 fprintf(stderr,
43 "Usage: oatdump [options] ...\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080044 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n"
45 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070046 "\n");
47 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080048 " --oat-file=<file.oat>: specifies an input oat filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080049 " Example: --image=/system/framework/boot.oat\n"
Brian Carlstromaded5f72011-10-07 17:15:04 -070050 "\n");
51 fprintf(stderr,
52 " --image=<file.art>: specifies an input image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080053 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070054 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070055 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070056 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080057 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070058 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070059 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070060 " --host-prefix may be used to translate host paths to target paths during\n"
61 " cross compilation.\n"
62 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstromfe487d02012-02-29 18:49:16 -080063 " Default: $ANDROID_PRODUCT_OUT\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070064 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070065 fprintf(stderr,
66 " --output=<file> may be used to send the output to a file.\n"
67 " Example: --output=/tmp/oatdump.txt\n"
68 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070069 exit(EXIT_FAILURE);
70}
71
Ian Rogersff1ed472011-09-20 13:46:24 -070072const char* image_roots_descriptions_[] = {
Brian Carlstrom78128a62011-09-15 17:21:19 -070073 "kJniStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070074 "kAbstractMethodErrorStubArray",
Ian Rogersad25ac52011-10-04 19:13:33 -070075 "kStaticResolutionStubArray",
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070076 "kUnknownMethodResolutionStubArray",
Ian Rogers19846512012-02-24 11:42:47 -080077 "kResolutionMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070078 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070079 "kRefsOnlySaveMethod",
80 "kRefsAndArgsSaveMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070081 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070082 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070083 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070084};
85
Elliott Hughese3c845c2012-02-28 17:23:01 -080086class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -070087 public:
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070088 explicit OatDumper(const std::string& host_prefix, const OatFile& oat_file)
89 : host_prefix_(host_prefix),
90 oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -080091 oat_dex_files_(oat_file.GetOatDexFiles()),
92 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet())) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080093 AddAllOffsets();
94 }
95
96 void Dump(std::ostream& os) {
97 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -070098
99 os << "MAGIC:\n";
100 os << oat_header.GetMagic() << "\n\n";
101
102 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800103 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700104
Elliott Hughesa72ec822012-03-05 17:12:22 -0800105 os << "INSTRUCTION SET:\n";
106 os << oat_header.GetInstructionSet() << "\n\n";
107
Brian Carlstromaded5f72011-10-07 17:15:04 -0700108 os << "DEX FILE COUNT:\n";
109 os << oat_header.GetDexFileCount() << "\n\n";
110
111 os << "EXECUTABLE OFFSET:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800112 os << StringPrintf("0x%08x\n\n", oat_header.GetExecutableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700113
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700114 os << "IMAGE FILE LOCATION CHECKSUM:\n";
115 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationChecksum());
116
117 os << "IMAGE FILE LOCATION:\n";
118 const std::string image_file_location(oat_header.GetImageFileLocation());
119 os << image_file_location;
120 if (!image_file_location.empty() && !host_prefix_.empty()) {
121 os << " (" << host_prefix_ << image_file_location << ")";
122 }
123 os << "\n\n";
124
Ian Rogers30fab402012-01-23 15:43:46 -0800125 os << "BEGIN:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800126 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700127
Ian Rogers30fab402012-01-23 15:43:46 -0800128 os << "END:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800129 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700130
131 os << std::flush;
132
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800133 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
134 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Brian Carlstromaded5f72011-10-07 17:15:04 -0700135 CHECK(oat_dex_file != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800136 DumpOatDexFile(os, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700137 }
138 }
139
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800140 size_t ComputeSize(const void* oat_data) {
141 if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() ||
142 reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) {
143 return 0; // Address not in oat file
144 }
145 uint32_t begin_offset = reinterpret_cast<size_t>(oat_data) -
146 reinterpret_cast<size_t>(oat_file_.Begin());
147 typedef std::set<uint32_t>::iterator It;
148 It it = offsets_.upper_bound(begin_offset);
149 CHECK(it != offsets_.end());
150 uint32_t end_offset = *it;
151 return end_offset - begin_offset;
152 }
153
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700154 InstructionSet GetInstructionSet() {
155 return oat_file_.GetOatHeader().GetInstructionSet();
156 }
157
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800158 const void* GetOatCode(Method* m) {
159 MethodHelper mh(m);
160 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
161 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
162 CHECK(oat_dex_file != NULL);
163 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
164 if (dex_file.get() != NULL) {
165 uint32_t class_def_index;
166 bool found = dex_file->FindClassDefIndex(mh.GetDeclaringClassDescriptor(), class_def_index);
167 if (found) {
168 const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_index);
169 CHECK(oat_class != NULL);
170 size_t method_index = m->GetMethodIndex();
171 return oat_class->GetOatMethod(method_index).GetCode();
172 }
173 }
174 }
175 return NULL;
176 }
177
Brian Carlstromaded5f72011-10-07 17:15:04 -0700178 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800179 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800180 // We don't know the length of the code for each method, but we need to know where to stop
181 // when disassembling. What we do know is that a region of code will be followed by some other
182 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
183 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800184 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
185 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800186 CHECK(oat_dex_file != NULL);
187 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
188 if (dex_file.get() == NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800189 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800190 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800191 offsets_.insert(reinterpret_cast<uint32_t>(&dex_file->GetHeader()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800192 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
193 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
194 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index));
195 const byte* class_data = dex_file->GetClassData(class_def);
196 if (class_data != NULL) {
197 ClassDataItemIterator it(*dex_file, class_data);
198 SkipAllFields(it);
199 uint32_t class_method_index = 0;
200 while (it.HasNextDirectMethod()) {
201 AddOffsets(oat_class->GetOatMethod(class_method_index++));
202 it.Next();
203 }
204 while (it.HasNextVirtualMethod()) {
205 AddOffsets(oat_class->GetOatMethod(class_method_index++));
206 it.Next();
207 }
208 }
209 }
210 }
211
212 // If the last thing in the file is code for a method, there won't be an offset for the "next"
213 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
214 // for the end of the file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800215 offsets_.insert(static_cast<uint32_t>(oat_file_.End() - oat_file_.Begin()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800216 }
217
218 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800219 uint32_t code_offset = oat_method.GetCodeOffset();
220 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
221 code_offset &= ~0x1;
222 }
223 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800224 offsets_.insert(oat_method.GetMappingTableOffset());
225 offsets_.insert(oat_method.GetVmapTableOffset());
226 offsets_.insert(oat_method.GetGcMapOffset());
227 offsets_.insert(oat_method.GetInvokeStubOffset());
228 }
229
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800230 void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700231 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800232 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800233 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800234 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile());
235 if (dex_file.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700236 os << "NOT FOUND\n\n";
237 return;
238 }
239 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
240 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
241 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700242 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
243 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800244 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800245 << oat_class->GetStatus() << ")\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800246 DumpOatClass(os, *oat_class.get(), *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700247 }
248
249 os << std::flush;
250 }
251
Elliott Hughese3c845c2012-02-28 17:23:01 -0800252 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700253 while (it.HasNextStaticField()) {
254 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700255 }
Ian Rogers0571d352011-11-03 19:51:38 -0700256 while (it.HasNextInstanceField()) {
257 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700258 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800259 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700260
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800261 void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
262 const DexFile::ClassDef& class_def) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800263 const byte* class_data = dex_file.GetClassData(class_def);
264 if (class_data == NULL) { // empty class such as a marker interface?
265 return;
266 }
267 ClassDataItemIterator it(dex_file, class_data);
268 SkipAllFields(it);
269
270 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700271 while (it.HasNextDirectMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800272 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800273 DumpOatMethod(os, class_method_index, oat_method, dex_file,
274 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800275 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700276 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700277 }
Ian Rogers0571d352011-11-03 19:51:38 -0700278 while (it.HasNextVirtualMethod()) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800279 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800280 DumpOatMethod(os, class_method_index, oat_method, dex_file,
281 it.GetMemberIndex(), it.GetMethodCodeItem());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800282 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700283 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700284 }
Ian Rogers0571d352011-11-03 19:51:38 -0700285 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700286 os << std::flush;
287 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800288
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800289 void DumpOatMethod(std::ostream& os, uint32_t class_method_index,
Elliott Hughese3c845c2012-02-28 17:23:01 -0800290 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
291 uint32_t dex_method_idx, const DexFile::CodeItem* code_item) {
292 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700293 const char* name = dex_file.GetMethodName(method_id);
Elliott Hughes95572412011-12-13 18:14:20 -0800294 std::string signature(dex_file.GetMethodSignature(method_id));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800295 os << StringPrintf("\t%d: %s %s (dex_method_idx=%d)\n",
296 class_method_index, name, signature.c_str(), dex_method_idx);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800297 os << StringPrintf("\t\tframe_size_in_bytes: %zd\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800298 oat_method.GetFrameSizeInBytes());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800299 os << StringPrintf("\t\tcore_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800300 oat_method.GetCoreSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800301 DumpSpillMask(os, oat_method.GetCoreSpillMask(), false);
302 os << StringPrintf("\n\t\tfp_spill_mask: 0x%08x",
Brian Carlstromae826982011-11-09 01:33:42 -0800303 oat_method.GetFpSpillMask());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800304 DumpSpillMask(os, oat_method.GetFpSpillMask(), true);
305 os << StringPrintf("\n\t\tmapping_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800306 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800307 DumpMappingTable(os, oat_method);
Elliott Hughesed2adb62012-02-29 14:41:01 -0800308 os << StringPrintf("\t\tvmap_table: %p (offset=0x%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800309 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800310 DumpVmap(os, oat_method.GetVmapTable(), oat_method.GetCoreSpillMask(),
311 oat_method.GetFpSpillMask());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800312 os << StringPrintf("\t\tgc_map: %p (offset=0x%08x)\n",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800313 oat_method.GetGcMap(), oat_method.GetGcMapOffset());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800314 DumpGcMap(os, oat_method.GetGcMap());
Elliott Hughes77405792012-03-15 15:22:12 -0700315 os << StringPrintf("\t\tCODE: %p (offset=0x%08x size=%d)%s\n",
316 oat_method.GetCode(),
317 oat_method.GetCodeOffset(),
318 oat_method.GetCodeSize(),
319 oat_method.GetCode() != NULL ? "..." : "");
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800320 DumpCode(os, oat_method.GetCode(), oat_method.GetMappingTable(), dex_file, code_item);
Elliott Hughes77405792012-03-15 15:22:12 -0700321 os << StringPrintf("\t\tINVOKE STUB: %p (offset=0x%08x size=%d)%s\n",
322 oat_method.GetInvokeStub(),
323 oat_method.GetInvokeStubOffset(),
324 oat_method.GetInvokeStubSize(),
325 oat_method.GetInvokeStub() != NULL ? "..." : "");
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800326 DumpCode(os, reinterpret_cast<const void*>(oat_method.GetInvokeStub()), NULL, dex_file, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700327 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800328
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800329 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
330 if (spill_mask == 0) {
331 return;
332 }
333 os << " (";
334 for (size_t i = 0; i < 32; i++) {
335 if ((spill_mask & (1 << i)) != 0) {
336 if (is_float) {
337 os << "fr" << i;
338 } else {
339 os << "r" << i;
340 }
341 spill_mask ^= 1 << i; // clear bit
342 if (spill_mask != 0) {
343 os << ", ";
344 } else {
345 break;
346 }
347 }
348 }
349 os << ")";
350 }
351
352 void DumpVmap(std::ostream& os, const uint16_t* raw_table, uint32_t core_spill_mask,
353 uint32_t fp_spill_mask) {
354 if (raw_table == NULL) {
355 return;
356 }
357 const VmapTable vmap_table(raw_table);
358 bool first = true;
359 os << "\t\t\t";
360 for (size_t i = 0; i < vmap_table.size(); i++) {
361 uint16_t dex_reg = vmap_table[i];
362 size_t matches = 0;
363 size_t spill_shifts = 0;
364 uint32_t spill_mask = core_spill_mask;
365 bool processing_fp = false;
366 while (matches != (i + 1)) {
367 if (spill_mask == 0) {
368 CHECK(!processing_fp);
369 spill_mask = fp_spill_mask;
370 processing_fp = true;
371 }
372 matches += spill_mask & 1; // Add 1 if the low bit is set
373 spill_mask >>= 1;
374 spill_shifts++;
375 }
376 size_t arm_reg = spill_shifts - 1; // wind back one as we want the last match
377 os << (first ? "v" : ", v") << dex_reg;
378 if (arm_reg < 16) {
379 os << "/r" << arm_reg;
380 } else {
381 os << "/fr" << (arm_reg - 16);
382 }
383 if (first) {
384 first = false;
385 }
386 }
387 os << std::endl;
388 }
389
390 void DumpGcMap(std::ostream& os, const uint8_t* gc_map_raw) {
391 if (gc_map_raw == NULL) {
392 return;
393 }
394 uint32_t gc_map_length = (gc_map_raw[0] << 24) | (gc_map_raw[1] << 16) |
395 (gc_map_raw[2] << 8) | (gc_map_raw[3] << 0);
396 verifier::PcToReferenceMap map(gc_map_raw + sizeof(uint32_t), gc_map_length);
397 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
398 os << StringPrintf("\t\t\t0x%04x", map.GetPC(entry));
399 size_t num_regs = map.RegWidth() * 8;
400 const uint8_t* reg_bitmap = map.GetBitMap(entry);
401 bool first = true;
402 for (size_t reg = 0; reg < num_regs; reg++) {
403 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
404 if (first) {
405 os << " v" << reg;
406 first = false;
407 } else {
408 os << ", v" << reg;
409 }
410 }
411 }
412 os << std::endl;
413 }
414 }
415
416 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800417 const uint32_t* raw_table = oat_method.GetMappingTable();
418 const void* code = oat_method.GetCode();
419 if (raw_table == NULL || code == NULL) {
420 return;
421 }
422
423 uint32_t length = *raw_table;
424 ++raw_table;
425
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800426 os << "\t\t{";
Elliott Hughese3c845c2012-02-28 17:23:01 -0800427 for (size_t i = 0; i < length; i += 2) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800428 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800429 uint32_t dex_pc = raw_table[i + 1];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800430 os << StringPrintf("%p -> 0x%04x", native_pc, dex_pc);
431 if (i + 2 < length) {
432 os << ", ";
433 }
434 }
435 os << "}" << std::endl << std::flush;
436 }
437
438 void DumpCode(std::ostream& os, const void* code, const uint32_t* raw_mapping_table,
439 const DexFile& dex_file, const DexFile::CodeItem* code_item) {
440 if (code == NULL) {
441 return;
442 }
443
444 if (raw_mapping_table == NULL) {
445 // code but no mapping table is most likely caused by code created by the JNI compiler
446 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code);
447 const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file_.Begin());
448 uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin);
449
450 typedef std::set<uint32_t>::iterator It;
451 It it = offsets_.upper_bound(last_offset);
452 CHECK(it != offsets_.end());
453 const uint8_t* end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it;
454 CHECK(native_pc < end_native_pc);
455
456 disassembler_->Dump(os, native_pc, end_native_pc);
457 return;
458 }
459
460 uint32_t length = *raw_mapping_table;
461 ++raw_mapping_table;
462
463 for (size_t i = 0; i < length; i += 2) {
464 uint32_t dex_pc = raw_mapping_table[i + 1];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800465 const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]);
466 os << StringPrintf("\t\t0x%04x: %s\n", dex_pc, instruction->DumpString(&dex_file).c_str());
467
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800468 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800469 const uint8_t* end_native_pc = NULL;
470 if (i + 2 < length) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800471 end_native_pc = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i + 2];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800472 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800473 const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file_.Begin());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800474 uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin);
475
476 typedef std::set<uint32_t>::iterator It;
Elliott Hughesed2adb62012-02-29 14:41:01 -0800477 It it = offsets_.upper_bound(last_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800478 CHECK(it != offsets_.end());
479 end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it;
480 }
Elliott Hughesed2adb62012-02-29 14:41:01 -0800481 CHECK(native_pc < end_native_pc);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800482 disassembler_->Dump(os, native_pc, end_native_pc);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800483 }
484 }
485
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700486 const std::string host_prefix_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800487 const OatFile& oat_file_;
488 std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800489 std::set<uint32_t> offsets_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800490 UniquePtr<Disassembler> disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700491};
492
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800493class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700494 public:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800495 explicit ImageDumper(std::ostream& os, const std::string& image_filename,
496 const std::string& host_prefix, Space& image_space,
497 const ImageHeader& image_header) : os_(os),
498 image_filename_(image_filename), host_prefix_(host_prefix),
499 image_space_(image_space), image_header_(image_header) {
500 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700501
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800502 void Dump() {
503 os_ << "MAGIC:\n";
504 os_ << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700505
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800506 os_ << "IMAGE BEGIN:\n";
507 os_ << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700508
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800509 os_ << "OAT CHECKSUM:\n";
510 os_ << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700511
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800512 os_ << "OAT BEGIN:\n";
513 os_ << reinterpret_cast<void*>(image_header_.GetOatBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700514
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800515 os_ << "OAT END:\n";
516 os_ << reinterpret_cast<void*>(image_header_.GetOatEnd()) << "\n\n";
517
518 os_ << "ROOTS:\n";
519 os_ << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700520 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700521 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
522 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700523 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800524 Object* image_root_object = image_header_.GetImageRoot(image_root);
525 os_ << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700526 if (image_root_object->IsObjectArray()) {
527 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
528 ObjectArray<Object>* image_root_object_array
529 = down_cast<ObjectArray<Object>*>(image_root_object);
530 // = image_root_object->AsObjectArray<Object>();
531 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800532 Object* value = image_root_object_array->Get(i);
533 if (value != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800534 os_ << "\t" << i << ": ";
Ian Rogersd5b32602012-02-26 16:40:04 -0800535 std::string summary;
536 PrettyObjectValue(summary, value->GetClass(), value);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800537 os_ << summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800538 } else {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800539 os_ << StringPrintf("\t%d: null\n", i);
Ian Rogersd5b32602012-02-26 16:40:04 -0800540 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700541 }
542 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700543 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800544 os_ << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700545
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800546 os_ << "OAT LOCATION:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700547 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800548 Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation);
549 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800550 os_ << oat_location;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800551 if (!host_prefix_.empty()) {
552 oat_location = host_prefix_ + oat_location;
553 os_ << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700554 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800555 os_ << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800556 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700557 if (oat_file == NULL) {
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800558 os_ << "NOT FOUND\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700559 return;
560 }
Brian Carlstrom4dcbffb2012-03-07 00:21:18 -0800561 os_ << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700562
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800563 stats_.oat_file_bytes = oat_file->Size();
564
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700565 oat_dumper_.reset(new OatDumper(host_prefix_, *oat_file));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800566
567 os_ << "OBJECTS:\n" << std::flush;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800568 HeapBitmap* heap_bitmap = Runtime::Current()->GetHeap()->GetLiveBits();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800569 DCHECK(heap_bitmap != NULL);
570 heap_bitmap->Walk(ImageDumper::Callback, this);
571 os_ << "\n";
572
573 os_ << "STATS:\n" << std::flush;
574 UniquePtr<File> file(OS::OpenFile(image_filename_.c_str(), false));
575 stats_.file_bytes = file->Length();
576 size_t header_bytes = sizeof(ImageHeader);
577 stats_.header_bytes = header_bytes;
578 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
579 stats_.alignment_bytes += alignment_bytes;
580 stats_.Dump(os_);
581 os_ << "\n";
582
583 os_ << std::flush;
584
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800585 oat_dumper_->Dump(os_);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700586 }
587
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700588 private:
589
Ian Rogersd5b32602012-02-26 16:40:04 -0800590 static void PrettyObjectValue(std::string& summary, Class* type, Object* value) {
591 CHECK(type != NULL);
592 if (value == NULL) {
593 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
594 } else if (type->IsStringClass()) {
595 String* string = value->AsString();
596 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
597 } else if (value->IsClass()) {
598 Class* klass = value->AsClass();
599 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
600 } else if (value->IsField()) {
601 Field* field = value->AsField();
602 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
603 } else if (value->IsMethod()) {
604 Method* method = value->AsMethod();
605 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
606 } else {
607 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
608 }
609 }
610
611 static void PrintField(std::string& summary, Field* field, Object* obj) {
612 FieldHelper fh(field);
613 Class* type = fh.GetType();
614 StringAppendF(&summary, "\t%s: ", fh.GetName());
615 if (type->IsPrimitiveLong()) {
616 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
617 } else if (type->IsPrimitiveDouble()) {
618 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
619 } else if (type->IsPrimitiveFloat()) {
620 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
621 } else if (type->IsPrimitive()){
622 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
623 } else {
624 Object* value = field->GetObj(obj);
625 PrettyObjectValue(summary, type, value);
626 }
627 }
628
629 static void DumpFields(std::string& summary, Object* obj, Class* klass) {
630 Class* super = klass->GetSuperClass();
631 if (super != NULL) {
632 DumpFields(summary, obj, super);
633 }
634 ObjectArray<Field>* fields = klass->GetIFields();
635 if (fields != NULL) {
636 for (int32_t i = 0; i < fields->GetLength(); i++) {
637 Field* field = fields->Get(i);
638 PrintField(summary, field, obj);
639 }
640 }
641 }
642
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800643 bool InDumpSpace(const Object* object) {
644 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700645 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800646
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700647 const void* GetOatCodeBegin(Method* m) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800648 Runtime* runtime = Runtime::Current();
649 const void* code = m->GetCode();
Ian Rogersfb6adba2012-03-04 21:51:51 -0800650 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800651 code = oat_dumper_->GetOatCode(m);
652 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700653 if (oat_dumper_->GetInstructionSet() == kThumb2) {
654 code = reinterpret_cast<void*>(reinterpret_cast<uint32_t>(code) & ~0x1);
655 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800656 return code;
657 }
658
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700659 uint32_t GetOatCodeSize(Method* m) {
660 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetOatCodeBegin(m));
661 if (oat_code_begin == NULL) {
662 return 0;
663 }
664 return oat_code_begin[-1];
665 }
666
667 const void* GetOatCodeEnd(Method* m) {
668 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetOatCodeBegin(m));
669 if (oat_code_begin == NULL) {
670 return NULL;
671 }
672 return oat_code_begin + GetOatCodeSize(m);
673 }
674
Brian Carlstrom78128a62011-09-15 17:21:19 -0700675 static void Callback(Object* obj, void* arg) {
676 DCHECK(obj != NULL);
677 DCHECK(arg != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800678 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700679 if (!state->InDumpSpace(obj)) {
680 return;
681 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700682
683 size_t object_bytes = obj->SizeOf();
684 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
685 state->stats_.object_bytes += object_bytes;
686 state->stats_.alignment_bytes += alignment_bytes;
687
Brian Carlstrom78128a62011-09-15 17:21:19 -0700688 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800689 Class* obj_class = obj->GetClass();
690 if (obj_class->IsArrayClass()) {
691 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
692 obj->AsArray()->GetLength());
693 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700694 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800695 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
696 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700697 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800698 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700699 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800700 } else if (obj->IsField()) {
701 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
702 PrettyField(obj->AsField()).c_str());
703 } else if (obj->IsMethod()) {
704 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
705 PrettyMethod(obj->AsMethod()).c_str());
706 } else if (obj_class->IsStringClass()) {
707 StringAppendF(&summary, "%p: java.lang.String \"%s\"\n", obj,
708 obj->AsString()->ToModifiedUtf8().c_str());
709 } else {
710 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
711 }
712 DumpFields(summary, obj, obj_class);
713 if (obj->IsObjectArray()) {
714 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
715 int32_t length = obj_array->GetLength();
716 for (int32_t i = 0; i < length; i++) {
717 Object* value = obj_array->Get(i);
718 size_t run = 0;
719 for (int32_t j = i + 1; j < length; j++) {
720 if (value == obj_array->Get(j)) {
721 run++;
722 } else {
723 break;
724 }
725 }
726 if (run == 0) {
727 StringAppendF(&summary, "\t%d: ", i);
728 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800729 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800730 i = i + run;
731 }
732 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
733 PrettyObjectValue(summary, value_class, value);
734 }
735 } else if (obj->IsClass()) {
736 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
737 if (sfields != NULL) {
738 summary += "\t\tSTATICS:\n";
739 for (int32_t i = 0; i < sfields->GetLength(); i++) {
740 Field* field = sfields->Get(i);
741 PrintField(summary, field, NULL);
742 }
743 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700744 } else if (obj->IsMethod()) {
745 Method* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700746 if (method->IsNative()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700747 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800748 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700749 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800750 bool first_occurrence;
751 size_t invoke_stub_size = state->ComputeOatSize(
752 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
753 if (first_occurrence) {
754 state->stats_.managed_to_native_code_bytes += invoke_stub_size;
755 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700756 const void* oat_code = state->GetOatCodeBegin(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800757 size_t code_size = state->ComputeOatSize(oat_code, &first_occurrence);
758 if (first_occurrence) {
759 state->stats_.native_to_managed_code_bytes += code_size;
760 }
761 if (oat_code != method->GetCode()) {
762 StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code);
763 }
Ian Rogers19846512012-02-24 11:42:47 -0800764 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
765 method->IsResolutionMethod()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700766 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800767 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700768 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700769 } else {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800770 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
771 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700772
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800773 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700774 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700775 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800776
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800777 bool first_occurance;
778 size_t gc_map_bytes = state->ComputeOatSize(method->GetGcMapRaw(), &first_occurance);
779 if (first_occurance) {
780 state->stats_.gc_map_bytes += gc_map_bytes;
781 }
782
783 size_t pc_mapping_table_bytes =
784 state->ComputeOatSize(method->GetMappingTableRaw(), &first_occurance);
785 if (first_occurance) {
786 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
787 }
788
789 size_t vmap_table_bytes =
790 state->ComputeOatSize(method->GetVmapTableRaw(), &first_occurance);
791 if (first_occurance) {
792 state->stats_.vmap_table_bytes += vmap_table_bytes;
793 }
794
795 size_t invoke_stub_size = state->ComputeOatSize(
796 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurance);
797 if (first_occurance) {
798 state->stats_.native_to_managed_code_bytes += invoke_stub_size;
799 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700800 const void* oat_code_begin = state->GetOatCodeBegin(method);
801 const void* oat_code_end = state->GetOatCodeEnd(method);
802 // TODO: use oat_code_size and remove code_size based on offsets
803 // uint32_t oat_code_size = state->GetOatCodeSize(method);
804 size_t code_size = state->ComputeOatSize(oat_code_begin, &first_occurance);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800805 if (first_occurance) {
806 state->stats_.managed_code_bytes += code_size;
807 }
808 state->stats_.managed_code_bytes_ignoring_deduplication += code_size;
809
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700810 StringAppendF(&summary, "\t\tOAT CODE: %p-%p\n", oat_code_begin, oat_code_end);
Ian Rogersd5b32602012-02-26 16:40:04 -0800811 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800812 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
813
814 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
815 vmap_table_bytes + invoke_stub_size + code_size + object_bytes;
816
817 double expansion =
818 static_cast<double>(code_size) / static_cast<double>(dex_instruction_bytes);
819 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700820 }
821 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800822 std::string descriptor(ClassHelper(obj_class).GetDescriptor());
823 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
824 state->stats_.descriptor_to_count[descriptor] += 1;
825
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700826 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700827 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700828
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800829 std::set<const void*> already_seen_;
830 // Compute the size of the given data within the oat file and whether this is the first time
831 // this data has been requested
832 size_t ComputeOatSize(const void* oat_data, bool* first_occurance) {
833 if (already_seen_.count(oat_data) == 0) {
834 *first_occurance = true;
835 already_seen_.insert(oat_data);
836 } else {
837 *first_occurance = false;
838 }
839 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700840 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700841
842 public:
843 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800844 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700845 size_t file_bytes;
846
847 size_t header_bytes;
848 size_t object_bytes;
849 size_t alignment_bytes;
850
851 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800852 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700853 size_t managed_to_native_code_bytes;
854 size_t native_to_managed_code_bytes;
855
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800856 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700857 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800858 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700859
860 size_t dex_instruction_bytes;
861
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800862 std::vector<Method*> method_outlier;
863 std::vector<size_t> method_outlier_size;
864 std::vector<double> method_outlier_expansion;
865
866 explicit Stats()
867 : oat_file_bytes(0),
868 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700869 header_bytes(0),
870 object_bytes(0),
871 alignment_bytes(0),
872 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800873 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700874 managed_to_native_code_bytes(0),
875 native_to_managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800876 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700877 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800878 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700879 dex_instruction_bytes(0) {}
880
Elliott Hughese5448b52012-01-18 16:44:06 -0800881 typedef std::map<std::string, size_t> TableBytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700882 TableBytes descriptor_to_bytes;
883
Elliott Hughese5448b52012-01-18 16:44:06 -0800884 typedef std::map<std::string, size_t> TableCount;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700885 TableCount descriptor_to_count;
886
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800887 double PercentOfOatBytes(size_t size) {
888 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
889 }
890
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700891 double PercentOfFileBytes(size_t size) {
892 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
893 }
894
895 double PercentOfObjectBytes(size_t size) {
896 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
897 }
898
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800899 void ComputeOutliers(size_t total_size, double expansion, Method* method) {
900 method_outlier_size.push_back(total_size);
901 method_outlier_expansion.push_back(expansion);
902 method_outlier.push_back(method);
903 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700904
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800905 void DumpOutliers(std::ostream& os) {
906 size_t sum_of_sizes = 0;
907 size_t sum_of_sizes_squared = 0;
908 size_t sum_of_expansion = 0;
909 size_t sum_of_expansion_squared = 0;
910 size_t n = method_outlier_size.size();
911 for (size_t i = 0; i < n; i++) {
912 size_t cur_size = method_outlier_size[i];
913 sum_of_sizes += cur_size;
914 sum_of_sizes_squared += cur_size * cur_size;
915 double cur_expansion = method_outlier_expansion[i];
916 sum_of_expansion += cur_expansion;
917 sum_of_expansion_squared += cur_expansion * cur_expansion;
918 }
919 size_t size_mean = sum_of_sizes / n;
920 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
921 double expansion_mean = sum_of_expansion / n;
922 double expansion_variance =
923 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
924
925 // Dump methods whose size is a certain number of standard deviations from the mean
926 size_t dumped_values = 0;
927 size_t skipped_values = 0;
928 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
929 size_t cur_size_variance = i * i * size_variance;
930 bool first = true;
931 for (size_t j = 0; j < n; j++) {
932 size_t cur_size = method_outlier_size[j];
933 if (cur_size > size_mean) {
934 size_t cur_var = cur_size - size_mean;
935 cur_var = cur_var * cur_var;
936 if (cur_var > cur_size_variance) {
937 if (dumped_values > 20) {
938 if (i == 1) {
939 skipped_values++;
940 } else {
941 i = 2; // jump to counting for 1 standard deviation
942 break;
943 }
944 } else {
945 if (first) {
946 os << "\nBig methods (size > " << i << " standard deviations the norm):"
947 << std::endl;
948 first = false;
949 }
950 os << "\t" << PrettyMethod(method_outlier[j]) << " requires storage of "
951 << PrettySize(cur_size) << std::endl;
952 method_outlier_size[j] = 0; // don't consider this method again
953 dumped_values++;
954 }
955 }
956 }
957 }
958 }
959 if (skipped_values > 0) {
960 os << "\t... skipped " << skipped_values
961 << " methods with size > 1 standard deviation from the norm" << std::endl;
962 }
963 os << std::endl << std::flush;
964
965 // Dump methods whose expansion is a certain number of standard deviations from the mean
966 dumped_values = 0;
967 skipped_values = 0;
968 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
969 double cur_expansion_variance = i * i * expansion_variance;
970 bool first = true;
971 for (size_t j = 0; j < n; j++) {
972 double cur_expansion = method_outlier_expansion[j];
973 if (cur_expansion > expansion_mean) {
974 size_t cur_var = cur_expansion - expansion_mean;
975 cur_var = cur_var * cur_var;
976 if (cur_var > cur_expansion_variance) {
977 if (dumped_values > 20) {
978 if (i == 1) {
979 skipped_values++;
980 } else {
981 i = 2; // jump to counting for 1 standard deviation
982 break;
983 }
984 } else {
985 if (first) {
986 os << "\nLarge expansion methods (size > " << i
987 << " standard deviations the norm):" << std::endl;
988 first = false;
989 }
990 os << "\t" << PrettyMethod(method_outlier[j]) << " expanded code by "
991 << cur_expansion << std::endl;
992 method_outlier_expansion[j] = 0.0; // don't consider this method again
993 dumped_values++;
994 }
995 }
996 }
997 }
998 }
999 if (skipped_values > 0) {
1000 os << "\t... skipped " << skipped_values
1001 << " methods with expansion > 1 standard deviation from the norm" << std::endl;
1002 }
1003 os << std::endl << std::flush;
1004 }
1005
1006 void Dump(std::ostream& os) {
1007 os << "\tart_file_bytes = " << PrettySize(file_bytes) << std::endl << std::endl
1008 << "\tart_file_bytes = header_bytes + object_bytes + alignment_bytes" << std::endl
1009 << StringPrintf("\theader_bytes = %8zd (%2.0f%% of art file bytes)\n"
1010 "\tobject_bytes = %8zd (%2.0f%% of art file bytes)\n"
1011 "\talignment_bytes = %8zd (%2.0f%% of art file bytes)\n",
1012 header_bytes, PercentOfFileBytes(header_bytes),
1013 object_bytes, PercentOfFileBytes(object_bytes),
1014 alignment_bytes, PercentOfFileBytes(alignment_bytes))
1015 << std::endl << std::flush;
1016
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001017 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
1018
1019 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
1020 size_t object_bytes_total = 0;
1021 typedef TableBytes::const_iterator It; // TODO: C++0x auto
1022 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -08001023 const std::string& descriptor(it->first);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001024 size_t bytes = it->second;
1025 size_t count = descriptor_to_count[descriptor];
1026 double average = static_cast<double>(bytes) / static_cast<double>(count);
1027 double percent = PercentOfObjectBytes(bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001028 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001029 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
1030 descriptor.c_str(), bytes, count,
1031 average, percent);
1032
1033 object_bytes_total += bytes;
1034 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001035 os << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001036 CHECK_EQ(object_bytes, object_bytes_total);
1037
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001038 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1039 "\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1040 "\tnative_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n",
1041 managed_code_bytes, PercentOfOatBytes(managed_code_bytes),
1042 managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes),
1043 native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes))
1044 << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001045
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001046 os << StringPrintf("\tgc_map_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1047 "\tpc_mapping_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n"
1048 "\tvmap_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n",
1049 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1050 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1051 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
1052 << std::endl << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001053
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001054 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001055 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n",
1056 static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes),
1057 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
1058 static_cast<double>(dex_instruction_bytes));
1059 os << std::endl << std::flush;
1060
1061 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001062 }
1063 } stats_;
1064
1065 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001066 UniquePtr<OatDumper> oat_dumper_;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001067 std::ostream& os_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001068 const std::string image_filename_;
1069 const std::string host_prefix_;
1070 Space& image_space_;
1071 const ImageHeader& image_header_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001072
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001073 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001074};
1075
1076int oatdump(int argc, char** argv) {
1077 // Skip over argv[0].
1078 argv++;
1079 argc--;
1080
1081 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001082 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001083 usage();
1084 }
1085
Brian Carlstromaded5f72011-10-07 17:15:04 -07001086 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001087 const char* image_filename = NULL;
1088 const char* boot_image_filename = NULL;
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001089 UniquePtr<std::string> host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001090 std::ostream* os = &std::cout;
1091 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001092
1093 for (int i = 0; i < argc; i++) {
1094 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001095 if (option.starts_with("--oat-file=")) {
1096 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001097 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001098 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001099 } else if (option.starts_with("--boot-image=")) {
1100 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001101 } else if (option.starts_with("--host-prefix=")) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001102 host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001103 } else if (option.starts_with("--output=")) {
1104 const char* filename = option.substr(strlen("--output=")).data();
1105 out.reset(new std::ofstream(filename));
1106 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001107 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001108 usage();
1109 }
1110 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001111 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001112 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001113 usage();
1114 }
1115 }
1116
Brian Carlstromaded5f72011-10-07 17:15:04 -07001117 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001118 fprintf(stderr, "Either --image or --oat must be specified\n");
1119 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001120 }
1121
Brian Carlstromaded5f72011-10-07 17:15:04 -07001122 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001123 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1124 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001125 }
1126
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001127 if (host_prefix.get() == NULL) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001128 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
1129 if (android_product_out != NULL) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001130 host_prefix.reset(new std::string(android_product_out));
1131 } else {
1132 host_prefix.reset(new std::string(""));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001133 }
1134 }
1135
Brian Carlstromaded5f72011-10-07 17:15:04 -07001136 if (oat_filename != NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001137 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001138 if (oat_file == NULL) {
1139 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
1140 return EXIT_FAILURE;
1141 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001142 OatDumper oat_dumper(*host_prefix.get(), *oat_file);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001143 oat_dumper.Dump(*os);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001144 return EXIT_SUCCESS;
1145 }
1146
Brian Carlstrom78128a62011-09-15 17:21:19 -07001147 Runtime::Options options;
1148 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001149 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001150 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001151 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001152 if (boot_image_filename != NULL) {
1153 boot_image_option += "-Ximage:";
1154 boot_image_option += boot_image_filename;
1155 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001156 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001157 if (image_filename != NULL) {
1158 image_option += "-Ximage:";
1159 image_option += image_filename;
1160 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1161 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001162
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001163 if (!host_prefix->empty()) {
1164 options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001165 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001166
1167 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
1168 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001169 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001170 return EXIT_FAILURE;
1171 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001172
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001173 Heap* heap = Runtime::Current()->GetHeap();
Brian Carlstromfddf6f62012-03-15 16:56:45 -07001174 ImageSpace* image_space = heap->GetImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001175 CHECK(image_space != NULL);
1176 const ImageHeader& image_header = image_space->GetImageHeader();
1177 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001178 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001179 return EXIT_FAILURE;
1180 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001181 ImageDumper image_dumper(*os, image_filename, *host_prefix.get(), *image_space, image_header);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001182 image_dumper.Dump();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001183 return EXIT_SUCCESS;
1184}
1185
1186} // namespace art
1187
1188int main(int argc, char** argv) {
1189 return art::oatdump(argc, argv);
1190}