Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 20 | #include <fstream> |
| 21 | #include <iostream> |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 22 | #include <string> |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 23 | #include <unordered_map> |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 26 | #include "base/stringpiece.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 27 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 28 | #include "class_linker.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "class_linker-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 30 | #include "dex_file-inl.h" |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 31 | #include "dex_instruction.h" |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 32 | #include "disassembler.h" |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 33 | #include "elf_builder.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 34 | #include "field_helper.h" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 35 | #include "gc_map.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 36 | #include "gc/space/image_space.h" |
| 37 | #include "gc/space/large_object_space.h" |
| 38 | #include "gc/space/space-inl.h" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 39 | #include "image.h" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 40 | #include "indenter.h" |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 41 | #include "mapping_table.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 42 | #include "mirror/art_field-inl.h" |
| 43 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 44 | #include "mirror/array-inl.h" |
| 45 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 46 | #include "mirror/object-inl.h" |
| 47 | #include "mirror/object_array-inl.h" |
Brian Carlstrom | c0a1b18 | 2014-03-04 23:19:06 -0800 | [diff] [blame] | 48 | #include "noop_compiler_callbacks.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 49 | #include "oat.h" |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 50 | #include "oat_file-inl.h" |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 51 | #include "os.h" |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 52 | #include "output_stream.h" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 53 | #include "runtime.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 54 | #include "safe_map.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 55 | #include "scoped_thread_state_change.h" |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 56 | #include "ScopedLocalRef.h" |
Mathieu Chartier | c22c59e | 2014-02-24 15:16:06 -0800 | [diff] [blame] | 57 | #include "thread_list.h" |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 58 | #include "verifier/dex_gc_map.h" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 59 | #include "verifier/method_verifier.h" |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 60 | #include "vmap_table.h" |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 61 | #include "well_known_classes.h" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 62 | |
| 63 | namespace art { |
| 64 | |
| 65 | static void usage() { |
| 66 | fprintf(stderr, |
| 67 | "Usage: oatdump [options] ...\n" |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 68 | " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n" |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 69 | " Example: adb shell oatdump --image=/system/framework/boot.art\n" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 70 | "\n"); |
| 71 | fprintf(stderr, |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 72 | " --oat-file=<file.oat>: specifies an input oat filename.\n" |
TDYa127 | a0a2a6c | 2012-10-16 22:47:38 -0700 | [diff] [blame] | 73 | " Example: --oat-file=/system/framework/boot.oat\n" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 74 | "\n"); |
| 75 | fprintf(stderr, |
| 76 | " --image=<file.art>: specifies an input image filename.\n" |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 77 | " Example: --image=/system/framework/boot.art\n" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 78 | "\n"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 79 | fprintf(stderr, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 80 | " --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] | 81 | " Example: --boot-image=/system/framework/boot.art\n" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 82 | "\n"); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 83 | fprintf(stderr, |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 84 | " --instruction-set=(arm|arm64|mips|x86|x86_64): for locating the image\n" |
| 85 | " file based on the image location set.\n" |
Brian Carlstrom | 0f5baa0 | 2014-05-22 11:54:18 -0700 | [diff] [blame] | 86 | " Example: --instruction-set=x86\n" |
| 87 | " Default: %s\n" |
| 88 | "\n", |
| 89 | GetInstructionSetString(kRuntimeISA)); |
| 90 | fprintf(stderr, |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 91 | " --output=<file> may be used to send the output to a file.\n" |
| 92 | " Example: --output=/tmp/oatdump.txt\n" |
| 93 | "\n"); |
Dave Allison | 404f59f | 2014-02-24 11:10:01 -0800 | [diff] [blame] | 94 | fprintf(stderr, |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 95 | " --dump:raw_mapping_table enables dumping of the mapping table.\n" |
| 96 | " Example: --dump:raw_mapping_table\n" |
| 97 | "\n"); |
| 98 | fprintf(stderr, |
| 99 | " --dump:raw_mapping_table enables dumping of the GC map.\n" |
| 100 | " Example: --dump:raw_gc_map\n" |
| 101 | "\n"); |
| 102 | fprintf(stderr, |
| 103 | " --no-dump:vmap may be used to disable vmap dumping.\n" |
| 104 | " Example: --no-dump:vmap\n" |
| 105 | "\n"); |
| 106 | fprintf(stderr, |
| 107 | " --no-disassemble may be used to disable disassembly.\n" |
| 108 | " Example: --no-disassemble\n" |
Dave Allison | 404f59f | 2014-02-24 11:10:01 -0800 | [diff] [blame] | 109 | "\n"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 112 | const char* image_roots_descriptions_[] = { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 113 | "kResolutionMethod", |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 114 | "kImtConflictMethod", |
| 115 | "kDefaultImt", |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 116 | "kCalleeSaveMethod", |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 117 | "kRefsOnlySaveMethod", |
| 118 | "kRefsAndArgsSaveMethod", |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 119 | "kDexCaches", |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 120 | "kClassRoots", |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 123 | class OatSymbolizer FINAL : public CodeOutput { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 124 | public: |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 125 | explicit OatSymbolizer(const OatFile* oat_file, const std::string& output_name) : |
| 126 | oat_file_(oat_file), builder_(nullptr), elf_output_(nullptr), |
| 127 | output_name_(output_name.empty() ? "symbolized.oat" : output_name) { |
| 128 | } |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 129 | |
| 130 | bool Init() { |
| 131 | Elf32_Word oat_data_size = oat_file_->GetOatHeader().GetExecutableOffset(); |
| 132 | |
| 133 | uint32_t diff = static_cast<uint32_t>(oat_file_->End() - oat_file_->Begin()); |
| 134 | uint32_t oat_exec_size = diff - oat_data_size; |
| 135 | |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 136 | elf_output_ = OS::CreateEmptyFile(output_name_.c_str()); |
| 137 | |
| 138 | builder_.reset(new ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn, |
| 139 | Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>( |
| 140 | this, |
| 141 | elf_output_, |
| 142 | oat_file_->GetOatHeader().GetInstructionSet(), |
| 143 | 0, |
| 144 | oat_data_size, |
| 145 | oat_data_size, |
| 146 | oat_exec_size, |
| 147 | true, |
| 148 | false)); |
| 149 | |
| 150 | if (!builder_->Init()) { |
| 151 | builder_.reset(nullptr); |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&, |
| 159 | uint32_t, |
| 160 | const OatFile::OatMethod&, |
| 161 | const DexFile&, |
| 162 | uint32_t, |
| 163 | const DexFile::CodeItem*, |
| 164 | uint32_t); |
| 165 | |
| 166 | bool Symbolize() { |
| 167 | if (builder_.get() == nullptr) { |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | Walk(&art::OatSymbolizer::RegisterForDedup); |
| 172 | |
| 173 | NormalizeState(); |
| 174 | |
| 175 | Walk(&art::OatSymbolizer::AddSymbol); |
| 176 | |
| 177 | bool result = builder_->Write(); |
| 178 | |
| 179 | elf_output_->Flush(); |
| 180 | elf_output_->Close(); |
| 181 | |
| 182 | return result; |
| 183 | } |
| 184 | |
| 185 | void Walk(Callback callback) { |
| 186 | std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles(); |
| 187 | for (size_t i = 0; i < oat_dex_files.size(); i++) { |
| 188 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i]; |
| 189 | CHECK(oat_dex_file != NULL); |
| 190 | WalkOatDexFile(oat_dex_file, callback); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) { |
| 195 | std::string error_msg; |
| 196 | std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg)); |
| 197 | if (dex_file.get() == nullptr) { |
| 198 | return; |
| 199 | } |
| 200 | for (size_t class_def_index = 0; |
| 201 | class_def_index < dex_file->NumClassDefs(); |
| 202 | class_def_index++) { |
| 203 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 204 | const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index); |
| 205 | OatClassType type = oat_class.GetType(); |
| 206 | switch (type) { |
| 207 | case kOatClassAllCompiled: |
| 208 | case kOatClassSomeCompiled: |
| 209 | WalkOatClass(oat_class, *dex_file.get(), class_def, callback); |
| 210 | break; |
| 211 | |
| 212 | case kOatClassNoneCompiled: |
| 213 | case kOatClassMax: |
| 214 | // Ignore. |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file, |
| 221 | const DexFile::ClassDef& class_def, Callback callback) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 222 | const uint8_t* class_data = dex_file.GetClassData(class_def); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 223 | if (class_data == nullptr) { // empty class such as a marker interface? |
| 224 | return; |
| 225 | } |
| 226 | // Note: even if this is an interface or a native class, we still have to walk it, as there |
| 227 | // might be a static initializer. |
| 228 | ClassDataItemIterator it(dex_file, class_data); |
| 229 | SkipAllFields(&it); |
| 230 | uint32_t class_method_idx = 0; |
| 231 | while (it.HasNextDirectMethod()) { |
| 232 | const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx); |
| 233 | WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(), |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 234 | it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 235 | class_method_idx++; |
| 236 | it.Next(); |
| 237 | } |
| 238 | while (it.HasNextVirtualMethod()) { |
| 239 | const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx); |
| 240 | WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(), |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 241 | it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 242 | class_method_idx++; |
| 243 | it.Next(); |
| 244 | } |
| 245 | DCHECK(!it.HasNext()); |
| 246 | } |
| 247 | |
| 248 | void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index, |
| 249 | const OatFile::OatMethod& oat_method, const DexFile& dex_file, |
| 250 | uint32_t dex_method_idx, const DexFile::CodeItem* code_item, |
| 251 | uint32_t method_access_flags, Callback callback) { |
| 252 | if ((method_access_flags & kAccAbstract) != 0) { |
| 253 | // Abstract method, no code. |
| 254 | return; |
| 255 | } |
| 256 | if (oat_method.GetCodeOffset() == 0) { |
| 257 | // No code. |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item, |
| 262 | method_access_flags); |
| 263 | } |
| 264 | |
| 265 | void RegisterForDedup(const DexFile::ClassDef& class_def, uint32_t class_method_index, |
| 266 | const OatFile::OatMethod& oat_method, const DexFile& dex_file, |
| 267 | uint32_t dex_method_idx, const DexFile::CodeItem* code_item, |
| 268 | uint32_t method_access_flags) { |
| 269 | state_[oat_method.GetCodeOffset()]++; |
| 270 | } |
| 271 | |
| 272 | void NormalizeState() { |
| 273 | for (auto& x : state_) { |
| 274 | if (x.second == 1) { |
| 275 | state_[x.first] = 0; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | enum class DedupState { // private |
| 281 | kNotDeduplicated, |
| 282 | kDeduplicatedFirst, |
| 283 | kDeduplicatedOther |
| 284 | }; |
| 285 | DedupState IsDuplicated(uint32_t offset) { |
| 286 | if (state_[offset] == 0) { |
| 287 | return DedupState::kNotDeduplicated; |
| 288 | } |
| 289 | if (state_[offset] == 1) { |
| 290 | return DedupState::kDeduplicatedOther; |
| 291 | } |
| 292 | state_[offset] = 1; |
| 293 | return DedupState::kDeduplicatedFirst; |
| 294 | } |
| 295 | |
| 296 | void AddSymbol(const DexFile::ClassDef& class_def, uint32_t class_method_index, |
| 297 | const OatFile::OatMethod& oat_method, const DexFile& dex_file, |
| 298 | uint32_t dex_method_idx, const DexFile::CodeItem* code_item, |
| 299 | uint32_t method_access_flags) { |
| 300 | DedupState dedup = IsDuplicated(oat_method.GetCodeOffset()); |
| 301 | if (dedup != DedupState::kDeduplicatedOther) { |
| 302 | std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true); |
| 303 | |
| 304 | if (dedup == DedupState::kDeduplicatedFirst) { |
| 305 | pretty_name = "[Dedup]" + pretty_name; |
| 306 | } |
| 307 | |
| 308 | ElfSymtabBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 309 | Elf32_Sym, Elf32_Shdr>* symtab = builder_->GetSymtabBuilder(); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 310 | |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 311 | symtab->AddSymbol(pretty_name, &builder_->GetTextBuilder(), |
| 312 | oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(), |
| 313 | true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 317 | // Set oat data offset. Required by ElfBuilder/CodeOutput. |
| 318 | void SetCodeOffset(size_t offset) { |
| 319 | // Nothing to do. |
| 320 | } |
| 321 | |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 322 | // Write oat code. Required by ElfBuilder/CodeOutput. |
| 323 | bool Write(OutputStream* out) { |
| 324 | return out->WriteFully(oat_file_->Begin(), oat_file_->End() - oat_file_->Begin()); |
| 325 | } |
| 326 | |
| 327 | private: |
| 328 | static void SkipAllFields(ClassDataItemIterator* it) { |
| 329 | while (it->HasNextStaticField()) { |
| 330 | it->Next(); |
| 331 | } |
| 332 | while (it->HasNextInstanceField()) { |
| 333 | it->Next(); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | const OatFile* oat_file_; |
| 338 | std::unique_ptr<ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn, |
| 339 | Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr> > builder_; |
| 340 | File* elf_output_; |
| 341 | std::unordered_map<uint32_t, uint32_t> state_; |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 342 | const std::string output_name_; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 343 | }; |
| 344 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 345 | class OatDumperOptions { |
| 346 | public: |
| 347 | OatDumperOptions(bool dump_raw_mapping_table, |
| 348 | bool dump_raw_gc_map, |
| 349 | bool dump_vmap, |
| 350 | bool disassemble_code, |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 351 | bool absolute_addresses, |
| 352 | Handle<mirror::ClassLoader>* class_loader) |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 353 | : dump_raw_mapping_table_(dump_raw_mapping_table), |
| 354 | dump_raw_gc_map_(dump_raw_gc_map), |
| 355 | dump_vmap_(dump_vmap), |
| 356 | disassemble_code_(disassemble_code), |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 357 | absolute_addresses_(absolute_addresses), |
| 358 | class_loader_(class_loader) {} |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 359 | |
| 360 | const bool dump_raw_mapping_table_; |
| 361 | const bool dump_raw_gc_map_; |
| 362 | const bool dump_vmap_; |
| 363 | const bool disassemble_code_; |
| 364 | const bool absolute_addresses_; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 365 | Handle<mirror::ClassLoader>* class_loader_; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 366 | }; |
| 367 | |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 368 | class OatDumper { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 369 | public: |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 370 | explicit OatDumper(const OatFile& oat_file, OatDumperOptions* options) |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 371 | : oat_file_(oat_file), |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 372 | oat_dex_files_(oat_file.GetOatDexFiles()), |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 373 | options_(options), |
| 374 | disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet(), |
| 375 | new DisassemblerOptions(options_->absolute_addresses_, |
| 376 | oat_file.Begin()))) { |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 377 | CHECK(options_->class_loader_ != nullptr); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 378 | AddAllOffsets(); |
| 379 | } |
| 380 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 381 | ~OatDumper() { |
| 382 | delete options_; |
| 383 | delete disassembler_; |
| 384 | } |
| 385 | |
| 386 | bool Dump(std::ostream& os) { |
| 387 | bool success = true; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 388 | const OatHeader& oat_header = oat_file_.GetOatHeader(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 389 | |
| 390 | os << "MAGIC:\n"; |
| 391 | os << oat_header.GetMagic() << "\n\n"; |
| 392 | |
| 393 | os << "CHECKSUM:\n"; |
Elliott Hughes | ed2adb6 | 2012-02-29 14:41:01 -0800 | [diff] [blame] | 394 | os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 395 | |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 396 | os << "INSTRUCTION SET:\n"; |
| 397 | os << oat_header.GetInstructionSet() << "\n\n"; |
| 398 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame^] | 399 | { |
| 400 | std::unique_ptr<const InstructionSetFeatures> features( |
| 401 | InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(), |
| 402 | oat_header.GetInstructionSetFeaturesBitmap())); |
| 403 | os << "INSTRUCTION SET FEATURES:\n"; |
| 404 | os << features->GetFeatureString() << "\n\n"; |
| 405 | } |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 406 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 407 | os << "DEX FILE COUNT:\n"; |
| 408 | os << oat_header.GetDexFileCount() << "\n\n"; |
| 409 | |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 410 | #define DUMP_OAT_HEADER_OFFSET(label, offset) \ |
| 411 | os << label " OFFSET:\n"; \ |
| 412 | os << StringPrintf("0x%08x", oat_header.offset()); \ |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 413 | if (oat_header.offset() != 0 && options_->absolute_addresses_) { \ |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 414 | os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \ |
| 415 | } \ |
| 416 | os << StringPrintf("\n\n"); |
| 417 | |
| 418 | DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset); |
| 419 | DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE", |
| 420 | GetInterpreterToInterpreterBridgeOffset); |
| 421 | DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE", |
| 422 | GetInterpreterToCompiledCodeBridgeOffset); |
| 423 | DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP", |
| 424 | GetJniDlsymLookupOffset); |
| 425 | DUMP_OAT_HEADER_OFFSET("PORTABLE IMT CONFLICT TRAMPOLINE", |
| 426 | GetPortableImtConflictTrampolineOffset); |
| 427 | DUMP_OAT_HEADER_OFFSET("PORTABLE RESOLUTION TRAMPOLINE", |
| 428 | GetPortableResolutionTrampolineOffset); |
| 429 | DUMP_OAT_HEADER_OFFSET("PORTABLE TO INTERPRETER BRIDGE", |
| 430 | GetPortableToInterpreterBridgeOffset); |
| 431 | DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE", |
| 432 | GetQuickGenericJniTrampolineOffset); |
| 433 | DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE", |
| 434 | GetQuickImtConflictTrampolineOffset); |
| 435 | DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE", |
| 436 | GetQuickResolutionTrampolineOffset); |
| 437 | DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE", |
| 438 | GetQuickToInterpreterBridgeOffset); |
| 439 | #undef DUMP_OAT_HEADER_OFFSET |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 440 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 441 | os << "IMAGE PATCH DELTA:\n"; |
| 442 | os << StringPrintf("%d (0x%08x)\n\n", |
| 443 | oat_header.GetImagePatchDelta(), |
| 444 | oat_header.GetImagePatchDelta()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 445 | |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 446 | os << "IMAGE FILE LOCATION OAT CHECKSUM:\n"; |
| 447 | os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum()); |
| 448 | |
| 449 | os << "IMAGE FILE LOCATION OAT BEGIN:\n"; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 450 | os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin()); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 451 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 452 | // Print the key-value store. |
| 453 | { |
| 454 | os << "KEY VALUE STORE:\n"; |
| 455 | size_t index = 0; |
| 456 | const char* key; |
| 457 | const char* value; |
| 458 | while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) { |
| 459 | os << key << " = " << value << "\n"; |
| 460 | index++; |
| 461 | } |
| 462 | os << "\n"; |
| 463 | } |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 464 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 465 | if (options_->absolute_addresses_) { |
| 466 | os << "BEGIN:\n"; |
| 467 | os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 468 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 469 | os << "END:\n"; |
| 470 | os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n"; |
| 471 | } |
| 472 | |
| 473 | os << "SIZE:\n"; |
| 474 | os << oat_file_.Size() << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 475 | |
| 476 | os << std::flush; |
| 477 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 478 | for (size_t i = 0; i < oat_dex_files_.size(); i++) { |
| 479 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 480 | CHECK(oat_dex_file != nullptr); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 481 | if (!DumpOatDexFile(os, *oat_dex_file)) { |
| 482 | success = false; |
| 483 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 484 | } |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 485 | os << std::flush; |
| 486 | return success; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 489 | size_t ComputeSize(const void* oat_data) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 490 | if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() || |
| 491 | reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 492 | return 0; // Address not in oat file |
| 493 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 494 | uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) - |
| 495 | reinterpret_cast<uintptr_t>(oat_file_.Begin()); |
| 496 | auto it = offsets_.upper_bound(begin_offset); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 497 | CHECK(it != offsets_.end()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 498 | uintptr_t end_offset = *it; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 499 | return end_offset - begin_offset; |
| 500 | } |
| 501 | |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 502 | InstructionSet GetInstructionSet() { |
| 503 | return oat_file_.GetOatHeader().GetInstructionSet(); |
| 504 | } |
| 505 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 506 | const void* GetQuickOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 507 | for (size_t i = 0; i < oat_dex_files_.size(); i++) { |
| 508 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 509 | CHECK(oat_dex_file != nullptr); |
| 510 | std::string error_msg; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 511 | std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 512 | if (dex_file.get() == nullptr) { |
| 513 | LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() |
| 514 | << "': " << error_msg; |
| 515 | } else { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 516 | const DexFile::ClassDef* class_def = |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 517 | dex_file->FindClassDef(m->GetDeclaringClassDescriptor()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 518 | if (class_def != nullptr) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 519 | uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 520 | const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 521 | size_t method_index = m->GetMethodIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 522 | return oat_class.GetOatMethod(method_index).GetQuickCode(); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 526 | return nullptr; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 527 | } |
| 528 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 529 | private: |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 530 | void AddAllOffsets() { |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 531 | // We don't know the length of the code for each method, but we need to know where to stop |
| 532 | // when disassembling. What we do know is that a region of code will be followed by some other |
| 533 | // region, so if we keep a sorted sequence of the start of each region, we can infer the length |
| 534 | // 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] | 535 | for (size_t i = 0; i < oat_dex_files_.size(); i++) { |
| 536 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 537 | CHECK(oat_dex_file != nullptr); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 538 | std::string error_msg; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 539 | std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 540 | if (dex_file.get() == nullptr) { |
| 541 | LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() |
| 542 | << "': " << error_msg; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 543 | continue; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 544 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 545 | offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader())); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 546 | for (size_t class_def_index = 0; |
| 547 | class_def_index < dex_file->NumClassDefs(); |
| 548 | class_def_index++) { |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 549 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 550 | const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 551 | const uint8_t* class_data = dex_file->GetClassData(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 552 | if (class_data != nullptr) { |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 553 | ClassDataItemIterator it(*dex_file, class_data); |
| 554 | SkipAllFields(it); |
| 555 | uint32_t class_method_index = 0; |
| 556 | while (it.HasNextDirectMethod()) { |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 557 | AddOffsets(oat_class.GetOatMethod(class_method_index++)); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 558 | it.Next(); |
| 559 | } |
| 560 | while (it.HasNextVirtualMethod()) { |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 561 | AddOffsets(oat_class.GetOatMethod(class_method_index++)); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 562 | it.Next(); |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | // If the last thing in the file is code for a method, there won't be an offset for the "next" |
| 569 | // thing. Instead of having a special case in the upper_bound code, let's just add an entry |
| 570 | // for the end of the file. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 571 | offsets_.insert(oat_file_.Size()); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 572 | } |
| 573 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 574 | static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) { |
| 575 | return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific. |
| 576 | } |
| 577 | |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 578 | void AddOffsets(const OatFile::OatMethod& oat_method) { |
Brian Carlstrom | 95ba0dc | 2012-03-05 22:06:14 -0800 | [diff] [blame] | 579 | uint32_t code_offset = oat_method.GetCodeOffset(); |
| 580 | if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) { |
| 581 | code_offset &= ~0x1; |
| 582 | } |
| 583 | offsets_.insert(code_offset); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 584 | offsets_.insert(oat_method.GetMappingTableOffset()); |
| 585 | offsets_.insert(oat_method.GetVmapTableOffset()); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 586 | offsets_.insert(oat_method.GetNativeGcMapOffset()); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 587 | } |
| 588 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 589 | bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) { |
| 590 | bool success = true; |
| 591 | os << "OatDexFile:\n"; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 592 | os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str()); |
Elliott Hughes | ed2adb6 | 2012-02-29 14:41:01 -0800 | [diff] [blame] | 593 | os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 594 | |
| 595 | // Create the verifier early. |
| 596 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 597 | std::string error_msg; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 598 | std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 599 | if (dex_file.get() == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 600 | os << "NOT FOUND: " << error_msg << "\n\n"; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 601 | os << std::flush; |
| 602 | return false; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 603 | } |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 604 | for (size_t class_def_index = 0; |
| 605 | class_def_index < dex_file->NumClassDefs(); |
| 606 | class_def_index++) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 607 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 608 | const char* descriptor = dex_file->GetClassDescriptor(class_def); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 609 | uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 610 | const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 611 | os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)", |
| 612 | class_def_index, descriptor, oat_class_offset, class_def.class_idx_) |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 613 | << " (" << oat_class.GetStatus() << ")" |
| 614 | << " (" << oat_class.GetType() << ")\n"; |
| 615 | // TODO: include bitmap here if type is kOatClassSomeCompiled? |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 616 | Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 617 | std::ostream indented_os(&indent_filter); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 618 | if (!DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def)) { |
| 619 | success = false; |
| 620 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | os << std::flush; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 624 | return success; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 627 | static void SkipAllFields(ClassDataItemIterator& it) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 628 | while (it.HasNextStaticField()) { |
| 629 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 630 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 631 | while (it.HasNextInstanceField()) { |
| 632 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 633 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 634 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 635 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 636 | bool DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file, |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 637 | const DexFile::ClassDef& class_def) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 638 | bool success = true; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 639 | const uint8_t* class_data = dex_file.GetClassData(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 640 | if (class_data == nullptr) { // empty class such as a marker interface? |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 641 | os << std::flush; |
| 642 | return success; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 643 | } |
| 644 | ClassDataItemIterator it(dex_file, class_data); |
| 645 | SkipAllFields(it); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 646 | uint32_t class_method_index = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 647 | while (it.HasNextDirectMethod()) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 648 | if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file, |
| 649 | it.GetMemberIndex(), it.GetMethodCodeItem(), |
| 650 | it.GetRawMemberAccessFlags())) { |
| 651 | success = false; |
| 652 | } |
| 653 | class_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 654 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 655 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 656 | while (it.HasNextVirtualMethod()) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 657 | if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file, |
| 658 | it.GetMemberIndex(), it.GetMethodCodeItem(), |
| 659 | it.GetRawMemberAccessFlags())) { |
| 660 | success = false; |
| 661 | } |
| 662 | class_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 663 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 664 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 665 | DCHECK(!it.HasNext()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 666 | os << std::flush; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 667 | return success; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 668 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 669 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 670 | static constexpr uint32_t kPrologueBytes = 16; |
| 671 | |
| 672 | // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes. |
| 673 | static constexpr uint32_t kMaxCodeSize = 100 * 1000; |
| 674 | |
| 675 | bool DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 676 | uint32_t class_method_index, |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 677 | const OatFile::OatClass& oat_class, const DexFile& dex_file, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 678 | uint32_t dex_method_idx, const DexFile::CodeItem* code_item, |
| 679 | uint32_t method_access_flags) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 680 | bool success = true; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 681 | os << StringPrintf("%d: %s (dex_method_idx=%d)\n", |
Ian Rogers | db7bdc1 | 2012-04-09 21:27:15 -0700 | [diff] [blame] | 682 | class_method_index, PrettyMethod(dex_method_idx, dex_file, true).c_str(), |
| 683 | dex_method_idx); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 684 | Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
Andreas Gampe | a8b41aa | 2014-06-04 22:42:28 -0700 | [diff] [blame] | 685 | std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter)); |
| 686 | Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count); |
| 687 | std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 688 | { |
Andreas Gampe | a8b41aa | 2014-06-04 22:42:28 -0700 | [diff] [blame] | 689 | *indent1_os << "DEX CODE:\n"; |
| 690 | DumpDexCode(*indent2_os, dex_file, code_item); |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 691 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 692 | |
| 693 | std::unique_ptr<verifier::MethodVerifier> verifier; |
| 694 | if (Runtime::Current() != nullptr) { |
Andreas Gampe | a8b41aa | 2014-06-04 22:42:28 -0700 | [diff] [blame] | 695 | *indent1_os << "VERIFIER TYPE ANALYSIS:\n"; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 696 | verifier.reset(DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item, |
| 697 | method_access_flags)); |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 698 | } |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 699 | |
| 700 | uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index); |
| 701 | const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index); |
| 702 | const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 703 | { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 704 | *indent1_os << "OatMethodOffsets "; |
| 705 | if (options_->absolute_addresses_) { |
| 706 | *indent1_os << StringPrintf("%p ", oat_method_offsets); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 707 | } |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 708 | *indent1_os << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset); |
| 709 | if (oat_method_offsets_offset > oat_file_.Size()) { |
| 710 | *indent1_os << StringPrintf( |
| 711 | "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n", |
| 712 | oat_method_offsets_offset, oat_file_.Size()); |
| 713 | // If we can't read OatMethodOffsets, the rest of the data is dangerous to read. |
| 714 | os << std::flush; |
| 715 | return false; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 716 | } |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 717 | |
| 718 | uint32_t code_offset = oat_method.GetCodeOffset(); |
| 719 | *indent2_os << StringPrintf("code_offset: 0x%08x ", code_offset); |
| 720 | uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset()); |
| 721 | if (aligned_code_begin > oat_file_.Size()) { |
| 722 | *indent2_os << StringPrintf("WARNING: " |
| 723 | "code offset 0x%08x is past end of file 0x%08zx.\n", |
| 724 | aligned_code_begin, oat_file_.Size()); |
| 725 | success = false; |
| 726 | } |
| 727 | *indent2_os << "\n"; |
| 728 | |
| 729 | *indent2_os << "gc_map: "; |
| 730 | if (options_->absolute_addresses_) { |
| 731 | *indent2_os << StringPrintf("%p ", oat_method.GetNativeGcMap()); |
| 732 | } |
| 733 | uint32_t gc_map_offset = oat_method.GetNativeGcMapOffset(); |
| 734 | *indent2_os << StringPrintf("(offset=0x%08x)\n", gc_map_offset); |
| 735 | if (gc_map_offset > oat_file_.Size()) { |
| 736 | *indent2_os << StringPrintf("WARNING: " |
| 737 | "gc map table offset 0x%08x is past end of file 0x%08zx.\n", |
| 738 | gc_map_offset, oat_file_.Size()); |
| 739 | success = false; |
| 740 | } else if (options_->dump_raw_gc_map_) { |
Andreas Gampe | a8b41aa | 2014-06-04 22:42:28 -0700 | [diff] [blame] | 741 | Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 742 | std::ostream indent3_os(&indent3_filter); |
| 743 | DumpGcMap(indent3_os, oat_method, code_item); |
| 744 | } |
| 745 | } |
| 746 | { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 747 | *indent1_os << "OatQuickMethodHeader "; |
| 748 | uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset(); |
| 749 | const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 750 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 751 | if (options_->absolute_addresses_) { |
| 752 | *indent1_os << StringPrintf("%p ", method_header); |
| 753 | } |
| 754 | *indent1_os << StringPrintf("(offset=0x%08x)\n", method_header_offset); |
| 755 | if (method_header_offset > oat_file_.Size()) { |
| 756 | *indent1_os << StringPrintf( |
| 757 | "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n", |
| 758 | method_header_offset, oat_file_.Size()); |
| 759 | // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read. |
| 760 | os << std::flush; |
| 761 | return false; |
| 762 | } |
| 763 | |
| 764 | *indent2_os << "mapping_table: "; |
| 765 | if (options_->absolute_addresses_) { |
| 766 | *indent2_os << StringPrintf("%p ", oat_method.GetMappingTable()); |
| 767 | } |
| 768 | uint32_t mapping_table_offset = oat_method.GetMappingTableOffset(); |
| 769 | *indent2_os << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset()); |
| 770 | if (mapping_table_offset > oat_file_.Size()) { |
| 771 | *indent2_os << StringPrintf("WARNING: " |
| 772 | "mapping table offset 0x%08x is past end of file 0x%08zx. " |
| 773 | "mapping table offset was loaded from offset 0x%08x.\n", |
| 774 | mapping_table_offset, oat_file_.Size(), |
| 775 | oat_method.GetMappingTableOffsetOffset()); |
| 776 | success = false; |
| 777 | } else if (options_->dump_raw_mapping_table_) { |
| 778 | Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count); |
| 779 | std::ostream indent3_os(&indent3_filter); |
| 780 | DumpMappingTable(indent3_os, oat_method); |
| 781 | } |
| 782 | |
| 783 | *indent2_os << "vmap_table: "; |
| 784 | if (options_->absolute_addresses_) { |
| 785 | *indent2_os << StringPrintf("%p ", oat_method.GetVmapTable()); |
| 786 | } |
| 787 | uint32_t vmap_table_offset = oat_method.GetVmapTableOffset(); |
| 788 | *indent2_os << StringPrintf("(offset=0x%08x)\n", vmap_table_offset); |
| 789 | if (vmap_table_offset > oat_file_.Size()) { |
| 790 | *indent2_os << StringPrintf("WARNING: " |
| 791 | "vmap table offset 0x%08x is past end of file 0x%08zx. " |
| 792 | "vmap table offset was loaded from offset 0x%08x.\n", |
| 793 | vmap_table_offset, oat_file_.Size(), |
| 794 | oat_method.GetVmapTableOffsetOffset()); |
| 795 | success = false; |
| 796 | } else if (options_->dump_vmap_) { |
Nicolas Geoffray | 20d3eae | 2014-09-17 11:27:23 +0100 | [diff] [blame] | 797 | DumpVmap(*indent2_os, oat_method); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 798 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 799 | } |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 800 | { |
| 801 | *indent1_os << "QuickMethodFrameInfo\n"; |
| 802 | |
| 803 | *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes()); |
| 804 | *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask()); |
| 805 | DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false); |
| 806 | *indent2_os << "\n"; |
| 807 | *indent2_os << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask()); |
| 808 | DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true); |
| 809 | *indent2_os << "\n"; |
| 810 | } |
| 811 | { |
| 812 | // Based on spill masks from QuickMethodFrameInfo so placed |
| 813 | // after it is dumped, but useful for understanding quick |
| 814 | // code, so dumped here. |
| 815 | DumpVregLocations(*indent2_os, oat_method, code_item); |
| 816 | } |
| 817 | { |
| 818 | *indent1_os << "CODE: "; |
| 819 | uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset(); |
| 820 | if (code_size_offset > oat_file_.Size()) { |
| 821 | *indent2_os << StringPrintf("WARNING: " |
| 822 | "code size offset 0x%08x is past end of file 0x%08zx.", |
| 823 | code_size_offset, oat_file_.Size()); |
| 824 | success = false; |
| 825 | } else { |
| 826 | const void* code = oat_method.GetQuickCode(); |
| 827 | uint32_t code_size = oat_method.GetQuickCodeSize(); |
| 828 | if (code == nullptr) { |
| 829 | code = oat_method.GetPortableCode(); |
| 830 | code_size = oat_method.GetPortableCodeSize(); |
| 831 | code_size_offset = 0; |
| 832 | } |
| 833 | uint32_t code_offset = oat_method.GetCodeOffset(); |
| 834 | uint32_t aligned_code_begin = AlignCodeOffset(code_offset); |
| 835 | uint64_t aligned_code_end = aligned_code_begin + code_size; |
| 836 | |
| 837 | if (options_->absolute_addresses_) { |
| 838 | *indent1_os << StringPrintf("%p ", code); |
| 839 | } |
| 840 | *indent1_os << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n", |
| 841 | code_offset, |
| 842 | code_size_offset, |
| 843 | code_size, |
| 844 | code != nullptr ? "..." : ""); |
| 845 | |
| 846 | if (aligned_code_begin > oat_file_.Size()) { |
| 847 | *indent2_os << StringPrintf("WARNING: " |
| 848 | "start of code at 0x%08x is past end of file 0x%08zx.", |
| 849 | aligned_code_begin, oat_file_.Size()); |
| 850 | success = false; |
| 851 | } else if (aligned_code_end > oat_file_.Size()) { |
| 852 | *indent2_os << StringPrintf("WARNING: " |
| 853 | "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. " |
| 854 | "code size is 0x%08x loaded from offset 0x%08x.\n", |
| 855 | aligned_code_end, oat_file_.Size(), |
| 856 | code_size, code_size_offset); |
| 857 | success = false; |
| 858 | if (options_->disassemble_code_) { |
| 859 | if (code_size_offset + kPrologueBytes <= oat_file_.Size()) { |
| 860 | DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes); |
| 861 | } |
| 862 | } |
| 863 | } else if (code_size > kMaxCodeSize) { |
| 864 | *indent2_os << StringPrintf("WARNING: " |
| 865 | "code size %d is bigger than max expected threshold of %d. " |
| 866 | "code size is 0x%08x loaded from offset 0x%08x.\n", |
| 867 | code_size, kMaxCodeSize, |
| 868 | code_size, code_size_offset); |
| 869 | success = false; |
| 870 | if (options_->disassemble_code_) { |
| 871 | if (code_size_offset + kPrologueBytes <= oat_file_.Size()) { |
| 872 | DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes); |
| 873 | } |
| 874 | } |
| 875 | } else if (options_->disassemble_code_) { |
| 876 | DumpCode(*indent2_os, verifier.get(), oat_method, code_item, !success, 0); |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | os << std::flush; |
| 881 | return success; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 882 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 883 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 884 | void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) { |
| 885 | if (spill_mask == 0) { |
| 886 | return; |
| 887 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 888 | os << "("; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 889 | for (size_t i = 0; i < 32; i++) { |
| 890 | if ((spill_mask & (1 << i)) != 0) { |
| 891 | if (is_float) { |
| 892 | os << "fr" << i; |
| 893 | } else { |
| 894 | os << "r" << i; |
| 895 | } |
| 896 | spill_mask ^= 1 << i; // clear bit |
| 897 | if (spill_mask != 0) { |
| 898 | os << ", "; |
| 899 | } else { |
| 900 | break; |
| 901 | } |
| 902 | } |
| 903 | } |
| 904 | os << ")"; |
| 905 | } |
| 906 | |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 907 | void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) { |
Nicolas Geoffray | 20d3eae | 2014-09-17 11:27:23 +0100 | [diff] [blame] | 908 | // If the native GC map is null, then this method has been compiled with the |
| 909 | // optimizing compiler. The optimizing compiler currently outputs its stack map |
| 910 | // in the vmap table, and the code below does not work with such a stack map. |
| 911 | if (oat_method.GetNativeGcMap() == nullptr) { |
| 912 | return; |
| 913 | } |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 914 | const uint8_t* raw_table = oat_method.GetVmapTable(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 915 | if (raw_table != nullptr) { |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 916 | const VmapTable vmap_table(raw_table); |
| 917 | bool first = true; |
| 918 | bool processing_fp = false; |
| 919 | uint32_t spill_mask = oat_method.GetCoreSpillMask(); |
| 920 | for (size_t i = 0; i < vmap_table.Size(); i++) { |
| 921 | uint16_t dex_reg = vmap_table[i]; |
| 922 | uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i, |
| 923 | processing_fp ? kFloatVReg : kIntVReg); |
| 924 | os << (first ? "v" : ", v") << dex_reg; |
| 925 | if (!processing_fp) { |
| 926 | os << "/r" << cpu_reg; |
| 927 | } else { |
| 928 | os << "/fr" << cpu_reg; |
| 929 | } |
| 930 | first = false; |
| 931 | if (!processing_fp && dex_reg == 0xFFFF) { |
| 932 | processing_fp = true; |
| 933 | spill_mask = oat_method.GetFpSpillMask(); |
| 934 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 935 | } |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 936 | os << "\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 937 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 938 | } |
| 939 | |
Razvan A Lupusoru | faf9f0d | 2014-08-29 17:56:46 -0700 | [diff] [blame] | 940 | void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method, |
| 941 | const DexFile::CodeItem* code_item) { |
| 942 | if (code_item != nullptr) { |
| 943 | size_t num_locals_ins = code_item->registers_size_; |
| 944 | size_t num_ins = code_item->ins_size_; |
| 945 | size_t num_locals = num_locals_ins - num_ins; |
| 946 | size_t num_outs = code_item->outs_size_; |
| 947 | |
| 948 | os << "vr_stack_locations:"; |
| 949 | for (size_t reg = 0; reg <= num_locals_ins; reg++) { |
| 950 | // For readability, delimit the different kinds of VRs. |
| 951 | if (reg == num_locals_ins) { |
| 952 | os << "\n\tmethod*:"; |
| 953 | } else if (reg == num_locals && num_ins > 0) { |
| 954 | os << "\n\tins:"; |
| 955 | } else if (reg == 0 && num_locals > 0) { |
| 956 | os << "\n\tlocals:"; |
| 957 | } |
| 958 | |
| 959 | uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(), |
| 960 | oat_method.GetFpSpillMask(), |
| 961 | oat_method.GetFrameSizeInBytes(), reg, |
| 962 | GetInstructionSet()); |
| 963 | os << " v" << reg << "[sp + #" << offset << "]"; |
| 964 | } |
| 965 | |
| 966 | for (size_t out_reg = 0; out_reg < num_outs; out_reg++) { |
| 967 | if (out_reg == 0) { |
| 968 | os << "\n\touts:"; |
| 969 | } |
| 970 | |
| 971 | uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet()); |
| 972 | os << " v" << out_reg << "[sp + #" << offset << "]"; |
| 973 | } |
| 974 | |
| 975 | os << "\n"; |
| 976 | } |
| 977 | } |
| 978 | |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 979 | void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 980 | const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) { |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 981 | const uint8_t* raw_table = oat_method.GetVmapTable(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 982 | if (raw_table != nullptr) { |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 983 | const VmapTable vmap_table(raw_table); |
| 984 | uint32_t vmap_offset; |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 985 | if (vmap_table.IsInContext(reg, kind, &vmap_offset)) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 986 | bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); |
| 987 | uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask() |
| 988 | : oat_method.GetCoreSpillMask(); |
| 989 | os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind); |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 990 | } else { |
| 991 | uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(), |
| 992 | oat_method.GetFpSpillMask(), |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 993 | oat_method.GetFrameSizeInBytes(), reg, |
| 994 | GetInstructionSet()); |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 995 | os << "[sp + #" << offset << "]"; |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1000 | void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method, |
| 1001 | const DexFile::CodeItem* code_item, |
| 1002 | size_t num_regs, const uint8_t* reg_bitmap) { |
| 1003 | bool first = true; |
| 1004 | for (size_t reg = 0; reg < num_regs; reg++) { |
| 1005 | if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) { |
| 1006 | if (first) { |
| 1007 | os << " v" << reg << " ("; |
| 1008 | DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg); |
| 1009 | os << ")"; |
| 1010 | first = false; |
| 1011 | } else { |
| 1012 | os << ", v" << reg << " ("; |
| 1013 | DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg); |
| 1014 | os << ")"; |
| 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | if (first) { |
| 1019 | os << "No registers in GC map\n"; |
| 1020 | } else { |
| 1021 | os << "\n"; |
| 1022 | } |
| 1023 | } |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1024 | void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method, |
| 1025 | const DexFile::CodeItem* code_item) { |
| 1026 | const uint8_t* gc_map_raw = oat_method.GetNativeGcMap(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1027 | if (gc_map_raw == nullptr) { |
| 1028 | return; // No GC map. |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1029 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1030 | const void* quick_code = oat_method.GetQuickCode(); |
| 1031 | if (quick_code != nullptr) { |
| 1032 | NativePcOffsetToReferenceMap map(gc_map_raw); |
| 1033 | for (size_t entry = 0; entry < map.NumEntries(); entry++) { |
| 1034 | const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) + |
| 1035 | map.GetNativePcOffset(entry); |
| 1036 | os << StringPrintf("%p", native_pc); |
| 1037 | DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry)); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1038 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1039 | } else { |
| 1040 | const void* portable_code = oat_method.GetPortableCode(); |
| 1041 | CHECK(portable_code != nullptr); |
| 1042 | verifier::DexPcToReferenceMap map(gc_map_raw); |
| 1043 | for (size_t entry = 0; entry < map.NumEntries(); entry++) { |
| 1044 | uint32_t dex_pc = map.GetDexPc(entry); |
| 1045 | os << StringPrintf("0x%08x", dex_pc); |
| 1046 | DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry)); |
| 1047 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1052 | const void* quick_code = oat_method.GetQuickCode(); |
| 1053 | if (quick_code == nullptr) { |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 1054 | return; |
| 1055 | } |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1056 | MappingTable table(oat_method.GetMappingTable()); |
| 1057 | if (table.TotalSize() != 0) { |
| 1058 | Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 1059 | std::ostream indent_os(&indent_filter); |
| 1060 | if (table.PcToDexSize() != 0) { |
| 1061 | typedef MappingTable::PcToDexIterator It; |
| 1062 | os << "suspend point mappings {\n"; |
| 1063 | for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) { |
| 1064 | indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc()); |
| 1065 | } |
| 1066 | os << "}\n"; |
| 1067 | } |
| 1068 | if (table.DexToPcSize() != 0) { |
| 1069 | typedef MappingTable::DexToPcIterator It; |
| 1070 | os << "catch entry mappings {\n"; |
| 1071 | for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) { |
| 1072 | indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc()); |
| 1073 | } |
| 1074 | os << "}\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1075 | } |
| 1076 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1077 | } |
| 1078 | |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1079 | uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method, |
| 1080 | size_t offset, bool suspend_point_mapping) { |
| 1081 | MappingTable table(oat_method.GetMappingTable()); |
| 1082 | if (suspend_point_mapping && table.PcToDexSize() > 0) { |
| 1083 | typedef MappingTable::PcToDexIterator It; |
| 1084 | for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) { |
| 1085 | if (offset == cur.NativePcOffset()) { |
Brian Carlstrom | 4b8c13e | 2013-08-23 18:10:32 -0700 | [diff] [blame] | 1086 | os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc()); |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1087 | return cur.DexPc(); |
| 1088 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 1089 | } |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1090 | } else if (!suspend_point_mapping && table.DexToPcSize() > 0) { |
| 1091 | typedef MappingTable::DexToPcIterator It; |
| 1092 | for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) { |
| 1093 | if (offset == cur.NativePcOffset()) { |
Brian Carlstrom | 4b8c13e | 2013-08-23 18:10:32 -0700 | [diff] [blame] | 1094 | os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc()); |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1095 | return cur.DexPc(); |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1096 | } |
| 1097 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 1098 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1099 | return DexFile::kDexNoIndex; |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1102 | void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method, |
| 1103 | const DexFile::CodeItem* code_item, size_t native_pc_offset) { |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1104 | const uint8_t* gc_map_raw = oat_method.GetNativeGcMap(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1105 | if (gc_map_raw != nullptr) { |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1106 | NativePcOffsetToReferenceMap map(gc_map_raw); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1107 | if (map.HasEntry(native_pc_offset)) { |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1108 | size_t num_regs = map.RegWidth() * 8; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1109 | const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset); |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1110 | bool first = true; |
| 1111 | for (size_t reg = 0; reg < num_regs; reg++) { |
| 1112 | if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) { |
| 1113 | if (first) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1114 | os << "GC map objects: v" << reg << " ("; |
| 1115 | DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg); |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1116 | os << ")"; |
| 1117 | first = false; |
| 1118 | } else { |
| 1119 | os << ", v" << reg << " ("; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1120 | DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg); |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1121 | os << ")"; |
| 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | if (!first) { |
| 1126 | os << "\n"; |
| 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1132 | void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier, |
| 1133 | const OatFile::OatMethod& oat_method, |
| 1134 | const DexFile::CodeItem* code_item, uint32_t dex_pc) { |
| 1135 | DCHECK(verifier != nullptr); |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 1136 | std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1137 | bool first = true; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1138 | for (size_t reg = 0; reg < code_item->registers_size_; reg++) { |
| 1139 | VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2)); |
| 1140 | if (kind != kUndefined) { |
| 1141 | if (first) { |
| 1142 | os << "VRegs: v"; |
| 1143 | first = false; |
| 1144 | } else { |
| 1145 | os << ", v"; |
| 1146 | } |
| 1147 | os << reg << " ("; |
| 1148 | switch (kind) { |
| 1149 | case kImpreciseConstant: |
| 1150 | os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", "; |
| 1151 | DescribeVReg(os, oat_method, code_item, reg, kind); |
| 1152 | break; |
| 1153 | case kConstant: |
| 1154 | os << "Constant: " << kinds.at((reg * 2) + 1); |
| 1155 | break; |
| 1156 | default: |
| 1157 | DescribeVReg(os, oat_method, code_item, reg, kind); |
| 1158 | break; |
| 1159 | } |
| 1160 | os << ")"; |
| 1161 | } |
| 1162 | } |
| 1163 | if (!first) { |
| 1164 | os << "\n"; |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1169 | void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1170 | if (code_item != nullptr) { |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1171 | size_t i = 0; |
| 1172 | while (i < code_item->insns_size_in_code_units_) { |
| 1173 | const Instruction* instruction = Instruction::At(&code_item->insns_[i]); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1174 | os << StringPrintf("0x%04zx: %s\n", i, instruction->DumpString(&dex_file).c_str()); |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1175 | i += instruction->SizeInCodeUnits(); |
| 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1180 | verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx, |
| 1181 | const DexFile* dex_file, |
| 1182 | const DexFile::ClassDef& class_def, |
| 1183 | const DexFile::CodeItem* code_item, |
| 1184 | uint32_t method_access_flags) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1185 | if ((method_access_flags & kAccNative) == 0) { |
| 1186 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 1187 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1188 | Handle<mirror::DexCache> dex_cache( |
| 1189 | hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file))); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 1190 | DCHECK(options_->class_loader_ != nullptr); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1191 | return verifier::MethodVerifier::VerifyMethodAndDump(soa.Self(), os, dex_method_idx, dex_file, |
| 1192 | dex_cache, |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 1193 | *options_->class_loader_, |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1194 | &class_def, code_item, |
| 1195 | NullHandle<mirror::ArtMethod>(), |
| 1196 | method_access_flags); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1197 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1198 | |
| 1199 | return nullptr; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1200 | } |
| 1201 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1202 | void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier, |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1203 | const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item, |
| 1204 | bool bad_input, size_t code_size) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1205 | const void* portable_code = oat_method.GetPortableCode(); |
| 1206 | const void* quick_code = oat_method.GetQuickCode(); |
| 1207 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1208 | if (code_size == 0) { |
| 1209 | code_size = oat_method.GetQuickCodeSize(); |
| 1210 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1211 | if ((code_size == 0) || ((portable_code == nullptr) && (quick_code == nullptr))) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1212 | os << "NO CODE!\n"; |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1213 | return; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1214 | } else if (quick_code != nullptr) { |
| 1215 | const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code); |
| 1216 | size_t offset = 0; |
| 1217 | while (offset < code_size) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1218 | if (!bad_input) { |
| 1219 | DumpMappingAtOffset(os, oat_method, offset, false); |
| 1220 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1221 | offset += disassembler_->Dump(os, quick_native_pc + offset); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1222 | if (!bad_input) { |
| 1223 | uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true); |
| 1224 | if (dex_pc != DexFile::kDexNoIndex) { |
| 1225 | DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset); |
| 1226 | if (verifier != nullptr) { |
| 1227 | DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc); |
| 1228 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1229 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1230 | } |
| 1231 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1232 | } else { |
| 1233 | CHECK(portable_code != nullptr); |
| 1234 | CHECK_EQ(code_size, 0U); // TODO: disassembly of portable is currently not supported. |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1235 | } |
| 1236 | } |
| 1237 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1238 | const OatFile& oat_file_; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1239 | const std::vector<const OatFile::OatDexFile*> oat_dex_files_; |
| 1240 | const OatDumperOptions* options_; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1241 | std::set<uintptr_t> offsets_; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1242 | Disassembler* disassembler_; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1243 | }; |
| 1244 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1245 | class ImageDumper { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1246 | public: |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 1247 | explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space, |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1248 | const ImageHeader& image_header, OatDumperOptions* oat_dumper_options) |
| 1249 | : os_(os), |
| 1250 | image_space_(image_space), |
| 1251 | image_header_(image_header), |
| 1252 | oat_dumper_options_(oat_dumper_options) {} |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1253 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1254 | bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1255 | std::ostream& os = *os_; |
| 1256 | os << "MAGIC: " << image_header_.GetMagic() << "\n\n"; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1257 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1258 | os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1259 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 1260 | os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset()) |
| 1261 | << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n"; |
| 1262 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1263 | os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum()); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1264 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1265 | os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1266 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1267 | os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n"; |
| 1268 | |
| 1269 | os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n"; |
| 1270 | |
| 1271 | os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1272 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1273 | os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n"; |
| 1274 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1275 | { |
| 1276 | os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n"; |
| 1277 | Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 1278 | std::ostream indent1_os(&indent1_filter); |
| 1279 | CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax)); |
| 1280 | for (int i = 0; i < ImageHeader::kImageRootsMax; i++) { |
| 1281 | ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i); |
| 1282 | const char* image_root_description = image_roots_descriptions_[i]; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1283 | mirror::Object* image_root_object = image_header_.GetImageRoot(image_root); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1284 | indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object); |
| 1285 | if (image_root_object->IsObjectArray()) { |
| 1286 | Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 1287 | std::ostream indent2_os(&indent2_filter); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1288 | mirror::ObjectArray<mirror::Object>* image_root_object_array |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 1289 | = image_root_object->AsObjectArray<mirror::Object>(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1290 | for (int i = 0; i < image_root_object_array->GetLength(); i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1291 | mirror::Object* value = image_root_object_array->Get(i); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 1292 | size_t run = 0; |
| 1293 | for (int32_t j = i + 1; j < image_root_object_array->GetLength(); j++) { |
| 1294 | if (value == image_root_object_array->Get(j)) { |
| 1295 | run++; |
| 1296 | } else { |
| 1297 | break; |
| 1298 | } |
| 1299 | } |
| 1300 | if (run == 0) { |
| 1301 | indent2_os << StringPrintf("%d: ", i); |
| 1302 | } else { |
| 1303 | indent2_os << StringPrintf("%d to %zd: ", i, i + run); |
| 1304 | i = i + run; |
| 1305 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1306 | if (value != nullptr) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1307 | PrettyObjectValue(indent2_os, value->GetClass(), value); |
| 1308 | } else { |
| 1309 | indent2_os << i << ": null\n"; |
| 1310 | } |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1311 | } |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 1312 | } |
| 1313 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1314 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1315 | os << "\n"; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1316 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1317 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 1318 | std::string image_filename = image_space_.GetImageFilename(); |
| 1319 | std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1320 | os << "OAT LOCATION: " << oat_location; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1321 | os << "\n"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1322 | std::string error_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1323 | const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location); |
| 1324 | if (oat_file == nullptr) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1325 | oat_file = OatFile::Open(oat_location, oat_location, nullptr, false, &error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1326 | if (oat_file == nullptr) { |
| 1327 | os << "NOT FOUND: " << error_msg << "\n"; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1328 | return false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1329 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1330 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1331 | os << "\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1332 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1333 | stats_.oat_file_bytes = oat_file->Size(); |
| 1334 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1335 | oat_dumper_.reset(new OatDumper(*oat_file, oat_dumper_options_.release())); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1336 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1337 | for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1338 | CHECK(oat_dex_file != nullptr); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1339 | stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(), |
| 1340 | oat_dex_file->FileSize())); |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1343 | os << "OBJECTS:\n" << std::flush; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1344 | |
| 1345 | // Loop through all the image spaces and dump their objects. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1346 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 1347 | const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1348 | Thread* self = Thread::Current(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1349 | { |
Mathieu Chartier | c22c59e | 2014-02-24 15:16:06 -0800 | [diff] [blame] | 1350 | { |
| 1351 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 1352 | heap->FlushAllocStack(); |
| 1353 | } |
Hiroshi Yamauchi | 90d7068 | 2014-02-20 16:17:30 -0800 | [diff] [blame] | 1354 | // Since FlushAllocStack() above resets the (active) allocation |
| 1355 | // stack. Need to revoke the thread-local allocation stacks that |
| 1356 | // point into it. |
Mathieu Chartier | c22c59e | 2014-02-24 15:16:06 -0800 | [diff] [blame] | 1357 | { |
| 1358 | self->TransitionFromRunnableToSuspended(kNative); |
| 1359 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 1360 | thread_list->SuspendAll(); |
| 1361 | heap->RevokeAllThreadLocalAllocationStacks(self); |
| 1362 | thread_list->ResumeAll(); |
| 1363 | self->TransitionFromSuspendedToRunnable(); |
| 1364 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1365 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1366 | { |
| 1367 | std::ostream* saved_os = os_; |
| 1368 | Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 1369 | std::ostream indent_os(&indent_filter); |
| 1370 | os_ = &indent_os; |
| 1371 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1372 | for (const auto& space : spaces) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1373 | if (space->IsImageSpace()) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1374 | gc::space::ImageSpace* image_space = space->AsImageSpace(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1375 | image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this); |
| 1376 | indent_os << "\n"; |
| 1377 | } |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1378 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1379 | // Dump the large objects separately. |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 1380 | heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1381 | indent_os << "\n"; |
| 1382 | os_ = saved_os; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1383 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1384 | os << "STATS:\n" << std::flush; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1385 | std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str())); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1386 | if (file.get() == nullptr) { |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 1387 | LOG(WARNING) << "Failed to find image in " << image_filename; |
Brian Carlstrom | 6f27775 | 2013-09-30 17:56:45 -0700 | [diff] [blame] | 1388 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1389 | if (file.get() != nullptr) { |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 1390 | stats_.file_bytes = file->GetLength(); |
Brian Carlstrom | 6f27775 | 2013-09-30 17:56:45 -0700 | [diff] [blame] | 1391 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1392 | size_t header_bytes = sizeof(ImageHeader); |
| 1393 | stats_.header_bytes = header_bytes; |
| 1394 | size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes; |
| 1395 | stats_.alignment_bytes += alignment_bytes; |
Mathieu Chartier | 3232709 | 2013-08-30 14:04:08 -0700 | [diff] [blame] | 1396 | stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize(); |
| 1397 | stats_.bitmap_bytes += image_header_.GetImageBitmapSize(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1398 | stats_.Dump(os); |
| 1399 | os << "\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1400 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1401 | os << std::flush; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1402 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1403 | return oat_dumper_->Dump(os); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1406 | private: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1407 | static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1408 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1409 | CHECK(type != nullptr); |
| 1410 | if (value == nullptr) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1411 | os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1412 | } else if (type->IsStringClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1413 | mirror::String* string = value->AsString(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1414 | os << StringPrintf("%p String: %s\n", string, |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1415 | PrintableString(string->ToModifiedUtf8().c_str()).c_str()); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 1416 | } else if (type->IsClassClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1417 | mirror::Class* klass = value->AsClass(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1418 | os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1419 | } else if (type->IsArtFieldClass()) { |
| 1420 | mirror::ArtField* field = value->AsArtField(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1421 | os << StringPrintf("%p Field: %s\n", field, PrettyField(field).c_str()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1422 | } else if (type->IsArtMethodClass()) { |
| 1423 | mirror::ArtMethod* method = value->AsArtMethod(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1424 | os << StringPrintf("%p Method: %s\n", method, PrettyMethod(method).c_str()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1425 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1426 | os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1427 | } |
| 1428 | } |
| 1429 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1430 | static void PrintField(std::ostream& os, mirror::ArtField* field, mirror::Object* obj) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1431 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1432 | const char* descriptor = field->GetTypeDescriptor(); |
| 1433 | os << StringPrintf("%s: ", field->GetName()); |
Ian Rogers | 48efc2b | 2012-08-27 17:20:31 -0700 | [diff] [blame] | 1434 | if (descriptor[0] != 'L' && descriptor[0] != '[') { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1435 | StackHandleScope<1> hs(Thread::Current()); |
| 1436 | FieldHelper fh(hs.NewHandle(field)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1437 | mirror::Class* type = fh.GetType(); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 1438 | DCHECK(type->IsPrimitive()); |
Ian Rogers | 48efc2b | 2012-08-27 17:20:31 -0700 | [diff] [blame] | 1439 | if (type->IsPrimitiveLong()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1440 | os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj)); |
Ian Rogers | 48efc2b | 2012-08-27 17:20:31 -0700 | [diff] [blame] | 1441 | } else if (type->IsPrimitiveDouble()) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1442 | os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj)); |
Ian Rogers | 48efc2b | 2012-08-27 17:20:31 -0700 | [diff] [blame] | 1443 | } else if (type->IsPrimitiveFloat()) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1444 | os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj)); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 1445 | } else if (type->IsPrimitiveInt()) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1446 | os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj)); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 1447 | } else if (type->IsPrimitiveChar()) { |
| 1448 | os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj)); |
| 1449 | } else if (type->IsPrimitiveShort()) { |
| 1450 | os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj)); |
| 1451 | } else if (type->IsPrimitiveBoolean()) { |
| 1452 | os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false", |
| 1453 | field->GetBoolean(obj)); |
| 1454 | } else if (type->IsPrimitiveByte()) { |
| 1455 | os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj)); |
| 1456 | } else { |
| 1457 | LOG(FATAL) << "Unknown type: " << PrettyClass(type); |
Ian Rogers | 48efc2b | 2012-08-27 17:20:31 -0700 | [diff] [blame] | 1458 | } |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1459 | } else { |
Ian Rogers | 48efc2b | 2012-08-27 17:20:31 -0700 | [diff] [blame] | 1460 | // Get the value, don't compute the type unless it is non-null as we don't want |
| 1461 | // to cause class loading. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1462 | mirror::Object* value = field->GetObj(obj); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1463 | if (value == nullptr) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1464 | os << StringPrintf("null %s\n", PrettyDescriptor(descriptor).c_str()); |
Ian Rogers | 48efc2b | 2012-08-27 17:20:31 -0700 | [diff] [blame] | 1465 | } else { |
Ian Rogers | 50239c7 | 2013-06-17 14:53:22 -0700 | [diff] [blame] | 1466 | // Grab the field type without causing resolution. |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1467 | StackHandleScope<1> hs(Thread::Current()); |
| 1468 | FieldHelper fh(hs.NewHandle(field)); |
Ian Rogers | 50239c7 | 2013-06-17 14:53:22 -0700 | [diff] [blame] | 1469 | mirror::Class* field_type = fh.GetType(false); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1470 | if (field_type != nullptr) { |
Ian Rogers | 50239c7 | 2013-06-17 14:53:22 -0700 | [diff] [blame] | 1471 | PrettyObjectValue(os, field_type, value); |
| 1472 | } else { |
| 1473 | os << StringPrintf("%p %s\n", value, PrettyDescriptor(descriptor).c_str()); |
| 1474 | } |
Ian Rogers | 48efc2b | 2012-08-27 17:20:31 -0700 | [diff] [blame] | 1475 | } |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1476 | } |
| 1477 | } |
| 1478 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1479 | static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1480 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1481 | mirror::Class* super = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1482 | if (super != nullptr) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1483 | DumpFields(os, obj, super); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1484 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1485 | mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1486 | if (fields != nullptr) { |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1487 | for (int32_t i = 0; i < fields->GetLength(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1488 | mirror::ArtField* field = fields->Get(i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1489 | PrintField(os, field, obj); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1494 | bool InDumpSpace(const mirror::Object* object) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1495 | return image_space_.Contains(object); |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 1496 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1497 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1498 | const void* GetQuickOatCodeBegin(mirror::ArtMethod* m) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1499 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1500 | const void* quick_code = m->GetEntryPointFromQuickCompiledCode(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame^] | 1501 | if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1502 | quick_code = oat_dumper_->GetQuickOatCode(m); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1503 | } |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 1504 | if (oat_dumper_->GetInstructionSet() == kThumb2) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1505 | quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1); |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 1506 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1507 | return quick_code; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1508 | } |
| 1509 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1510 | uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1511 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1512 | const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m)); |
| 1513 | if (oat_code_begin == nullptr) { |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 1514 | return 0; |
| 1515 | } |
| 1516 | return oat_code_begin[-1]; |
| 1517 | } |
| 1518 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1519 | const void* GetQuickOatCodeEnd(mirror::ArtMethod* m) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1520 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1521 | const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1522 | if (oat_code_begin == nullptr) { |
| 1523 | return nullptr; |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 1524 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1525 | return oat_code_begin + GetQuickOatCodeSize(m); |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 1526 | } |
| 1527 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1528 | static void Callback(mirror::Object* obj, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1529 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1530 | DCHECK(obj != nullptr); |
| 1531 | DCHECK(arg != nullptr); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1532 | ImageDumper* state = reinterpret_cast<ImageDumper*>(arg); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1533 | if (!state->InDumpSpace(obj)) { |
| 1534 | return; |
| 1535 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1536 | |
| 1537 | size_t object_bytes = obj->SizeOf(); |
| 1538 | size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes; |
| 1539 | state->stats_.object_bytes += object_bytes; |
| 1540 | state->stats_.alignment_bytes += alignment_bytes; |
| 1541 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1542 | std::ostream& os = *state->os_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1543 | mirror::Class* obj_class = obj->GetClass(); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1544 | if (obj_class->IsArrayClass()) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1545 | os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(), |
| 1546 | obj->AsArray()->GetLength()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1547 | } else if (obj->IsClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1548 | mirror::Class* klass = obj->AsClass(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1549 | os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str()) |
| 1550 | << klass->GetStatus() << ")\n"; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1551 | } else if (obj->IsArtField()) { |
Brian Carlstrom | 4b8c13e | 2013-08-23 18:10:32 -0700 | [diff] [blame] | 1552 | os << StringPrintf("%p: java.lang.reflect.ArtField %s\n", obj, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1553 | PrettyField(obj->AsArtField()).c_str()); |
| 1554 | } else if (obj->IsArtMethod()) { |
Brian Carlstrom | 4b8c13e | 2013-08-23 18:10:32 -0700 | [diff] [blame] | 1555 | os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1556 | PrettyMethod(obj->AsArtMethod()).c_str()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1557 | } else if (obj_class->IsStringClass()) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1558 | os << StringPrintf("%p: java.lang.String %s\n", obj, |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1559 | PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1560 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1561 | os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1562 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1563 | Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 1564 | std::ostream indent_os(&indent_filter); |
| 1565 | DumpFields(indent_os, obj, obj_class); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1566 | if (obj->IsObjectArray()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1567 | mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>(); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1568 | int32_t length = obj_array->GetLength(); |
| 1569 | for (int32_t i = 0; i < length; i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1570 | mirror::Object* value = obj_array->Get(i); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1571 | size_t run = 0; |
| 1572 | for (int32_t j = i + 1; j < length; j++) { |
| 1573 | if (value == obj_array->Get(j)) { |
| 1574 | run++; |
| 1575 | } else { |
| 1576 | break; |
| 1577 | } |
| 1578 | } |
| 1579 | if (run == 0) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1580 | indent_os << StringPrintf("%d: ", i); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1581 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1582 | indent_os << StringPrintf("%d to %zd: ", i, i + run); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1583 | i = i + run; |
| 1584 | } |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 1585 | mirror::Class* value_class = |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1586 | (value == nullptr) ? obj_class->GetComponentType() : value->GetClass(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1587 | PrettyObjectValue(indent_os, value_class, value); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1588 | } |
| 1589 | } else if (obj->IsClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1590 | mirror::ObjectArray<mirror::ArtField>* sfields = obj->AsClass()->GetSFields(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1591 | if (sfields != nullptr) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1592 | indent_os << "STATICS:\n"; |
| 1593 | Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 1594 | std::ostream indent2_os(&indent2_filter); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1595 | for (int32_t i = 0; i < sfields->GetLength(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1596 | mirror::ArtField* field = sfields->Get(i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1597 | PrintField(indent2_os, field, field->GetDeclaringClass()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1598 | } |
| 1599 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1600 | } else if (obj->IsArtMethod()) { |
| 1601 | mirror::ArtMethod* method = obj->AsArtMethod(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1602 | if (method->IsNative()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1603 | // TODO: portable dumping. |
| 1604 | DCHECK(method->GetNativeGcMap() == nullptr) << PrettyMethod(method); |
| 1605 | DCHECK(method->GetMappingTable() == nullptr) << PrettyMethod(method); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1606 | bool first_occurrence; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1607 | const void* quick_oat_code = state->GetQuickOatCodeBegin(method); |
| 1608 | uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method); |
| 1609 | state->ComputeOatSize(quick_oat_code, &first_occurrence); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1610 | if (first_occurrence) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1611 | state->stats_.native_to_managed_code_bytes += quick_oat_code_size; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1612 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1613 | if (quick_oat_code != method->GetEntryPointFromQuickCompiledCode()) { |
| 1614 | indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1615 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1616 | } else if (method->IsAbstract() || method->IsCalleeSaveMethod() || |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1617 | method->IsResolutionMethod() || method->IsImtConflictMethod() || |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1618 | method->IsClassInitializer()) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1619 | DCHECK(method->GetNativeGcMap() == nullptr) << PrettyMethod(method); |
| 1620 | DCHECK(method->GetMappingTable() == nullptr) << PrettyMethod(method); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1621 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1622 | const DexFile::CodeItem* code_item = method->GetCodeItem(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1623 | 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] | 1624 | state->stats_.dex_instruction_bytes += dex_instruction_bytes; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1625 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 1626 | bool first_occurrence; |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 1627 | size_t gc_map_bytes = state->ComputeOatSize(method->GetNativeGcMap(), &first_occurrence); |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 1628 | if (first_occurrence) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1629 | state->stats_.gc_map_bytes += gc_map_bytes; |
| 1630 | } |
| 1631 | |
| 1632 | size_t pc_mapping_table_bytes = |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1633 | state->ComputeOatSize(method->GetMappingTable(), &first_occurrence); |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 1634 | if (first_occurrence) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1635 | state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes; |
| 1636 | } |
| 1637 | |
| 1638 | size_t vmap_table_bytes = |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1639 | state->ComputeOatSize(method->GetVmapTable(), &first_occurrence); |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 1640 | if (first_occurrence) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1641 | state->stats_.vmap_table_bytes += vmap_table_bytes; |
| 1642 | } |
| 1643 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1644 | // TODO: portable dumping. |
| 1645 | const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method); |
| 1646 | const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method); |
| 1647 | uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method); |
| 1648 | state->ComputeOatSize(quick_oat_code_begin, &first_occurrence); |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 1649 | if (first_occurrence) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1650 | state->stats_.managed_code_bytes += quick_oat_code_size; |
Ian Rogers | 0d2d378 | 2012-04-10 11:09:18 -0700 | [diff] [blame] | 1651 | if (method->IsConstructor()) { |
| 1652 | if (method->IsStatic()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1653 | state->stats_.class_initializer_code_bytes += quick_oat_code_size; |
Ian Rogers | 0d2d378 | 2012-04-10 11:09:18 -0700 | [diff] [blame] | 1654 | } else if (dex_instruction_bytes > kLargeConstructorDexBytes) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1655 | state->stats_.large_initializer_code_bytes += quick_oat_code_size; |
Ian Rogers | 0d2d378 | 2012-04-10 11:09:18 -0700 | [diff] [blame] | 1656 | } |
| 1657 | } else if (dex_instruction_bytes > kLargeMethodDexBytes) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1658 | state->stats_.large_method_code_bytes += quick_oat_code_size; |
Ian Rogers | 0d2d378 | 2012-04-10 11:09:18 -0700 | [diff] [blame] | 1659 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1660 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1661 | state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1662 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1663 | indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1664 | indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n", |
| 1665 | dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1666 | |
| 1667 | size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes + |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1668 | vmap_table_bytes + quick_oat_code_size + object_bytes; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1669 | |
| 1670 | double expansion = |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1671 | static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1672 | state->stats_.ComputeOutliers(total_size, expansion, method); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1673 | } |
| 1674 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1675 | std::string temp; |
| 1676 | state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1677 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1678 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1679 | std::set<const void*> already_seen_; |
| 1680 | // Compute the size of the given data within the oat file and whether this is the first time |
| 1681 | // this data has been requested |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 1682 | size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1683 | if (already_seen_.count(oat_data) == 0) { |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 1684 | *first_occurrence = true; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1685 | already_seen_.insert(oat_data); |
| 1686 | } else { |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 1687 | *first_occurrence = false; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1688 | } |
| 1689 | return oat_dumper_->ComputeSize(oat_data); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1690 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1691 | |
| 1692 | public: |
| 1693 | struct Stats { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1694 | size_t oat_file_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1695 | size_t file_bytes; |
| 1696 | |
| 1697 | size_t header_bytes; |
| 1698 | size_t object_bytes; |
Mathieu Chartier | 3232709 | 2013-08-30 14:04:08 -0700 | [diff] [blame] | 1699 | size_t bitmap_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1700 | size_t alignment_bytes; |
| 1701 | |
| 1702 | size_t managed_code_bytes; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1703 | size_t managed_code_bytes_ignoring_deduplication; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1704 | size_t managed_to_native_code_bytes; |
| 1705 | size_t native_to_managed_code_bytes; |
Ian Rogers | 0d2d378 | 2012-04-10 11:09:18 -0700 | [diff] [blame] | 1706 | size_t class_initializer_code_bytes; |
| 1707 | size_t large_initializer_code_bytes; |
| 1708 | size_t large_method_code_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1709 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1710 | size_t gc_map_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1711 | size_t pc_mapping_table_bytes; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1712 | size_t vmap_table_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1713 | |
| 1714 | size_t dex_instruction_bytes; |
| 1715 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1716 | std::vector<mirror::ArtMethod*> method_outlier; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1717 | std::vector<size_t> method_outlier_size; |
| 1718 | std::vector<double> method_outlier_expansion; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1719 | std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1720 | |
| 1721 | explicit Stats() |
| 1722 | : oat_file_bytes(0), |
| 1723 | file_bytes(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1724 | header_bytes(0), |
| 1725 | object_bytes(0), |
Mathieu Chartier | 3232709 | 2013-08-30 14:04:08 -0700 | [diff] [blame] | 1726 | bitmap_bytes(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1727 | alignment_bytes(0), |
| 1728 | managed_code_bytes(0), |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1729 | managed_code_bytes_ignoring_deduplication(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1730 | managed_to_native_code_bytes(0), |
| 1731 | native_to_managed_code_bytes(0), |
Ian Rogers | 0d2d378 | 2012-04-10 11:09:18 -0700 | [diff] [blame] | 1732 | class_initializer_code_bytes(0), |
| 1733 | large_initializer_code_bytes(0), |
| 1734 | large_method_code_bytes(0), |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1735 | gc_map_bytes(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1736 | pc_mapping_table_bytes(0), |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1737 | vmap_table_bytes(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1738 | dex_instruction_bytes(0) {} |
| 1739 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 1740 | struct SizeAndCount { |
| 1741 | SizeAndCount(size_t bytes, size_t count) : bytes(bytes), count(count) {} |
| 1742 | size_t bytes; |
| 1743 | size_t count; |
| 1744 | }; |
| 1745 | typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable; |
| 1746 | SizeAndCountTable sizes_and_counts; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1747 | |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 1748 | void Update(const char* descriptor, size_t object_bytes) { |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 1749 | SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor); |
| 1750 | if (it != sizes_and_counts.end()) { |
| 1751 | it->second.bytes += object_bytes; |
| 1752 | it->second.count += 1; |
| 1753 | } else { |
| 1754 | sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes, 1)); |
| 1755 | } |
| 1756 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1757 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1758 | double PercentOfOatBytes(size_t size) { |
| 1759 | return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100; |
| 1760 | } |
| 1761 | |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1762 | double PercentOfFileBytes(size_t size) { |
| 1763 | return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100; |
| 1764 | } |
| 1765 | |
| 1766 | double PercentOfObjectBytes(size_t size) { |
| 1767 | return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100; |
| 1768 | } |
| 1769 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1770 | void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1771 | method_outlier_size.push_back(total_size); |
| 1772 | method_outlier_expansion.push_back(expansion); |
| 1773 | method_outlier.push_back(method); |
| 1774 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1775 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1776 | void DumpOutliers(std::ostream& os) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1777 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1778 | size_t sum_of_sizes = 0; |
| 1779 | size_t sum_of_sizes_squared = 0; |
| 1780 | size_t sum_of_expansion = 0; |
| 1781 | size_t sum_of_expansion_squared = 0; |
| 1782 | size_t n = method_outlier_size.size(); |
| 1783 | for (size_t i = 0; i < n; i++) { |
| 1784 | size_t cur_size = method_outlier_size[i]; |
| 1785 | sum_of_sizes += cur_size; |
| 1786 | sum_of_sizes_squared += cur_size * cur_size; |
| 1787 | double cur_expansion = method_outlier_expansion[i]; |
| 1788 | sum_of_expansion += cur_expansion; |
| 1789 | sum_of_expansion_squared += cur_expansion * cur_expansion; |
| 1790 | } |
| 1791 | size_t size_mean = sum_of_sizes / n; |
| 1792 | size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1); |
| 1793 | double expansion_mean = sum_of_expansion / n; |
| 1794 | double expansion_variance = |
| 1795 | (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1); |
| 1796 | |
| 1797 | // Dump methods whose size is a certain number of standard deviations from the mean |
| 1798 | size_t dumped_values = 0; |
| 1799 | size_t skipped_values = 0; |
| 1800 | for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations |
| 1801 | size_t cur_size_variance = i * i * size_variance; |
| 1802 | bool first = true; |
| 1803 | for (size_t j = 0; j < n; j++) { |
| 1804 | size_t cur_size = method_outlier_size[j]; |
| 1805 | if (cur_size > size_mean) { |
| 1806 | size_t cur_var = cur_size - size_mean; |
| 1807 | cur_var = cur_var * cur_var; |
| 1808 | if (cur_var > cur_size_variance) { |
| 1809 | if (dumped_values > 20) { |
| 1810 | if (i == 1) { |
| 1811 | skipped_values++; |
| 1812 | } else { |
| 1813 | i = 2; // jump to counting for 1 standard deviation |
| 1814 | break; |
| 1815 | } |
| 1816 | } else { |
| 1817 | if (first) { |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1818 | os << "\nBig methods (size > " << i << " standard deviations the norm):\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1819 | first = false; |
| 1820 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1821 | os << PrettyMethod(method_outlier[j]) << " requires storage of " |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1822 | << PrettySize(cur_size) << "\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1823 | method_outlier_size[j] = 0; // don't consider this method again |
| 1824 | dumped_values++; |
| 1825 | } |
| 1826 | } |
| 1827 | } |
| 1828 | } |
| 1829 | } |
| 1830 | if (skipped_values > 0) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1831 | os << "... skipped " << skipped_values |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1832 | << " methods with size > 1 standard deviation from the norm\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1833 | } |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1834 | os << std::flush; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1835 | |
| 1836 | // Dump methods whose expansion is a certain number of standard deviations from the mean |
| 1837 | dumped_values = 0; |
| 1838 | skipped_values = 0; |
| 1839 | for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations |
| 1840 | double cur_expansion_variance = i * i * expansion_variance; |
| 1841 | bool first = true; |
| 1842 | for (size_t j = 0; j < n; j++) { |
| 1843 | double cur_expansion = method_outlier_expansion[j]; |
| 1844 | if (cur_expansion > expansion_mean) { |
| 1845 | size_t cur_var = cur_expansion - expansion_mean; |
| 1846 | cur_var = cur_var * cur_var; |
| 1847 | if (cur_var > cur_expansion_variance) { |
| 1848 | if (dumped_values > 20) { |
| 1849 | if (i == 1) { |
| 1850 | skipped_values++; |
| 1851 | } else { |
| 1852 | i = 2; // jump to counting for 1 standard deviation |
| 1853 | break; |
| 1854 | } |
| 1855 | } else { |
| 1856 | if (first) { |
| 1857 | os << "\nLarge expansion methods (size > " << i |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1858 | << " standard deviations the norm):\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1859 | first = false; |
| 1860 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1861 | os << PrettyMethod(method_outlier[j]) << " expanded code by " |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1862 | << cur_expansion << "\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1863 | method_outlier_expansion[j] = 0.0; // don't consider this method again |
| 1864 | dumped_values++; |
| 1865 | } |
| 1866 | } |
| 1867 | } |
| 1868 | } |
| 1869 | } |
| 1870 | if (skipped_values > 0) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1871 | os << "... skipped " << skipped_values |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1872 | << " methods with expansion > 1 standard deviation from the norm\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1873 | } |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1874 | os << "\n" << std::flush; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1875 | } |
| 1876 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1877 | void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1878 | { |
| 1879 | os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n" |
| 1880 | << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n"; |
| 1881 | Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 1882 | std::ostream indent_os(&indent_filter); |
| 1883 | indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n" |
| 1884 | "object_bytes = %8zd (%2.0f%% of art file bytes)\n" |
Mathieu Chartier | 3232709 | 2013-08-30 14:04:08 -0700 | [diff] [blame] | 1885 | "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1886 | "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n", |
| 1887 | header_bytes, PercentOfFileBytes(header_bytes), |
| 1888 | object_bytes, PercentOfFileBytes(object_bytes), |
Mathieu Chartier | 3232709 | 2013-08-30 14:04:08 -0700 | [diff] [blame] | 1889 | bitmap_bytes, PercentOfFileBytes(bitmap_bytes), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1890 | alignment_bytes, PercentOfFileBytes(alignment_bytes)) |
| 1891 | << std::flush; |
Mathieu Chartier | 3232709 | 2013-08-30 14:04:08 -0700 | [diff] [blame] | 1892 | CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1893 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1894 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1895 | os << "object_bytes breakdown:\n"; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1896 | size_t object_bytes_total = 0; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1897 | for (const auto& sizes_and_count : sizes_and_counts) { |
| 1898 | const std::string& descriptor(sizes_and_count.first); |
| 1899 | double average = static_cast<double>(sizes_and_count.second.bytes) / |
| 1900 | static_cast<double>(sizes_and_count.second.count); |
| 1901 | double percent = PercentOfObjectBytes(sizes_and_count.second.bytes); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1902 | os << StringPrintf("%32s %8zd bytes %6zd instances " |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 1903 | "(%4.0f bytes/instance) %2.0f%% of object_bytes\n", |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1904 | descriptor.c_str(), sizes_and_count.second.bytes, |
| 1905 | sizes_and_count.second.count, average, percent); |
| 1906 | object_bytes_total += sizes_and_count.second.bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1907 | } |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1908 | os << "\n" << std::flush; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1909 | CHECK_EQ(object_bytes, object_bytes_total); |
| 1910 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1911 | os << StringPrintf("oat_file_bytes = %8zd\n" |
| 1912 | "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" |
| 1913 | "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" |
| 1914 | "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n" |
| 1915 | "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" |
| 1916 | "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" |
| 1917 | "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n", |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 1918 | oat_file_bytes, |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 1919 | managed_code_bytes, |
| 1920 | PercentOfOatBytes(managed_code_bytes), |
| 1921 | managed_to_native_code_bytes, |
| 1922 | PercentOfOatBytes(managed_to_native_code_bytes), |
| 1923 | native_to_managed_code_bytes, |
| 1924 | PercentOfOatBytes(native_to_managed_code_bytes), |
| 1925 | class_initializer_code_bytes, |
| 1926 | PercentOfOatBytes(class_initializer_code_bytes), |
| 1927 | large_initializer_code_bytes, |
| 1928 | PercentOfOatBytes(large_initializer_code_bytes), |
| 1929 | large_method_code_bytes, |
| 1930 | PercentOfOatBytes(large_method_code_bytes)) |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1931 | << "DexFile sizes:\n"; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1932 | for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1933 | os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n", |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1934 | oat_dex_file_size.first.c_str(), oat_dex_file_size.second, |
| 1935 | PercentOfOatBytes(oat_dex_file_size.second)); |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 1936 | } |
| 1937 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1938 | os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n" |
| 1939 | "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n" |
| 1940 | "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n", |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 1941 | gc_map_bytes, PercentOfOatBytes(gc_map_bytes), |
| 1942 | pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes), |
| 1943 | vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes)) |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1944 | << std::flush; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1945 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1946 | os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes) |
| 1947 | << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n", |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 1948 | static_cast<double>(managed_code_bytes) / |
| 1949 | static_cast<double>(dex_instruction_bytes), |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1950 | static_cast<double>(managed_code_bytes_ignoring_deduplication) / |
Elliott Hughes | cf44e6f | 2012-05-24 19:42:18 -0700 | [diff] [blame] | 1951 | static_cast<double>(dex_instruction_bytes)) |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1952 | << std::flush; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1953 | |
| 1954 | DumpOutliers(os); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1955 | } |
| 1956 | } stats_; |
| 1957 | |
| 1958 | private: |
Ian Rogers | 0d2d378 | 2012-04-10 11:09:18 -0700 | [diff] [blame] | 1959 | enum { |
| 1960 | // Number of bytes for a constructor to be considered large. Based on the 1000 basic block |
| 1961 | // threshold, we assume 2 bytes per instruction and 2 instructions per block. |
| 1962 | kLargeConstructorDexBytes = 4000, |
| 1963 | // Number of bytes for a method to be considered large. Based on the 4000 basic block |
| 1964 | // threshold, we assume 2 bytes per instruction and 2 instructions per block. |
| 1965 | kLargeMethodDexBytes = 16000 |
| 1966 | }; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1967 | std::ostream* os_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1968 | gc::space::ImageSpace& image_space_; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1969 | const ImageHeader& image_header_; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1970 | std::unique_ptr<OatDumper> oat_dumper_; |
| 1971 | std::unique_ptr<OatDumperOptions> oat_dumper_options_; |
Elliott Hughes | d1bb4f6 | 2011-09-23 14:09:45 -0700 | [diff] [blame] | 1972 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1973 | DISALLOW_COPY_AND_ASSIGN(ImageDumper); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1974 | }; |
| 1975 | |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 1976 | static NoopCompilerCallbacks callbacks; |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 1977 | |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 1978 | static Runtime* StartRuntime(const char* boot_image_location, const char* image_location, |
| 1979 | InstructionSet instruction_set) { |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 1980 | RuntimeOptions options; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1981 | std::string image_option; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1982 | std::string oat_option; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1983 | std::string boot_image_option; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1984 | std::string boot_oat_option; |
Brian Carlstrom | 6c87180 | 2013-07-17 14:25:35 -0700 | [diff] [blame] | 1985 | |
| 1986 | // We are more like a compiler than a run-time. We don't want to execute code. |
Brian Carlstrom | c0a1b18 | 2014-03-04 23:19:06 -0800 | [diff] [blame] | 1987 | options.push_back(std::make_pair("compilercallbacks", &callbacks)); |
Brian Carlstrom | 6c87180 | 2013-07-17 14:25:35 -0700 | [diff] [blame] | 1988 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1989 | if (boot_image_location != nullptr) { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1990 | boot_image_option += "-Ximage:"; |
Brian Carlstrom | 0f5baa0 | 2014-05-22 11:54:18 -0700 | [diff] [blame] | 1991 | boot_image_option += boot_image_location; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1992 | options.push_back(std::make_pair(boot_image_option.c_str(), nullptr)); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1993 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1994 | if (image_location != nullptr) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1995 | image_option += "-Ximage:"; |
Brian Carlstrom | 0f5baa0 | 2014-05-22 11:54:18 -0700 | [diff] [blame] | 1996 | image_option += image_location; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1997 | options.push_back(std::make_pair(image_option.c_str(), nullptr)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1998 | } |
Brian Carlstrom | 0f5baa0 | 2014-05-22 11:54:18 -0700 | [diff] [blame] | 1999 | options.push_back( |
| 2000 | std::make_pair("imageinstructionset", |
| 2001 | reinterpret_cast<const void*>(GetInstructionSetString(instruction_set)))); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 2002 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2003 | if (!Runtime::Create(options, false)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2004 | fprintf(stderr, "Failed to create runtime\n"); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2005 | return nullptr; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 2006 | } |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2007 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2008 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
Ian Rogers | b9beb2e | 2014-05-09 16:57:40 -0700 | [diff] [blame] | 2009 | // give it away now and then switch to a more manageable ScopedObjectAccess. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2010 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2011 | |
| 2012 | return Runtime::Current(); |
| 2013 | } |
| 2014 | |
| 2015 | static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options, |
| 2016 | std::ostream* os) { |
| 2017 | // Dumping the image, no explicit class loader. |
| 2018 | NullHandle<mirror::ClassLoader> null_class_loader; |
| 2019 | options->class_loader_ = &null_class_loader; |
| 2020 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2021 | ScopedObjectAccess soa(Thread::Current()); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2022 | gc::Heap* heap = runtime->GetHeap(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2023 | gc::space::ImageSpace* image_space = heap->GetImageSpace(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2024 | CHECK(image_space != nullptr); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 2025 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 2026 | if (!image_header.IsValid()) { |
Brian Carlstrom | 0f5baa0 | 2014-05-22 11:54:18 -0700 | [diff] [blame] | 2027 | fprintf(stderr, "Invalid image header %s\n", image_location); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 2028 | return EXIT_FAILURE; |
| 2029 | } |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2030 | ImageDumper image_dumper(os, *image_space, image_header, options); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 2031 | bool success = image_dumper.Dump(); |
| 2032 | return (success) ? EXIT_SUCCESS : EXIT_FAILURE; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 2033 | } |
| 2034 | |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2035 | static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options, |
| 2036 | std::ostream* os) { |
| 2037 | CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr); |
| 2038 | |
| 2039 | Thread* self = Thread::Current(); |
| 2040 | CHECK(self != nullptr); |
| 2041 | // Need well-known-classes. |
| 2042 | WellKnownClasses::Init(self->GetJniEnv()); |
| 2043 | |
| 2044 | // Need to register dex files to get a working dex cache. |
| 2045 | ScopedObjectAccess soa(self); |
| 2046 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 2047 | class_linker->RegisterOatFile(oat_file); |
| 2048 | std::vector<const DexFile*> dex_files; |
| 2049 | for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) { |
| 2050 | std::string error_msg; |
| 2051 | const DexFile* dex_file = odf->OpenDexFile(&error_msg); |
| 2052 | CHECK(dex_file != nullptr) << error_msg; |
| 2053 | class_linker->RegisterDexFile(*dex_file); |
| 2054 | dex_files.push_back(dex_file); |
| 2055 | } |
| 2056 | |
| 2057 | // Need a class loader. |
| 2058 | soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader); |
| 2059 | ScopedLocalRef<jobject> class_loader_local(soa.Env(), |
| 2060 | soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader)); |
| 2061 | jobject class_loader = soa.Env()->NewGlobalRef(class_loader_local.get()); |
| 2062 | // Fake that we're a compiler. |
| 2063 | runtime->SetCompileTimeClassPath(class_loader, dex_files); |
| 2064 | |
| 2065 | // Use the class loader while dumping. |
| 2066 | StackHandleScope<1> scope(self); |
| 2067 | Handle<mirror::ClassLoader> loader_handle = scope.NewHandle( |
| 2068 | soa.Decode<mirror::ClassLoader*>(class_loader)); |
| 2069 | options->class_loader_ = &loader_handle; |
| 2070 | |
| 2071 | OatDumper oat_dumper(*oat_file, options); |
| 2072 | bool success = oat_dumper.Dump(*os); |
| 2073 | return (success) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 2074 | } |
| 2075 | |
| 2076 | static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) { |
| 2077 | // No image = no class loader. |
| 2078 | NullHandle<mirror::ClassLoader> null_class_loader; |
| 2079 | options->class_loader_ = &null_class_loader; |
| 2080 | |
| 2081 | OatDumper oat_dumper(*oat_file, options); |
| 2082 | bool success = oat_dumper.Dump(*os); |
| 2083 | return (success) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 2084 | } |
| 2085 | |
| 2086 | static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options, |
| 2087 | std::ostream* os) { |
| 2088 | std::string error_msg; |
| 2089 | OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, false, &error_msg); |
| 2090 | if (oat_file == nullptr) { |
| 2091 | fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str()); |
| 2092 | return EXIT_FAILURE; |
| 2093 | } |
| 2094 | |
| 2095 | if (runtime != nullptr) { |
| 2096 | return DumpOatWithRuntime(runtime, oat_file, options, os); |
| 2097 | } else { |
| 2098 | return DumpOatWithoutRuntime(oat_file, options, os); |
| 2099 | } |
| 2100 | } |
| 2101 | |
| 2102 | static int SymbolizeOat(const char* oat_filename, std::string& output_name) { |
| 2103 | std::string error_msg; |
| 2104 | OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, false, &error_msg); |
| 2105 | if (oat_file == nullptr) { |
| 2106 | fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str()); |
| 2107 | return EXIT_FAILURE; |
| 2108 | } |
| 2109 | |
| 2110 | OatSymbolizer oat_symbolizer(oat_file, output_name); |
| 2111 | if (!oat_symbolizer.Init()) { |
| 2112 | fprintf(stderr, "Failed to initialize symbolizer\n"); |
| 2113 | return EXIT_FAILURE; |
| 2114 | } |
| 2115 | if (!oat_symbolizer.Symbolize()) { |
| 2116 | fprintf(stderr, "Failed to symbolize\n"); |
| 2117 | return EXIT_FAILURE; |
| 2118 | } |
| 2119 | |
| 2120 | return EXIT_SUCCESS; |
| 2121 | } |
| 2122 | |
| 2123 | struct OatdumpArgs { |
| 2124 | bool Parse(int argc, char** argv) { |
| 2125 | // Skip over argv[0]. |
| 2126 | argv++; |
| 2127 | argc--; |
| 2128 | |
| 2129 | if (argc == 0) { |
| 2130 | fprintf(stderr, "No arguments specified\n"); |
| 2131 | usage(); |
| 2132 | return false; |
| 2133 | } |
| 2134 | |
| 2135 | for (int i = 0; i < argc; i++) { |
| 2136 | const StringPiece option(argv[i]); |
| 2137 | if (option.starts_with("--oat-file=")) { |
| 2138 | oat_filename_ = option.substr(strlen("--oat-file=")).data(); |
| 2139 | } else if (option.starts_with("--image=")) { |
| 2140 | image_location_ = option.substr(strlen("--image=")).data(); |
| 2141 | } else if (option.starts_with("--boot-image=")) { |
| 2142 | boot_image_location_ = option.substr(strlen("--boot-image=")).data(); |
| 2143 | } else if (option.starts_with("--instruction-set=")) { |
| 2144 | StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data(); |
| 2145 | instruction_set_ = GetInstructionSetFromString(instruction_set_str.data()); |
| 2146 | if (instruction_set_ == kNone) { |
| 2147 | fprintf(stderr, "Unsupported instruction set %s\n", instruction_set_str.data()); |
| 2148 | usage(); |
| 2149 | return false; |
| 2150 | } |
| 2151 | } else if (option =="--dump:raw_mapping_table") { |
| 2152 | dump_raw_mapping_table_ = true; |
| 2153 | } else if (option == "--dump:raw_gc_map") { |
| 2154 | dump_raw_gc_map_ = true; |
| 2155 | } else if (option == "--no-dump:vmap") { |
| 2156 | dump_vmap_ = false; |
| 2157 | } else if (option == "--no-disassemble") { |
| 2158 | disassemble_code_ = false; |
| 2159 | } else if (option.starts_with("--output=")) { |
| 2160 | output_name_ = option.substr(strlen("--output=")).ToString(); |
| 2161 | const char* filename = output_name_.c_str(); |
| 2162 | out_.reset(new std::ofstream(filename)); |
| 2163 | if (!out_->good()) { |
| 2164 | fprintf(stderr, "Failed to open output filename %s\n", filename); |
| 2165 | usage(); |
| 2166 | return false; |
| 2167 | } |
| 2168 | os_ = out_.get(); |
| 2169 | } else if (option.starts_with("--symbolize=")) { |
| 2170 | oat_filename_ = option.substr(strlen("--symbolize=")).data(); |
| 2171 | symbolize_ = true; |
| 2172 | } else { |
| 2173 | fprintf(stderr, "Unknown argument %s\n", option.data()); |
| 2174 | usage(); |
| 2175 | return false; |
| 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | if (image_location_ == nullptr && oat_filename_ == nullptr) { |
| 2180 | fprintf(stderr, "Either --image or --oat must be specified\n"); |
| 2181 | return false; |
| 2182 | } |
| 2183 | |
| 2184 | if (image_location_ != nullptr && oat_filename_ != nullptr) { |
| 2185 | fprintf(stderr, "Either --image or --oat must be specified but not both\n"); |
| 2186 | return false; |
| 2187 | } |
| 2188 | |
| 2189 | return true; |
| 2190 | } |
| 2191 | |
| 2192 | const char* oat_filename_ = nullptr; |
| 2193 | const char* image_location_ = nullptr; |
| 2194 | const char* boot_image_location_ = nullptr; |
| 2195 | InstructionSet instruction_set_ = kRuntimeISA; |
| 2196 | std::string elf_filename_prefix_; |
| 2197 | std::ostream* os_ = &std::cout; |
| 2198 | std::unique_ptr<std::ofstream> out_; |
| 2199 | std::string output_name_; |
| 2200 | bool dump_raw_mapping_table_ = false; |
| 2201 | bool dump_raw_gc_map_ = false; |
| 2202 | bool dump_vmap_ = true; |
| 2203 | bool disassemble_code_ = true; |
| 2204 | bool symbolize_ = false; |
| 2205 | }; |
| 2206 | |
| 2207 | static int oatdump(int argc, char** argv) { |
| 2208 | InitLogging(argv); |
| 2209 | |
| 2210 | OatdumpArgs args; |
| 2211 | if (!args.Parse(argc, argv)) { |
| 2212 | return EXIT_FAILURE; |
| 2213 | } |
| 2214 | |
| 2215 | // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping. |
| 2216 | bool absolute_addresses = (args.oat_filename_ == nullptr); |
| 2217 | |
| 2218 | std::unique_ptr<OatDumperOptions> oat_dumper_options(new OatDumperOptions( |
| 2219 | args.dump_raw_mapping_table_, |
| 2220 | args.dump_raw_gc_map_, |
| 2221 | args.dump_vmap_, |
| 2222 | args.disassemble_code_, |
| 2223 | absolute_addresses, |
| 2224 | nullptr)); |
| 2225 | |
| 2226 | std::unique_ptr<Runtime> runtime; |
| 2227 | if ((args.boot_image_location_ != nullptr || args.image_location_ != nullptr) && |
| 2228 | !args.symbolize_) { |
| 2229 | // If we have a boot image option, try to start the runtime; except when just symbolizing. |
| 2230 | runtime.reset(StartRuntime(args.boot_image_location_, |
| 2231 | args.image_location_, |
| 2232 | args.instruction_set_)); |
| 2233 | } |
| 2234 | |
| 2235 | if (args.oat_filename_ != nullptr) { |
| 2236 | if (args.symbolize_) { |
| 2237 | return SymbolizeOat(args.oat_filename_, args.output_name_); |
| 2238 | } else { |
| 2239 | return DumpOat(runtime.get(), args.oat_filename_, oat_dumper_options.release(), args.os_); |
| 2240 | } |
| 2241 | } |
| 2242 | |
| 2243 | if (runtime.get() == nullptr) { |
| 2244 | // We need the runtime when printing an image. |
| 2245 | return EXIT_FAILURE; |
| 2246 | } |
| 2247 | |
| 2248 | return DumpImage(runtime.get(), args.image_location_, oat_dumper_options.release(), args.os_); |
| 2249 | } |
| 2250 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 2251 | } // namespace art |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 2252 | |
| 2253 | int main(int argc, char** argv) { |
| 2254 | return art::oatdump(argc, argv); |
| 2255 | } |