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