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> |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 22 | #include <map> |
| 23 | #include <set> |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 24 | #include <string> |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 25 | #include <unordered_map> |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 28 | #include "arch/instruction_set_features.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 29 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 30 | #include "art_method-inl.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 31 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 32 | #include "class_linker.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | #include "class_linker-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 34 | #include "dex_file-inl.h" |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 35 | #include "dex_instruction.h" |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 36 | #include "disassembler.h" |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 37 | #include "elf_builder.h" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 38 | #include "gc_map.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 39 | #include "gc/space/image_space.h" |
| 40 | #include "gc/space/large_object_space.h" |
| 41 | #include "gc/space/space-inl.h" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 42 | #include "image.h" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 43 | #include "indenter.h" |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 44 | #include "mapping_table.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 45 | #include "mirror/array-inl.h" |
| 46 | #include "mirror/class-inl.h" |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 47 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 48 | #include "mirror/object-inl.h" |
| 49 | #include "mirror/object_array-inl.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 50 | #include "oat.h" |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 51 | #include "oat_file-inl.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 52 | #include "oat_file_manager.h" |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 53 | #include "os.h" |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 54 | #include "output_stream.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 55 | #include "safe_map.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 56 | #include "scoped_thread_state_change.h" |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 57 | #include "stack_map.h" |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 58 | #include "ScopedLocalRef.h" |
Mathieu Chartier | c22c59e | 2014-02-24 15:16:06 -0800 | [diff] [blame] | 59 | #include "thread_list.h" |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 60 | #include "verifier/dex_gc_map.h" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 61 | #include "verifier/method_verifier.h" |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 62 | #include "vmap_table.h" |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 63 | #include "well_known_classes.h" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 64 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 65 | #include <sys/stat.h> |
| 66 | #include "cmdline.h" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 67 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 68 | namespace art { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 69 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 70 | const char* image_methods_descriptions_[] = { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 71 | "kResolutionMethod", |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 72 | "kImtConflictMethod", |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 73 | "kImtUnimplementedMethod", |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 74 | "kCalleeSaveMethod", |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 75 | "kRefsOnlySaveMethod", |
| 76 | "kRefsAndArgsSaveMethod", |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | const char* image_roots_descriptions_[] = { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 80 | "kDexCaches", |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 81 | "kClassRoots", |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
Mathieu Chartier | ac8f439 | 2015-08-27 13:54:20 -0700 | [diff] [blame] | 84 | // Map is so that we don't allocate multiple dex files for the same OatDexFile. |
| 85 | static std::map<const OatFile::OatDexFile*, |
| 86 | std::unique_ptr<const DexFile>> opened_dex_files; |
| 87 | |
| 88 | const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) { |
| 89 | DCHECK(oat_dex_file != nullptr); |
| 90 | auto it = opened_dex_files.find(oat_dex_file); |
| 91 | if (it != opened_dex_files.end()) { |
| 92 | return it->second.get(); |
| 93 | } |
| 94 | const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release(); |
| 95 | opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret)); |
| 96 | return ret; |
| 97 | } |
| 98 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 99 | class OatSymbolizer FINAL { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 100 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 101 | OatSymbolizer(const OatFile* oat_file, const std::string& output_name) : |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 102 | oat_file_(oat_file), builder_(nullptr), |
| 103 | output_name_(output_name.empty() ? "symbolized.oat" : output_name) { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&, |
| 107 | uint32_t, |
| 108 | const OatFile::OatMethod&, |
| 109 | const DexFile&, |
| 110 | uint32_t, |
| 111 | const DexFile::CodeItem*, |
| 112 | uint32_t); |
| 113 | |
| 114 | bool Symbolize() { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 115 | const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet(); |
| 116 | |
| 117 | File* elf_file = OS::CreateEmptyFile(output_name_.c_str()); |
| 118 | std::unique_ptr<BufferedOutputStream> output_stream( |
| 119 | new BufferedOutputStream(new FileOutputStream(elf_file))); |
| 120 | builder_.reset(new ElfBuilder<ElfTypes32>(isa, output_stream.get())); |
| 121 | |
| 122 | builder_->Start(); |
| 123 | |
| 124 | auto* rodata = builder_->GetRoData(); |
| 125 | auto* text = builder_->GetText(); |
| 126 | auto* bss = builder_->GetBss(); |
| 127 | auto* strtab = builder_->GetStrTab(); |
| 128 | auto* symtab = builder_->GetSymTab(); |
| 129 | |
| 130 | rodata->Start(); |
| 131 | const uint8_t* rodata_begin = oat_file_->Begin(); |
| 132 | const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset(); |
| 133 | rodata->WriteFully(rodata_begin, rodata_size); |
| 134 | rodata->End(); |
| 135 | |
| 136 | text->Start(); |
| 137 | const uint8_t* text_begin = oat_file_->Begin() + rodata_size; |
| 138 | const size_t text_size = oat_file_->End() - text_begin; |
| 139 | text->WriteFully(text_begin, text_size); |
| 140 | text->End(); |
| 141 | |
| 142 | if (oat_file_->BssSize() != 0) { |
| 143 | bss->Start(); |
| 144 | bss->SetSize(oat_file_->BssSize()); |
| 145 | bss->End(); |
| 146 | } |
| 147 | |
| 148 | builder_->WriteDynamicSection(elf_file->GetPath()); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 149 | |
| 150 | Walk(&art::OatSymbolizer::RegisterForDedup); |
| 151 | |
| 152 | NormalizeState(); |
| 153 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 154 | strtab->Start(); |
| 155 | strtab->Write(""); // strtab should start with empty string. |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 156 | Walk(&art::OatSymbolizer::AddSymbol); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 157 | strtab->End(); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 158 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 159 | symtab->Start(); |
| 160 | symtab->Write(); |
| 161 | symtab->End(); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 162 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 163 | builder_->End(); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 164 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 165 | return builder_->Good() && output_stream->Flush(); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void Walk(Callback callback) { |
| 169 | std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles(); |
| 170 | for (size_t i = 0; i < oat_dex_files.size(); i++) { |
| 171 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i]; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 172 | CHECK(oat_dex_file != nullptr); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 173 | WalkOatDexFile(oat_dex_file, callback); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) { |
| 178 | std::string error_msg; |
Mathieu Chartier | ac8f439 | 2015-08-27 13:54:20 -0700 | [diff] [blame] | 179 | const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg); |
| 180 | if (dex_file == nullptr) { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 181 | return; |
| 182 | } |
| 183 | for (size_t class_def_index = 0; |
| 184 | class_def_index < dex_file->NumClassDefs(); |
| 185 | class_def_index++) { |
| 186 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 187 | const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index); |
| 188 | OatClassType type = oat_class.GetType(); |
| 189 | switch (type) { |
| 190 | case kOatClassAllCompiled: |
| 191 | case kOatClassSomeCompiled: |
Mathieu Chartier | ac8f439 | 2015-08-27 13:54:20 -0700 | [diff] [blame] | 192 | WalkOatClass(oat_class, *dex_file, class_def, callback); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 193 | break; |
| 194 | |
| 195 | case kOatClassNoneCompiled: |
| 196 | case kOatClassMax: |
| 197 | // Ignore. |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file, |
| 204 | const DexFile::ClassDef& class_def, Callback callback) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 205 | const uint8_t* class_data = dex_file.GetClassData(class_def); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 206 | if (class_data == nullptr) { // empty class such as a marker interface? |
| 207 | return; |
| 208 | } |
| 209 | // Note: even if this is an interface or a native class, we still have to walk it, as there |
| 210 | // might be a static initializer. |
| 211 | ClassDataItemIterator it(dex_file, class_data); |
| 212 | SkipAllFields(&it); |
| 213 | uint32_t class_method_idx = 0; |
| 214 | while (it.HasNextDirectMethod()) { |
| 215 | const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx); |
| 216 | WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(), |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 217 | it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 218 | class_method_idx++; |
| 219 | it.Next(); |
| 220 | } |
| 221 | while (it.HasNextVirtualMethod()) { |
| 222 | const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx); |
| 223 | WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(), |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 224 | it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 225 | class_method_idx++; |
| 226 | it.Next(); |
| 227 | } |
| 228 | DCHECK(!it.HasNext()); |
| 229 | } |
| 230 | |
| 231 | void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index, |
| 232 | const OatFile::OatMethod& oat_method, const DexFile& dex_file, |
| 233 | uint32_t dex_method_idx, const DexFile::CodeItem* code_item, |
| 234 | uint32_t method_access_flags, Callback callback) { |
| 235 | if ((method_access_flags & kAccAbstract) != 0) { |
| 236 | // Abstract method, no code. |
| 237 | return; |
| 238 | } |
| 239 | if (oat_method.GetCodeOffset() == 0) { |
| 240 | // No code. |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item, |
| 245 | method_access_flags); |
| 246 | } |
| 247 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 248 | void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED, |
| 249 | uint32_t class_method_index ATTRIBUTE_UNUSED, |
| 250 | const OatFile::OatMethod& oat_method, |
| 251 | const DexFile& dex_file ATTRIBUTE_UNUSED, |
| 252 | uint32_t dex_method_idx ATTRIBUTE_UNUSED, |
| 253 | const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED, |
| 254 | uint32_t method_access_flags ATTRIBUTE_UNUSED) { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 255 | state_[oat_method.GetCodeOffset()]++; |
| 256 | } |
| 257 | |
| 258 | void NormalizeState() { |
| 259 | for (auto& x : state_) { |
| 260 | if (x.second == 1) { |
| 261 | state_[x.first] = 0; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | enum class DedupState { // private |
| 267 | kNotDeduplicated, |
| 268 | kDeduplicatedFirst, |
| 269 | kDeduplicatedOther |
| 270 | }; |
| 271 | DedupState IsDuplicated(uint32_t offset) { |
| 272 | if (state_[offset] == 0) { |
| 273 | return DedupState::kNotDeduplicated; |
| 274 | } |
| 275 | if (state_[offset] == 1) { |
| 276 | return DedupState::kDeduplicatedOther; |
| 277 | } |
| 278 | state_[offset] = 1; |
| 279 | return DedupState::kDeduplicatedFirst; |
| 280 | } |
| 281 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 282 | void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED, |
| 283 | uint32_t class_method_index ATTRIBUTE_UNUSED, |
| 284 | const OatFile::OatMethod& oat_method, |
| 285 | const DexFile& dex_file, |
| 286 | uint32_t dex_method_idx, |
| 287 | const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED, |
| 288 | uint32_t method_access_flags ATTRIBUTE_UNUSED) { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 289 | DedupState dedup = IsDuplicated(oat_method.GetCodeOffset()); |
| 290 | if (dedup != DedupState::kDeduplicatedOther) { |
| 291 | std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true); |
| 292 | |
| 293 | if (dedup == DedupState::kDeduplicatedFirst) { |
| 294 | pretty_name = "[Dedup]" + pretty_name; |
| 295 | } |
| 296 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 297 | int name_offset = builder_->GetStrTab()->Write(pretty_name); |
| 298 | builder_->GetSymTab()->Add(name_offset, builder_->GetText(), |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 299 | oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(), |
| 300 | true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 304 | private: |
| 305 | static void SkipAllFields(ClassDataItemIterator* it) { |
| 306 | while (it->HasNextStaticField()) { |
| 307 | it->Next(); |
| 308 | } |
| 309 | while (it->HasNextInstanceField()) { |
| 310 | it->Next(); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | const OatFile* oat_file_; |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 315 | std::unique_ptr<ElfBuilder<ElfTypes32> > builder_; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 316 | std::unordered_map<uint32_t, uint32_t> state_; |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 317 | const std::string output_name_; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 318 | }; |
| 319 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 320 | class OatDumperOptions { |
| 321 | public: |
| 322 | OatDumperOptions(bool dump_raw_mapping_table, |
| 323 | bool dump_raw_gc_map, |
| 324 | bool dump_vmap, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 325 | bool dump_code_info_stack_maps, |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 326 | bool disassemble_code, |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 327 | bool absolute_addresses, |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 328 | const char* class_filter, |
| 329 | const char* method_filter, |
| 330 | bool list_classes, |
| 331 | bool list_methods, |
| 332 | const char* export_dex_location, |
| 333 | uint32_t addr2instr) |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 334 | : dump_raw_mapping_table_(dump_raw_mapping_table), |
| 335 | dump_raw_gc_map_(dump_raw_gc_map), |
| 336 | dump_vmap_(dump_vmap), |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 337 | dump_code_info_stack_maps_(dump_code_info_stack_maps), |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 338 | disassemble_code_(disassemble_code), |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 339 | absolute_addresses_(absolute_addresses), |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 340 | class_filter_(class_filter), |
Nicolas Geoffray | 3fcd220 | 2014-11-12 18:02:36 +0000 | [diff] [blame] | 341 | method_filter_(method_filter), |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 342 | list_classes_(list_classes), |
| 343 | list_methods_(list_methods), |
| 344 | export_dex_location_(export_dex_location), |
| 345 | addr2instr_(addr2instr), |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 346 | class_loader_(nullptr) {} |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 347 | |
| 348 | const bool dump_raw_mapping_table_; |
| 349 | const bool dump_raw_gc_map_; |
| 350 | const bool dump_vmap_; |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 351 | const bool dump_code_info_stack_maps_; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 352 | const bool disassemble_code_; |
| 353 | const bool absolute_addresses_; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 354 | const char* const class_filter_; |
Nicolas Geoffray | 3fcd220 | 2014-11-12 18:02:36 +0000 | [diff] [blame] | 355 | const char* const method_filter_; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 356 | const bool list_classes_; |
| 357 | const bool list_methods_; |
| 358 | const char* const export_dex_location_; |
| 359 | uint32_t addr2instr_; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 360 | Handle<mirror::ClassLoader>* class_loader_; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 361 | }; |
| 362 | |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 363 | class OatDumper { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 364 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 365 | OatDumper(const OatFile& oat_file, const OatDumperOptions& options) |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 366 | : oat_file_(oat_file), |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 367 | oat_dex_files_(oat_file.GetOatDexFiles()), |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 368 | options_(options), |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 369 | resolved_addr2instr_(0), |
Mathieu Chartier | a7dd038 | 2014-11-20 17:08:58 -0800 | [diff] [blame] | 370 | instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()), |
| 371 | disassembler_(Disassembler::Create(instruction_set_, |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 372 | new DisassemblerOptions(options_.absolute_addresses_, |
Alexandre Rames | a37d925 | 2014-10-27 11:28:14 +0000 | [diff] [blame] | 373 | oat_file.Begin(), |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 374 | true /* can_read_literals_ */))) { |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 375 | CHECK(options_.class_loader_ != nullptr); |
| 376 | CHECK(options_.class_filter_ != nullptr); |
| 377 | CHECK(options_.method_filter_ != 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() { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 382 | delete disassembler_; |
| 383 | } |
| 384 | |
Mathieu Chartier | a7dd038 | 2014-11-20 17:08:58 -0800 | [diff] [blame] | 385 | InstructionSet GetInstructionSet() { |
| 386 | return instruction_set_; |
| 387 | } |
| 388 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 389 | bool Dump(std::ostream& os) { |
| 390 | bool success = true; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 391 | const OatHeader& oat_header = oat_file_.GetOatHeader(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 392 | |
| 393 | os << "MAGIC:\n"; |
| 394 | os << oat_header.GetMagic() << "\n\n"; |
| 395 | |
| 396 | os << "CHECKSUM:\n"; |
Elliott Hughes | ed2adb6 | 2012-02-29 14:41:01 -0800 | [diff] [blame] | 397 | os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 398 | |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 399 | os << "INSTRUCTION SET:\n"; |
| 400 | os << oat_header.GetInstructionSet() << "\n\n"; |
| 401 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 402 | { |
| 403 | std::unique_ptr<const InstructionSetFeatures> features( |
| 404 | InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(), |
| 405 | oat_header.GetInstructionSetFeaturesBitmap())); |
| 406 | os << "INSTRUCTION SET FEATURES:\n"; |
| 407 | os << features->GetFeatureString() << "\n\n"; |
| 408 | } |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 409 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 410 | os << "DEX FILE COUNT:\n"; |
| 411 | os << oat_header.GetDexFileCount() << "\n\n"; |
| 412 | |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 413 | #define DUMP_OAT_HEADER_OFFSET(label, offset) \ |
| 414 | os << label " OFFSET:\n"; \ |
| 415 | os << StringPrintf("0x%08x", oat_header.offset()); \ |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 416 | if (oat_header.offset() != 0 && options_.absolute_addresses_) { \ |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 417 | os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \ |
| 418 | } \ |
| 419 | os << StringPrintf("\n\n"); |
| 420 | |
| 421 | DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset); |
| 422 | DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE", |
| 423 | GetInterpreterToInterpreterBridgeOffset); |
| 424 | DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE", |
| 425 | GetInterpreterToCompiledCodeBridgeOffset); |
| 426 | DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP", |
| 427 | GetJniDlsymLookupOffset); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 428 | DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE", |
| 429 | GetQuickGenericJniTrampolineOffset); |
| 430 | DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE", |
| 431 | GetQuickImtConflictTrampolineOffset); |
| 432 | DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE", |
| 433 | GetQuickResolutionTrampolineOffset); |
| 434 | DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE", |
| 435 | GetQuickToInterpreterBridgeOffset); |
| 436 | #undef DUMP_OAT_HEADER_OFFSET |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 437 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 438 | os << "IMAGE PATCH DELTA:\n"; |
| 439 | os << StringPrintf("%d (0x%08x)\n\n", |
| 440 | oat_header.GetImagePatchDelta(), |
| 441 | oat_header.GetImagePatchDelta()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 442 | |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 443 | os << "IMAGE FILE LOCATION OAT CHECKSUM:\n"; |
| 444 | os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum()); |
| 445 | |
| 446 | os << "IMAGE FILE LOCATION OAT BEGIN:\n"; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 447 | os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin()); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 448 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 449 | // Print the key-value store. |
| 450 | { |
| 451 | os << "KEY VALUE STORE:\n"; |
| 452 | size_t index = 0; |
| 453 | const char* key; |
| 454 | const char* value; |
| 455 | while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) { |
| 456 | os << key << " = " << value << "\n"; |
| 457 | index++; |
| 458 | } |
| 459 | os << "\n"; |
| 460 | } |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 461 | |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 462 | if (options_.absolute_addresses_) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 463 | os << "BEGIN:\n"; |
| 464 | os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 465 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 466 | os << "END:\n"; |
| 467 | os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n"; |
| 468 | } |
| 469 | |
| 470 | os << "SIZE:\n"; |
| 471 | os << oat_file_.Size() << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 472 | |
| 473 | os << std::flush; |
| 474 | |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 475 | // If set, adjust relative address to be searched |
| 476 | if (options_.addr2instr_ != 0) { |
| 477 | resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset(); |
| 478 | os << "SEARCH ADDRESS (executable offset + input):\n"; |
| 479 | os << StringPrintf("0x%08x\n\n", resolved_addr2instr_); |
| 480 | } |
| 481 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 482 | for (size_t i = 0; i < oat_dex_files_.size(); i++) { |
| 483 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 484 | CHECK(oat_dex_file != nullptr); |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 485 | |
| 486 | // If file export selected skip file analysis |
| 487 | if (options_.export_dex_location_) { |
| 488 | if (!ExportDexFile(os, *oat_dex_file)) { |
| 489 | success = false; |
| 490 | } |
| 491 | } else { |
| 492 | if (!DumpOatDexFile(os, *oat_dex_file)) { |
| 493 | success = false; |
| 494 | } |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 495 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 496 | } |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 497 | os << std::flush; |
| 498 | return success; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 501 | size_t ComputeSize(const void* oat_data) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 502 | if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() || |
| 503 | reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 504 | return 0; // Address not in oat file |
| 505 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 506 | uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) - |
| 507 | reinterpret_cast<uintptr_t>(oat_file_.Begin()); |
| 508 | auto it = offsets_.upper_bound(begin_offset); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 509 | CHECK(it != offsets_.end()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 510 | uintptr_t end_offset = *it; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 511 | return end_offset - begin_offset; |
| 512 | } |
| 513 | |
Mathieu Chartier | a7dd038 | 2014-11-20 17:08:58 -0800 | [diff] [blame] | 514 | InstructionSet GetOatInstructionSet() { |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 515 | return oat_file_.GetOatHeader().GetInstructionSet(); |
| 516 | } |
| 517 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 518 | const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 519 | for (size_t i = 0; i < oat_dex_files_.size(); i++) { |
| 520 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 521 | CHECK(oat_dex_file != nullptr); |
| 522 | std::string error_msg; |
Mathieu Chartier | ac8f439 | 2015-08-27 13:54:20 -0700 | [diff] [blame] | 523 | const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg); |
| 524 | if (dex_file == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 525 | LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() |
| 526 | << "': " << error_msg; |
| 527 | } else { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 528 | const char* descriptor = m->GetDeclaringClassDescriptor(); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 529 | const DexFile::ClassDef* class_def = |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 530 | dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 531 | if (class_def != nullptr) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 532 | uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 533 | const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 534 | size_t method_index = m->GetMethodIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 535 | return oat_class.GetOatMethod(method_index).GetQuickCode(); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 539 | return nullptr; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 542 | private: |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 543 | void AddAllOffsets() { |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 544 | // We don't know the length of the code for each method, but we need to know where to stop |
| 545 | // when disassembling. What we do know is that a region of code will be followed by some other |
| 546 | // region, so if we keep a sorted sequence of the start of each region, we can infer the length |
| 547 | // 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] | 548 | for (size_t i = 0; i < oat_dex_files_.size(); i++) { |
| 549 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 550 | CHECK(oat_dex_file != nullptr); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 551 | std::string error_msg; |
Mathieu Chartier | ac8f439 | 2015-08-27 13:54:20 -0700 | [diff] [blame] | 552 | const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg); |
| 553 | if (dex_file == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 554 | LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() |
| 555 | << "': " << error_msg; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 556 | continue; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 557 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 558 | offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader())); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 559 | for (size_t class_def_index = 0; |
| 560 | class_def_index < dex_file->NumClassDefs(); |
| 561 | class_def_index++) { |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 562 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 563 | const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 564 | const uint8_t* class_data = dex_file->GetClassData(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 565 | if (class_data != nullptr) { |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 566 | ClassDataItemIterator it(*dex_file, class_data); |
| 567 | SkipAllFields(it); |
| 568 | uint32_t class_method_index = 0; |
| 569 | while (it.HasNextDirectMethod()) { |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 570 | AddOffsets(oat_class.GetOatMethod(class_method_index++)); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 571 | it.Next(); |
| 572 | } |
| 573 | while (it.HasNextVirtualMethod()) { |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 574 | AddOffsets(oat_class.GetOatMethod(class_method_index++)); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 575 | it.Next(); |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | // If the last thing in the file is code for a method, there won't be an offset for the "next" |
| 582 | // thing. Instead of having a special case in the upper_bound code, let's just add an entry |
| 583 | // for the end of the file. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 584 | offsets_.insert(oat_file_.Size()); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 585 | } |
| 586 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 587 | static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) { |
| 588 | return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific. |
| 589 | } |
| 590 | |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 591 | void AddOffsets(const OatFile::OatMethod& oat_method) { |
Brian Carlstrom | 95ba0dc | 2012-03-05 22:06:14 -0800 | [diff] [blame] | 592 | uint32_t code_offset = oat_method.GetCodeOffset(); |
| 593 | if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) { |
| 594 | code_offset &= ~0x1; |
| 595 | } |
| 596 | offsets_.insert(code_offset); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 597 | offsets_.insert(oat_method.GetMappingTableOffset()); |
| 598 | offsets_.insert(oat_method.GetVmapTableOffset()); |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 599 | offsets_.insert(oat_method.GetGcMapOffset()); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 600 | } |
| 601 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 602 | bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) { |
| 603 | bool success = true; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 604 | bool stop_analysis = false; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 605 | os << "OatDexFile:\n"; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 606 | os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str()); |
Elliott Hughes | ed2adb6 | 2012-02-29 14:41:01 -0800 | [diff] [blame] | 607 | os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 608 | |
| 609 | // Create the verifier early. |
| 610 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 611 | std::string error_msg; |
Mathieu Chartier | ac8f439 | 2015-08-27 13:54:20 -0700 | [diff] [blame] | 612 | const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg); |
| 613 | if (dex_file == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 614 | os << "NOT FOUND: " << error_msg << "\n\n"; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 615 | os << std::flush; |
| 616 | return false; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 617 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 618 | |
| 619 | VariableIndentationOutputStream vios(&os); |
| 620 | ScopedIndentation indent1(&vios); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 621 | for (size_t class_def_index = 0; |
| 622 | class_def_index < dex_file->NumClassDefs(); |
| 623 | class_def_index++) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 624 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 625 | const char* descriptor = dex_file->GetClassDescriptor(class_def); |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 626 | |
| 627 | // TODO: Support regex |
| 628 | if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) { |
| 629 | continue; |
| 630 | } |
| 631 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 632 | uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 633 | const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 634 | os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)", |
| 635 | class_def_index, descriptor, oat_class_offset, class_def.class_idx_) |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 636 | << " (" << oat_class.GetStatus() << ")" |
| 637 | << " (" << oat_class.GetType() << ")\n"; |
| 638 | // TODO: include bitmap here if type is kOatClassSomeCompiled? |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 639 | if (options_.list_classes_) continue; |
Mathieu Chartier | ac8f439 | 2015-08-27 13:54:20 -0700 | [diff] [blame] | 640 | if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 641 | success = false; |
| 642 | } |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 643 | if (stop_analysis) { |
| 644 | os << std::flush; |
| 645 | return success; |
| 646 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | os << std::flush; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 650 | return success; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 653 | bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) { |
| 654 | std::string error_msg; |
| 655 | std::string dex_file_location = oat_dex_file.GetDexFileLocation(); |
| 656 | |
Mathieu Chartier | ac8f439 | 2015-08-27 13:54:20 -0700 | [diff] [blame] | 657 | const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg); |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 658 | if (dex_file == nullptr) { |
| 659 | os << "Failed to open dex file '" << dex_file_location << "': " << error_msg; |
| 660 | return false; |
| 661 | } |
| 662 | size_t fsize = oat_dex_file.FileSize(); |
| 663 | |
| 664 | // Some quick checks just in case |
| 665 | if (fsize == 0 || fsize < sizeof(DexFile::Header)) { |
| 666 | os << "Invalid dex file\n"; |
| 667 | return false; |
| 668 | } |
| 669 | |
| 670 | // Verify output directory exists |
| 671 | if (!OS::DirectoryExists(options_.export_dex_location_)) { |
| 672 | // TODO: Extend OS::DirectoryExists if symlink support is required |
| 673 | os << options_.export_dex_location_ << " output directory not found or symlink\n"; |
| 674 | return false; |
| 675 | } |
| 676 | |
| 677 | // Beautify path names |
| 678 | if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) { |
| 679 | return false; |
| 680 | } |
| 681 | |
| 682 | std::string dex_orig_name; |
| 683 | size_t dex_orig_pos = dex_file_location.rfind('/'); |
| 684 | if (dex_orig_pos == std::string::npos) |
| 685 | dex_orig_name = dex_file_location; |
| 686 | else |
| 687 | dex_orig_name = dex_file_location.substr(dex_orig_pos + 1); |
| 688 | |
| 689 | // A more elegant approach to efficiently name user installed apps is welcome |
| 690 | if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) { |
| 691 | dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1); |
| 692 | size_t apk_orig_pos = dex_file_location.rfind('/'); |
| 693 | if (apk_orig_pos != std::string::npos) { |
| 694 | dex_orig_name = dex_file_location.substr(++apk_orig_pos); |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | std::string out_dex_path(options_.export_dex_location_); |
| 699 | if (out_dex_path.back() != '/') { |
| 700 | out_dex_path.append("/"); |
| 701 | } |
| 702 | out_dex_path.append(dex_orig_name); |
| 703 | out_dex_path.append("_export.dex"); |
| 704 | if (out_dex_path.length() > PATH_MAX) { |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str())); |
| 709 | if (file.get() == nullptr) { |
| 710 | os << "Failed to open output dex file " << out_dex_path; |
| 711 | return false; |
| 712 | } |
| 713 | |
| 714 | if (!file->WriteFully(dex_file->Begin(), fsize)) { |
| 715 | os << "Failed to write dex file"; |
| 716 | file->Erase(); |
| 717 | return false; |
| 718 | } |
| 719 | |
| 720 | if (file->FlushCloseOrErase() != 0) { |
| 721 | os << "Flush and close failed"; |
| 722 | return false; |
| 723 | } |
| 724 | |
| 725 | os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize); |
| 726 | os << std::flush; |
| 727 | |
| 728 | return true; |
| 729 | } |
| 730 | |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 731 | static void SkipAllFields(ClassDataItemIterator& it) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 732 | while (it.HasNextStaticField()) { |
| 733 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 734 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 735 | while (it.HasNextInstanceField()) { |
| 736 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 737 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 738 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 739 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 740 | bool DumpOatClass(VariableIndentationOutputStream* vios, |
| 741 | const OatFile::OatClass& oat_class, const DexFile& dex_file, |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 742 | const DexFile::ClassDef& class_def, bool* stop_analysis) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 743 | bool success = true; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 744 | bool addr_found = false; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 745 | const uint8_t* class_data = dex_file.GetClassData(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 746 | if (class_data == nullptr) { // empty class such as a marker interface? |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 747 | vios->Stream() << std::flush; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 748 | return success; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 749 | } |
| 750 | ClassDataItemIterator it(dex_file, class_data); |
| 751 | SkipAllFields(it); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 752 | uint32_t class_method_index = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 753 | while (it.HasNextDirectMethod()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 754 | if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file, |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 755 | it.GetMemberIndex(), it.GetMethodCodeItem(), |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 756 | it.GetRawMemberAccessFlags(), &addr_found)) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 757 | success = false; |
| 758 | } |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 759 | if (addr_found) { |
| 760 | *stop_analysis = true; |
| 761 | return success; |
| 762 | } |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 763 | class_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 764 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 765 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 766 | while (it.HasNextVirtualMethod()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 767 | if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file, |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 768 | it.GetMemberIndex(), it.GetMethodCodeItem(), |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 769 | it.GetRawMemberAccessFlags(), &addr_found)) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 770 | success = false; |
| 771 | } |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 772 | if (addr_found) { |
| 773 | *stop_analysis = true; |
| 774 | return success; |
| 775 | } |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 776 | class_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 777 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 778 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 779 | DCHECK(!it.HasNext()); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 780 | vios->Stream() << std::flush; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 781 | return success; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 782 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 783 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 784 | static constexpr uint32_t kPrologueBytes = 16; |
| 785 | |
| 786 | // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes. |
| 787 | static constexpr uint32_t kMaxCodeSize = 100 * 1000; |
| 788 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 789 | bool DumpOatMethod(VariableIndentationOutputStream* vios, |
| 790 | const DexFile::ClassDef& class_def, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 791 | uint32_t class_method_index, |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 792 | const OatFile::OatClass& oat_class, const DexFile& dex_file, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 793 | uint32_t dex_method_idx, const DexFile::CodeItem* code_item, |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 794 | uint32_t method_access_flags, bool* addr_found) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 795 | bool success = true; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 796 | |
| 797 | // TODO: Support regex |
| 798 | std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx)); |
| 799 | if (method_name.find(options_.method_filter_) == std::string::npos) { |
Nicolas Geoffray | 3fcd220 | 2014-11-12 18:02:36 +0000 | [diff] [blame] | 800 | return success; |
| 801 | } |
| 802 | |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 803 | std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 804 | vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n", |
| 805 | class_method_index, pretty_method.c_str(), |
| 806 | dex_method_idx); |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 807 | if (options_.list_methods_) return success; |
| 808 | |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 809 | uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index); |
| 810 | const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index); |
| 811 | const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index); |
| 812 | uint32_t code_offset = oat_method.GetCodeOffset(); |
| 813 | uint32_t code_size = oat_method.GetQuickCodeSize(); |
| 814 | if (resolved_addr2instr_ != 0) { |
| 815 | if (resolved_addr2instr_ > code_offset + code_size) { |
| 816 | return success; |
| 817 | } else { |
| 818 | *addr_found = true; // stop analyzing file at next iteration |
| 819 | } |
| 820 | } |
| 821 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 822 | // Everything below is indented at least once. |
| 823 | ScopedIndentation indent1(vios); |
| 824 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 825 | { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 826 | vios->Stream() << "DEX CODE:\n"; |
| 827 | ScopedIndentation indent2(vios); |
| 828 | DumpDexCode(vios->Stream(), dex_file, code_item); |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 829 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 830 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 831 | std::unique_ptr<StackHandleScope<1>> hs; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 832 | std::unique_ptr<verifier::MethodVerifier> verifier; |
| 833 | if (Runtime::Current() != nullptr) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 834 | // We need to have the handle scope stay live until after the verifier since the verifier has |
| 835 | // a handle to the dex cache from hs. |
| 836 | hs.reset(new StackHandleScope<1>(Thread::Current())); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 837 | vios->Stream() << "VERIFIER TYPE ANALYSIS:\n"; |
| 838 | ScopedIndentation indent2(vios); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 839 | verifier.reset(DumpVerifier(vios, hs.get(), |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 840 | dex_method_idx, &dex_file, class_def, code_item, |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 841 | method_access_flags)); |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 842 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 843 | { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 844 | vios->Stream() << "OatMethodOffsets "; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 845 | if (options_.absolute_addresses_) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 846 | vios->Stream() << StringPrintf("%p ", oat_method_offsets); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 847 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 848 | vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 849 | if (oat_method_offsets_offset > oat_file_.Size()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 850 | vios->Stream() << StringPrintf( |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 851 | "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n", |
| 852 | oat_method_offsets_offset, oat_file_.Size()); |
| 853 | // If we can't read OatMethodOffsets, the rest of the data is dangerous to read. |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 854 | vios->Stream() << std::flush; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 855 | return false; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 856 | } |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 857 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 858 | ScopedIndentation indent2(vios); |
| 859 | vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 860 | uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset()); |
| 861 | if (aligned_code_begin > oat_file_.Size()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 862 | vios->Stream() << StringPrintf("WARNING: " |
| 863 | "code offset 0x%08x is past end of file 0x%08zx.\n", |
| 864 | aligned_code_begin, oat_file_.Size()); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 865 | success = false; |
| 866 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 867 | vios->Stream() << "\n"; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 868 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 869 | vios->Stream() << "gc_map: "; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 870 | if (options_.absolute_addresses_) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 871 | vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap()); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 872 | } |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 873 | uint32_t gc_map_offset = oat_method.GetGcMapOffset(); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 874 | vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 875 | if (gc_map_offset > oat_file_.Size()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 876 | vios->Stream() << StringPrintf("WARNING: " |
| 877 | "gc map table offset 0x%08x is past end of file 0x%08zx.\n", |
| 878 | gc_map_offset, oat_file_.Size()); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 879 | success = false; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 880 | } else if (options_.dump_raw_gc_map_) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 881 | ScopedIndentation indent3(vios); |
| 882 | DumpGcMap(vios->Stream(), oat_method, code_item); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 886 | vios->Stream() << "OatQuickMethodHeader "; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 887 | uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset(); |
| 888 | const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 889 | |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 890 | if (options_.absolute_addresses_) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 891 | vios->Stream() << StringPrintf("%p ", method_header); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 892 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 893 | vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 894 | if (method_header_offset > oat_file_.Size()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 895 | vios->Stream() << StringPrintf( |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 896 | "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n", |
| 897 | method_header_offset, oat_file_.Size()); |
| 898 | // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read. |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 899 | vios->Stream() << std::flush; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 900 | return false; |
| 901 | } |
| 902 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 903 | ScopedIndentation indent2(vios); |
| 904 | vios->Stream() << "mapping_table: "; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 905 | if (options_.absolute_addresses_) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 906 | vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable()); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 907 | } |
| 908 | uint32_t mapping_table_offset = oat_method.GetMappingTableOffset(); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 909 | vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset()); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 910 | if (mapping_table_offset > oat_file_.Size()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 911 | vios->Stream() << StringPrintf("WARNING: " |
| 912 | "mapping table offset 0x%08x is past end of file 0x%08zx. " |
| 913 | "mapping table offset was loaded from offset 0x%08x.\n", |
| 914 | mapping_table_offset, oat_file_.Size(), |
| 915 | oat_method.GetMappingTableOffsetOffset()); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 916 | success = false; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 917 | } else if (options_.dump_raw_mapping_table_) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 918 | ScopedIndentation indent3(vios); |
| 919 | DumpMappingTable(vios, oat_method); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 920 | } |
| 921 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 922 | vios->Stream() << "vmap_table: "; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 923 | if (options_.absolute_addresses_) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 924 | vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable()); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 925 | } |
| 926 | uint32_t vmap_table_offset = oat_method.GetVmapTableOffset(); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 927 | vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 928 | if (vmap_table_offset > oat_file_.Size()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 929 | vios->Stream() << StringPrintf("WARNING: " |
| 930 | "vmap table offset 0x%08x is past end of file 0x%08zx. " |
| 931 | "vmap table offset was loaded from offset 0x%08x.\n", |
| 932 | vmap_table_offset, oat_file_.Size(), |
| 933 | oat_method.GetVmapTableOffsetOffset()); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 934 | success = false; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 935 | } else if (options_.dump_vmap_) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 936 | DumpVmapData(vios, oat_method, code_item); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 937 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 938 | } |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 939 | { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 940 | vios->Stream() << "QuickMethodFrameInfo\n"; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 941 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 942 | ScopedIndentation indent2(vios); |
| 943 | vios->Stream() |
| 944 | << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes()); |
| 945 | vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask()); |
| 946 | DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false); |
| 947 | vios->Stream() << "\n"; |
| 948 | vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask()); |
| 949 | DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true); |
| 950 | vios->Stream() << "\n"; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 951 | } |
| 952 | { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 953 | // Based on spill masks from QuickMethodFrameInfo so placed |
| 954 | // after it is dumped, but useful for understanding quick |
| 955 | // code, so dumped here. |
| 956 | ScopedIndentation indent2(vios); |
| 957 | DumpVregLocations(vios->Stream(), oat_method, code_item); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 958 | } |
| 959 | { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 960 | vios->Stream() << "CODE: "; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 961 | uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset(); |
| 962 | if (code_size_offset > oat_file_.Size()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 963 | ScopedIndentation indent2(vios); |
| 964 | vios->Stream() << StringPrintf("WARNING: " |
| 965 | "code size offset 0x%08x is past end of file 0x%08zx.", |
| 966 | code_size_offset, oat_file_.Size()); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 967 | success = false; |
| 968 | } else { |
| 969 | const void* code = oat_method.GetQuickCode(); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 970 | uint32_t aligned_code_begin = AlignCodeOffset(code_offset); |
| 971 | uint64_t aligned_code_end = aligned_code_begin + code_size; |
| 972 | |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 973 | if (options_.absolute_addresses_) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 974 | vios->Stream() << StringPrintf("%p ", code); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 975 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 976 | vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n", |
| 977 | code_offset, |
| 978 | code_size_offset, |
| 979 | code_size, |
| 980 | code != nullptr ? "..." : ""); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 981 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 982 | ScopedIndentation indent2(vios); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 983 | if (aligned_code_begin > oat_file_.Size()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 984 | vios->Stream() << StringPrintf("WARNING: " |
| 985 | "start of code at 0x%08x is past end of file 0x%08zx.", |
| 986 | aligned_code_begin, oat_file_.Size()); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 987 | success = false; |
| 988 | } else if (aligned_code_end > oat_file_.Size()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 989 | vios->Stream() << StringPrintf( |
| 990 | "WARNING: " |
| 991 | "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. " |
| 992 | "code size is 0x%08x loaded from offset 0x%08x.\n", |
| 993 | aligned_code_end, oat_file_.Size(), |
| 994 | code_size, code_size_offset); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 995 | success = false; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 996 | if (options_.disassemble_code_) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 997 | if (code_size_offset + kPrologueBytes <= oat_file_.Size()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 998 | DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | } else if (code_size > kMaxCodeSize) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1002 | vios->Stream() << StringPrintf( |
| 1003 | "WARNING: " |
| 1004 | "code size %d is bigger than max expected threshold of %d. " |
| 1005 | "code size is 0x%08x loaded from offset 0x%08x.\n", |
| 1006 | code_size, kMaxCodeSize, |
| 1007 | code_size, code_size_offset); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1008 | success = false; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1009 | if (options_.disassemble_code_) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1010 | if (code_size_offset + kPrologueBytes <= oat_file_.Size()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1011 | DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1012 | } |
| 1013 | } |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1014 | } else if (options_.disassemble_code_) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1015 | DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1019 | vios->Stream() << std::flush; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1020 | return success; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1021 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 1022 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1023 | void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) { |
| 1024 | if (spill_mask == 0) { |
| 1025 | return; |
| 1026 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1027 | os << "("; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1028 | for (size_t i = 0; i < 32; i++) { |
| 1029 | if ((spill_mask & (1 << i)) != 0) { |
| 1030 | if (is_float) { |
| 1031 | os << "fr" << i; |
| 1032 | } else { |
| 1033 | os << "r" << i; |
| 1034 | } |
| 1035 | spill_mask ^= 1 << i; // clear bit |
| 1036 | if (spill_mask != 0) { |
| 1037 | os << ", "; |
| 1038 | } else { |
| 1039 | break; |
| 1040 | } |
| 1041 | } |
| 1042 | } |
| 1043 | os << ")"; |
| 1044 | } |
| 1045 | |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 1046 | // Display data stored at the the vmap offset of an oat method. |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1047 | void DumpVmapData(VariableIndentationOutputStream* vios, |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 1048 | const OatFile::OatMethod& oat_method, |
| 1049 | const DexFile::CodeItem* code_item) { |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1050 | if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) { |
| 1051 | // The optimizing compiler outputs its CodeInfo data in the vmap table. |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 1052 | const void* raw_code_info = oat_method.GetVmapTable(); |
| 1053 | if (raw_code_info != nullptr) { |
| 1054 | CodeInfo code_info(raw_code_info); |
| 1055 | DCHECK(code_item != nullptr); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1056 | ScopedIndentation indent1(vios); |
| 1057 | DumpCodeInfo(vios, code_info, oat_method, *code_item); |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 1058 | } |
Nicolas Geoffray | 0a5cd12 | 2015-07-16 14:15:05 +0100 | [diff] [blame] | 1059 | } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) { |
| 1060 | // We don't encode the size in the table, so just emit that we have quickened |
| 1061 | // information. |
| 1062 | ScopedIndentation indent(vios); |
| 1063 | vios->Stream() << "quickened data\n"; |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 1064 | } else { |
| 1065 | // Otherwise, display the vmap table. |
| 1066 | const uint8_t* raw_table = oat_method.GetVmapTable(); |
| 1067 | if (raw_table != nullptr) { |
| 1068 | VmapTable vmap_table(raw_table); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1069 | DumpVmapTable(vios->Stream(), oat_method, vmap_table); |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 1070 | } |
Nicolas Geoffray | 20d3eae | 2014-09-17 11:27:23 +0100 | [diff] [blame] | 1071 | } |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | // Display a CodeInfo object emitted by the optimizing compiler. |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1075 | void DumpCodeInfo(VariableIndentationOutputStream* vios, |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 1076 | const CodeInfo& code_info, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1077 | const OatFile::OatMethod& oat_method, |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 1078 | const DexFile::CodeItem& code_item) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1079 | code_info.Dump(vios, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1080 | oat_method.GetCodeOffset(), |
| 1081 | code_item.registers_size_, |
| 1082 | options_.dump_code_info_stack_maps_); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1083 | } |
| 1084 | |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 1085 | // Display a vmap table. |
| 1086 | void DumpVmapTable(std::ostream& os, |
| 1087 | const OatFile::OatMethod& oat_method, |
| 1088 | const VmapTable& vmap_table) { |
| 1089 | bool first = true; |
| 1090 | bool processing_fp = false; |
| 1091 | uint32_t spill_mask = oat_method.GetCoreSpillMask(); |
| 1092 | for (size_t i = 0; i < vmap_table.Size(); i++) { |
| 1093 | uint16_t dex_reg = vmap_table[i]; |
| 1094 | uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i, |
| 1095 | processing_fp ? kFloatVReg : kIntVReg); |
| 1096 | os << (first ? "v" : ", v") << dex_reg; |
| 1097 | if (!processing_fp) { |
| 1098 | os << "/r" << cpu_reg; |
| 1099 | } else { |
| 1100 | os << "/fr" << cpu_reg; |
| 1101 | } |
| 1102 | first = false; |
| 1103 | if (!processing_fp && dex_reg == 0xFFFF) { |
| 1104 | processing_fp = true; |
| 1105 | spill_mask = oat_method.GetFpSpillMask(); |
| 1106 | } |
| 1107 | } |
| 1108 | os << "\n"; |
| 1109 | } |
| 1110 | |
Razvan A Lupusoru | faf9f0d | 2014-08-29 17:56:46 -0700 | [diff] [blame] | 1111 | void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method, |
| 1112 | const DexFile::CodeItem* code_item) { |
| 1113 | if (code_item != nullptr) { |
| 1114 | size_t num_locals_ins = code_item->registers_size_; |
| 1115 | size_t num_ins = code_item->ins_size_; |
| 1116 | size_t num_locals = num_locals_ins - num_ins; |
| 1117 | size_t num_outs = code_item->outs_size_; |
| 1118 | |
| 1119 | os << "vr_stack_locations:"; |
| 1120 | for (size_t reg = 0; reg <= num_locals_ins; reg++) { |
| 1121 | // For readability, delimit the different kinds of VRs. |
| 1122 | if (reg == num_locals_ins) { |
| 1123 | os << "\n\tmethod*:"; |
| 1124 | } else if (reg == num_locals && num_ins > 0) { |
| 1125 | os << "\n\tins:"; |
| 1126 | } else if (reg == 0 && num_locals > 0) { |
| 1127 | os << "\n\tlocals:"; |
| 1128 | } |
| 1129 | |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 1130 | uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode( |
| 1131 | code_item, |
| 1132 | oat_method.GetCoreSpillMask(), |
| 1133 | oat_method.GetFpSpillMask(), |
| 1134 | oat_method.GetFrameSizeInBytes(), |
| 1135 | reg, |
| 1136 | GetInstructionSet()); |
Razvan A Lupusoru | faf9f0d | 2014-08-29 17:56:46 -0700 | [diff] [blame] | 1137 | os << " v" << reg << "[sp + #" << offset << "]"; |
| 1138 | } |
| 1139 | |
| 1140 | for (size_t out_reg = 0; out_reg < num_outs; out_reg++) { |
| 1141 | if (out_reg == 0) { |
| 1142 | os << "\n\touts:"; |
| 1143 | } |
| 1144 | |
| 1145 | uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet()); |
| 1146 | os << " v" << out_reg << "[sp + #" << offset << "]"; |
| 1147 | } |
| 1148 | |
| 1149 | os << "\n"; |
| 1150 | } |
| 1151 | } |
| 1152 | |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1153 | void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1154 | const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) { |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1155 | const uint8_t* raw_table = oat_method.GetVmapTable(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1156 | if (raw_table != nullptr) { |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1157 | const VmapTable vmap_table(raw_table); |
| 1158 | uint32_t vmap_offset; |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1159 | if (vmap_table.IsInContext(reg, kind, &vmap_offset)) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1160 | bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); |
| 1161 | uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask() |
| 1162 | : oat_method.GetCoreSpillMask(); |
| 1163 | 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] | 1164 | } else { |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 1165 | uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode( |
| 1166 | code_item, |
| 1167 | oat_method.GetCoreSpillMask(), |
| 1168 | oat_method.GetFpSpillMask(), |
| 1169 | oat_method.GetFrameSizeInBytes(), |
| 1170 | reg, |
| 1171 | GetInstructionSet()); |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1172 | os << "[sp + #" << offset << "]"; |
| 1173 | } |
| 1174 | } |
| 1175 | } |
| 1176 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1177 | void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method, |
| 1178 | const DexFile::CodeItem* code_item, |
| 1179 | size_t num_regs, const uint8_t* reg_bitmap) { |
| 1180 | bool first = true; |
| 1181 | for (size_t reg = 0; reg < num_regs; reg++) { |
| 1182 | if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) { |
| 1183 | if (first) { |
| 1184 | os << " v" << reg << " ("; |
| 1185 | DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg); |
| 1186 | os << ")"; |
| 1187 | first = false; |
| 1188 | } else { |
| 1189 | os << ", v" << reg << " ("; |
| 1190 | DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg); |
| 1191 | os << ")"; |
| 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | if (first) { |
| 1196 | os << "No registers in GC map\n"; |
| 1197 | } else { |
| 1198 | os << "\n"; |
| 1199 | } |
| 1200 | } |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1201 | void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method, |
| 1202 | const DexFile::CodeItem* code_item) { |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 1203 | const uint8_t* gc_map_raw = oat_method.GetGcMap(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1204 | if (gc_map_raw == nullptr) { |
| 1205 | return; // No GC map. |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1206 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1207 | const void* quick_code = oat_method.GetQuickCode(); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 1208 | NativePcOffsetToReferenceMap map(gc_map_raw); |
| 1209 | for (size_t entry = 0; entry < map.NumEntries(); entry++) { |
| 1210 | const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) + |
| 1211 | map.GetNativePcOffset(entry); |
| 1212 | os << StringPrintf("%p", native_pc); |
| 1213 | DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry)); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1214 | } |
| 1215 | } |
| 1216 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1217 | void DumpMappingTable(VariableIndentationOutputStream* vios, |
| 1218 | const OatFile::OatMethod& oat_method) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1219 | const void* quick_code = oat_method.GetQuickCode(); |
| 1220 | if (quick_code == nullptr) { |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 1221 | return; |
| 1222 | } |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1223 | MappingTable table(oat_method.GetMappingTable()); |
| 1224 | if (table.TotalSize() != 0) { |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1225 | if (table.PcToDexSize() != 0) { |
| 1226 | typedef MappingTable::PcToDexIterator It; |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1227 | vios->Stream() << "suspend point mappings {\n"; |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1228 | for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1229 | ScopedIndentation indent1(vios); |
| 1230 | vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc()); |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1231 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1232 | vios->Stream() << "}\n"; |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1233 | } |
| 1234 | if (table.DexToPcSize() != 0) { |
| 1235 | typedef MappingTable::DexToPcIterator It; |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1236 | vios->Stream() << "catch entry mappings {\n"; |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1237 | for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1238 | ScopedIndentation indent1(vios); |
| 1239 | vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc()); |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1240 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1241 | vios->Stream() << "}\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1242 | } |
| 1243 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1244 | } |
| 1245 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1246 | uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1247 | const OatFile::OatMethod& oat_method, |
| 1248 | const DexFile::CodeItem* code_item, |
| 1249 | size_t offset, |
| 1250 | bool suspend_point_mapping) { |
| 1251 | if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) { |
| 1252 | if (suspend_point_mapping) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1253 | ScopedIndentation indent1(vios); |
| 1254 | DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset); |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1255 | } |
| 1256 | // The return value is not used in the case of a method compiled |
| 1257 | // with the optimizing compiler. |
| 1258 | return DexFile::kDexNoIndex; |
| 1259 | } else { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1260 | return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping); |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1264 | uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method, |
| 1265 | size_t offset, bool suspend_point_mapping) { |
| 1266 | MappingTable table(oat_method.GetMappingTable()); |
| 1267 | if (suspend_point_mapping && table.PcToDexSize() > 0) { |
| 1268 | typedef MappingTable::PcToDexIterator It; |
| 1269 | for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) { |
| 1270 | if (offset == cur.NativePcOffset()) { |
Brian Carlstrom | 4b8c13e | 2013-08-23 18:10:32 -0700 | [diff] [blame] | 1271 | os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc()); |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1272 | return cur.DexPc(); |
| 1273 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 1274 | } |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1275 | } else if (!suspend_point_mapping && table.DexToPcSize() > 0) { |
| 1276 | typedef MappingTable::DexToPcIterator It; |
| 1277 | for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) { |
| 1278 | if (offset == cur.NativePcOffset()) { |
Brian Carlstrom | 4b8c13e | 2013-08-23 18:10:32 -0700 | [diff] [blame] | 1279 | os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc()); |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1280 | return cur.DexPc(); |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1281 | } |
| 1282 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 1283 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1284 | return DexFile::kDexNoIndex; |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1285 | } |
| 1286 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1287 | void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method, |
| 1288 | const DexFile::CodeItem* code_item, size_t native_pc_offset) { |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 1289 | const uint8_t* gc_map_raw = oat_method.GetGcMap(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1290 | if (gc_map_raw != nullptr) { |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1291 | NativePcOffsetToReferenceMap map(gc_map_raw); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1292 | if (map.HasEntry(native_pc_offset)) { |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1293 | size_t num_regs = map.RegWidth() * 8; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1294 | const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset); |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1295 | bool first = true; |
| 1296 | for (size_t reg = 0; reg < num_regs; reg++) { |
| 1297 | if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) { |
| 1298 | if (first) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1299 | os << "GC map objects: v" << reg << " ("; |
| 1300 | DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg); |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1301 | os << ")"; |
| 1302 | first = false; |
| 1303 | } else { |
| 1304 | os << ", v" << reg << " ("; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1305 | DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg); |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1306 | os << ")"; |
| 1307 | } |
| 1308 | } |
| 1309 | } |
| 1310 | if (!first) { |
| 1311 | os << "\n"; |
| 1312 | } |
| 1313 | } |
| 1314 | } |
| 1315 | } |
| 1316 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1317 | void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier, |
| 1318 | const OatFile::OatMethod& oat_method, |
| 1319 | const DexFile::CodeItem* code_item, uint32_t dex_pc) { |
| 1320 | DCHECK(verifier != nullptr); |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 1321 | std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1322 | bool first = true; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1323 | for (size_t reg = 0; reg < code_item->registers_size_; reg++) { |
| 1324 | VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2)); |
| 1325 | if (kind != kUndefined) { |
| 1326 | if (first) { |
| 1327 | os << "VRegs: v"; |
| 1328 | first = false; |
| 1329 | } else { |
| 1330 | os << ", v"; |
| 1331 | } |
| 1332 | os << reg << " ("; |
| 1333 | switch (kind) { |
| 1334 | case kImpreciseConstant: |
| 1335 | os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", "; |
| 1336 | DescribeVReg(os, oat_method, code_item, reg, kind); |
| 1337 | break; |
| 1338 | case kConstant: |
| 1339 | os << "Constant: " << kinds.at((reg * 2) + 1); |
| 1340 | break; |
| 1341 | default: |
| 1342 | DescribeVReg(os, oat_method, code_item, reg, kind); |
| 1343 | break; |
| 1344 | } |
| 1345 | os << ")"; |
| 1346 | } |
| 1347 | } |
| 1348 | if (!first) { |
| 1349 | os << "\n"; |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1354 | 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] | 1355 | if (code_item != nullptr) { |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1356 | size_t i = 0; |
| 1357 | while (i < code_item->insns_size_in_code_units_) { |
| 1358 | const Instruction* instruction = Instruction::At(&code_item->insns_[i]); |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1359 | os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5) |
| 1360 | << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str()); |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1361 | i += instruction->SizeInCodeUnits(); |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1366 | // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by |
| 1367 | // the optimizing compiler? |
| 1368 | static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method, |
| 1369 | const DexFile::CodeItem* code_item) { |
| 1370 | // If the native GC map is null and the Dex `code_item` is not |
| 1371 | // null, then this method has been compiled with the optimizing |
| 1372 | // compiler. |
Nicolas Geoffray | 0a5cd12 | 2015-07-16 14:15:05 +0100 | [diff] [blame] | 1373 | return oat_method.GetQuickCode() != nullptr && |
| 1374 | oat_method.GetGcMap() == nullptr && |
| 1375 | code_item != nullptr; |
| 1376 | } |
| 1377 | |
| 1378 | // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by |
| 1379 | // the dextodex compiler? |
| 1380 | static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method, |
| 1381 | const DexFile::CodeItem* code_item) { |
| 1382 | // If the quick code is null, the Dex `code_item` is not |
| 1383 | // null, and the vmap table is not null, then this method has been compiled |
| 1384 | // with the dextodex compiler. |
| 1385 | return oat_method.GetQuickCode() == nullptr && |
| 1386 | oat_method.GetVmapTable() != nullptr && |
| 1387 | code_item != nullptr; |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1388 | } |
| 1389 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1390 | void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1391 | const OatFile::OatMethod& oat_method, |
| 1392 | const DexFile::CodeItem* code_item, |
| 1393 | size_t offset) { |
| 1394 | // This method is only relevant for oat methods compiled with the |
| 1395 | // optimizing compiler. |
| 1396 | DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)); |
| 1397 | |
| 1398 | // The optimizing compiler outputs its CodeInfo data in the vmap table. |
| 1399 | const void* raw_code_info = oat_method.GetVmapTable(); |
| 1400 | if (raw_code_info != nullptr) { |
| 1401 | CodeInfo code_info(raw_code_info); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 1402 | StackMapEncoding encoding = code_info.ExtractEncoding(); |
| 1403 | StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding); |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1404 | if (stack_map.IsValid()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1405 | stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(), |
| 1406 | code_item->registers_size_); |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1411 | verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios, |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1412 | StackHandleScope<1>* hs, |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1413 | uint32_t dex_method_idx, |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1414 | const DexFile* dex_file, |
| 1415 | const DexFile::ClassDef& class_def, |
| 1416 | const DexFile::CodeItem* code_item, |
| 1417 | uint32_t method_access_flags) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1418 | if ((method_access_flags & kAccNative) == 0) { |
| 1419 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 1420 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1421 | Handle<mirror::DexCache> dex_cache( |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 1422 | hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, |
| 1423 | runtime->GetLinearAlloc()))); |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1424 | DCHECK(options_.class_loader_ != nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1425 | return verifier::MethodVerifier::VerifyMethodAndDump( |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1426 | soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_, |
| 1427 | &class_def, code_item, nullptr, method_access_flags); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1428 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1429 | |
| 1430 | return nullptr; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1431 | } |
| 1432 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1433 | void DumpCode(VariableIndentationOutputStream* vios, |
| 1434 | verifier::MethodVerifier* verifier, |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1435 | const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item, |
| 1436 | bool bad_input, size_t code_size) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1437 | const void* quick_code = oat_method.GetQuickCode(); |
| 1438 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1439 | if (code_size == 0) { |
| 1440 | code_size = oat_method.GetQuickCodeSize(); |
| 1441 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 1442 | if (code_size == 0 || quick_code == nullptr) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1443 | vios->Stream() << "NO CODE!\n"; |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1444 | return; |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 1445 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1446 | const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code); |
| 1447 | size_t offset = 0; |
| 1448 | while (offset < code_size) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1449 | if (!bad_input) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1450 | DumpInformationAtOffset(vios, oat_method, code_item, offset, false); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1451 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1452 | offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1453 | if (!bad_input) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1454 | uint32_t dex_pc = |
| 1455 | DumpInformationAtOffset(vios, oat_method, code_item, offset, true); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1456 | if (dex_pc != DexFile::kDexNoIndex) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1457 | DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1458 | if (verifier != nullptr) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1459 | DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1460 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1461 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1462 | } |
| 1463 | } |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1464 | } |
| 1465 | } |
| 1466 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1467 | const OatFile& oat_file_; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1468 | const std::vector<const OatFile::OatDexFile*> oat_dex_files_; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1469 | const OatDumperOptions& options_; |
| 1470 | uint32_t resolved_addr2instr_; |
Mathieu Chartier | a7dd038 | 2014-11-20 17:08:58 -0800 | [diff] [blame] | 1471 | InstructionSet instruction_set_; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1472 | std::set<uintptr_t> offsets_; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1473 | Disassembler* disassembler_; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1474 | }; |
| 1475 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1476 | class ImageDumper { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1477 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 1478 | ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space, |
| 1479 | const ImageHeader& image_header, OatDumperOptions* oat_dumper_options) |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1480 | : os_(os), |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1481 | vios_(os), |
| 1482 | indent1_(&vios_), |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1483 | image_space_(image_space), |
| 1484 | image_header_(image_header), |
| 1485 | oat_dumper_options_(oat_dumper_options) {} |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1486 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1487 | bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1488 | std::ostream& os = *os_; |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1489 | std::ostream& indent_os = vios_.Stream(); |
| 1490 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1491 | os << "MAGIC: " << image_header_.GetMagic() << "\n\n"; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1492 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1493 | os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1494 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1495 | os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n"; |
| 1496 | |
| 1497 | for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) { |
| 1498 | auto section = static_cast<ImageHeader::ImageSections>(i); |
| 1499 | os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n"; |
| 1500 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 1501 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1502 | os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum()); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1503 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1504 | os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1505 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1506 | os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n"; |
| 1507 | |
| 1508 | os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n"; |
| 1509 | |
| 1510 | os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1511 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1512 | os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n"; |
| 1513 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1514 | os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n"; |
| 1515 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1516 | { |
| 1517 | os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n"; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1518 | static_assert(arraysize(image_roots_descriptions_) == |
| 1519 | static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match"); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1520 | for (int i = 0; i < ImageHeader::kImageRootsMax; i++) { |
| 1521 | ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i); |
| 1522 | const char* image_root_description = image_roots_descriptions_[i]; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1523 | mirror::Object* image_root_object = image_header_.GetImageRoot(image_root); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1524 | indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1525 | if (image_root_object->IsObjectArray()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1526 | mirror::ObjectArray<mirror::Object>* image_root_object_array |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 1527 | = image_root_object->AsObjectArray<mirror::Object>(); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1528 | ScopedIndentation indent2(&vios_); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1529 | for (int j = 0; j < image_root_object_array->GetLength(); j++) { |
| 1530 | mirror::Object* value = image_root_object_array->Get(j); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 1531 | size_t run = 0; |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1532 | for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) { |
| 1533 | if (value == image_root_object_array->Get(k)) { |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 1534 | run++; |
| 1535 | } else { |
| 1536 | break; |
| 1537 | } |
| 1538 | } |
| 1539 | if (run == 0) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1540 | indent_os << StringPrintf("%d: ", j); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 1541 | } else { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1542 | indent_os << StringPrintf("%d to %zd: ", j, j + run); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1543 | j = j + run; |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 1544 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1545 | if (value != nullptr) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1546 | PrettyObjectValue(indent_os, value->GetClass(), value); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1547 | } else { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1548 | indent_os << j << ": null\n"; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1549 | } |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1550 | } |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 1551 | } |
| 1552 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1553 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1554 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1555 | { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1556 | os << "METHOD ROOTS\n"; |
| 1557 | static_assert(arraysize(image_methods_descriptions_) == |
| 1558 | static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match"); |
| 1559 | for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) { |
| 1560 | auto image_root = static_cast<ImageHeader::ImageMethod>(i); |
| 1561 | const char* description = image_methods_descriptions_[i]; |
| 1562 | auto* image_method = image_header_.GetImageMethod(image_root); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1563 | indent_os << StringPrintf("%s: %p\n", description, image_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1564 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1565 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1566 | os << "\n"; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1567 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1568 | Runtime* const runtime = Runtime::Current(); |
| 1569 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 1570 | std::string image_filename = image_space_.GetImageFilename(); |
| 1571 | std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1572 | os << "OAT LOCATION: " << oat_location; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1573 | os << "\n"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1574 | std::string error_msg; |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1575 | const OatFile* oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation( |
| 1576 | oat_location); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1577 | if (oat_file == nullptr) { |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 1578 | oat_file = OatFile::Open(oat_location, oat_location, |
| 1579 | nullptr, nullptr, false, nullptr, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 1580 | &error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1581 | if (oat_file == nullptr) { |
| 1582 | os << "NOT FOUND: " << error_msg << "\n"; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1583 | return false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1584 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1585 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1586 | os << "\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1587 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1588 | stats_.oat_file_bytes = oat_file->Size(); |
| 1589 | |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1590 | oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_)); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1591 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1592 | for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1593 | CHECK(oat_dex_file != nullptr); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1594 | stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(), |
| 1595 | oat_dex_file->FileSize())); |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 1596 | } |
| 1597 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1598 | os << "OBJECTS:\n" << std::flush; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1599 | |
| 1600 | // Loop through all the image spaces and dump their objects. |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1601 | gc::Heap* heap = runtime->GetHeap(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1602 | const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1603 | Thread* self = Thread::Current(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1604 | { |
Mathieu Chartier | c22c59e | 2014-02-24 15:16:06 -0800 | [diff] [blame] | 1605 | { |
| 1606 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 1607 | heap->FlushAllocStack(); |
| 1608 | } |
Hiroshi Yamauchi | 90d7068 | 2014-02-20 16:17:30 -0800 | [diff] [blame] | 1609 | // Since FlushAllocStack() above resets the (active) allocation |
| 1610 | // stack. Need to revoke the thread-local allocation stacks that |
| 1611 | // point into it. |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 1612 | ScopedThreadSuspension sts(self, kNative); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 1613 | ScopedSuspendAll ssa(__FUNCTION__); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 1614 | heap->RevokeAllThreadLocalAllocationStacks(self); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1615 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1616 | { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1617 | // Mark dex caches. |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1618 | dex_caches_.clear(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1619 | { |
| 1620 | ReaderMutexLock mu(self, *class_linker->DexLock()); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame^] | 1621 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1622 | mirror::DexCache* dex_cache = |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame^] | 1623 | down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1624 | if (dex_cache != nullptr) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1625 | dex_caches_.insert(dex_cache); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1626 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1627 | } |
| 1628 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1629 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1630 | for (const auto& space : spaces) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1631 | if (space->IsImageSpace()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1632 | auto* image_space = space->AsImageSpace(); |
| 1633 | // Dump the normal objects before ArtMethods. |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1634 | image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this); |
| 1635 | indent_os << "\n"; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1636 | // TODO: Dump fields. |
| 1637 | // Dump methods after. |
| 1638 | const auto& methods_section = image_header_.GetMethodsSection(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1639 | const size_t pointer_size = |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1640 | InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1641 | DumpArtMethodVisitor visitor(this); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1642 | methods_section.VisitPackedArtMethods(&visitor, image_space->Begin(), pointer_size); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1643 | } |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1644 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1645 | // Dump the large objects separately. |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 1646 | heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1647 | indent_os << "\n"; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1648 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1649 | os << "STATS:\n" << std::flush; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1650 | std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str())); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1651 | if (file.get() == nullptr) { |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 1652 | LOG(WARNING) << "Failed to find image in " << image_filename; |
Brian Carlstrom | 6f27775 | 2013-09-30 17:56:45 -0700 | [diff] [blame] | 1653 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1654 | if (file.get() != nullptr) { |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 1655 | stats_.file_bytes = file->GetLength(); |
Brian Carlstrom | 6f27775 | 2013-09-30 17:56:45 -0700 | [diff] [blame] | 1656 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1657 | size_t header_bytes = sizeof(ImageHeader); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1658 | const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap); |
| 1659 | const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields); |
| 1660 | const auto& method_section = image_header_.GetMethodsSection(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1661 | const auto& dex_cache_arrays_section = image_header_.GetImageSection( |
| 1662 | ImageHeader::kSectionDexCacheArrays); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1663 | const auto& intern_section = image_header_.GetImageSection( |
| 1664 | ImageHeader::kSectionInternedStrings); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1665 | stats_.header_bytes = header_bytes; |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1666 | stats_.alignment_bytes += RoundUp(header_bytes, kObjectAlignment) - header_bytes; |
| 1667 | // Add padding between the field and method section. |
| 1668 | // (Field section is 4-byte aligned, method section is 8-byte aligned on 64-bit targets.) |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1669 | stats_.alignment_bytes += method_section.Offset() - |
| 1670 | (field_section.Offset() + field_section.Size()); |
| 1671 | // Add padding between the dex cache arrays section and the intern table. (Dex cache |
| 1672 | // arrays section is 4-byte aligned on 32-bit targets, intern table is 8-byte aligned.) |
| 1673 | stats_.alignment_bytes += intern_section.Offset() - |
| 1674 | (dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1675 | stats_.alignment_bytes += bitmap_section.Offset() - image_header_.GetImageSize(); |
| 1676 | stats_.bitmap_bytes += bitmap_section.Size(); |
| 1677 | stats_.art_field_bytes += field_section.Size(); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1678 | stats_.art_method_bytes += method_section.Size(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1679 | stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size(); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1680 | stats_.interned_strings_bytes += intern_section.Size(); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1681 | stats_.Dump(os, indent_os); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1682 | os << "\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1683 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1684 | os << std::flush; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1685 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1686 | return oat_dumper_->Dump(os); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1687 | } |
| 1688 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1689 | private: |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1690 | class DumpArtMethodVisitor : public ArtMethodVisitor { |
| 1691 | public: |
| 1692 | explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {} |
| 1693 | |
| 1694 | virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1695 | std::ostream& indent_os = image_dumper_->vios_.Stream(); |
| 1696 | indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n"; |
| 1697 | image_dumper_->DumpMethod(method, image_dumper_, indent_os); |
| 1698 | indent_os << "\n"; |
| 1699 | } |
| 1700 | |
| 1701 | private: |
| 1702 | ImageDumper* const image_dumper_; |
| 1703 | }; |
| 1704 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1705 | static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1706 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1707 | CHECK(type != nullptr); |
| 1708 | if (value == nullptr) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1709 | os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1710 | } else if (type->IsStringClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1711 | mirror::String* string = value->AsString(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1712 | os << StringPrintf("%p String: %s\n", string, |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1713 | PrintableString(string->ToModifiedUtf8().c_str()).c_str()); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 1714 | } else if (type->IsClassClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1715 | mirror::Class* klass = value->AsClass(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1716 | os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1717 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1718 | os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1719 | } |
| 1720 | } |
| 1721 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1722 | static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1723 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1724 | os << StringPrintf("%s: ", field->GetName()); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 1725 | switch (field->GetTypeAsPrimitiveType()) { |
| 1726 | case Primitive::kPrimLong: |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1727 | os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj)); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 1728 | break; |
| 1729 | case Primitive::kPrimDouble: |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1730 | os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj)); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 1731 | break; |
| 1732 | case Primitive::kPrimFloat: |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1733 | os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj)); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 1734 | break; |
| 1735 | case Primitive::kPrimInt: |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1736 | os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj)); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 1737 | break; |
| 1738 | case Primitive::kPrimChar: |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 1739 | os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj)); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 1740 | break; |
| 1741 | case Primitive::kPrimShort: |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 1742 | os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj)); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 1743 | break; |
| 1744 | case Primitive::kPrimBoolean: |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 1745 | os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false", |
| 1746 | field->GetBoolean(obj)); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 1747 | break; |
| 1748 | case Primitive::kPrimByte: |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 1749 | os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj)); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 1750 | break; |
| 1751 | case Primitive::kPrimNot: { |
| 1752 | // Get the value, don't compute the type unless it is non-null as we don't want |
| 1753 | // to cause class loading. |
| 1754 | mirror::Object* value = field->GetObj(obj); |
| 1755 | if (value == nullptr) { |
| 1756 | os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str()); |
Ian Rogers | 50239c7 | 2013-06-17 14:53:22 -0700 | [diff] [blame] | 1757 | } else { |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 1758 | // Grab the field type without causing resolution. |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 1759 | mirror::Class* field_type = field->GetType<false>(); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 1760 | if (field_type != nullptr) { |
| 1761 | PrettyObjectValue(os, field_type, value); |
| 1762 | } else { |
| 1763 | os << StringPrintf("%p %s\n", value, |
| 1764 | PrettyDescriptor(field->GetTypeDescriptor()).c_str()); |
| 1765 | } |
Ian Rogers | 50239c7 | 2013-06-17 14:53:22 -0700 | [diff] [blame] | 1766 | } |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 1767 | break; |
Ian Rogers | 48efc2b | 2012-08-27 17:20:31 -0700 | [diff] [blame] | 1768 | } |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 1769 | default: |
| 1770 | os << "unexpected field type: " << field->GetTypeDescriptor() << "\n"; |
| 1771 | break; |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1772 | } |
| 1773 | } |
| 1774 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1775 | static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1776 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1777 | mirror::Class* super = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1778 | if (super != nullptr) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1779 | DumpFields(os, obj, super); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1780 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1781 | for (ArtField& field : klass->GetIFields()) { |
| 1782 | PrintField(os, &field, obj); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1783 | } |
| 1784 | } |
| 1785 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1786 | bool InDumpSpace(const mirror::Object* object) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1787 | return image_space_.Contains(object); |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 1788 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1789 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1790 | const void* GetQuickOatCodeBegin(ArtMethod* m) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1791 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | a7dd038 | 2014-11-20 17:08:58 -0800 | [diff] [blame] | 1792 | const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize( |
| 1793 | InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet())); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 1794 | if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1795 | quick_code = oat_dumper_->GetQuickOatCode(m); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1796 | } |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 1797 | if (oat_dumper_->GetInstructionSet() == kThumb2) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1798 | quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1); |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 1799 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1800 | return quick_code; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1801 | } |
| 1802 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1803 | uint32_t GetQuickOatCodeSize(ArtMethod* m) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1804 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1805 | const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m)); |
| 1806 | if (oat_code_begin == nullptr) { |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 1807 | return 0; |
| 1808 | } |
| 1809 | return oat_code_begin[-1]; |
| 1810 | } |
| 1811 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1812 | const void* GetQuickOatCodeEnd(ArtMethod* m) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1813 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1814 | 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] | 1815 | if (oat_code_begin == nullptr) { |
| 1816 | return nullptr; |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 1817 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1818 | return oat_code_begin + GetQuickOatCodeSize(m); |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1821 | static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1822 | DCHECK(obj != nullptr); |
| 1823 | DCHECK(arg != nullptr); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1824 | ImageDumper* state = reinterpret_cast<ImageDumper*>(arg); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1825 | if (!state->InDumpSpace(obj)) { |
| 1826 | return; |
| 1827 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1828 | |
| 1829 | size_t object_bytes = obj->SizeOf(); |
| 1830 | size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes; |
| 1831 | state->stats_.object_bytes += object_bytes; |
| 1832 | state->stats_.alignment_bytes += alignment_bytes; |
| 1833 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1834 | std::ostream& os = state->vios_.Stream(); |
| 1835 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1836 | mirror::Class* obj_class = obj->GetClass(); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1837 | if (obj_class->IsArrayClass()) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1838 | os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(), |
| 1839 | obj->AsArray()->GetLength()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1840 | } else if (obj->IsClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1841 | mirror::Class* klass = obj->AsClass(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1842 | os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str()) |
| 1843 | << klass->GetStatus() << ")\n"; |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1844 | } else if (obj_class->IsStringClass()) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1845 | os << StringPrintf("%p: java.lang.String %s\n", obj, |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 1846 | PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1847 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1848 | os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1849 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1850 | ScopedIndentation indent1(&state->vios_); |
| 1851 | DumpFields(os, obj, obj_class); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1852 | const auto image_pointer_size = |
| 1853 | InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1854 | if (obj->IsObjectArray()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1855 | auto* obj_array = obj->AsObjectArray<mirror::Object>(); |
| 1856 | for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1857 | mirror::Object* value = obj_array->Get(i); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1858 | size_t run = 0; |
| 1859 | for (int32_t j = i + 1; j < length; j++) { |
| 1860 | if (value == obj_array->Get(j)) { |
| 1861 | run++; |
| 1862 | } else { |
| 1863 | break; |
| 1864 | } |
| 1865 | } |
| 1866 | if (run == 0) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1867 | os << StringPrintf("%d: ", i); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1868 | } else { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1869 | os << StringPrintf("%d to %zd: ", i, i + run); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1870 | i = i + run; |
| 1871 | } |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 1872 | mirror::Class* value_class = |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1873 | (value == nullptr) ? obj_class->GetComponentType() : value->GetClass(); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1874 | PrettyObjectValue(os, value_class, value); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1875 | } |
| 1876 | } else if (obj->IsClass()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1877 | mirror::Class* klass = obj->AsClass(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1878 | if (klass->NumStaticFields() != 0) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1879 | os << "STATICS:\n"; |
| 1880 | ScopedIndentation indent2(&state->vios_); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1881 | for (ArtField& field : klass->GetSFields()) { |
| 1882 | PrintField(os, &field, field.GetDeclaringClass()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 1883 | } |
| 1884 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1885 | } else { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1886 | auto it = state->dex_caches_.find(obj); |
| 1887 | if (it != state->dex_caches_.end()) { |
| 1888 | auto* dex_cache = down_cast<mirror::DexCache*>(obj); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1889 | const auto& field_section = state->image_header_.GetImageSection( |
| 1890 | ImageHeader::kSectionArtFields); |
| 1891 | const auto& method_section = state->image_header_.GetMethodsSection(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1892 | size_t num_methods = dex_cache->NumResolvedMethods(); |
| 1893 | if (num_methods != 0u) { |
| 1894 | os << "Methods (size=" << num_methods << "):"; |
| 1895 | ScopedIndentation indent2(&state->vios_); |
| 1896 | auto* resolved_methods = dex_cache->GetResolvedMethods(); |
| 1897 | for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) { |
| 1898 | auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods, i, image_pointer_size); |
| 1899 | size_t run = 0; |
| 1900 | for (size_t j = i + 1; |
| 1901 | j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods, |
| 1902 | j, |
| 1903 | image_pointer_size); |
| 1904 | ++j, ++run) {} |
| 1905 | if (run == 0) { |
| 1906 | os << StringPrintf("%zd: ", i); |
| 1907 | } else { |
| 1908 | os << StringPrintf("%zd to %zd: ", i, i + run); |
| 1909 | i = i + run; |
| 1910 | } |
| 1911 | std::string msg; |
| 1912 | if (elem == nullptr) { |
| 1913 | msg = "null"; |
| 1914 | } else if (method_section.Contains( |
| 1915 | reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) { |
| 1916 | msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem)); |
| 1917 | } else { |
| 1918 | msg = "<not in method section>"; |
| 1919 | } |
| 1920 | os << StringPrintf("%p %s\n", elem, msg.c_str()); |
Ian Rogers | 0d2d378 | 2012-04-10 11:09:18 -0700 | [diff] [blame] | 1921 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1922 | } |
| 1923 | size_t num_fields = dex_cache->NumResolvedFields(); |
| 1924 | if (num_fields != 0u) { |
| 1925 | os << "Fields (size=" << num_fields << "):"; |
| 1926 | ScopedIndentation indent2(&state->vios_); |
| 1927 | auto* resolved_fields = dex_cache->GetResolvedFields(); |
| 1928 | for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) { |
| 1929 | auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size); |
| 1930 | size_t run = 0; |
| 1931 | for (size_t j = i + 1; |
| 1932 | j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields, |
| 1933 | j, |
| 1934 | image_pointer_size); |
| 1935 | ++j, ++run) {} |
| 1936 | if (run == 0) { |
| 1937 | os << StringPrintf("%zd: ", i); |
| 1938 | } else { |
| 1939 | os << StringPrintf("%zd to %zd: ", i, i + run); |
| 1940 | i = i + run; |
| 1941 | } |
| 1942 | std::string msg; |
| 1943 | if (elem == nullptr) { |
| 1944 | msg = "null"; |
| 1945 | } else if (field_section.Contains( |
| 1946 | reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) { |
| 1947 | msg = PrettyField(reinterpret_cast<ArtField*>(elem)); |
| 1948 | } else { |
| 1949 | msg = "<not in field section>"; |
| 1950 | } |
| 1951 | os << StringPrintf("%p %s\n", elem, msg.c_str()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1952 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1953 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1954 | } |
| 1955 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1956 | std::string temp; |
| 1957 | state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1958 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1959 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1960 | void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1961 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1962 | DCHECK(method != nullptr); |
| 1963 | const auto image_pointer_size = |
| 1964 | InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet()); |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 1965 | const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method); |
| 1966 | const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 1967 | OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>( |
| 1968 | reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1969 | if (method->IsNative()) { |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 1970 | if (!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(quick_oat_code_begin)) { |
| 1971 | DCHECK(method_header->GetNativeGcMap() == nullptr) << PrettyMethod(method); |
| 1972 | DCHECK(method_header->GetMappingTable() == nullptr) << PrettyMethod(method); |
| 1973 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1974 | bool first_occurrence; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1975 | uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method); |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 1976 | state->ComputeOatSize(quick_oat_code_begin, &first_occurrence); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1977 | if (first_occurrence) { |
| 1978 | state->stats_.native_to_managed_code_bytes += quick_oat_code_size; |
| 1979 | } |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 1980 | if (quick_oat_code_begin != |
| 1981 | method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) { |
| 1982 | indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1983 | } |
| 1984 | } else if (method->IsAbstract() || method->IsCalleeSaveMethod() || |
| 1985 | method->IsResolutionMethod() || method->IsImtConflictMethod() || |
| 1986 | method->IsImtUnimplementedMethod() || method->IsClassInitializer()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1987 | } else { |
| 1988 | const DexFile::CodeItem* code_item = method->GetCodeItem(); |
| 1989 | size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2; |
| 1990 | state->stats_.dex_instruction_bytes += dex_instruction_bytes; |
| 1991 | |
| 1992 | bool first_occurrence; |
| 1993 | size_t gc_map_bytes = state->ComputeOatSize( |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 1994 | method_header->GetNativeGcMap(), &first_occurrence); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1995 | if (first_occurrence) { |
| 1996 | state->stats_.gc_map_bytes += gc_map_bytes; |
| 1997 | } |
| 1998 | |
| 1999 | size_t pc_mapping_table_bytes = state->ComputeOatSize( |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 2000 | method_header->GetMappingTable(), &first_occurrence); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2001 | if (first_occurrence) { |
| 2002 | state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes; |
| 2003 | } |
| 2004 | |
Roland Levillain | 6d7f179 | 2015-07-02 10:59:15 +0100 | [diff] [blame] | 2005 | size_t vmap_table_bytes = 0u; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 2006 | if (!method_header->IsOptimized()) { |
Roland Levillain | 6d7f179 | 2015-07-02 10:59:15 +0100 | [diff] [blame] | 2007 | // Method compiled with the optimizing compiler have no vmap table. |
| 2008 | vmap_table_bytes = state->ComputeOatSize( |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 2009 | method_header->GetVmapTable(), &first_occurrence); |
Roland Levillain | 6d7f179 | 2015-07-02 10:59:15 +0100 | [diff] [blame] | 2010 | if (first_occurrence) { |
| 2011 | state->stats_.vmap_table_bytes += vmap_table_bytes; |
| 2012 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2013 | } |
| 2014 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2015 | uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method); |
| 2016 | state->ComputeOatSize(quick_oat_code_begin, &first_occurrence); |
| 2017 | if (first_occurrence) { |
| 2018 | state->stats_.managed_code_bytes += quick_oat_code_size; |
| 2019 | if (method->IsConstructor()) { |
| 2020 | if (method->IsStatic()) { |
| 2021 | state->stats_.class_initializer_code_bytes += quick_oat_code_size; |
| 2022 | } else if (dex_instruction_bytes > kLargeConstructorDexBytes) { |
| 2023 | state->stats_.large_initializer_code_bytes += quick_oat_code_size; |
| 2024 | } |
| 2025 | } else if (dex_instruction_bytes > kLargeMethodDexBytes) { |
| 2026 | state->stats_.large_method_code_bytes += quick_oat_code_size; |
| 2027 | } |
| 2028 | } |
| 2029 | state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size; |
| 2030 | |
Igor Murashkin | 7617abd | 2015-07-10 18:27:47 -0700 | [diff] [blame] | 2031 | uint32_t method_access_flags = method->GetAccessFlags(); |
| 2032 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2033 | indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end); |
Igor Murashkin | 7617abd | 2015-07-10 18:27:47 -0700 | [diff] [blame] | 2034 | indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n", |
| 2035 | dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes, |
| 2036 | method_access_flags); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2037 | |
| 2038 | size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes + |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 2039 | vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_pointer_size); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2040 | |
| 2041 | double expansion = |
| 2042 | static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes); |
| 2043 | state->stats_.ComputeOutliers(total_size, expansion, method); |
| 2044 | } |
| 2045 | } |
| 2046 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2047 | std::set<const void*> already_seen_; |
| 2048 | // Compute the size of the given data within the oat file and whether this is the first time |
| 2049 | // this data has been requested |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 2050 | size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2051 | if (already_seen_.count(oat_data) == 0) { |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 2052 | *first_occurrence = true; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2053 | already_seen_.insert(oat_data); |
| 2054 | } else { |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 2055 | *first_occurrence = false; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2056 | } |
| 2057 | return oat_dumper_->ComputeSize(oat_data); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2058 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2059 | |
| 2060 | public: |
| 2061 | struct Stats { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2062 | size_t oat_file_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2063 | size_t file_bytes; |
| 2064 | |
| 2065 | size_t header_bytes; |
| 2066 | size_t object_bytes; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2067 | size_t art_field_bytes; |
| 2068 | size_t art_method_bytes; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2069 | size_t dex_cache_arrays_bytes; |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 2070 | size_t interned_strings_bytes; |
Mathieu Chartier | 3232709 | 2013-08-30 14:04:08 -0700 | [diff] [blame] | 2071 | size_t bitmap_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2072 | size_t alignment_bytes; |
| 2073 | |
| 2074 | size_t managed_code_bytes; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2075 | size_t managed_code_bytes_ignoring_deduplication; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2076 | size_t managed_to_native_code_bytes; |
| 2077 | size_t native_to_managed_code_bytes; |
Ian Rogers | 0d2d378 | 2012-04-10 11:09:18 -0700 | [diff] [blame] | 2078 | size_t class_initializer_code_bytes; |
| 2079 | size_t large_initializer_code_bytes; |
| 2080 | size_t large_method_code_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2081 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2082 | size_t gc_map_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2083 | size_t pc_mapping_table_bytes; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2084 | size_t vmap_table_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2085 | |
| 2086 | size_t dex_instruction_bytes; |
| 2087 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2088 | std::vector<ArtMethod*> method_outlier; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2089 | std::vector<size_t> method_outlier_size; |
| 2090 | std::vector<double> method_outlier_expansion; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2091 | std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2092 | |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 2093 | Stats() |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2094 | : oat_file_bytes(0), |
| 2095 | file_bytes(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2096 | header_bytes(0), |
| 2097 | object_bytes(0), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2098 | art_field_bytes(0), |
| 2099 | art_method_bytes(0), |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2100 | dex_cache_arrays_bytes(0), |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 2101 | interned_strings_bytes(0), |
Mathieu Chartier | 3232709 | 2013-08-30 14:04:08 -0700 | [diff] [blame] | 2102 | bitmap_bytes(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2103 | alignment_bytes(0), |
| 2104 | managed_code_bytes(0), |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2105 | managed_code_bytes_ignoring_deduplication(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2106 | managed_to_native_code_bytes(0), |
| 2107 | native_to_managed_code_bytes(0), |
Ian Rogers | 0d2d378 | 2012-04-10 11:09:18 -0700 | [diff] [blame] | 2108 | class_initializer_code_bytes(0), |
| 2109 | large_initializer_code_bytes(0), |
| 2110 | large_method_code_bytes(0), |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2111 | gc_map_bytes(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2112 | pc_mapping_table_bytes(0), |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2113 | vmap_table_bytes(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2114 | dex_instruction_bytes(0) {} |
| 2115 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 2116 | struct SizeAndCount { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2117 | SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {} |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 2118 | size_t bytes; |
| 2119 | size_t count; |
| 2120 | }; |
| 2121 | typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable; |
| 2122 | SizeAndCountTable sizes_and_counts; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2123 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2124 | void Update(const char* descriptor, size_t object_bytes_in) { |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 2125 | SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor); |
| 2126 | if (it != sizes_and_counts.end()) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2127 | it->second.bytes += object_bytes_in; |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 2128 | it->second.count += 1; |
| 2129 | } else { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2130 | sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1)); |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 2131 | } |
| 2132 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2133 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2134 | double PercentOfOatBytes(size_t size) { |
| 2135 | return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100; |
| 2136 | } |
| 2137 | |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2138 | double PercentOfFileBytes(size_t size) { |
| 2139 | return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100; |
| 2140 | } |
| 2141 | |
| 2142 | double PercentOfObjectBytes(size_t size) { |
| 2143 | return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100; |
| 2144 | } |
| 2145 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2146 | void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2147 | method_outlier_size.push_back(total_size); |
| 2148 | method_outlier_expansion.push_back(expansion); |
| 2149 | method_outlier.push_back(method); |
| 2150 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2151 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2152 | void DumpOutliers(std::ostream& os) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 2153 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2154 | size_t sum_of_sizes = 0; |
| 2155 | size_t sum_of_sizes_squared = 0; |
| 2156 | size_t sum_of_expansion = 0; |
| 2157 | size_t sum_of_expansion_squared = 0; |
| 2158 | size_t n = method_outlier_size.size(); |
| 2159 | for (size_t i = 0; i < n; i++) { |
| 2160 | size_t cur_size = method_outlier_size[i]; |
| 2161 | sum_of_sizes += cur_size; |
| 2162 | sum_of_sizes_squared += cur_size * cur_size; |
| 2163 | double cur_expansion = method_outlier_expansion[i]; |
| 2164 | sum_of_expansion += cur_expansion; |
| 2165 | sum_of_expansion_squared += cur_expansion * cur_expansion; |
| 2166 | } |
| 2167 | size_t size_mean = sum_of_sizes / n; |
| 2168 | size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1); |
| 2169 | double expansion_mean = sum_of_expansion / n; |
| 2170 | double expansion_variance = |
| 2171 | (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1); |
| 2172 | |
| 2173 | // Dump methods whose size is a certain number of standard deviations from the mean |
| 2174 | size_t dumped_values = 0; |
| 2175 | size_t skipped_values = 0; |
| 2176 | for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations |
| 2177 | size_t cur_size_variance = i * i * size_variance; |
| 2178 | bool first = true; |
| 2179 | for (size_t j = 0; j < n; j++) { |
| 2180 | size_t cur_size = method_outlier_size[j]; |
| 2181 | if (cur_size > size_mean) { |
| 2182 | size_t cur_var = cur_size - size_mean; |
| 2183 | cur_var = cur_var * cur_var; |
| 2184 | if (cur_var > cur_size_variance) { |
| 2185 | if (dumped_values > 20) { |
| 2186 | if (i == 1) { |
| 2187 | skipped_values++; |
| 2188 | } else { |
| 2189 | i = 2; // jump to counting for 1 standard deviation |
| 2190 | break; |
| 2191 | } |
| 2192 | } else { |
| 2193 | if (first) { |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2194 | os << "\nBig methods (size > " << i << " standard deviations the norm):\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2195 | first = false; |
| 2196 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2197 | os << PrettyMethod(method_outlier[j]) << " requires storage of " |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2198 | << PrettySize(cur_size) << "\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2199 | method_outlier_size[j] = 0; // don't consider this method again |
| 2200 | dumped_values++; |
| 2201 | } |
| 2202 | } |
| 2203 | } |
| 2204 | } |
| 2205 | } |
| 2206 | if (skipped_values > 0) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2207 | os << "... skipped " << skipped_values |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2208 | << " methods with size > 1 standard deviation from the norm\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2209 | } |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2210 | os << std::flush; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2211 | |
| 2212 | // Dump methods whose expansion is a certain number of standard deviations from the mean |
| 2213 | dumped_values = 0; |
| 2214 | skipped_values = 0; |
| 2215 | for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations |
| 2216 | double cur_expansion_variance = i * i * expansion_variance; |
| 2217 | bool first = true; |
| 2218 | for (size_t j = 0; j < n; j++) { |
| 2219 | double cur_expansion = method_outlier_expansion[j]; |
| 2220 | if (cur_expansion > expansion_mean) { |
| 2221 | size_t cur_var = cur_expansion - expansion_mean; |
| 2222 | cur_var = cur_var * cur_var; |
| 2223 | if (cur_var > cur_expansion_variance) { |
| 2224 | if (dumped_values > 20) { |
| 2225 | if (i == 1) { |
| 2226 | skipped_values++; |
| 2227 | } else { |
| 2228 | i = 2; // jump to counting for 1 standard deviation |
| 2229 | break; |
| 2230 | } |
| 2231 | } else { |
| 2232 | if (first) { |
| 2233 | os << "\nLarge expansion methods (size > " << i |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2234 | << " standard deviations the norm):\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2235 | first = false; |
| 2236 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2237 | os << PrettyMethod(method_outlier[j]) << " expanded code by " |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2238 | << cur_expansion << "\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2239 | method_outlier_expansion[j] = 0.0; // don't consider this method again |
| 2240 | dumped_values++; |
| 2241 | } |
| 2242 | } |
| 2243 | } |
| 2244 | } |
| 2245 | } |
| 2246 | if (skipped_values > 0) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2247 | os << "... skipped " << skipped_values |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2248 | << " methods with expansion > 1 standard deviation from the norm\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2249 | } |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2250 | os << "\n" << std::flush; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2251 | } |
| 2252 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2253 | void Dump(std::ostream& os, std::ostream& indent_os) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 2254 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2255 | { |
| 2256 | os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n" |
| 2257 | << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n"; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2258 | indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n" |
| 2259 | "object_bytes = %8zd (%2.0f%% of art file bytes)\n" |
| 2260 | "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n" |
| 2261 | "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n" |
| 2262 | "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n" |
| 2263 | "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n" |
| 2264 | "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n" |
| 2265 | "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n", |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2266 | header_bytes, PercentOfFileBytes(header_bytes), |
| 2267 | object_bytes, PercentOfFileBytes(object_bytes), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2268 | art_field_bytes, PercentOfFileBytes(art_field_bytes), |
| 2269 | art_method_bytes, PercentOfFileBytes(art_method_bytes), |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2270 | dex_cache_arrays_bytes, |
| 2271 | PercentOfFileBytes(dex_cache_arrays_bytes), |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 2272 | interned_strings_bytes, |
| 2273 | PercentOfFileBytes(interned_strings_bytes), |
Mathieu Chartier | 3232709 | 2013-08-30 14:04:08 -0700 | [diff] [blame] | 2274 | bitmap_bytes, PercentOfFileBytes(bitmap_bytes), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2275 | alignment_bytes, PercentOfFileBytes(alignment_bytes)) |
| 2276 | << std::flush; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2277 | CHECK_EQ(file_bytes, header_bytes + object_bytes + art_field_bytes + art_method_bytes + |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2278 | dex_cache_arrays_bytes + interned_strings_bytes + bitmap_bytes + alignment_bytes); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2279 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2280 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2281 | os << "object_bytes breakdown:\n"; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2282 | size_t object_bytes_total = 0; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 2283 | for (const auto& sizes_and_count : sizes_and_counts) { |
| 2284 | const std::string& descriptor(sizes_and_count.first); |
| 2285 | double average = static_cast<double>(sizes_and_count.second.bytes) / |
| 2286 | static_cast<double>(sizes_and_count.second.count); |
| 2287 | double percent = PercentOfObjectBytes(sizes_and_count.second.bytes); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2288 | os << StringPrintf("%32s %8zd bytes %6zd instances " |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 2289 | "(%4.0f bytes/instance) %2.0f%% of object_bytes\n", |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 2290 | descriptor.c_str(), sizes_and_count.second.bytes, |
| 2291 | sizes_and_count.second.count, average, percent); |
| 2292 | object_bytes_total += sizes_and_count.second.bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2293 | } |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2294 | os << "\n" << std::flush; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2295 | CHECK_EQ(object_bytes, object_bytes_total); |
| 2296 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2297 | os << StringPrintf("oat_file_bytes = %8zd\n" |
| 2298 | "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" |
| 2299 | "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" |
| 2300 | "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n" |
| 2301 | "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" |
| 2302 | "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" |
| 2303 | "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] | 2304 | oat_file_bytes, |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 2305 | managed_code_bytes, |
| 2306 | PercentOfOatBytes(managed_code_bytes), |
| 2307 | managed_to_native_code_bytes, |
| 2308 | PercentOfOatBytes(managed_to_native_code_bytes), |
| 2309 | native_to_managed_code_bytes, |
| 2310 | PercentOfOatBytes(native_to_managed_code_bytes), |
| 2311 | class_initializer_code_bytes, |
| 2312 | PercentOfOatBytes(class_initializer_code_bytes), |
| 2313 | large_initializer_code_bytes, |
| 2314 | PercentOfOatBytes(large_initializer_code_bytes), |
| 2315 | large_method_code_bytes, |
| 2316 | PercentOfOatBytes(large_method_code_bytes)) |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2317 | << "DexFile sizes:\n"; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 2318 | 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] | 2319 | os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n", |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 2320 | oat_dex_file_size.first.c_str(), oat_dex_file_size.second, |
| 2321 | PercentOfOatBytes(oat_dex_file_size.second)); |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 2322 | } |
| 2323 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2324 | os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n" |
| 2325 | "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n" |
| 2326 | "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n", |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 2327 | gc_map_bytes, PercentOfOatBytes(gc_map_bytes), |
| 2328 | pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes), |
| 2329 | vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes)) |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2330 | << std::flush; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2331 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2332 | os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes) |
| 2333 | << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n", |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 2334 | static_cast<double>(managed_code_bytes) / |
| 2335 | static_cast<double>(dex_instruction_bytes), |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2336 | static_cast<double>(managed_code_bytes_ignoring_deduplication) / |
Elliott Hughes | cf44e6f | 2012-05-24 19:42:18 -0700 | [diff] [blame] | 2337 | static_cast<double>(dex_instruction_bytes)) |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2338 | << std::flush; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2339 | |
| 2340 | DumpOutliers(os); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2341 | } |
| 2342 | } stats_; |
| 2343 | |
| 2344 | private: |
Ian Rogers | 0d2d378 | 2012-04-10 11:09:18 -0700 | [diff] [blame] | 2345 | enum { |
| 2346 | // Number of bytes for a constructor to be considered large. Based on the 1000 basic block |
| 2347 | // threshold, we assume 2 bytes per instruction and 2 instructions per block. |
| 2348 | kLargeConstructorDexBytes = 4000, |
| 2349 | // Number of bytes for a method to be considered large. Based on the 4000 basic block |
| 2350 | // threshold, we assume 2 bytes per instruction and 2 instructions per block. |
| 2351 | kLargeMethodDexBytes = 16000 |
| 2352 | }; |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2353 | |
| 2354 | // For performance, use the *os_ directly for anything that doesn't need indentation |
| 2355 | // and prepare an indentation stream with default indentation 1. |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2356 | std::ostream* os_; |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2357 | VariableIndentationOutputStream vios_; |
| 2358 | ScopedIndentation indent1_; |
| 2359 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2360 | gc::space::ImageSpace& image_space_; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2361 | const ImageHeader& image_header_; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 2362 | std::unique_ptr<OatDumper> oat_dumper_; |
Andreas Gampe | df2bb1f | 2015-05-04 18:25:23 -0700 | [diff] [blame] | 2363 | OatDumperOptions* oat_dumper_options_; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2364 | std::set<mirror::Object*> dex_caches_; |
Elliott Hughes | d1bb4f6 | 2011-09-23 14:09:45 -0700 | [diff] [blame] | 2365 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2366 | DISALLOW_COPY_AND_ASSIGN(ImageDumper); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 2367 | }; |
| 2368 | |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2369 | static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options, |
| 2370 | std::ostream* os) { |
| 2371 | // Dumping the image, no explicit class loader. |
| 2372 | NullHandle<mirror::ClassLoader> null_class_loader; |
| 2373 | options->class_loader_ = &null_class_loader; |
| 2374 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2375 | ScopedObjectAccess soa(Thread::Current()); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2376 | gc::Heap* heap = runtime->GetHeap(); |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 2377 | gc::space::ImageSpace* image_space = heap->GetBootImageSpace(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2378 | CHECK(image_space != nullptr); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 2379 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 2380 | if (!image_header.IsValid()) { |
Brian Carlstrom | 0f5baa0 | 2014-05-22 11:54:18 -0700 | [diff] [blame] | 2381 | fprintf(stderr, "Invalid image header %s\n", image_location); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 2382 | return EXIT_FAILURE; |
| 2383 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2384 | |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2385 | ImageDumper image_dumper(os, *image_space, image_header, options); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2386 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 2387 | bool success = image_dumper.Dump(); |
| 2388 | return (success) ? EXIT_SUCCESS : EXIT_FAILURE; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 2389 | } |
| 2390 | |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2391 | static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options, |
| 2392 | std::ostream* os) { |
| 2393 | CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr); |
| 2394 | |
| 2395 | Thread* self = Thread::Current(); |
| 2396 | CHECK(self != nullptr); |
| 2397 | // Need well-known-classes. |
| 2398 | WellKnownClasses::Init(self->GetJniEnv()); |
| 2399 | |
| 2400 | // Need to register dex files to get a working dex cache. |
| 2401 | ScopedObjectAccess soa(self); |
| 2402 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 2403 | runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file)); |
Mathieu Chartier | ac8f439 | 2015-08-27 13:54:20 -0700 | [diff] [blame] | 2404 | std::vector<const DexFile*> class_path; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2405 | for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) { |
| 2406 | std::string error_msg; |
Mathieu Chartier | ac8f439 | 2015-08-27 13:54:20 -0700 | [diff] [blame] | 2407 | const DexFile* const dex_file = OpenDexFile(odf, &error_msg); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2408 | CHECK(dex_file != nullptr) << error_msg; |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 2409 | class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc()); |
Mathieu Chartier | ac8f439 | 2015-08-27 13:54:20 -0700 | [diff] [blame] | 2410 | class_path.push_back(dex_file); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2411 | } |
| 2412 | |
| 2413 | // Need a class loader. |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2414 | // Fake that we're a compiler. |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 2415 | jobject class_loader = class_linker->CreatePathClassLoader(self, class_path); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2416 | |
| 2417 | // Use the class loader while dumping. |
| 2418 | StackHandleScope<1> scope(self); |
| 2419 | Handle<mirror::ClassLoader> loader_handle = scope.NewHandle( |
| 2420 | soa.Decode<mirror::ClassLoader*>(class_loader)); |
| 2421 | options->class_loader_ = &loader_handle; |
| 2422 | |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 2423 | OatDumper oat_dumper(*oat_file, *options); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2424 | bool success = oat_dumper.Dump(*os); |
| 2425 | return (success) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 2426 | } |
| 2427 | |
| 2428 | static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) { |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 2429 | CHECK(oat_file != nullptr && options != nullptr); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2430 | // No image = no class loader. |
| 2431 | NullHandle<mirror::ClassLoader> null_class_loader; |
| 2432 | options->class_loader_ = &null_class_loader; |
| 2433 | |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 2434 | OatDumper oat_dumper(*oat_file, *options); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2435 | bool success = oat_dumper.Dump(*os); |
| 2436 | return (success) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 2437 | } |
| 2438 | |
| 2439 | static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options, |
| 2440 | std::ostream* os) { |
| 2441 | std::string error_msg; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2442 | OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2443 | nullptr, &error_msg); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2444 | if (oat_file == nullptr) { |
| 2445 | fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str()); |
| 2446 | return EXIT_FAILURE; |
| 2447 | } |
| 2448 | |
| 2449 | if (runtime != nullptr) { |
| 2450 | return DumpOatWithRuntime(runtime, oat_file, options, os); |
| 2451 | } else { |
| 2452 | return DumpOatWithoutRuntime(oat_file, options, os); |
| 2453 | } |
| 2454 | } |
| 2455 | |
| 2456 | static int SymbolizeOat(const char* oat_filename, std::string& output_name) { |
| 2457 | std::string error_msg; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2458 | OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2459 | nullptr, &error_msg); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2460 | if (oat_file == nullptr) { |
| 2461 | fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str()); |
| 2462 | return EXIT_FAILURE; |
| 2463 | } |
| 2464 | |
| 2465 | OatSymbolizer oat_symbolizer(oat_file, output_name); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2466 | if (!oat_symbolizer.Symbolize()) { |
| 2467 | fprintf(stderr, "Failed to symbolize\n"); |
| 2468 | return EXIT_FAILURE; |
| 2469 | } |
| 2470 | |
| 2471 | return EXIT_SUCCESS; |
| 2472 | } |
| 2473 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2474 | struct OatdumpArgs : public CmdlineArgs { |
| 2475 | protected: |
| 2476 | using Base = CmdlineArgs; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2477 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2478 | virtual ParseStatus ParseCustom(const StringPiece& option, |
| 2479 | std::string* error_msg) OVERRIDE { |
| 2480 | { |
| 2481 | ParseStatus base_parse = Base::ParseCustom(option, error_msg); |
| 2482 | if (base_parse != kParseUnknownArgument) { |
| 2483 | return base_parse; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2484 | } |
| 2485 | } |
| 2486 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2487 | if (option.starts_with("--oat-file=")) { |
| 2488 | oat_filename_ = option.substr(strlen("--oat-file=")).data(); |
| 2489 | } else if (option.starts_with("--image=")) { |
| 2490 | image_location_ = option.substr(strlen("--image=")).data(); |
| 2491 | } else if (option =="--dump:raw_mapping_table") { |
| 2492 | dump_raw_mapping_table_ = true; |
| 2493 | } else if (option == "--dump:raw_gc_map") { |
| 2494 | dump_raw_gc_map_ = true; |
| 2495 | } else if (option == "--no-dump:vmap") { |
| 2496 | dump_vmap_ = false; |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 2497 | } else if (option =="--dump:code_info_stack_maps") { |
| 2498 | dump_code_info_stack_maps_ = true; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2499 | } else if (option == "--no-disassemble") { |
| 2500 | disassemble_code_ = false; |
| 2501 | } else if (option.starts_with("--symbolize=")) { |
| 2502 | oat_filename_ = option.substr(strlen("--symbolize=")).data(); |
| 2503 | symbolize_ = true; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 2504 | } else if (option.starts_with("--class-filter=")) { |
| 2505 | class_filter_ = option.substr(strlen("--class-filter=")).data(); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2506 | } else if (option.starts_with("--method-filter=")) { |
| 2507 | method_filter_ = option.substr(strlen("--method-filter=")).data(); |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 2508 | } else if (option.starts_with("--list-classes")) { |
| 2509 | list_classes_ = true; |
| 2510 | } else if (option.starts_with("--list-methods")) { |
| 2511 | list_methods_ = true; |
| 2512 | } else if (option.starts_with("--export-dex-to=")) { |
| 2513 | export_dex_location_ = option.substr(strlen("--export-dex-to=")).data(); |
| 2514 | } else if (option.starts_with("--addr2instr=")) { |
| 2515 | if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) { |
| 2516 | *error_msg = "Address conversion failed"; |
| 2517 | return kParseError; |
| 2518 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2519 | } else { |
| 2520 | return kParseUnknownArgument; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2521 | } |
| 2522 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2523 | return kParseOk; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2524 | } |
| 2525 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2526 | virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE { |
| 2527 | // Infer boot image location from the image location if possible. |
| 2528 | if (boot_image_location_ == nullptr) { |
| 2529 | boot_image_location_ = image_location_; |
| 2530 | } |
| 2531 | |
| 2532 | // Perform the parent checks. |
| 2533 | ParseStatus parent_checks = Base::ParseChecks(error_msg); |
| 2534 | if (parent_checks != kParseOk) { |
| 2535 | return parent_checks; |
| 2536 | } |
| 2537 | |
| 2538 | // Perform our own checks. |
| 2539 | if (image_location_ == nullptr && oat_filename_ == nullptr) { |
| 2540 | *error_msg = "Either --image or --oat-file must be specified"; |
| 2541 | return kParseError; |
| 2542 | } else if (image_location_ != nullptr && oat_filename_ != nullptr) { |
| 2543 | *error_msg = "Either --image or --oat-file must be specified but not both"; |
| 2544 | return kParseError; |
| 2545 | } |
| 2546 | |
| 2547 | return kParseOk; |
| 2548 | } |
| 2549 | |
| 2550 | virtual std::string GetUsage() const { |
| 2551 | std::string usage; |
| 2552 | |
| 2553 | usage += |
| 2554 | "Usage: oatdump [options] ...\n" |
| 2555 | " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n" |
| 2556 | " Example: adb shell oatdump --image=/system/framework/boot.art\n" |
| 2557 | "\n" |
| 2558 | // Either oat-file or image is required. |
| 2559 | " --oat-file=<file.oat>: specifies an input oat filename.\n" |
| 2560 | " Example: --oat-file=/system/framework/boot.oat\n" |
| 2561 | "\n" |
| 2562 | " --image=<file.art>: specifies an input image location.\n" |
| 2563 | " Example: --image=/system/framework/boot.art\n" |
| 2564 | "\n"; |
| 2565 | |
| 2566 | usage += Base::GetUsage(); |
| 2567 | |
| 2568 | usage += // Optional. |
| 2569 | " --dump:raw_mapping_table enables dumping of the mapping table.\n" |
| 2570 | " Example: --dump:raw_mapping_table\n" |
| 2571 | "\n" |
Mathieu Chartier | 19510f0 | 2015-05-26 14:44:35 -0700 | [diff] [blame] | 2572 | " --dump:raw_gc_map enables dumping of the GC map.\n" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2573 | " Example: --dump:raw_gc_map\n" |
| 2574 | "\n" |
| 2575 | " --no-dump:vmap may be used to disable vmap dumping.\n" |
| 2576 | " Example: --no-dump:vmap\n" |
| 2577 | "\n" |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 2578 | " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n" |
| 2579 | " Example: --dump:code_info_stack_maps\n" |
| 2580 | "\n" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2581 | " --no-disassemble may be used to disable disassembly.\n" |
| 2582 | " Example: --no-disassemble\n" |
| 2583 | "\n" |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 2584 | " --list-classes may be used to list target file classes (can be used with filters).\n" |
| 2585 | " Example: --list-classes\n" |
| 2586 | " Example: --list-classes --class-filter=com.example.foo\n" |
| 2587 | "\n" |
| 2588 | " --list-methods may be used to list target file methods (can be used with filters).\n" |
| 2589 | " Example: --list-methods\n" |
| 2590 | " Example: --list-methods --class-filter=com.example --method-filter=foo\n" |
| 2591 | "\n" |
| 2592 | " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n" |
| 2593 | " Example: --symbolize=/system/framework/boot.oat\n" |
| 2594 | "\n" |
| 2595 | " --class-filter=<class name>: only dumps classes that contain the filter.\n" |
| 2596 | " Example: --class-filter=com.example.foo\n" |
| 2597 | "\n" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2598 | " --method-filter=<method name>: only dumps methods that contain the filter.\n" |
| 2599 | " Example: --method-filter=foo\n" |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 2600 | "\n" |
| 2601 | " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n" |
| 2602 | " Example: --export-dex-to=/data/local/tmp\n" |
| 2603 | "\n" |
| 2604 | " --addr2instr=<address>: output matching method disassembled code from relative\n" |
| 2605 | " address (e.g. PC from crash dump)\n" |
| 2606 | " Example: --addr2instr=0x00001a3b\n" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2607 | "\n"; |
| 2608 | |
| 2609 | return usage; |
| 2610 | } |
| 2611 | |
| 2612 | public: |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2613 | const char* oat_filename_ = nullptr; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 2614 | const char* class_filter_ = ""; |
Nicolas Geoffray | 3fcd220 | 2014-11-12 18:02:36 +0000 | [diff] [blame] | 2615 | const char* method_filter_ = ""; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2616 | const char* image_location_ = nullptr; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2617 | std::string elf_filename_prefix_; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2618 | bool dump_raw_mapping_table_ = false; |
| 2619 | bool dump_raw_gc_map_ = false; |
| 2620 | bool dump_vmap_ = true; |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 2621 | bool dump_code_info_stack_maps_ = false; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2622 | bool disassemble_code_ = true; |
| 2623 | bool symbolize_ = false; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 2624 | bool list_classes_ = false; |
| 2625 | bool list_methods_ = false; |
| 2626 | uint32_t addr2instr_ = 0; |
| 2627 | const char* export_dex_location_ = nullptr; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2628 | }; |
| 2629 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2630 | struct OatdumpMain : public CmdlineMain<OatdumpArgs> { |
| 2631 | virtual bool NeedsRuntime() OVERRIDE { |
| 2632 | CHECK(args_ != nullptr); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2633 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2634 | // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping. |
| 2635 | bool absolute_addresses = (args_->oat_filename_ == nullptr); |
| 2636 | |
| 2637 | oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions( |
| 2638 | args_->dump_raw_mapping_table_, |
| 2639 | args_->dump_raw_gc_map_, |
| 2640 | args_->dump_vmap_, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 2641 | args_->dump_code_info_stack_maps_, |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2642 | args_->disassemble_code_, |
| 2643 | absolute_addresses, |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 2644 | args_->class_filter_, |
| 2645 | args_->method_filter_, |
| 2646 | args_->list_classes_, |
| 2647 | args_->list_methods_, |
| 2648 | args_->export_dex_location_, |
| 2649 | args_->addr2instr_)); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2650 | |
| 2651 | return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) && |
| 2652 | !args_->symbolize_; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2653 | } |
| 2654 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2655 | virtual bool ExecuteWithoutRuntime() OVERRIDE { |
| 2656 | CHECK(args_ != nullptr); |
Andreas Gampe | c24f399 | 2014-12-17 20:40:11 -0800 | [diff] [blame] | 2657 | CHECK(args_->oat_filename_ != nullptr); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2658 | |
Mathieu Chartier | d424d08 | 2014-10-15 10:31:46 -0700 | [diff] [blame] | 2659 | MemMap::Init(); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2660 | |
Andreas Gampe | c24f399 | 2014-12-17 20:40:11 -0800 | [diff] [blame] | 2661 | if (args_->symbolize_) { |
| 2662 | return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS; |
| 2663 | } else { |
| 2664 | return DumpOat(nullptr, |
| 2665 | args_->oat_filename_, |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 2666 | oat_dumper_options_.get(), |
Andreas Gampe | c24f399 | 2014-12-17 20:40:11 -0800 | [diff] [blame] | 2667 | args_->os_) == EXIT_SUCCESS; |
| 2668 | } |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2669 | } |
| 2670 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2671 | virtual bool ExecuteWithRuntime(Runtime* runtime) { |
| 2672 | CHECK(args_ != nullptr); |
| 2673 | |
| 2674 | if (args_->oat_filename_ != nullptr) { |
| 2675 | return DumpOat(runtime, |
| 2676 | args_->oat_filename_, |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 2677 | oat_dumper_options_.get(), |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2678 | args_->os_) == EXIT_SUCCESS; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2679 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2680 | |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 2681 | return DumpImage(runtime, args_->image_location_, oat_dumper_options_.get(), args_->os_) |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2682 | == EXIT_SUCCESS; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2683 | } |
| 2684 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2685 | std::unique_ptr<OatDumperOptions> oat_dumper_options_; |
| 2686 | }; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2687 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 2688 | } // namespace art |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 2689 | |
| 2690 | int main(int argc, char** argv) { |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 2691 | art::OatdumpMain main; |
| 2692 | return main.Main(argc, argv); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 2693 | } |