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