blob: a70bcd3b0bd7dd8400739b0eb16635723cc53ede [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Elliott Hughese5448b52012-01-18 16:44:06 -080022#include <map>
Brian Carlstrom78128a62011-09-15 17:21:19 -070023#include <string>
24#include <vector>
25
26#include "class_linker.h"
Brian Carlstrom916e74e2011-09-23 11:42:01 -070027#include "file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070028#include "image.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080029#include "object_utils.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080030#include "os.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070031#include "runtime.h"
32#include "space.h"
33#include "stringpiece.h"
34
35namespace art {
36
37static void usage() {
38 fprintf(stderr,
39 "Usage: oatdump [options] ...\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080040 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n"
41 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070042 "\n");
43 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080044 " --oat-file=<file.oat>: specifies an input oat filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080045 " Example: --image=/system/framework/boot.oat\n"
Brian Carlstromaded5f72011-10-07 17:15:04 -070046 "\n");
47 fprintf(stderr,
48 " --image=<file.art>: specifies an input image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080049 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070050 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070051 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070052 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080053 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070054 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070055 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070056 " --host-prefix may be used to translate host paths to target paths during\n"
57 " cross compilation.\n"
58 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070059 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070060 fprintf(stderr,
61 " --output=<file> may be used to send the output to a file.\n"
62 " Example: --output=/tmp/oatdump.txt\n"
63 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070064 exit(EXIT_FAILURE);
65}
66
Ian Rogersff1ed472011-09-20 13:46:24 -070067const char* image_roots_descriptions_[] = {
Brian Carlstrom78128a62011-09-15 17:21:19 -070068 "kJniStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070069 "kAbstractMethodErrorStubArray",
Ian Rogersad25ac52011-10-04 19:13:33 -070070 "kInstanceResolutionStubArray",
71 "kStaticResolutionStubArray",
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070072 "kUnknownMethodResolutionStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070073 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070074 "kRefsOnlySaveMethod",
75 "kRefsAndArgsSaveMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070076 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070077 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070078 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070079};
80
Brian Carlstrom27ec9612011-09-19 20:20:38 -070081class OatDump {
Brian Carlstromaded5f72011-10-07 17:15:04 -070082 public:
83 static void Dump(const std::string& oat_filename,
84 const std::string& host_prefix,
85 std::ostream& os,
86 const OatFile& oat_file) {
87 const OatHeader& oat_header = oat_file.GetOatHeader();
88
89 os << "MAGIC:\n";
90 os << oat_header.GetMagic() << "\n\n";
91
92 os << "CHECKSUM:\n";
93 os << StringPrintf("%08x\n\n", oat_header.GetChecksum());
94
95 os << "DEX FILE COUNT:\n";
96 os << oat_header.GetDexFileCount() << "\n\n";
97
98 os << "EXECUTABLE OFFSET:\n";
99 os << StringPrintf("%08x\n\n", oat_header.GetExecutableOffset());
100
Ian Rogers30fab402012-01-23 15:43:46 -0800101 os << "BEGIN:\n";
102 os << reinterpret_cast<const void*>(oat_file.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700103
Ian Rogers30fab402012-01-23 15:43:46 -0800104 os << "END:\n";
105 os << reinterpret_cast<const void*>(oat_file.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700106
107 os << std::flush;
108
Elliott Hughesba8eee12012-01-24 20:25:24 -0800109 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file.GetOatDexFiles();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700110 for (size_t i = 0; i < oat_dex_files.size(); i++) {
111 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
112 CHECK(oat_dex_file != NULL);
113 DumpOatDexFile(host_prefix, os, oat_file, *oat_dex_file);
114 }
115 }
116
117 private:
118 static void DumpOatDexFile(const std::string& host_prefix,
119 std::ostream& os,
120 const OatFile& oat_file,
121 const OatFile::OatDexFile& oat_dex_file) {
122 os << "OAT DEX FILE:\n";
Elliott Hughes95572412011-12-13 18:14:20 -0800123 std::string dex_file_location(oat_dex_file.GetDexFileLocation());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700124 os << "location: " << dex_file_location;
125 if (!host_prefix.empty()) {
126 dex_file_location = host_prefix + dex_file_location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700127 os << " (" << dex_file_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700128 }
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700129 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700130 os << StringPrintf("checksum: %08x\n", oat_dex_file.GetDexFileChecksum());
131 const DexFile* dex_file = DexFile::Open(dex_file_location, "");
132 if (dex_file == NULL) {
133 os << "NOT FOUND\n\n";
134 return;
135 }
136 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
137 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
138 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700139 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
140 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800141 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800142 << oat_class->GetStatus() << ")\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700143 DumpOatClass(os, oat_file, *oat_class.get(), *dex_file, class_def);
144 }
145
146 os << std::flush;
147 }
148
149 static void DumpOatClass(std::ostream& os,
150 const OatFile& oat_file,
151 const OatFile::OatClass& oat_class,
152 const DexFile& dex_file,
153 const DexFile::ClassDef& class_def) {
154 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700155 if (class_data == NULL) { // empty class such as a marker interface?
156 return;
157 }
158 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700159
160 // just skipping through the fields to advance class_data
Ian Rogers0571d352011-11-03 19:51:38 -0700161 while (it.HasNextStaticField()) {
162 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700163 }
Ian Rogers0571d352011-11-03 19:51:38 -0700164 while (it.HasNextInstanceField()) {
165 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700166 }
167
Ian Rogers0571d352011-11-03 19:51:38 -0700168 uint32_t method_index = 0;
169 while (it.HasNextDirectMethod()) {
170 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index);
171 DumpOatMethod(os, method_index, oat_file, oat_method, dex_file, it.GetMemberIndex());
172 method_index++;
173 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700174 }
Ian Rogers0571d352011-11-03 19:51:38 -0700175 while (it.HasNextVirtualMethod()) {
176 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index);
177 DumpOatMethod(os, method_index, oat_file, oat_method, dex_file, it.GetMemberIndex());
178 method_index++;
179 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700180 }
Ian Rogers0571d352011-11-03 19:51:38 -0700181 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700182 os << std::flush;
183 }
184 static void DumpOatMethod(std::ostream& os,
185 uint32_t method_index,
186 const OatFile& oat_file,
187 const OatFile::OatMethod& oat_method,
188 const DexFile& dex_file,
Ian Rogers0571d352011-11-03 19:51:38 -0700189 uint32_t method_idx) {
190 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700191 const char* name = dex_file.GetMethodName(method_id);
Elliott Hughes95572412011-12-13 18:14:20 -0800192 std::string signature(dex_file.GetMethodSignature(method_id));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700193 os << StringPrintf("\t%d: %s %s (method_idx=%d)\n",
Ian Rogers0571d352011-11-03 19:51:38 -0700194 method_index, name, signature.c_str(), method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700195 os << StringPrintf("\t\tcode: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800196 oat_method.GetCode(), oat_method.GetCodeOffset());
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800197 os << StringPrintf("\t\tframe_size_in_bytes: %zd\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800198 oat_method.GetFrameSizeInBytes());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700199 os << StringPrintf("\t\tcore_spill_mask: %08x\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800200 oat_method.GetCoreSpillMask());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700201 os << StringPrintf("\t\tfp_spill_mask: %08x\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800202 oat_method.GetFpSpillMask());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700203 os << StringPrintf("\t\tmapping_table: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800204 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700205 os << StringPrintf("\t\tvmap_table: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800206 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800207 os << StringPrintf("\t\tgc_map: %p (offset=%08x)\n",
208 oat_method.GetGcMap(), oat_method.GetGcMapOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700209 os << StringPrintf("\t\tinvoke_stub: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800210 oat_method.GetInvokeStub(), oat_method.GetInvokeStubOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700211 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700212};
213
214class ImageDump {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700215 public:
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700216 static void Dump(const std::string& image_filename,
Brian Carlstromaded5f72011-10-07 17:15:04 -0700217 const std::string& host_prefix,
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700218 std::ostream& os,
219 Space& image_space,
220 const ImageHeader& image_header) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700221 os << "MAGIC:\n";
222 os << image_header.GetMagic() << "\n\n";
223
Ian Rogers30fab402012-01-23 15:43:46 -0800224 os << "IMAGE BEGIN:\n";
225 os << reinterpret_cast<void*>(image_header.GetImageBegin()) << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700226
Brian Carlstromaded5f72011-10-07 17:15:04 -0700227 os << "OAT CHECKSUM:\n";
228 os << StringPrintf("%08x\n\n", image_header.GetOatChecksum());
229
Ian Rogers30fab402012-01-23 15:43:46 -0800230 os << "OAT BEGIN:\n";
231 os << reinterpret_cast<void*>(image_header.GetOatBegin()) << "\n\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700232
Ian Rogers30fab402012-01-23 15:43:46 -0800233 os << "OAT END:\n";
234 os << reinterpret_cast<void*>(image_header.GetOatEnd()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700235
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700236 os << "ROOTS:\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700237 os << reinterpret_cast<void*>(image_header.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700238 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700239 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
240 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700241 const char* image_root_description = image_roots_descriptions_[i];
242 Object* image_root_object = image_header.GetImageRoot(image_root);
243 os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
244 if (image_root_object->IsObjectArray()) {
245 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
246 ObjectArray<Object>* image_root_object_array
247 = down_cast<ObjectArray<Object>*>(image_root_object);
248 // = image_root_object->AsObjectArray<Object>();
249 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
250 os << StringPrintf("\t%d: %p\n", i, image_root_object_array->Get(i));
251 }
252 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700253 }
254 os << "\n";
255
256 os << "OBJECTS:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700257 ImageDump state(image_space, os);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700258 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
259 DCHECK(heap_bitmap != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700260 heap_bitmap->Walk(ImageDump::Callback, &state);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700261 os << "\n";
262
263 os << "STATS:\n" << std::flush;
264 UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), false));
265 state.stats_.file_bytes = file->Length();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700266 size_t header_bytes = sizeof(ImageHeader);
267 state.stats_.header_bytes = header_bytes;
268 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
269 state.stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700270 state.stats_.Dump(os);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700271 os << "\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700272
273 os << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700274
275 os << "OAT LOCATION:\n" << std::flush;
276 Object* oat_location_object = image_header.GetImageRoot(ImageHeader::kOatLocation);
Elliott Hughes95572412011-12-13 18:14:20 -0800277 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700278 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
279 os << oat_location;
280 if (!host_prefix.empty()) {
281 oat_location = host_prefix + oat_location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700282 os << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700283 }
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700284 os << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800285 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700286 if (oat_file == NULL) {
287 os << "NOT FOUND\n";
288 os << std::flush;
289 return;
290 }
291 os << "\n";
292 os << std::flush;
293
Brian Carlstrom866c8622012-01-06 16:35:13 -0800294 class_linker->RegisterOatFile(*oat_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700295 OatDump::Dump(oat_location, host_prefix, os, *oat_file);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700296 }
297
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700298 private:
299
Brian Carlstromaded5f72011-10-07 17:15:04 -0700300 ImageDump(const Space& dump_space, std::ostream& os) : dump_space_(dump_space), os_(os) {}
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700301
Brian Carlstromaded5f72011-10-07 17:15:04 -0700302 ~ImageDump() {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700303
Brian Carlstrom78128a62011-09-15 17:21:19 -0700304 static void Callback(Object* obj, void* arg) {
305 DCHECK(obj != NULL);
306 DCHECK(arg != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700307 ImageDump* state = reinterpret_cast<ImageDump*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700308 if (!state->InDumpSpace(obj)) {
309 return;
310 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700311
312 size_t object_bytes = obj->SizeOf();
313 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
314 state->stats_.object_bytes += object_bytes;
315 state->stats_.alignment_bytes += alignment_bytes;
316
Brian Carlstrom78128a62011-09-15 17:21:19 -0700317 std::string summary;
318 StringAppendF(&summary, "%p: ", obj);
319 if (obj->IsClass()) {
320 Class* klass = obj->AsClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800321 summary += "CLASS ";
322 summary += ClassHelper(klass).GetDescriptor();
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700323 std::ostringstream ss;
Brian Carlstrome10b6972011-09-26 13:49:03 -0700324 ss << " (" << klass->GetStatus() << ")";
325 summary += ss.str();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700326 } else if (obj->IsMethod()) {
327 Method* method = obj->AsMethod();
328 StringAppendF(&summary, "METHOD %s", PrettyMethod(method).c_str());
329 } else if (obj->IsField()) {
330 Field* field = obj->AsField();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700331 StringAppendF(&summary, "FIELD %s", PrettyField(field).c_str());
332 } else if (obj->IsArrayInstance()) {
333 StringAppendF(&summary, "ARRAY %d", obj->AsArray()->GetLength());
Elliott Hughesdbb40792011-11-18 17:05:22 -0800334 } else if (obj->GetClass()->IsStringClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700335 StringAppendF(&summary, "STRING %s", obj->AsString()->ToModifiedUtf8().c_str());
336 } else {
337 StringAppendF(&summary, "OBJECT");
338 }
339 StringAppendF(&summary, "\n");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800340 std::string descriptor(ClassHelper(obj->GetClass()).GetDescriptor());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700341 StringAppendF(&summary, "\tclass %p: %s\n", obj->GetClass(), descriptor.c_str());
342 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
343 state->stats_.descriptor_to_count[descriptor] += 1;
344 // StringAppendF(&summary, "\tsize %d (alignment padding %d)\n",
345 // object_bytes, RoundUp(object_bytes, kObjectAlignment) - object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700346 if (obj->IsMethod()) {
347 Method* method = obj->AsMethod();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700348 if (!method->IsCalleeSaveMethod()) {
349 const int8_t* code =reinterpret_cast<const int8_t*>(method->GetCode());
350 StringAppendF(&summary, "\tCODE %p\n", code);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700351
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700352 const Method::InvokeStub* invoke_stub = method->GetInvokeStub();
353 StringAppendF(&summary, "\tJNI STUB %p\n", invoke_stub);
Ian Rogersff1ed472011-09-20 13:46:24 -0700354 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700355 if (method->IsNative()) {
356 if (method->IsRegistered()) {
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700357 StringAppendF(&summary, "\tNATIVE REGISTERED %p\n", method->GetNativeMethod());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700358 } else {
359 StringAppendF(&summary, "\tNATIVE UNREGISTERED\n");
360 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700361 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800362 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700363 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700364 } else if (method->IsAbstract()) {
365 StringAppendF(&summary, "\tABSTRACT\n");
Ian Rogersd81871c2011-10-03 13:57:23 -0700366 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800367 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700368 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
369 } else if (method->IsCalleeSaveMethod()) {
370 StringAppendF(&summary, "\tCALLEE SAVE METHOD\n");
Ian Rogersd81871c2011-10-03 13:57:23 -0700371 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800372 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700373 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700374 } else {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800375 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
376 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700377
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800378 size_t register_map_bytes = method->GetGcMapLength();
379 state->stats_.register_map_bytes += register_map_bytes;
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800380 StringAppendF(&summary, "GC=%zd ", register_map_bytes);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800381
382 size_t pc_mapping_table_bytes = method->GetMappingTableLength();
383 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800384 StringAppendF(&summary, "Mapping=%zd ", pc_mapping_table_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700385
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800386 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700387 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700388 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800389
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800390 StringAppendF(&summary, "\tSIZE Code=%zd GC=%zd Mapping=%zd",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800391 dex_instruction_bytes, register_map_bytes, pc_mapping_table_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700392 }
393 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700394 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700395 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700396
397 bool InDumpSpace(const Object* object) {
Ian Rogers30fab402012-01-23 15:43:46 -0800398 return dump_space_.Contains(object);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700399 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700400
401 public:
402 struct Stats {
403 size_t file_bytes;
404
405 size_t header_bytes;
406 size_t object_bytes;
407 size_t alignment_bytes;
408
409 size_t managed_code_bytes;
410 size_t managed_to_native_code_bytes;
411 size_t native_to_managed_code_bytes;
412
413 size_t register_map_bytes;
414 size_t pc_mapping_table_bytes;
415
416 size_t dex_instruction_bytes;
417
418 Stats()
419 : file_bytes(0),
420 header_bytes(0),
421 object_bytes(0),
422 alignment_bytes(0),
423 managed_code_bytes(0),
424 managed_to_native_code_bytes(0),
425 native_to_managed_code_bytes(0),
426 register_map_bytes(0),
427 pc_mapping_table_bytes(0),
428 dex_instruction_bytes(0) {}
429
Elliott Hughese5448b52012-01-18 16:44:06 -0800430 typedef std::map<std::string, size_t> TableBytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700431 TableBytes descriptor_to_bytes;
432
Elliott Hughese5448b52012-01-18 16:44:06 -0800433 typedef std::map<std::string, size_t> TableCount;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700434 TableCount descriptor_to_count;
435
436 double PercentOfFileBytes(size_t size) {
437 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
438 }
439
440 double PercentOfObjectBytes(size_t size) {
441 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
442 }
443
444 void Dump(std::ostream& os) {
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800445 os << StringPrintf("\tfile_bytes = %zd\n", file_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700446 os << "\n";
447
448 os << "\tfile_bytes = header_bytes + object_bytes + alignment_bytes\n";
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800449 os << StringPrintf("\theader_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700450 header_bytes, PercentOfFileBytes(header_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800451 os << StringPrintf("\tobject_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700452 object_bytes, PercentOfFileBytes(object_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800453 os << StringPrintf("\talignment_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700454 alignment_bytes, PercentOfFileBytes(alignment_bytes));
455 os << "\n";
456 os << std::flush;
457 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
458
459 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
460 size_t object_bytes_total = 0;
461 typedef TableBytes::const_iterator It; // TODO: C++0x auto
462 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -0800463 const std::string& descriptor(it->first);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700464 size_t bytes = it->second;
465 size_t count = descriptor_to_count[descriptor];
466 double average = static_cast<double>(bytes) / static_cast<double>(count);
467 double percent = PercentOfObjectBytes(bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800468 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700469 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
470 descriptor.c_str(), bytes, count,
471 average, percent);
472
473 object_bytes_total += bytes;
474 }
475 os << "\n";
476 os << std::flush;
477 CHECK_EQ(object_bytes, object_bytes_total);
478
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800479 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700480 managed_code_bytes, PercentOfObjectBytes(managed_code_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800481 os << StringPrintf("\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700482 managed_to_native_code_bytes,
483 PercentOfObjectBytes(managed_to_native_code_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800484 os << StringPrintf("\tnative_to_managed_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700485 native_to_managed_code_bytes,
486 PercentOfObjectBytes(native_to_managed_code_bytes));
487 os << "\n";
488 os << std::flush;
489
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800490 os << StringPrintf("\tregister_map_bytes = %7zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700491 register_map_bytes, PercentOfObjectBytes(register_map_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800492 os << StringPrintf("\tpc_mapping_table_bytes = %7zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700493 pc_mapping_table_bytes, PercentOfObjectBytes(pc_mapping_table_bytes));
494 os << "\n";
495 os << std::flush;
496
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800497 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700498 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f\n",
499 static_cast<double>(managed_code_bytes)
500 / static_cast<double>(dex_instruction_bytes));
501 os << "\n";
502 os << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700503 }
504 } stats_;
505
506 private:
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700507 const Space& dump_space_;
508 std::ostream& os_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700509
Brian Carlstromaded5f72011-10-07 17:15:04 -0700510 DISALLOW_COPY_AND_ASSIGN(ImageDump);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700511};
512
513int oatdump(int argc, char** argv) {
514 // Skip over argv[0].
515 argv++;
516 argc--;
517
518 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700519 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700520 usage();
521 }
522
Brian Carlstromaded5f72011-10-07 17:15:04 -0700523 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700524 const char* image_filename = NULL;
525 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700526 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700527 std::ostream* os = &std::cout;
528 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700529
530 for (int i = 0; i < argc; i++) {
531 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800532 if (option.starts_with("--oat-file=")) {
533 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700534 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700535 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700536 } else if (option.starts_with("--boot-image=")) {
537 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700538 } else if (option.starts_with("--host-prefix=")) {
539 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700540 } else if (option.starts_with("--output=")) {
541 const char* filename = option.substr(strlen("--output=")).data();
542 out.reset(new std::ofstream(filename));
543 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700544 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700545 usage();
546 }
547 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700548 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700549 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700550 usage();
551 }
552 }
553
Brian Carlstromaded5f72011-10-07 17:15:04 -0700554 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700555 fprintf(stderr, "Either --image or --oat must be specified\n");
556 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700557 }
558
Brian Carlstromaded5f72011-10-07 17:15:04 -0700559 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700560 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
561 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700562 }
563
564 if (oat_filename != NULL) {
565 const OatFile* oat_file = OatFile::Open(oat_filename, "", NULL);
566 if (oat_file == NULL) {
567 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
568 return EXIT_FAILURE;
569 }
570 OatDump::Dump(oat_filename, host_prefix, *os, *oat_file);
571 return EXIT_SUCCESS;
572 }
573
Brian Carlstrom78128a62011-09-15 17:21:19 -0700574 Runtime::Options options;
575 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700576 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700577 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700578 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700579 if (boot_image_filename != NULL) {
580 boot_image_option += "-Ximage:";
581 boot_image_option += boot_image_filename;
582 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -0700583 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700584 if (image_filename != NULL) {
585 image_option += "-Ximage:";
586 image_option += image_filename;
587 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
588 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700589
590 if (!host_prefix.empty()) {
591 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
592 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700593
594 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
595 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700596 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700597 return EXIT_FAILURE;
598 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700599
Ian Rogers30fab402012-01-23 15:43:46 -0800600 ImageSpace* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]->AsImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700601 CHECK(image_space != NULL);
602 const ImageHeader& image_header = image_space->GetImageHeader();
603 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700604 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700605 return EXIT_FAILURE;
606 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700607 ImageDump::Dump(image_filename, host_prefix, *os, *image_space, image_header);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700608 return EXIT_SUCCESS;
609}
610
611} // namespace art
612
613int main(int argc, char** argv) {
614 return art::oatdump(argc, argv);
615}