blob: fe3cf61178877ff460e5ab0d3d03f40d14c2707f [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"
25 " Example: oatdump --dex-file=$ANDROID_PRODUCT_OUT/system/framework/core.jar --image=$ANDROID_PRODUCT_OUT/system/framework/boot.oat --strip-prefix=$ANDROID_PRODUCT_OUT\n"
26 " Example: adb shell oatdump --dex-file=/system/framework/core.jar --image=/system/framework/boot.oat\n"
27 "\n");
28 // TODO: remove this by making image contain boot DexFile information?
29 fprintf(stderr,
30 " --dex-file=<dex-file>: specifies a .dex file location. At least one .dex\n"
31 " file must be specified. \n"
32 " Example: --dex-file=/system/framework/core.jar\n"
33 "\n");
34 fprintf(stderr,
35 " --image=<file>: specifies the required input image filename.\n"
36 " Example: --image=/system/framework/boot.oat\n"
37 "\n");
38 fprintf(stderr,
39 " --boot=<oat-file>: provide the oat file for the boot class path.\n"
40 " Example: --boot=/system/framework/boot.oat\n"
41 "\n");
42 // TODO: remove this by making boot image contain boot DexFile information?
43 fprintf(stderr,
44 " --boot-dex-file=<dex-file>: specifies a .dex file that is part of the boot\n"
45 " image specified with --boot. \n"
46 " Example: --boot-dex-file=/system/framework/core.jar\n"
47 "\n");
48 fprintf(stderr,
49 " --strip-prefix may be used to strip a path prefix from dex file names in the\n"
50 " the generated image to match the target file system layout.\n"
51 " Example: --strip-prefix=out/target/product/crespo\n"
52 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070053 fprintf(stderr,
54 " --output=<file> may be used to send the output to a file.\n"
55 " Example: --output=/tmp/oatdump.txt\n"
56 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070057 exit(EXIT_FAILURE);
58}
59
Ian Rogersff1ed472011-09-20 13:46:24 -070060const char* image_roots_descriptions_[] = {
Brian Carlstrom78128a62011-09-15 17:21:19 -070061 "kJniStubArray",
Ian Rogersff1ed472011-09-20 13:46:24 -070062 "kCalleeSaveMethod"
Brian Carlstrom78128a62011-09-15 17:21:19 -070063};
64
Brian Carlstrom27ec9612011-09-19 20:20:38 -070065class OatDump {
Brian Carlstrom78128a62011-09-15 17:21:19 -070066
Brian Carlstrom27ec9612011-09-19 20:20:38 -070067 public:
Brian Carlstrom916e74e2011-09-23 11:42:01 -070068 static void Dump(const std::string& image_filename,
69 std::ostream& os,
70 Space& image_space,
71 const ImageHeader& image_header) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -070072 os << "MAGIC:\n";
73 os << image_header.GetMagic() << "\n\n";
74
Brian Carlstrom916e74e2011-09-23 11:42:01 -070075 os << "BASE:\n";
76 os << reinterpret_cast<void*>(image_header.GetBaseAddr()) << "\n\n";
77
Brian Carlstrom27ec9612011-09-19 20:20:38 -070078 os << "ROOTS:\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -070079 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -070080 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
81 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
82 os << StringPrintf("%s: %p\n",
83 image_roots_descriptions_[i], image_header.GetImageRoot(image_root));
84 }
85 os << "\n";
86
87 os << "OBJECTS:\n" << std::flush;
88 OatDump state(image_space, os);
89 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
90 DCHECK(heap_bitmap != NULL);
91 heap_bitmap->Walk(OatDump::Callback, &state);
Brian Carlstrom916e74e2011-09-23 11:42:01 -070092 os << "\n";
93
94 os << "STATS:\n" << std::flush;
95 UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), false));
96 state.stats_.file_bytes = file->Length();
97 state.stats_.header_bytes = sizeof(ImageHeader);
98 state.stats_.Dump(os);
99
100 os << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700101 }
102
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700103 private:
104
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700105 OatDump(const Space& dump_space, std::ostream& os) : dump_space_(dump_space), os_(os) {
106 }
107
108 ~OatDump() {
109 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700110
Brian Carlstrom78128a62011-09-15 17:21:19 -0700111 static void Callback(Object* obj, void* arg) {
112 DCHECK(obj != NULL);
113 DCHECK(arg != NULL);
114 OatDump* state = reinterpret_cast<OatDump*>(arg);
115 if (!state->InDumpSpace(obj)) {
116 return;
117 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700118
119 size_t object_bytes = obj->SizeOf();
120 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
121 state->stats_.object_bytes += object_bytes;
122 state->stats_.alignment_bytes += alignment_bytes;
123
Brian Carlstrom78128a62011-09-15 17:21:19 -0700124 std::string summary;
125 StringAppendF(&summary, "%p: ", obj);
126 if (obj->IsClass()) {
127 Class* klass = obj->AsClass();
128 StringAppendF(&summary, "CLASS %s", klass->GetDescriptor()->ToModifiedUtf8().c_str());
Brian Carlstrome10b6972011-09-26 13:49:03 -0700129 std::stringstream ss;
130 ss << " (" << klass->GetStatus() << ")";
131 summary += ss.str();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700132 } else if (obj->IsMethod()) {
133 Method* method = obj->AsMethod();
134 StringAppendF(&summary, "METHOD %s", PrettyMethod(method).c_str());
135 } else if (obj->IsField()) {
136 Field* field = obj->AsField();
137 Class* type = field->GetType();
138 std::string type_string;
139 type_string += (type == NULL) ? "<UNKNOWN>" : type->GetDescriptor()->ToModifiedUtf8();
140 StringAppendF(&summary, "FIELD %s", PrettyField(field).c_str());
141 } else if (obj->IsArrayInstance()) {
142 StringAppendF(&summary, "ARRAY %d", obj->AsArray()->GetLength());
143 } else if (obj->IsString()) {
144 StringAppendF(&summary, "STRING %s", obj->AsString()->ToModifiedUtf8().c_str());
145 } else {
146 StringAppendF(&summary, "OBJECT");
147 }
148 StringAppendF(&summary, "\n");
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700149 std::string descriptor = obj->GetClass()->GetDescriptor()->ToModifiedUtf8();
150 StringAppendF(&summary, "\tclass %p: %s\n", obj->GetClass(), descriptor.c_str());
151 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
152 state->stats_.descriptor_to_count[descriptor] += 1;
153 // StringAppendF(&summary, "\tsize %d (alignment padding %d)\n",
154 // object_bytes, RoundUp(object_bytes, kObjectAlignment) - object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700155 if (obj->IsMethod()) {
156 Method* method = obj->AsMethod();
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700157 if (!method->IsPhony()) {
Brian Carlstrome10b6972011-09-26 13:49:03 -0700158 const ByteArray* code = method->GetCodeArray();
159 const int8_t* code_base = NULL;
160 const int8_t* code_limit = NULL;
161 if (code != NULL) {
162 size_t code_bytes = code->GetLength();
163 code_base = code->GetData();
164 code_limit = code_base + code_bytes;
165 if (method->IsNative()) {
166 state->stats_.managed_to_native_code_bytes += code_bytes;
167 } else {
168 state->stats_.managed_code_bytes += code_bytes;
169 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700170 }
Brian Carlstrome10b6972011-09-26 13:49:03 -0700171 StringAppendF(&summary, "\tCODE %p-%p\n", code_base, code_limit);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700172
Ian Rogersff1ed472011-09-20 13:46:24 -0700173 const ByteArray* invoke = method->GetInvokeStubArray();
Brian Carlstrome10b6972011-09-26 13:49:03 -0700174 const int8_t* invoke_base = NULL;
175 const int8_t* invoke_limit = NULL;
176 if (invoke != NULL) {
177 size_t native_to_managed_code_bytes = invoke->GetLength();
178 invoke_base = invoke->GetData();
179 invoke_limit = invoke_base + native_to_managed_code_bytes;
180 state->stats_.native_to_managed_code_bytes += native_to_managed_code_bytes;
181 StringAppendF(&summary, "\tJNI STUB %p-%p\n", invoke_base, invoke_limit);
182 }
Ian Rogersff1ed472011-09-20 13:46:24 -0700183 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700184 if (method->IsNative()) {
185 if (method->IsRegistered()) {
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700186 StringAppendF(&summary, "\tNATIVE REGISTERED %p\n", method->GetNativeMethod());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700187 } else {
188 StringAppendF(&summary, "\tNATIVE UNREGISTERED\n");
189 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700190 DCHECK(method->GetRegisterMapHeader() == NULL);
191 DCHECK(method->GetRegisterMapData() == NULL);
192 DCHECK(method->GetMappingTable() == NULL);
193 } else if (method->IsAbstract()) {
194 StringAppendF(&summary, "\tABSTRACT\n");
195 DCHECK(method->GetRegisterMapHeader() == NULL);
196 DCHECK(method->GetRegisterMapData() == NULL);
197 DCHECK(method->GetMappingTable() == NULL);
198 } else if (method->IsPhony()) {
199 StringAppendF(&summary, "\tPHONY\n");
200 DCHECK(method->GetRegisterMapHeader() == NULL);
201 DCHECK(method->GetRegisterMapData() == NULL);
202 DCHECK(method->GetMappingTable() == NULL);
203 } else {
204 size_t register_map_bytes = (method->GetRegisterMapHeader()->GetLength() +
205 method->GetRegisterMapData()->GetLength());
206 state->stats_.register_map_bytes += register_map_bytes;
207
Brian Carlstrome10b6972011-09-26 13:49:03 -0700208 if (method->GetMappingTable() != NULL) {
209 size_t pc_mapping_table_bytes = method->GetMappingTable()->GetLength();
210 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
211 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700212
213 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
214 class DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
215 const DexFile& dex_file = class_linker->FindDexFile(dex_cache);
216 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
217 size_t dex_instruction_bytes = code_item->insns_size_ * 2;
218 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700219 }
220 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700221 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700222 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700223
224 bool InDumpSpace(const Object* object) {
225 const byte* o = reinterpret_cast<const byte*>(object);
226 return (o >= dump_space_.GetBase() && o < dump_space_.GetLimit());
227 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700228
229 public:
230 struct Stats {
231 size_t file_bytes;
232
233 size_t header_bytes;
234 size_t object_bytes;
235 size_t alignment_bytes;
236
237 size_t managed_code_bytes;
238 size_t managed_to_native_code_bytes;
239 size_t native_to_managed_code_bytes;
240
241 size_t register_map_bytes;
242 size_t pc_mapping_table_bytes;
243
244 size_t dex_instruction_bytes;
245
246 Stats()
247 : file_bytes(0),
248 header_bytes(0),
249 object_bytes(0),
250 alignment_bytes(0),
251 managed_code_bytes(0),
252 managed_to_native_code_bytes(0),
253 native_to_managed_code_bytes(0),
254 register_map_bytes(0),
255 pc_mapping_table_bytes(0),
256 dex_instruction_bytes(0) {}
257
258 typedef std::tr1::unordered_map<std::string,size_t> TableBytes;
259 TableBytes descriptor_to_bytes;
260
261 typedef std::tr1::unordered_map<std::string,size_t> TableCount;
262 TableCount descriptor_to_count;
263
264 double PercentOfFileBytes(size_t size) {
265 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
266 }
267
268 double PercentOfObjectBytes(size_t size) {
269 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
270 }
271
272 void Dump(std::ostream& os) {
273 os << StringPrintf("\tfile_bytes = %d\n", file_bytes);
274 os << "\n";
275
276 os << "\tfile_bytes = header_bytes + object_bytes + alignment_bytes\n";
277 os << StringPrintf("\theader_bytes = %10d (%2.0f%% of file_bytes)\n",
278 header_bytes, PercentOfFileBytes(header_bytes));
279 os << StringPrintf("\tobject_bytes = %10d (%2.0f%% of file_bytes)\n",
280 object_bytes, PercentOfFileBytes(object_bytes));
281 os << StringPrintf("\talignment_bytes = %10d (%2.0f%% of file_bytes)\n",
282 alignment_bytes, PercentOfFileBytes(alignment_bytes));
283 os << "\n";
284 os << std::flush;
285 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
286
287 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
288 size_t object_bytes_total = 0;
289 typedef TableBytes::const_iterator It; // TODO: C++0x auto
290 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
291 const std::string& descriptor = it->first;
292 size_t bytes = it->second;
293 size_t count = descriptor_to_count[descriptor];
294 double average = static_cast<double>(bytes) / static_cast<double>(count);
295 double percent = PercentOfObjectBytes(bytes);
Brian Carlstromf153fe12011-09-27 16:27:12 -0700296 os << StringPrintf("\t%32s %8d bytes %6d instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700297 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
298 descriptor.c_str(), bytes, count,
299 average, percent);
300
301 object_bytes_total += bytes;
302 }
303 os << "\n";
304 os << std::flush;
305 CHECK_EQ(object_bytes, object_bytes_total);
306
307 os << StringPrintf("\tmanaged_code_bytes = %8d (%2.0f%% of object_bytes)\n",
308 managed_code_bytes, PercentOfObjectBytes(managed_code_bytes));
309 os << StringPrintf("\tmanaged_to_native_code_bytes = %8d (%2.0f%% of object_bytes)\n",
310 managed_to_native_code_bytes,
311 PercentOfObjectBytes(managed_to_native_code_bytes));
312 os << StringPrintf("\tnative_to_managed_code_bytes = %8d (%2.0f%% of object_bytes)\n",
313 native_to_managed_code_bytes,
314 PercentOfObjectBytes(native_to_managed_code_bytes));
315 os << "\n";
316 os << std::flush;
317
318 os << StringPrintf("\tregister_map_bytes = %7d (%2.0f%% of object_bytes)\n",
319 register_map_bytes, PercentOfObjectBytes(register_map_bytes));
320 os << StringPrintf("\tpc_mapping_table_bytes = %7d (%2.0f%% of object_bytes)\n",
321 pc_mapping_table_bytes, PercentOfObjectBytes(pc_mapping_table_bytes));
322 os << "\n";
323 os << std::flush;
324
325 os << StringPrintf("\tdex_instruction_bytes = %d\n", dex_instruction_bytes);
326 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f\n",
327 static_cast<double>(managed_code_bytes)
328 / static_cast<double>(dex_instruction_bytes));
329 os << "\n";
330 os << std::flush;
331
332 }
333 } stats_;
334
335 private:
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700336 const Space& dump_space_;
337 std::ostream& os_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700338
339 DISALLOW_COPY_AND_ASSIGN(OatDump);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700340};
341
342int oatdump(int argc, char** argv) {
343 // Skip over argv[0].
344 argv++;
345 argc--;
346
347 if (argc == 0) {
348 fprintf(stderr, "no arguments specified\n");
349 usage();
350 }
351
352 std::vector<const char*> dex_filenames;
353 const char* image_filename = NULL;
354 const char* boot_image_filename = NULL;
355 std::vector<const char*> boot_dex_filenames;
356 std::string strip_location_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700357 std::ostream* os = &std::cout;
358 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700359
360 for (int i = 0; i < argc; i++) {
361 const StringPiece option(argv[i]);
362 if (option.starts_with("--dex-file=")) {
363 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
364 } else if (option.starts_with("--image=")) {
365 image_filename = option.substr(strlen("--image=")).data();
366 } else if (option.starts_with("--boot=")) {
367 boot_image_filename = option.substr(strlen("--boot=")).data();
368 } else if (option.starts_with("--boot-dex-file=")) {
369 boot_dex_filenames.push_back(option.substr(strlen("--boot-dex-file=")).data());
370 } else if (option.starts_with("--strip-prefix=")) {
371 strip_location_prefix = option.substr(strlen("--strip-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700372 } else if (option.starts_with("--output=")) {
373 const char* filename = option.substr(strlen("--output=")).data();
374 out.reset(new std::ofstream(filename));
375 if (!out->good()) {
376 fprintf(stderr, "failed to open output filename %s\n", filename);
377 usage();
378 }
379 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700380 } else {
381 fprintf(stderr, "unknown argument %s\n", option.data());
382 usage();
383 }
384 }
385
386 if (image_filename == NULL) {
387 fprintf(stderr, "--image file name not specified\n");
388 return EXIT_FAILURE;
389 }
390
391 if (dex_filenames.empty()) {
392 fprintf(stderr, "no --dex-file values specified\n");
393 return EXIT_FAILURE;
394 }
395
396 if (boot_image_filename != NULL && boot_dex_filenames.empty()) {
397 fprintf(stderr, "no --boot-dex-file values specified with --boot\n");
398 return EXIT_FAILURE;
399 }
400
401 std::vector<const DexFile*> dex_files;
402 DexFile::OpenDexFiles(dex_filenames, dex_files, strip_location_prefix);
403
404 std::vector<const DexFile*> boot_dex_files;
405 DexFile::OpenDexFiles(boot_dex_filenames, boot_dex_files, strip_location_prefix);
406
407 Runtime::Options options;
408 std::string image_option;
409 std::string boot_image_option;
410 if (boot_image_filename == NULL) {
411 // if we don't have multiple images, pass the main one as the boot to match dex2oat
412 boot_image_filename = image_filename;
413 boot_dex_files = dex_files;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700414 dex_files.clear();
415 } else {
416 image_option += "-Ximage:";
417 image_option += image_filename;
418 options.push_back(std::make_pair("classpath", &dex_files));
419 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
420 }
421 boot_image_option += "-Xbootimage:";
422 boot_image_option += boot_image_filename;
423 options.push_back(std::make_pair("bootclasspath", &boot_dex_files));
424 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
425
426 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
427 if (runtime.get() == NULL) {
428 fprintf(stderr, "could not create runtime\n");
429 return EXIT_FAILURE;
430 }
431 ClassLinker* class_linker = runtime->GetClassLinker();
432 for (size_t i = 0; i < dex_files.size(); i++) {
433 class_linker->RegisterDexFile(*dex_files[i]);
434 }
435
436 Space* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2];
437 CHECK(image_space != NULL);
438 const ImageHeader& image_header = image_space->GetImageHeader();
439 if (!image_header.IsValid()) {
440 fprintf(stderr, "invalid image header %s\n", image_filename);
441 return EXIT_FAILURE;
442 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700443 OatDump::Dump(image_filename, *os, *image_space, image_header);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700444 return EXIT_SUCCESS;
445}
446
447} // namespace art
448
449int main(int argc, char** argv) {
450 return art::oatdump(argc, argv);
451}