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