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