blob: 7c9fa92272cc51fff545e10f5f76698ccc808d86 [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",
Ian Rogers19846512012-02-24 11:42:47 -080073 "kResolutionMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070074 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070075 "kRefsOnlySaveMethod",
76 "kRefsAndArgsSaveMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070077 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070078 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070079 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070080};
81
Brian Carlstrom27ec9612011-09-19 20:20:38 -070082class OatDump {
Brian Carlstromaded5f72011-10-07 17:15:04 -070083 public:
84 static void Dump(const std::string& oat_filename,
Brian Carlstromaded5f72011-10-07 17:15:04 -070085 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);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800113 DumpOatDexFile(os, oat_file, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700114 }
115 }
116
117 private:
Brian Carlstroma004aa92012-02-08 18:05:09 -0800118 static void DumpOatDexFile(std::ostream& os,
Brian Carlstromaded5f72011-10-07 17:15:04 -0700119 const OatFile& oat_file,
120 const OatFile::OatDexFile& oat_dex_file) {
121 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800122 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800123 os << StringPrintf("checksum: %08x\n", oat_dex_file.GetDexFileLocationChecksum());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800124 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile());
125 if (dex_file.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700126 os << "NOT FOUND\n\n";
127 return;
128 }
129 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
130 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
131 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700132 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
133 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800134 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800135 << oat_class->GetStatus() << ")\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800136 DumpOatClass(os, oat_file, *oat_class.get(), *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700137 }
138
139 os << std::flush;
140 }
141
142 static void DumpOatClass(std::ostream& os,
143 const OatFile& oat_file,
144 const OatFile::OatClass& oat_class,
145 const DexFile& dex_file,
146 const DexFile::ClassDef& class_def) {
147 const byte* class_data = dex_file.GetClassData(class_def);
Ian Rogers0571d352011-11-03 19:51:38 -0700148 if (class_data == NULL) { // empty class such as a marker interface?
149 return;
150 }
151 ClassDataItemIterator it(dex_file, class_data);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700152
153 // just skipping through the fields to advance class_data
Ian Rogers0571d352011-11-03 19:51:38 -0700154 while (it.HasNextStaticField()) {
155 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700156 }
Ian Rogers0571d352011-11-03 19:51:38 -0700157 while (it.HasNextInstanceField()) {
158 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700159 }
160
Ian Rogers0571d352011-11-03 19:51:38 -0700161 uint32_t method_index = 0;
162 while (it.HasNextDirectMethod()) {
163 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index);
164 DumpOatMethod(os, method_index, oat_file, oat_method, dex_file, it.GetMemberIndex());
165 method_index++;
166 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700167 }
Ian Rogers0571d352011-11-03 19:51:38 -0700168 while (it.HasNextVirtualMethod()) {
169 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index);
170 DumpOatMethod(os, method_index, oat_file, oat_method, dex_file, it.GetMemberIndex());
171 method_index++;
172 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700173 }
Ian Rogers0571d352011-11-03 19:51:38 -0700174 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700175 os << std::flush;
176 }
177 static void DumpOatMethod(std::ostream& os,
178 uint32_t method_index,
179 const OatFile& oat_file,
180 const OatFile::OatMethod& oat_method,
181 const DexFile& dex_file,
Ian Rogers0571d352011-11-03 19:51:38 -0700182 uint32_t method_idx) {
183 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700184 const char* name = dex_file.GetMethodName(method_id);
Elliott Hughes95572412011-12-13 18:14:20 -0800185 std::string signature(dex_file.GetMethodSignature(method_id));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700186 os << StringPrintf("\t%d: %s %s (method_idx=%d)\n",
Ian Rogers0571d352011-11-03 19:51:38 -0700187 method_index, name, signature.c_str(), method_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700188 os << StringPrintf("\t\tcode: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800189 oat_method.GetCode(), oat_method.GetCodeOffset());
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800190 os << StringPrintf("\t\tframe_size_in_bytes: %zd\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800191 oat_method.GetFrameSizeInBytes());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700192 os << StringPrintf("\t\tcore_spill_mask: %08x\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800193 oat_method.GetCoreSpillMask());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700194 os << StringPrintf("\t\tfp_spill_mask: %08x\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800195 oat_method.GetFpSpillMask());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700196 os << StringPrintf("\t\tmapping_table: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800197 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700198 os << StringPrintf("\t\tvmap_table: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800199 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800200 os << StringPrintf("\t\tgc_map: %p (offset=%08x)\n",
201 oat_method.GetGcMap(), oat_method.GetGcMapOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700202 os << StringPrintf("\t\tinvoke_stub: %p (offset=%08x)\n",
Brian Carlstromae826982011-11-09 01:33:42 -0800203 oat_method.GetInvokeStub(), oat_method.GetInvokeStubOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700204 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700205};
206
207class ImageDump {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700208 public:
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700209 static void Dump(const std::string& image_filename,
Brian Carlstromaded5f72011-10-07 17:15:04 -0700210 const std::string& host_prefix,
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700211 std::ostream& os,
212 Space& image_space,
213 const ImageHeader& image_header) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700214 os << "MAGIC:\n";
215 os << image_header.GetMagic() << "\n\n";
216
Ian Rogers30fab402012-01-23 15:43:46 -0800217 os << "IMAGE BEGIN:\n";
218 os << reinterpret_cast<void*>(image_header.GetImageBegin()) << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700219
Brian Carlstromaded5f72011-10-07 17:15:04 -0700220 os << "OAT CHECKSUM:\n";
221 os << StringPrintf("%08x\n\n", image_header.GetOatChecksum());
222
Ian Rogers30fab402012-01-23 15:43:46 -0800223 os << "OAT BEGIN:\n";
224 os << reinterpret_cast<void*>(image_header.GetOatBegin()) << "\n\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700225
Ian Rogers30fab402012-01-23 15:43:46 -0800226 os << "OAT END:\n";
227 os << reinterpret_cast<void*>(image_header.GetOatEnd()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700228
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700229 os << "ROOTS:\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700230 os << reinterpret_cast<void*>(image_header.GetImageRoots()) << "\n";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700231 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700232 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
233 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700234 const char* image_root_description = image_roots_descriptions_[i];
235 Object* image_root_object = image_header.GetImageRoot(image_root);
236 os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
237 if (image_root_object->IsObjectArray()) {
238 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
239 ObjectArray<Object>* image_root_object_array
240 = down_cast<ObjectArray<Object>*>(image_root_object);
241 // = image_root_object->AsObjectArray<Object>();
242 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800243 Object* value = image_root_object_array->Get(i);
244 if (value != NULL) {
245 os << "\t" << i << ": ";
246 std::string summary;
247 PrettyObjectValue(summary, value->GetClass(), value);
248 os << summary;
249 } else {
250 os << StringPrintf("\t%d: null\n", i);
251 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700252 }
253 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700254 }
255 os << "\n";
256
257 os << "OBJECTS:\n" << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700258 ImageDump state(image_space, os);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700259 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
260 DCHECK(heap_bitmap != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700261 heap_bitmap->Walk(ImageDump::Callback, &state);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700262 os << "\n";
263
264 os << "STATS:\n" << std::flush;
265 UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), false));
266 state.stats_.file_bytes = file->Length();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700267 size_t header_bytes = sizeof(ImageHeader);
268 state.stats_.header_bytes = header_bytes;
269 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
270 state.stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700271 state.stats_.Dump(os);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700272 os << "\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700273
274 os << std::flush;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700275
276 os << "OAT LOCATION:\n" << std::flush;
277 Object* oat_location_object = image_header.GetImageRoot(ImageHeader::kOatLocation);
Elliott Hughes95572412011-12-13 18:14:20 -0800278 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700279 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
280 os << oat_location;
281 if (!host_prefix.empty()) {
282 oat_location = host_prefix + oat_location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700283 os << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700284 }
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700285 os << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800286 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700287 if (oat_file == NULL) {
288 os << "NOT FOUND\n";
289 os << std::flush;
290 return;
291 }
292 os << "\n";
293 os << std::flush;
294
Brian Carlstroma004aa92012-02-08 18:05:09 -0800295 OatDump::Dump(oat_location, os, *oat_file);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700296 }
297
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700298 private:
299
Brian Carlstromaded5f72011-10-07 17:15:04 -0700300 ImageDump(const Space& dump_space, std::ostream& os) : dump_space_(dump_space), os_(os) {}
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700301
Brian Carlstromaded5f72011-10-07 17:15:04 -0700302 ~ImageDump() {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700303
Ian Rogersd5b32602012-02-26 16:40:04 -0800304 static void PrettyObjectValue(std::string& summary, Class* type, Object* value) {
305 CHECK(type != NULL);
306 if (value == NULL) {
307 StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str());
308 } else if (type->IsStringClass()) {
309 String* string = value->AsString();
310 StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str());
311 } else if (value->IsClass()) {
312 Class* klass = value->AsClass();
313 StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
314 } else if (value->IsField()) {
315 Field* field = value->AsField();
316 StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str());
317 } else if (value->IsMethod()) {
318 Method* method = value->AsMethod();
319 StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str());
320 } else {
321 StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str());
322 }
323 }
324
325 static void PrintField(std::string& summary, Field* field, Object* obj) {
326 FieldHelper fh(field);
327 Class* type = fh.GetType();
328 StringAppendF(&summary, "\t%s: ", fh.GetName());
329 if (type->IsPrimitiveLong()) {
330 StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
331 } else if (type->IsPrimitiveDouble()) {
332 StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
333 } else if (type->IsPrimitiveFloat()) {
334 StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
335 } else if (type->IsPrimitive()){
336 StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
337 } else {
338 Object* value = field->GetObj(obj);
339 PrettyObjectValue(summary, type, value);
340 }
341 }
342
343 static void DumpFields(std::string& summary, Object* obj, Class* klass) {
344 Class* super = klass->GetSuperClass();
345 if (super != NULL) {
346 DumpFields(summary, obj, super);
347 }
348 ObjectArray<Field>* fields = klass->GetIFields();
349 if (fields != NULL) {
350 for (int32_t i = 0; i < fields->GetLength(); i++) {
351 Field* field = fields->Get(i);
352 PrintField(summary, field, obj);
353 }
354 }
355 }
356
Brian Carlstrom78128a62011-09-15 17:21:19 -0700357 static void Callback(Object* obj, void* arg) {
358 DCHECK(obj != NULL);
359 DCHECK(arg != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700360 ImageDump* state = reinterpret_cast<ImageDump*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700361 if (!state->InDumpSpace(obj)) {
362 return;
363 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700364
365 size_t object_bytes = obj->SizeOf();
366 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
367 state->stats_.object_bytes += object_bytes;
368 state->stats_.alignment_bytes += alignment_bytes;
369
Brian Carlstrom78128a62011-09-15 17:21:19 -0700370 std::string summary;
Ian Rogersd5b32602012-02-26 16:40:04 -0800371 Class* obj_class = obj->GetClass();
372 if (obj_class->IsArrayClass()) {
373 StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
374 obj->AsArray()->GetLength());
375 } else if (obj->IsClass()) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700376 Class* klass = obj->AsClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800377 StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj,
378 PrettyDescriptor(klass).c_str());
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700379 std::ostringstream ss;
Ian Rogersd5b32602012-02-26 16:40:04 -0800380 ss << klass->GetStatus() << ")\n";
Brian Carlstrome10b6972011-09-26 13:49:03 -0700381 summary += ss.str();
Ian Rogersd5b32602012-02-26 16:40:04 -0800382 } else if (obj->IsField()) {
383 StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj,
384 PrettyField(obj->AsField()).c_str());
385 } else if (obj->IsMethod()) {
386 StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj,
387 PrettyMethod(obj->AsMethod()).c_str());
388 } else if (obj_class->IsStringClass()) {
389 StringAppendF(&summary, "%p: java.lang.String \"%s\"\n", obj,
390 obj->AsString()->ToModifiedUtf8().c_str());
391 } else {
392 StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
393 }
394 DumpFields(summary, obj, obj_class);
395 if (obj->IsObjectArray()) {
396 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
397 int32_t length = obj_array->GetLength();
398 for (int32_t i = 0; i < length; i++) {
399 Object* value = obj_array->Get(i);
400 size_t run = 0;
401 for (int32_t j = i + 1; j < length; j++) {
402 if (value == obj_array->Get(j)) {
403 run++;
404 } else {
405 break;
406 }
407 }
408 if (run == 0) {
409 StringAppendF(&summary, "\t%d: ", i);
410 } else {
Elliott Hughesc1051ae2012-02-27 12:52:31 -0800411 StringAppendF(&summary, "\t%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800412 i = i + run;
413 }
414 Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
415 PrettyObjectValue(summary, value_class, value);
416 }
417 } else if (obj->IsClass()) {
418 ObjectArray<Field>* sfields = obj->AsClass()->GetSFields();
419 if (sfields != NULL) {
420 summary += "\t\tSTATICS:\n";
421 for (int32_t i = 0; i < sfields->GetLength(); i++) {
422 Field* field = sfields->Get(i);
423 PrintField(summary, field, NULL);
424 }
425 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700426 } else if (obj->IsMethod()) {
427 Method* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700428 if (method->IsNative()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700429 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800430 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700431 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers19846512012-02-24 11:42:47 -0800432 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
433 method->IsResolutionMethod()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700434 DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800435 DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700436 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700437 } else {
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800438 DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method);
439 DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700440
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800441 size_t register_map_bytes = method->GetGcMapLength();
442 state->stats_.register_map_bytes += register_map_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800443
444 size_t pc_mapping_table_bytes = method->GetMappingTableLength();
445 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700446
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800447 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700448 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700449 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800450
Ian Rogersd5b32602012-02-26 16:40:04 -0800451 StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800452 dex_instruction_bytes, register_map_bytes, pc_mapping_table_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700453 }
454 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800455 std::string descriptor(ClassHelper(obj_class).GetDescriptor());
456 state->stats_.descriptor_to_bytes[descriptor] += object_bytes;
457 state->stats_.descriptor_to_count[descriptor] += 1;
458
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700459 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700460 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700461
462 bool InDumpSpace(const Object* object) {
Ian Rogers30fab402012-01-23 15:43:46 -0800463 return dump_space_.Contains(object);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700464 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700465
466 public:
467 struct Stats {
468 size_t file_bytes;
469
470 size_t header_bytes;
471 size_t object_bytes;
472 size_t alignment_bytes;
473
474 size_t managed_code_bytes;
475 size_t managed_to_native_code_bytes;
476 size_t native_to_managed_code_bytes;
477
478 size_t register_map_bytes;
479 size_t pc_mapping_table_bytes;
480
481 size_t dex_instruction_bytes;
482
483 Stats()
484 : file_bytes(0),
485 header_bytes(0),
486 object_bytes(0),
487 alignment_bytes(0),
488 managed_code_bytes(0),
489 managed_to_native_code_bytes(0),
490 native_to_managed_code_bytes(0),
491 register_map_bytes(0),
492 pc_mapping_table_bytes(0),
493 dex_instruction_bytes(0) {}
494
Elliott Hughese5448b52012-01-18 16:44:06 -0800495 typedef std::map<std::string, size_t> TableBytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700496 TableBytes descriptor_to_bytes;
497
Elliott Hughese5448b52012-01-18 16:44:06 -0800498 typedef std::map<std::string, size_t> TableCount;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700499 TableCount descriptor_to_count;
500
501 double PercentOfFileBytes(size_t size) {
502 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
503 }
504
505 double PercentOfObjectBytes(size_t size) {
506 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
507 }
508
509 void Dump(std::ostream& os) {
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800510 os << StringPrintf("\tfile_bytes = %zd\n", file_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700511 os << "\n";
512
513 os << "\tfile_bytes = header_bytes + object_bytes + alignment_bytes\n";
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800514 os << StringPrintf("\theader_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700515 header_bytes, PercentOfFileBytes(header_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800516 os << StringPrintf("\tobject_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700517 object_bytes, PercentOfFileBytes(object_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800518 os << StringPrintf("\talignment_bytes = %10zd (%2.0f%% of file_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700519 alignment_bytes, PercentOfFileBytes(alignment_bytes));
520 os << "\n";
521 os << std::flush;
522 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
523
524 os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n";
525 size_t object_bytes_total = 0;
526 typedef TableBytes::const_iterator It; // TODO: C++0x auto
527 for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -0800528 const std::string& descriptor(it->first);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700529 size_t bytes = it->second;
530 size_t count = descriptor_to_count[descriptor];
531 double average = static_cast<double>(bytes) / static_cast<double>(count);
532 double percent = PercentOfObjectBytes(bytes);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800533 os << StringPrintf("\t%32s %8zd bytes %6zd instances "
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700534 "(%3.0f bytes/instance) %2.0f%% of object_bytes\n",
535 descriptor.c_str(), bytes, count,
536 average, percent);
537
538 object_bytes_total += bytes;
539 }
540 os << "\n";
541 os << std::flush;
542 CHECK_EQ(object_bytes, object_bytes_total);
543
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800544 os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700545 managed_code_bytes, PercentOfObjectBytes(managed_code_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800546 os << StringPrintf("\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700547 managed_to_native_code_bytes,
548 PercentOfObjectBytes(managed_to_native_code_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800549 os << StringPrintf("\tnative_to_managed_code_bytes = %8zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700550 native_to_managed_code_bytes,
551 PercentOfObjectBytes(native_to_managed_code_bytes));
552 os << "\n";
553 os << std::flush;
554
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800555 os << StringPrintf("\tregister_map_bytes = %7zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700556 register_map_bytes, PercentOfObjectBytes(register_map_bytes));
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800557 os << StringPrintf("\tpc_mapping_table_bytes = %7zd (%2.0f%% of object_bytes)\n",
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700558 pc_mapping_table_bytes, PercentOfObjectBytes(pc_mapping_table_bytes));
559 os << "\n";
560 os << std::flush;
561
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800562 os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes);
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700563 os << StringPrintf("\tmanaged_code_bytes expansion = %.2f\n",
564 static_cast<double>(managed_code_bytes)
565 / static_cast<double>(dex_instruction_bytes));
566 os << "\n";
567 os << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700568 }
569 } stats_;
570
571 private:
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700572 const Space& dump_space_;
573 std::ostream& os_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -0700574
Brian Carlstromaded5f72011-10-07 17:15:04 -0700575 DISALLOW_COPY_AND_ASSIGN(ImageDump);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700576};
577
578int oatdump(int argc, char** argv) {
579 // Skip over argv[0].
580 argv++;
581 argc--;
582
583 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700584 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700585 usage();
586 }
587
Brian Carlstromaded5f72011-10-07 17:15:04 -0700588 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700589 const char* image_filename = NULL;
590 const char* boot_image_filename = NULL;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700591 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700592 std::ostream* os = &std::cout;
593 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700594
595 for (int i = 0; i < argc; i++) {
596 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -0800597 if (option.starts_with("--oat-file=")) {
598 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700599 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700600 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700601 } else if (option.starts_with("--boot-image=")) {
602 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700603 } else if (option.starts_with("--host-prefix=")) {
604 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700605 } else if (option.starts_with("--output=")) {
606 const char* filename = option.substr(strlen("--output=")).data();
607 out.reset(new std::ofstream(filename));
608 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700609 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700610 usage();
611 }
612 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700613 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700614 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -0700615 usage();
616 }
617 }
618
Brian Carlstromaded5f72011-10-07 17:15:04 -0700619 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700620 fprintf(stderr, "Either --image or --oat must be specified\n");
621 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700622 }
623
Brian Carlstromaded5f72011-10-07 17:15:04 -0700624 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700625 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
626 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700627 }
628
629 if (oat_filename != NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800630 const OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700631 if (oat_file == NULL) {
632 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
633 return EXIT_FAILURE;
634 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800635 OatDump::Dump(oat_filename, *os, *oat_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700636 return EXIT_SUCCESS;
637 }
638
Brian Carlstrom78128a62011-09-15 17:21:19 -0700639 Runtime::Options options;
640 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700641 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700642 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700643 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700644 if (boot_image_filename != NULL) {
645 boot_image_option += "-Ximage:";
646 boot_image_option += boot_image_filename;
647 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -0700648 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700649 if (image_filename != NULL) {
650 image_option += "-Ximage:";
651 image_option += image_filename;
652 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
653 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700654
655 if (!host_prefix.empty()) {
656 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
657 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700658
659 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
660 if (runtime.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700661 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700662 return EXIT_FAILURE;
663 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700664
Ian Rogers30fab402012-01-23 15:43:46 -0800665 ImageSpace* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]->AsImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700666 CHECK(image_space != NULL);
667 const ImageHeader& image_header = image_space->GetImageHeader();
668 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700669 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700670 return EXIT_FAILURE;
671 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700672 ImageDump::Dump(image_filename, host_prefix, *os, *image_space, image_header);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700673 return EXIT_SUCCESS;
674}
675
676} // namespace art
677
678int main(int argc, char** argv) {
679 return art::oatdump(argc, argv);
680}