blob: e96b25ca9d6076dbf8be8ff2bf6f53b3424612fd [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",
51 "kCalleeSaveMethod",
52 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070053 "kDexCaches",
Brian Carlstrom78128a62011-09-15 17:21:19 -070054};
55
Brian Carlstrom27ec9612011-09-19 20:20:38 -070056class OatDump {
Brian Carlstrom78128a62011-09-15 17:21:19 -070057
Brian Carlstrom27ec9612011-09-19 20:20:38 -070058 public:
Brian Carlstrom916e74e2011-09-23 11:42:01 -070059 static void Dump(const std::string& image_filename,
60 std::ostream& os,
61 Space& image_space,
62 const ImageHeader& image_header) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -070063 os << "MAGIC:\n";
64 os << image_header.GetMagic() << "\n\n";
65
Brian Carlstrome24fa612011-09-29 00:53:55 -070066 os << "IMAGE BASE:\n";
67 os << reinterpret_cast<void*>(image_header.GetImageBaseAddr()) << "\n\n";
68
69 os << "OAT BASE:\n";
70 os << reinterpret_cast<void*>(image_header.GetOatBaseAddr()) << "\n\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -070071
Brian Carlstrom27ec9612011-09-19 20:20:38 -070072 os << "ROOTS:\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -070073 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -070074 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
75 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
76 os << StringPrintf("%s: %p\n",
77 image_roots_descriptions_[i], image_header.GetImageRoot(image_root));
78 }
79 os << "\n";
80
81 os << "OBJECTS:\n" << std::flush;
82 OatDump state(image_space, os);
83 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
84 DCHECK(heap_bitmap != NULL);
85 heap_bitmap->Walk(OatDump::Callback, &state);
Brian Carlstrom916e74e2011-09-23 11:42:01 -070086 os << "\n";
87
88 os << "STATS:\n" << std::flush;
89 UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), false));
90 state.stats_.file_bytes = file->Length();
Brian Carlstrome24fa612011-09-29 00:53:55 -070091 size_t header_bytes = sizeof(ImageHeader);
92 state.stats_.header_bytes = header_bytes;
93 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
94 state.stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -070095 state.stats_.Dump(os);
96
97 os << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -070098 }
99
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700100 private:
101
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700102 OatDump(const Space& dump_space, std::ostream& os) : dump_space_(dump_space), os_(os) {
103 }
104
105 ~OatDump() {
106 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700107
Brian Carlstrom78128a62011-09-15 17:21:19 -0700108 static void Callback(Object* obj, void* arg) {
109 DCHECK(obj != NULL);
110 DCHECK(arg != NULL);
111 OatDump* state = reinterpret_cast<OatDump*>(arg);
112 if (!state->InDumpSpace(obj)) {
113 return;
114 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700115
116 size_t object_bytes = obj->SizeOf();
117 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
118 state->stats_.object_bytes += object_bytes;
119 state->stats_.alignment_bytes += alignment_bytes;
120
Brian Carlstrom78128a62011-09-15 17:21:19 -0700121 std::string summary;
122 StringAppendF(&summary, "%p: ", obj);
123 if (obj->IsClass()) {
124 Class* klass = obj->AsClass();
125 StringAppendF(&summary, "CLASS %s", klass->GetDescriptor()->ToModifiedUtf8().c_str());
Brian Carlstrome10b6972011-09-26 13:49:03 -0700126 std::stringstream ss;
127 ss << " (" << klass->GetStatus() << ")";
128 summary += ss.str();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700129 } 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();
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700154 if (!method->IsPhony()) {
Brian Carlstrome10b6972011-09-26 13:49:03 -0700155 const ByteArray* code = method->GetCodeArray();
156 const int8_t* code_base = NULL;
157 const int8_t* code_limit = NULL;
158 if (code != NULL) {
159 size_t code_bytes = code->GetLength();
160 code_base = code->GetData();
161 code_limit = code_base + code_bytes;
162 if (method->IsNative()) {
163 state->stats_.managed_to_native_code_bytes += code_bytes;
164 } else {
165 state->stats_.managed_code_bytes += code_bytes;
166 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700167 } else {
168 code_base = reinterpret_cast<const int8_t*>(method->GetCode());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700169 }
Brian Carlstrome10b6972011-09-26 13:49:03 -0700170 StringAppendF(&summary, "\tCODE %p-%p\n", code_base, code_limit);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700171
Ian Rogersff1ed472011-09-20 13:46:24 -0700172 const ByteArray* invoke = method->GetInvokeStubArray();
Brian Carlstrome10b6972011-09-26 13:49:03 -0700173 const int8_t* invoke_base = NULL;
174 const int8_t* invoke_limit = NULL;
175 if (invoke != NULL) {
176 size_t native_to_managed_code_bytes = invoke->GetLength();
177 invoke_base = invoke->GetData();
178 invoke_limit = invoke_base + native_to_managed_code_bytes;
179 state->stats_.native_to_managed_code_bytes += native_to_managed_code_bytes;
180 StringAppendF(&summary, "\tJNI STUB %p-%p\n", invoke_base, invoke_limit);
181 }
Ian Rogersff1ed472011-09-20 13:46:24 -0700182 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700183 if (method->IsNative()) {
184 if (method->IsRegistered()) {
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700185 StringAppendF(&summary, "\tNATIVE REGISTERED %p\n", method->GetNativeMethod());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700186 } else {
187 StringAppendF(&summary, "\tNATIVE UNREGISTERED\n");
188 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700189 DCHECK(method->GetRegisterMapHeader() == NULL);
190 DCHECK(method->GetRegisterMapData() == NULL);
191 DCHECK(method->GetMappingTable() == NULL);
192 } else if (method->IsAbstract()) {
193 StringAppendF(&summary, "\tABSTRACT\n");
194 DCHECK(method->GetRegisterMapHeader() == NULL);
195 DCHECK(method->GetRegisterMapData() == NULL);
196 DCHECK(method->GetMappingTable() == NULL);
197 } else if (method->IsPhony()) {
198 StringAppendF(&summary, "\tPHONY\n");
199 DCHECK(method->GetRegisterMapHeader() == NULL);
200 DCHECK(method->GetRegisterMapData() == NULL);
201 DCHECK(method->GetMappingTable() == NULL);
202 } else {
203 size_t register_map_bytes = (method->GetRegisterMapHeader()->GetLength() +
204 method->GetRegisterMapData()->GetLength());
205 state->stats_.register_map_bytes += register_map_bytes;
206
Brian Carlstrome10b6972011-09-26 13:49:03 -0700207 if (method->GetMappingTable() != NULL) {
208 size_t pc_mapping_table_bytes = method->GetMappingTable()->GetLength();
209 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
210 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700211
212 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
213 class DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
214 const DexFile& dex_file = class_linker->FindDexFile(dex_cache);
215 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
216 size_t dex_instruction_bytes = code_item->insns_size_ * 2;
217 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700218 }
219 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700220 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700221 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700222
223 bool InDumpSpace(const Object* object) {
224 const byte* o = reinterpret_cast<const byte*>(object);
225 return (o >= dump_space_.GetBase() && o < dump_space_.GetLimit());
226 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700227
228 public:
229 struct Stats {
230 size_t file_bytes;
231
232 size_t header_bytes;
233 size_t object_bytes;
234 size_t alignment_bytes;
235
236 size_t managed_code_bytes;
237 size_t managed_to_native_code_bytes;
238 size_t native_to_managed_code_bytes;
239
240 size_t register_map_bytes;
241 size_t pc_mapping_table_bytes;
242
243 size_t dex_instruction_bytes;
244
245 Stats()
246 : file_bytes(0),
247 header_bytes(0),
248 object_bytes(0),
249 alignment_bytes(0),
250 managed_code_bytes(0),
251 managed_to_native_code_bytes(0),
252 native_to_managed_code_bytes(0),
253 register_map_bytes(0),
254 pc_mapping_table_bytes(0),
255 dex_instruction_bytes(0) {}
256
257 typedef std::tr1::unordered_map<std::string,size_t> TableBytes;
258 TableBytes descriptor_to_bytes;
259
260 typedef std::tr1::unordered_map<std::string,size_t> TableCount;
261 TableCount descriptor_to_count;
262
263 double PercentOfFileBytes(size_t size) {
264 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
265 }
266
267 double PercentOfObjectBytes(size_t size) {
268 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
269 }
270
271 void Dump(std::ostream& os) {
272 os << StringPrintf("\tfile_bytes = %d\n", file_bytes);
273 os << "\n";
274
275 os << "\tfile_bytes = header_bytes + object_bytes + alignment_bytes\n";
276 os << StringPrintf("\theader_bytes = %10d (%2.0f%% of file_bytes)\n",
277 header_bytes, PercentOfFileBytes(header_bytes));
278 os << StringPrintf("\tobject_bytes = %10d (%2.0f%% of file_bytes)\n",
279 object_bytes, PercentOfFileBytes(object_bytes));
280 os << StringPrintf("\talignment_bytes = %10d (%2.0f%% of file_bytes)\n",
281 alignment_bytes, PercentOfFileBytes(alignment_bytes));
282 os << "\n";
283 os << std::flush;
284 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
285
286 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
287 size_t object_bytes_total = 0;
288 typedef TableBytes::const_iterator It; // TODO: C++0x auto
289 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
290 const std::string& descriptor = it->first;
291 size_t bytes = it->second;
292 size_t count = descriptor_to_count[descriptor];
293 double average = static_cast<double>(bytes) / static_cast<double>(count);
294 double percent = PercentOfObjectBytes(bytes);
Brian Carlstromf153fe12011-09-27 16:27:12 -0700295 os << StringPrintf("\t%32s %8d bytes %6d instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700296 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
297 descriptor.c_str(), bytes, count,
298 average, percent);
299
300 object_bytes_total += bytes;
301 }
302 os << "\n";
303 os << std::flush;
304 CHECK_EQ(object_bytes, object_bytes_total);
305
306 os << StringPrintf("\tmanaged_code_bytes = %8d (%2.0f%% of object_bytes)\n",
307 managed_code_bytes, PercentOfObjectBytes(managed_code_bytes));
308 os << StringPrintf("\tmanaged_to_native_code_bytes = %8d (%2.0f%% of object_bytes)\n",
309 managed_to_native_code_bytes,
310 PercentOfObjectBytes(managed_to_native_code_bytes));
311 os << StringPrintf("\tnative_to_managed_code_bytes = %8d (%2.0f%% of object_bytes)\n",
312 native_to_managed_code_bytes,
313 PercentOfObjectBytes(native_to_managed_code_bytes));
314 os << "\n";
315 os << std::flush;
316
317 os << StringPrintf("\tregister_map_bytes = %7d (%2.0f%% of object_bytes)\n",
318 register_map_bytes, PercentOfObjectBytes(register_map_bytes));
319 os << StringPrintf("\tpc_mapping_table_bytes = %7d (%2.0f%% of object_bytes)\n",
320 pc_mapping_table_bytes, PercentOfObjectBytes(pc_mapping_table_bytes));
321 os << "\n";
322 os << std::flush;
323
324 os << StringPrintf("\tdex_instruction_bytes = %d\n", dex_instruction_bytes);
325 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f\n",
326 static_cast<double>(managed_code_bytes)
327 / static_cast<double>(dex_instruction_bytes));
328 os << "\n";
329 os << std::flush;
330
331 }
332 } stats_;
333
334 private:
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700335 const Space& dump_space_;
336 std::ostream& os_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700337
338 DISALLOW_COPY_AND_ASSIGN(OatDump);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700339};
340
341int oatdump(int argc, char** argv) {
342 // Skip over argv[0].
343 argv++;
344 argc--;
345
346 if (argc == 0) {
347 fprintf(stderr, "no arguments specified\n");
348 usage();
349 }
350
Brian Carlstrom78128a62011-09-15 17:21:19 -0700351 const char* image_filename = NULL;
352 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700353 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700354 std::ostream* os = &std::cout;
355 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700356
357 for (int i = 0; i < argc; i++) {
358 const StringPiece option(argv[i]);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700359 if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700360 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700361 } else if (option.starts_with("--boot-image=")) {
362 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700363 } else if (option.starts_with("--host-prefix=")) {
364 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700365 } else if (option.starts_with("--output=")) {
366 const char* filename = option.substr(strlen("--output=")).data();
367 out.reset(new std::ofstream(filename));
368 if (!out->good()) {
369 fprintf(stderr, "failed to open output filename %s\n", filename);
370 usage();
371 }
372 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700373 } else {
374 fprintf(stderr, "unknown argument %s\n", option.data());
375 usage();
376 }
377 }
378
379 if (image_filename == NULL) {
380 fprintf(stderr, "--image file name not specified\n");
381 return EXIT_FAILURE;
382 }
383
Brian Carlstrom78128a62011-09-15 17:21:19 -0700384 Runtime::Options options;
385 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700386 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700387 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700388 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700389 if (boot_image_filename != NULL) {
390 boot_image_option += "-Ximage:";
391 boot_image_option += boot_image_filename;
392 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -0700393 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700394 image_option += "-Ximage:";
395 image_option += image_filename;
396 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
397
398 if (!host_prefix.empty()) {
399 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
400 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700401
402 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
403 if (runtime.get() == NULL) {
404 fprintf(stderr, "could not create runtime\n");
405 return EXIT_FAILURE;
406 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700407
408 Space* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2];
409 CHECK(image_space != NULL);
410 const ImageHeader& image_header = image_space->GetImageHeader();
411 if (!image_header.IsValid()) {
412 fprintf(stderr, "invalid image header %s\n", image_filename);
413 return EXIT_FAILURE;
414 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700415 OatDump::Dump(image_filename, *os, *image_space, image_header);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700416 return EXIT_SUCCESS;
417}
418
419} // namespace art
420
421int main(int argc, char** argv) {
422 return art::oatdump(argc, argv);
423}