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