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, |
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) { |
| 300 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 301 | const char* name = dex_file.GetMethodName(method_id); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 302 | std::string signature(dex_file.GetMethodSignature(method_id)); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 303 | os << StringPrintf("\t%d: %s %s (dex_method_idx=%d)\n", |
| 304 | class_method_index, name, signature.c_str(), dex_method_idx); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 305 | os << StringPrintf("\t\tframe_size_in_bytes: %zd\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 306 | oat_method.GetFrameSizeInBytes()); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 307 | os << StringPrintf("\t\tcore_spill_mask: 0x%08x", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 308 | oat_method.GetCoreSpillMask()); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 309 | DumpSpillMask(os, oat_method.GetCoreSpillMask(), false); |
| 310 | os << StringPrintf("\n\t\tfp_spill_mask: 0x%08x", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 311 | oat_method.GetFpSpillMask()); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 312 | DumpSpillMask(os, oat_method.GetFpSpillMask(), true); |
| 313 | os << StringPrintf("\n\t\tmapping_table: %p (offset=0x%08x)\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 314 | oat_method.GetMappingTable(), oat_method.GetMappingTableOffset()); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 315 | DumpMappingTable(os, oat_method); |
Elliott Hughes | ed2adb6 | 2012-02-29 14:41:01 -0800 | [diff] [blame] | 316 | os << StringPrintf("\t\tvmap_table: %p (offset=0x%08x)\n", |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 317 | oat_method.GetVmapTable(), oat_method.GetVmapTableOffset()); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 318 | DumpVmap(os, oat_method.GetVmapTable(), oat_method.GetCoreSpillMask(), |
| 319 | oat_method.GetFpSpillMask()); |
Elliott Hughes | ed2adb6 | 2012-02-29 14:41:01 -0800 | [diff] [blame] | 320 | os << StringPrintf("\t\tgc_map: %p (offset=0x%08x)\n", |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 321 | oat_method.GetGcMap(), oat_method.GetGcMapOffset()); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 322 | DumpGcMap(os, oat_method.GetGcMap()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 323 | os << StringPrintf("\t\tCODE: %p (offset=0x%08x size=%d)%s\n", |
| 324 | oat_method.GetCode(), |
| 325 | oat_method.GetCodeOffset(), |
| 326 | oat_method.GetCodeSize(), |
| 327 | oat_method.GetCode() != NULL ? "..." : ""); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 328 | DumpCode(os, oat_method.GetCode(), oat_method.GetMappingTable(), dex_file, code_item); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 329 | os << StringPrintf("\t\tINVOKE STUB: %p (offset=0x%08x size=%d)%s\n", |
| 330 | oat_method.GetInvokeStub(), |
| 331 | oat_method.GetInvokeStubOffset(), |
| 332 | oat_method.GetInvokeStubSize(), |
| 333 | oat_method.GetInvokeStub() != NULL ? "..." : ""); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 334 | 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] | 335 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 336 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 337 | void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) { |
| 338 | if (spill_mask == 0) { |
| 339 | return; |
| 340 | } |
| 341 | os << " ("; |
| 342 | for (size_t i = 0; i < 32; i++) { |
| 343 | if ((spill_mask & (1 << i)) != 0) { |
| 344 | if (is_float) { |
| 345 | os << "fr" << i; |
| 346 | } else { |
| 347 | os << "r" << i; |
| 348 | } |
| 349 | spill_mask ^= 1 << i; // clear bit |
| 350 | if (spill_mask != 0) { |
| 351 | os << ", "; |
| 352 | } else { |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | os << ")"; |
| 358 | } |
| 359 | |
| 360 | void DumpVmap(std::ostream& os, const uint16_t* raw_table, uint32_t core_spill_mask, |
| 361 | uint32_t fp_spill_mask) { |
| 362 | if (raw_table == NULL) { |
| 363 | return; |
| 364 | } |
| 365 | const VmapTable vmap_table(raw_table); |
| 366 | bool first = true; |
| 367 | os << "\t\t\t"; |
| 368 | for (size_t i = 0; i < vmap_table.size(); i++) { |
| 369 | uint16_t dex_reg = vmap_table[i]; |
| 370 | size_t matches = 0; |
| 371 | size_t spill_shifts = 0; |
| 372 | uint32_t spill_mask = core_spill_mask; |
| 373 | bool processing_fp = false; |
| 374 | while (matches != (i + 1)) { |
| 375 | if (spill_mask == 0) { |
| 376 | CHECK(!processing_fp); |
| 377 | spill_mask = fp_spill_mask; |
| 378 | processing_fp = true; |
| 379 | } |
| 380 | matches += spill_mask & 1; // Add 1 if the low bit is set |
| 381 | spill_mask >>= 1; |
| 382 | spill_shifts++; |
| 383 | } |
| 384 | size_t arm_reg = spill_shifts - 1; // wind back one as we want the last match |
| 385 | os << (first ? "v" : ", v") << dex_reg; |
| 386 | if (arm_reg < 16) { |
| 387 | os << "/r" << arm_reg; |
| 388 | } else { |
| 389 | os << "/fr" << (arm_reg - 16); |
| 390 | } |
| 391 | if (first) { |
| 392 | first = false; |
| 393 | } |
| 394 | } |
| 395 | os << std::endl; |
| 396 | } |
| 397 | |
| 398 | void DumpGcMap(std::ostream& os, const uint8_t* gc_map_raw) { |
| 399 | if (gc_map_raw == NULL) { |
| 400 | return; |
| 401 | } |
| 402 | uint32_t gc_map_length = (gc_map_raw[0] << 24) | (gc_map_raw[1] << 16) | |
| 403 | (gc_map_raw[2] << 8) | (gc_map_raw[3] << 0); |
| 404 | verifier::PcToReferenceMap map(gc_map_raw + sizeof(uint32_t), gc_map_length); |
| 405 | for (size_t entry = 0; entry < map.NumEntries(); entry++) { |
| 406 | os << StringPrintf("\t\t\t0x%04x", map.GetPC(entry)); |
| 407 | size_t num_regs = map.RegWidth() * 8; |
| 408 | const uint8_t* reg_bitmap = map.GetBitMap(entry); |
| 409 | bool first = true; |
| 410 | for (size_t reg = 0; reg < num_regs; reg++) { |
| 411 | if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) { |
| 412 | if (first) { |
| 413 | os << " v" << reg; |
| 414 | first = false; |
| 415 | } else { |
| 416 | os << ", v" << reg; |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | os << std::endl; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) { |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 425 | const uint32_t* raw_table = oat_method.GetMappingTable(); |
| 426 | const void* code = oat_method.GetCode(); |
| 427 | if (raw_table == NULL || code == NULL) { |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | uint32_t length = *raw_table; |
| 432 | ++raw_table; |
| 433 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 434 | os << "\t\t{"; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 435 | for (size_t i = 0; i < length; i += 2) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 436 | 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] | 437 | uint32_t dex_pc = raw_table[i + 1]; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 438 | os << StringPrintf("%p -> 0x%04x", native_pc, dex_pc); |
| 439 | if (i + 2 < length) { |
| 440 | os << ", "; |
| 441 | } |
| 442 | } |
| 443 | os << "}" << std::endl << std::flush; |
| 444 | } |
| 445 | |
| 446 | void DumpCode(std::ostream& os, const void* code, const uint32_t* raw_mapping_table, |
| 447 | const DexFile& dex_file, const DexFile::CodeItem* code_item) { |
| 448 | if (code == NULL) { |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | if (raw_mapping_table == NULL) { |
| 453 | // code but no mapping table is most likely caused by code created by the JNI compiler |
| 454 | const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code); |
| 455 | const uint8_t* oat_begin = reinterpret_cast<const uint8_t*>(oat_file_.Begin()); |
| 456 | uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin); |
| 457 | |
| 458 | typedef std::set<uint32_t>::iterator It; |
| 459 | It it = offsets_.upper_bound(last_offset); |
| 460 | CHECK(it != offsets_.end()); |
| 461 | const uint8_t* end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it; |
| 462 | CHECK(native_pc < end_native_pc); |
| 463 | |
| 464 | disassembler_->Dump(os, native_pc, end_native_pc); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | uint32_t length = *raw_mapping_table; |
| 469 | ++raw_mapping_table; |
| 470 | |
| 471 | for (size_t i = 0; i < length; i += 2) { |
| 472 | uint32_t dex_pc = raw_mapping_table[i + 1]; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 473 | const Instruction* instruction = Instruction::At(&code_item->insns_[dex_pc]); |
| 474 | os << StringPrintf("\t\t0x%04x: %s\n", dex_pc, instruction->DumpString(&dex_file).c_str()); |
| 475 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 476 | 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] | 477 | const uint8_t* end_native_pc = NULL; |
| 478 | if (i + 2 < length) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 479 | 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] | 480 | } else { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 481 | 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] | 482 | uint32_t last_offset = static_cast<uint32_t>(native_pc - oat_begin); |
| 483 | |
| 484 | typedef std::set<uint32_t>::iterator It; |
Elliott Hughes | ed2adb6 | 2012-02-29 14:41:01 -0800 | [diff] [blame] | 485 | It it = offsets_.upper_bound(last_offset); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 486 | CHECK(it != offsets_.end()); |
| 487 | end_native_pc = reinterpret_cast<const uint8_t*>(oat_begin) + *it; |
| 488 | } |
Elliott Hughes | ed2adb6 | 2012-02-29 14:41:01 -0800 | [diff] [blame] | 489 | CHECK(native_pc < end_native_pc); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 490 | disassembler_->Dump(os, native_pc, end_native_pc); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 494 | const std::string host_prefix_; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 495 | const OatFile& oat_file_; |
| 496 | std::vector<const OatFile::OatDexFile*> oat_dex_files_; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 497 | std::set<uint32_t> offsets_; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 498 | UniquePtr<Disassembler> disassembler_; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 499 | }; |
| 500 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 501 | class ImageDumper { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 502 | public: |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 503 | explicit ImageDumper(std::ostream& os, const std::string& image_filename, |
| 504 | const std::string& host_prefix, Space& image_space, |
| 505 | const ImageHeader& image_header) : os_(os), |
| 506 | image_filename_(image_filename), host_prefix_(host_prefix), |
| 507 | image_space_(image_space), image_header_(image_header) { |
| 508 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 509 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 510 | void Dump() { |
| 511 | os_ << "MAGIC:\n"; |
| 512 | os_ << image_header_.GetMagic() << "\n\n"; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 513 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 514 | os_ << "IMAGE BEGIN:\n"; |
| 515 | os_ << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 516 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 517 | os_ << "OAT CHECKSUM:\n"; |
| 518 | os_ << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum()); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 519 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 520 | os_ << "OAT BEGIN:\n"; |
| 521 | os_ << reinterpret_cast<void*>(image_header_.GetOatBegin()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 522 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 523 | os_ << "OAT END:\n"; |
| 524 | os_ << reinterpret_cast<void*>(image_header_.GetOatEnd()) << "\n\n"; |
| 525 | |
| 526 | os_ << "ROOTS:\n"; |
| 527 | os_ << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 528 | CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax)); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 529 | for (int i = 0; i < ImageHeader::kImageRootsMax; i++) { |
| 530 | ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i); |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 531 | const char* image_root_description = image_roots_descriptions_[i]; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 532 | Object* image_root_object = image_header_.GetImageRoot(image_root); |
| 533 | os_ << StringPrintf("%s: %p\n", image_root_description, image_root_object); |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 534 | if (image_root_object->IsObjectArray()) { |
| 535 | // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this) |
| 536 | ObjectArray<Object>* image_root_object_array |
| 537 | = down_cast<ObjectArray<Object>*>(image_root_object); |
| 538 | // = image_root_object->AsObjectArray<Object>(); |
| 539 | for (int i = 0; i < image_root_object_array->GetLength(); i++) { |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 540 | Object* value = image_root_object_array->Get(i); |
| 541 | if (value != NULL) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 542 | os_ << "\t" << i << ": "; |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 543 | std::string summary; |
| 544 | PrettyObjectValue(summary, value->GetClass(), value); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 545 | os_ << summary; |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 546 | } else { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 547 | os_ << StringPrintf("\t%d: null\n", i); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 548 | } |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 549 | } |
| 550 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 551 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 552 | os_ << "\n"; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 553 | |
Brian Carlstrom | 4dcbffb | 2012-03-07 00:21:18 -0800 | [diff] [blame] | 554 | os_ << "OAT LOCATION:\n" << std::flush; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 555 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 556 | Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation); |
| 557 | std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8()); |
Brian Carlstrom | 4dcbffb | 2012-03-07 00:21:18 -0800 | [diff] [blame] | 558 | os_ << oat_location; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 559 | if (!host_prefix_.empty()) { |
| 560 | oat_location = host_prefix_ + oat_location; |
| 561 | os_ << " (" << oat_location << ")"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 562 | } |
Brian Carlstrom | 4dcbffb | 2012-03-07 00:21:18 -0800 | [diff] [blame] | 563 | os_ << "\n"; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 564 | const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 565 | if (oat_file == NULL) { |
Brian Carlstrom | 4dcbffb | 2012-03-07 00:21:18 -0800 | [diff] [blame] | 566 | os_ << "NOT FOUND\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 567 | return; |
| 568 | } |
Brian Carlstrom | 4dcbffb | 2012-03-07 00:21:18 -0800 | [diff] [blame] | 569 | os_ << "\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 570 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 571 | stats_.oat_file_bytes = oat_file->Size(); |
| 572 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 573 | oat_dumper_.reset(new OatDumper(host_prefix_, *oat_file)); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 574 | |
| 575 | os_ << "OBJECTS:\n" << std::flush; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 576 | HeapBitmap* heap_bitmap = Runtime::Current()->GetHeap()->GetLiveBits(); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 577 | DCHECK(heap_bitmap != NULL); |
| 578 | heap_bitmap->Walk(ImageDumper::Callback, this); |
| 579 | os_ << "\n"; |
| 580 | |
| 581 | os_ << "STATS:\n" << std::flush; |
| 582 | UniquePtr<File> file(OS::OpenFile(image_filename_.c_str(), false)); |
| 583 | stats_.file_bytes = file->Length(); |
| 584 | size_t header_bytes = sizeof(ImageHeader); |
| 585 | stats_.header_bytes = header_bytes; |
| 586 | size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes; |
| 587 | stats_.alignment_bytes += alignment_bytes; |
| 588 | stats_.Dump(os_); |
| 589 | os_ << "\n"; |
| 590 | |
| 591 | os_ << std::flush; |
| 592 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 593 | oat_dumper_->Dump(os_); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 596 | private: |
| 597 | |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 598 | static void PrettyObjectValue(std::string& summary, Class* type, Object* value) { |
| 599 | CHECK(type != NULL); |
| 600 | if (value == NULL) { |
| 601 | StringAppendF(&summary, "null %s\n", PrettyDescriptor(type).c_str()); |
| 602 | } else if (type->IsStringClass()) { |
| 603 | String* string = value->AsString(); |
| 604 | StringAppendF(&summary, "%p String: \"%s\"\n", string, string->ToModifiedUtf8().c_str()); |
| 605 | } else if (value->IsClass()) { |
| 606 | Class* klass = value->AsClass(); |
| 607 | StringAppendF(&summary, "%p Class: %s\n", klass, PrettyDescriptor(klass).c_str()); |
| 608 | } else if (value->IsField()) { |
| 609 | Field* field = value->AsField(); |
| 610 | StringAppendF(&summary, "%p Field: %s\n", field, PrettyField(field).c_str()); |
| 611 | } else if (value->IsMethod()) { |
| 612 | Method* method = value->AsMethod(); |
| 613 | StringAppendF(&summary, "%p Method: %s\n", method, PrettyMethod(method).c_str()); |
| 614 | } else { |
| 615 | StringAppendF(&summary, "%p %s\n", value, PrettyDescriptor(type).c_str()); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | static void PrintField(std::string& summary, Field* field, Object* obj) { |
| 620 | FieldHelper fh(field); |
| 621 | Class* type = fh.GetType(); |
| 622 | StringAppendF(&summary, "\t%s: ", fh.GetName()); |
| 623 | if (type->IsPrimitiveLong()) { |
| 624 | StringAppendF(&summary, "%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj)); |
| 625 | } else if (type->IsPrimitiveDouble()) { |
| 626 | StringAppendF(&summary, "%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj)); |
| 627 | } else if (type->IsPrimitiveFloat()) { |
| 628 | StringAppendF(&summary, "%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj)); |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 629 | } else if (type->IsPrimitive()) { |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 630 | StringAppendF(&summary, "%d (0x%x)\n", field->Get32(obj), field->Get32(obj)); |
| 631 | } else { |
| 632 | Object* value = field->GetObj(obj); |
| 633 | PrettyObjectValue(summary, type, value); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | static void DumpFields(std::string& summary, Object* obj, Class* klass) { |
| 638 | Class* super = klass->GetSuperClass(); |
| 639 | if (super != NULL) { |
| 640 | DumpFields(summary, obj, super); |
| 641 | } |
| 642 | ObjectArray<Field>* fields = klass->GetIFields(); |
| 643 | if (fields != NULL) { |
| 644 | for (int32_t i = 0; i < fields->GetLength(); i++) { |
| 645 | Field* field = fields->Get(i); |
| 646 | PrintField(summary, field, obj); |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 651 | bool InDumpSpace(const Object* object) { |
| 652 | return image_space_.Contains(object); |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 653 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 654 | |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 655 | const void* GetOatCodeBegin(Method* m) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 656 | Runtime* runtime = Runtime::Current(); |
| 657 | const void* code = m->GetCode(); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 658 | if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 659 | code = oat_dumper_->GetOatCode(m); |
| 660 | } |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 661 | if (oat_dumper_->GetInstructionSet() == kThumb2) { |
| 662 | code = reinterpret_cast<void*>(reinterpret_cast<uint32_t>(code) & ~0x1); |
| 663 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 664 | return code; |
| 665 | } |
| 666 | |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 667 | uint32_t GetOatCodeSize(Method* m) { |
| 668 | const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetOatCodeBegin(m)); |
| 669 | if (oat_code_begin == NULL) { |
| 670 | return 0; |
| 671 | } |
| 672 | return oat_code_begin[-1]; |
| 673 | } |
| 674 | |
| 675 | const void* GetOatCodeEnd(Method* m) { |
| 676 | const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetOatCodeBegin(m)); |
| 677 | if (oat_code_begin == NULL) { |
| 678 | return NULL; |
| 679 | } |
| 680 | return oat_code_begin + GetOatCodeSize(m); |
| 681 | } |
| 682 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 683 | static void Callback(Object* obj, void* arg) { |
| 684 | DCHECK(obj != NULL); |
| 685 | DCHECK(arg != NULL); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 686 | ImageDumper* state = reinterpret_cast<ImageDumper*>(arg); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 687 | if (!state->InDumpSpace(obj)) { |
| 688 | return; |
| 689 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 690 | |
| 691 | size_t object_bytes = obj->SizeOf(); |
| 692 | size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes; |
| 693 | state->stats_.object_bytes += object_bytes; |
| 694 | state->stats_.alignment_bytes += alignment_bytes; |
| 695 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 696 | std::string summary; |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 697 | Class* obj_class = obj->GetClass(); |
| 698 | if (obj_class->IsArrayClass()) { |
| 699 | StringAppendF(&summary, "%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(), |
| 700 | obj->AsArray()->GetLength()); |
| 701 | } else if (obj->IsClass()) { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 702 | Class* klass = obj->AsClass(); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 703 | StringAppendF(&summary, "%p: java.lang.Class \"%s\" (", obj, |
| 704 | PrettyDescriptor(klass).c_str()); |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 705 | std::ostringstream ss; |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 706 | ss << klass->GetStatus() << ")\n"; |
Brian Carlstrom | e10b697 | 2011-09-26 13:49:03 -0700 | [diff] [blame] | 707 | summary += ss.str(); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 708 | } else if (obj->IsField()) { |
| 709 | StringAppendF(&summary, "%p: java.lang.reflect.Field %s\n", obj, |
| 710 | PrettyField(obj->AsField()).c_str()); |
| 711 | } else if (obj->IsMethod()) { |
| 712 | StringAppendF(&summary, "%p: java.lang.reflect.Method %s\n", obj, |
| 713 | PrettyMethod(obj->AsMethod()).c_str()); |
| 714 | } else if (obj_class->IsStringClass()) { |
| 715 | StringAppendF(&summary, "%p: java.lang.String \"%s\"\n", obj, |
| 716 | obj->AsString()->ToModifiedUtf8().c_str()); |
| 717 | } else { |
| 718 | StringAppendF(&summary, "%p: %s\n", obj, PrettyDescriptor(obj_class).c_str()); |
| 719 | } |
| 720 | DumpFields(summary, obj, obj_class); |
| 721 | if (obj->IsObjectArray()) { |
| 722 | ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>(); |
| 723 | int32_t length = obj_array->GetLength(); |
| 724 | for (int32_t i = 0; i < length; i++) { |
| 725 | Object* value = obj_array->Get(i); |
| 726 | size_t run = 0; |
| 727 | for (int32_t j = i + 1; j < length; j++) { |
| 728 | if (value == obj_array->Get(j)) { |
| 729 | run++; |
| 730 | } else { |
| 731 | break; |
| 732 | } |
| 733 | } |
| 734 | if (run == 0) { |
| 735 | StringAppendF(&summary, "\t%d: ", i); |
| 736 | } else { |
Elliott Hughes | c1051ae | 2012-02-27 12:52:31 -0800 | [diff] [blame] | 737 | StringAppendF(&summary, "\t%d to %zd: ", i, i + run); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 738 | i = i + run; |
| 739 | } |
| 740 | Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass(); |
| 741 | PrettyObjectValue(summary, value_class, value); |
| 742 | } |
| 743 | } else if (obj->IsClass()) { |
| 744 | ObjectArray<Field>* sfields = obj->AsClass()->GetSFields(); |
| 745 | if (sfields != NULL) { |
| 746 | summary += "\t\tSTATICS:\n"; |
| 747 | for (int32_t i = 0; i < sfields->GetLength(); i++) { |
| 748 | Field* field = sfields->Get(i); |
| 749 | PrintField(summary, field, NULL); |
| 750 | } |
| 751 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 752 | } else if (obj->IsMethod()) { |
| 753 | Method* method = obj->AsMethod(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 754 | if (method->IsNative()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 755 | DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 756 | DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 757 | DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 758 | bool first_occurrence; |
| 759 | size_t invoke_stub_size = state->ComputeOatSize( |
| 760 | reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence); |
| 761 | if (first_occurrence) { |
| 762 | state->stats_.managed_to_native_code_bytes += invoke_stub_size; |
| 763 | } |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 764 | const void* oat_code = state->GetOatCodeBegin(method); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 765 | size_t code_size = state->ComputeOatSize(oat_code, &first_occurrence); |
| 766 | if (first_occurrence) { |
| 767 | state->stats_.native_to_managed_code_bytes += code_size; |
| 768 | } |
| 769 | if (oat_code != method->GetCode()) { |
| 770 | StringAppendF(&summary, "\t\tOAT CODE: %p\n", oat_code); |
| 771 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 772 | } else if (method->IsAbstract() || method->IsCalleeSaveMethod() || |
| 773 | method->IsResolutionMethod()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 774 | DCHECK(method->GetGcMap() == NULL) << PrettyMethod(method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 775 | DCHECK_EQ(0U, method->GetGcMapLength()) << PrettyMethod(method); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 776 | DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 777 | } else { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 778 | DCHECK(method->GetGcMap() != NULL) << PrettyMethod(method); |
| 779 | DCHECK_NE(0U, method->GetGcMapLength()) << PrettyMethod(method); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 780 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 781 | const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 782 | 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] | 783 | state->stats_.dex_instruction_bytes += dex_instruction_bytes; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 784 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 785 | bool first_occurance; |
| 786 | size_t gc_map_bytes = state->ComputeOatSize(method->GetGcMapRaw(), &first_occurance); |
| 787 | if (first_occurance) { |
| 788 | state->stats_.gc_map_bytes += gc_map_bytes; |
| 789 | } |
| 790 | |
| 791 | size_t pc_mapping_table_bytes = |
| 792 | state->ComputeOatSize(method->GetMappingTableRaw(), &first_occurance); |
| 793 | if (first_occurance) { |
| 794 | state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes; |
| 795 | } |
| 796 | |
| 797 | size_t vmap_table_bytes = |
| 798 | state->ComputeOatSize(method->GetVmapTableRaw(), &first_occurance); |
| 799 | if (first_occurance) { |
| 800 | state->stats_.vmap_table_bytes += vmap_table_bytes; |
| 801 | } |
| 802 | |
| 803 | size_t invoke_stub_size = state->ComputeOatSize( |
| 804 | reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurance); |
| 805 | if (first_occurance) { |
| 806 | state->stats_.native_to_managed_code_bytes += invoke_stub_size; |
| 807 | } |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 808 | const void* oat_code_begin = state->GetOatCodeBegin(method); |
| 809 | const void* oat_code_end = state->GetOatCodeEnd(method); |
| 810 | // TODO: use oat_code_size and remove code_size based on offsets |
| 811 | // uint32_t oat_code_size = state->GetOatCodeSize(method); |
| 812 | size_t code_size = state->ComputeOatSize(oat_code_begin, &first_occurance); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 813 | if (first_occurance) { |
| 814 | state->stats_.managed_code_bytes += code_size; |
| 815 | } |
| 816 | state->stats_.managed_code_bytes_ignoring_deduplication += code_size; |
| 817 | |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 818 | 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] | 819 | StringAppendF(&summary, "\t\tSIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n", |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 820 | dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes); |
| 821 | |
| 822 | size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes + |
| 823 | vmap_table_bytes + invoke_stub_size + code_size + object_bytes; |
| 824 | |
| 825 | double expansion = |
| 826 | static_cast<double>(code_size) / static_cast<double>(dex_instruction_bytes); |
| 827 | state->stats_.ComputeOutliers(total_size, expansion, method); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 828 | } |
| 829 | } |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 830 | std::string descriptor(ClassHelper(obj_class).GetDescriptor()); |
| 831 | state->stats_.descriptor_to_bytes[descriptor] += object_bytes; |
| 832 | state->stats_.descriptor_to_count[descriptor] += 1; |
| 833 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 834 | state->os_ << summary << std::flush; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 835 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 836 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 837 | std::set<const void*> already_seen_; |
| 838 | // Compute the size of the given data within the oat file and whether this is the first time |
| 839 | // this data has been requested |
| 840 | size_t ComputeOatSize(const void* oat_data, bool* first_occurance) { |
| 841 | if (already_seen_.count(oat_data) == 0) { |
| 842 | *first_occurance = true; |
| 843 | already_seen_.insert(oat_data); |
| 844 | } else { |
| 845 | *first_occurance = false; |
| 846 | } |
| 847 | return oat_dumper_->ComputeSize(oat_data); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 848 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 849 | |
| 850 | public: |
| 851 | struct Stats { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 852 | size_t oat_file_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 853 | size_t file_bytes; |
| 854 | |
| 855 | size_t header_bytes; |
| 856 | size_t object_bytes; |
| 857 | size_t alignment_bytes; |
| 858 | |
| 859 | size_t managed_code_bytes; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 860 | size_t managed_code_bytes_ignoring_deduplication; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 861 | size_t managed_to_native_code_bytes; |
| 862 | size_t native_to_managed_code_bytes; |
| 863 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 864 | size_t gc_map_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 865 | size_t pc_mapping_table_bytes; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 866 | size_t vmap_table_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 867 | |
| 868 | size_t dex_instruction_bytes; |
| 869 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 870 | std::vector<Method*> method_outlier; |
| 871 | std::vector<size_t> method_outlier_size; |
| 872 | std::vector<double> method_outlier_expansion; |
| 873 | |
| 874 | explicit Stats() |
| 875 | : oat_file_bytes(0), |
| 876 | file_bytes(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 877 | header_bytes(0), |
| 878 | object_bytes(0), |
| 879 | alignment_bytes(0), |
| 880 | managed_code_bytes(0), |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 881 | managed_code_bytes_ignoring_deduplication(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 882 | managed_to_native_code_bytes(0), |
| 883 | native_to_managed_code_bytes(0), |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 884 | gc_map_bytes(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 885 | pc_mapping_table_bytes(0), |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 886 | vmap_table_bytes(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 887 | dex_instruction_bytes(0) {} |
| 888 | |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 889 | typedef std::map<std::string, size_t> TableBytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 890 | TableBytes descriptor_to_bytes; |
| 891 | |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 892 | typedef std::map<std::string, size_t> TableCount; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 893 | TableCount descriptor_to_count; |
| 894 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 895 | double PercentOfOatBytes(size_t size) { |
| 896 | return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100; |
| 897 | } |
| 898 | |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 899 | double PercentOfFileBytes(size_t size) { |
| 900 | return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100; |
| 901 | } |
| 902 | |
| 903 | double PercentOfObjectBytes(size_t size) { |
| 904 | return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100; |
| 905 | } |
| 906 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 907 | void ComputeOutliers(size_t total_size, double expansion, Method* method) { |
| 908 | method_outlier_size.push_back(total_size); |
| 909 | method_outlier_expansion.push_back(expansion); |
| 910 | method_outlier.push_back(method); |
| 911 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 912 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 913 | void DumpOutliers(std::ostream& os) { |
| 914 | size_t sum_of_sizes = 0; |
| 915 | size_t sum_of_sizes_squared = 0; |
| 916 | size_t sum_of_expansion = 0; |
| 917 | size_t sum_of_expansion_squared = 0; |
| 918 | size_t n = method_outlier_size.size(); |
| 919 | for (size_t i = 0; i < n; i++) { |
| 920 | size_t cur_size = method_outlier_size[i]; |
| 921 | sum_of_sizes += cur_size; |
| 922 | sum_of_sizes_squared += cur_size * cur_size; |
| 923 | double cur_expansion = method_outlier_expansion[i]; |
| 924 | sum_of_expansion += cur_expansion; |
| 925 | sum_of_expansion_squared += cur_expansion * cur_expansion; |
| 926 | } |
| 927 | size_t size_mean = sum_of_sizes / n; |
| 928 | size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1); |
| 929 | double expansion_mean = sum_of_expansion / n; |
| 930 | double expansion_variance = |
| 931 | (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1); |
| 932 | |
| 933 | // Dump methods whose size is a certain number of standard deviations from the mean |
| 934 | size_t dumped_values = 0; |
| 935 | size_t skipped_values = 0; |
| 936 | for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations |
| 937 | size_t cur_size_variance = i * i * size_variance; |
| 938 | bool first = true; |
| 939 | for (size_t j = 0; j < n; j++) { |
| 940 | size_t cur_size = method_outlier_size[j]; |
| 941 | if (cur_size > size_mean) { |
| 942 | size_t cur_var = cur_size - size_mean; |
| 943 | cur_var = cur_var * cur_var; |
| 944 | if (cur_var > cur_size_variance) { |
| 945 | if (dumped_values > 20) { |
| 946 | if (i == 1) { |
| 947 | skipped_values++; |
| 948 | } else { |
| 949 | i = 2; // jump to counting for 1 standard deviation |
| 950 | break; |
| 951 | } |
| 952 | } else { |
| 953 | if (first) { |
| 954 | os << "\nBig methods (size > " << i << " standard deviations the norm):" |
| 955 | << std::endl; |
| 956 | first = false; |
| 957 | } |
| 958 | os << "\t" << PrettyMethod(method_outlier[j]) << " requires storage of " |
| 959 | << PrettySize(cur_size) << std::endl; |
| 960 | method_outlier_size[j] = 0; // don't consider this method again |
| 961 | dumped_values++; |
| 962 | } |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | } |
| 967 | if (skipped_values > 0) { |
| 968 | os << "\t... skipped " << skipped_values |
| 969 | << " methods with size > 1 standard deviation from the norm" << std::endl; |
| 970 | } |
| 971 | os << std::endl << std::flush; |
| 972 | |
| 973 | // Dump methods whose expansion is a certain number of standard deviations from the mean |
| 974 | dumped_values = 0; |
| 975 | skipped_values = 0; |
| 976 | for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations |
| 977 | double cur_expansion_variance = i * i * expansion_variance; |
| 978 | bool first = true; |
| 979 | for (size_t j = 0; j < n; j++) { |
| 980 | double cur_expansion = method_outlier_expansion[j]; |
| 981 | if (cur_expansion > expansion_mean) { |
| 982 | size_t cur_var = cur_expansion - expansion_mean; |
| 983 | cur_var = cur_var * cur_var; |
| 984 | if (cur_var > cur_expansion_variance) { |
| 985 | if (dumped_values > 20) { |
| 986 | if (i == 1) { |
| 987 | skipped_values++; |
| 988 | } else { |
| 989 | i = 2; // jump to counting for 1 standard deviation |
| 990 | break; |
| 991 | } |
| 992 | } else { |
| 993 | if (first) { |
| 994 | os << "\nLarge expansion methods (size > " << i |
| 995 | << " standard deviations the norm):" << std::endl; |
| 996 | first = false; |
| 997 | } |
| 998 | os << "\t" << PrettyMethod(method_outlier[j]) << " expanded code by " |
| 999 | << cur_expansion << std::endl; |
| 1000 | method_outlier_expansion[j] = 0.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 |
| 1009 | << " methods with expansion > 1 standard deviation from the norm" << std::endl; |
| 1010 | } |
| 1011 | os << std::endl << std::flush; |
| 1012 | } |
| 1013 | |
| 1014 | void Dump(std::ostream& os) { |
| 1015 | os << "\tart_file_bytes = " << PrettySize(file_bytes) << std::endl << std::endl |
| 1016 | << "\tart_file_bytes = header_bytes + object_bytes + alignment_bytes" << std::endl |
| 1017 | << StringPrintf("\theader_bytes = %8zd (%2.0f%% of art file bytes)\n" |
| 1018 | "\tobject_bytes = %8zd (%2.0f%% of art file bytes)\n" |
| 1019 | "\talignment_bytes = %8zd (%2.0f%% of art file bytes)\n", |
| 1020 | header_bytes, PercentOfFileBytes(header_bytes), |
| 1021 | object_bytes, PercentOfFileBytes(object_bytes), |
| 1022 | alignment_bytes, PercentOfFileBytes(alignment_bytes)) |
| 1023 | << std::endl << std::flush; |
| 1024 | |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1025 | CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes); |
| 1026 | |
| 1027 | os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n"; |
| 1028 | size_t object_bytes_total = 0; |
| 1029 | typedef TableBytes::const_iterator It; // TODO: C++0x auto |
| 1030 | 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] | 1031 | const std::string& descriptor(it->first); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1032 | size_t bytes = it->second; |
| 1033 | size_t count = descriptor_to_count[descriptor]; |
| 1034 | double average = static_cast<double>(bytes) / static_cast<double>(count); |
| 1035 | double percent = PercentOfObjectBytes(bytes); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 1036 | os << StringPrintf("\t%32s %8zd bytes %6zd instances " |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1037 | "(%3.0f bytes/instance) %2.0f%% of object_bytes\n", |
| 1038 | descriptor.c_str(), bytes, count, |
| 1039 | average, percent); |
| 1040 | |
| 1041 | object_bytes_total += bytes; |
| 1042 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1043 | os << std::endl << std::flush; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1044 | CHECK_EQ(object_bytes, object_bytes_total); |
| 1045 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1046 | os << StringPrintf("\tmanaged_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" |
| 1047 | "\tmanaged_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" |
| 1048 | "\tnative_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n", |
| 1049 | managed_code_bytes, PercentOfOatBytes(managed_code_bytes), |
| 1050 | managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes), |
| 1051 | native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes)) |
| 1052 | << std::endl << std::flush; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1053 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1054 | os << StringPrintf("\tgc_map_bytes = %7zd (%2.0f%% of oat file_bytes)\n" |
| 1055 | "\tpc_mapping_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n" |
| 1056 | "\tvmap_table_bytes = %7zd (%2.0f%% of oat file_bytes)\n", |
| 1057 | gc_map_bytes, PercentOfOatBytes(gc_map_bytes), |
| 1058 | pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes), |
| 1059 | vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes)) |
| 1060 | << std::endl << std::flush; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1061 | |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 1062 | os << StringPrintf("\tdex_instruction_bytes = %zd\n", dex_instruction_bytes); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1063 | os << StringPrintf("\tmanaged_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n", |
| 1064 | static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes), |
| 1065 | static_cast<double>(managed_code_bytes_ignoring_deduplication) / |
| 1066 | static_cast<double>(dex_instruction_bytes)); |
| 1067 | os << std::endl << std::flush; |
| 1068 | |
| 1069 | DumpOutliers(os); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1070 | } |
| 1071 | } stats_; |
| 1072 | |
| 1073 | private: |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1074 | UniquePtr<OatDumper> oat_dumper_; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1075 | std::ostream& os_; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1076 | const std::string image_filename_; |
| 1077 | const std::string host_prefix_; |
| 1078 | Space& image_space_; |
| 1079 | const ImageHeader& image_header_; |
Elliott Hughes | d1bb4f6 | 2011-09-23 14:09:45 -0700 | [diff] [blame] | 1080 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1081 | DISALLOW_COPY_AND_ASSIGN(ImageDumper); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1082 | }; |
| 1083 | |
| 1084 | int oatdump(int argc, char** argv) { |
| 1085 | // Skip over argv[0]. |
| 1086 | argv++; |
| 1087 | argc--; |
| 1088 | |
| 1089 | if (argc == 0) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1090 | fprintf(stderr, "No arguments specified\n"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1091 | usage(); |
| 1092 | } |
| 1093 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1094 | const char* oat_filename = NULL; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1095 | const char* image_filename = NULL; |
| 1096 | const char* boot_image_filename = NULL; |
Logan Chien | 0cc6ab6 | 2012-03-20 22:57:52 +0800 | [diff] [blame^] | 1097 | std::string elf_filename_prefix; |
Brian Carlstrom | 13c492b | 2012-03-22 17:09:17 -0700 | [diff] [blame] | 1098 | UniquePtr<std::string> host_prefix; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1099 | std::ostream* os = &std::cout; |
| 1100 | UniquePtr<std::ofstream> out; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1101 | |
| 1102 | for (int i = 0; i < argc; i++) { |
| 1103 | const StringPiece option(argv[i]); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 1104 | if (option.starts_with("--oat-file=")) { |
| 1105 | oat_filename = option.substr(strlen("--oat-file=")).data(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1106 | } else if (option.starts_with("--image=")) { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1107 | image_filename = option.substr(strlen("--image=")).data(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1108 | } else if (option.starts_with("--boot-image=")) { |
| 1109 | boot_image_filename = option.substr(strlen("--boot-image=")).data(); |
Logan Chien | 0cc6ab6 | 2012-03-20 22:57:52 +0800 | [diff] [blame^] | 1110 | } else if (option.starts_with("--extract-elf-to=")) { |
| 1111 | elf_filename_prefix = option.substr(strlen("--extract-elf-to=")).data(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1112 | } else if (option.starts_with("--host-prefix=")) { |
Brian Carlstrom | 13c492b | 2012-03-22 17:09:17 -0700 | [diff] [blame] | 1113 | host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data())); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1114 | } else if (option.starts_with("--output=")) { |
| 1115 | const char* filename = option.substr(strlen("--output=")).data(); |
| 1116 | out.reset(new std::ofstream(filename)); |
| 1117 | if (!out->good()) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1118 | fprintf(stderr, "Failed to open output filename %s\n", filename); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1119 | usage(); |
| 1120 | } |
| 1121 | os = out.get(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1122 | } else { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1123 | fprintf(stderr, "Unknown argument %s\n", option.data()); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1124 | usage(); |
| 1125 | } |
| 1126 | } |
| 1127 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1128 | if (image_filename == NULL && oat_filename == NULL) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 1129 | fprintf(stderr, "Either --image or --oat must be specified\n"); |
| 1130 | return EXIT_FAILURE; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1133 | if (image_filename != NULL && oat_filename != NULL) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 1134 | fprintf(stderr, "Either --image or --oat must be specified but not both\n"); |
| 1135 | return EXIT_FAILURE; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Brian Carlstrom | 13c492b | 2012-03-22 17:09:17 -0700 | [diff] [blame] | 1138 | if (host_prefix.get() == NULL) { |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 1139 | const char* android_product_out = getenv("ANDROID_PRODUCT_OUT"); |
| 1140 | if (android_product_out != NULL) { |
Brian Carlstrom | 13c492b | 2012-03-22 17:09:17 -0700 | [diff] [blame] | 1141 | host_prefix.reset(new std::string(android_product_out)); |
| 1142 | } else { |
| 1143 | host_prefix.reset(new std::string("")); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 1144 | } |
| 1145 | } |
| 1146 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1147 | if (oat_filename != NULL) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1148 | OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, NULL); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1149 | if (oat_file == NULL) { |
| 1150 | fprintf(stderr, "Failed to open oat file from %s\n", oat_filename); |
| 1151 | return EXIT_FAILURE; |
| 1152 | } |
Brian Carlstrom | 13c492b | 2012-03-22 17:09:17 -0700 | [diff] [blame] | 1153 | OatDumper oat_dumper(*host_prefix.get(), *oat_file); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1154 | oat_dumper.Dump(*os); |
Logan Chien | 0cc6ab6 | 2012-03-20 22:57:52 +0800 | [diff] [blame^] | 1155 | |
| 1156 | if (!elf_filename_prefix.empty()) { |
| 1157 | uint32_t elf_image_count = oat_file->GetOatHeader().GetElfImageCount(); |
| 1158 | for (uint32_t i = 0; i < elf_image_count; ++i) { |
| 1159 | const OatFile::OatElfImage* elf_image = oat_file->GetOatElfImage(i); |
| 1160 | |
| 1161 | std::string elf_filename( |
| 1162 | StringPrintf("%s-%u", elf_filename_prefix.c_str(), i)); |
| 1163 | |
| 1164 | UniquePtr<File> elf_file(OS::OpenFile(elf_filename.c_str(), true)); |
| 1165 | |
| 1166 | if (!elf_file->WriteFully(elf_image->begin(), elf_image->size())) { |
| 1167 | fprintf(stderr, "Failed to write ELF image to: %s\n", |
| 1168 | elf_filename.c_str()); |
| 1169 | } |
| 1170 | } |
| 1171 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1172 | return EXIT_SUCCESS; |
| 1173 | } |
| 1174 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1175 | Runtime::Options options; |
| 1176 | std::string image_option; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1177 | std::string oat_option; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1178 | std::string boot_image_option; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1179 | std::string boot_oat_option; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1180 | if (boot_image_filename != NULL) { |
| 1181 | boot_image_option += "-Ximage:"; |
| 1182 | boot_image_option += boot_image_filename; |
| 1183 | 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] | 1184 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1185 | if (image_filename != NULL) { |
| 1186 | image_option += "-Ximage:"; |
| 1187 | image_option += image_filename; |
| 1188 | options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL))); |
| 1189 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1190 | |
Brian Carlstrom | 13c492b | 2012-03-22 17:09:17 -0700 | [diff] [blame] | 1191 | if (!host_prefix->empty()) { |
| 1192 | options.push_back(std::make_pair("host-prefix", host_prefix->c_str())); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1193 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1194 | |
| 1195 | UniquePtr<Runtime> runtime(Runtime::Create(options, false)); |
| 1196 | if (runtime.get() == NULL) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1197 | fprintf(stderr, "Failed to create runtime\n"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1198 | return EXIT_FAILURE; |
| 1199 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1200 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1201 | Heap* heap = Runtime::Current()->GetHeap(); |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1202 | ImageSpace* image_space = heap->GetImageSpace(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1203 | CHECK(image_space != NULL); |
| 1204 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1205 | if (!image_header.IsValid()) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1206 | fprintf(stderr, "Invalid image header %s\n", image_filename); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1207 | return EXIT_FAILURE; |
| 1208 | } |
Brian Carlstrom | 13c492b | 2012-03-22 17:09:17 -0700 | [diff] [blame] | 1209 | 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] | 1210 | image_dumper.Dump(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1211 | return EXIT_SUCCESS; |
| 1212 | } |
| 1213 | |
| 1214 | } // namespace art |
| 1215 | |
| 1216 | int main(int argc, char** argv) { |
| 1217 | return art::oatdump(argc, argv); |
| 1218 | } |