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" |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 27 | #include "context.h" |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 28 | #include "dex_instruction.h" |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 29 | #include "dex_verifier.h" |
| 30 | #include "disassembler.h" |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 31 | #include "file.h" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 32 | #include "image.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 33 | #include "object_utils.h" |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 34 | #include "os.h" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 35 | #include "runtime.h" |
| 36 | #include "space.h" |
| 37 | #include "stringpiece.h" |
| 38 | |
| 39 | namespace art { |
| 40 | |
| 41 | static void usage() { |
| 42 | fprintf(stderr, |
| 43 | "Usage: oatdump [options] ...\n" |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 44 | " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n" |
| 45 | " Example: adb shell oatdump --image=/system/framework/boot.art\n" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 46 | "\n"); |
| 47 | fprintf(stderr, |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 48 | " --oat-file=<file.oat>: specifies an input oat filename.\n" |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 49 | " Example: --image=/system/framework/boot.oat\n" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 50 | "\n"); |
| 51 | fprintf(stderr, |
| 52 | " --image=<file.art>: specifies an input image filename.\n" |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 53 | " Example: --image=/system/framework/boot.art\n" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 54 | "\n"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 55 | fprintf(stderr, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 56 | " --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] | 57 | " Example: --boot-image=/system/framework/boot.art\n" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 58 | "\n"); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 59 | fprintf(stderr, |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 60 | " --host-prefix may be used to translate host paths to target paths during\n" |
| 61 | " cross compilation.\n" |
| 62 | " Example: --host-prefix=out/target/product/crespo\n" |
Brian Carlstrom | fe487d0 | 2012-02-29 18:49:16 -0800 | [diff] [blame] | 63 | " Default: $ANDROID_PRODUCT_OUT\n" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 64 | "\n"); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 65 | fprintf(stderr, |
| 66 | " --output=<file> may be used to send the output to a file.\n" |
| 67 | " Example: --output=/tmp/oatdump.txt\n" |
| 68 | "\n"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 69 | exit(EXIT_FAILURE); |
| 70 | } |
| 71 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 72 | const char* image_roots_descriptions_[] = { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 73 | "kJniStubArray", |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 74 | "kAbstractMethodErrorStubArray", |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 75 | "kStaticResolutionStubArray", |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 76 | "kUnknownMethodResolutionStubArray", |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 77 | "kResolutionMethod", |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 78 | "kCalleeSaveMethod", |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 79 | "kRefsOnlySaveMethod", |
| 80 | "kRefsAndArgsSaveMethod", |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 81 | "kOatLocation", |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 82 | "kDexCaches", |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 83 | "kClassRoots", |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 86 | class OatDumper { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 87 | public: |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 88 | explicit OatDumper(const OatFile& oat_file) : oat_file_(oat_file), |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 89 | oat_dex_files_(oat_file.GetOatDexFiles()), |
| 90 | disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet())) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 91 | AddAllOffsets(); |
| 92 | } |
| 93 | |
| 94 | void Dump(std::ostream& os) { |
| 95 | const OatHeader& oat_header = oat_file_.GetOatHeader(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 96 | |
| 97 | os << "MAGIC:\n"; |
| 98 | os << oat_header.GetMagic() << "\n\n"; |
| 99 | |
| 100 | os << "CHECKSUM:\n"; |
Elliott Hughes | ed2adb6 | 2012-02-29 14:41:01 -0800 | [diff] [blame] | 101 | os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 102 | |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 103 | os << "INSTRUCTION SET:\n"; |
| 104 | os << oat_header.GetInstructionSet() << "\n\n"; |
| 105 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 106 | os << "DEX FILE COUNT:\n"; |
| 107 | os << oat_header.GetDexFileCount() << "\n\n"; |
| 108 | |
| 109 | os << "EXECUTABLE OFFSET:\n"; |
Elliott Hughes | ed2adb6 | 2012-02-29 14:41:01 -0800 | [diff] [blame] | 110 | os << StringPrintf("0x%08x\n\n", oat_header.GetExecutableOffset()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 111 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 112 | os << "BEGIN:\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 113 | os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 114 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 115 | os << "END:\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 116 | os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 117 | |
| 118 | os << std::flush; |
| 119 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 120 | for (size_t i = 0; i < oat_dex_files_.size(); i++) { |
| 121 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 122 | CHECK(oat_dex_file != NULL); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 123 | DumpOatDexFile(os, *oat_dex_file); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 127 | size_t ComputeSize(const void* oat_data) { |
| 128 | if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() || |
| 129 | reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) { |
| 130 | return 0; // Address not in oat file |
| 131 | } |
| 132 | uint32_t begin_offset = reinterpret_cast<size_t>(oat_data) - |
| 133 | reinterpret_cast<size_t>(oat_file_.Begin()); |
| 134 | typedef std::set<uint32_t>::iterator It; |
| 135 | It it = offsets_.upper_bound(begin_offset); |
| 136 | CHECK(it != offsets_.end()); |
| 137 | uint32_t end_offset = *it; |
| 138 | return end_offset - begin_offset; |
| 139 | } |
| 140 | |
| 141 | const void* GetOatCode(Method* m) { |
| 142 | MethodHelper mh(m); |
| 143 | for (size_t i = 0; i < oat_dex_files_.size(); i++) { |
| 144 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; |
| 145 | CHECK(oat_dex_file != NULL); |
| 146 | UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile()); |
| 147 | if (dex_file.get() != NULL) { |
| 148 | uint32_t class_def_index; |
| 149 | bool found = dex_file->FindClassDefIndex(mh.GetDeclaringClassDescriptor(), class_def_index); |
| 150 | if (found) { |
| 151 | const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_index); |
| 152 | CHECK(oat_class != NULL); |
| 153 | size_t method_index = m->GetMethodIndex(); |
| 154 | return oat_class->GetOatMethod(method_index).GetCode(); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | return NULL; |
| 159 | } |
| 160 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 161 | private: |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 162 | void AddAllOffsets() { |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 163 | // We don't know the length of the code for each method, but we need to know where to stop |
| 164 | // when disassembling. What we do know is that a region of code will be followed by some other |
| 165 | // region, so if we keep a sorted sequence of the start of each region, we can infer the length |
| 166 | // of a piece of code by using upper_bound to find the start of the next region. |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 167 | for (size_t i = 0; i < oat_dex_files_.size(); i++) { |
| 168 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 169 | CHECK(oat_dex_file != NULL); |
| 170 | UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile()); |
| 171 | if (dex_file.get() == NULL) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 172 | continue; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 173 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 174 | offsets_.insert(reinterpret_cast<uint32_t>(&dex_file->GetHeader())); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 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 | UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index)); |
| 178 | const byte* class_data = dex_file->GetClassData(class_def); |
| 179 | if (class_data != NULL) { |
| 180 | ClassDataItemIterator it(*dex_file, class_data); |
| 181 | SkipAllFields(it); |
| 182 | uint32_t class_method_index = 0; |
| 183 | while (it.HasNextDirectMethod()) { |
| 184 | AddOffsets(oat_class->GetOatMethod(class_method_index++)); |
| 185 | it.Next(); |
| 186 | } |
| 187 | while (it.HasNextVirtualMethod()) { |
| 188 | AddOffsets(oat_class->GetOatMethod(class_method_index++)); |
| 189 | it.Next(); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // If the last thing in the file is code for a method, there won't be an offset for the "next" |
| 196 | // thing. Instead of having a special case in the upper_bound code, let's just add an entry |
| 197 | // for the end of the file. |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 198 | offsets_.insert(static_cast<uint32_t>(oat_file_.End() - oat_file_.Begin())); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void AddOffsets(const OatFile::OatMethod& oat_method) { |
Brian Carlstrom | 95ba0dc | 2012-03-05 22:06:14 -0800 | [diff] [blame] | 202 | uint32_t code_offset = oat_method.GetCodeOffset(); |
| 203 | if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) { |
| 204 | code_offset &= ~0x1; |
| 205 | } |
| 206 | offsets_.insert(code_offset); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 207 | offsets_.insert(oat_method.GetMappingTableOffset()); |
| 208 | offsets_.insert(oat_method.GetVmapTableOffset()); |
| 209 | offsets_.insert(oat_method.GetGcMapOffset()); |
| 210 | offsets_.insert(oat_method.GetInvokeStubOffset()); |
| 211 | } |
| 212 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 213 | void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 214 | os << "OAT DEX FILE:\n"; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 215 | os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str()); |
Elliott Hughes | ed2adb6 | 2012-02-29 14:41:01 -0800 | [diff] [blame] | 216 | os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum()); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 217 | UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile()); |
| 218 | if (dex_file.get() == NULL) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 219 | os << "NOT FOUND\n\n"; |
| 220 | return; |
| 221 | } |
| 222 | for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) { |
| 223 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 224 | const char* descriptor = dex_file->GetClassDescriptor(class_def); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 225 | UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index)); |
| 226 | CHECK(oat_class.get() != NULL); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 227 | 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] | 228 | << oat_class->GetStatus() << ")\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 229 | DumpOatClass(os, *oat_class.get(), *(dex_file.get()), class_def); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | os << std::flush; |
| 233 | } |
| 234 | |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 235 | static void SkipAllFields(ClassDataItemIterator& it) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 236 | while (it.HasNextStaticField()) { |
| 237 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 238 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 239 | while (it.HasNextInstanceField()) { |
| 240 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 241 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 242 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 243 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 244 | void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file, |
| 245 | const DexFile::ClassDef& class_def) { |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 246 | const byte* class_data = dex_file.GetClassData(class_def); |
| 247 | if (class_data == NULL) { // empty class such as a marker interface? |
| 248 | return; |
| 249 | } |
| 250 | ClassDataItemIterator it(dex_file, class_data); |
| 251 | SkipAllFields(it); |
| 252 | |
| 253 | uint32_t class_method_index = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 254 | while (it.HasNextDirectMethod()) { |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 255 | const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 256 | DumpOatMethod(os, class_method_index, oat_method, dex_file, |
| 257 | it.GetMemberIndex(), it.GetMethodCodeItem()); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 258 | class_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 259 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 260 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 261 | while (it.HasNextVirtualMethod()) { |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 262 | const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 263 | DumpOatMethod(os, class_method_index, oat_method, dex_file, |
| 264 | it.GetMemberIndex(), it.GetMethodCodeItem()); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 265 | class_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 266 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 267 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 268 | DCHECK(!it.HasNext()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 269 | os << std::flush; |
| 270 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 271 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 272 | void DumpOatMethod(std::ostream& os, uint32_t class_method_index, |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 273 | const OatFile::OatMethod& oat_method, const DexFile& dex_file, |
| 274 | uint32_t dex_method_idx, const DexFile::CodeItem* code_item) { |
| 275 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 276 | const char* name = dex_file.GetMethodName(method_id); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 277 | std::string signature(dex_file.GetMethodSignature(method_id)); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 278 | os << StringPrintf("\t%d: %s %s (dex_method_idx=%d)\n", |
| 279 | class_method_index, name, signature.c_str(), dex_method_idx); |
Elliott Hughes | ed2adb6 | 2012-02-29 14:41:01 -0800 | [diff] [blame] | 280 | os << StringPrintf("\t\tcode: %p (offset=0x%08x)\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 281 | oat_method.GetCode(), oat_method.GetCodeOffset()); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 282 | os << StringPrintf("\t\tframe_size_in_bytes: %zd\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 283 | oat_method.GetFrameSizeInBytes()); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 284 | os << StringPrintf("\t\tcore_spill_mask: 0x%08x", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 285 | oat_method.GetCoreSpillMask()); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 286 | DumpSpillMask(os, oat_method.GetCoreSpillMask(), false); |
| 287 | os << StringPrintf("\n\t\tfp_spill_mask: 0x%08x", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 288 | oat_method.GetFpSpillMask()); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 289 | DumpSpillMask(os, oat_method.GetFpSpillMask(), true); |
| 290 | os << StringPrintf("\n\t\tmapping_table: %p (offset=0x%08x)\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 291 | oat_method.GetMappingTable(), oat_method.GetMappingTableOffset()); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 292 | DumpMappingTable(os, oat_method); |
Elliott Hughes | ed2adb6 | 2012-02-29 14:41:01 -0800 | [diff] [blame] | 293 | os << StringPrintf("\t\tvmap_table: %p (offset=0x%08x)\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 294 | oat_method.GetVmapTable(), oat_method.GetVmapTableOffset()); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 295 | DumpVmap(os, oat_method.GetVmapTable(), oat_method.GetCoreSpillMask(), |
| 296 | oat_method.GetFpSpillMask()); |
Elliott Hughes | ed2adb6 | 2012-02-29 14:41:01 -0800 | [diff] [blame] | 297 | os << StringPrintf("\t\tgc_map: %p (offset=0x%08x)\n", |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 298 | oat_method.GetGcMap(), oat_method.GetGcMapOffset()); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 299 | DumpGcMap(os, oat_method.GetGcMap()); |
Elliott Hughes | ed2adb6 | 2012-02-29 14:41:01 -0800 | [diff] [blame] | 300 | os << StringPrintf("\t\tinvoke_stub: %p (offset=0x%08x)\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 301 | oat_method.GetInvokeStub(), oat_method.GetInvokeStubOffset()); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 302 | os << "\t\tCODE:\n"; |
| 303 | DumpCode(os, oat_method.GetCode(), oat_method.GetMappingTable(), dex_file, code_item); |
| 304 | os << "\t\tINVOKE STUB:\n"; |
| 305 | DumpCode(os, reinterpret_cast<const void*>(oat_method.GetInvokeStub()), NULL, dex_file, NULL); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 306 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 307 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 308 | void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) { |
| 309 | if (spill_mask == 0) { |
| 310 | return; |
| 311 | } |
| 312 | os << " ("; |
| 313 | for (size_t i = 0; i < 32; i++) { |
| 314 | if ((spill_mask & (1 << i)) != 0) { |
| 315 | if (is_float) { |
| 316 | os << "fr" << i; |
| 317 | } else { |
| 318 | os << "r" << i; |
| 319 | } |
| 320 | spill_mask ^= 1 << i; // clear bit |
| 321 | if (spill_mask != 0) { |
| 322 | os << ", "; |
| 323 | } else { |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | os << ")"; |
| 329 | } |
| 330 | |
| 331 | void DumpVmap(std::ostream& os, const uint16_t* raw_table, uint32_t core_spill_mask, |
| 332 | uint32_t fp_spill_mask) { |
| 333 | if (raw_table == NULL) { |
| 334 | return; |
| 335 | } |
| 336 | const VmapTable vmap_table(raw_table); |
| 337 | bool first = true; |
| 338 | os << "\t\t\t"; |
| 339 | for (size_t i = 0; i < vmap_table.size(); i++) { |
| 340 | uint16_t dex_reg = vmap_table[i]; |
| 341 | size_t matches = 0; |
| 342 | size_t spill_shifts = 0; |
| 343 | uint32_t spill_mask = core_spill_mask; |
| 344 | bool processing_fp = false; |
| 345 | while (matches != (i + 1)) { |
| 346 | if (spill_mask == 0) { |
| 347 | CHECK(!processing_fp); |
| 348 | spill_mask = fp_spill_mask; |
| 349 | processing_fp = true; |
| 350 | } |
| 351 | matches += spill_mask & 1; // Add 1 if the low bit is set |
| 352 | spill_mask >>= 1; |
| 353 | spill_shifts++; |
| 354 | } |
| 355 | size_t arm_reg = spill_shifts - 1; // wind back one as we want the last match |
| 356 | os << (first ? "v" : ", v") << dex_reg; |
| 357 | if (arm_reg < 16) { |
| 358 | os << "/r" << arm_reg; |
| 359 | } else { |
| 360 | os << "/fr" << (arm_reg - 16); |
| 361 | } |
| 362 | if (first) { |
| 363 | first = false; |
| 364 | } |
| 365 | } |
| 366 | os << std::endl; |
| 367 | } |
| 368 | |
| 369 | void DumpGcMap(std::ostream& os, const uint8_t* gc_map_raw) { |
| 370 | if (gc_map_raw == NULL) { |
| 371 | return; |
| 372 | } |
| 373 | uint32_t gc_map_length = (gc_map_raw[0] << 24) | (gc_map_raw[1] << 16) | |
| 374 | (gc_map_raw[2] << 8) | (gc_map_raw[3] << 0); |
| 375 | verifier::PcToReferenceMap map(gc_map_raw + sizeof(uint32_t), gc_map_length); |
| 376 | for (size_t entry = 0; entry < map.NumEntries(); entry++) { |
| 377 | os << StringPrintf("\t\t\t0x%04x", map.GetPC(entry)); |
| 378 | size_t num_regs = map.RegWidth() * 8; |
| 379 | const uint8_t* reg_bitmap = map.GetBitMap(entry); |
| 380 | bool first = true; |
| 381 | for (size_t reg = 0; reg < num_regs; reg++) { |
| 382 | if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) { |
| 383 | if (first) { |
| 384 | os << " v" << reg; |
| 385 | first = false; |
| 386 | } else { |
| 387 | os << ", v" << reg; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | os << std::endl; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) { |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 396 | const uint32_t* raw_table = oat_method.GetMappingTable(); |
| 397 | const void* code = oat_method.GetCode(); |
| 398 | if (raw_table == NULL || code == NULL) { |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | uint32_t length = *raw_table; |
| 403 | ++raw_table; |
| 404 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 405 | os << "\t\t{"; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 406 | for (size_t i = 0; i < length; i += 2) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 407 | const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i]; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 408 | uint32_t dex_pc = raw_table[i + 1]; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 409 | os << StringPrintf("%p -> 0x%04x", native_pc, dex_pc); |
| 410 | if (i + 2 < length) { |
| 411 | os << ", "; |
| 412 | } |
| 413 | } |
| 414 | os << "}" << std::endl << std::flush; |
| 415 | } |
| 416 | |
| 417 | void DumpCode(std::ostream& os, const void* code, const uint32_t* raw_mapping_table, |
| 418 | const DexFile& dex_file, const DexFile::CodeItem* code_item) { |
| 419 | if (code == NULL) { |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | if (raw_mapping_table == NULL) { |
| 424 | // code but no mapping table is most likely caused by code created by the JNI compiler |
| 425 | const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code); |
| 426 | const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file_.Begin()); |
| 427 | uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin); |
| 428 | |
| 429 | typedef std::set<uint32_t>::iterator It; |
| 430 | It it = offsets_.upper_bound(last_offset); |
| 431 | CHECK(it != offsets_.end()); |
| 432 | const uint8_t* end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it; |
| 433 | CHECK(native_pc < end_native_pc); |
| 434 | |
| 435 | disassembler_->Dump(os, native_pc, end_native_pc); |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | uint32_t length = *raw_mapping_table; |
| 440 | ++raw_mapping_table; |
| 441 | |
| 442 | for (size_t i = 0; i < length; i += 2) { |
| 443 | uint32_t dex_pc = raw_mapping_table[i + 1]; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 444 | const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]); |
| 445 | os << StringPrintf("\t\t0x%04x: %s\n", dex_pc, instruction->DumpString(&dex_file).c_str()); |
| 446 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 447 | const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i]; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 448 | const uint8_t* end_native_pc = NULL; |
| 449 | if (i + 2 < length) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 450 | end_native_pc = reinterpret_cast<const uint8_t*>(code) + raw_mapping_table[i + 2]; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 451 | } else { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 452 | const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file_.Begin()); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 453 | uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin); |
| 454 | |
| 455 | typedef std::set<uint32_t>::iterator It; |
Elliott Hughes | ed2adb6 | 2012-02-29 14:41:01 -0800 | [diff] [blame] | 456 | It it = offsets_.upper_bound(last_offset); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 457 | CHECK(it != offsets_.end()); |
| 458 | end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it; |
| 459 | } |
Elliott Hughes | ed2adb6 | 2012-02-29 14:41:01 -0800 | [diff] [blame] | 460 | CHECK(native_pc < end_native_pc); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 461 | disassembler_->Dump(os, native_pc, end_native_pc); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 465 | const OatFile& oat_file_; |
| 466 | std::vector<const OatFile::OatDexFile*> oat_dex_files_; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 467 | std::set<uint32_t> offsets_; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 468 | UniquePtr<Disassembler> disassembler_; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 469 | }; |
| 470 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 471 | class ImageDumper { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 472 | public: |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 473 | explicit ImageDumper(std::ostream& os, const std::string& image_filename, |
| 474 | const std::string& host_prefix, Space& image_space, |
| 475 | const ImageHeader& image_header) : os_(os), |
| 476 | image_filename_(image_filename), host_prefix_(host_prefix), |
| 477 | image_space_(image_space), image_header_(image_header) { |
| 478 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 479 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 480 | void Dump() { |
| 481 | os_ << "MAGIC:\n"; |
| 482 | os_ << image_header_.GetMagic() << "\n\n"; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 483 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 484 | os_ << "IMAGE BEGIN:\n"; |
| 485 | os_ << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 486 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 487 | os_ << "OAT CHECKSUM:\n"; |
| 488 | os_ << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum()); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 489 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 490 | os_ << "OAT BEGIN:\n"; |
| 491 | os_ << reinterpret_cast<void*>(image_header_.GetOatBegin()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 492 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 493 | os_ << "OAT END:\n"; |
| 494 | os_ << reinterpret_cast<void*>(image_header_.GetOatEnd()) << "\n\n"; |
| 495 | |
| 496 | os_ << "ROOTS:\n"; |
| 497 | os_ << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 498 | CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax)); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 499 | for (int i = 0; i < ImageHeader::kImageRootsMax; i++) { |
| 500 | ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i); |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 501 | const char* image_root_description = image_roots_descriptions_[i]; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 502 | Object* image_root_object = image_header_.GetImageRoot(image_root); |
| 503 | os_ << StringPrintf("%s: %p\n", image_root_description, image_root_object); |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 504 | if (image_root_object->IsObjectArray()) { |
| 505 | // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this) |
| 506 | ObjectArray<Object>* image_root_object_array |
| 507 | = down_cast<ObjectArray<Object>*>(image_root_object); |
| 508 | // = image_root_object->AsObjectArray<Object>(); |
| 509 | for (int i = 0; i < image_root_object_array->GetLength(); i++) { |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 510 | Object* value = image_root_object_array->Get(i); |
| 511 | if (value != NULL) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 512 | os_ << "\t" << i << ": "; |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 513 | std::string summary; |
| 514 | PrettyObjectValue(summary, value->GetClass(), value); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 515 | os_ << summary; |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 516 | } else { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 517 | os_ << StringPrintf("\t%d: null\n", i); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 518 | } |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 521 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 522 | os_ << "\n"; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 523 | |
Brian Carlstrom | 4dcbffb | 2012-03-07 00:21:18 -0800 | [diff] [blame] | 524 | os_ << "OAT LOCATION:\n" << std::flush; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 525 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 526 | Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation); |
| 527 | std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8()); |
Brian Carlstrom | 4dcbffb | 2012-03-07 00:21:18 -0800 | [diff] [blame] | 528 | os_ << oat_location; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 529 | if (!host_prefix_.empty()) { |
| 530 | oat_location = host_prefix_ + oat_location; |
| 531 | os_ << " (" << oat_location << ")"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 532 | } |
Brian Carlstrom | 4dcbffb | 2012-03-07 00:21:18 -0800 | [diff] [blame] | 533 | os_ << "\n"; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 534 | const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 535 | if (oat_file == NULL) { |
Brian Carlstrom | 4dcbffb | 2012-03-07 00:21:18 -0800 | [diff] [blame] | 536 | os_ << "NOT FOUND\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 537 | return; |
| 538 | } |
Brian Carlstrom | 4dcbffb | 2012-03-07 00:21:18 -0800 | [diff] [blame] | 539 | os_ << "\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 540 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 541 | stats_.oat_file_bytes = oat_file->Size(); |
| 542 | |
| 543 | oat_dumper_.reset(new OatDumper(*oat_file)); |
| 544 | |
| 545 | os_ << "OBJECTS:\n" << std::flush; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 546 | HeapBitmap* heap_bitmap = Runtime::Current()->GetHeap()->GetLiveBits(); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 547 | DCHECK(heap_bitmap != NULL); |
| 548 | heap_bitmap->Walk(ImageDumper::Callback, this); |
| 549 | os_ << "\n"; |
| 550 | |
| 551 | os_ << "STATS:\n" << std::flush; |
| 552 | UniquePtr<File> file(OS::OpenFile(image_filename_.c_str(), false)); |
| 553 | stats_.file_bytes = file->Length(); |
| 554 | size_t header_bytes = sizeof(ImageHeader); |
| 555 | stats_.header_bytes = header_bytes; |
| 556 | size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes; |
| 557 | stats_.alignment_bytes += alignment_bytes; |
| 558 | stats_.Dump(os_); |
| 559 | os_ << "\n"; |
| 560 | |
| 561 | os_ << std::flush; |
| 562 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 563 | oat_dumper_->Dump(os_); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 566 | private: |
| 567 | |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 568 | static void PrettyObjectValue(std::string& summary, Class* type, Object* value) { |
| 569 | CHECK(type != NULL); |
| 570 | if (value == NULL) { |
| 571 | StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str()); |
| 572 | } else if (type->IsStringClass()) { |
| 573 | String* string = value->AsString(); |
| 574 | StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str()); |
| 575 | } else if (value->IsClass()) { |
| 576 | Class* klass = value->AsClass(); |
| 577 | StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str()); |
| 578 | } else if (value->IsField()) { |
| 579 | Field* field = value->AsField(); |
| 580 | StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str()); |
| 581 | } else if (value->IsMethod()) { |
| 582 | Method* method = value->AsMethod(); |
| 583 | StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str()); |
| 584 | } else { |
| 585 | StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str()); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | static void PrintField(std::string& summary, Field* field, Object* obj) { |
| 590 | FieldHelper fh(field); |
| 591 | Class* type = fh.GetType(); |
| 592 | StringAppendF(&summary, "\t%s: ", fh.GetName()); |
| 593 | if (type->IsPrimitiveLong()) { |
| 594 | StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj)); |
| 595 | } else if (type->IsPrimitiveDouble()) { |
| 596 | StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj)); |
| 597 | } else if (type->IsPrimitiveFloat()) { |
| 598 | StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj)); |
| 599 | } else if (type->IsPrimitive()){ |
| 600 | StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj)); |
| 601 | } else { |
| 602 | Object* value = field->GetObj(obj); |
| 603 | PrettyObjectValue(summary, type, value); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | static void DumpFields(std::string& summary, Object* obj, Class* klass) { |
| 608 | Class* super = klass->GetSuperClass(); |
| 609 | if (super != NULL) { |
| 610 | DumpFields(summary, obj, super); |
| 611 | } |
| 612 | ObjectArray<Field>* fields = klass->GetIFields(); |
| 613 | if (fields != NULL) { |
| 614 | for (int32_t i = 0; i < fields->GetLength(); i++) { |
| 615 | Field* field = fields->Get(i); |
| 616 | PrintField(summary, field, obj); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 621 | bool InDumpSpace(const Object* object) { |
| 622 | return image_space_.Contains(object); |
| 623 | } |
| 624 | |
| 625 | const void* GetOatCode(Method* m) { |
| 626 | Runtime* runtime = Runtime::Current(); |
| 627 | const void* code = m->GetCode(); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 628 | if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 629 | code = oat_dumper_->GetOatCode(m); |
| 630 | } |
| 631 | return code; |
| 632 | } |
| 633 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 634 | static void Callback(Object* obj, void* arg) { |
| 635 | DCHECK(obj != NULL); |
| 636 | DCHECK(arg != NULL); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 637 | ImageDumper* state = reinterpret_cast<ImageDumper*>(arg); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 638 | if (!state->InDumpSpace(obj)) { |
| 639 | return; |
| 640 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 641 | |
| 642 | size_t object_bytes = obj->SizeOf(); |
| 643 | size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes; |
| 644 | state->stats_.object_bytes += object_bytes; |
| 645 | state->stats_.alignment_bytes += alignment_bytes; |
| 646 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 647 | std::string summary; |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 648 | Class* obj_class = obj->GetClass(); |
| 649 | if (obj_class->IsArrayClass()) { |
| 650 | StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(), |
| 651 | obj->AsArray()->GetLength()); |
| 652 | } else if (obj->IsClass()) { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 653 | Class* klass = obj->AsClass(); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 654 | StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj, |
| 655 | PrettyDescriptor(klass).c_str()); |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 656 | std::ostringstream ss; |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 657 | ss << klass->GetStatus() << ")\n"; |
Brian Carlstrom | e10b697 | 2011-09-26 13:49:03 -0700 | [diff] [blame] | 658 | summary += ss.str(); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 659 | } else if (obj->IsField()) { |
| 660 | StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj, |
| 661 | PrettyField(obj->AsField()).c_str()); |
| 662 | } else if (obj->IsMethod()) { |
| 663 | StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj, |
| 664 | PrettyMethod(obj->AsMethod()).c_str()); |
| 665 | } else if (obj_class->IsStringClass()) { |
| 666 | StringAppendF(&summary, "%p: java.lang.String \"%s\"\n", obj, |
| 667 | obj->AsString()->ToModifiedUtf8().c_str()); |
| 668 | } else { |
| 669 | StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str()); |
| 670 | } |
| 671 | DumpFields(summary, obj, obj_class); |
| 672 | if (obj->IsObjectArray()) { |
| 673 | ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>(); |
| 674 | int32_t length = obj_array->GetLength(); |
| 675 | for (int32_t i = 0; i < length; i++) { |
| 676 | Object* value = obj_array->Get(i); |
| 677 | size_t run = 0; |
| 678 | for (int32_t j = i + 1; j < length; j++) { |
| 679 | if (value == obj_array->Get(j)) { |
| 680 | run++; |
| 681 | } else { |
| 682 | break; |
| 683 | } |
| 684 | } |
| 685 | if (run == 0) { |
| 686 | StringAppendF(&summary, "\t%d: ", i); |
| 687 | } else { |
Elliott Hughes | c1051ae | 2012-02-27 12:52:31 -0800 | [diff] [blame] | 688 | StringAppendF(&summary, "\t%d to %zd: ", i, i + run); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 689 | i = i + run; |
| 690 | } |
| 691 | Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass(); |
| 692 | PrettyObjectValue(summary, value_class, value); |
| 693 | } |
| 694 | } else if (obj->IsClass()) { |
| 695 | ObjectArray<Field>* sfields = obj->AsClass()->GetSFields(); |
| 696 | if (sfields != NULL) { |
| 697 | summary += "\t\tSTATICS:\n"; |
| 698 | for (int32_t i = 0; i < sfields->GetLength(); i++) { |
| 699 | Field* field = sfields->Get(i); |
| 700 | PrintField(summary, field, NULL); |
| 701 | } |
| 702 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 703 | } else if (obj->IsMethod()) { |
| 704 | Method* method = obj->AsMethod(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 705 | if (method->IsNative()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 706 | DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 707 | DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 708 | DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 709 | bool first_occurrence; |
| 710 | size_t invoke_stub_size = state->ComputeOatSize( |
| 711 | reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence); |
| 712 | if (first_occurrence) { |
| 713 | state->stats_.managed_to_native_code_bytes += invoke_stub_size; |
| 714 | } |
| 715 | const void* oat_code = state->GetOatCode(method); |
| 716 | size_t code_size = state->ComputeOatSize(oat_code, &first_occurrence); |
| 717 | if (first_occurrence) { |
| 718 | state->stats_.native_to_managed_code_bytes += code_size; |
| 719 | } |
| 720 | if (oat_code != method->GetCode()) { |
| 721 | StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code); |
| 722 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 723 | } else if (method->IsAbstract() || method->IsCalleeSaveMethod() || |
| 724 | method->IsResolutionMethod()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 725 | DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 726 | DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 727 | DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 728 | } else { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 729 | DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method); |
| 730 | DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 731 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 732 | const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 733 | 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] | 734 | state->stats_.dex_instruction_bytes += dex_instruction_bytes; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 735 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 736 | bool first_occurance; |
| 737 | size_t gc_map_bytes = state->ComputeOatSize(method->GetGcMapRaw(), &first_occurance); |
| 738 | if (first_occurance) { |
| 739 | state->stats_.gc_map_bytes += gc_map_bytes; |
| 740 | } |
| 741 | |
| 742 | size_t pc_mapping_table_bytes = |
| 743 | state->ComputeOatSize(method->GetMappingTableRaw(), &first_occurance); |
| 744 | if (first_occurance) { |
| 745 | state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes; |
| 746 | } |
| 747 | |
| 748 | size_t vmap_table_bytes = |
| 749 | state->ComputeOatSize(method->GetVmapTableRaw(), &first_occurance); |
| 750 | if (first_occurance) { |
| 751 | state->stats_.vmap_table_bytes += vmap_table_bytes; |
| 752 | } |
| 753 | |
| 754 | size_t invoke_stub_size = state->ComputeOatSize( |
| 755 | reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurance); |
| 756 | if (first_occurance) { |
| 757 | state->stats_.native_to_managed_code_bytes += invoke_stub_size; |
| 758 | } |
| 759 | const void* oat_code = state->GetOatCode(method); |
| 760 | size_t code_size = state->ComputeOatSize(oat_code, &first_occurance); |
| 761 | if (first_occurance) { |
| 762 | state->stats_.managed_code_bytes += code_size; |
| 763 | } |
| 764 | state->stats_.managed_code_bytes_ignoring_deduplication += code_size; |
| 765 | |
| 766 | if (oat_code != method->GetCode()) { |
| 767 | StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code); |
| 768 | } |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 769 | StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n", |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 770 | dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes); |
| 771 | |
| 772 | size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes + |
| 773 | vmap_table_bytes + invoke_stub_size + code_size + object_bytes; |
| 774 | |
| 775 | double expansion = |
| 776 | static_cast<double>(code_size) / static_cast<double>(dex_instruction_bytes); |
| 777 | state->stats_.ComputeOutliers(total_size, expansion, method); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 778 | } |
| 779 | } |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 780 | std::string descriptor(ClassHelper(obj_class).GetDescriptor()); |
| 781 | state->stats_.descriptor_to_bytes[descriptor] += object_bytes; |
| 782 | state->stats_.descriptor_to_count[descriptor] += 1; |
| 783 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 784 | state->os_ << summary << std::flush; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 785 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 786 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 787 | std::set<const void*> already_seen_; |
| 788 | // Compute the size of the given data within the oat file and whether this is the first time |
| 789 | // this data has been requested |
| 790 | size_t ComputeOatSize(const void* oat_data, bool* first_occurance) { |
| 791 | if (already_seen_.count(oat_data) == 0) { |
| 792 | *first_occurance = true; |
| 793 | already_seen_.insert(oat_data); |
| 794 | } else { |
| 795 | *first_occurance = false; |
| 796 | } |
| 797 | return oat_dumper_->ComputeSize(oat_data); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 798 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 799 | |
| 800 | public: |
| 801 | struct Stats { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 802 | size_t oat_file_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 803 | size_t file_bytes; |
| 804 | |
| 805 | size_t header_bytes; |
| 806 | size_t object_bytes; |
| 807 | size_t alignment_bytes; |
| 808 | |
| 809 | size_t managed_code_bytes; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 810 | size_t managed_code_bytes_ignoring_deduplication; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 811 | size_t managed_to_native_code_bytes; |
| 812 | size_t native_to_managed_code_bytes; |
| 813 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 814 | size_t gc_map_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 815 | size_t pc_mapping_table_bytes; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 816 | size_t vmap_table_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 817 | |
| 818 | size_t dex_instruction_bytes; |
| 819 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 820 | std::vector<Method*> method_outlier; |
| 821 | std::vector<size_t> method_outlier_size; |
| 822 | std::vector<double> method_outlier_expansion; |
| 823 | |
| 824 | explicit Stats() |
| 825 | : oat_file_bytes(0), |
| 826 | file_bytes(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 827 | header_bytes(0), |
| 828 | object_bytes(0), |
| 829 | alignment_bytes(0), |
| 830 | managed_code_bytes(0), |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 831 | managed_code_bytes_ignoring_deduplication(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 832 | managed_to_native_code_bytes(0), |
| 833 | native_to_managed_code_bytes(0), |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 834 | gc_map_bytes(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 835 | pc_mapping_table_bytes(0), |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 836 | vmap_table_bytes(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 837 | dex_instruction_bytes(0) {} |
| 838 | |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 839 | typedef std::map<std::string, size_t> TableBytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 840 | TableBytes descriptor_to_bytes; |
| 841 | |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 842 | typedef std::map<std::string, size_t> TableCount; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 843 | TableCount descriptor_to_count; |
| 844 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 845 | double PercentOfOatBytes(size_t size) { |
| 846 | return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100; |
| 847 | } |
| 848 | |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 849 | double PercentOfFileBytes(size_t size) { |
| 850 | return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100; |
| 851 | } |
| 852 | |
| 853 | double PercentOfObjectBytes(size_t size) { |
| 854 | return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100; |
| 855 | } |
| 856 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 857 | void ComputeOutliers(size_t total_size, double expansion, Method* method) { |
| 858 | method_outlier_size.push_back(total_size); |
| 859 | method_outlier_expansion.push_back(expansion); |
| 860 | method_outlier.push_back(method); |
| 861 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 862 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 863 | void DumpOutliers(std::ostream& os) { |
| 864 | size_t sum_of_sizes = 0; |
| 865 | size_t sum_of_sizes_squared = 0; |
| 866 | size_t sum_of_expansion = 0; |
| 867 | size_t sum_of_expansion_squared = 0; |
| 868 | size_t n = method_outlier_size.size(); |
| 869 | for (size_t i = 0; i < n; i++) { |
| 870 | size_t cur_size = method_outlier_size[i]; |
| 871 | sum_of_sizes += cur_size; |
| 872 | sum_of_sizes_squared += cur_size * cur_size; |
| 873 | double cur_expansion = method_outlier_expansion[i]; |
| 874 | sum_of_expansion += cur_expansion; |
| 875 | sum_of_expansion_squared += cur_expansion * cur_expansion; |
| 876 | } |
| 877 | size_t size_mean = sum_of_sizes / n; |
| 878 | size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1); |
| 879 | double expansion_mean = sum_of_expansion / n; |
| 880 | double expansion_variance = |
| 881 | (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1); |
| 882 | |
| 883 | // Dump methods whose size is a certain number of standard deviations from the mean |
| 884 | size_t dumped_values = 0; |
| 885 | size_t skipped_values = 0; |
| 886 | for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations |
| 887 | size_t cur_size_variance = i * i * size_variance; |
| 888 | bool first = true; |
| 889 | for (size_t j = 0; j < n; j++) { |
| 890 | size_t cur_size = method_outlier_size[j]; |
| 891 | if (cur_size > size_mean) { |
| 892 | size_t cur_var = cur_size - size_mean; |
| 893 | cur_var = cur_var * cur_var; |
| 894 | if (cur_var > cur_size_variance) { |
| 895 | if (dumped_values > 20) { |
| 896 | if (i == 1) { |
| 897 | skipped_values++; |
| 898 | } else { |
| 899 | i = 2; // jump to counting for 1 standard deviation |
| 900 | break; |
| 901 | } |
| 902 | } else { |
| 903 | if (first) { |
| 904 | os << "\nBig methods (size > " << i << " standard deviations the norm):" |
| 905 | << std::endl; |
| 906 | first = false; |
| 907 | } |
| 908 | os << "\t" << PrettyMethod(method_outlier[j]) << " requires storage of " |
| 909 | << PrettySize(cur_size) << std::endl; |
| 910 | method_outlier_size[j] = 0; // don't consider this method again |
| 911 | dumped_values++; |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | if (skipped_values > 0) { |
| 918 | os << "\t... skipped " << skipped_values |
| 919 | << " methods with size > 1 standard deviation from the norm" << std::endl; |
| 920 | } |
| 921 | os << std::endl << std::flush; |
| 922 | |
| 923 | // Dump methods whose expansion is a certain number of standard deviations from the mean |
| 924 | dumped_values = 0; |
| 925 | skipped_values = 0; |
| 926 | for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations |
| 927 | double cur_expansion_variance = i * i * expansion_variance; |
| 928 | bool first = true; |
| 929 | for (size_t j = 0; j < n; j++) { |
| 930 | double cur_expansion = method_outlier_expansion[j]; |
| 931 | if (cur_expansion > expansion_mean) { |
| 932 | size_t cur_var = cur_expansion - expansion_mean; |
| 933 | cur_var = cur_var * cur_var; |
| 934 | if (cur_var > cur_expansion_variance) { |
| 935 | if (dumped_values > 20) { |
| 936 | if (i == 1) { |
| 937 | skipped_values++; |
| 938 | } else { |
| 939 | i = 2; // jump to counting for 1 standard deviation |
| 940 | break; |
| 941 | } |
| 942 | } else { |
| 943 | if (first) { |
| 944 | os << "\nLarge expansion methods (size > " << i |
| 945 | << " standard deviations the norm):" << std::endl; |
| 946 | first = false; |
| 947 | } |
| 948 | os << "\t" << PrettyMethod(method_outlier[j]) << " expanded code by " |
| 949 | << cur_expansion << std::endl; |
| 950 | method_outlier_expansion[j] = 0.0; // don't consider this method again |
| 951 | dumped_values++; |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | if (skipped_values > 0) { |
| 958 | os << "\t... skipped " << skipped_values |
| 959 | << " methods with expansion > 1 standard deviation from the norm" << std::endl; |
| 960 | } |
| 961 | os << std::endl << std::flush; |
| 962 | } |
| 963 | |
| 964 | void Dump(std::ostream& os) { |
| 965 | os << "\tart_file_bytes = " << PrettySize(file_bytes) << std::endl << std::endl |
| 966 | << "\tart_file_bytes = header_bytes + object_bytes + alignment_bytes" << std::endl |
| 967 | << StringPrintf("\theader_bytes = %8zd (%2.0f%% of art file bytes)\n" |
| 968 | "\tobject_bytes = %8zd (%2.0f%% of art file bytes)\n" |
| 969 | "\talignment_bytes = %8zd (%2.0f%% of art file bytes)\n", |
| 970 | header_bytes, PercentOfFileBytes(header_bytes), |
| 971 | object_bytes, PercentOfFileBytes(object_bytes), |
| 972 | alignment_bytes, PercentOfFileBytes(alignment_bytes)) |
| 973 | << std::endl << std::flush; |
| 974 | |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 975 | CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes); |
| 976 | |
| 977 | os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n"; |
| 978 | size_t object_bytes_total = 0; |
| 979 | typedef TableBytes::const_iterator It; // TODO: C++0x auto |
| 980 | 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] | 981 | const std::string& descriptor(it->first); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 982 | size_t bytes = it->second; |
| 983 | size_t count = descriptor_to_count[descriptor]; |
| 984 | double average = static_cast<double>(bytes) / static_cast<double>(count); |
| 985 | double percent = PercentOfObjectBytes(bytes); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 986 | os << StringPrintf("\t%32s %8zd bytes %6zd instances " |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 987 | "(%3.0f bytes/instance) %2.0f%% of object_bytes\n", |
| 988 | descriptor.c_str(), bytes, count, |
| 989 | average, percent); |
| 990 | |
| 991 | object_bytes_total += bytes; |
| 992 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 993 | os << std::endl << std::flush; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 994 | CHECK_EQ(object_bytes, object_bytes_total); |
| 995 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 996 | os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" |
| 997 | "\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" |
| 998 | "\tnative_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n", |
| 999 | managed_code_bytes, PercentOfOatBytes(managed_code_bytes), |
| 1000 | managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes), |
| 1001 | native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes)) |
| 1002 | << std::endl << std::flush; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1003 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1004 | os << StringPrintf("\tgc_map_bytes = %7zd (%2.0f%% of oat file_bytes)\n" |
| 1005 | "\tpc_mapping_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n" |
| 1006 | "\tvmap_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n", |
| 1007 | gc_map_bytes, PercentOfOatBytes(gc_map_bytes), |
| 1008 | pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes), |
| 1009 | vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes)) |
| 1010 | << std::endl << std::flush; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1011 | |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 1012 | os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1013 | os << StringPrintf("\tmanaged_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n", |
| 1014 | static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes), |
| 1015 | static_cast<double>(managed_code_bytes_ignoring_deduplication) / |
| 1016 | static_cast<double>(dex_instruction_bytes)); |
| 1017 | os << std::endl << std::flush; |
| 1018 | |
| 1019 | DumpOutliers(os); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1020 | } |
| 1021 | } stats_; |
| 1022 | |
| 1023 | private: |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1024 | UniquePtr<OatDumper> oat_dumper_; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1025 | std::ostream& os_; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1026 | const std::string image_filename_; |
| 1027 | const std::string host_prefix_; |
| 1028 | Space& image_space_; |
| 1029 | const ImageHeader& image_header_; |
Elliott Hughes | d1bb4f6 | 2011-09-23 14:09:45 -0700 | [diff] [blame] | 1030 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1031 | DISALLOW_COPY_AND_ASSIGN(ImageDumper); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1032 | }; |
| 1033 | |
| 1034 | int oatdump(int argc, char** argv) { |
| 1035 | // Skip over argv[0]. |
| 1036 | argv++; |
| 1037 | argc--; |
| 1038 | |
| 1039 | if (argc == 0) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1040 | fprintf(stderr, "No arguments specified\n"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1041 | usage(); |
| 1042 | } |
| 1043 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1044 | const char* oat_filename = NULL; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1045 | const char* image_filename = NULL; |
| 1046 | const char* boot_image_filename = NULL; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1047 | std::string host_prefix; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1048 | std::ostream* os = &std::cout; |
| 1049 | UniquePtr<std::ofstream> out; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1050 | |
| 1051 | for (int i = 0; i < argc; i++) { |
| 1052 | const StringPiece option(argv[i]); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 1053 | if (option.starts_with("--oat-file=")) { |
| 1054 | oat_filename = option.substr(strlen("--oat-file=")).data(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1055 | } else if (option.starts_with("--image=")) { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1056 | image_filename = option.substr(strlen("--image=")).data(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1057 | } else if (option.starts_with("--boot-image=")) { |
| 1058 | boot_image_filename = option.substr(strlen("--boot-image=")).data(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1059 | } else if (option.starts_with("--host-prefix=")) { |
| 1060 | host_prefix = option.substr(strlen("--host-prefix=")).data(); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1061 | } else if (option.starts_with("--output=")) { |
| 1062 | const char* filename = option.substr(strlen("--output=")).data(); |
| 1063 | out.reset(new std::ofstream(filename)); |
| 1064 | if (!out->good()) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1065 | fprintf(stderr, "Failed to open output filename %s\n", filename); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1066 | usage(); |
| 1067 | } |
| 1068 | os = out.get(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1069 | } else { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1070 | fprintf(stderr, "Unknown argument %s\n", option.data()); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1071 | usage(); |
| 1072 | } |
| 1073 | } |
| 1074 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1075 | if (image_filename == NULL && oat_filename == NULL) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 1076 | fprintf(stderr, "Either --image or --oat must be specified\n"); |
| 1077 | return EXIT_FAILURE; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1080 | if (image_filename != NULL && oat_filename != NULL) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 1081 | fprintf(stderr, "Either --image or --oat must be specified but not both\n"); |
| 1082 | return EXIT_FAILURE; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | if (oat_filename != NULL) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1086 | OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, NULL); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1087 | if (oat_file == NULL) { |
| 1088 | fprintf(stderr, "Failed to open oat file from %s\n", oat_filename); |
| 1089 | return EXIT_FAILURE; |
| 1090 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1091 | OatDumper oat_dumper(*oat_file); |
| 1092 | oat_dumper.Dump(*os); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1093 | return EXIT_SUCCESS; |
| 1094 | } |
| 1095 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1096 | Runtime::Options options; |
| 1097 | std::string image_option; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1098 | std::string oat_option; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1099 | std::string boot_image_option; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1100 | std::string boot_oat_option; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1101 | if (boot_image_filename != NULL) { |
| 1102 | boot_image_option += "-Ximage:"; |
| 1103 | boot_image_option += boot_image_filename; |
| 1104 | 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] | 1105 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1106 | if (image_filename != NULL) { |
| 1107 | image_option += "-Ximage:"; |
| 1108 | image_option += image_filename; |
| 1109 | options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL))); |
| 1110 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1111 | |
Brian Carlstrom | fe487d0 | 2012-02-29 18:49:16 -0800 | [diff] [blame] | 1112 | if (host_prefix.empty()) { |
| 1113 | const char* android_product_out = getenv("ANDROID_PRODUCT_OUT"); |
| 1114 | if (android_product_out != NULL) { |
| 1115 | host_prefix = android_product_out; |
| 1116 | } |
| 1117 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1118 | if (!host_prefix.empty()) { |
| 1119 | options.push_back(std::make_pair("host-prefix", host_prefix.c_str())); |
| 1120 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1121 | |
| 1122 | UniquePtr<Runtime> runtime(Runtime::Create(options, false)); |
| 1123 | if (runtime.get() == NULL) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1124 | fprintf(stderr, "Failed to create runtime\n"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1125 | return EXIT_FAILURE; |
| 1126 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1127 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame^] | 1128 | Heap* heap = Runtime::Current()->GetHeap(); |
| 1129 | ImageSpace* image_space = heap->GetSpaces()[heap->GetSpaces().size()-2]->AsImageSpace(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1130 | CHECK(image_space != NULL); |
| 1131 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1132 | if (!image_header.IsValid()) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1133 | fprintf(stderr, "Invalid image header %s\n", image_filename); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1134 | return EXIT_FAILURE; |
| 1135 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1136 | ImageDumper image_dumper(*os, image_filename, host_prefix, *image_space, image_header); |
| 1137 | image_dumper.Dump(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1138 | return EXIT_SUCCESS; |
| 1139 | } |
| 1140 | |
| 1141 | } // namespace art |
| 1142 | |
| 1143 | int main(int argc, char** argv) { |
| 1144 | return art::oatdump(argc, argv); |
| 1145 | } |