blob: 2d5732f8500bdf66c2a49fb9d567705e318812d1 [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";
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070087 os << reinterpret_cast<const void*>(oat_file.GetBase()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -070088
89 os << "LIMIT:\n";
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070090 os << reinterpret_cast<const void*>(oat_file.GetLimit()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -070091
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;
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700112 os << " (" << dex_file_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700113 }
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700114 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700115 os << StringPrintf("checksum: %08x\n", oat_dex_file.GetDexFileChecksum());
116 const DexFile* dex_file = DexFile::Open(dex_file_location, "");
117 if (dex_file == NULL) {
118 os << "NOT FOUND\n\n";
119 return;
120 }
121 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
122 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
123 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700124 os << StringPrintf("%d: %s (type_idx=%d)\n", class_def_index, descriptor, class_def.class_idx_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700125 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
126 CHECK(oat_class.get() != NULL);
127 DumpOatClass(os, oat_file, *oat_class.get(), *dex_file, class_def);
128 }
129
130 os << std::flush;
131 }
132
133 static void DumpOatClass(std::ostream& os,
134 const OatFile& oat_file,
135 const OatFile::OatClass& oat_class,
136 const DexFile& dex_file,
137 const DexFile::ClassDef& class_def) {
138 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700139 if (class_data == NULL) { // empty class such as a marker interface?
140 return;
141 }
142 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700143
144 // just skipping through the fields to advance class_data
Ian Rogers0571d352011-11-03 19:51:38 -0700145 while (it.HasNextStaticField()) {
146 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700147 }
Ian Rogers0571d352011-11-03 19:51:38 -0700148 while (it.HasNextInstanceField()) {
149 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700150 }
151
Ian Rogers0571d352011-11-03 19:51:38 -0700152 uint32_t method_index = 0;
153 while (it.HasNextDirectMethod()) {
154 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index);
155 DumpOatMethod(os, method_index, oat_file, oat_method, dex_file, it.GetMemberIndex());
156 method_index++;
157 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700158 }
Ian Rogers0571d352011-11-03 19:51:38 -0700159 while (it.HasNextVirtualMethod()) {
160 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index);
161 DumpOatMethod(os, method_index, oat_file, oat_method, dex_file, it.GetMemberIndex());
162 method_index++;
163 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700164 }
Ian Rogers0571d352011-11-03 19:51:38 -0700165 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700166 os << std::flush;
167 }
168 static void DumpOatMethod(std::ostream& os,
169 uint32_t method_index,
170 const OatFile& oat_file,
171 const OatFile::OatMethod& oat_method,
172 const DexFile& dex_file,
Ian Rogers0571d352011-11-03 19:51:38 -0700173 uint32_t method_idx) {
174 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700175 const char* name = dex_file.GetMethodName(method_id);
176 std::string signature = dex_file.GetMethodSignature(method_id);
177 os << StringPrintf("\t%d: %s %s (method_idx=%d)\n",
Ian Rogers0571d352011-11-03 19:51:38 -0700178 method_index, name, signature.c_str(), method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700179 os << StringPrintf("\t\tcode: %p (offset=%08x)\n",
180 oat_method.code_,
181 reinterpret_cast<const byte*>(oat_method.code_) - oat_file.GetBase());
182 os << StringPrintf("\t\tframe_size_in_bytes: %d\n",
183 oat_method.frame_size_in_bytes_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700184 os << StringPrintf("\t\tcore_spill_mask: %08x\n",
185 oat_method.core_spill_mask_);
186 os << StringPrintf("\t\tfp_spill_mask: %08x\n",
187 oat_method.fp_spill_mask_);
188 os << StringPrintf("\t\tmapping_table: %p (offset=%08x)\n",
189 oat_method.mapping_table_,
190 reinterpret_cast<const byte*>(oat_method.mapping_table_) - oat_file.GetBase());
191 os << StringPrintf("\t\tvmap_table: %p (offset=%08x)\n",
192 oat_method.vmap_table_,
193 reinterpret_cast<const byte*>(oat_method.vmap_table_) - oat_file.GetBase());
194 os << StringPrintf("\t\tinvoke_stub: %p (offset=%08x)\n",
195 oat_method.invoke_stub_,
196 reinterpret_cast<const byte*>(oat_method.invoke_stub_) - oat_file.GetBase());
197 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700198};
199
200class ImageDump {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700201 public:
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700202 static void Dump(const std::string& image_filename,
Brian Carlstromaded5f72011-10-07 17:15:04 -0700203 const std::string& host_prefix,
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700204 std::ostream& os,
205 Space& image_space,
206 const ImageHeader& image_header) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700207 os << "MAGIC:\n";
208 os << image_header.GetMagic() << "\n\n";
209
Brian Carlstrome24fa612011-09-29 00:53:55 -0700210 os << "IMAGE BASE:\n";
211 os << reinterpret_cast<void*>(image_header.GetImageBaseAddr()) << "\n\n";
212
Brian Carlstromaded5f72011-10-07 17:15:04 -0700213 os << "OAT CHECKSUM:\n";
214 os << StringPrintf("%08x\n\n", image_header.GetOatChecksum());
215
Brian Carlstrome24fa612011-09-29 00:53:55 -0700216 os << "OAT BASE:\n";
217 os << reinterpret_cast<void*>(image_header.GetOatBaseAddr()) << "\n\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700218
Brian Carlstromaded5f72011-10-07 17:15:04 -0700219 os << "OAT LIMIT:\n";
220 os << reinterpret_cast<void*>(image_header.GetOatLimitAddr()) << "\n\n";
221
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700222 os << "ROOTS:\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700223 os << reinterpret_cast<void*>(image_header.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700224 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700225 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
226 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700227 const char* image_root_description = image_roots_descriptions_[i];
228 Object* image_root_object = image_header.GetImageRoot(image_root);
229 os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
230 if (image_root_object->IsObjectArray()) {
231 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
232 ObjectArray<Object>* image_root_object_array
233 = down_cast<ObjectArray<Object>*>(image_root_object);
234 // = image_root_object->AsObjectArray<Object>();
235 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
236 os << StringPrintf("\t%d: %p\n", i, image_root_object_array->Get(i));
237 }
238 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700239 }
240 os << "\n";
241
242 os << "OBJECTS:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700243 ImageDump state(image_space, os);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700244 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
245 DCHECK(heap_bitmap != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700246 heap_bitmap->Walk(ImageDump::Callback, &state);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700247 os << "\n";
248
249 os << "STATS:\n" << std::flush;
250 UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), false));
251 state.stats_.file_bytes = file->Length();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700252 size_t header_bytes = sizeof(ImageHeader);
253 state.stats_.header_bytes = header_bytes;
254 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
255 state.stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700256 state.stats_.Dump(os);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700257 os << "\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700258
259 os << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700260
261 os << "OAT LOCATION:\n" << std::flush;
262 Object* oat_location_object = image_header.GetImageRoot(ImageHeader::kOatLocation);
263 std::string oat_location = oat_location_object->AsString()->ToModifiedUtf8();
264 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
265 os << oat_location;
266 if (!host_prefix.empty()) {
267 oat_location = host_prefix + oat_location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700268 os << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700269 }
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700270 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700271 const OatFile* oat_file = class_linker->FindOatFile(oat_location);
272 if (oat_file == NULL) {
273 os << "NOT FOUND\n";
274 os << std::flush;
275 return;
276 }
277 os << "\n";
278 os << std::flush;
279
280 OatDump::Dump(oat_location, host_prefix, os, *oat_file);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700281 }
282
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700283 private:
284
Brian Carlstromaded5f72011-10-07 17:15:04 -0700285 ImageDump(const Space& dump_space, std::ostream& os) : dump_space_(dump_space), os_(os) {}
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700286
Brian Carlstromaded5f72011-10-07 17:15:04 -0700287 ~ImageDump() {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700288
Brian Carlstrom78128a62011-09-15 17:21:19 -0700289 static void Callback(Object* obj, void* arg) {
290 DCHECK(obj != NULL);
291 DCHECK(arg != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700292 ImageDump* state = reinterpret_cast<ImageDump*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700293 if (!state->InDumpSpace(obj)) {
294 return;
295 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700296
297 size_t object_bytes = obj->SizeOf();
298 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
299 state->stats_.object_bytes += object_bytes;
300 state->stats_.alignment_bytes += alignment_bytes;
301
Brian Carlstrom78128a62011-09-15 17:21:19 -0700302 std::string summary;
303 StringAppendF(&summary, "%p: ", obj);
304 if (obj->IsClass()) {
305 Class* klass = obj->AsClass();
306 StringAppendF(&summary, "CLASS %s", klass->GetDescriptor()->ToModifiedUtf8().c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700307 std::ostringstream ss;
Brian Carlstrome10b6972011-09-26 13:49:03 -0700308 ss << " (" << klass->GetStatus() << ")";
309 summary += ss.str();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700310 } else if (obj->IsMethod()) {
311 Method* method = obj->AsMethod();
312 StringAppendF(&summary, "METHOD %s", PrettyMethod(method).c_str());
313 } else if (obj->IsField()) {
314 Field* field = obj->AsField();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700315 StringAppendF(&summary, "FIELD %s", PrettyField(field).c_str());
316 } else if (obj->IsArrayInstance()) {
317 StringAppendF(&summary, "ARRAY %d", obj->AsArray()->GetLength());
Elliott Hughesdbb40792011-11-18 17:05:22 -0800318 } else if (obj->GetClass()->IsStringClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700319 StringAppendF(&summary, "STRING %s", obj->AsString()->ToModifiedUtf8().c_str());
320 } else {
321 StringAppendF(&summary, "OBJECT");
322 }
323 StringAppendF(&summary, "\n");
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700324 std::string descriptor = obj->GetClass()->GetDescriptor()->ToModifiedUtf8();
325 StringAppendF(&summary, "\tclass %p: %s\n", obj->GetClass(), descriptor.c_str());
326 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
327 state->stats_.descriptor_to_count[descriptor] += 1;
328 // StringAppendF(&summary, "\tsize %d (alignment padding %d)\n",
329 // object_bytes, RoundUp(object_bytes, kObjectAlignment) - object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700330 if (obj->IsMethod()) {
331 Method* method = obj->AsMethod();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700332 if (!method->IsCalleeSaveMethod()) {
333 const int8_t* code =reinterpret_cast<const int8_t*>(method->GetCode());
334 StringAppendF(&summary, "\tCODE %p\n", code);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700335
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700336 const Method::InvokeStub* invoke_stub = method->GetInvokeStub();
337 StringAppendF(&summary, "\tJNI STUB %p\n", invoke_stub);
Ian Rogersff1ed472011-09-20 13:46:24 -0700338 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700339 if (method->IsNative()) {
340 if (method->IsRegistered()) {
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700341 StringAppendF(&summary, "\tNATIVE REGISTERED %p\n", method->GetNativeMethod());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700342 } else {
343 StringAppendF(&summary, "\tNATIVE UNREGISTERED\n");
344 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700345 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700346 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700347 } else if (method->IsAbstract()) {
348 StringAppendF(&summary, "\tABSTRACT\n");
Ian Rogersd81871c2011-10-03 13:57:23 -0700349 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700350 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
351 } else if (method->IsCalleeSaveMethod()) {
352 StringAppendF(&summary, "\tCALLEE SAVE METHOD\n");
Ian Rogersd81871c2011-10-03 13:57:23 -0700353 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700354 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700355 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -0700356 size_t register_map_bytes = method->GetGcMap()->SizeOf();
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700357 state->stats_.register_map_bytes += register_map_bytes;
358
Brian Carlstrome10b6972011-09-26 13:49:03 -0700359 if (method->GetMappingTable() != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700360 size_t pc_mapping_table_bytes = method->GetMappingTableLength();
Brian Carlstrome10b6972011-09-26 13:49:03 -0700361 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
362 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700363
364 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
365 class DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
366 const DexFile& dex_file = class_linker->FindDexFile(dex_cache);
367 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
Ian Rogersd81871c2011-10-03 13:57:23 -0700368 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700369 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700370 }
371 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700372 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700373 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700374
375 bool InDumpSpace(const Object* object) {
376 const byte* o = reinterpret_cast<const byte*>(object);
377 return (o >= dump_space_.GetBase() && o < dump_space_.GetLimit());
378 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700379
380 public:
381 struct Stats {
382 size_t file_bytes;
383
384 size_t header_bytes;
385 size_t object_bytes;
386 size_t alignment_bytes;
387
388 size_t managed_code_bytes;
389 size_t managed_to_native_code_bytes;
390 size_t native_to_managed_code_bytes;
391
392 size_t register_map_bytes;
393 size_t pc_mapping_table_bytes;
394
395 size_t dex_instruction_bytes;
396
397 Stats()
398 : file_bytes(0),
399 header_bytes(0),
400 object_bytes(0),
401 alignment_bytes(0),
402 managed_code_bytes(0),
403 managed_to_native_code_bytes(0),
404 native_to_managed_code_bytes(0),
405 register_map_bytes(0),
406 pc_mapping_table_bytes(0),
407 dex_instruction_bytes(0) {}
408
409 typedef std::tr1::unordered_map<std::string,size_t> TableBytes;
410 TableBytes descriptor_to_bytes;
411
412 typedef std::tr1::unordered_map<std::string,size_t> TableCount;
413 TableCount descriptor_to_count;
414
415 double PercentOfFileBytes(size_t size) {
416 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
417 }
418
419 double PercentOfObjectBytes(size_t size) {
420 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
421 }
422
423 void Dump(std::ostream& os) {
424 os << StringPrintf("\tfile_bytes = %d\n", file_bytes);
425 os << "\n";
426
427 os << "\tfile_bytes = header_bytes + object_bytes + alignment_bytes\n";
428 os << StringPrintf("\theader_bytes = %10d (%2.0f%% of file_bytes)\n",
429 header_bytes, PercentOfFileBytes(header_bytes));
430 os << StringPrintf("\tobject_bytes = %10d (%2.0f%% of file_bytes)\n",
431 object_bytes, PercentOfFileBytes(object_bytes));
432 os << StringPrintf("\talignment_bytes = %10d (%2.0f%% of file_bytes)\n",
433 alignment_bytes, PercentOfFileBytes(alignment_bytes));
434 os << "\n";
435 os << std::flush;
436 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
437
438 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
439 size_t object_bytes_total = 0;
440 typedef TableBytes::const_iterator It; // TODO: C++0x auto
441 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
442 const std::string& descriptor = it->first;
443 size_t bytes = it->second;
444 size_t count = descriptor_to_count[descriptor];
445 double average = static_cast<double>(bytes) / static_cast<double>(count);
446 double percent = PercentOfObjectBytes(bytes);
Brian Carlstromf153fe12011-09-27 16:27:12 -0700447 os << StringPrintf("\t%32s %8d bytes %6d instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700448 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
449 descriptor.c_str(), bytes, count,
450 average, percent);
451
452 object_bytes_total += bytes;
453 }
454 os << "\n";
455 os << std::flush;
456 CHECK_EQ(object_bytes, object_bytes_total);
457
458 os << StringPrintf("\tmanaged_code_bytes = %8d (%2.0f%% of object_bytes)\n",
459 managed_code_bytes, PercentOfObjectBytes(managed_code_bytes));
460 os << StringPrintf("\tmanaged_to_native_code_bytes = %8d (%2.0f%% of object_bytes)\n",
461 managed_to_native_code_bytes,
462 PercentOfObjectBytes(managed_to_native_code_bytes));
463 os << StringPrintf("\tnative_to_managed_code_bytes = %8d (%2.0f%% of object_bytes)\n",
464 native_to_managed_code_bytes,
465 PercentOfObjectBytes(native_to_managed_code_bytes));
466 os << "\n";
467 os << std::flush;
468
469 os << StringPrintf("\tregister_map_bytes = %7d (%2.0f%% of object_bytes)\n",
470 register_map_bytes, PercentOfObjectBytes(register_map_bytes));
471 os << StringPrintf("\tpc_mapping_table_bytes = %7d (%2.0f%% of object_bytes)\n",
472 pc_mapping_table_bytes, PercentOfObjectBytes(pc_mapping_table_bytes));
473 os << "\n";
474 os << std::flush;
475
476 os << StringPrintf("\tdex_instruction_bytes = %d\n", dex_instruction_bytes);
477 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f\n",
478 static_cast<double>(managed_code_bytes)
479 / static_cast<double>(dex_instruction_bytes));
480 os << "\n";
481 os << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700482 }
483 } stats_;
484
485 private:
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700486 const Space& dump_space_;
487 std::ostream& os_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700488
Brian Carlstromaded5f72011-10-07 17:15:04 -0700489 DISALLOW_COPY_AND_ASSIGN(ImageDump);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700490};
491
492int oatdump(int argc, char** argv) {
493 // Skip over argv[0].
494 argv++;
495 argc--;
496
497 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700498 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700499 usage();
500 }
501
Brian Carlstromaded5f72011-10-07 17:15:04 -0700502 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700503 const char* image_filename = NULL;
504 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700505 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700506 std::ostream* os = &std::cout;
507 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700508
509 for (int i = 0; i < argc; i++) {
510 const StringPiece option(argv[i]);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700511 if (option.starts_with("--oat=")) {
512 oat_filename = option.substr(strlen("--oat=")).data();
513 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700514 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700515 } else if (option.starts_with("--boot-image=")) {
516 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700517 } else if (option.starts_with("--host-prefix=")) {
518 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700519 } else if (option.starts_with("--output=")) {
520 const char* filename = option.substr(strlen("--output=")).data();
521 out.reset(new std::ofstream(filename));
522 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700523 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700524 usage();
525 }
526 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700527 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700528 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700529 usage();
530 }
531 }
532
Brian Carlstromaded5f72011-10-07 17:15:04 -0700533 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700534 fprintf(stderr, "Either --image or --oat must be specified\n");
535 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700536 }
537
Brian Carlstromaded5f72011-10-07 17:15:04 -0700538 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700539 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
540 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700541 }
542
543 if (oat_filename != NULL) {
544 const OatFile* oat_file = OatFile::Open(oat_filename, "", NULL);
545 if (oat_file == NULL) {
546 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
547 return EXIT_FAILURE;
548 }
549 OatDump::Dump(oat_filename, host_prefix, *os, *oat_file);
550 return EXIT_SUCCESS;
551 }
552
Brian Carlstrom78128a62011-09-15 17:21:19 -0700553 Runtime::Options options;
554 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700555 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700556 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700557 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700558 if (boot_image_filename != NULL) {
559 boot_image_option += "-Ximage:";
560 boot_image_option += boot_image_filename;
561 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -0700562 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700563 if (image_filename != NULL) {
564 image_option += "-Ximage:";
565 image_option += image_filename;
566 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
567 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700568
569 if (!host_prefix.empty()) {
570 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
571 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700572
573 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
574 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700575 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700576 return EXIT_FAILURE;
577 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700578
579 Space* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2];
580 CHECK(image_space != NULL);
581 const ImageHeader& image_header = image_space->GetImageHeader();
582 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700583 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700584 return EXIT_FAILURE;
585 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700586 ImageDump::Dump(image_filename, host_prefix, *os, *image_space, image_header);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700587 return EXIT_SUCCESS;
588}
589
590} // namespace art
591
592int main(int argc, char** argv) {
593 return art::oatdump(argc, argv);
594}