blob: bf9962cea435224decca2a0c5bf795a8b28eb2b3 [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>
Elliott Hughese5448b52012-01-18 16:44:06 -08008#include <map>
Brian Carlstrom78128a62011-09-15 17:21:19 -07009#include <string>
10#include <vector>
11
12#include "class_linker.h"
Brian Carlstrom916e74e2011-09-23 11:42:01 -070013#include "file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070014#include "image.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080015#include "object_utils.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080016#include "os.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070017#include "runtime.h"
18#include "space.h"
19#include "stringpiece.h"
20
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
Ian Rogers30fab402012-01-23 15:43:46 -080087 os << "BEGIN:\n";
88 os << reinterpret_cast<const void*>(oat_file.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -070089
Ian Rogers30fab402012-01-23 15:43:46 -080090 os << "END:\n";
91 os << reinterpret_cast<const void*>(oat_file.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -070092
93 os << std::flush;
94
Elliott Hughesba8eee12012-01-24 20:25:24 -080095 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file.GetOatDexFiles();
Brian Carlstromaded5f72011-10-07 17:15:04 -070096 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 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);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800127 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800128 << oat_class->GetStatus() << ")\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700129 DumpOatClass(os, oat_file, *oat_class.get(), *dex_file, class_def);
130 }
131
132 os << std::flush;
133 }
134
135 static void DumpOatClass(std::ostream& os,
136 const OatFile& oat_file,
137 const OatFile::OatClass& oat_class,
138 const DexFile& dex_file,
139 const DexFile::ClassDef& class_def) {
140 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700141 if (class_data == NULL) { // empty class such as a marker interface?
142 return;
143 }
144 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700145
146 // just skipping through the fields to advance class_data
Ian Rogers0571d352011-11-03 19:51:38 -0700147 while (it.HasNextStaticField()) {
148 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700149 }
Ian Rogers0571d352011-11-03 19:51:38 -0700150 while (it.HasNextInstanceField()) {
151 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700152 }
153
Ian Rogers0571d352011-11-03 19:51:38 -0700154 uint32_t method_index = 0;
155 while (it.HasNextDirectMethod()) {
156 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index);
157 DumpOatMethod(os, method_index, oat_file, oat_method, dex_file, it.GetMemberIndex());
158 method_index++;
159 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700160 }
Ian Rogers0571d352011-11-03 19:51:38 -0700161 while (it.HasNextVirtualMethod()) {
162 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index);
163 DumpOatMethod(os, method_index, oat_file, oat_method, dex_file, it.GetMemberIndex());
164 method_index++;
165 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700166 }
Ian Rogers0571d352011-11-03 19:51:38 -0700167 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700168 os << std::flush;
169 }
170 static void DumpOatMethod(std::ostream& os,
171 uint32_t method_index,
172 const OatFile& oat_file,
173 const OatFile::OatMethod& oat_method,
174 const DexFile& dex_file,
Ian Rogers0571d352011-11-03 19:51:38 -0700175 uint32_t method_idx) {
176 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700177 const char* name = dex_file.GetMethodName(method_id);
Elliott Hughes95572412011-12-13 18:14:20 -0800178 std::string signature(dex_file.GetMethodSignature(method_id));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700179 os << StringPrintf("\t%d: %s %s (method_idx=%d)\n",
Ian Rogers0571d352011-11-03 19:51:38 -0700180 method_index, name, signature.c_str(), method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700181 os << StringPrintf("\t\tcode: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800182 oat_method.GetCode(), oat_method.GetCodeOffset());
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800183 os << StringPrintf("\t\tframe_size_in_bytes: %zd\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800184 oat_method.GetFrameSizeInBytes());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700185 os << StringPrintf("\t\tcore_spill_mask: %08x\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800186 oat_method.GetCoreSpillMask());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700187 os << StringPrintf("\t\tfp_spill_mask: %08x\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800188 oat_method.GetFpSpillMask());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700189 os << StringPrintf("\t\tmapping_table: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800190 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700191 os << StringPrintf("\t\tvmap_table: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800192 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800193 os << StringPrintf("\t\tgc_map: %p (offset=%08x)\n",
194 oat_method.GetGcMap(), oat_method.GetGcMapOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700195 os << StringPrintf("\t\tinvoke_stub: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800196 oat_method.GetInvokeStub(), oat_method.GetInvokeStubOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700197 }
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
Ian Rogers30fab402012-01-23 15:43:46 -0800210 os << "IMAGE BEGIN:\n";
211 os << reinterpret_cast<void*>(image_header.GetImageBegin()) << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700212
Brian Carlstromaded5f72011-10-07 17:15:04 -0700213 os << "OAT CHECKSUM:\n";
214 os << StringPrintf("%08x\n\n", image_header.GetOatChecksum());
215
Ian Rogers30fab402012-01-23 15:43:46 -0800216 os << "OAT BEGIN:\n";
217 os << reinterpret_cast<void*>(image_header.GetOatBegin()) << "\n\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700218
Ian Rogers30fab402012-01-23 15:43:46 -0800219 os << "OAT END:\n";
220 os << reinterpret_cast<void*>(image_header.GetOatEnd()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700221
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);
Elliott Hughes95572412011-12-13 18:14:20 -0800263 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700264 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 Carlstromae826982011-11-09 01:33:42 -0800271 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700272 if (oat_file == NULL) {
273 os << "NOT FOUND\n";
274 os << std::flush;
275 return;
276 }
277 os << "\n";
278 os << std::flush;
279
Brian Carlstrom866c8622012-01-06 16:35:13 -0800280 class_linker->RegisterOatFile(*oat_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700281 OatDump::Dump(oat_location, host_prefix, os, *oat_file);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700282 }
283
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700284 private:
285
Brian Carlstromaded5f72011-10-07 17:15:04 -0700286 ImageDump(const Space& dump_space, std::ostream& os) : dump_space_(dump_space), os_(os) {}
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700287
Brian Carlstromaded5f72011-10-07 17:15:04 -0700288 ~ImageDump() {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700289
Brian Carlstrom78128a62011-09-15 17:21:19 -0700290 static void Callback(Object* obj, void* arg) {
291 DCHECK(obj != NULL);
292 DCHECK(arg != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700293 ImageDump* state = reinterpret_cast<ImageDump*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700294 if (!state->InDumpSpace(obj)) {
295 return;
296 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700297
298 size_t object_bytes = obj->SizeOf();
299 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
300 state->stats_.object_bytes += object_bytes;
301 state->stats_.alignment_bytes += alignment_bytes;
302
Brian Carlstrom78128a62011-09-15 17:21:19 -0700303 std::string summary;
304 StringAppendF(&summary, "%p: ", obj);
305 if (obj->IsClass()) {
306 Class* klass = obj->AsClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800307 summary += "CLASS ";
308 summary += ClassHelper(klass).GetDescriptor();
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700309 std::ostringstream ss;
Brian Carlstrome10b6972011-09-26 13:49:03 -0700310 ss << " (" << klass->GetStatus() << ")";
311 summary += ss.str();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700312 } else if (obj->IsMethod()) {
313 Method* method = obj->AsMethod();
314 StringAppendF(&summary, "METHOD %s", PrettyMethod(method).c_str());
315 } else if (obj->IsField()) {
316 Field* field = obj->AsField();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700317 StringAppendF(&summary, "FIELD %s", PrettyField(field).c_str());
318 } else if (obj->IsArrayInstance()) {
319 StringAppendF(&summary, "ARRAY %d", obj->AsArray()->GetLength());
Elliott Hughesdbb40792011-11-18 17:05:22 -0800320 } else if (obj->GetClass()->IsStringClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700321 StringAppendF(&summary, "STRING %s", obj->AsString()->ToModifiedUtf8().c_str());
322 } else {
323 StringAppendF(&summary, "OBJECT");
324 }
325 StringAppendF(&summary, "\n");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800326 std::string descriptor(ClassHelper(obj->GetClass()).GetDescriptor());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700327 StringAppendF(&summary, "\tclass %p: %s\n", obj->GetClass(), descriptor.c_str());
328 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
329 state->stats_.descriptor_to_count[descriptor] += 1;
330 // StringAppendF(&summary, "\tsize %d (alignment padding %d)\n",
331 // object_bytes, RoundUp(object_bytes, kObjectAlignment) - object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700332 if (obj->IsMethod()) {
333 Method* method = obj->AsMethod();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700334 if (!method->IsCalleeSaveMethod()) {
335 const int8_t* code =reinterpret_cast<const int8_t*>(method->GetCode());
336 StringAppendF(&summary, "\tCODE %p\n", code);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700337
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700338 const Method::InvokeStub* invoke_stub = method->GetInvokeStub();
339 StringAppendF(&summary, "\tJNI STUB %p\n", invoke_stub);
Ian Rogersff1ed472011-09-20 13:46:24 -0700340 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700341 if (method->IsNative()) {
342 if (method->IsRegistered()) {
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700343 StringAppendF(&summary, "\tNATIVE REGISTERED %p\n", method->GetNativeMethod());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700344 } else {
345 StringAppendF(&summary, "\tNATIVE UNREGISTERED\n");
346 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700347 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800348 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700349 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700350 } else if (method->IsAbstract()) {
351 StringAppendF(&summary, "\tABSTRACT\n");
Ian Rogersd81871c2011-10-03 13:57:23 -0700352 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800353 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700354 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
355 } else if (method->IsCalleeSaveMethod()) {
356 StringAppendF(&summary, "\tCALLEE SAVE METHOD\n");
Ian Rogersd81871c2011-10-03 13:57:23 -0700357 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800358 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700359 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700360 } else {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800361 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
362 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700363
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800364 size_t register_map_bytes = method->GetGcMapLength();
365 state->stats_.register_map_bytes += register_map_bytes;
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800366 StringAppendF(&summary, "GC=%zd ", register_map_bytes);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800367
368 size_t pc_mapping_table_bytes = method->GetMappingTableLength();
369 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800370 StringAppendF(&summary, "Mapping=%zd ", pc_mapping_table_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700371
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800372 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700373 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700374 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800375
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800376 StringAppendF(&summary, "\tSIZE Code=%zd GC=%zd Mapping=%zd",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800377 dex_instruction_bytes, register_map_bytes, pc_mapping_table_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700378 }
379 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700380 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700381 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700382
383 bool InDumpSpace(const Object* object) {
Ian Rogers30fab402012-01-23 15:43:46 -0800384 return dump_space_.Contains(object);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700385 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700386
387 public:
388 struct Stats {
389 size_t file_bytes;
390
391 size_t header_bytes;
392 size_t object_bytes;
393 size_t alignment_bytes;
394
395 size_t managed_code_bytes;
396 size_t managed_to_native_code_bytes;
397 size_t native_to_managed_code_bytes;
398
399 size_t register_map_bytes;
400 size_t pc_mapping_table_bytes;
401
402 size_t dex_instruction_bytes;
403
404 Stats()
405 : file_bytes(0),
406 header_bytes(0),
407 object_bytes(0),
408 alignment_bytes(0),
409 managed_code_bytes(0),
410 managed_to_native_code_bytes(0),
411 native_to_managed_code_bytes(0),
412 register_map_bytes(0),
413 pc_mapping_table_bytes(0),
414 dex_instruction_bytes(0) {}
415
Elliott Hughese5448b52012-01-18 16:44:06 -0800416 typedef std::map<std::string, size_t> TableBytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700417 TableBytes descriptor_to_bytes;
418
Elliott Hughese5448b52012-01-18 16:44:06 -0800419 typedef std::map<std::string, size_t> TableCount;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700420 TableCount descriptor_to_count;
421
422 double PercentOfFileBytes(size_t size) {
423 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
424 }
425
426 double PercentOfObjectBytes(size_t size) {
427 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
428 }
429
430 void Dump(std::ostream& os) {
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800431 os << StringPrintf("\tfile_bytes = %zd\n", file_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700432 os << "\n";
433
434 os << "\tfile_bytes = header_bytes + object_bytes + alignment_bytes\n";
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800435 os << StringPrintf("\theader_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700436 header_bytes, PercentOfFileBytes(header_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800437 os << StringPrintf("\tobject_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700438 object_bytes, PercentOfFileBytes(object_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800439 os << StringPrintf("\talignment_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700440 alignment_bytes, PercentOfFileBytes(alignment_bytes));
441 os << "\n";
442 os << std::flush;
443 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
444
445 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
446 size_t object_bytes_total = 0;
447 typedef TableBytes::const_iterator It; // TODO: C++0x auto
448 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -0800449 const std::string& descriptor(it->first);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700450 size_t bytes = it->second;
451 size_t count = descriptor_to_count[descriptor];
452 double average = static_cast<double>(bytes) / static_cast<double>(count);
453 double percent = PercentOfObjectBytes(bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800454 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700455 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
456 descriptor.c_str(), bytes, count,
457 average, percent);
458
459 object_bytes_total += bytes;
460 }
461 os << "\n";
462 os << std::flush;
463 CHECK_EQ(object_bytes, object_bytes_total);
464
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800465 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700466 managed_code_bytes, PercentOfObjectBytes(managed_code_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800467 os << StringPrintf("\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700468 managed_to_native_code_bytes,
469 PercentOfObjectBytes(managed_to_native_code_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800470 os << StringPrintf("\tnative_to_managed_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700471 native_to_managed_code_bytes,
472 PercentOfObjectBytes(native_to_managed_code_bytes));
473 os << "\n";
474 os << std::flush;
475
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800476 os << StringPrintf("\tregister_map_bytes = %7zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700477 register_map_bytes, PercentOfObjectBytes(register_map_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800478 os << StringPrintf("\tpc_mapping_table_bytes = %7zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700479 pc_mapping_table_bytes, PercentOfObjectBytes(pc_mapping_table_bytes));
480 os << "\n";
481 os << std::flush;
482
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800483 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700484 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f\n",
485 static_cast<double>(managed_code_bytes)
486 / static_cast<double>(dex_instruction_bytes));
487 os << "\n";
488 os << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700489 }
490 } stats_;
491
492 private:
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700493 const Space& dump_space_;
494 std::ostream& os_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700495
Brian Carlstromaded5f72011-10-07 17:15:04 -0700496 DISALLOW_COPY_AND_ASSIGN(ImageDump);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700497};
498
499int oatdump(int argc, char** argv) {
500 // Skip over argv[0].
501 argv++;
502 argc--;
503
504 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700505 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700506 usage();
507 }
508
Brian Carlstromaded5f72011-10-07 17:15:04 -0700509 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700510 const char* image_filename = NULL;
511 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700512 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700513 std::ostream* os = &std::cout;
514 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700515
516 for (int i = 0; i < argc; i++) {
517 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800518 if (option.starts_with("--oat-file=")) {
519 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700520 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700521 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700522 } else if (option.starts_with("--boot-image=")) {
523 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700524 } else if (option.starts_with("--host-prefix=")) {
525 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700526 } else if (option.starts_with("--output=")) {
527 const char* filename = option.substr(strlen("--output=")).data();
528 out.reset(new std::ofstream(filename));
529 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700530 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700531 usage();
532 }
533 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700534 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700535 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700536 usage();
537 }
538 }
539
Brian Carlstromaded5f72011-10-07 17:15:04 -0700540 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700541 fprintf(stderr, "Either --image or --oat must be specified\n");
542 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700543 }
544
Brian Carlstromaded5f72011-10-07 17:15:04 -0700545 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700546 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
547 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700548 }
549
550 if (oat_filename != NULL) {
551 const OatFile* oat_file = OatFile::Open(oat_filename, "", NULL);
552 if (oat_file == NULL) {
553 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
554 return EXIT_FAILURE;
555 }
556 OatDump::Dump(oat_filename, host_prefix, *os, *oat_file);
557 return EXIT_SUCCESS;
558 }
559
Brian Carlstrom78128a62011-09-15 17:21:19 -0700560 Runtime::Options options;
561 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700562 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700563 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700564 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700565 if (boot_image_filename != NULL) {
566 boot_image_option += "-Ximage:";
567 boot_image_option += boot_image_filename;
568 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -0700569 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700570 if (image_filename != NULL) {
571 image_option += "-Ximage:";
572 image_option += image_filename;
573 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
574 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700575
576 if (!host_prefix.empty()) {
577 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
578 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700579
580 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
581 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700582 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700583 return EXIT_FAILURE;
584 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700585
Ian Rogers30fab402012-01-23 15:43:46 -0800586 ImageSpace* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]->AsImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700587 CHECK(image_space != NULL);
588 const ImageHeader& image_header = image_space->GetImageHeader();
589 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700590 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700591 return EXIT_FAILURE;
592 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700593 ImageDump::Dump(image_filename, host_prefix, *os, *image_space, image_header);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700594 return EXIT_SUCCESS;
595}
596
597} // namespace art
598
599int main(int argc, char** argv) {
600 return art::oatdump(argc, argv);
601}