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