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