blob: 1b54b245f2c1ccb66897971adc492d92ed0c298b [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,
Brian Carlstromaded5f72011-10-07 17:15:04 -070084 std::ostream& os,
85 const OatFile& oat_file) {
86 const OatHeader& oat_header = oat_file.GetOatHeader();
87
88 os << "MAGIC:\n";
89 os << oat_header.GetMagic() << "\n\n";
90
91 os << "CHECKSUM:\n";
92 os << StringPrintf("%08x\n\n", oat_header.GetChecksum());
93
94 os << "DEX FILE COUNT:\n";
95 os << oat_header.GetDexFileCount() << "\n\n";
96
97 os << "EXECUTABLE OFFSET:\n";
98 os << StringPrintf("%08x\n\n", oat_header.GetExecutableOffset());
99
Ian Rogers30fab402012-01-23 15:43:46 -0800100 os << "BEGIN:\n";
101 os << reinterpret_cast<const void*>(oat_file.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700102
Ian Rogers30fab402012-01-23 15:43:46 -0800103 os << "END:\n";
104 os << reinterpret_cast<const void*>(oat_file.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700105
106 os << std::flush;
107
Elliott Hughesba8eee12012-01-24 20:25:24 -0800108 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file.GetOatDexFiles();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700109 for (size_t i = 0; i < oat_dex_files.size(); i++) {
110 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
111 CHECK(oat_dex_file != NULL);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800112 DumpOatDexFile(os, oat_file, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700113 }
114 }
115
116 private:
Brian Carlstroma004aa92012-02-08 18:05:09 -0800117 static void DumpOatDexFile(std::ostream& os,
Brian Carlstromaded5f72011-10-07 17:15:04 -0700118 const OatFile& oat_file,
119 const OatFile::OatDexFile& oat_dex_file) {
120 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800121 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800122 os << StringPrintf("checksum: %08x\n", oat_dex_file.GetDexFileLocationChecksum());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800123 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile());
124 if (dex_file.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700125 os << "NOT FOUND\n\n";
126 return;
127 }
128 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
129 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
130 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700131 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
132 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800133 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800134 << oat_class->GetStatus() << ")\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800135 DumpOatClass(os, oat_file, *oat_class.get(), *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700136 }
137
138 os << std::flush;
139 }
140
141 static void DumpOatClass(std::ostream& os,
142 const OatFile& oat_file,
143 const OatFile::OatClass& oat_class,
144 const DexFile& dex_file,
145 const DexFile::ClassDef& class_def) {
146 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700147 if (class_data == NULL) { // empty class such as a marker interface?
148 return;
149 }
150 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700151
152 // just skipping through the fields to advance class_data
Ian Rogers0571d352011-11-03 19:51:38 -0700153 while (it.HasNextStaticField()) {
154 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700155 }
Ian Rogers0571d352011-11-03 19:51:38 -0700156 while (it.HasNextInstanceField()) {
157 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700158 }
159
Ian Rogers0571d352011-11-03 19:51:38 -0700160 uint32_t method_index = 0;
161 while (it.HasNextDirectMethod()) {
162 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index);
163 DumpOatMethod(os, method_index, oat_file, oat_method, dex_file, it.GetMemberIndex());
164 method_index++;
165 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700166 }
Ian Rogers0571d352011-11-03 19:51:38 -0700167 while (it.HasNextVirtualMethod()) {
168 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index);
169 DumpOatMethod(os, method_index, oat_file, oat_method, dex_file, it.GetMemberIndex());
170 method_index++;
171 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700172 }
Ian Rogers0571d352011-11-03 19:51:38 -0700173 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700174 os << std::flush;
175 }
176 static void DumpOatMethod(std::ostream& os,
177 uint32_t method_index,
178 const OatFile& oat_file,
179 const OatFile::OatMethod& oat_method,
180 const DexFile& dex_file,
Ian Rogers0571d352011-11-03 19:51:38 -0700181 uint32_t method_idx) {
182 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700183 const char* name = dex_file.GetMethodName(method_id);
Elliott Hughes95572412011-12-13 18:14:20 -0800184 std::string signature(dex_file.GetMethodSignature(method_id));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700185 os << StringPrintf("\t%d: %s %s (method_idx=%d)\n",
Ian Rogers0571d352011-11-03 19:51:38 -0700186 method_index, name, signature.c_str(), method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700187 os << StringPrintf("\t\tcode: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800188 oat_method.GetCode(), oat_method.GetCodeOffset());
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800189 os << StringPrintf("\t\tframe_size_in_bytes: %zd\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800190 oat_method.GetFrameSizeInBytes());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700191 os << StringPrintf("\t\tcore_spill_mask: %08x\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800192 oat_method.GetCoreSpillMask());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700193 os << StringPrintf("\t\tfp_spill_mask: %08x\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800194 oat_method.GetFpSpillMask());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700195 os << StringPrintf("\t\tmapping_table: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800196 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700197 os << StringPrintf("\t\tvmap_table: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800198 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800199 os << StringPrintf("\t\tgc_map: %p (offset=%08x)\n",
200 oat_method.GetGcMap(), oat_method.GetGcMapOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700201 os << StringPrintf("\t\tinvoke_stub: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800202 oat_method.GetInvokeStub(), oat_method.GetInvokeStubOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700203 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700204};
205
206class ImageDump {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700207 public:
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700208 static void Dump(const std::string& image_filename,
Brian Carlstromaded5f72011-10-07 17:15:04 -0700209 const std::string& host_prefix,
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700210 std::ostream& os,
211 Space& image_space,
212 const ImageHeader& image_header) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700213 os << "MAGIC:\n";
214 os << image_header.GetMagic() << "\n\n";
215
Ian Rogers30fab402012-01-23 15:43:46 -0800216 os << "IMAGE BEGIN:\n";
217 os << reinterpret_cast<void*>(image_header.GetImageBegin()) << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700218
Brian Carlstromaded5f72011-10-07 17:15:04 -0700219 os << "OAT CHECKSUM:\n";
220 os << StringPrintf("%08x\n\n", image_header.GetOatChecksum());
221
Ian Rogers30fab402012-01-23 15:43:46 -0800222 os << "OAT BEGIN:\n";
223 os << reinterpret_cast<void*>(image_header.GetOatBegin()) << "\n\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700224
Ian Rogers30fab402012-01-23 15:43:46 -0800225 os << "OAT END:\n";
226 os << reinterpret_cast<void*>(image_header.GetOatEnd()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700227
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700228 os << "ROOTS:\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700229 os << reinterpret_cast<void*>(image_header.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700230 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700231 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
232 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700233 const char* image_root_description = image_roots_descriptions_[i];
234 Object* image_root_object = image_header.GetImageRoot(image_root);
235 os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
236 if (image_root_object->IsObjectArray()) {
237 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
238 ObjectArray<Object>* image_root_object_array
239 = down_cast<ObjectArray<Object>*>(image_root_object);
240 // = image_root_object->AsObjectArray<Object>();
241 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
242 os << StringPrintf("\t%d: %p\n", i, image_root_object_array->Get(i));
243 }
244 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700245 }
246 os << "\n";
247
248 os << "OBJECTS:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700249 ImageDump state(image_space, os);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700250 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
251 DCHECK(heap_bitmap != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700252 heap_bitmap->Walk(ImageDump::Callback, &state);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700253 os << "\n";
254
255 os << "STATS:\n" << std::flush;
256 UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), false));
257 state.stats_.file_bytes = file->Length();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700258 size_t header_bytes = sizeof(ImageHeader);
259 state.stats_.header_bytes = header_bytes;
260 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
261 state.stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700262 state.stats_.Dump(os);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700263 os << "\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700264
265 os << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700266
267 os << "OAT LOCATION:\n" << std::flush;
268 Object* oat_location_object = image_header.GetImageRoot(ImageHeader::kOatLocation);
Elliott Hughes95572412011-12-13 18:14:20 -0800269 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700270 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
271 os << oat_location;
272 if (!host_prefix.empty()) {
273 oat_location = host_prefix + oat_location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700274 os << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700275 }
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700276 os << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800277 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700278 if (oat_file == NULL) {
279 os << "NOT FOUND\n";
280 os << std::flush;
281 return;
282 }
283 os << "\n";
284 os << std::flush;
285
Brian Carlstroma004aa92012-02-08 18:05:09 -0800286 OatDump::Dump(oat_location, os, *oat_file);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700287 }
288
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700289 private:
290
Brian Carlstromaded5f72011-10-07 17:15:04 -0700291 ImageDump(const Space& dump_space, std::ostream& os) : dump_space_(dump_space), os_(os) {}
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700292
Brian Carlstromaded5f72011-10-07 17:15:04 -0700293 ~ImageDump() {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700294
Brian Carlstrom78128a62011-09-15 17:21:19 -0700295 static void Callback(Object* obj, void* arg) {
296 DCHECK(obj != NULL);
297 DCHECK(arg != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700298 ImageDump* state = reinterpret_cast<ImageDump*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700299 if (!state->InDumpSpace(obj)) {
300 return;
301 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700302
303 size_t object_bytes = obj->SizeOf();
304 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
305 state->stats_.object_bytes += object_bytes;
306 state->stats_.alignment_bytes += alignment_bytes;
307
Brian Carlstrom78128a62011-09-15 17:21:19 -0700308 std::string summary;
309 StringAppendF(&summary, "%p: ", obj);
310 if (obj->IsClass()) {
311 Class* klass = obj->AsClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800312 summary += "CLASS ";
313 summary += ClassHelper(klass).GetDescriptor();
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700314 std::ostringstream ss;
Brian Carlstrome10b6972011-09-26 13:49:03 -0700315 ss << " (" << klass->GetStatus() << ")";
316 summary += ss.str();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700317 } else if (obj->IsMethod()) {
318 Method* method = obj->AsMethod();
319 StringAppendF(&summary, "METHOD %s", PrettyMethod(method).c_str());
320 } else if (obj->IsField()) {
321 Field* field = obj->AsField();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700322 StringAppendF(&summary, "FIELD %s", PrettyField(field).c_str());
323 } else if (obj->IsArrayInstance()) {
324 StringAppendF(&summary, "ARRAY %d", obj->AsArray()->GetLength());
Elliott Hughesdbb40792011-11-18 17:05:22 -0800325 } else if (obj->GetClass()->IsStringClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700326 StringAppendF(&summary, "STRING %s", obj->AsString()->ToModifiedUtf8().c_str());
327 } else {
328 StringAppendF(&summary, "OBJECT");
329 }
330 StringAppendF(&summary, "\n");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800331 std::string descriptor(ClassHelper(obj->GetClass()).GetDescriptor());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700332 StringAppendF(&summary, "\tclass %p: %s\n", obj->GetClass(), descriptor.c_str());
333 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
334 state->stats_.descriptor_to_count[descriptor] += 1;
335 // StringAppendF(&summary, "\tsize %d (alignment padding %d)\n",
336 // object_bytes, RoundUp(object_bytes, kObjectAlignment) - object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700337 if (obj->IsMethod()) {
338 Method* method = obj->AsMethod();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700339 if (!method->IsCalleeSaveMethod()) {
340 const int8_t* code =reinterpret_cast<const int8_t*>(method->GetCode());
341 StringAppendF(&summary, "\tCODE %p\n", code);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700342
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700343 const Method::InvokeStub* invoke_stub = method->GetInvokeStub();
344 StringAppendF(&summary, "\tJNI STUB %p\n", invoke_stub);
Ian Rogersff1ed472011-09-20 13:46:24 -0700345 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700346 if (method->IsNative()) {
347 if (method->IsRegistered()) {
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700348 StringAppendF(&summary, "\tNATIVE REGISTERED %p\n", method->GetNativeMethod());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700349 } else {
350 StringAppendF(&summary, "\tNATIVE UNREGISTERED\n");
351 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700352 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800353 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700354 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700355 } else if (method->IsAbstract()) {
356 StringAppendF(&summary, "\tABSTRACT\n");
Ian Rogersd81871c2011-10-03 13:57:23 -0700357 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800358 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700359 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
360 } else if (method->IsCalleeSaveMethod()) {
361 StringAppendF(&summary, "\tCALLEE SAVE METHOD\n");
Ian Rogersd81871c2011-10-03 13:57:23 -0700362 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800363 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700364 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700365 } else {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800366 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
367 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700368
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800369 size_t register_map_bytes = method->GetGcMapLength();
370 state->stats_.register_map_bytes += register_map_bytes;
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800371 StringAppendF(&summary, "GC=%zd ", register_map_bytes);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800372
373 size_t pc_mapping_table_bytes = method->GetMappingTableLength();
374 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800375 StringAppendF(&summary, "Mapping=%zd ", pc_mapping_table_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700376
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800377 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700378 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700379 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800380
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800381 StringAppendF(&summary, "\tSIZE Code=%zd GC=%zd Mapping=%zd",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800382 dex_instruction_bytes, register_map_bytes, pc_mapping_table_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700383 }
384 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700385 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700386 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700387
388 bool InDumpSpace(const Object* object) {
Ian Rogers30fab402012-01-23 15:43:46 -0800389 return dump_space_.Contains(object);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700390 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700391
392 public:
393 struct Stats {
394 size_t file_bytes;
395
396 size_t header_bytes;
397 size_t object_bytes;
398 size_t alignment_bytes;
399
400 size_t managed_code_bytes;
401 size_t managed_to_native_code_bytes;
402 size_t native_to_managed_code_bytes;
403
404 size_t register_map_bytes;
405 size_t pc_mapping_table_bytes;
406
407 size_t dex_instruction_bytes;
408
409 Stats()
410 : file_bytes(0),
411 header_bytes(0),
412 object_bytes(0),
413 alignment_bytes(0),
414 managed_code_bytes(0),
415 managed_to_native_code_bytes(0),
416 native_to_managed_code_bytes(0),
417 register_map_bytes(0),
418 pc_mapping_table_bytes(0),
419 dex_instruction_bytes(0) {}
420
Elliott Hughese5448b52012-01-18 16:44:06 -0800421 typedef std::map<std::string, size_t> TableBytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700422 TableBytes descriptor_to_bytes;
423
Elliott Hughese5448b52012-01-18 16:44:06 -0800424 typedef std::map<std::string, size_t> TableCount;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700425 TableCount descriptor_to_count;
426
427 double PercentOfFileBytes(size_t size) {
428 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
429 }
430
431 double PercentOfObjectBytes(size_t size) {
432 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
433 }
434
435 void Dump(std::ostream& os) {
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800436 os << StringPrintf("\tfile_bytes = %zd\n", file_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700437 os << "\n";
438
439 os << "\tfile_bytes = header_bytes + object_bytes + alignment_bytes\n";
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800440 os << StringPrintf("\theader_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700441 header_bytes, PercentOfFileBytes(header_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800442 os << StringPrintf("\tobject_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700443 object_bytes, PercentOfFileBytes(object_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800444 os << StringPrintf("\talignment_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700445 alignment_bytes, PercentOfFileBytes(alignment_bytes));
446 os << "\n";
447 os << std::flush;
448 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
449
450 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
451 size_t object_bytes_total = 0;
452 typedef TableBytes::const_iterator It; // TODO: C++0x auto
453 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -0800454 const std::string& descriptor(it->first);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700455 size_t bytes = it->second;
456 size_t count = descriptor_to_count[descriptor];
457 double average = static_cast<double>(bytes) / static_cast<double>(count);
458 double percent = PercentOfObjectBytes(bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800459 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700460 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
461 descriptor.c_str(), bytes, count,
462 average, percent);
463
464 object_bytes_total += bytes;
465 }
466 os << "\n";
467 os << std::flush;
468 CHECK_EQ(object_bytes, object_bytes_total);
469
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800470 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700471 managed_code_bytes, PercentOfObjectBytes(managed_code_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800472 os << StringPrintf("\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700473 managed_to_native_code_bytes,
474 PercentOfObjectBytes(managed_to_native_code_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800475 os << StringPrintf("\tnative_to_managed_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700476 native_to_managed_code_bytes,
477 PercentOfObjectBytes(native_to_managed_code_bytes));
478 os << "\n";
479 os << std::flush;
480
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800481 os << StringPrintf("\tregister_map_bytes = %7zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700482 register_map_bytes, PercentOfObjectBytes(register_map_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800483 os << StringPrintf("\tpc_mapping_table_bytes = %7zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700484 pc_mapping_table_bytes, PercentOfObjectBytes(pc_mapping_table_bytes));
485 os << "\n";
486 os << std::flush;
487
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800488 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700489 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f\n",
490 static_cast<double>(managed_code_bytes)
491 / static_cast<double>(dex_instruction_bytes));
492 os << "\n";
493 os << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700494 }
495 } stats_;
496
497 private:
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700498 const Space& dump_space_;
499 std::ostream& os_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700500
Brian Carlstromaded5f72011-10-07 17:15:04 -0700501 DISALLOW_COPY_AND_ASSIGN(ImageDump);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700502};
503
504int oatdump(int argc, char** argv) {
505 // Skip over argv[0].
506 argv++;
507 argc--;
508
509 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700510 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700511 usage();
512 }
513
Brian Carlstromaded5f72011-10-07 17:15:04 -0700514 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700515 const char* image_filename = NULL;
516 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700517 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700518 std::ostream* os = &std::cout;
519 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700520
521 for (int i = 0; i < argc; i++) {
522 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800523 if (option.starts_with("--oat-file=")) {
524 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700525 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700526 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700527 } else if (option.starts_with("--boot-image=")) {
528 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700529 } else if (option.starts_with("--host-prefix=")) {
530 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700531 } else if (option.starts_with("--output=")) {
532 const char* filename = option.substr(strlen("--output=")).data();
533 out.reset(new std::ofstream(filename));
534 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700535 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700536 usage();
537 }
538 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700539 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700540 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700541 usage();
542 }
543 }
544
Brian Carlstromaded5f72011-10-07 17:15:04 -0700545 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700546 fprintf(stderr, "Either --image or --oat must be specified\n");
547 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700548 }
549
Brian Carlstromaded5f72011-10-07 17:15:04 -0700550 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700551 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
552 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700553 }
554
555 if (oat_filename != NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800556 const OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700557 if (oat_file == NULL) {
558 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
559 return EXIT_FAILURE;
560 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800561 OatDump::Dump(oat_filename, *os, *oat_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700562 return EXIT_SUCCESS;
563 }
564
Brian Carlstrom78128a62011-09-15 17:21:19 -0700565 Runtime::Options options;
566 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700567 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700568 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700569 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700570 if (boot_image_filename != NULL) {
571 boot_image_option += "-Ximage:";
572 boot_image_option += boot_image_filename;
573 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -0700574 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700575 if (image_filename != NULL) {
576 image_option += "-Ximage:";
577 image_option += image_filename;
578 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
579 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700580
581 if (!host_prefix.empty()) {
582 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
583 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700584
585 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
586 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700587 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700588 return EXIT_FAILURE;
589 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700590
Ian Rogers30fab402012-01-23 15:43:46 -0800591 ImageSpace* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]->AsImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700592 CHECK(image_space != NULL);
593 const ImageHeader& image_header = image_space->GetImageHeader();
594 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700595 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700596 return EXIT_FAILURE;
597 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700598 ImageDump::Dump(image_filename, host_prefix, *os, *image_space, image_header);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700599 return EXIT_SUCCESS;
600}
601
602} // namespace art
603
604int main(int argc, char** argv) {
605 return art::oatdump(argc, argv);
606}