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