blob: 1bbbb085d9d77a294af4c1dd14d2c86c4a7c86fe [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++) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800242 Object* value = image_root_object_array->Get(i);
243 if (value != NULL) {
244 os << "\t" << i << ": ";
245 std::string summary;
246 PrettyObjectValue(summary, value->GetClass(), value);
247 os << summary;
248 } else {
249 os << StringPrintf("\t%d: null\n", i);
250 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700251 }
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 Carlstroma004aa92012-02-08 18:05:09 -0800294 OatDump::Dump(oat_location, 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
Ian Rogersd5b32602012-02-26 16:40:04 -0800303 static void PrettyObjectValue(std::string& summary, Class* type, Object* value) {
304 CHECK(type != NULL);
305 if (value == NULL) {
306 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
307 } else if (type->IsStringClass()) {
308 String* string = value->AsString();
309 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
310 } else if (value->IsClass()) {
311 Class* klass = value->AsClass();
312 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
313 } else if (value->IsField()) {
314 Field* field = value->AsField();
315 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
316 } else if (value->IsMethod()) {
317 Method* method = value->AsMethod();
318 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
319 } else {
320 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
321 }
322 }
323
324 static void PrintField(std::string& summary, Field* field, Object* obj) {
325 FieldHelper fh(field);
326 Class* type = fh.GetType();
327 StringAppendF(&summary, "\t%s: ", fh.GetName());
328 if (type->IsPrimitiveLong()) {
329 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
330 } else if (type->IsPrimitiveDouble()) {
331 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
332 } else if (type->IsPrimitiveFloat()) {
333 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
334 } else if (type->IsPrimitive()){
335 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
336 } else {
337 Object* value = field->GetObj(obj);
338 PrettyObjectValue(summary, type, value);
339 }
340 }
341
342 static void DumpFields(std::string& summary, Object* obj, Class* klass) {
343 Class* super = klass->GetSuperClass();
344 if (super != NULL) {
345 DumpFields(summary, obj, super);
346 }
347 ObjectArray<Field>* fields = klass->GetIFields();
348 if (fields != NULL) {
349 for (int32_t i = 0; i < fields->GetLength(); i++) {
350 Field* field = fields->Get(i);
351 PrintField(summary, field, obj);
352 }
353 }
354 }
355
Brian Carlstrom78128a62011-09-15 17:21:19 -0700356 static void Callback(Object* obj, void* arg) {
357 DCHECK(obj != NULL);
358 DCHECK(arg != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700359 ImageDump* state = reinterpret_cast<ImageDump*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700360 if (!state->InDumpSpace(obj)) {
361 return;
362 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700363
364 size_t object_bytes = obj->SizeOf();
365 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
366 state->stats_.object_bytes += object_bytes;
367 state->stats_.alignment_bytes += alignment_bytes;
368
Brian Carlstrom78128a62011-09-15 17:21:19 -0700369 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800370 Class* obj_class = obj->GetClass();
371 if (obj_class->IsArrayClass()) {
372 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
373 obj->AsArray()->GetLength());
374 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700375 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800376 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
377 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700378 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800379 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700380 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800381 } else if (obj->IsField()) {
382 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
383 PrettyField(obj->AsField()).c_str());
384 } else if (obj->IsMethod()) {
385 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
386 PrettyMethod(obj->AsMethod()).c_str());
387 } else if (obj_class->IsStringClass()) {
388 StringAppendF(&summary, "%p: java.lang.String \"%s\"\n", obj,
389 obj->AsString()->ToModifiedUtf8().c_str());
390 } else {
391 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
392 }
393 DumpFields(summary, obj, obj_class);
394 if (obj->IsObjectArray()) {
395 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
396 int32_t length = obj_array->GetLength();
397 for (int32_t i = 0; i < length; i++) {
398 Object* value = obj_array->Get(i);
399 size_t run = 0;
400 for (int32_t j = i + 1; j < length; j++) {
401 if (value == obj_array->Get(j)) {
402 run++;
403 } else {
404 break;
405 }
406 }
407 if (run == 0) {
408 StringAppendF(&summary, "\t%d: ", i);
409 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800410 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800411 i = i + run;
412 }
413 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
414 PrettyObjectValue(summary, value_class, value);
415 }
416 } else if (obj->IsClass()) {
417 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
418 if (sfields != NULL) {
419 summary += "\t\tSTATICS:\n";
420 for (int32_t i = 0; i < sfields->GetLength(); i++) {
421 Field* field = sfields->Get(i);
422 PrintField(summary, field, NULL);
423 }
424 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700425 } else if (obj->IsMethod()) {
426 Method* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700427 if (method->IsNative()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700428 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800429 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700430 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogersd5b32602012-02-26 16:40:04 -0800431 } else if (method->IsAbstract() || method->IsCalleeSaveMethod()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700432 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800433 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700434 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700435 } else {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800436 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
437 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700438
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800439 size_t register_map_bytes = method->GetGcMapLength();
440 state->stats_.register_map_bytes += register_map_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800441
442 size_t pc_mapping_table_bytes = method->GetMappingTableLength();
443 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700444
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800445 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700446 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700447 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800448
Ian Rogersd5b32602012-02-26 16:40:04 -0800449 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800450 dex_instruction_bytes, register_map_bytes, pc_mapping_table_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700451 }
452 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800453 std::string descriptor(ClassHelper(obj_class).GetDescriptor());
454 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
455 state->stats_.descriptor_to_count[descriptor] += 1;
456
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700457 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700458 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700459
460 bool InDumpSpace(const Object* object) {
Ian Rogers30fab402012-01-23 15:43:46 -0800461 return dump_space_.Contains(object);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700462 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700463
464 public:
465 struct Stats {
466 size_t file_bytes;
467
468 size_t header_bytes;
469 size_t object_bytes;
470 size_t alignment_bytes;
471
472 size_t managed_code_bytes;
473 size_t managed_to_native_code_bytes;
474 size_t native_to_managed_code_bytes;
475
476 size_t register_map_bytes;
477 size_t pc_mapping_table_bytes;
478
479 size_t dex_instruction_bytes;
480
481 Stats()
482 : file_bytes(0),
483 header_bytes(0),
484 object_bytes(0),
485 alignment_bytes(0),
486 managed_code_bytes(0),
487 managed_to_native_code_bytes(0),
488 native_to_managed_code_bytes(0),
489 register_map_bytes(0),
490 pc_mapping_table_bytes(0),
491 dex_instruction_bytes(0) {}
492
Elliott Hughese5448b52012-01-18 16:44:06 -0800493 typedef std::map<std::string, size_t> TableBytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700494 TableBytes descriptor_to_bytes;
495
Elliott Hughese5448b52012-01-18 16:44:06 -0800496 typedef std::map<std::string, size_t> TableCount;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700497 TableCount descriptor_to_count;
498
499 double PercentOfFileBytes(size_t size) {
500 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
501 }
502
503 double PercentOfObjectBytes(size_t size) {
504 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
505 }
506
507 void Dump(std::ostream& os) {
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800508 os << StringPrintf("\tfile_bytes = %zd\n", file_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700509 os << "\n";
510
511 os << "\tfile_bytes = header_bytes + object_bytes + alignment_bytes\n";
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800512 os << StringPrintf("\theader_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700513 header_bytes, PercentOfFileBytes(header_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800514 os << StringPrintf("\tobject_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700515 object_bytes, PercentOfFileBytes(object_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800516 os << StringPrintf("\talignment_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700517 alignment_bytes, PercentOfFileBytes(alignment_bytes));
518 os << "\n";
519 os << std::flush;
520 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
521
522 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
523 size_t object_bytes_total = 0;
524 typedef TableBytes::const_iterator It; // TODO: C++0x auto
525 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -0800526 const std::string& descriptor(it->first);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700527 size_t bytes = it->second;
528 size_t count = descriptor_to_count[descriptor];
529 double average = static_cast<double>(bytes) / static_cast<double>(count);
530 double percent = PercentOfObjectBytes(bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800531 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700532 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
533 descriptor.c_str(), bytes, count,
534 average, percent);
535
536 object_bytes_total += bytes;
537 }
538 os << "\n";
539 os << std::flush;
540 CHECK_EQ(object_bytes, object_bytes_total);
541
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800542 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700543 managed_code_bytes, PercentOfObjectBytes(managed_code_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800544 os << StringPrintf("\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700545 managed_to_native_code_bytes,
546 PercentOfObjectBytes(managed_to_native_code_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800547 os << StringPrintf("\tnative_to_managed_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700548 native_to_managed_code_bytes,
549 PercentOfObjectBytes(native_to_managed_code_bytes));
550 os << "\n";
551 os << std::flush;
552
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800553 os << StringPrintf("\tregister_map_bytes = %7zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700554 register_map_bytes, PercentOfObjectBytes(register_map_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800555 os << StringPrintf("\tpc_mapping_table_bytes = %7zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700556 pc_mapping_table_bytes, PercentOfObjectBytes(pc_mapping_table_bytes));
557 os << "\n";
558 os << std::flush;
559
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800560 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700561 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f\n",
562 static_cast<double>(managed_code_bytes)
563 / static_cast<double>(dex_instruction_bytes));
564 os << "\n";
565 os << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700566 }
567 } stats_;
568
569 private:
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700570 const Space& dump_space_;
571 std::ostream& os_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700572
Brian Carlstromaded5f72011-10-07 17:15:04 -0700573 DISALLOW_COPY_AND_ASSIGN(ImageDump);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700574};
575
576int oatdump(int argc, char** argv) {
577 // Skip over argv[0].
578 argv++;
579 argc--;
580
581 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700582 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700583 usage();
584 }
585
Brian Carlstromaded5f72011-10-07 17:15:04 -0700586 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700587 const char* image_filename = NULL;
588 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700589 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700590 std::ostream* os = &std::cout;
591 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700592
593 for (int i = 0; i < argc; i++) {
594 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800595 if (option.starts_with("--oat-file=")) {
596 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700597 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700598 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700599 } else if (option.starts_with("--boot-image=")) {
600 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700601 } else if (option.starts_with("--host-prefix=")) {
602 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700603 } else if (option.starts_with("--output=")) {
604 const char* filename = option.substr(strlen("--output=")).data();
605 out.reset(new std::ofstream(filename));
606 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700607 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700608 usage();
609 }
610 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700611 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700612 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700613 usage();
614 }
615 }
616
Brian Carlstromaded5f72011-10-07 17:15:04 -0700617 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700618 fprintf(stderr, "Either --image or --oat must be specified\n");
619 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700620 }
621
Brian Carlstromaded5f72011-10-07 17:15:04 -0700622 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700623 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
624 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700625 }
626
627 if (oat_filename != NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800628 const OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700629 if (oat_file == NULL) {
630 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
631 return EXIT_FAILURE;
632 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800633 OatDump::Dump(oat_filename, *os, *oat_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700634 return EXIT_SUCCESS;
635 }
636
Brian Carlstrom78128a62011-09-15 17:21:19 -0700637 Runtime::Options options;
638 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700639 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700640 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700641 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700642 if (boot_image_filename != NULL) {
643 boot_image_option += "-Ximage:";
644 boot_image_option += boot_image_filename;
645 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -0700646 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700647 if (image_filename != NULL) {
648 image_option += "-Ximage:";
649 image_option += image_filename;
650 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
651 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700652
653 if (!host_prefix.empty()) {
654 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
655 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700656
657 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
658 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700659 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700660 return EXIT_FAILURE;
661 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700662
Ian Rogers30fab402012-01-23 15:43:46 -0800663 ImageSpace* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]->AsImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700664 CHECK(image_space != NULL);
665 const ImageHeader& image_header = image_space->GetImageHeader();
666 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700667 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700668 return EXIT_FAILURE;
669 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700670 ImageDump::Dump(image_filename, host_prefix, *os, *image_space, image_header);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700671 return EXIT_SUCCESS;
672}
673
674} // namespace art
675
676int main(int argc, char** argv) {
677 return art::oatdump(argc, argv);
678}