blob: 60b1a3baeff6b8fa9581e5359dbf4660d7d4352c [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
294 OatDump::Dump(oat_location, host_prefix, os, *oat_file);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700295 }
296
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700297 private:
298
Brian Carlstromaded5f72011-10-07 17:15:04 -0700299 ImageDump(const Space& dump_space, std::ostream& os) : dump_space_(dump_space), os_(os) {}
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700300
Brian Carlstromaded5f72011-10-07 17:15:04 -0700301 ~ImageDump() {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700302
Brian Carlstrom78128a62011-09-15 17:21:19 -0700303 static void Callback(Object* obj, void* arg) {
304 DCHECK(obj != NULL);
305 DCHECK(arg != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700306 ImageDump* state = reinterpret_cast<ImageDump*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700307 if (!state->InDumpSpace(obj)) {
308 return;
309 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700310
311 size_t object_bytes = obj->SizeOf();
312 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
313 state->stats_.object_bytes += object_bytes;
314 state->stats_.alignment_bytes += alignment_bytes;
315
Brian Carlstrom78128a62011-09-15 17:21:19 -0700316 std::string summary;
317 StringAppendF(&summary, "%p: ", obj);
318 if (obj->IsClass()) {
319 Class* klass = obj->AsClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800320 summary += "CLASS ";
321 summary += ClassHelper(klass).GetDescriptor();
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700322 std::ostringstream ss;
Brian Carlstrome10b6972011-09-26 13:49:03 -0700323 ss << " (" << klass->GetStatus() << ")";
324 summary += ss.str();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700325 } else if (obj->IsMethod()) {
326 Method* method = obj->AsMethod();
327 StringAppendF(&summary, "METHOD %s", PrettyMethod(method).c_str());
328 } else if (obj->IsField()) {
329 Field* field = obj->AsField();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700330 StringAppendF(&summary, "FIELD %s", PrettyField(field).c_str());
331 } else if (obj->IsArrayInstance()) {
332 StringAppendF(&summary, "ARRAY %d", obj->AsArray()->GetLength());
Elliott Hughesdbb40792011-11-18 17:05:22 -0800333 } else if (obj->GetClass()->IsStringClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700334 StringAppendF(&summary, "STRING %s", obj->AsString()->ToModifiedUtf8().c_str());
335 } else {
336 StringAppendF(&summary, "OBJECT");
337 }
338 StringAppendF(&summary, "\n");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800339 std::string descriptor(ClassHelper(obj->GetClass()).GetDescriptor());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700340 StringAppendF(&summary, "\tclass %p: %s\n", obj->GetClass(), descriptor.c_str());
341 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
342 state->stats_.descriptor_to_count[descriptor] += 1;
343 // StringAppendF(&summary, "\tsize %d (alignment padding %d)\n",
344 // object_bytes, RoundUp(object_bytes, kObjectAlignment) - object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700345 if (obj->IsMethod()) {
346 Method* method = obj->AsMethod();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700347 if (!method->IsCalleeSaveMethod()) {
348 const int8_t* code =reinterpret_cast<const int8_t*>(method->GetCode());
349 StringAppendF(&summary, "\tCODE %p\n", code);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700350
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700351 const Method::InvokeStub* invoke_stub = method->GetInvokeStub();
352 StringAppendF(&summary, "\tJNI STUB %p\n", invoke_stub);
Ian Rogersff1ed472011-09-20 13:46:24 -0700353 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700354 if (method->IsNative()) {
355 if (method->IsRegistered()) {
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700356 StringAppendF(&summary, "\tNATIVE REGISTERED %p\n", method->GetNativeMethod());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700357 } else {
358 StringAppendF(&summary, "\tNATIVE UNREGISTERED\n");
359 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700360 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800361 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700362 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700363 } else if (method->IsAbstract()) {
364 StringAppendF(&summary, "\tABSTRACT\n");
Ian Rogersd81871c2011-10-03 13:57:23 -0700365 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800366 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700367 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
368 } else if (method->IsCalleeSaveMethod()) {
369 StringAppendF(&summary, "\tCALLEE SAVE METHOD\n");
Ian Rogersd81871c2011-10-03 13:57:23 -0700370 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800371 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700372 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700373 } else {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800374 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
375 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700376
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800377 size_t register_map_bytes = method->GetGcMapLength();
378 state->stats_.register_map_bytes += register_map_bytes;
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800379 StringAppendF(&summary, "GC=%zd ", register_map_bytes);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800380
381 size_t pc_mapping_table_bytes = method->GetMappingTableLength();
382 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800383 StringAppendF(&summary, "Mapping=%zd ", pc_mapping_table_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700384
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800385 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700386 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700387 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800388
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800389 StringAppendF(&summary, "\tSIZE Code=%zd GC=%zd Mapping=%zd",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800390 dex_instruction_bytes, register_map_bytes, pc_mapping_table_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700391 }
392 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700393 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700394 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700395
396 bool InDumpSpace(const Object* object) {
Ian Rogers30fab402012-01-23 15:43:46 -0800397 return dump_space_.Contains(object);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700398 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700399
400 public:
401 struct Stats {
402 size_t file_bytes;
403
404 size_t header_bytes;
405 size_t object_bytes;
406 size_t alignment_bytes;
407
408 size_t managed_code_bytes;
409 size_t managed_to_native_code_bytes;
410 size_t native_to_managed_code_bytes;
411
412 size_t register_map_bytes;
413 size_t pc_mapping_table_bytes;
414
415 size_t dex_instruction_bytes;
416
417 Stats()
418 : file_bytes(0),
419 header_bytes(0),
420 object_bytes(0),
421 alignment_bytes(0),
422 managed_code_bytes(0),
423 managed_to_native_code_bytes(0),
424 native_to_managed_code_bytes(0),
425 register_map_bytes(0),
426 pc_mapping_table_bytes(0),
427 dex_instruction_bytes(0) {}
428
Elliott Hughese5448b52012-01-18 16:44:06 -0800429 typedef std::map<std::string, size_t> TableBytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700430 TableBytes descriptor_to_bytes;
431
Elliott Hughese5448b52012-01-18 16:44:06 -0800432 typedef std::map<std::string, size_t> TableCount;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700433 TableCount descriptor_to_count;
434
435 double PercentOfFileBytes(size_t size) {
436 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
437 }
438
439 double PercentOfObjectBytes(size_t size) {
440 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
441 }
442
443 void Dump(std::ostream& os) {
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800444 os << StringPrintf("\tfile_bytes = %zd\n", file_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700445 os << "\n";
446
447 os << "\tfile_bytes = header_bytes + object_bytes + alignment_bytes\n";
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800448 os << StringPrintf("\theader_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700449 header_bytes, PercentOfFileBytes(header_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800450 os << StringPrintf("\tobject_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700451 object_bytes, PercentOfFileBytes(object_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800452 os << StringPrintf("\talignment_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700453 alignment_bytes, PercentOfFileBytes(alignment_bytes));
454 os << "\n";
455 os << std::flush;
456 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
457
458 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
459 size_t object_bytes_total = 0;
460 typedef TableBytes::const_iterator It; // TODO: C++0x auto
461 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -0800462 const std::string& descriptor(it->first);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700463 size_t bytes = it->second;
464 size_t count = descriptor_to_count[descriptor];
465 double average = static_cast<double>(bytes) / static_cast<double>(count);
466 double percent = PercentOfObjectBytes(bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800467 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700468 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
469 descriptor.c_str(), bytes, count,
470 average, percent);
471
472 object_bytes_total += bytes;
473 }
474 os << "\n";
475 os << std::flush;
476 CHECK_EQ(object_bytes, object_bytes_total);
477
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800478 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700479 managed_code_bytes, PercentOfObjectBytes(managed_code_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800480 os << StringPrintf("\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700481 managed_to_native_code_bytes,
482 PercentOfObjectBytes(managed_to_native_code_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800483 os << StringPrintf("\tnative_to_managed_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700484 native_to_managed_code_bytes,
485 PercentOfObjectBytes(native_to_managed_code_bytes));
486 os << "\n";
487 os << std::flush;
488
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800489 os << StringPrintf("\tregister_map_bytes = %7zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700490 register_map_bytes, PercentOfObjectBytes(register_map_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800491 os << StringPrintf("\tpc_mapping_table_bytes = %7zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700492 pc_mapping_table_bytes, PercentOfObjectBytes(pc_mapping_table_bytes));
493 os << "\n";
494 os << std::flush;
495
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800496 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700497 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f\n",
498 static_cast<double>(managed_code_bytes)
499 / static_cast<double>(dex_instruction_bytes));
500 os << "\n";
501 os << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700502 }
503 } stats_;
504
505 private:
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700506 const Space& dump_space_;
507 std::ostream& os_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700508
Brian Carlstromaded5f72011-10-07 17:15:04 -0700509 DISALLOW_COPY_AND_ASSIGN(ImageDump);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700510};
511
512int oatdump(int argc, char** argv) {
513 // Skip over argv[0].
514 argv++;
515 argc--;
516
517 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700518 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700519 usage();
520 }
521
Brian Carlstromaded5f72011-10-07 17:15:04 -0700522 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700523 const char* image_filename = NULL;
524 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700525 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700526 std::ostream* os = &std::cout;
527 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700528
529 for (int i = 0; i < argc; i++) {
530 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800531 if (option.starts_with("--oat-file=")) {
532 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700533 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700534 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700535 } else if (option.starts_with("--boot-image=")) {
536 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700537 } else if (option.starts_with("--host-prefix=")) {
538 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700539 } else if (option.starts_with("--output=")) {
540 const char* filename = option.substr(strlen("--output=")).data();
541 out.reset(new std::ofstream(filename));
542 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700543 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700544 usage();
545 }
546 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700547 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700548 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700549 usage();
550 }
551 }
552
Brian Carlstromaded5f72011-10-07 17:15:04 -0700553 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700554 fprintf(stderr, "Either --image or --oat must be specified\n");
555 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700556 }
557
Brian Carlstromaded5f72011-10-07 17:15:04 -0700558 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700559 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
560 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700561 }
562
563 if (oat_filename != NULL) {
564 const OatFile* oat_file = OatFile::Open(oat_filename, "", NULL);
565 if (oat_file == NULL) {
566 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
567 return EXIT_FAILURE;
568 }
569 OatDump::Dump(oat_filename, host_prefix, *os, *oat_file);
570 return EXIT_SUCCESS;
571 }
572
Brian Carlstrom78128a62011-09-15 17:21:19 -0700573 Runtime::Options options;
574 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700575 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700576 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700577 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700578 if (boot_image_filename != NULL) {
579 boot_image_option += "-Ximage:";
580 boot_image_option += boot_image_filename;
581 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -0700582 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700583 if (image_filename != NULL) {
584 image_option += "-Ximage:";
585 image_option += image_filename;
586 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
587 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700588
589 if (!host_prefix.empty()) {
590 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
591 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700592
593 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
594 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700595 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700596 return EXIT_FAILURE;
597 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700598
Ian Rogers30fab402012-01-23 15:43:46 -0800599 ImageSpace* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]->AsImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700600 CHECK(image_space != NULL);
601 const ImageHeader& image_header = image_space->GetImageHeader();
602 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700603 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700604 return EXIT_FAILURE;
605 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700606 ImageDump::Dump(image_filename, host_prefix, *os, *image_space, image_header);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700607 return EXIT_SUCCESS;
608}
609
610} // namespace art
611
612int main(int argc, char** argv) {
613 return art::oatdump(argc, argv);
614}