Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 6 | #include <fstream> |
| 7 | #include <iostream> |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 8 | #include <map> |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "class_linker.h" |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 13 | #include "file.h" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 14 | #include "image.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 15 | #include "object_utils.h" |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 16 | #include "os.h" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 17 | #include "runtime.h" |
| 18 | #include "space.h" |
| 19 | #include "stringpiece.h" |
| 20 | |
| 21 | namespace art { |
| 22 | |
| 23 | static void usage() { |
| 24 | fprintf(stderr, |
| 25 | "Usage: oatdump [options] ...\n" |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 26 | " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n" |
| 27 | " Example: adb shell oatdump --image=/system/framework/boot.art\n" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 28 | "\n"); |
| 29 | fprintf(stderr, |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 30 | " --oat-file=<file.oat>: specifies an input oat filename.\n" |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 31 | " Example: --image=/system/framework/boot.oat\n" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 32 | "\n"); |
| 33 | fprintf(stderr, |
| 34 | " --image=<file.art>: specifies an input image filename.\n" |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 35 | " Example: --image=/system/framework/boot.art\n" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 36 | "\n"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 37 | fprintf(stderr, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 38 | " --boot-image=<file.art>: provide the image file for the boot class path.\n" |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 39 | " Example: --boot-image=/system/framework/boot.art\n" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 40 | "\n"); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 41 | fprintf(stderr, |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 42 | " --host-prefix may be used to translate host paths to target paths during\n" |
| 43 | " cross compilation.\n" |
| 44 | " Example: --host-prefix=out/target/product/crespo\n" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 45 | "\n"); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 46 | fprintf(stderr, |
| 47 | " --output=<file> may be used to send the output to a file.\n" |
| 48 | " Example: --output=/tmp/oatdump.txt\n" |
| 49 | "\n"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 50 | exit(EXIT_FAILURE); |
| 51 | } |
| 52 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 53 | const char* image_roots_descriptions_[] = { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 54 | "kJniStubArray", |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 55 | "kAbstractMethodErrorStubArray", |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 56 | "kInstanceResolutionStubArray", |
| 57 | "kStaticResolutionStubArray", |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 58 | "kUnknownMethodResolutionStubArray", |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 59 | "kCalleeSaveMethod", |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 60 | "kRefsOnlySaveMethod", |
| 61 | "kRefsAndArgsSaveMethod", |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 62 | "kOatLocation", |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 63 | "kDexCaches", |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 64 | "kClassRoots", |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 67 | class OatDump { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 68 | public: |
| 69 | static void Dump(const std::string& oat_filename, |
| 70 | const std::string& host_prefix, |
| 71 | std::ostream& os, |
| 72 | const OatFile& oat_file) { |
| 73 | const OatHeader& oat_header = oat_file.GetOatHeader(); |
| 74 | |
| 75 | os << "MAGIC:\n"; |
| 76 | os << oat_header.GetMagic() << "\n\n"; |
| 77 | |
| 78 | os << "CHECKSUM:\n"; |
| 79 | os << StringPrintf("%08x\n\n", oat_header.GetChecksum()); |
| 80 | |
| 81 | os << "DEX FILE COUNT:\n"; |
| 82 | os << oat_header.GetDexFileCount() << "\n\n"; |
| 83 | |
| 84 | os << "EXECUTABLE OFFSET:\n"; |
| 85 | os << StringPrintf("%08x\n\n", oat_header.GetExecutableOffset()); |
| 86 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame^] | 87 | os << "BEGIN:\n"; |
| 88 | os << reinterpret_cast<const void*>(oat_file.Begin()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 89 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame^] | 90 | os << "END:\n"; |
| 91 | os << reinterpret_cast<const void*>(oat_file.End()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 92 | |
| 93 | os << std::flush; |
| 94 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 95 | std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file.GetOatDexFiles(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 96 | for (size_t i = 0; i < oat_dex_files.size(); i++) { |
| 97 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i]; |
| 98 | CHECK(oat_dex_file != NULL); |
| 99 | DumpOatDexFile(host_prefix, os, oat_file, *oat_dex_file); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private: |
| 104 | static void DumpOatDexFile(const std::string& host_prefix, |
| 105 | std::ostream& os, |
| 106 | const OatFile& oat_file, |
| 107 | const OatFile::OatDexFile& oat_dex_file) { |
| 108 | os << "OAT DEX FILE:\n"; |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 109 | std::string dex_file_location(oat_dex_file.GetDexFileLocation()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 110 | os << "location: " << dex_file_location; |
| 111 | if (!host_prefix.empty()) { |
| 112 | dex_file_location = host_prefix + dex_file_location; |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 113 | os << " (" << dex_file_location << ")"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 114 | } |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 115 | os << "\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 116 | os << StringPrintf("checksum: %08x\n", oat_dex_file.GetDexFileChecksum()); |
| 117 | const DexFile* dex_file = DexFile::Open(dex_file_location, ""); |
| 118 | if (dex_file == NULL) { |
| 119 | os << "NOT FOUND\n\n"; |
| 120 | return; |
| 121 | } |
| 122 | for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) { |
| 123 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 124 | const char* descriptor = dex_file->GetClassDescriptor(class_def); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 125 | UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index)); |
| 126 | CHECK(oat_class.get() != NULL); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 127 | os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_) |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 128 | << oat_class->GetStatus() << ")\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 129 | DumpOatClass(os, oat_file, *oat_class.get(), *dex_file, class_def); |
| 130 | } |
| 131 | |
| 132 | os << std::flush; |
| 133 | } |
| 134 | |
| 135 | static void DumpOatClass(std::ostream& os, |
| 136 | const OatFile& oat_file, |
| 137 | const OatFile::OatClass& oat_class, |
| 138 | const DexFile& dex_file, |
| 139 | const DexFile::ClassDef& class_def) { |
| 140 | const byte* class_data = dex_file.GetClassData(class_def); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 141 | if (class_data == NULL) { // empty class such as a marker interface? |
| 142 | return; |
| 143 | } |
| 144 | ClassDataItemIterator it(dex_file, class_data); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 145 | |
| 146 | // just skipping through the fields to advance class_data |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 147 | while (it.HasNextStaticField()) { |
| 148 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 149 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 150 | while (it.HasNextInstanceField()) { |
| 151 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 154 | uint32_t method_index = 0; |
| 155 | while (it.HasNextDirectMethod()) { |
| 156 | const OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index); |
| 157 | DumpOatMethod(os, method_index, oat_file, oat_method, dex_file, it.GetMemberIndex()); |
| 158 | method_index++; |
| 159 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 160 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 161 | while (it.HasNextVirtualMethod()) { |
| 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 Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 166 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 167 | DCHECK(!it.HasNext()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 168 | os << std::flush; |
| 169 | } |
| 170 | static void DumpOatMethod(std::ostream& os, |
| 171 | uint32_t method_index, |
| 172 | const OatFile& oat_file, |
| 173 | const OatFile::OatMethod& oat_method, |
| 174 | const DexFile& dex_file, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 175 | uint32_t method_idx) { |
| 176 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 177 | const char* name = dex_file.GetMethodName(method_id); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 178 | std::string signature(dex_file.GetMethodSignature(method_id)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 179 | os << StringPrintf("\t%d: %s %s (method_idx=%d)\n", |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 180 | method_index, name, signature.c_str(), method_idx); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 181 | os << StringPrintf("\t\tcode: %p (offset=%08x)\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 182 | oat_method.GetCode(), oat_method.GetCodeOffset()); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 183 | os << StringPrintf("\t\tframe_size_in_bytes: %zd\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 184 | oat_method.GetFrameSizeInBytes()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 185 | os << StringPrintf("\t\tcore_spill_mask: %08x\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 186 | oat_method.GetCoreSpillMask()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 187 | os << StringPrintf("\t\tfp_spill_mask: %08x\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 188 | oat_method.GetFpSpillMask()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 189 | os << StringPrintf("\t\tmapping_table: %p (offset=%08x)\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 190 | oat_method.GetMappingTable(), oat_method.GetMappingTableOffset()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 191 | os << StringPrintf("\t\tvmap_table: %p (offset=%08x)\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 192 | oat_method.GetVmapTable(), oat_method.GetVmapTableOffset()); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 193 | os << StringPrintf("\t\tgc_map: %p (offset=%08x)\n", |
| 194 | oat_method.GetGcMap(), oat_method.GetGcMapOffset()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 195 | os << StringPrintf("\t\tinvoke_stub: %p (offset=%08x)\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 196 | oat_method.GetInvokeStub(), oat_method.GetInvokeStubOffset()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 197 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | class ImageDump { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 201 | public: |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 202 | static void Dump(const std::string& image_filename, |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 203 | const std::string& host_prefix, |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 204 | std::ostream& os, |
| 205 | Space& image_space, |
| 206 | const ImageHeader& image_header) { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 207 | os << "MAGIC:\n"; |
| 208 | os << image_header.GetMagic() << "\n\n"; |
| 209 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame^] | 210 | os << "IMAGE BEGIN:\n"; |
| 211 | os << reinterpret_cast<void*>(image_header.GetImageBegin()) << "\n\n"; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 212 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 213 | os << "OAT CHECKSUM:\n"; |
| 214 | os << StringPrintf("%08x\n\n", image_header.GetOatChecksum()); |
| 215 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame^] | 216 | os << "OAT BEGIN:\n"; |
| 217 | os << reinterpret_cast<void*>(image_header.GetOatBegin()) << "\n\n"; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 218 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame^] | 219 | os << "OAT END:\n"; |
| 220 | os << reinterpret_cast<void*>(image_header.GetOatEnd()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 221 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 222 | os << "ROOTS:\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 223 | os << reinterpret_cast<void*>(image_header.GetImageRoots()) << "\n"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 224 | CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax)); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 225 | for (int i = 0; i < ImageHeader::kImageRootsMax; i++) { |
| 226 | ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i); |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 227 | const char* image_root_description = image_roots_descriptions_[i]; |
| 228 | Object* image_root_object = image_header.GetImageRoot(image_root); |
| 229 | os << StringPrintf("%s: %p\n", image_root_description, image_root_object); |
| 230 | if (image_root_object->IsObjectArray()) { |
| 231 | // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this) |
| 232 | ObjectArray<Object>* image_root_object_array |
| 233 | = down_cast<ObjectArray<Object>*>(image_root_object); |
| 234 | // = image_root_object->AsObjectArray<Object>(); |
| 235 | for (int i = 0; i < image_root_object_array->GetLength(); i++) { |
| 236 | os << StringPrintf("\t%d: %p\n", i, image_root_object_array->Get(i)); |
| 237 | } |
| 238 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 239 | } |
| 240 | os << "\n"; |
| 241 | |
| 242 | os << "OBJECTS:\n" << std::flush; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 243 | ImageDump state(image_space, os); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 244 | HeapBitmap* heap_bitmap = Heap::GetLiveBits(); |
| 245 | DCHECK(heap_bitmap != NULL); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 246 | heap_bitmap->Walk(ImageDump::Callback, &state); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 247 | os << "\n"; |
| 248 | |
| 249 | os << "STATS:\n" << std::flush; |
| 250 | UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), false)); |
| 251 | state.stats_.file_bytes = file->Length(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 252 | size_t header_bytes = sizeof(ImageHeader); |
| 253 | state.stats_.header_bytes = header_bytes; |
| 254 | size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes; |
| 255 | state.stats_.alignment_bytes += alignment_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 256 | state.stats_.Dump(os); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 257 | os << "\n"; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 258 | |
| 259 | os << std::flush; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 260 | |
| 261 | os << "OAT LOCATION:\n" << std::flush; |
| 262 | Object* oat_location_object = image_header.GetImageRoot(ImageHeader::kOatLocation); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 263 | std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 264 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 265 | os << oat_location; |
| 266 | if (!host_prefix.empty()) { |
| 267 | oat_location = host_prefix + oat_location; |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 268 | os << " (" << oat_location << ")"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 269 | } |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 270 | os << "\n"; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 271 | const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 272 | if (oat_file == NULL) { |
| 273 | os << "NOT FOUND\n"; |
| 274 | os << std::flush; |
| 275 | return; |
| 276 | } |
| 277 | os << "\n"; |
| 278 | os << std::flush; |
| 279 | |
Brian Carlstrom | 866c862 | 2012-01-06 16:35:13 -0800 | [diff] [blame] | 280 | class_linker->RegisterOatFile(*oat_file); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 281 | OatDump::Dump(oat_location, host_prefix, os, *oat_file); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 284 | private: |
| 285 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 286 | ImageDump(const Space& dump_space, std::ostream& os) : dump_space_(dump_space), os_(os) {} |
Elliott Hughes | d1bb4f6 | 2011-09-23 14:09:45 -0700 | [diff] [blame] | 287 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 288 | ~ImageDump() {} |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 289 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 290 | static void Callback(Object* obj, void* arg) { |
| 291 | DCHECK(obj != NULL); |
| 292 | DCHECK(arg != NULL); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 293 | ImageDump* state = reinterpret_cast<ImageDump*>(arg); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 294 | if (!state->InDumpSpace(obj)) { |
| 295 | return; |
| 296 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 297 | |
| 298 | size_t object_bytes = obj->SizeOf(); |
| 299 | size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes; |
| 300 | state->stats_.object_bytes += object_bytes; |
| 301 | state->stats_.alignment_bytes += alignment_bytes; |
| 302 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 303 | std::string summary; |
| 304 | StringAppendF(&summary, "%p: ", obj); |
| 305 | if (obj->IsClass()) { |
| 306 | Class* klass = obj->AsClass(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 307 | summary += "CLASS "; |
| 308 | summary += ClassHelper(klass).GetDescriptor(); |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 309 | std::ostringstream ss; |
Brian Carlstrom | e10b697 | 2011-09-26 13:49:03 -0700 | [diff] [blame] | 310 | ss << " (" << klass->GetStatus() << ")"; |
| 311 | summary += ss.str(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 312 | } else if (obj->IsMethod()) { |
| 313 | Method* method = obj->AsMethod(); |
| 314 | StringAppendF(&summary, "METHOD %s", PrettyMethod(method).c_str()); |
| 315 | } else if (obj->IsField()) { |
| 316 | Field* field = obj->AsField(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 317 | StringAppendF(&summary, "FIELD %s", PrettyField(field).c_str()); |
| 318 | } else if (obj->IsArrayInstance()) { |
| 319 | StringAppendF(&summary, "ARRAY %d", obj->AsArray()->GetLength()); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 320 | } else if (obj->GetClass()->IsStringClass()) { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 321 | StringAppendF(&summary, "STRING %s", obj->AsString()->ToModifiedUtf8().c_str()); |
| 322 | } else { |
| 323 | StringAppendF(&summary, "OBJECT"); |
| 324 | } |
| 325 | StringAppendF(&summary, "\n"); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 326 | std::string descriptor(ClassHelper(obj->GetClass()).GetDescriptor()); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 327 | StringAppendF(&summary, "\tclass %p: %s\n", obj->GetClass(), descriptor.c_str()); |
| 328 | state->stats_.descriptor_to_bytes[descriptor] += object_bytes; |
| 329 | state->stats_.descriptor_to_count[descriptor] += 1; |
| 330 | // StringAppendF(&summary, "\tsize %d (alignment padding %d)\n", |
| 331 | // object_bytes, RoundUp(object_bytes, kObjectAlignment) - object_bytes); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 332 | if (obj->IsMethod()) { |
| 333 | Method* method = obj->AsMethod(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 334 | if (!method->IsCalleeSaveMethod()) { |
| 335 | const int8_t* code =reinterpret_cast<const int8_t*>(method->GetCode()); |
| 336 | StringAppendF(&summary, "\tCODE %p\n", code); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 337 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 338 | const Method::InvokeStub* invoke_stub = method->GetInvokeStub(); |
| 339 | StringAppendF(&summary, "\tJNI STUB %p\n", invoke_stub); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 340 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 341 | if (method->IsNative()) { |
| 342 | if (method->IsRegistered()) { |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 343 | StringAppendF(&summary, "\tNATIVE REGISTERED %p\n", method->GetNativeMethod()); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 344 | } else { |
| 345 | StringAppendF(&summary, "\tNATIVE UNREGISTERED\n"); |
| 346 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 347 | DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 348 | DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 349 | DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 350 | } else if (method->IsAbstract()) { |
| 351 | StringAppendF(&summary, "\tABSTRACT\n"); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 352 | DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 353 | DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 354 | DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method); |
| 355 | } else if (method->IsCalleeSaveMethod()) { |
| 356 | StringAppendF(&summary, "\tCALLEE SAVE METHOD\n"); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 357 | DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 358 | DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 359 | DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 360 | } else { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 361 | DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method); |
| 362 | DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 363 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 364 | size_t register_map_bytes = method->GetGcMapLength(); |
| 365 | state->stats_.register_map_bytes += register_map_bytes; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 366 | StringAppendF(&summary, "GC=%zd ", register_map_bytes); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 367 | |
| 368 | size_t pc_mapping_table_bytes = method->GetMappingTableLength(); |
| 369 | state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 370 | StringAppendF(&summary, "Mapping=%zd ", pc_mapping_table_bytes); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 371 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 372 | const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 373 | size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 374 | state->stats_.dex_instruction_bytes += dex_instruction_bytes; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 375 | |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 376 | StringAppendF(&summary, "\tSIZE Code=%zd GC=%zd Mapping=%zd", |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 377 | dex_instruction_bytes, register_map_bytes, pc_mapping_table_bytes); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 380 | state->os_ << summary << std::flush; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 381 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 382 | |
| 383 | bool InDumpSpace(const Object* object) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame^] | 384 | return dump_space_.Contains(object); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 385 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 386 | |
| 387 | public: |
| 388 | struct Stats { |
| 389 | size_t file_bytes; |
| 390 | |
| 391 | size_t header_bytes; |
| 392 | size_t object_bytes; |
| 393 | size_t alignment_bytes; |
| 394 | |
| 395 | size_t managed_code_bytes; |
| 396 | size_t managed_to_native_code_bytes; |
| 397 | size_t native_to_managed_code_bytes; |
| 398 | |
| 399 | size_t register_map_bytes; |
| 400 | size_t pc_mapping_table_bytes; |
| 401 | |
| 402 | size_t dex_instruction_bytes; |
| 403 | |
| 404 | Stats() |
| 405 | : file_bytes(0), |
| 406 | header_bytes(0), |
| 407 | object_bytes(0), |
| 408 | alignment_bytes(0), |
| 409 | managed_code_bytes(0), |
| 410 | managed_to_native_code_bytes(0), |
| 411 | native_to_managed_code_bytes(0), |
| 412 | register_map_bytes(0), |
| 413 | pc_mapping_table_bytes(0), |
| 414 | dex_instruction_bytes(0) {} |
| 415 | |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 416 | typedef std::map<std::string, size_t> TableBytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 417 | TableBytes descriptor_to_bytes; |
| 418 | |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 419 | typedef std::map<std::string, size_t> TableCount; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 420 | TableCount descriptor_to_count; |
| 421 | |
| 422 | double PercentOfFileBytes(size_t size) { |
| 423 | return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100; |
| 424 | } |
| 425 | |
| 426 | double PercentOfObjectBytes(size_t size) { |
| 427 | return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100; |
| 428 | } |
| 429 | |
| 430 | void Dump(std::ostream& os) { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 431 | os << StringPrintf("\tfile_bytes = %zd\n", file_bytes); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 432 | os << "\n"; |
| 433 | |
| 434 | os << "\tfile_bytes = header_bytes + object_bytes + alignment_bytes\n"; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 435 | os << StringPrintf("\theader_bytes = %10zd (%2.0f%% of file_bytes)\n", |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 436 | header_bytes, PercentOfFileBytes(header_bytes)); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 437 | os << StringPrintf("\tobject_bytes = %10zd (%2.0f%% of file_bytes)\n", |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 438 | object_bytes, PercentOfFileBytes(object_bytes)); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 439 | os << StringPrintf("\talignment_bytes = %10zd (%2.0f%% of file_bytes)\n", |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 440 | alignment_bytes, PercentOfFileBytes(alignment_bytes)); |
| 441 | os << "\n"; |
| 442 | os << std::flush; |
| 443 | CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes); |
| 444 | |
| 445 | os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n"; |
| 446 | size_t object_bytes_total = 0; |
| 447 | typedef TableBytes::const_iterator It; // TODO: C++0x auto |
| 448 | for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 449 | const std::string& descriptor(it->first); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 450 | size_t bytes = it->second; |
| 451 | size_t count = descriptor_to_count[descriptor]; |
| 452 | double average = static_cast<double>(bytes) / static_cast<double>(count); |
| 453 | double percent = PercentOfObjectBytes(bytes); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 454 | os << StringPrintf("\t%32s %8zd bytes %6zd instances " |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 455 | "(%3.0f bytes/instance) %2.0f%% of object_bytes\n", |
| 456 | descriptor.c_str(), bytes, count, |
| 457 | average, percent); |
| 458 | |
| 459 | object_bytes_total += bytes; |
| 460 | } |
| 461 | os << "\n"; |
| 462 | os << std::flush; |
| 463 | CHECK_EQ(object_bytes, object_bytes_total); |
| 464 | |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 465 | os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of object_bytes)\n", |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 466 | managed_code_bytes, PercentOfObjectBytes(managed_code_bytes)); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 467 | os << StringPrintf("\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of object_bytes)\n", |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 468 | managed_to_native_code_bytes, |
| 469 | PercentOfObjectBytes(managed_to_native_code_bytes)); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 470 | os << StringPrintf("\tnative_to_managed_code_bytes = %8zd (%2.0f%% of object_bytes)\n", |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 471 | native_to_managed_code_bytes, |
| 472 | PercentOfObjectBytes(native_to_managed_code_bytes)); |
| 473 | os << "\n"; |
| 474 | os << std::flush; |
| 475 | |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 476 | os << StringPrintf("\tregister_map_bytes = %7zd (%2.0f%% of object_bytes)\n", |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 477 | register_map_bytes, PercentOfObjectBytes(register_map_bytes)); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 478 | os << StringPrintf("\tpc_mapping_table_bytes = %7zd (%2.0f%% of object_bytes)\n", |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 479 | pc_mapping_table_bytes, PercentOfObjectBytes(pc_mapping_table_bytes)); |
| 480 | os << "\n"; |
| 481 | os << std::flush; |
| 482 | |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 483 | os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 484 | os << StringPrintf("\tmanaged_code_bytes expansion = %.2f\n", |
| 485 | static_cast<double>(managed_code_bytes) |
| 486 | / static_cast<double>(dex_instruction_bytes)); |
| 487 | os << "\n"; |
| 488 | os << std::flush; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 489 | } |
| 490 | } stats_; |
| 491 | |
| 492 | private: |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 493 | const Space& dump_space_; |
| 494 | std::ostream& os_; |
Elliott Hughes | d1bb4f6 | 2011-09-23 14:09:45 -0700 | [diff] [blame] | 495 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 496 | DISALLOW_COPY_AND_ASSIGN(ImageDump); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 497 | }; |
| 498 | |
| 499 | int oatdump(int argc, char** argv) { |
| 500 | // Skip over argv[0]. |
| 501 | argv++; |
| 502 | argc--; |
| 503 | |
| 504 | if (argc == 0) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 505 | fprintf(stderr, "No arguments specified\n"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 506 | usage(); |
| 507 | } |
| 508 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 509 | const char* oat_filename = NULL; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 510 | const char* image_filename = NULL; |
| 511 | const char* boot_image_filename = NULL; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 512 | std::string host_prefix; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 513 | std::ostream* os = &std::cout; |
| 514 | UniquePtr<std::ofstream> out; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 515 | |
| 516 | for (int i = 0; i < argc; i++) { |
| 517 | const StringPiece option(argv[i]); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 518 | if (option.starts_with("--oat-file=")) { |
| 519 | oat_filename = option.substr(strlen("--oat-file=")).data(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 520 | } else if (option.starts_with("--image=")) { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 521 | image_filename = option.substr(strlen("--image=")).data(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 522 | } else if (option.starts_with("--boot-image=")) { |
| 523 | boot_image_filename = option.substr(strlen("--boot-image=")).data(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 524 | } else if (option.starts_with("--host-prefix=")) { |
| 525 | host_prefix = option.substr(strlen("--host-prefix=")).data(); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 526 | } else if (option.starts_with("--output=")) { |
| 527 | const char* filename = option.substr(strlen("--output=")).data(); |
| 528 | out.reset(new std::ofstream(filename)); |
| 529 | if (!out->good()) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 530 | fprintf(stderr, "Failed to open output filename %s\n", filename); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 531 | usage(); |
| 532 | } |
| 533 | os = out.get(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 534 | } else { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 535 | fprintf(stderr, "Unknown argument %s\n", option.data()); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 536 | usage(); |
| 537 | } |
| 538 | } |
| 539 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 540 | if (image_filename == NULL && oat_filename == NULL) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 541 | fprintf(stderr, "Either --image or --oat must be specified\n"); |
| 542 | return EXIT_FAILURE; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 545 | if (image_filename != NULL && oat_filename != NULL) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 546 | fprintf(stderr, "Either --image or --oat must be specified but not both\n"); |
| 547 | return EXIT_FAILURE; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | if (oat_filename != NULL) { |
| 551 | const OatFile* oat_file = OatFile::Open(oat_filename, "", NULL); |
| 552 | if (oat_file == NULL) { |
| 553 | fprintf(stderr, "Failed to open oat file from %s\n", oat_filename); |
| 554 | return EXIT_FAILURE; |
| 555 | } |
| 556 | OatDump::Dump(oat_filename, host_prefix, *os, *oat_file); |
| 557 | return EXIT_SUCCESS; |
| 558 | } |
| 559 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 560 | Runtime::Options options; |
| 561 | std::string image_option; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 562 | std::string oat_option; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 563 | std::string boot_image_option; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 564 | std::string boot_oat_option; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 565 | if (boot_image_filename != NULL) { |
| 566 | boot_image_option += "-Ximage:"; |
| 567 | boot_image_option += boot_image_filename; |
| 568 | options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 569 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 570 | if (image_filename != NULL) { |
| 571 | image_option += "-Ximage:"; |
| 572 | image_option += image_filename; |
| 573 | options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL))); |
| 574 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 575 | |
| 576 | if (!host_prefix.empty()) { |
| 577 | options.push_back(std::make_pair("host-prefix", host_prefix.c_str())); |
| 578 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 579 | |
| 580 | UniquePtr<Runtime> runtime(Runtime::Create(options, false)); |
| 581 | if (runtime.get() == NULL) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 582 | fprintf(stderr, "Failed to create runtime\n"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 583 | return EXIT_FAILURE; |
| 584 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 585 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame^] | 586 | ImageSpace* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]->AsImageSpace(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 587 | CHECK(image_space != NULL); |
| 588 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 589 | if (!image_header.IsValid()) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 590 | fprintf(stderr, "Invalid image header %s\n", image_filename); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 591 | return EXIT_FAILURE; |
| 592 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 593 | ImageDump::Dump(image_filename, host_prefix, *os, *image_space, image_header); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 594 | return EXIT_SUCCESS; |
| 595 | } |
| 596 | |
| 597 | } // namespace art |
| 598 | |
| 599 | int main(int argc, char** argv) { |
| 600 | return art::oatdump(argc, argv); |
| 601 | } |