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