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