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