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