blob: 8113c2bfc2d080cca303b7269a9d09dc0fd54f92 [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 Carlstrom58ae9412011-10-04 00:56:06 -070025 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n"
26 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070027 "\n");
28 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070029 " --image=<file.art>: specifies the required input image filename.\n"
30 " Example: --image=/system/framework/boot.art\n"
31 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070032 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070033 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
34 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070035 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070036 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070037 " --host-prefix may be used to translate host paths to target paths during\n"
38 " cross compilation.\n"
39 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070040 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070041 fprintf(stderr,
42 " --output=<file> may be used to send the output to a file.\n"
43 " Example: --output=/tmp/oatdump.txt\n"
44 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070045 exit(EXIT_FAILURE);
46}
47
Ian Rogersff1ed472011-09-20 13:46:24 -070048const char* image_roots_descriptions_[] = {
Brian Carlstrom78128a62011-09-15 17:21:19 -070049 "kJniStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070050 "kAbstractMethodErrorStubArray",
Ian Rogersad25ac52011-10-04 19:13:33 -070051 "kInstanceResolutionStubArray",
52 "kStaticResolutionStubArray",
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070053 "kUnknownMethodResolutionStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070054 "kCalleeSaveMethod",
55 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070056 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070057 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070058};
59
Brian Carlstrom27ec9612011-09-19 20:20:38 -070060class OatDump {
Brian Carlstrom78128a62011-09-15 17:21:19 -070061
Brian Carlstrom27ec9612011-09-19 20:20:38 -070062 public:
Brian Carlstrom916e74e2011-09-23 11:42:01 -070063 static void Dump(const std::string& image_filename,
64 std::ostream& os,
65 Space& image_space,
66 const ImageHeader& image_header) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -070067 os << "MAGIC:\n";
68 os << image_header.GetMagic() << "\n\n";
69
Brian Carlstrome24fa612011-09-29 00:53:55 -070070 os << "IMAGE BASE:\n";
71 os << reinterpret_cast<void*>(image_header.GetImageBaseAddr()) << "\n\n";
72
73 os << "OAT BASE:\n";
74 os << reinterpret_cast<void*>(image_header.GetOatBaseAddr()) << "\n\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -070075
Brian Carlstrom27ec9612011-09-19 20:20:38 -070076 os << "ROOTS:\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -070077 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -070078 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
79 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -070080 const char* image_root_description = image_roots_descriptions_[i];
81 Object* image_root_object = image_header.GetImageRoot(image_root);
82 os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
83 if (image_root_object->IsObjectArray()) {
84 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
85 ObjectArray<Object>* image_root_object_array
86 = down_cast<ObjectArray<Object>*>(image_root_object);
87 // = image_root_object->AsObjectArray<Object>();
88 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
89 os << StringPrintf("\t%d: %p\n", i, image_root_object_array->Get(i));
90 }
91 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -070092 }
93 os << "\n";
94
95 os << "OBJECTS:\n" << std::flush;
96 OatDump state(image_space, os);
97 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
98 DCHECK(heap_bitmap != NULL);
99 heap_bitmap->Walk(OatDump::Callback, &state);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700100 os << "\n";
101
102 os << "STATS:\n" << std::flush;
103 UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), false));
104 state.stats_.file_bytes = file->Length();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700105 size_t header_bytes = sizeof(ImageHeader);
106 state.stats_.header_bytes = header_bytes;
107 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
108 state.stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700109 state.stats_.Dump(os);
110
111 os << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700112 }
113
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700114 private:
115
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700116 OatDump(const Space& dump_space, std::ostream& os) : dump_space_(dump_space), os_(os) {
117 }
118
119 ~OatDump() {
120 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700121
Brian Carlstrom78128a62011-09-15 17:21:19 -0700122 static void Callback(Object* obj, void* arg) {
123 DCHECK(obj != NULL);
124 DCHECK(arg != NULL);
125 OatDump* state = reinterpret_cast<OatDump*>(arg);
126 if (!state->InDumpSpace(obj)) {
127 return;
128 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700129
130 size_t object_bytes = obj->SizeOf();
131 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
132 state->stats_.object_bytes += object_bytes;
133 state->stats_.alignment_bytes += alignment_bytes;
134
Brian Carlstrom78128a62011-09-15 17:21:19 -0700135 std::string summary;
136 StringAppendF(&summary, "%p: ", obj);
137 if (obj->IsClass()) {
138 Class* klass = obj->AsClass();
139 StringAppendF(&summary, "CLASS %s", klass->GetDescriptor()->ToModifiedUtf8().c_str());
Brian Carlstrome10b6972011-09-26 13:49:03 -0700140 std::stringstream ss;
141 ss << " (" << klass->GetStatus() << ")";
142 summary += ss.str();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700143 } else if (obj->IsMethod()) {
144 Method* method = obj->AsMethod();
145 StringAppendF(&summary, "METHOD %s", PrettyMethod(method).c_str());
146 } else if (obj->IsField()) {
147 Field* field = obj->AsField();
148 Class* type = field->GetType();
149 std::string type_string;
150 type_string += (type == NULL) ? "<UNKNOWN>" : type->GetDescriptor()->ToModifiedUtf8();
151 StringAppendF(&summary, "FIELD %s", PrettyField(field).c_str());
152 } else if (obj->IsArrayInstance()) {
153 StringAppendF(&summary, "ARRAY %d", obj->AsArray()->GetLength());
154 } else if (obj->IsString()) {
155 StringAppendF(&summary, "STRING %s", obj->AsString()->ToModifiedUtf8().c_str());
156 } else {
157 StringAppendF(&summary, "OBJECT");
158 }
159 StringAppendF(&summary, "\n");
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700160 std::string descriptor = obj->GetClass()->GetDescriptor()->ToModifiedUtf8();
161 StringAppendF(&summary, "\tclass %p: %s\n", obj->GetClass(), descriptor.c_str());
162 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
163 state->stats_.descriptor_to_count[descriptor] += 1;
164 // StringAppendF(&summary, "\tsize %d (alignment padding %d)\n",
165 // object_bytes, RoundUp(object_bytes, kObjectAlignment) - object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700166 if (obj->IsMethod()) {
167 Method* method = obj->AsMethod();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700168 if (!method->IsCalleeSaveMethod()) {
169 const int8_t* code =reinterpret_cast<const int8_t*>(method->GetCode());
170 StringAppendF(&summary, "\tCODE %p\n", code);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700171
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700172 const Method::InvokeStub* invoke_stub = method->GetInvokeStub();
173 StringAppendF(&summary, "\tJNI STUB %p\n", invoke_stub);
Ian Rogersff1ed472011-09-20 13:46:24 -0700174 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700175 if (method->IsNative()) {
176 if (method->IsRegistered()) {
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700177 StringAppendF(&summary, "\tNATIVE REGISTERED %p\n", method->GetNativeMethod());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700178 } else {
179 StringAppendF(&summary, "\tNATIVE UNREGISTERED\n");
180 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700181 DCHECK(method->GetRegisterMapHeader() == NULL) << PrettyMethod(method);
182 DCHECK(method->GetRegisterMapData() == NULL) << PrettyMethod(method);
183 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700184 } else if (method->IsAbstract()) {
185 StringAppendF(&summary, "\tABSTRACT\n");
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700186 DCHECK(method->GetRegisterMapHeader() == NULL) << PrettyMethod(method);
187 DCHECK(method->GetRegisterMapData() == NULL) << PrettyMethod(method);
188 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
189 } else if (method->IsCalleeSaveMethod()) {
190 StringAppendF(&summary, "\tCALLEE SAVE METHOD\n");
191 DCHECK(method->GetRegisterMapHeader() == NULL) << PrettyMethod(method);
192 DCHECK(method->GetRegisterMapData() == NULL) << PrettyMethod(method);
193 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700194 } else {
195 size_t register_map_bytes = (method->GetRegisterMapHeader()->GetLength() +
196 method->GetRegisterMapData()->GetLength());
197 state->stats_.register_map_bytes += register_map_bytes;
198
Brian Carlstrome10b6972011-09-26 13:49:03 -0700199 if (method->GetMappingTable() != NULL) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700200 size_t pc_mapping_table_bytes = method->GetMappingTableLength();
Brian Carlstrome10b6972011-09-26 13:49:03 -0700201 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
202 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700203
204 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
205 class DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
206 const DexFile& dex_file = class_linker->FindDexFile(dex_cache);
207 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
208 size_t dex_instruction_bytes = code_item->insns_size_ * 2;
209 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700210 }
211 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700212 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700213 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700214
215 bool InDumpSpace(const Object* object) {
216 const byte* o = reinterpret_cast<const byte*>(object);
217 return (o >= dump_space_.GetBase() && o < dump_space_.GetLimit());
218 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700219
220 public:
221 struct Stats {
222 size_t file_bytes;
223
224 size_t header_bytes;
225 size_t object_bytes;
226 size_t alignment_bytes;
227
228 size_t managed_code_bytes;
229 size_t managed_to_native_code_bytes;
230 size_t native_to_managed_code_bytes;
231
232 size_t register_map_bytes;
233 size_t pc_mapping_table_bytes;
234
235 size_t dex_instruction_bytes;
236
237 Stats()
238 : file_bytes(0),
239 header_bytes(0),
240 object_bytes(0),
241 alignment_bytes(0),
242 managed_code_bytes(0),
243 managed_to_native_code_bytes(0),
244 native_to_managed_code_bytes(0),
245 register_map_bytes(0),
246 pc_mapping_table_bytes(0),
247 dex_instruction_bytes(0) {}
248
249 typedef std::tr1::unordered_map<std::string,size_t> TableBytes;
250 TableBytes descriptor_to_bytes;
251
252 typedef std::tr1::unordered_map<std::string,size_t> TableCount;
253 TableCount descriptor_to_count;
254
255 double PercentOfFileBytes(size_t size) {
256 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
257 }
258
259 double PercentOfObjectBytes(size_t size) {
260 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
261 }
262
263 void Dump(std::ostream& os) {
264 os << StringPrintf("\tfile_bytes = %d\n", file_bytes);
265 os << "\n";
266
267 os << "\tfile_bytes = header_bytes + object_bytes + alignment_bytes\n";
268 os << StringPrintf("\theader_bytes = %10d (%2.0f%% of file_bytes)\n",
269 header_bytes, PercentOfFileBytes(header_bytes));
270 os << StringPrintf("\tobject_bytes = %10d (%2.0f%% of file_bytes)\n",
271 object_bytes, PercentOfFileBytes(object_bytes));
272 os << StringPrintf("\talignment_bytes = %10d (%2.0f%% of file_bytes)\n",
273 alignment_bytes, PercentOfFileBytes(alignment_bytes));
274 os << "\n";
275 os << std::flush;
276 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
277
278 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
279 size_t object_bytes_total = 0;
280 typedef TableBytes::const_iterator It; // TODO: C++0x auto
281 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
282 const std::string& descriptor = it->first;
283 size_t bytes = it->second;
284 size_t count = descriptor_to_count[descriptor];
285 double average = static_cast<double>(bytes) / static_cast<double>(count);
286 double percent = PercentOfObjectBytes(bytes);
Brian Carlstromf153fe12011-09-27 16:27:12 -0700287 os << StringPrintf("\t%32s %8d bytes %6d instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700288 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
289 descriptor.c_str(), bytes, count,
290 average, percent);
291
292 object_bytes_total += bytes;
293 }
294 os << "\n";
295 os << std::flush;
296 CHECK_EQ(object_bytes, object_bytes_total);
297
298 os << StringPrintf("\tmanaged_code_bytes = %8d (%2.0f%% of object_bytes)\n",
299 managed_code_bytes, PercentOfObjectBytes(managed_code_bytes));
300 os << StringPrintf("\tmanaged_to_native_code_bytes = %8d (%2.0f%% of object_bytes)\n",
301 managed_to_native_code_bytes,
302 PercentOfObjectBytes(managed_to_native_code_bytes));
303 os << StringPrintf("\tnative_to_managed_code_bytes = %8d (%2.0f%% of object_bytes)\n",
304 native_to_managed_code_bytes,
305 PercentOfObjectBytes(native_to_managed_code_bytes));
306 os << "\n";
307 os << std::flush;
308
309 os << StringPrintf("\tregister_map_bytes = %7d (%2.0f%% of object_bytes)\n",
310 register_map_bytes, PercentOfObjectBytes(register_map_bytes));
311 os << StringPrintf("\tpc_mapping_table_bytes = %7d (%2.0f%% of object_bytes)\n",
312 pc_mapping_table_bytes, PercentOfObjectBytes(pc_mapping_table_bytes));
313 os << "\n";
314 os << std::flush;
315
316 os << StringPrintf("\tdex_instruction_bytes = %d\n", dex_instruction_bytes);
317 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f\n",
318 static_cast<double>(managed_code_bytes)
319 / static_cast<double>(dex_instruction_bytes));
320 os << "\n";
321 os << std::flush;
322
323 }
324 } stats_;
325
326 private:
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700327 const Space& dump_space_;
328 std::ostream& os_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700329
330 DISALLOW_COPY_AND_ASSIGN(OatDump);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700331};
332
333int oatdump(int argc, char** argv) {
334 // Skip over argv[0].
335 argv++;
336 argc--;
337
338 if (argc == 0) {
339 fprintf(stderr, "no arguments specified\n");
340 usage();
341 }
342
Brian Carlstrom78128a62011-09-15 17:21:19 -0700343 const char* image_filename = NULL;
344 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700345 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700346 std::ostream* os = &std::cout;
347 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700348
349 for (int i = 0; i < argc; i++) {
350 const StringPiece option(argv[i]);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700351 if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700352 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700353 } else if (option.starts_with("--boot-image=")) {
354 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700355 } else if (option.starts_with("--host-prefix=")) {
356 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700357 } else if (option.starts_with("--output=")) {
358 const char* filename = option.substr(strlen("--output=")).data();
359 out.reset(new std::ofstream(filename));
360 if (!out->good()) {
361 fprintf(stderr, "failed to open output filename %s\n", filename);
362 usage();
363 }
364 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700365 } else {
366 fprintf(stderr, "unknown argument %s\n", option.data());
367 usage();
368 }
369 }
370
371 if (image_filename == NULL) {
372 fprintf(stderr, "--image file name not specified\n");
373 return EXIT_FAILURE;
374 }
375
Brian Carlstrom78128a62011-09-15 17:21:19 -0700376 Runtime::Options options;
377 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700378 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700379 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700380 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700381 if (boot_image_filename != NULL) {
382 boot_image_option += "-Ximage:";
383 boot_image_option += boot_image_filename;
384 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -0700385 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700386 image_option += "-Ximage:";
387 image_option += image_filename;
388 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
389
390 if (!host_prefix.empty()) {
391 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
392 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700393
394 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
395 if (runtime.get() == NULL) {
396 fprintf(stderr, "could not create runtime\n");
397 return EXIT_FAILURE;
398 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700399
400 Space* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2];
401 CHECK(image_space != NULL);
402 const ImageHeader& image_header = image_space->GetImageHeader();
403 if (!image_header.IsValid()) {
404 fprintf(stderr, "invalid image header %s\n", image_filename);
405 return EXIT_FAILURE;
406 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700407 OatDump::Dump(image_filename, *os, *image_space, image_header);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700408 return EXIT_SUCCESS;
409}
410
411} // namespace art
412
413int main(int argc, char** argv) {
414 return art::oatdump(argc, argv);
415}