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