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