Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 20 | #include <fstream> |
| 21 | #include <iostream> |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 22 | #include <map> |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include "class_linker.h" |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 27 | #include "dex_instruction.h" |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 28 | #include "file.h" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 29 | #include "image.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 30 | #include "object_utils.h" |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 31 | #include "os.h" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 32 | #include "runtime.h" |
| 33 | #include "space.h" |
| 34 | #include "stringpiece.h" |
| 35 | |
| 36 | namespace art { |
| 37 | |
| 38 | static void usage() { |
| 39 | fprintf(stderr, |
| 40 | "Usage: oatdump [options] ...\n" |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 41 | " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n" |
| 42 | " Example: adb shell oatdump --image=/system/framework/boot.art\n" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 43 | "\n"); |
| 44 | fprintf(stderr, |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 45 | " --oat-file=<file.oat>: specifies an input oat filename.\n" |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 46 | " Example: --image=/system/framework/boot.oat\n" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 47 | "\n"); |
| 48 | fprintf(stderr, |
| 49 | " --image=<file.art>: specifies an input image filename.\n" |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 50 | " Example: --image=/system/framework/boot.art\n" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 51 | "\n"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 52 | fprintf(stderr, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 53 | " --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] | 54 | " Example: --boot-image=/system/framework/boot.art\n" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 55 | "\n"); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 56 | fprintf(stderr, |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 57 | " --host-prefix may be used to translate host paths to target paths during\n" |
| 58 | " cross compilation.\n" |
| 59 | " Example: --host-prefix=out/target/product/crespo\n" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 60 | "\n"); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 61 | fprintf(stderr, |
| 62 | " --output=<file> may be used to send the output to a file.\n" |
| 63 | " Example: --output=/tmp/oatdump.txt\n" |
| 64 | "\n"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 65 | exit(EXIT_FAILURE); |
| 66 | } |
| 67 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 68 | const char* image_roots_descriptions_[] = { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 69 | "kJniStubArray", |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 70 | "kAbstractMethodErrorStubArray", |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 71 | "kInstanceResolutionStubArray", |
| 72 | "kStaticResolutionStubArray", |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 73 | "kUnknownMethodResolutionStubArray", |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 74 | "kResolutionMethod", |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 75 | "kCalleeSaveMethod", |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 76 | "kRefsOnlySaveMethod", |
| 77 | "kRefsAndArgsSaveMethod", |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 78 | "kOatLocation", |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 79 | "kDexCaches", |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 80 | "kClassRoots", |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 83 | class OatDumper { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 84 | public: |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 85 | void Dump(const std::string& oat_filename, std::ostream& os, const OatFile& oat_file) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 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 Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 100 | os << "BEGIN:\n"; |
| 101 | os << reinterpret_cast<const void*>(oat_file.Begin()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 102 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 103 | os << "END:\n"; |
| 104 | os << reinterpret_cast<const void*>(oat_file.End()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 105 | |
| 106 | os << std::flush; |
| 107 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 108 | std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file.GetOatDexFiles(); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 109 | AddAllOffsets(oat_file, oat_dex_files); |
| 110 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 111 | for (size_t i = 0; i < oat_dex_files.size(); i++) { |
| 112 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i]; |
| 113 | CHECK(oat_dex_file != NULL); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 114 | DumpOatDexFile(os, oat_file, *oat_dex_file); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | private: |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 119 | void AddAllOffsets(const OatFile& oat_file, std::vector<const OatFile::OatDexFile*>& oat_dex_files) { |
| 120 | // We don't know the length of the code for each method, but we need to know where to stop |
| 121 | // when disassembling. What we do know is that a region of code will be followed by some other |
| 122 | // region, so if we keep a sorted sequence of the start of each region, we can infer the length |
| 123 | // of a piece of code by using upper_bound to find the start of the next region. |
| 124 | for (size_t i = 0; i < oat_dex_files.size(); i++) { |
| 125 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i]; |
| 126 | CHECK(oat_dex_file != NULL); |
| 127 | UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile()); |
| 128 | if (dex_file.get() == NULL) { |
| 129 | return; |
| 130 | } |
| 131 | for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) { |
| 132 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 133 | UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index)); |
| 134 | const byte* class_data = dex_file->GetClassData(class_def); |
| 135 | if (class_data != NULL) { |
| 136 | ClassDataItemIterator it(*dex_file, class_data); |
| 137 | SkipAllFields(it); |
| 138 | uint32_t class_method_index = 0; |
| 139 | while (it.HasNextDirectMethod()) { |
| 140 | AddOffsets(oat_class->GetOatMethod(class_method_index++)); |
| 141 | it.Next(); |
| 142 | } |
| 143 | while (it.HasNextVirtualMethod()) { |
| 144 | AddOffsets(oat_class->GetOatMethod(class_method_index++)); |
| 145 | it.Next(); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // If the last thing in the file is code for a method, there won't be an offset for the "next" |
| 152 | // thing. Instead of having a special case in the upper_bound code, let's just add an entry |
| 153 | // for the end of the file. |
| 154 | offsets_.insert(static_cast<uint32_t>(oat_file.End() - oat_file.Begin())); |
| 155 | } |
| 156 | |
| 157 | void AddOffsets(const OatFile::OatMethod& oat_method) { |
| 158 | offsets_.insert(oat_method.GetCodeOffset()); |
| 159 | offsets_.insert(oat_method.GetMappingTableOffset()); |
| 160 | offsets_.insert(oat_method.GetVmapTableOffset()); |
| 161 | offsets_.insert(oat_method.GetGcMapOffset()); |
| 162 | offsets_.insert(oat_method.GetInvokeStubOffset()); |
| 163 | } |
| 164 | |
| 165 | void DumpOatDexFile(std::ostream& os, const OatFile& oat_file, |
| 166 | const OatFile::OatDexFile& oat_dex_file) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 167 | os << "OAT DEX FILE:\n"; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 168 | os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str()); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 169 | os << StringPrintf("checksum: %08x\n", oat_dex_file.GetDexFileLocationChecksum()); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 170 | UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile()); |
| 171 | if (dex_file.get() == NULL) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 172 | os << "NOT FOUND\n\n"; |
| 173 | return; |
| 174 | } |
| 175 | for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) { |
| 176 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 177 | const char* descriptor = dex_file->GetClassDescriptor(class_def); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 178 | UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index)); |
| 179 | CHECK(oat_class.get() != NULL); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 180 | 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] | 181 | << oat_class->GetStatus() << ")\n"; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 182 | DumpOatClass(os, oat_file, *oat_class.get(), *(dex_file.get()), class_def); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | os << std::flush; |
| 186 | } |
| 187 | |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 188 | static void SkipAllFields(ClassDataItemIterator& it) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 189 | while (it.HasNextStaticField()) { |
| 190 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 191 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 192 | while (it.HasNextInstanceField()) { |
| 193 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 194 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 195 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 196 | |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 197 | void DumpOatClass(std::ostream& os, const OatFile& oat_file, const OatFile::OatClass& oat_class, |
| 198 | const DexFile& dex_file, const DexFile::ClassDef& class_def) { |
| 199 | const byte* class_data = dex_file.GetClassData(class_def); |
| 200 | if (class_data == NULL) { // empty class such as a marker interface? |
| 201 | return; |
| 202 | } |
| 203 | ClassDataItemIterator it(dex_file, class_data); |
| 204 | SkipAllFields(it); |
| 205 | |
| 206 | uint32_t class_method_index = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 207 | while (it.HasNextDirectMethod()) { |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 208 | const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index); |
| 209 | DumpOatMethod(os, class_method_index, oat_file, oat_method, dex_file, |
| 210 | it.GetMemberIndex(), it.GetMethodCodeItem()); |
| 211 | class_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 212 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 213 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 214 | while (it.HasNextVirtualMethod()) { |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 215 | const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index); |
| 216 | DumpOatMethod(os, class_method_index, oat_file, oat_method, dex_file, |
| 217 | it.GetMemberIndex(), it.GetMethodCodeItem()); |
| 218 | class_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 219 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 220 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 221 | DCHECK(!it.HasNext()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 222 | os << std::flush; |
| 223 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 224 | |
| 225 | void DumpOatMethod(std::ostream& os, uint32_t class_method_index, const OatFile& oat_file, |
| 226 | const OatFile::OatMethod& oat_method, const DexFile& dex_file, |
| 227 | uint32_t dex_method_idx, const DexFile::CodeItem* code_item) { |
| 228 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 229 | const char* name = dex_file.GetMethodName(method_id); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 230 | std::string signature(dex_file.GetMethodSignature(method_id)); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 231 | os << StringPrintf("\t%d: %s %s (dex_method_idx=%d)\n", |
| 232 | class_method_index, name, signature.c_str(), dex_method_idx); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 233 | os << StringPrintf("\t\tcode: %p (offset=%08x)\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 234 | oat_method.GetCode(), oat_method.GetCodeOffset()); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 235 | os << StringPrintf("\t\tframe_size_in_bytes: %zd\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 236 | oat_method.GetFrameSizeInBytes()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 237 | os << StringPrintf("\t\tcore_spill_mask: %08x\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 238 | oat_method.GetCoreSpillMask()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 239 | os << StringPrintf("\t\tfp_spill_mask: %08x\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 240 | oat_method.GetFpSpillMask()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 241 | os << StringPrintf("\t\tmapping_table: %p (offset=%08x)\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 242 | oat_method.GetMappingTable(), oat_method.GetMappingTableOffset()); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 243 | DumpMappingTable(os, oat_file, oat_method, dex_file, code_item); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 244 | os << StringPrintf("\t\tvmap_table: %p (offset=%08x)\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 245 | oat_method.GetVmapTable(), oat_method.GetVmapTableOffset()); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 246 | os << StringPrintf("\t\tgc_map: %p (offset=%08x)\n", |
| 247 | oat_method.GetGcMap(), oat_method.GetGcMapOffset()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 248 | os << StringPrintf("\t\tinvoke_stub: %p (offset=%08x)\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 249 | oat_method.GetInvokeStub(), oat_method.GetInvokeStubOffset()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 250 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 251 | |
| 252 | void DumpMappingTable(std::ostream& os, |
| 253 | const OatFile& oat_file, const OatFile::OatMethod& oat_method, |
| 254 | const DexFile& dex_file, const DexFile::CodeItem* code_item) { |
| 255 | const uint32_t* raw_table = oat_method.GetMappingTable(); |
| 256 | const void* code = oat_method.GetCode(); |
| 257 | if (raw_table == NULL || code == NULL) { |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | uint32_t length = *raw_table; |
| 262 | ++raw_table; |
| 263 | |
| 264 | for (size_t i = 0; i < length; i += 2) { |
| 265 | uint32_t dex_pc = raw_table[i + 1]; |
| 266 | const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]); |
| 267 | os << StringPrintf("\t\t0x%04x: %s\n", dex_pc, instruction->DumpString(&dex_file).c_str()); |
| 268 | |
| 269 | // TODO: this is thumb2-specific. |
| 270 | const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i]; |
| 271 | const uint8_t* end_native_pc = NULL; |
| 272 | if (i + 2 < length) { |
| 273 | end_native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i + 2]; |
| 274 | } else { |
| 275 | const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file.Begin()); |
| 276 | uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin); |
| 277 | |
| 278 | typedef std::set<uint32_t>::iterator It; |
| 279 | It it = offsets_.lower_bound(last_offset); |
| 280 | CHECK(it != offsets_.end()); |
| 281 | end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it; |
| 282 | } |
| 283 | |
| 284 | // TODO: insert disassembler here. |
| 285 | CHECK(native_pc <= end_native_pc); |
| 286 | for (; native_pc < end_native_pc; native_pc += 2) { |
| 287 | os << StringPrintf("\t\t\t%p: 0x%04x\n", native_pc, *reinterpret_cast<const uint16_t*>(native_pc)); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | std::set<uint32_t> offsets_; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | class ImageDump { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 296 | public: |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 297 | static void Dump(const std::string& image_filename, |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 298 | const std::string& host_prefix, |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 299 | std::ostream& os, |
| 300 | Space& image_space, |
| 301 | const ImageHeader& image_header) { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 302 | os << "MAGIC:\n"; |
| 303 | os << image_header.GetMagic() << "\n\n"; |
| 304 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 305 | os << "IMAGE BEGIN:\n"; |
| 306 | os << reinterpret_cast<void*>(image_header.GetImageBegin()) << "\n\n"; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 307 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 308 | os << "OAT CHECKSUM:\n"; |
| 309 | os << StringPrintf("%08x\n\n", image_header.GetOatChecksum()); |
| 310 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 311 | os << "OAT BEGIN:\n"; |
| 312 | os << reinterpret_cast<void*>(image_header.GetOatBegin()) << "\n\n"; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 313 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 314 | os << "OAT END:\n"; |
| 315 | os << reinterpret_cast<void*>(image_header.GetOatEnd()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 316 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 317 | os << "ROOTS:\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 318 | os << reinterpret_cast<void*>(image_header.GetImageRoots()) << "\n"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 319 | CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax)); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 320 | for (int i = 0; i < ImageHeader::kImageRootsMax; i++) { |
| 321 | ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i); |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 322 | const char* image_root_description = image_roots_descriptions_[i]; |
| 323 | Object* image_root_object = image_header.GetImageRoot(image_root); |
| 324 | os << StringPrintf("%s: %p\n", image_root_description, image_root_object); |
| 325 | if (image_root_object->IsObjectArray()) { |
| 326 | // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this) |
| 327 | ObjectArray<Object>* image_root_object_array |
| 328 | = down_cast<ObjectArray<Object>*>(image_root_object); |
| 329 | // = image_root_object->AsObjectArray<Object>(); |
| 330 | for (int i = 0; i < image_root_object_array->GetLength(); i++) { |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 331 | Object* value = image_root_object_array->Get(i); |
| 332 | if (value != NULL) { |
| 333 | os << "\t" << i << ": "; |
| 334 | std::string summary; |
| 335 | PrettyObjectValue(summary, value->GetClass(), value); |
| 336 | os << summary; |
| 337 | } else { |
| 338 | os << StringPrintf("\t%d: null\n", i); |
| 339 | } |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 340 | } |
| 341 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 342 | } |
| 343 | os << "\n"; |
| 344 | |
| 345 | os << "OBJECTS:\n" << std::flush; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 346 | ImageDump state(image_space, os); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 347 | HeapBitmap* heap_bitmap = Heap::GetLiveBits(); |
| 348 | DCHECK(heap_bitmap != NULL); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 349 | heap_bitmap->Walk(ImageDump::Callback, &state); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 350 | os << "\n"; |
| 351 | |
| 352 | os << "STATS:\n" << std::flush; |
| 353 | UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), false)); |
| 354 | state.stats_.file_bytes = file->Length(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 355 | size_t header_bytes = sizeof(ImageHeader); |
| 356 | state.stats_.header_bytes = header_bytes; |
| 357 | size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes; |
| 358 | state.stats_.alignment_bytes += alignment_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 359 | state.stats_.Dump(os); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 360 | os << "\n"; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 361 | |
| 362 | os << std::flush; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 363 | |
| 364 | os << "OAT LOCATION:\n" << std::flush; |
| 365 | Object* oat_location_object = image_header.GetImageRoot(ImageHeader::kOatLocation); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 366 | std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 367 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 368 | os << oat_location; |
| 369 | if (!host_prefix.empty()) { |
| 370 | oat_location = host_prefix + oat_location; |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 371 | os << " (" << oat_location << ")"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 372 | } |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 373 | os << "\n"; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 374 | const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 375 | if (oat_file == NULL) { |
| 376 | os << "NOT FOUND\n"; |
| 377 | os << std::flush; |
| 378 | return; |
| 379 | } |
| 380 | os << "\n"; |
| 381 | os << std::flush; |
| 382 | |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 383 | OatDumper oat_dumper; |
| 384 | oat_dumper.Dump(oat_location, os, *oat_file); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 387 | private: |
| 388 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 389 | 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] | 390 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 391 | ~ImageDump() {} |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 392 | |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 393 | static void PrettyObjectValue(std::string& summary, Class* type, Object* value) { |
| 394 | CHECK(type != NULL); |
| 395 | if (value == NULL) { |
| 396 | StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str()); |
| 397 | } else if (type->IsStringClass()) { |
| 398 | String* string = value->AsString(); |
| 399 | StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str()); |
| 400 | } else if (value->IsClass()) { |
| 401 | Class* klass = value->AsClass(); |
| 402 | StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str()); |
| 403 | } else if (value->IsField()) { |
| 404 | Field* field = value->AsField(); |
| 405 | StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str()); |
| 406 | } else if (value->IsMethod()) { |
| 407 | Method* method = value->AsMethod(); |
| 408 | StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str()); |
| 409 | } else { |
| 410 | StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str()); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | static void PrintField(std::string& summary, Field* field, Object* obj) { |
| 415 | FieldHelper fh(field); |
| 416 | Class* type = fh.GetType(); |
| 417 | StringAppendF(&summary, "\t%s: ", fh.GetName()); |
| 418 | if (type->IsPrimitiveLong()) { |
| 419 | StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj)); |
| 420 | } else if (type->IsPrimitiveDouble()) { |
| 421 | StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj)); |
| 422 | } else if (type->IsPrimitiveFloat()) { |
| 423 | StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj)); |
| 424 | } else if (type->IsPrimitive()){ |
| 425 | StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj)); |
| 426 | } else { |
| 427 | Object* value = field->GetObj(obj); |
| 428 | PrettyObjectValue(summary, type, value); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | static void DumpFields(std::string& summary, Object* obj, Class* klass) { |
| 433 | Class* super = klass->GetSuperClass(); |
| 434 | if (super != NULL) { |
| 435 | DumpFields(summary, obj, super); |
| 436 | } |
| 437 | ObjectArray<Field>* fields = klass->GetIFields(); |
| 438 | if (fields != NULL) { |
| 439 | for (int32_t i = 0; i < fields->GetLength(); i++) { |
| 440 | Field* field = fields->Get(i); |
| 441 | PrintField(summary, field, obj); |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 446 | static void Callback(Object* obj, void* arg) { |
| 447 | DCHECK(obj != NULL); |
| 448 | DCHECK(arg != NULL); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 449 | ImageDump* state = reinterpret_cast<ImageDump*>(arg); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 450 | if (!state->InDumpSpace(obj)) { |
| 451 | return; |
| 452 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 453 | |
| 454 | size_t object_bytes = obj->SizeOf(); |
| 455 | size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes; |
| 456 | state->stats_.object_bytes += object_bytes; |
| 457 | state->stats_.alignment_bytes += alignment_bytes; |
| 458 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 459 | std::string summary; |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 460 | Class* obj_class = obj->GetClass(); |
| 461 | if (obj_class->IsArrayClass()) { |
| 462 | StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(), |
| 463 | obj->AsArray()->GetLength()); |
| 464 | } else if (obj->IsClass()) { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 465 | Class* klass = obj->AsClass(); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 466 | StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj, |
| 467 | PrettyDescriptor(klass).c_str()); |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 468 | std::ostringstream ss; |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 469 | ss << klass->GetStatus() << ")\n"; |
Brian Carlstrom | e10b697 | 2011-09-26 13:49:03 -0700 | [diff] [blame] | 470 | summary += ss.str(); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 471 | } else if (obj->IsField()) { |
| 472 | StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj, |
| 473 | PrettyField(obj->AsField()).c_str()); |
| 474 | } else if (obj->IsMethod()) { |
| 475 | StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj, |
| 476 | PrettyMethod(obj->AsMethod()).c_str()); |
| 477 | } else if (obj_class->IsStringClass()) { |
| 478 | StringAppendF(&summary, "%p: java.lang.String \"%s\"\n", obj, |
| 479 | obj->AsString()->ToModifiedUtf8().c_str()); |
| 480 | } else { |
| 481 | StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str()); |
| 482 | } |
| 483 | DumpFields(summary, obj, obj_class); |
| 484 | if (obj->IsObjectArray()) { |
| 485 | ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>(); |
| 486 | int32_t length = obj_array->GetLength(); |
| 487 | for (int32_t i = 0; i < length; i++) { |
| 488 | Object* value = obj_array->Get(i); |
| 489 | size_t run = 0; |
| 490 | for (int32_t j = i + 1; j < length; j++) { |
| 491 | if (value == obj_array->Get(j)) { |
| 492 | run++; |
| 493 | } else { |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | if (run == 0) { |
| 498 | StringAppendF(&summary, "\t%d: ", i); |
| 499 | } else { |
Elliott Hughes | c1051ae | 2012-02-27 12:52:31 -0800 | [diff] [blame] | 500 | StringAppendF(&summary, "\t%d to %zd: ", i, i + run); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 501 | i = i + run; |
| 502 | } |
| 503 | Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass(); |
| 504 | PrettyObjectValue(summary, value_class, value); |
| 505 | } |
| 506 | } else if (obj->IsClass()) { |
| 507 | ObjectArray<Field>* sfields = obj->AsClass()->GetSFields(); |
| 508 | if (sfields != NULL) { |
| 509 | summary += "\t\tSTATICS:\n"; |
| 510 | for (int32_t i = 0; i < sfields->GetLength(); i++) { |
| 511 | Field* field = sfields->Get(i); |
| 512 | PrintField(summary, field, NULL); |
| 513 | } |
| 514 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 515 | } else if (obj->IsMethod()) { |
| 516 | Method* method = obj->AsMethod(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 517 | if (method->IsNative()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 518 | DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 519 | DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 520 | DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 521 | } else if (method->IsAbstract() || method->IsCalleeSaveMethod() || |
| 522 | method->IsResolutionMethod()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 523 | DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 524 | DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 525 | DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 526 | } else { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 527 | DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method); |
| 528 | DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 529 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 530 | size_t register_map_bytes = method->GetGcMapLength(); |
| 531 | state->stats_.register_map_bytes += register_map_bytes; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 532 | |
| 533 | size_t pc_mapping_table_bytes = method->GetMappingTableLength(); |
| 534 | state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 535 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 536 | const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 537 | 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] | 538 | state->stats_.dex_instruction_bytes += dex_instruction_bytes; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 539 | |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 540 | StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n", |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 541 | dex_instruction_bytes, register_map_bytes, pc_mapping_table_bytes); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 542 | } |
| 543 | } |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 544 | std::string descriptor(ClassHelper(obj_class).GetDescriptor()); |
| 545 | state->stats_.descriptor_to_bytes[descriptor] += object_bytes; |
| 546 | state->stats_.descriptor_to_count[descriptor] += 1; |
| 547 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 548 | state->os_ << summary << std::flush; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 549 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 550 | |
| 551 | bool InDumpSpace(const Object* object) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 552 | return dump_space_.Contains(object); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 553 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 554 | |
| 555 | public: |
| 556 | struct Stats { |
| 557 | size_t file_bytes; |
| 558 | |
| 559 | size_t header_bytes; |
| 560 | size_t object_bytes; |
| 561 | size_t alignment_bytes; |
| 562 | |
| 563 | size_t managed_code_bytes; |
| 564 | size_t managed_to_native_code_bytes; |
| 565 | size_t native_to_managed_code_bytes; |
| 566 | |
| 567 | size_t register_map_bytes; |
| 568 | size_t pc_mapping_table_bytes; |
| 569 | |
| 570 | size_t dex_instruction_bytes; |
| 571 | |
| 572 | Stats() |
| 573 | : file_bytes(0), |
| 574 | header_bytes(0), |
| 575 | object_bytes(0), |
| 576 | alignment_bytes(0), |
| 577 | managed_code_bytes(0), |
| 578 | managed_to_native_code_bytes(0), |
| 579 | native_to_managed_code_bytes(0), |
| 580 | register_map_bytes(0), |
| 581 | pc_mapping_table_bytes(0), |
| 582 | dex_instruction_bytes(0) {} |
| 583 | |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 584 | typedef std::map<std::string, size_t> TableBytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 585 | TableBytes descriptor_to_bytes; |
| 586 | |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 587 | typedef std::map<std::string, size_t> TableCount; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 588 | TableCount descriptor_to_count; |
| 589 | |
| 590 | double PercentOfFileBytes(size_t size) { |
| 591 | return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100; |
| 592 | } |
| 593 | |
| 594 | double PercentOfObjectBytes(size_t size) { |
| 595 | return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100; |
| 596 | } |
| 597 | |
| 598 | void Dump(std::ostream& os) { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 599 | os << StringPrintf("\tfile_bytes = %zd\n", file_bytes); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 600 | os << "\n"; |
| 601 | |
| 602 | os << "\tfile_bytes = header_bytes + object_bytes + alignment_bytes\n"; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 603 | os << StringPrintf("\theader_bytes = %10zd (%2.0f%% of file_bytes)\n", |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 604 | header_bytes, PercentOfFileBytes(header_bytes)); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 605 | os << StringPrintf("\tobject_bytes = %10zd (%2.0f%% of file_bytes)\n", |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 606 | object_bytes, PercentOfFileBytes(object_bytes)); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 607 | os << StringPrintf("\talignment_bytes = %10zd (%2.0f%% of file_bytes)\n", |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 608 | alignment_bytes, PercentOfFileBytes(alignment_bytes)); |
| 609 | os << "\n"; |
| 610 | os << std::flush; |
| 611 | CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes); |
| 612 | |
| 613 | os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n"; |
| 614 | size_t object_bytes_total = 0; |
| 615 | typedef TableBytes::const_iterator It; // TODO: C++0x auto |
| 616 | 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] | 617 | const std::string& descriptor(it->first); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 618 | size_t bytes = it->second; |
| 619 | size_t count = descriptor_to_count[descriptor]; |
| 620 | double average = static_cast<double>(bytes) / static_cast<double>(count); |
| 621 | double percent = PercentOfObjectBytes(bytes); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 622 | os << StringPrintf("\t%32s %8zd bytes %6zd instances " |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 623 | "(%3.0f bytes/instance) %2.0f%% of object_bytes\n", |
| 624 | descriptor.c_str(), bytes, count, |
| 625 | average, percent); |
| 626 | |
| 627 | object_bytes_total += bytes; |
| 628 | } |
| 629 | os << "\n"; |
| 630 | os << std::flush; |
| 631 | CHECK_EQ(object_bytes, object_bytes_total); |
| 632 | |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 633 | os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of object_bytes)\n", |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 634 | managed_code_bytes, PercentOfObjectBytes(managed_code_bytes)); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 635 | 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] | 636 | managed_to_native_code_bytes, |
| 637 | PercentOfObjectBytes(managed_to_native_code_bytes)); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 638 | 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] | 639 | native_to_managed_code_bytes, |
| 640 | PercentOfObjectBytes(native_to_managed_code_bytes)); |
| 641 | os << "\n"; |
| 642 | os << std::flush; |
| 643 | |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 644 | os << StringPrintf("\tregister_map_bytes = %7zd (%2.0f%% of object_bytes)\n", |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 645 | register_map_bytes, PercentOfObjectBytes(register_map_bytes)); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 646 | 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] | 647 | pc_mapping_table_bytes, PercentOfObjectBytes(pc_mapping_table_bytes)); |
| 648 | os << "\n"; |
| 649 | os << std::flush; |
| 650 | |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 651 | os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 652 | os << StringPrintf("\tmanaged_code_bytes expansion = %.2f\n", |
| 653 | static_cast<double>(managed_code_bytes) |
| 654 | / static_cast<double>(dex_instruction_bytes)); |
| 655 | os << "\n"; |
| 656 | os << std::flush; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 657 | } |
| 658 | } stats_; |
| 659 | |
| 660 | private: |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 661 | const Space& dump_space_; |
| 662 | std::ostream& os_; |
Elliott Hughes | d1bb4f6 | 2011-09-23 14:09:45 -0700 | [diff] [blame] | 663 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 664 | DISALLOW_COPY_AND_ASSIGN(ImageDump); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 665 | }; |
| 666 | |
| 667 | int oatdump(int argc, char** argv) { |
| 668 | // Skip over argv[0]. |
| 669 | argv++; |
| 670 | argc--; |
| 671 | |
| 672 | if (argc == 0) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 673 | fprintf(stderr, "No arguments specified\n"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 674 | usage(); |
| 675 | } |
| 676 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 677 | const char* oat_filename = NULL; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 678 | const char* image_filename = NULL; |
| 679 | const char* boot_image_filename = NULL; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 680 | std::string host_prefix; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 681 | std::ostream* os = &std::cout; |
| 682 | UniquePtr<std::ofstream> out; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 683 | |
| 684 | for (int i = 0; i < argc; i++) { |
| 685 | const StringPiece option(argv[i]); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 686 | if (option.starts_with("--oat-file=")) { |
| 687 | oat_filename = option.substr(strlen("--oat-file=")).data(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 688 | } else if (option.starts_with("--image=")) { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 689 | image_filename = option.substr(strlen("--image=")).data(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 690 | } else if (option.starts_with("--boot-image=")) { |
| 691 | boot_image_filename = option.substr(strlen("--boot-image=")).data(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 692 | } else if (option.starts_with("--host-prefix=")) { |
| 693 | host_prefix = option.substr(strlen("--host-prefix=")).data(); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 694 | } else if (option.starts_with("--output=")) { |
| 695 | const char* filename = option.substr(strlen("--output=")).data(); |
| 696 | out.reset(new std::ofstream(filename)); |
| 697 | if (!out->good()) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 698 | fprintf(stderr, "Failed to open output filename %s\n", filename); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 699 | usage(); |
| 700 | } |
| 701 | os = out.get(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 702 | } else { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 703 | fprintf(stderr, "Unknown argument %s\n", option.data()); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 704 | usage(); |
| 705 | } |
| 706 | } |
| 707 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 708 | if (image_filename == NULL && oat_filename == NULL) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 709 | fprintf(stderr, "Either --image or --oat must be specified\n"); |
| 710 | return EXIT_FAILURE; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 713 | if (image_filename != NULL && oat_filename != NULL) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 714 | fprintf(stderr, "Either --image or --oat must be specified but not both\n"); |
| 715 | return EXIT_FAILURE; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | if (oat_filename != NULL) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 719 | const OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, NULL); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 720 | if (oat_file == NULL) { |
| 721 | fprintf(stderr, "Failed to open oat file from %s\n", oat_filename); |
| 722 | return EXIT_FAILURE; |
| 723 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 724 | OatDumper oat_dumper; |
| 725 | oat_dumper.Dump(oat_filename, *os, *oat_file); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 726 | return EXIT_SUCCESS; |
| 727 | } |
| 728 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 729 | Runtime::Options options; |
| 730 | std::string image_option; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 731 | std::string oat_option; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 732 | std::string boot_image_option; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 733 | std::string boot_oat_option; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 734 | if (boot_image_filename != NULL) { |
| 735 | boot_image_option += "-Ximage:"; |
| 736 | boot_image_option += boot_image_filename; |
| 737 | 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] | 738 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 739 | if (image_filename != NULL) { |
| 740 | image_option += "-Ximage:"; |
| 741 | image_option += image_filename; |
| 742 | options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL))); |
| 743 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 744 | |
| 745 | if (!host_prefix.empty()) { |
| 746 | options.push_back(std::make_pair("host-prefix", host_prefix.c_str())); |
| 747 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 748 | |
| 749 | UniquePtr<Runtime> runtime(Runtime::Create(options, false)); |
| 750 | if (runtime.get() == NULL) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 751 | fprintf(stderr, "Failed to create runtime\n"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 752 | return EXIT_FAILURE; |
| 753 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 754 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 755 | ImageSpace* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]->AsImageSpace(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 756 | CHECK(image_space != NULL); |
| 757 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 758 | if (!image_header.IsValid()) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 759 | fprintf(stderr, "Invalid image header %s\n", image_filename); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 760 | return EXIT_FAILURE; |
| 761 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 762 | ImageDump::Dump(image_filename, host_prefix, *os, *image_space, image_header); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 763 | return EXIT_SUCCESS; |
| 764 | } |
| 765 | |
| 766 | } // namespace art |
| 767 | |
| 768 | int main(int argc, char** argv) { |
| 769 | return art::oatdump(argc, argv); |
| 770 | } |