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