blob: 46f6c249681484d0eeb514cf577628cc05ab06c5 [file] [log] [blame]
Brian Carlstrom78128a62011-09-15 17:21:19 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include <stdio.h>
4#include <stdlib.h>
5
Brian Carlstrom27ec9612011-09-19 20:20:38 -07006#include <fstream>
7#include <iostream>
Brian Carlstrom78128a62011-09-15 17:21:19 -07008#include <string>
9#include <vector>
10
11#include "class_linker.h"
Brian Carlstrom916e74e2011-09-23 11:42:01 -070012#include "file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070013#include "image.h"
Brian Carlstrom916e74e2011-09-23 11:42:01 -070014#include "os.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070015#include "runtime.h"
16#include "space.h"
17#include "stringpiece.h"
Brian Carlstrom916e74e2011-09-23 11:42:01 -070018#include "unordered_map.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070019
20namespace art {
21
22static void usage() {
23 fprintf(stderr,
24 "Usage: oatdump [options] ...\n"
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -070025 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/data/art-cache/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n"
26 " Example: adb shell oatdump --image=/data/art-cache/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070027 "\n");
28 fprintf(stderr,
Brian Carlstromaded5f72011-10-07 17:15:04 -070029 " --oat=<file.oat>: specifies an input oat filename.\n"
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -070030 " Example: --image=/data/art-cache/boot.oat\n"
Brian Carlstromaded5f72011-10-07 17:15:04 -070031 "\n");
32 fprintf(stderr,
33 " --image=<file.art>: specifies an input image filename.\n"
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -070034 " Example: --image=/data/art-cache/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070035 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070036 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070037 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -070038 " Example: --boot-image=/data/art-cache/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070039 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070040 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070041 " --host-prefix may be used to translate host paths to target paths during\n"
42 " cross compilation.\n"
43 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070044 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070045 fprintf(stderr,
46 " --output=<file> may be used to send the output to a file.\n"
47 " Example: --output=/tmp/oatdump.txt\n"
48 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070049 exit(EXIT_FAILURE);
50}
51
Ian Rogersff1ed472011-09-20 13:46:24 -070052const char* image_roots_descriptions_[] = {
Brian Carlstrom78128a62011-09-15 17:21:19 -070053 "kJniStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070054 "kAbstractMethodErrorStubArray",
Ian Rogersad25ac52011-10-04 19:13:33 -070055 "kInstanceResolutionStubArray",
56 "kStaticResolutionStubArray",
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070057 "kUnknownMethodResolutionStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070058 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070059 "kRefsOnlySaveMethod",
60 "kRefsAndArgsSaveMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070061 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070062 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070063 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070064};
65
Brian Carlstrom27ec9612011-09-19 20:20:38 -070066class OatDump {
Brian Carlstromaded5f72011-10-07 17:15:04 -070067 public:
68 static void Dump(const std::string& oat_filename,
69 const std::string& host_prefix,
70 std::ostream& os,
71 const OatFile& oat_file) {
72 const OatHeader& oat_header = oat_file.GetOatHeader();
73
74 os << "MAGIC:\n";
75 os << oat_header.GetMagic() << "\n\n";
76
77 os << "CHECKSUM:\n";
78 os << StringPrintf("%08x\n\n", oat_header.GetChecksum());
79
80 os << "DEX FILE COUNT:\n";
81 os << oat_header.GetDexFileCount() << "\n\n";
82
83 os << "EXECUTABLE OFFSET:\n";
84 os << StringPrintf("%08x\n\n", oat_header.GetExecutableOffset());
85
86 os << "BASE:\n";
87 os << oat_file.GetBase() << "\n\n";
88
89 os << "LIMIT:\n";
90 os << oat_file.GetLimit() << "\n\n";
91
92 os << std::flush;
93
94 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file.GetOatDexFiles() ;
95 for (size_t i = 0; i < oat_dex_files.size(); i++) {
96 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
97 CHECK(oat_dex_file != NULL);
98 DumpOatDexFile(host_prefix, os, oat_file, *oat_dex_file);
99 }
100 }
101
102 private:
103 static void DumpOatDexFile(const std::string& host_prefix,
104 std::ostream& os,
105 const OatFile& oat_file,
106 const OatFile::OatDexFile& oat_dex_file) {
107 os << "OAT DEX FILE:\n";
108 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
109 os << "location: " << dex_file_location;
110 if (!host_prefix.empty()) {
111 dex_file_location = host_prefix + dex_file_location;
112 os << " (" << dex_file_location << ")\n";
113 }
114 os << StringPrintf("checksum: %08x\n", oat_dex_file.GetDexFileChecksum());
115 const DexFile* dex_file = DexFile::Open(dex_file_location, "");
116 if (dex_file == NULL) {
117 os << "NOT FOUND\n\n";
118 return;
119 }
120 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
121 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
122 const char* descriptor = dex_file->GetClassDescriptor(class_def);
123 os << StringPrintf("%d: %s (type_ide=%d)\n", class_def_index, descriptor, class_def.class_idx_);
124 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
125 CHECK(oat_class.get() != NULL);
126 DumpOatClass(os, oat_file, *oat_class.get(), *dex_file, class_def);
127 }
128
129 os << std::flush;
130 }
131
132 static void DumpOatClass(std::ostream& os,
133 const OatFile& oat_file,
134 const OatFile::OatClass& oat_class,
135 const DexFile& dex_file,
136 const DexFile::ClassDef& class_def) {
137 const byte* class_data = dex_file.GetClassData(class_def);
138 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
139 size_t num_static_fields = header.static_fields_size_;
140 size_t num_instance_fields = header.instance_fields_size_;
141 size_t num_direct_methods = header.direct_methods_size_;
142 size_t num_virtual_methods = header.virtual_methods_size_;
143 uint32_t method_index = 0;
144
145 // just skipping through the fields to advance class_data
146 if (num_static_fields != 0) {
147 uint32_t last_idx = 0;
148 for (size_t i = 0; i < num_static_fields; ++i) {
149 DexFile::Field dex_field;
150 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
151 }
152 }
153 if (num_instance_fields != 0) {
154 uint32_t last_idx = 0;
155 for (size_t i = 0; i < num_instance_fields; ++i) {
156 DexFile::Field dex_field;
157 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
158 }
159 }
160
161 if (num_direct_methods != 0) {
162 uint32_t last_idx = 0;
163 for (size_t i = 0; i < num_direct_methods; ++i, method_index++) {
164 DexFile::Method dex_method;
165 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
166 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index);
167 DumpOatMethod(os, method_index, oat_file, oat_method, dex_file, dex_method);
168 }
169 }
170 if (num_virtual_methods != 0) {
171 uint32_t last_idx = 0;
172 for (size_t i = 0; i < num_virtual_methods; ++i, method_index++) {
173 DexFile::Method dex_method;
174 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
175 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index);
176 DumpOatMethod(os, method_index, oat_file, oat_method, dex_file, dex_method);
177 }
178 }
179 os << std::flush;
180 }
181 static void DumpOatMethod(std::ostream& os,
182 uint32_t method_index,
183 const OatFile& oat_file,
184 const OatFile::OatMethod& oat_method,
185 const DexFile& dex_file,
186 const DexFile::Method& dex_method) {
187 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method.method_idx_);
188 const char* name = dex_file.GetMethodName(method_id);
189 std::string signature = dex_file.GetMethodSignature(method_id);
190 os << StringPrintf("\t%d: %s %s (method_idx=%d)\n",
191 method_index, name, signature.c_str(), dex_method.method_idx_);
192 os << StringPrintf("\t\tcode: %p (offset=%08x)\n",
193 oat_method.code_,
194 reinterpret_cast<const byte*>(oat_method.code_) - oat_file.GetBase());
195 os << StringPrintf("\t\tframe_size_in_bytes: %d\n",
196 oat_method.frame_size_in_bytes_);
197 os << StringPrintf("\t\treturn_pc_offset_in_bytes: %d\n",
198 oat_method.return_pc_offset_in_bytes_);
199 os << StringPrintf("\t\tcore_spill_mask: %08x\n",
200 oat_method.core_spill_mask_);
201 os << StringPrintf("\t\tfp_spill_mask: %08x\n",
202 oat_method.fp_spill_mask_);
203 os << StringPrintf("\t\tmapping_table: %p (offset=%08x)\n",
204 oat_method.mapping_table_,
205 reinterpret_cast<const byte*>(oat_method.mapping_table_) - oat_file.GetBase());
206 os << StringPrintf("\t\tvmap_table: %p (offset=%08x)\n",
207 oat_method.vmap_table_,
208 reinterpret_cast<const byte*>(oat_method.vmap_table_) - oat_file.GetBase());
209 os << StringPrintf("\t\tinvoke_stub: %p (offset=%08x)\n",
210 oat_method.invoke_stub_,
211 reinterpret_cast<const byte*>(oat_method.invoke_stub_) - oat_file.GetBase());
212 }
213
214};
215
216class ImageDump {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700217
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700218 public:
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700219 static void Dump(const std::string& image_filename,
Brian Carlstromaded5f72011-10-07 17:15:04 -0700220 const std::string& host_prefix,
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700221 std::ostream& os,
222 Space& image_space,
223 const ImageHeader& image_header) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700224 os << "MAGIC:\n";
225 os << image_header.GetMagic() << "\n\n";
226
Brian Carlstrome24fa612011-09-29 00:53:55 -0700227 os << "IMAGE BASE:\n";
228 os << reinterpret_cast<void*>(image_header.GetImageBaseAddr()) << "\n\n";
229
Brian Carlstromaded5f72011-10-07 17:15:04 -0700230 os << "OAT CHECKSUM:\n";
231 os << StringPrintf("%08x\n\n", image_header.GetOatChecksum());
232
Brian Carlstrome24fa612011-09-29 00:53:55 -0700233 os << "OAT BASE:\n";
234 os << reinterpret_cast<void*>(image_header.GetOatBaseAddr()) << "\n\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700235
Brian Carlstromaded5f72011-10-07 17:15:04 -0700236 os << "OAT LIMIT:\n";
237 os << reinterpret_cast<void*>(image_header.GetOatLimitAddr()) << "\n\n";
238
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700239 os << "ROOTS:\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700240 os << reinterpret_cast<void*>(image_header.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700241 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700242 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
243 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700244 const char* image_root_description = image_roots_descriptions_[i];
245 Object* image_root_object = image_header.GetImageRoot(image_root);
246 os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
247 if (image_root_object->IsObjectArray()) {
248 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
249 ObjectArray<Object>* image_root_object_array
250 = down_cast<ObjectArray<Object>*>(image_root_object);
251 // = image_root_object->AsObjectArray<Object>();
252 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
253 os << StringPrintf("\t%d: %p\n", i, image_root_object_array->Get(i));
254 }
255 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700256 }
257 os << "\n";
258
259 os << "OBJECTS:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700260 ImageDump state(image_space, os);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700261 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
262 DCHECK(heap_bitmap != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700263 heap_bitmap->Walk(ImageDump::Callback, &state);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700264 os << "\n";
265
266 os << "STATS:\n" << std::flush;
267 UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), false));
268 state.stats_.file_bytes = file->Length();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700269 size_t header_bytes = sizeof(ImageHeader);
270 state.stats_.header_bytes = header_bytes;
271 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
272 state.stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700273 state.stats_.Dump(os);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700274 os << "\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700275
276 os << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700277
278 os << "OAT LOCATION:\n" << std::flush;
279 Object* oat_location_object = image_header.GetImageRoot(ImageHeader::kOatLocation);
280 std::string oat_location = oat_location_object->AsString()->ToModifiedUtf8();
281 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
282 os << oat_location;
283 if (!host_prefix.empty()) {
284 oat_location = host_prefix + oat_location;
285 os << " (" << oat_location << ")\n";
286 }
287 const OatFile* oat_file = class_linker->FindOatFile(oat_location);
288 if (oat_file == NULL) {
289 os << "NOT FOUND\n";
290 os << std::flush;
291 return;
292 }
293 os << "\n";
294 os << std::flush;
295
296 OatDump::Dump(oat_location, host_prefix, os, *oat_file);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700297 }
298
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700299 private:
300
Brian Carlstromaded5f72011-10-07 17:15:04 -0700301 ImageDump(const Space& dump_space, std::ostream& os) : dump_space_(dump_space), os_(os) {}
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700302
Brian Carlstromaded5f72011-10-07 17:15:04 -0700303 ~ImageDump() {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700304
Brian Carlstrom78128a62011-09-15 17:21:19 -0700305 static void Callback(Object* obj, void* arg) {
306 DCHECK(obj != NULL);
307 DCHECK(arg != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700308 ImageDump* state = reinterpret_cast<ImageDump*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700309 if (!state->InDumpSpace(obj)) {
310 return;
311 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700312
313 size_t object_bytes = obj->SizeOf();
314 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
315 state->stats_.object_bytes += object_bytes;
316 state->stats_.alignment_bytes += alignment_bytes;
317
Brian Carlstrom78128a62011-09-15 17:21:19 -0700318 std::string summary;
319 StringAppendF(&summary, "%p: ", obj);
320 if (obj->IsClass()) {
321 Class* klass = obj->AsClass();
322 StringAppendF(&summary, "CLASS %s", klass->GetDescriptor()->ToModifiedUtf8().c_str());
Brian Carlstrome10b6972011-09-26 13:49:03 -0700323 std::stringstream ss;
324 ss << " (" << klass->GetStatus() << ")";
325 summary += ss.str();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700326 } else if (obj->IsMethod()) {
327 Method* method = obj->AsMethod();
328 StringAppendF(&summary, "METHOD %s", PrettyMethod(method).c_str());
329 } else if (obj->IsField()) {
330 Field* field = obj->AsField();
331 Class* type = field->GetType();
332 std::string type_string;
333 type_string += (type == NULL) ? "<UNKNOWN>" : type->GetDescriptor()->ToModifiedUtf8();
334 StringAppendF(&summary, "FIELD %s", PrettyField(field).c_str());
335 } else if (obj->IsArrayInstance()) {
336 StringAppendF(&summary, "ARRAY %d", obj->AsArray()->GetLength());
337 } else if (obj->IsString()) {
338 StringAppendF(&summary, "STRING %s", obj->AsString()->ToModifiedUtf8().c_str());
339 } else {
340 StringAppendF(&summary, "OBJECT");
341 }
342 StringAppendF(&summary, "\n");
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700343 std::string descriptor = obj->GetClass()->GetDescriptor()->ToModifiedUtf8();
344 StringAppendF(&summary, "\tclass %p: %s\n", obj->GetClass(), descriptor.c_str());
345 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
346 state->stats_.descriptor_to_count[descriptor] += 1;
347 // StringAppendF(&summary, "\tsize %d (alignment padding %d)\n",
348 // object_bytes, RoundUp(object_bytes, kObjectAlignment) - object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700349 if (obj->IsMethod()) {
350 Method* method = obj->AsMethod();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700351 if (!method->IsCalleeSaveMethod()) {
352 const int8_t* code =reinterpret_cast<const int8_t*>(method->GetCode());
353 StringAppendF(&summary, "\tCODE %p\n", code);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700354
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700355 const Method::InvokeStub* invoke_stub = method->GetInvokeStub();
356 StringAppendF(&summary, "\tJNI STUB %p\n", invoke_stub);
Ian Rogersff1ed472011-09-20 13:46:24 -0700357 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700358 if (method->IsNative()) {
359 if (method->IsRegistered()) {
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700360 StringAppendF(&summary, "\tNATIVE REGISTERED %p\n", method->GetNativeMethod());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700361 } else {
362 StringAppendF(&summary, "\tNATIVE UNREGISTERED\n");
363 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700364 DCHECK(method->GetRegisterMapHeader() == NULL) << PrettyMethod(method);
365 DCHECK(method->GetRegisterMapData() == NULL) << PrettyMethod(method);
366 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700367 } else if (method->IsAbstract()) {
368 StringAppendF(&summary, "\tABSTRACT\n");
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700369 DCHECK(method->GetRegisterMapHeader() == NULL) << PrettyMethod(method);
370 DCHECK(method->GetRegisterMapData() == NULL) << PrettyMethod(method);
371 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
372 } else if (method->IsCalleeSaveMethod()) {
373 StringAppendF(&summary, "\tCALLEE SAVE METHOD\n");
374 DCHECK(method->GetRegisterMapHeader() == NULL) << PrettyMethod(method);
375 DCHECK(method->GetRegisterMapData() == NULL) << PrettyMethod(method);
376 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700377 } else {
378 size_t register_map_bytes = (method->GetRegisterMapHeader()->GetLength() +
379 method->GetRegisterMapData()->GetLength());
380 state->stats_.register_map_bytes += register_map_bytes;
381
Brian Carlstrome10b6972011-09-26 13:49:03 -0700382 if (method->GetMappingTable() != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700383 size_t pc_mapping_table_bytes = method->GetMappingTableLength();
Brian Carlstrome10b6972011-09-26 13:49:03 -0700384 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
385 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700386
387 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
388 class DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
389 const DexFile& dex_file = class_linker->FindDexFile(dex_cache);
390 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
391 size_t dex_instruction_bytes = code_item->insns_size_ * 2;
392 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700393 }
394 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700395 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700396 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700397
398 bool InDumpSpace(const Object* object) {
399 const byte* o = reinterpret_cast<const byte*>(object);
400 return (o >= dump_space_.GetBase() && o < dump_space_.GetLimit());
401 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700402
403 public:
404 struct Stats {
405 size_t file_bytes;
406
407 size_t header_bytes;
408 size_t object_bytes;
409 size_t alignment_bytes;
410
411 size_t managed_code_bytes;
412 size_t managed_to_native_code_bytes;
413 size_t native_to_managed_code_bytes;
414
415 size_t register_map_bytes;
416 size_t pc_mapping_table_bytes;
417
418 size_t dex_instruction_bytes;
419
420 Stats()
421 : file_bytes(0),
422 header_bytes(0),
423 object_bytes(0),
424 alignment_bytes(0),
425 managed_code_bytes(0),
426 managed_to_native_code_bytes(0),
427 native_to_managed_code_bytes(0),
428 register_map_bytes(0),
429 pc_mapping_table_bytes(0),
430 dex_instruction_bytes(0) {}
431
432 typedef std::tr1::unordered_map<std::string,size_t> TableBytes;
433 TableBytes descriptor_to_bytes;
434
435 typedef std::tr1::unordered_map<std::string,size_t> TableCount;
436 TableCount descriptor_to_count;
437
438 double PercentOfFileBytes(size_t size) {
439 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
440 }
441
442 double PercentOfObjectBytes(size_t size) {
443 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
444 }
445
446 void Dump(std::ostream& os) {
447 os << StringPrintf("\tfile_bytes = %d\n", file_bytes);
448 os << "\n";
449
450 os << "\tfile_bytes = header_bytes + object_bytes + alignment_bytes\n";
451 os << StringPrintf("\theader_bytes = %10d (%2.0f%% of file_bytes)\n",
452 header_bytes, PercentOfFileBytes(header_bytes));
453 os << StringPrintf("\tobject_bytes = %10d (%2.0f%% of file_bytes)\n",
454 object_bytes, PercentOfFileBytes(object_bytes));
455 os << StringPrintf("\talignment_bytes = %10d (%2.0f%% of file_bytes)\n",
456 alignment_bytes, PercentOfFileBytes(alignment_bytes));
457 os << "\n";
458 os << std::flush;
459 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
460
461 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
462 size_t object_bytes_total = 0;
463 typedef TableBytes::const_iterator It; // TODO: C++0x auto
464 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
465 const std::string& descriptor = it->first;
466 size_t bytes = it->second;
467 size_t count = descriptor_to_count[descriptor];
468 double average = static_cast<double>(bytes) / static_cast<double>(count);
469 double percent = PercentOfObjectBytes(bytes);
Brian Carlstromf153fe12011-09-27 16:27:12 -0700470 os << StringPrintf("\t%32s %8d bytes %6d instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700471 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
472 descriptor.c_str(), bytes, count,
473 average, percent);
474
475 object_bytes_total += bytes;
476 }
477 os << "\n";
478 os << std::flush;
479 CHECK_EQ(object_bytes, object_bytes_total);
480
481 os << StringPrintf("\tmanaged_code_bytes = %8d (%2.0f%% of object_bytes)\n",
482 managed_code_bytes, PercentOfObjectBytes(managed_code_bytes));
483 os << StringPrintf("\tmanaged_to_native_code_bytes = %8d (%2.0f%% of object_bytes)\n",
484 managed_to_native_code_bytes,
485 PercentOfObjectBytes(managed_to_native_code_bytes));
486 os << StringPrintf("\tnative_to_managed_code_bytes = %8d (%2.0f%% of object_bytes)\n",
487 native_to_managed_code_bytes,
488 PercentOfObjectBytes(native_to_managed_code_bytes));
489 os << "\n";
490 os << std::flush;
491
492 os << StringPrintf("\tregister_map_bytes = %7d (%2.0f%% of object_bytes)\n",
493 register_map_bytes, PercentOfObjectBytes(register_map_bytes));
494 os << StringPrintf("\tpc_mapping_table_bytes = %7d (%2.0f%% of object_bytes)\n",
495 pc_mapping_table_bytes, PercentOfObjectBytes(pc_mapping_table_bytes));
496 os << "\n";
497 os << std::flush;
498
499 os << StringPrintf("\tdex_instruction_bytes = %d\n", dex_instruction_bytes);
500 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f\n",
501 static_cast<double>(managed_code_bytes)
502 / static_cast<double>(dex_instruction_bytes));
503 os << "\n";
504 os << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700505 }
506 } stats_;
507
508 private:
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700509 const Space& dump_space_;
510 std::ostream& os_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700511
Brian Carlstromaded5f72011-10-07 17:15:04 -0700512 DISALLOW_COPY_AND_ASSIGN(ImageDump);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700513};
514
515int oatdump(int argc, char** argv) {
516 // Skip over argv[0].
517 argv++;
518 argc--;
519
520 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700521 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700522 usage();
523 }
524
Brian Carlstromaded5f72011-10-07 17:15:04 -0700525 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700526 const char* image_filename = NULL;
527 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700528 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700529 std::ostream* os = &std::cout;
530 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700531
532 for (int i = 0; i < argc; i++) {
533 const StringPiece option(argv[i]);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700534 if (option.starts_with("--oat=")) {
535 oat_filename = option.substr(strlen("--oat=")).data();
536 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700537 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700538 } else if (option.starts_with("--boot-image=")) {
539 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700540 } else if (option.starts_with("--host-prefix=")) {
541 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700542 } else if (option.starts_with("--output=")) {
543 const char* filename = option.substr(strlen("--output=")).data();
544 out.reset(new std::ofstream(filename));
545 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700546 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700547 usage();
548 }
549 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700550 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700551 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700552 usage();
553 }
554 }
555
Brian Carlstromaded5f72011-10-07 17:15:04 -0700556 if (image_filename == NULL && oat_filename == NULL) {
557 fprintf(stderr, "Either --image or --oat must be specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700558 return EXIT_FAILURE;
559 }
560
Brian Carlstromaded5f72011-10-07 17:15:04 -0700561 if (image_filename != NULL && oat_filename != NULL) {
562 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
563 return EXIT_FAILURE;
564 }
565
566 if (oat_filename != NULL) {
567 const OatFile* oat_file = OatFile::Open(oat_filename, "", NULL);
568 if (oat_file == NULL) {
569 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
570 return EXIT_FAILURE;
571 }
572 OatDump::Dump(oat_filename, host_prefix, *os, *oat_file);
573 return EXIT_SUCCESS;
574 }
575
Brian Carlstrom78128a62011-09-15 17:21:19 -0700576 Runtime::Options options;
577 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700578 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700579 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700580 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700581 if (boot_image_filename != NULL) {
582 boot_image_option += "-Ximage:";
583 boot_image_option += boot_image_filename;
584 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -0700585 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700586 if (image_filename != NULL) {
587 image_option += "-Ximage:";
588 image_option += image_filename;
589 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
590 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700591
592 if (!host_prefix.empty()) {
593 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
594 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700595
596 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
597 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700598 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700599 return EXIT_FAILURE;
600 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700601
602 Space* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2];
603 CHECK(image_space != NULL);
604 const ImageHeader& image_header = image_space->GetImageHeader();
605 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700606 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700607 return EXIT_FAILURE;
608 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700609 ImageDump::Dump(image_filename, host_prefix, *os, *image_space, image_header);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700610 return EXIT_SUCCESS;
611}
612
613} // namespace art
614
615int main(int argc, char** argv) {
616 return art::oatdump(argc, argv);
617}