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