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