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