blob: 4cb9cf0b681bc793d3cd908a69454c6add909888 [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());
129 } else if (obj->IsMethod()) {
130 Method* method = obj->AsMethod();
131 StringAppendF(&summary, "METHOD %s", PrettyMethod(method).c_str());
132 } else if (obj->IsField()) {
133 Field* field = obj->AsField();
134 Class* type = field->GetType();
135 std::string type_string;
136 type_string += (type == NULL) ? "<UNKNOWN>" : type->GetDescriptor()->ToModifiedUtf8();
137 StringAppendF(&summary, "FIELD %s", PrettyField(field).c_str());
138 } else if (obj->IsArrayInstance()) {
139 StringAppendF(&summary, "ARRAY %d", obj->AsArray()->GetLength());
140 } else if (obj->IsString()) {
141 StringAppendF(&summary, "STRING %s", obj->AsString()->ToModifiedUtf8().c_str());
142 } else {
143 StringAppendF(&summary, "OBJECT");
144 }
145 StringAppendF(&summary, "\n");
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700146 std::string descriptor = obj->GetClass()->GetDescriptor()->ToModifiedUtf8();
147 StringAppendF(&summary, "\tclass %p: %s\n", obj->GetClass(), descriptor.c_str());
148 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
149 state->stats_.descriptor_to_count[descriptor] += 1;
150 // StringAppendF(&summary, "\tsize %d (alignment padding %d)\n",
151 // object_bytes, RoundUp(object_bytes, kObjectAlignment) - object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700152 if (obj->IsMethod()) {
153 Method* method = obj->AsMethod();
154 const ByteArray* code = method->GetCodeArray();
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700155 if (!method->IsPhony()) {
156 size_t code_bytes = code->GetLength();
157 if (method->IsNative()) {
158 state->stats_.managed_to_native_code_bytes += code_bytes;
159 } else {
160 state->stats_.managed_code_bytes += code_bytes;
161 }
162 StringAppendF(&summary, "\tCODE %p-%p\n", code->GetData(), code->GetData() + code_bytes);
163
Ian Rogersff1ed472011-09-20 13:46:24 -0700164 const ByteArray* invoke = method->GetInvokeStubArray();
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700165 size_t native_to_managed_code_bytes = invoke->GetLength();
166 state->stats_.native_to_managed_code_bytes += native_to_managed_code_bytes;
167 StringAppendF(&summary, "\tJNI STUB %p-%p\n",
168 invoke->GetData(), invoke->GetData() + native_to_managed_code_bytes);
Ian Rogersff1ed472011-09-20 13:46:24 -0700169 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700170 if (method->IsNative()) {
171 if (method->IsRegistered()) {
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700172 StringAppendF(&summary, "\tNATIVE REGISTERED %p\n", method->GetNativeMethod());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700173 } else {
174 StringAppendF(&summary, "\tNATIVE UNREGISTERED\n");
175 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700176 DCHECK(method->GetRegisterMapHeader() == NULL);
177 DCHECK(method->GetRegisterMapData() == NULL);
178 DCHECK(method->GetMappingTable() == NULL);
179 } else if (method->IsAbstract()) {
180 StringAppendF(&summary, "\tABSTRACT\n");
181 DCHECK(method->GetRegisterMapHeader() == NULL);
182 DCHECK(method->GetRegisterMapData() == NULL);
183 DCHECK(method->GetMappingTable() == NULL);
184 } else if (method->IsPhony()) {
185 StringAppendF(&summary, "\tPHONY\n");
186 DCHECK(method->GetRegisterMapHeader() == NULL);
187 DCHECK(method->GetRegisterMapData() == NULL);
188 DCHECK(method->GetMappingTable() == NULL);
189 } else {
190 size_t register_map_bytes = (method->GetRegisterMapHeader()->GetLength() +
191 method->GetRegisterMapData()->GetLength());
192 state->stats_.register_map_bytes += register_map_bytes;
193
194 size_t pc_mapping_table_bytes = method->GetMappingTable()->GetLength();
195 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
196
197 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
198 class DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
199 const DexFile& dex_file = class_linker->FindDexFile(dex_cache);
200 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
201 size_t dex_instruction_bytes = code_item->insns_size_ * 2;
202 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700203 }
204 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700205 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700206 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700207
208 bool InDumpSpace(const Object* object) {
209 const byte* o = reinterpret_cast<const byte*>(object);
210 return (o >= dump_space_.GetBase() && o < dump_space_.GetLimit());
211 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700212
213 public:
214 struct Stats {
215 size_t file_bytes;
216
217 size_t header_bytes;
218 size_t object_bytes;
219 size_t alignment_bytes;
220
221 size_t managed_code_bytes;
222 size_t managed_to_native_code_bytes;
223 size_t native_to_managed_code_bytes;
224
225 size_t register_map_bytes;
226 size_t pc_mapping_table_bytes;
227
228 size_t dex_instruction_bytes;
229
230 Stats()
231 : file_bytes(0),
232 header_bytes(0),
233 object_bytes(0),
234 alignment_bytes(0),
235 managed_code_bytes(0),
236 managed_to_native_code_bytes(0),
237 native_to_managed_code_bytes(0),
238 register_map_bytes(0),
239 pc_mapping_table_bytes(0),
240 dex_instruction_bytes(0) {}
241
242 typedef std::tr1::unordered_map<std::string,size_t> TableBytes;
243 TableBytes descriptor_to_bytes;
244
245 typedef std::tr1::unordered_map<std::string,size_t> TableCount;
246 TableCount descriptor_to_count;
247
248 double PercentOfFileBytes(size_t size) {
249 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
250 }
251
252 double PercentOfObjectBytes(size_t size) {
253 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
254 }
255
256 void Dump(std::ostream& os) {
257 os << StringPrintf("\tfile_bytes = %d\n", file_bytes);
258 os << "\n";
259
260 os << "\tfile_bytes = header_bytes + object_bytes + alignment_bytes\n";
261 os << StringPrintf("\theader_bytes = %10d (%2.0f%% of file_bytes)\n",
262 header_bytes, PercentOfFileBytes(header_bytes));
263 os << StringPrintf("\tobject_bytes = %10d (%2.0f%% of file_bytes)\n",
264 object_bytes, PercentOfFileBytes(object_bytes));
265 os << StringPrintf("\talignment_bytes = %10d (%2.0f%% of file_bytes)\n",
266 alignment_bytes, PercentOfFileBytes(alignment_bytes));
267 os << "\n";
268 os << std::flush;
269 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
270
271 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
272 size_t object_bytes_total = 0;
273 typedef TableBytes::const_iterator It; // TODO: C++0x auto
274 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
275 const std::string& descriptor = it->first;
276 size_t bytes = it->second;
277 size_t count = descriptor_to_count[descriptor];
278 double average = static_cast<double>(bytes) / static_cast<double>(count);
279 double percent = PercentOfObjectBytes(bytes);
280 os << StringPrintf("\t%28s %8d bytes %6d instances "
281 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
282 descriptor.c_str(), bytes, count,
283 average, percent);
284
285 object_bytes_total += bytes;
286 }
287 os << "\n";
288 os << std::flush;
289 CHECK_EQ(object_bytes, object_bytes_total);
290
291 os << StringPrintf("\tmanaged_code_bytes = %8d (%2.0f%% of object_bytes)\n",
292 managed_code_bytes, PercentOfObjectBytes(managed_code_bytes));
293 os << StringPrintf("\tmanaged_to_native_code_bytes = %8d (%2.0f%% of object_bytes)\n",
294 managed_to_native_code_bytes,
295 PercentOfObjectBytes(managed_to_native_code_bytes));
296 os << StringPrintf("\tnative_to_managed_code_bytes = %8d (%2.0f%% of object_bytes)\n",
297 native_to_managed_code_bytes,
298 PercentOfObjectBytes(native_to_managed_code_bytes));
299 os << "\n";
300 os << std::flush;
301
302 os << StringPrintf("\tregister_map_bytes = %7d (%2.0f%% of object_bytes)\n",
303 register_map_bytes, PercentOfObjectBytes(register_map_bytes));
304 os << StringPrintf("\tpc_mapping_table_bytes = %7d (%2.0f%% of object_bytes)\n",
305 pc_mapping_table_bytes, PercentOfObjectBytes(pc_mapping_table_bytes));
306 os << "\n";
307 os << std::flush;
308
309 os << StringPrintf("\tdex_instruction_bytes = %d\n", dex_instruction_bytes);
310 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f\n",
311 static_cast<double>(managed_code_bytes)
312 / static_cast<double>(dex_instruction_bytes));
313 os << "\n";
314 os << std::flush;
315
316 }
317 } stats_;
318
319 private:
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700320 const Space& dump_space_;
321 std::ostream& os_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700322
323 DISALLOW_COPY_AND_ASSIGN(OatDump);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700324};
325
326int oatdump(int argc, char** argv) {
327 // Skip over argv[0].
328 argv++;
329 argc--;
330
331 if (argc == 0) {
332 fprintf(stderr, "no arguments specified\n");
333 usage();
334 }
335
336 std::vector<const char*> dex_filenames;
337 const char* image_filename = NULL;
338 const char* boot_image_filename = NULL;
339 std::vector<const char*> boot_dex_filenames;
340 std::string strip_location_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700341 std::ostream* os = &std::cout;
342 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700343
344 for (int i = 0; i < argc; i++) {
345 const StringPiece option(argv[i]);
346 if (option.starts_with("--dex-file=")) {
347 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
348 } else if (option.starts_with("--image=")) {
349 image_filename = option.substr(strlen("--image=")).data();
350 } else if (option.starts_with("--boot=")) {
351 boot_image_filename = option.substr(strlen("--boot=")).data();
352 } else if (option.starts_with("--boot-dex-file=")) {
353 boot_dex_filenames.push_back(option.substr(strlen("--boot-dex-file=")).data());
354 } else if (option.starts_with("--strip-prefix=")) {
355 strip_location_prefix = option.substr(strlen("--strip-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700356 } else if (option.starts_with("--output=")) {
357 const char* filename = option.substr(strlen("--output=")).data();
358 out.reset(new std::ofstream(filename));
359 if (!out->good()) {
360 fprintf(stderr, "failed to open output filename %s\n", filename);
361 usage();
362 }
363 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700364 } else {
365 fprintf(stderr, "unknown argument %s\n", option.data());
366 usage();
367 }
368 }
369
370 if (image_filename == NULL) {
371 fprintf(stderr, "--image file name not specified\n");
372 return EXIT_FAILURE;
373 }
374
375 if (dex_filenames.empty()) {
376 fprintf(stderr, "no --dex-file values specified\n");
377 return EXIT_FAILURE;
378 }
379
380 if (boot_image_filename != NULL && boot_dex_filenames.empty()) {
381 fprintf(stderr, "no --boot-dex-file values specified with --boot\n");
382 return EXIT_FAILURE;
383 }
384
385 std::vector<const DexFile*> dex_files;
386 DexFile::OpenDexFiles(dex_filenames, dex_files, strip_location_prefix);
387
388 std::vector<const DexFile*> boot_dex_files;
389 DexFile::OpenDexFiles(boot_dex_filenames, boot_dex_files, strip_location_prefix);
390
391 Runtime::Options options;
392 std::string image_option;
393 std::string boot_image_option;
394 if (boot_image_filename == NULL) {
395 // if we don't have multiple images, pass the main one as the boot to match dex2oat
396 boot_image_filename = image_filename;
397 boot_dex_files = dex_files;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700398 dex_files.clear();
399 } else {
400 image_option += "-Ximage:";
401 image_option += image_filename;
402 options.push_back(std::make_pair("classpath", &dex_files));
403 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
404 }
405 boot_image_option += "-Xbootimage:";
406 boot_image_option += boot_image_filename;
407 options.push_back(std::make_pair("bootclasspath", &boot_dex_files));
408 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
409
410 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
411 if (runtime.get() == NULL) {
412 fprintf(stderr, "could not create runtime\n");
413 return EXIT_FAILURE;
414 }
415 ClassLinker* class_linker = runtime->GetClassLinker();
416 for (size_t i = 0; i < dex_files.size(); i++) {
417 class_linker->RegisterDexFile(*dex_files[i]);
418 }
419
420 Space* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2];
421 CHECK(image_space != NULL);
422 const ImageHeader& image_header = image_space->GetImageHeader();
423 if (!image_header.IsValid()) {
424 fprintf(stderr, "invalid image header %s\n", image_filename);
425 return EXIT_FAILURE;
426 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700427 OatDump::Dump(image_filename, *os, *image_space, image_header);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700428 return EXIT_SUCCESS;
429}
430
431} // namespace art
432
433int main(int argc, char** argv) {
434 return art::oatdump(argc, argv);
435}