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