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