blob: dd46896b12d353c7747e31d9eb69d23f8f1bccbf [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"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080015#include "object_utils.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070016#include "runtime.h"
17#include "space.h"
18#include "stringpiece.h"
Brian Carlstrom916e74e2011-09-23 11:42:01 -070019#include "unordered_map.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070020
21namespace art {
22
23static void usage() {
24 fprintf(stderr,
25 "Usage: oatdump [options] ...\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080026 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n"
27 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070028 "\n");
29 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080030 " --oat-file=<file.oat>: specifies an input oat filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080031 " Example: --image=/system/framework/boot.oat\n"
Brian Carlstromaded5f72011-10-07 17:15:04 -070032 "\n");
33 fprintf(stderr,
34 " --image=<file.art>: specifies an input image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080035 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070036 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070037 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070038 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080039 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070040 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070041 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070042 " --host-prefix may be used to translate host paths to target paths during\n"
43 " cross compilation.\n"
44 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070045 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070046 fprintf(stderr,
47 " --output=<file> may be used to send the output to a file.\n"
48 " Example: --output=/tmp/oatdump.txt\n"
49 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070050 exit(EXIT_FAILURE);
51}
52
Ian Rogersff1ed472011-09-20 13:46:24 -070053const char* image_roots_descriptions_[] = {
Brian Carlstrom78128a62011-09-15 17:21:19 -070054 "kJniStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070055 "kAbstractMethodErrorStubArray",
Ian Rogersad25ac52011-10-04 19:13:33 -070056 "kInstanceResolutionStubArray",
57 "kStaticResolutionStubArray",
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070058 "kUnknownMethodResolutionStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070059 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070060 "kRefsOnlySaveMethod",
61 "kRefsAndArgsSaveMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070062 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070063 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070064 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070065};
66
Brian Carlstrom27ec9612011-09-19 20:20:38 -070067class OatDump {
Brian Carlstromaded5f72011-10-07 17:15:04 -070068 public:
69 static void Dump(const std::string& oat_filename,
70 const std::string& host_prefix,
71 std::ostream& os,
72 const OatFile& oat_file) {
73 const OatHeader& oat_header = oat_file.GetOatHeader();
74
75 os << "MAGIC:\n";
76 os << oat_header.GetMagic() << "\n\n";
77
78 os << "CHECKSUM:\n";
79 os << StringPrintf("%08x\n\n", oat_header.GetChecksum());
80
81 os << "DEX FILE COUNT:\n";
82 os << oat_header.GetDexFileCount() << "\n\n";
83
84 os << "EXECUTABLE OFFSET:\n";
85 os << StringPrintf("%08x\n\n", oat_header.GetExecutableOffset());
86
87 os << "BASE:\n";
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070088 os << reinterpret_cast<const void*>(oat_file.GetBase()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -070089
90 os << "LIMIT:\n";
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070091 os << reinterpret_cast<const void*>(oat_file.GetLimit()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -070092
93 os << std::flush;
94
95 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file.GetOatDexFiles() ;
96 for (size_t i = 0; i < oat_dex_files.size(); i++) {
97 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
98 CHECK(oat_dex_file != NULL);
99 DumpOatDexFile(host_prefix, os, oat_file, *oat_dex_file);
100 }
101 }
102
103 private:
104 static void DumpOatDexFile(const std::string& host_prefix,
105 std::ostream& os,
106 const OatFile& oat_file,
107 const OatFile::OatDexFile& oat_dex_file) {
108 os << "OAT DEX FILE:\n";
Elliott Hughes95572412011-12-13 18:14:20 -0800109 std::string dex_file_location(oat_dex_file.GetDexFileLocation());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700110 os << "location: " << dex_file_location;
111 if (!host_prefix.empty()) {
112 dex_file_location = host_prefix + dex_file_location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700113 os << " (" << dex_file_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700114 }
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700115 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700116 os << StringPrintf("checksum: %08x\n", oat_dex_file.GetDexFileChecksum());
117 const DexFile* dex_file = DexFile::Open(dex_file_location, "");
118 if (dex_file == NULL) {
119 os << "NOT FOUND\n\n";
120 return;
121 }
122 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
123 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
124 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700125 os << StringPrintf("%d: %s (type_idx=%d)\n", class_def_index, descriptor, class_def.class_idx_);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700126 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
127 CHECK(oat_class.get() != NULL);
128 DumpOatClass(os, oat_file, *oat_class.get(), *dex_file, class_def);
129 }
130
131 os << std::flush;
132 }
133
134 static void DumpOatClass(std::ostream& os,
135 const OatFile& oat_file,
136 const OatFile::OatClass& oat_class,
137 const DexFile& dex_file,
138 const DexFile::ClassDef& class_def) {
139 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700140 if (class_data == NULL) { // empty class such as a marker interface?
141 return;
142 }
143 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700144
145 // just skipping through the fields to advance class_data
Ian Rogers0571d352011-11-03 19:51:38 -0700146 while (it.HasNextStaticField()) {
147 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700148 }
Ian Rogers0571d352011-11-03 19:51:38 -0700149 while (it.HasNextInstanceField()) {
150 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700151 }
152
Ian Rogers0571d352011-11-03 19:51:38 -0700153 uint32_t method_index = 0;
154 while (it.HasNextDirectMethod()) {
155 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index);
156 DumpOatMethod(os, method_index, oat_file, oat_method, dex_file, it.GetMemberIndex());
157 method_index++;
158 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700159 }
Ian Rogers0571d352011-11-03 19:51:38 -0700160 while (it.HasNextVirtualMethod()) {
161 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index);
162 DumpOatMethod(os, method_index, oat_file, oat_method, dex_file, it.GetMemberIndex());
163 method_index++;
164 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700165 }
Ian Rogers0571d352011-11-03 19:51:38 -0700166 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700167 os << std::flush;
168 }
169 static void DumpOatMethod(std::ostream& os,
170 uint32_t method_index,
171 const OatFile& oat_file,
172 const OatFile::OatMethod& oat_method,
173 const DexFile& dex_file,
Ian Rogers0571d352011-11-03 19:51:38 -0700174 uint32_t method_idx) {
175 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700176 const char* name = dex_file.GetMethodName(method_id);
Elliott Hughes95572412011-12-13 18:14:20 -0800177 std::string signature(dex_file.GetMethodSignature(method_id));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700178 os << StringPrintf("\t%d: %s %s (method_idx=%d)\n",
Ian Rogers0571d352011-11-03 19:51:38 -0700179 method_index, name, signature.c_str(), method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700180 os << StringPrintf("\t\tcode: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800181 oat_method.GetCode(), oat_method.GetCodeOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700182 os << StringPrintf("\t\tframe_size_in_bytes: %d\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800183 oat_method.GetFrameSizeInBytes());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700184 os << StringPrintf("\t\tcore_spill_mask: %08x\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800185 oat_method.GetCoreSpillMask());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700186 os << StringPrintf("\t\tfp_spill_mask: %08x\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800187 oat_method.GetFpSpillMask());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700188 os << StringPrintf("\t\tmapping_table: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800189 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700190 os << StringPrintf("\t\tvmap_table: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800191 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700192 os << StringPrintf("\t\tinvoke_stub: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800193 oat_method.GetInvokeStub(), oat_method.GetInvokeStubOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700194 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700195};
196
197class ImageDump {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700198 public:
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700199 static void Dump(const std::string& image_filename,
Brian Carlstromaded5f72011-10-07 17:15:04 -0700200 const std::string& host_prefix,
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700201 std::ostream& os,
202 Space& image_space,
203 const ImageHeader& image_header) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700204 os << "MAGIC:\n";
205 os << image_header.GetMagic() << "\n\n";
206
Brian Carlstrome24fa612011-09-29 00:53:55 -0700207 os << "IMAGE BASE:\n";
208 os << reinterpret_cast<void*>(image_header.GetImageBaseAddr()) << "\n\n";
209
Brian Carlstromaded5f72011-10-07 17:15:04 -0700210 os << "OAT CHECKSUM:\n";
211 os << StringPrintf("%08x\n\n", image_header.GetOatChecksum());
212
Brian Carlstrome24fa612011-09-29 00:53:55 -0700213 os << "OAT BASE:\n";
214 os << reinterpret_cast<void*>(image_header.GetOatBaseAddr()) << "\n\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700215
Brian Carlstromaded5f72011-10-07 17:15:04 -0700216 os << "OAT LIMIT:\n";
217 os << reinterpret_cast<void*>(image_header.GetOatLimitAddr()) << "\n\n";
218
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700219 os << "ROOTS:\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700220 os << reinterpret_cast<void*>(image_header.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700221 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700222 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
223 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700224 const char* image_root_description = image_roots_descriptions_[i];
225 Object* image_root_object = image_header.GetImageRoot(image_root);
226 os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
227 if (image_root_object->IsObjectArray()) {
228 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
229 ObjectArray<Object>* image_root_object_array
230 = down_cast<ObjectArray<Object>*>(image_root_object);
231 // = image_root_object->AsObjectArray<Object>();
232 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
233 os << StringPrintf("\t%d: %p\n", i, image_root_object_array->Get(i));
234 }
235 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700236 }
237 os << "\n";
238
239 os << "OBJECTS:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700240 ImageDump state(image_space, os);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700241 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
242 DCHECK(heap_bitmap != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700243 heap_bitmap->Walk(ImageDump::Callback, &state);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700244 os << "\n";
245
246 os << "STATS:\n" << std::flush;
247 UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), false));
248 state.stats_.file_bytes = file->Length();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700249 size_t header_bytes = sizeof(ImageHeader);
250 state.stats_.header_bytes = header_bytes;
251 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
252 state.stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700253 state.stats_.Dump(os);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700254 os << "\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700255
256 os << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700257
258 os << "OAT LOCATION:\n" << std::flush;
259 Object* oat_location_object = image_header.GetImageRoot(ImageHeader::kOatLocation);
Elliott Hughes95572412011-12-13 18:14:20 -0800260 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700261 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
262 os << oat_location;
263 if (!host_prefix.empty()) {
264 oat_location = host_prefix + oat_location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700265 os << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700266 }
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700267 os << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800268 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700269 if (oat_file == NULL) {
270 os << "NOT FOUND\n";
271 os << std::flush;
272 return;
273 }
274 os << "\n";
275 os << std::flush;
276
Brian Carlstrom866c8622012-01-06 16:35:13 -0800277 class_linker->RegisterOatFile(*oat_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700278 OatDump::Dump(oat_location, host_prefix, os, *oat_file);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700279 }
280
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700281 private:
282
Brian Carlstromaded5f72011-10-07 17:15:04 -0700283 ImageDump(const Space& dump_space, std::ostream& os) : dump_space_(dump_space), os_(os) {}
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700284
Brian Carlstromaded5f72011-10-07 17:15:04 -0700285 ~ImageDump() {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700286
Brian Carlstrom78128a62011-09-15 17:21:19 -0700287 static void Callback(Object* obj, void* arg) {
288 DCHECK(obj != NULL);
289 DCHECK(arg != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700290 ImageDump* state = reinterpret_cast<ImageDump*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700291 if (!state->InDumpSpace(obj)) {
292 return;
293 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700294
295 size_t object_bytes = obj->SizeOf();
296 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
297 state->stats_.object_bytes += object_bytes;
298 state->stats_.alignment_bytes += alignment_bytes;
299
Brian Carlstrom78128a62011-09-15 17:21:19 -0700300 std::string summary;
301 StringAppendF(&summary, "%p: ", obj);
302 if (obj->IsClass()) {
303 Class* klass = obj->AsClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800304 summary += "CLASS ";
305 summary += ClassHelper(klass).GetDescriptor();
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700306 std::ostringstream ss;
Brian Carlstrome10b6972011-09-26 13:49:03 -0700307 ss << " (" << klass->GetStatus() << ")";
308 summary += ss.str();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700309 } else if (obj->IsMethod()) {
310 Method* method = obj->AsMethod();
311 StringAppendF(&summary, "METHOD %s", PrettyMethod(method).c_str());
312 } else if (obj->IsField()) {
313 Field* field = obj->AsField();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700314 StringAppendF(&summary, "FIELD %s", PrettyField(field).c_str());
315 } else if (obj->IsArrayInstance()) {
316 StringAppendF(&summary, "ARRAY %d", obj->AsArray()->GetLength());
Elliott Hughesdbb40792011-11-18 17:05:22 -0800317 } else if (obj->GetClass()->IsStringClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700318 StringAppendF(&summary, "STRING %s", obj->AsString()->ToModifiedUtf8().c_str());
319 } else {
320 StringAppendF(&summary, "OBJECT");
321 }
322 StringAppendF(&summary, "\n");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800323 std::string descriptor(ClassHelper(obj->GetClass()).GetDescriptor());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700324 StringAppendF(&summary, "\tclass %p: %s\n", obj->GetClass(), descriptor.c_str());
325 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
326 state->stats_.descriptor_to_count[descriptor] += 1;
327 // StringAppendF(&summary, "\tsize %d (alignment padding %d)\n",
328 // object_bytes, RoundUp(object_bytes, kObjectAlignment) - object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700329 if (obj->IsMethod()) {
330 Method* method = obj->AsMethod();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700331 if (!method->IsCalleeSaveMethod()) {
332 const int8_t* code =reinterpret_cast<const int8_t*>(method->GetCode());
333 StringAppendF(&summary, "\tCODE %p\n", code);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700334
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700335 const Method::InvokeStub* invoke_stub = method->GetInvokeStub();
336 StringAppendF(&summary, "\tJNI STUB %p\n", invoke_stub);
Ian Rogersff1ed472011-09-20 13:46:24 -0700337 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700338 if (method->IsNative()) {
339 if (method->IsRegistered()) {
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700340 StringAppendF(&summary, "\tNATIVE REGISTERED %p\n", method->GetNativeMethod());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700341 } else {
342 StringAppendF(&summary, "\tNATIVE UNREGISTERED\n");
343 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700344 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700345 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700346 } else if (method->IsAbstract()) {
347 StringAppendF(&summary, "\tABSTRACT\n");
Ian Rogersd81871c2011-10-03 13:57:23 -0700348 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700349 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
350 } else if (method->IsCalleeSaveMethod()) {
351 StringAppendF(&summary, "\tCALLEE SAVE METHOD\n");
Ian Rogersd81871c2011-10-03 13:57:23 -0700352 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700353 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700354 } else {
Brian Carlstromae826982011-11-09 01:33:42 -0800355 if (method->GetGcMap() != NULL) {
356 size_t register_map_bytes = method->GetGcMap()->SizeOf();
357 state->stats_.register_map_bytes += register_map_bytes;
358 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700359
Brian Carlstrome10b6972011-09-26 13:49:03 -0700360 if (method->GetMappingTable() != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700361 size_t pc_mapping_table_bytes = method->GetMappingTableLength();
Brian Carlstrome10b6972011-09-26 13:49:03 -0700362 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
363 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700364
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800365 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700366 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700367 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700368 }
369 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700370 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700371 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700372
373 bool InDumpSpace(const Object* object) {
374 const byte* o = reinterpret_cast<const byte*>(object);
375 return (o >= dump_space_.GetBase() && o < dump_space_.GetLimit());
376 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700377
378 public:
379 struct Stats {
380 size_t file_bytes;
381
382 size_t header_bytes;
383 size_t object_bytes;
384 size_t alignment_bytes;
385
386 size_t managed_code_bytes;
387 size_t managed_to_native_code_bytes;
388 size_t native_to_managed_code_bytes;
389
390 size_t register_map_bytes;
391 size_t pc_mapping_table_bytes;
392
393 size_t dex_instruction_bytes;
394
395 Stats()
396 : file_bytes(0),
397 header_bytes(0),
398 object_bytes(0),
399 alignment_bytes(0),
400 managed_code_bytes(0),
401 managed_to_native_code_bytes(0),
402 native_to_managed_code_bytes(0),
403 register_map_bytes(0),
404 pc_mapping_table_bytes(0),
405 dex_instruction_bytes(0) {}
406
407 typedef std::tr1::unordered_map<std::string,size_t> TableBytes;
408 TableBytes descriptor_to_bytes;
409
410 typedef std::tr1::unordered_map<std::string,size_t> TableCount;
411 TableCount descriptor_to_count;
412
413 double PercentOfFileBytes(size_t size) {
414 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
415 }
416
417 double PercentOfObjectBytes(size_t size) {
418 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
419 }
420
421 void Dump(std::ostream& os) {
422 os << StringPrintf("\tfile_bytes = %d\n", file_bytes);
423 os << "\n";
424
425 os << "\tfile_bytes = header_bytes + object_bytes + alignment_bytes\n";
426 os << StringPrintf("\theader_bytes = %10d (%2.0f%% of file_bytes)\n",
427 header_bytes, PercentOfFileBytes(header_bytes));
428 os << StringPrintf("\tobject_bytes = %10d (%2.0f%% of file_bytes)\n",
429 object_bytes, PercentOfFileBytes(object_bytes));
430 os << StringPrintf("\talignment_bytes = %10d (%2.0f%% of file_bytes)\n",
431 alignment_bytes, PercentOfFileBytes(alignment_bytes));
432 os << "\n";
433 os << std::flush;
434 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
435
436 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
437 size_t object_bytes_total = 0;
438 typedef TableBytes::const_iterator It; // TODO: C++0x auto
439 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -0800440 const std::string& descriptor(it->first);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700441 size_t bytes = it->second;
442 size_t count = descriptor_to_count[descriptor];
443 double average = static_cast<double>(bytes) / static_cast<double>(count);
444 double percent = PercentOfObjectBytes(bytes);
Brian Carlstromf153fe12011-09-27 16:27:12 -0700445 os << StringPrintf("\t%32s %8d bytes %6d instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700446 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
447 descriptor.c_str(), bytes, count,
448 average, percent);
449
450 object_bytes_total += bytes;
451 }
452 os << "\n";
453 os << std::flush;
454 CHECK_EQ(object_bytes, object_bytes_total);
455
456 os << StringPrintf("\tmanaged_code_bytes = %8d (%2.0f%% of object_bytes)\n",
457 managed_code_bytes, PercentOfObjectBytes(managed_code_bytes));
458 os << StringPrintf("\tmanaged_to_native_code_bytes = %8d (%2.0f%% of object_bytes)\n",
459 managed_to_native_code_bytes,
460 PercentOfObjectBytes(managed_to_native_code_bytes));
461 os << StringPrintf("\tnative_to_managed_code_bytes = %8d (%2.0f%% of object_bytes)\n",
462 native_to_managed_code_bytes,
463 PercentOfObjectBytes(native_to_managed_code_bytes));
464 os << "\n";
465 os << std::flush;
466
467 os << StringPrintf("\tregister_map_bytes = %7d (%2.0f%% of object_bytes)\n",
468 register_map_bytes, PercentOfObjectBytes(register_map_bytes));
469 os << StringPrintf("\tpc_mapping_table_bytes = %7d (%2.0f%% of object_bytes)\n",
470 pc_mapping_table_bytes, PercentOfObjectBytes(pc_mapping_table_bytes));
471 os << "\n";
472 os << std::flush;
473
474 os << StringPrintf("\tdex_instruction_bytes = %d\n", dex_instruction_bytes);
475 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f\n",
476 static_cast<double>(managed_code_bytes)
477 / static_cast<double>(dex_instruction_bytes));
478 os << "\n";
479 os << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700480 }
481 } stats_;
482
483 private:
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700484 const Space& dump_space_;
485 std::ostream& os_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700486
Brian Carlstromaded5f72011-10-07 17:15:04 -0700487 DISALLOW_COPY_AND_ASSIGN(ImageDump);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700488};
489
490int oatdump(int argc, char** argv) {
491 // Skip over argv[0].
492 argv++;
493 argc--;
494
495 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700496 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700497 usage();
498 }
499
Brian Carlstromaded5f72011-10-07 17:15:04 -0700500 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700501 const char* image_filename = NULL;
502 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700503 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700504 std::ostream* os = &std::cout;
505 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700506
507 for (int i = 0; i < argc; i++) {
508 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800509 if (option.starts_with("--oat-file=")) {
510 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700511 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700512 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700513 } else if (option.starts_with("--boot-image=")) {
514 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700515 } else if (option.starts_with("--host-prefix=")) {
516 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700517 } else if (option.starts_with("--output=")) {
518 const char* filename = option.substr(strlen("--output=")).data();
519 out.reset(new std::ofstream(filename));
520 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700521 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700522 usage();
523 }
524 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700525 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700526 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700527 usage();
528 }
529 }
530
Brian Carlstromaded5f72011-10-07 17:15:04 -0700531 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700532 fprintf(stderr, "Either --image or --oat must be specified\n");
533 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700534 }
535
Brian Carlstromaded5f72011-10-07 17:15:04 -0700536 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700537 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
538 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700539 }
540
541 if (oat_filename != NULL) {
542 const OatFile* oat_file = OatFile::Open(oat_filename, "", NULL);
543 if (oat_file == NULL) {
544 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
545 return EXIT_FAILURE;
546 }
547 OatDump::Dump(oat_filename, host_prefix, *os, *oat_file);
548 return EXIT_SUCCESS;
549 }
550
Brian Carlstrom78128a62011-09-15 17:21:19 -0700551 Runtime::Options options;
552 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700553 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700554 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700555 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700556 if (boot_image_filename != NULL) {
557 boot_image_option += "-Ximage:";
558 boot_image_option += boot_image_filename;
559 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -0700560 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700561 if (image_filename != NULL) {
562 image_option += "-Ximage:";
563 image_option += image_filename;
564 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
565 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700566
567 if (!host_prefix.empty()) {
568 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
569 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700570
571 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
572 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700573 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700574 return EXIT_FAILURE;
575 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700576
577 Space* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2];
578 CHECK(image_space != NULL);
579 const ImageHeader& image_header = image_space->GetImageHeader();
580 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700581 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700582 return EXIT_FAILURE;
583 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700584 ImageDump::Dump(image_filename, host_prefix, *os, *image_space, image_header);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700585 return EXIT_SUCCESS;
586}
587
588} // namespace art
589
590int main(int argc, char** argv) {
591 return art::oatdump(argc, argv);
592}