Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 20 | #include <fstream> |
| 21 | #include <iostream> |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 22 | #include <map> |
| 23 | #include <set> |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 24 | #include <string> |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 25 | #include <unordered_map> |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 26 | #include <unordered_set> |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 29 | #include "android-base/logging.h" |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 30 | #include "android-base/stringprintf.h" |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 31 | #include "android-base/strings.h" |
| 32 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 33 | #include "arch/instruction_set_features.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 34 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 35 | #include "art_method-inl.h" |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 36 | #include "base/bit_utils_iterator.h" |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 37 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 38 | #include "base/unix_file/fd_file.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | #include "class_linker-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 40 | #include "class_linker.h" |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 41 | #include "compiled_method.h" |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 42 | #include "debug/debug_info.h" |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 43 | #include "debug/elf_debug_writer.h" |
| 44 | #include "debug/method_debug_info.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 45 | #include "dex/code_item_accessors-inl.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame^] | 46 | #include "dex/descriptors_names.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 47 | #include "dex/dex_file-inl.h" |
| 48 | #include "dex/dex_instruction-inl.h" |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 49 | #include "disassembler.h" |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 50 | #include "gc/accounting/space_bitmap-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 51 | #include "gc/space/image_space.h" |
| 52 | #include "gc/space/large_object_space.h" |
| 53 | #include "gc/space/space-inl.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 54 | #include "image-inl.h" |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 55 | #include "imtable-inl.h" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 56 | #include "indenter.h" |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 57 | #include "subtype_check.h" |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 58 | #include "index_bss_mapping.h" |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 59 | #include "interpreter/unstarted_runtime.h" |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 60 | #include "linker/buffered_output_stream.h" |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 61 | #include "linker/elf_builder.h" |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 62 | #include "linker/file_output_stream.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 63 | #include "mirror/array-inl.h" |
| 64 | #include "mirror/class-inl.h" |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 65 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 66 | #include "mirror/object-inl.h" |
| 67 | #include "mirror/object_array-inl.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 68 | #include "oat.h" |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 69 | #include "oat_file-inl.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 70 | #include "oat_file_manager.h" |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 71 | #include "os.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 72 | #include "safe_map.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 73 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 74 | #include "stack.h" |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 75 | #include "stack_map.h" |
| 76 | #include "string_reference.h" |
Mathieu Chartier | c22c59e | 2014-02-24 15:16:06 -0800 | [diff] [blame] | 77 | #include "thread_list.h" |
Andreas Gampe | 2ba8895 | 2016-04-29 17:52:07 -0700 | [diff] [blame] | 78 | #include "type_lookup_table.h" |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 79 | #include "vdex_file.h" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 80 | #include "verifier/method_verifier.h" |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 81 | #include "verifier/verifier_deps.h" |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 82 | #include "well_known_classes.h" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 83 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 84 | #include <sys/stat.h> |
| 85 | #include "cmdline.h" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 86 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 87 | namespace art { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 88 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 89 | using android::base::StringPrintf; |
| 90 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 91 | const char* image_methods_descriptions_[] = { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 92 | "kResolutionMethod", |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 93 | "kImtConflictMethod", |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 94 | "kImtUnimplementedMethod", |
Vladimir Marko | fd36f1f | 2016-08-03 18:49:58 +0100 | [diff] [blame] | 95 | "kSaveAllCalleeSavesMethod", |
| 96 | "kSaveRefsOnlyMethod", |
| 97 | "kSaveRefsAndArgsMethod", |
Vladimir Marko | 952dbb1 | 2016-07-28 12:01:51 +0100 | [diff] [blame] | 98 | "kSaveEverythingMethod", |
Mingyao Yang | 0a87a65 | 2017-04-12 13:43:15 -0700 | [diff] [blame] | 99 | "kSaveEverythingMethodForClinit", |
| 100 | "kSaveEverythingMethodForSuspendCheck", |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | const char* image_roots_descriptions_[] = { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 104 | "kDexCaches", |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 105 | "kClassRoots", |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 106 | "kClassLoader", |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
Mathieu Chartier | ac8f439 | 2015-08-27 13:54:20 -0700 | [diff] [blame] | 109 | // Map is so that we don't allocate multiple dex files for the same OatDexFile. |
| 110 | static std::map<const OatFile::OatDexFile*, |
| 111 | std::unique_ptr<const DexFile>> opened_dex_files; |
| 112 | |
| 113 | const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) { |
| 114 | DCHECK(oat_dex_file != nullptr); |
| 115 | auto it = opened_dex_files.find(oat_dex_file); |
| 116 | if (it != opened_dex_files.end()) { |
| 117 | return it->second.get(); |
| 118 | } |
| 119 | const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release(); |
| 120 | opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret)); |
| 121 | return ret; |
| 122 | } |
| 123 | |
Andreas Gampe | 2d8614b | 2016-03-07 16:31:34 -0800 | [diff] [blame] | 124 | template <typename ElfTypes> |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 125 | class OatSymbolizer FINAL { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 126 | public: |
David Srbecky | 2fdd03c | 2016-03-10 15:32:37 +0000 | [diff] [blame] | 127 | OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) : |
| 128 | oat_file_(oat_file), |
| 129 | builder_(nullptr), |
| 130 | output_name_(output_name.empty() ? "symbolized.oat" : output_name), |
| 131 | no_bits_(no_bits) { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 134 | bool Symbolize() { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 135 | const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet(); |
Andreas Gampe | 0415b4e | 2015-01-06 15:17:07 -0800 | [diff] [blame] | 136 | std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap( |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 137 | isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap()); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 138 | |
Andreas Gampe | 08c277c | 2017-04-26 22:22:15 -0700 | [diff] [blame] | 139 | std::unique_ptr<File> elf_file(OS::CreateEmptyFile(output_name_.c_str())); |
| 140 | if (elf_file == nullptr) { |
| 141 | return false; |
| 142 | } |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 143 | std::unique_ptr<linker::BufferedOutputStream> output_stream = |
| 144 | std::make_unique<linker::BufferedOutputStream>( |
| 145 | std::make_unique<linker::FileOutputStream>(elf_file.get())); |
| 146 | builder_.reset(new linker::ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get())); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 147 | |
| 148 | builder_->Start(); |
| 149 | |
| 150 | auto* rodata = builder_->GetRoData(); |
| 151 | auto* text = builder_->GetText(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 152 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 153 | const uint8_t* rodata_begin = oat_file_->Begin(); |
| 154 | const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset(); |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 155 | if (!no_bits_) { |
David Srbecky | 2fdd03c | 2016-03-10 15:32:37 +0000 | [diff] [blame] | 156 | rodata->Start(); |
| 157 | rodata->WriteFully(rodata_begin, rodata_size); |
| 158 | rodata->End(); |
| 159 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 160 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 161 | const uint8_t* text_begin = oat_file_->Begin() + rodata_size; |
| 162 | const size_t text_size = oat_file_->End() - text_begin; |
David Srbecky | e155f4b | 2017-12-06 15:18:38 +0000 | [diff] [blame] | 163 | if (!no_bits_) { |
David Srbecky | 2fdd03c | 2016-03-10 15:32:37 +0000 | [diff] [blame] | 164 | text->Start(); |
| 165 | text->WriteFully(text_begin, text_size); |
| 166 | text->End(); |
| 167 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 168 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 169 | if (isa == InstructionSet::kMips || isa == InstructionSet::kMips64) { |
Douglas Leung | 316a218 | 2015-09-17 15:26:25 -0700 | [diff] [blame] | 170 | builder_->WriteMIPSabiflagsSection(); |
| 171 | } |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 172 | builder_->PrepareDynamicSection(elf_file->GetPath(), |
| 173 | rodata_size, |
| 174 | text_size, |
| 175 | oat_file_->BssSize(), |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 176 | oat_file_->BssMethodsOffset(), |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 177 | oat_file_->BssRootsOffset(), |
| 178 | oat_file_->VdexSize()); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 179 | builder_->WriteDynamicSection(); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 180 | |
Vladimir Marko | 1b404a8 | 2017-09-01 13:35:26 +0100 | [diff] [blame] | 181 | const OatHeader& oat_header = oat_file_->GetOatHeader(); |
| 182 | #define DO_TRAMPOLINE(fn_name) \ |
| 183 | if (oat_header.Get ## fn_name ## Offset() != 0) { \ |
| 184 | debug::MethodDebugInfo info = {}; \ |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 185 | info.custom_name = #fn_name; \ |
Vladimir Marko | 1b404a8 | 2017-09-01 13:35:26 +0100 | [diff] [blame] | 186 | info.isa = oat_header.GetInstructionSet(); \ |
| 187 | info.is_code_address_text_relative = true; \ |
| 188 | size_t code_offset = oat_header.Get ## fn_name ## Offset(); \ |
| 189 | code_offset -= CompiledCode::CodeDelta(oat_header.GetInstructionSet()); \ |
| 190 | info.code_address = code_offset - oat_header.GetExecutableOffset(); \ |
| 191 | info.code_size = 0; /* The symbol lasts until the next symbol. */ \ |
| 192 | method_debug_infos_.push_back(std::move(info)); \ |
| 193 | } |
| 194 | DO_TRAMPOLINE(InterpreterToInterpreterBridge) |
| 195 | DO_TRAMPOLINE(InterpreterToCompiledCodeBridge) |
| 196 | DO_TRAMPOLINE(JniDlsymLookup); |
| 197 | DO_TRAMPOLINE(QuickGenericJniTrampoline); |
| 198 | DO_TRAMPOLINE(QuickImtConflictTrampoline); |
| 199 | DO_TRAMPOLINE(QuickResolutionTrampoline); |
| 200 | DO_TRAMPOLINE(QuickToInterpreterBridge); |
| 201 | #undef DO_TRAMPOLINE |
| 202 | |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 203 | Walk(); |
Vladimir Marko | 1b404a8 | 2017-09-01 13:35:26 +0100 | [diff] [blame] | 204 | |
| 205 | // TODO: Try to symbolize link-time thunks? |
| 206 | // This would require disassembling all methods to find branches outside the method code. |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 207 | |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 208 | // TODO: Add symbols for dex bytecode in the .dex section. |
| 209 | |
| 210 | debug::DebugInfo debug_info{}; |
| 211 | debug_info.compiled_methods = ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_); |
| 212 | |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 213 | debug::WriteDebugInfo(builder_.get(), |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 214 | debug_info, |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 215 | dwarf::DW_DEBUG_FRAME_FORMAT, |
| 216 | true /* write_oat_patches */); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 217 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 218 | builder_->End(); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 219 | |
Andreas Gampe | 08c277c | 2017-04-26 22:22:15 -0700 | [diff] [blame] | 220 | bool ret_value = builder_->Good(); |
| 221 | |
| 222 | builder_.reset(); |
| 223 | output_stream.reset(); |
| 224 | |
| 225 | if (elf_file->FlushCloseOrErase() != 0) { |
| 226 | return false; |
| 227 | } |
| 228 | elf_file.reset(); |
| 229 | |
| 230 | return ret_value; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 231 | } |
| 232 | |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 233 | void Walk() { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 234 | std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles(); |
| 235 | for (size_t i = 0; i < oat_dex_files.size(); i++) { |
| 236 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i]; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 237 | CHECK(oat_dex_file != nullptr); |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 238 | WalkOatDexFile(oat_dex_file); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 242 | void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 243 | std::string error_msg; |
Mathieu Chartier | ac8f439 | 2015-08-27 13:54:20 -0700 | [diff] [blame] | 244 | const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg); |
| 245 | if (dex_file == nullptr) { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 246 | return; |
| 247 | } |
| 248 | for (size_t class_def_index = 0; |
| 249 | class_def_index < dex_file->NumClassDefs(); |
| 250 | class_def_index++) { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 251 | const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index); |
| 252 | OatClassType type = oat_class.GetType(); |
| 253 | switch (type) { |
| 254 | case kOatClassAllCompiled: |
| 255 | case kOatClassSomeCompiled: |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 256 | WalkOatClass(oat_class, *dex_file, class_def_index); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 257 | break; |
| 258 | |
| 259 | case kOatClassNoneCompiled: |
| 260 | case kOatClassMax: |
| 261 | // Ignore. |
| 262 | break; |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 267 | void WalkOatClass(const OatFile::OatClass& oat_class, |
| 268 | const DexFile& dex_file, |
| 269 | uint32_t class_def_index) { |
| 270 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 271 | const uint8_t* class_data = dex_file.GetClassData(class_def); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 272 | if (class_data == nullptr) { // empty class such as a marker interface? |
| 273 | return; |
| 274 | } |
| 275 | // Note: even if this is an interface or a native class, we still have to walk it, as there |
| 276 | // might be a static initializer. |
| 277 | ClassDataItemIterator it(dex_file, class_data); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 278 | uint32_t class_method_idx = 0; |
Mathieu Chartier | e17cf24 | 2017-06-19 11:05:51 -0700 | [diff] [blame] | 279 | it.SkipAllFields(); |
Mathieu Chartier | b7c273c | 2017-11-10 18:07:56 -0800 | [diff] [blame] | 280 | for (; it.HasNextMethod(); it.Next()) { |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 281 | WalkOatMethod(oat_class.GetOatMethod(class_method_idx++), |
| 282 | dex_file, |
| 283 | class_def_index, |
| 284 | it.GetMemberIndex(), |
| 285 | it.GetMethodCodeItem(), |
| 286 | it.GetMethodAccessFlags()); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 287 | } |
| 288 | DCHECK(!it.HasNext()); |
| 289 | } |
| 290 | |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 291 | void WalkOatMethod(const OatFile::OatMethod& oat_method, |
| 292 | const DexFile& dex_file, |
| 293 | uint32_t class_def_index, |
| 294 | uint32_t dex_method_index, |
| 295 | const DexFile::CodeItem* code_item, |
| 296 | uint32_t method_access_flags) { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 297 | if ((method_access_flags & kAccAbstract) != 0) { |
| 298 | // Abstract method, no code. |
| 299 | return; |
| 300 | } |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 301 | const OatHeader& oat_header = oat_file_->GetOatHeader(); |
| 302 | const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader(); |
| 303 | if (method_header == nullptr || method_header->GetCodeSize() == 0) { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 304 | // No code. |
| 305 | return; |
| 306 | } |
| 307 | |
David Srbecky | a1b4c5f | 2016-03-31 18:17:59 +0100 | [diff] [blame] | 308 | uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset(); |
| 309 | // Clear Thumb2 bit. |
| 310 | const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point)); |
| 311 | |
Vladimir Marko | 1b404a8 | 2017-09-01 13:35:26 +0100 | [diff] [blame] | 312 | debug::MethodDebugInfo info = {}; |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 313 | DCHECK(info.custom_name.empty()); |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 314 | info.dex_file = &dex_file; |
| 315 | info.class_def_index = class_def_index; |
| 316 | info.dex_method_index = dex_method_index; |
| 317 | info.access_flags = method_access_flags; |
| 318 | info.code_item = code_item; |
| 319 | info.isa = oat_header.GetInstructionSet(); |
| 320 | info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second; |
| 321 | info.is_native_debuggable = oat_header.IsNativeDebuggable(); |
| 322 | info.is_optimized = method_header->IsOptimized(); |
| 323 | info.is_code_address_text_relative = true; |
David Srbecky | a1b4c5f | 2016-03-31 18:17:59 +0100 | [diff] [blame] | 324 | info.code_address = reinterpret_cast<uintptr_t>(code_address); |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 325 | info.code_size = method_header->GetCodeSize(); |
| 326 | info.frame_size_in_bytes = method_header->GetFrameSizeInBytes(); |
| 327 | info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr; |
| 328 | info.cfi = ArrayRef<uint8_t>(); |
| 329 | method_debug_infos_.push_back(info); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 332 | private: |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 333 | const OatFile* oat_file_; |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 334 | std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder_; |
David Srbecky | 5d95076 | 2016-03-07 20:47:29 +0000 | [diff] [blame] | 335 | std::vector<debug::MethodDebugInfo> method_debug_infos_; |
| 336 | std::unordered_set<uint32_t> seen_offsets_; |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 337 | const std::string output_name_; |
David Srbecky | 2fdd03c | 2016-03-10 15:32:37 +0000 | [diff] [blame] | 338 | bool no_bits_; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 339 | }; |
| 340 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 341 | class OatDumperOptions { |
| 342 | public: |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 343 | OatDumperOptions(bool dump_vmap, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 344 | bool dump_code_info_stack_maps, |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 345 | bool disassemble_code, |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 346 | bool absolute_addresses, |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 347 | const char* class_filter, |
| 348 | const char* method_filter, |
| 349 | bool list_classes, |
| 350 | bool list_methods, |
David Brazdil | c03d7b6 | 2016-03-02 12:18:03 +0000 | [diff] [blame] | 351 | bool dump_header_only, |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 352 | const char* export_dex_location, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 353 | const char* app_image, |
| 354 | const char* app_oat, |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 355 | uint32_t addr2instr) |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 356 | : dump_vmap_(dump_vmap), |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 357 | dump_code_info_stack_maps_(dump_code_info_stack_maps), |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 358 | disassemble_code_(disassemble_code), |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 359 | absolute_addresses_(absolute_addresses), |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 360 | class_filter_(class_filter), |
Nicolas Geoffray | 3fcd220 | 2014-11-12 18:02:36 +0000 | [diff] [blame] | 361 | method_filter_(method_filter), |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 362 | list_classes_(list_classes), |
| 363 | list_methods_(list_methods), |
David Brazdil | c03d7b6 | 2016-03-02 12:18:03 +0000 | [diff] [blame] | 364 | dump_header_only_(dump_header_only), |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 365 | export_dex_location_(export_dex_location), |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 366 | app_image_(app_image), |
| 367 | app_oat_(app_oat), |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 368 | addr2instr_(addr2instr), |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 369 | class_loader_(nullptr) {} |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 370 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 371 | const bool dump_vmap_; |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 372 | const bool dump_code_info_stack_maps_; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 373 | const bool disassemble_code_; |
| 374 | const bool absolute_addresses_; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 375 | const char* const class_filter_; |
Nicolas Geoffray | 3fcd220 | 2014-11-12 18:02:36 +0000 | [diff] [blame] | 376 | const char* const method_filter_; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 377 | const bool list_classes_; |
| 378 | const bool list_methods_; |
David Brazdil | c03d7b6 | 2016-03-02 12:18:03 +0000 | [diff] [blame] | 379 | const bool dump_header_only_; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 380 | const char* const export_dex_location_; |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 381 | const char* const app_image_; |
| 382 | const char* const app_oat_; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 383 | uint32_t addr2instr_; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 384 | Handle<mirror::ClassLoader>* class_loader_; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 385 | }; |
| 386 | |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 387 | class OatDumper { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 388 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 389 | OatDumper(const OatFile& oat_file, const OatDumperOptions& options) |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 390 | : oat_file_(oat_file), |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 391 | oat_dex_files_(oat_file.GetOatDexFiles()), |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 392 | options_(options), |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 393 | resolved_addr2instr_(0), |
Mathieu Chartier | a7dd038 | 2014-11-20 17:08:58 -0800 | [diff] [blame] | 394 | instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()), |
| 395 | disassembler_(Disassembler::Create(instruction_set_, |
Andreas Gampe | 372f3a3 | 2016-08-19 10:49:06 -0700 | [diff] [blame] | 396 | new DisassemblerOptions( |
| 397 | options_.absolute_addresses_, |
| 398 | oat_file.Begin(), |
| 399 | oat_file.End(), |
| 400 | true /* can_read_literals_ */, |
| 401 | Is64BitInstructionSet(instruction_set_) |
| 402 | ? &Thread::DumpThreadOffset<PointerSize::k64> |
| 403 | : &Thread::DumpThreadOffset<PointerSize::k32>))) { |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 404 | CHECK(options_.class_loader_ != nullptr); |
| 405 | CHECK(options_.class_filter_ != nullptr); |
| 406 | CHECK(options_.method_filter_ != nullptr); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 407 | AddAllOffsets(); |
| 408 | } |
| 409 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 410 | ~OatDumper() { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 411 | delete disassembler_; |
| 412 | } |
| 413 | |
Mathieu Chartier | a7dd038 | 2014-11-20 17:08:58 -0800 | [diff] [blame] | 414 | InstructionSet GetInstructionSet() { |
| 415 | return instruction_set_; |
| 416 | } |
| 417 | |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 418 | typedef std::vector<std::unique_ptr<const DexFile>> DexFileUniqV; |
| 419 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 420 | bool Dump(std::ostream& os) { |
| 421 | bool success = true; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 422 | const OatHeader& oat_header = oat_file_.GetOatHeader(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 423 | |
| 424 | os << "MAGIC:\n"; |
| 425 | os << oat_header.GetMagic() << "\n\n"; |
| 426 | |
Jeff Hao | 4b07a6e | 2016-01-15 12:50:44 -0800 | [diff] [blame] | 427 | os << "LOCATION:\n"; |
| 428 | os << oat_file_.GetLocation() << "\n\n"; |
| 429 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 430 | os << "CHECKSUM:\n"; |
Elliott Hughes | ed2adb6 | 2012-02-29 14:41:01 -0800 | [diff] [blame] | 431 | os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 432 | |
Elliott Hughes | a72ec82 | 2012-03-05 17:12:22 -0800 | [diff] [blame] | 433 | os << "INSTRUCTION SET:\n"; |
| 434 | os << oat_header.GetInstructionSet() << "\n\n"; |
| 435 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 436 | { |
| 437 | std::unique_ptr<const InstructionSetFeatures> features( |
| 438 | InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(), |
| 439 | oat_header.GetInstructionSetFeaturesBitmap())); |
| 440 | os << "INSTRUCTION SET FEATURES:\n"; |
| 441 | os << features->GetFeatureString() << "\n\n"; |
| 442 | } |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 443 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 444 | os << "DEX FILE COUNT:\n"; |
| 445 | os << oat_header.GetDexFileCount() << "\n\n"; |
| 446 | |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 447 | #define DUMP_OAT_HEADER_OFFSET(label, offset) \ |
| 448 | os << label " OFFSET:\n"; \ |
| 449 | os << StringPrintf("0x%08x", oat_header.offset()); \ |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 450 | if (oat_header.offset() != 0 && options_.absolute_addresses_) { \ |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 451 | os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \ |
| 452 | } \ |
| 453 | os << StringPrintf("\n\n"); |
| 454 | |
| 455 | DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset); |
| 456 | DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE", |
| 457 | GetInterpreterToInterpreterBridgeOffset); |
| 458 | DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE", |
| 459 | GetInterpreterToCompiledCodeBridgeOffset); |
| 460 | DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP", |
| 461 | GetJniDlsymLookupOffset); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 462 | DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE", |
| 463 | GetQuickGenericJniTrampolineOffset); |
| 464 | DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE", |
| 465 | GetQuickImtConflictTrampolineOffset); |
| 466 | DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE", |
| 467 | GetQuickResolutionTrampolineOffset); |
| 468 | DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE", |
| 469 | GetQuickToInterpreterBridgeOffset); |
| 470 | #undef DUMP_OAT_HEADER_OFFSET |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 471 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 472 | os << "IMAGE PATCH DELTA:\n"; |
| 473 | os << StringPrintf("%d (0x%08x)\n\n", |
| 474 | oat_header.GetImagePatchDelta(), |
| 475 | oat_header.GetImagePatchDelta()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 476 | |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 477 | os << "IMAGE FILE LOCATION OAT CHECKSUM:\n"; |
| 478 | os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum()); |
| 479 | |
| 480 | os << "IMAGE FILE LOCATION OAT BEGIN:\n"; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 481 | os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin()); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 482 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 483 | // Print the key-value store. |
| 484 | { |
| 485 | os << "KEY VALUE STORE:\n"; |
| 486 | size_t index = 0; |
| 487 | const char* key; |
| 488 | const char* value; |
| 489 | while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) { |
| 490 | os << key << " = " << value << "\n"; |
| 491 | index++; |
| 492 | } |
| 493 | os << "\n"; |
| 494 | } |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 495 | |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 496 | if (options_.absolute_addresses_) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 497 | os << "BEGIN:\n"; |
| 498 | os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 499 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 500 | os << "END:\n"; |
| 501 | os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n"; |
| 502 | } |
| 503 | |
| 504 | os << "SIZE:\n"; |
| 505 | os << oat_file_.Size() << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 506 | |
| 507 | os << std::flush; |
| 508 | |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 509 | // If set, adjust relative address to be searched |
| 510 | if (options_.addr2instr_ != 0) { |
| 511 | resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset(); |
| 512 | os << "SEARCH ADDRESS (executable offset + input):\n"; |
| 513 | os << StringPrintf("0x%08x\n\n", resolved_addr2instr_); |
| 514 | } |
| 515 | |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 516 | // Dumping the dex file overview is compact enough to do even if header only. |
| 517 | DexFileData cumulative; |
| 518 | for (size_t i = 0; i < oat_dex_files_.size(); i++) { |
| 519 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; |
| 520 | CHECK(oat_dex_file != nullptr); |
| 521 | std::string error_msg; |
| 522 | const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg); |
| 523 | if (dex_file == nullptr) { |
| 524 | os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': " |
| 525 | << error_msg; |
| 526 | continue; |
| 527 | } |
| 528 | DexFileData data(*dex_file); |
| 529 | os << "Dex file data for " << dex_file->GetLocation() << "\n"; |
| 530 | data.Dump(os); |
| 531 | os << "\n"; |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 532 | const DexLayoutSections* const layout_sections = oat_dex_file->GetDexLayoutSections(); |
| 533 | if (layout_sections != nullptr) { |
| 534 | os << "Layout data\n"; |
| 535 | os << *layout_sections; |
| 536 | os << "\n"; |
| 537 | } |
| 538 | |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 539 | cumulative.Add(data); |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 540 | |
| 541 | // Dump .bss entries. |
| 542 | DumpBssEntries( |
| 543 | os, |
| 544 | "ArtMethod", |
| 545 | oat_dex_file->GetMethodBssMapping(), |
| 546 | dex_file->NumMethodIds(), |
| 547 | static_cast<size_t>(GetInstructionSetPointerSize(instruction_set_)), |
| 548 | [=](uint32_t index) { return dex_file->PrettyMethod(index); }); |
| 549 | DumpBssEntries( |
| 550 | os, |
| 551 | "Class", |
| 552 | oat_dex_file->GetTypeBssMapping(), |
| 553 | dex_file->NumTypeIds(), |
| 554 | sizeof(GcRoot<mirror::Class>), |
| 555 | [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); }); |
| 556 | DumpBssEntries( |
| 557 | os, |
| 558 | "String", |
| 559 | oat_dex_file->GetStringBssMapping(), |
| 560 | dex_file->NumStringIds(), |
| 561 | sizeof(GcRoot<mirror::Class>), |
| 562 | [=](uint32_t index) { return dex_file->StringDataByIdx(dex::StringIndex(index)); }); |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 563 | } |
| 564 | os << "Cumulative dex file data\n"; |
| 565 | cumulative.Dump(os); |
| 566 | os << "\n"; |
| 567 | |
David Brazdil | c03d7b6 | 2016-03-02 12:18:03 +0000 | [diff] [blame] | 568 | if (!options_.dump_header_only_) { |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 569 | VariableIndentationOutputStream vios(&os); |
| 570 | VdexFile::Header vdex_header = oat_file_.GetVdexFile()->GetHeader(); |
| 571 | if (vdex_header.IsValid()) { |
| 572 | std::string error_msg; |
| 573 | std::vector<const DexFile*> dex_files; |
| 574 | for (size_t i = 0; i < oat_dex_files_.size(); i++) { |
| 575 | const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg); |
| 576 | if (dex_file == nullptr) { |
| 577 | os << "Error opening dex file: " << error_msg << std::endl; |
| 578 | return false; |
| 579 | } |
| 580 | dex_files.push_back(dex_file); |
| 581 | } |
| 582 | verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData()); |
| 583 | deps.Dump(&vios); |
| 584 | } else { |
| 585 | os << "UNRECOGNIZED vdex file, magic " |
| 586 | << vdex_header.GetMagic() |
| 587 | << ", version " |
| 588 | << vdex_header.GetVersion() |
| 589 | << "\n"; |
| 590 | } |
David Brazdil | c03d7b6 | 2016-03-02 12:18:03 +0000 | [diff] [blame] | 591 | for (size_t i = 0; i < oat_dex_files_.size(); i++) { |
| 592 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; |
| 593 | CHECK(oat_dex_file != nullptr); |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 594 | if (!DumpOatDexFile(os, *oat_dex_file)) { |
| 595 | success = false; |
| 596 | } |
| 597 | } |
| 598 | } |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 599 | |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 600 | if (options_.export_dex_location_) { |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 601 | std::string error_msg; |
| 602 | std::string vdex_filename = GetVdexFilename(oat_file_.GetLocation()); |
| 603 | if (!OS::FileExists(vdex_filename.c_str())) { |
| 604 | os << "File " << vdex_filename.c_str() << " does not exist\n"; |
| 605 | return false; |
| 606 | } |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 607 | |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 608 | DexFileUniqV vdex_dex_files; |
| 609 | std::unique_ptr<const VdexFile> vdex_file = OpenVdexUnquicken(vdex_filename, |
| 610 | &vdex_dex_files, |
| 611 | &error_msg); |
| 612 | if (vdex_file.get() == nullptr) { |
| 613 | os << "Failed to open vdex file: " << error_msg << "\n"; |
| 614 | return false; |
| 615 | } |
| 616 | if (oat_dex_files_.size() != vdex_dex_files.size()) { |
| 617 | os << "Dex files number in Vdex file does not match Dex files number in Oat file: " |
| 618 | << vdex_dex_files.size() << " vs " << oat_dex_files_.size() << '\n'; |
| 619 | return false; |
| 620 | } |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 621 | |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 622 | size_t i = 0; |
| 623 | for (const auto& vdex_dex_file : vdex_dex_files) { |
| 624 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; |
| 625 | CHECK(oat_dex_file != nullptr); |
| 626 | CHECK(vdex_dex_file != nullptr); |
| 627 | if (!ExportDexFile(os, *oat_dex_file, vdex_dex_file.get())) { |
| 628 | success = false; |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 629 | } |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 630 | i++; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 631 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 632 | } |
David Brazdil | c03d7b6 | 2016-03-02 12:18:03 +0000 | [diff] [blame] | 633 | |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 634 | { |
| 635 | os << "OAT FILE STATS:\n"; |
| 636 | VariableIndentationOutputStream vios(&os); |
| 637 | stats_.Dump(vios); |
| 638 | } |
| 639 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 640 | os << std::flush; |
| 641 | return success; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 644 | size_t ComputeSize(const void* oat_data) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 645 | if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() || |
| 646 | reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 647 | return 0; // Address not in oat file |
| 648 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 649 | uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) - |
| 650 | reinterpret_cast<uintptr_t>(oat_file_.Begin()); |
| 651 | auto it = offsets_.upper_bound(begin_offset); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 652 | CHECK(it != offsets_.end()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 653 | uintptr_t end_offset = *it; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 654 | return end_offset - begin_offset; |
| 655 | } |
| 656 | |
Mathieu Chartier | a7dd038 | 2014-11-20 17:08:58 -0800 | [diff] [blame] | 657 | InstructionSet GetOatInstructionSet() { |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 658 | return oat_file_.GetOatHeader().GetInstructionSet(); |
| 659 | } |
| 660 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 661 | const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 662 | for (size_t i = 0; i < oat_dex_files_.size(); i++) { |
| 663 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 664 | CHECK(oat_dex_file != nullptr); |
| 665 | std::string error_msg; |
Mathieu Chartier | ac8f439 | 2015-08-27 13:54:20 -0700 | [diff] [blame] | 666 | const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg); |
| 667 | if (dex_file == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 668 | LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() |
| 669 | << "': " << error_msg; |
| 670 | } else { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 671 | const char* descriptor = m->GetDeclaringClassDescriptor(); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 672 | const DexFile::ClassDef* class_def = |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 673 | OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 674 | if (class_def != nullptr) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 675 | uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 676 | const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 677 | size_t method_index = m->GetMethodIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 678 | return oat_class.GetOatMethod(method_index).GetQuickCode(); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 682 | return nullptr; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 683 | } |
| 684 | |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 685 | // Returns nullptr and updates error_msg if the Vdex file cannot be opened, otherwise all Dex |
| 686 | // files are fully unquickened and stored in dex_files |
| 687 | std::unique_ptr<const VdexFile> OpenVdexUnquicken(const std::string& vdex_filename, |
| 688 | /* out */ DexFileUniqV* dex_files, |
| 689 | /* out */ std::string* error_msg) { |
| 690 | std::unique_ptr<const File> file(OS::OpenFileForReading(vdex_filename.c_str())); |
| 691 | if (file == nullptr) { |
| 692 | *error_msg = "Could not open file " + vdex_filename + " for reading."; |
| 693 | return nullptr; |
| 694 | } |
| 695 | |
| 696 | int64_t vdex_length = file->GetLength(); |
| 697 | if (vdex_length == -1) { |
| 698 | *error_msg = "Could not read the length of file " + vdex_filename; |
| 699 | return nullptr; |
| 700 | } |
| 701 | |
| 702 | std::unique_ptr<MemMap> mmap(MemMap::MapFile( |
| 703 | file->GetLength(), |
| 704 | PROT_READ | PROT_WRITE, |
| 705 | MAP_PRIVATE, |
| 706 | file->Fd(), |
| 707 | /* start offset */ 0, |
| 708 | /* low_4gb */ false, |
| 709 | vdex_filename.c_str(), |
| 710 | error_msg)); |
| 711 | if (mmap == nullptr) { |
| 712 | *error_msg = "Failed to mmap file " + vdex_filename + ": " + *error_msg; |
| 713 | return nullptr; |
| 714 | } |
| 715 | |
| 716 | std::unique_ptr<VdexFile> vdex_file(new VdexFile(mmap.release())); |
| 717 | if (!vdex_file->IsValid()) { |
| 718 | *error_msg = "Vdex file is not valid"; |
| 719 | return nullptr; |
| 720 | } |
| 721 | |
| 722 | DexFileUniqV tmp_dex_files; |
| 723 | if (!vdex_file->OpenAllDexFiles(&tmp_dex_files, error_msg)) { |
| 724 | *error_msg = "Failed to open Dex files from Vdex: " + *error_msg; |
| 725 | return nullptr; |
| 726 | } |
| 727 | |
| 728 | vdex_file->Unquicken(MakeNonOwningPointerVector(tmp_dex_files), |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 729 | /* decompile_return_instruction */ true); |
| 730 | |
| 731 | *dex_files = std::move(tmp_dex_files); |
| 732 | return vdex_file; |
| 733 | } |
| 734 | |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 735 | struct Stats { |
| 736 | enum ByteKind { |
| 737 | kByteKindCode, |
| 738 | kByteKindQuickMethodHeader, |
| 739 | kByteKindCodeInfoLocationCatalog, |
| 740 | kByteKindCodeInfoDexRegisterMap, |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 741 | kByteKindCodeInfoEncoding, |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 742 | kByteKindCodeInfoInvokeInfo, |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 743 | kByteKindCodeInfoStackMasks, |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 744 | kByteKindCodeInfoRegisterMasks, |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 745 | kByteKindStackMapNativePc, |
| 746 | kByteKindStackMapDexPc, |
| 747 | kByteKindStackMapDexRegisterMap, |
Mathieu Chartier | 1e08379 | 2017-02-08 13:30:04 -0800 | [diff] [blame] | 748 | kByteKindStackMapInlineInfoIndex, |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 749 | kByteKindStackMapRegisterMaskIndex, |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 750 | kByteKindStackMapStackMaskIndex, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 751 | kByteKindInlineInfoMethodIndexIdx, |
Mathieu Chartier | 1e08379 | 2017-02-08 13:30:04 -0800 | [diff] [blame] | 752 | kByteKindInlineInfoDexPc, |
| 753 | kByteKindInlineInfoExtraData, |
| 754 | kByteKindInlineInfoDexRegisterMap, |
| 755 | kByteKindInlineInfoIsLast, |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 756 | kByteKindCount, |
Mathieu Chartier | 1e08379 | 2017-02-08 13:30:04 -0800 | [diff] [blame] | 757 | // Special ranges for std::accumulate convenience. |
| 758 | kByteKindStackMapFirst = kByteKindStackMapNativePc, |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 759 | kByteKindStackMapLast = kByteKindStackMapStackMaskIndex, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 760 | kByteKindInlineInfoFirst = kByteKindInlineInfoMethodIndexIdx, |
Mathieu Chartier | 1e08379 | 2017-02-08 13:30:04 -0800 | [diff] [blame] | 761 | kByteKindInlineInfoLast = kByteKindInlineInfoIsLast, |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 762 | }; |
| 763 | int64_t bits[kByteKindCount] = {}; |
| 764 | // Since code has deduplication, seen tracks already seen pointers to avoid double counting |
| 765 | // deduplicated code and tables. |
| 766 | std::unordered_set<const void*> seen; |
| 767 | |
| 768 | // Returns true if it was newly added. |
| 769 | bool AddBitsIfUnique(ByteKind kind, int64_t count, const void* address) { |
| 770 | if (seen.insert(address).second == true) { |
| 771 | // True means the address was not already in the set. |
| 772 | AddBits(kind, count); |
| 773 | return true; |
| 774 | } |
| 775 | return false; |
| 776 | } |
| 777 | |
| 778 | void AddBits(ByteKind kind, int64_t count) { |
| 779 | bits[kind] += count; |
| 780 | } |
| 781 | |
| 782 | void Dump(VariableIndentationOutputStream& os) { |
| 783 | const int64_t sum = std::accumulate(bits, bits + kByteKindCount, 0u); |
| 784 | os.Stream() << "Dumping cumulative use of " << sum / kBitsPerByte << " accounted bytes\n"; |
| 785 | if (sum > 0) { |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 786 | Dump(os, "Code ", bits[kByteKindCode], sum); |
| 787 | Dump(os, "QuickMethodHeader ", bits[kByteKindQuickMethodHeader], sum); |
| 788 | Dump(os, "CodeInfoEncoding ", bits[kByteKindCodeInfoEncoding], sum); |
| 789 | Dump(os, "CodeInfoLocationCatalog ", bits[kByteKindCodeInfoLocationCatalog], sum); |
| 790 | Dump(os, "CodeInfoDexRegisterMap ", bits[kByteKindCodeInfoDexRegisterMap], sum); |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 791 | Dump(os, "CodeInfoStackMasks ", bits[kByteKindCodeInfoStackMasks], sum); |
| 792 | Dump(os, "CodeInfoRegisterMasks ", bits[kByteKindCodeInfoRegisterMasks], sum); |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 793 | Dump(os, "CodeInfoInvokeInfo ", bits[kByteKindCodeInfoInvokeInfo], sum); |
Mathieu Chartier | 1e08379 | 2017-02-08 13:30:04 -0800 | [diff] [blame] | 794 | // Stack map section. |
| 795 | const int64_t stack_map_bits = std::accumulate(bits + kByteKindStackMapFirst, |
| 796 | bits + kByteKindStackMapLast + 1, |
| 797 | 0u); |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 798 | Dump(os, "CodeInfoStackMap ", stack_map_bits, sum); |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 799 | { |
| 800 | ScopedIndentation indent1(&os); |
| 801 | Dump(os, |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 802 | "StackMapNativePc ", |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 803 | bits[kByteKindStackMapNativePc], |
| 804 | stack_map_bits, |
| 805 | "stack map"); |
| 806 | Dump(os, |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 807 | "StackMapDexPcEncoding ", |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 808 | bits[kByteKindStackMapDexPc], |
| 809 | stack_map_bits, |
| 810 | "stack map"); |
| 811 | Dump(os, |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 812 | "StackMapDexRegisterMap ", |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 813 | bits[kByteKindStackMapDexRegisterMap], |
| 814 | stack_map_bits, |
| 815 | "stack map"); |
| 816 | Dump(os, |
Mathieu Chartier | 697dc66 | 2017-02-08 16:56:48 -0800 | [diff] [blame] | 817 | "StackMapInlineInfoIndex ", |
Mathieu Chartier | 1e08379 | 2017-02-08 13:30:04 -0800 | [diff] [blame] | 818 | bits[kByteKindStackMapInlineInfoIndex], |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 819 | stack_map_bits, |
| 820 | "stack map"); |
| 821 | Dump(os, |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 822 | "StackMapRegisterMaskIndex ", |
| 823 | bits[kByteKindStackMapRegisterMaskIndex], |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 824 | stack_map_bits, |
| 825 | "stack map"); |
| 826 | Dump(os, |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 827 | "StackMapStackMaskIndex ", |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 828 | bits[kByteKindStackMapStackMaskIndex], |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 829 | stack_map_bits, |
| 830 | "stack map"); |
| 831 | } |
Mathieu Chartier | 1e08379 | 2017-02-08 13:30:04 -0800 | [diff] [blame] | 832 | // Inline info section. |
| 833 | const int64_t inline_info_bits = std::accumulate(bits + kByteKindInlineInfoFirst, |
| 834 | bits + kByteKindInlineInfoLast + 1, |
| 835 | 0u); |
| 836 | Dump(os, "CodeInfoInlineInfo ", inline_info_bits, sum); |
| 837 | { |
| 838 | ScopedIndentation indent1(&os); |
| 839 | Dump(os, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 840 | "InlineInfoMethodIndexIdx ", |
| 841 | bits[kByteKindInlineInfoMethodIndexIdx], |
Mathieu Chartier | 1e08379 | 2017-02-08 13:30:04 -0800 | [diff] [blame] | 842 | inline_info_bits, |
| 843 | "inline info"); |
| 844 | Dump(os, |
| 845 | "InlineInfoDexPc ", |
| 846 | bits[kByteKindStackMapDexPc], |
| 847 | inline_info_bits, |
| 848 | "inline info"); |
| 849 | Dump(os, |
| 850 | "InlineInfoExtraData ", |
| 851 | bits[kByteKindInlineInfoExtraData], |
| 852 | inline_info_bits, |
| 853 | "inline info"); |
| 854 | Dump(os, |
| 855 | "InlineInfoDexRegisterMap ", |
| 856 | bits[kByteKindInlineInfoDexRegisterMap], |
| 857 | inline_info_bits, |
| 858 | "inline info"); |
| 859 | Dump(os, |
| 860 | "InlineInfoIsLast ", |
| 861 | bits[kByteKindInlineInfoIsLast], |
| 862 | inline_info_bits, |
| 863 | "inline info"); |
| 864 | } |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 865 | } |
| 866 | os.Stream() << "\n" << std::flush; |
| 867 | } |
| 868 | |
| 869 | private: |
| 870 | void Dump(VariableIndentationOutputStream& os, |
| 871 | const char* name, |
| 872 | int64_t size, |
| 873 | int64_t total, |
| 874 | const char* sum_of = "total") { |
| 875 | const double percent = (static_cast<double>(size) / static_cast<double>(total)) * 100; |
| 876 | os.Stream() << StringPrintf("%s = %8" PRId64 " (%2.0f%% of %s)\n", |
| 877 | name, |
| 878 | size / kBitsPerByte, |
| 879 | percent, |
| 880 | sum_of); |
| 881 | } |
| 882 | }; |
| 883 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 884 | private: |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 885 | void AddAllOffsets() { |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 886 | // We don't know the length of the code for each method, but we need to know where to stop |
| 887 | // when disassembling. What we do know is that a region of code will be followed by some other |
| 888 | // region, so if we keep a sorted sequence of the start of each region, we can infer the length |
| 889 | // 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] | 890 | for (size_t i = 0; i < oat_dex_files_.size(); i++) { |
| 891 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 892 | CHECK(oat_dex_file != nullptr); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 893 | std::string error_msg; |
Mathieu Chartier | ac8f439 | 2015-08-27 13:54:20 -0700 | [diff] [blame] | 894 | const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg); |
| 895 | if (dex_file == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 896 | LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() |
| 897 | << "': " << error_msg; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 898 | continue; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 899 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 900 | offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader())); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 901 | for (size_t class_def_index = 0; |
| 902 | class_def_index < dex_file->NumClassDefs(); |
| 903 | class_def_index++) { |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 904 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 905 | const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 906 | const uint8_t* class_data = dex_file->GetClassData(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 907 | if (class_data != nullptr) { |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 908 | ClassDataItemIterator it(*dex_file, class_data); |
Mathieu Chartier | e17cf24 | 2017-06-19 11:05:51 -0700 | [diff] [blame] | 909 | it.SkipAllFields(); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 910 | uint32_t class_method_index = 0; |
Mathieu Chartier | b7c273c | 2017-11-10 18:07:56 -0800 | [diff] [blame] | 911 | while (it.HasNextMethod()) { |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 912 | AddOffsets(oat_class.GetOatMethod(class_method_index++)); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 913 | it.Next(); |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | // If the last thing in the file is code for a method, there won't be an offset for the "next" |
| 920 | // thing. Instead of having a special case in the upper_bound code, let's just add an entry |
| 921 | // for the end of the file. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 922 | offsets_.insert(oat_file_.Size()); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 923 | } |
| 924 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 925 | static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) { |
| 926 | return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific. |
| 927 | } |
| 928 | |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 929 | void AddOffsets(const OatFile::OatMethod& oat_method) { |
Brian Carlstrom | 95ba0dc | 2012-03-05 22:06:14 -0800 | [diff] [blame] | 930 | uint32_t code_offset = oat_method.GetCodeOffset(); |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 931 | if (oat_file_.GetOatHeader().GetInstructionSet() == InstructionSet::kThumb2) { |
Brian Carlstrom | 95ba0dc | 2012-03-05 22:06:14 -0800 | [diff] [blame] | 932 | code_offset &= ~0x1; |
| 933 | } |
| 934 | offsets_.insert(code_offset); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 935 | offsets_.insert(oat_method.GetVmapTableOffset()); |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 936 | } |
| 937 | |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 938 | // Dex file data, may be for multiple different dex files. |
| 939 | class DexFileData { |
| 940 | public: |
| 941 | DexFileData() {} |
| 942 | |
| 943 | explicit DexFileData(const DexFile& dex_file) |
| 944 | : num_string_ids_(dex_file.NumStringIds()), |
| 945 | num_method_ids_(dex_file.NumMethodIds()), |
| 946 | num_field_ids_(dex_file.NumFieldIds()), |
| 947 | num_type_ids_(dex_file.NumTypeIds()), |
| 948 | num_class_defs_(dex_file.NumClassDefs()) { |
| 949 | for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) { |
| 950 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); |
| 951 | WalkClass(dex_file, class_def); |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | void Add(const DexFileData& other) { |
| 956 | AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_); |
| 957 | num_string_ids_from_code_ += other.num_string_ids_from_code_; |
| 958 | AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_); |
| 959 | dex_code_bytes_ += other.dex_code_bytes_; |
| 960 | num_string_ids_ += other.num_string_ids_; |
| 961 | num_method_ids_ += other.num_method_ids_; |
| 962 | num_field_ids_ += other.num_field_ids_; |
| 963 | num_type_ids_ += other.num_type_ids_; |
| 964 | num_class_defs_ += other.num_class_defs_; |
| 965 | } |
| 966 | |
| 967 | void Dump(std::ostream& os) { |
| 968 | os << "Num string ids: " << num_string_ids_ << "\n"; |
| 969 | os << "Num method ids: " << num_method_ids_ << "\n"; |
| 970 | os << "Num field ids: " << num_field_ids_ << "\n"; |
| 971 | os << "Num type ids: " << num_type_ids_ << "\n"; |
| 972 | os << "Num class defs: " << num_class_defs_ << "\n"; |
| 973 | os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n"; |
| 974 | os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n"; |
| 975 | os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n"; |
| 976 | os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n"; |
| 977 | } |
| 978 | |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 979 | private: |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 980 | // All of the elements from one container to another. |
| 981 | template <typename Dest, typename Src> |
| 982 | static void AddAll(Dest& dest, const Src& src) { |
| 983 | dest.insert(src.begin(), src.end()); |
| 984 | } |
| 985 | |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 986 | void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) { |
| 987 | const uint8_t* class_data = dex_file.GetClassData(class_def); |
| 988 | if (class_data == nullptr) { // empty class such as a marker interface? |
| 989 | return; |
| 990 | } |
| 991 | ClassDataItemIterator it(dex_file, class_data); |
Mathieu Chartier | e17cf24 | 2017-06-19 11:05:51 -0700 | [diff] [blame] | 992 | it.SkipAllFields(); |
Mathieu Chartier | b7c273c | 2017-11-10 18:07:56 -0800 | [diff] [blame] | 993 | while (it.HasNextMethod()) { |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 994 | WalkCodeItem(dex_file, it.GetMethodCodeItem()); |
| 995 | it.Next(); |
| 996 | } |
| 997 | DCHECK(!it.HasNext()); |
| 998 | } |
| 999 | |
| 1000 | void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) { |
| 1001 | if (code_item == nullptr) { |
| 1002 | return; |
| 1003 | } |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 1004 | CodeItemInstructionAccessor instructions(dex_file, code_item); |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 1005 | |
| 1006 | // If we inserted a new dex code item pointer, add to total code bytes. |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1007 | const uint16_t* code_ptr = instructions.Insns(); |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 1008 | if (dex_code_item_ptrs_.insert(code_ptr).second) { |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1009 | dex_code_bytes_ += instructions.InsnsSizeInCodeUnits() * sizeof(code_ptr[0]); |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1012 | for (const DexInstructionPcPair& inst : instructions) { |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1013 | switch (inst->Opcode()) { |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 1014 | case Instruction::CONST_STRING: { |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1015 | const dex::StringIndex string_index(inst->VRegB_21c()); |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 1016 | unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index)); |
| 1017 | ++num_string_ids_from_code_; |
| 1018 | break; |
| 1019 | } |
| 1020 | case Instruction::CONST_STRING_JUMBO: { |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1021 | const dex::StringIndex string_index(inst->VRegB_31c()); |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 1022 | unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index)); |
| 1023 | ++num_string_ids_from_code_; |
| 1024 | break; |
| 1025 | } |
| 1026 | default: |
| 1027 | break; |
| 1028 | } |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | // Unique string ids loaded from dex code. |
Mathieu Chartier | fc8b422 | 2017-09-17 13:44:24 -0700 | [diff] [blame] | 1033 | std::set<StringReference> unique_string_ids_from_code_; |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 1034 | |
| 1035 | // Total string ids loaded from dex code. |
| 1036 | size_t num_string_ids_from_code_ = 0; |
| 1037 | |
| 1038 | // Unique code pointers. |
| 1039 | std::set<const void*> dex_code_item_ptrs_; |
| 1040 | |
| 1041 | // Total "unique" dex code bytes. |
| 1042 | size_t dex_code_bytes_ = 0; |
| 1043 | |
| 1044 | // Other dex ids. |
| 1045 | size_t num_string_ids_ = 0; |
| 1046 | size_t num_method_ids_ = 0; |
| 1047 | size_t num_field_ids_ = 0; |
| 1048 | size_t num_type_ids_ = 0; |
| 1049 | size_t num_class_defs_ = 0; |
| 1050 | }; |
| 1051 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1052 | bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) { |
| 1053 | bool success = true; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1054 | bool stop_analysis = false; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1055 | os << "OatDexFile:\n"; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 1056 | os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str()); |
Elliott Hughes | ed2adb6 | 2012-02-29 14:41:01 -0800 | [diff] [blame] | 1057 | os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1058 | |
Andreas Gampe | 2ba8895 | 2016-04-29 17:52:07 -0700 | [diff] [blame] | 1059 | const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin(); |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 1060 | if (oat_dex_file.GetOatFile()->ContainsDexCode()) { |
| 1061 | const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin(); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1062 | |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 1063 | // Print data range of the dex file embedded inside the corresponding vdex file. |
| 1064 | const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer(); |
| 1065 | uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin); |
| 1066 | os << StringPrintf( |
| 1067 | "dex-file: 0x%08x..0x%08x\n", |
| 1068 | dex_offset, |
| 1069 | dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1)); |
| 1070 | } else { |
| 1071 | os << StringPrintf("dex-file not in VDEX file\n"); |
| 1072 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1073 | |
Andreas Gampe | 2ba8895 | 2016-04-29 17:52:07 -0700 | [diff] [blame] | 1074 | // Create the dex file early. A lot of print-out things depend on it. |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1075 | std::string error_msg; |
Mathieu Chartier | ac8f439 | 2015-08-27 13:54:20 -0700 | [diff] [blame] | 1076 | const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg); |
| 1077 | if (dex_file == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1078 | os << "NOT FOUND: " << error_msg << "\n\n"; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1079 | os << std::flush; |
| 1080 | return false; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1081 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1082 | |
Andreas Gampe | 2ba8895 | 2016-04-29 17:52:07 -0700 | [diff] [blame] | 1083 | // Print lookup table, if it exists. |
| 1084 | if (oat_dex_file.GetLookupTableData() != nullptr) { |
| 1085 | uint32_t table_offset = dchecked_integral_cast<uint32_t>( |
| 1086 | oat_dex_file.GetLookupTableData() - oat_file_begin); |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 1087 | uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs()); |
Andreas Gampe | 2ba8895 | 2016-04-29 17:52:07 -0700 | [diff] [blame] | 1088 | os << StringPrintf("type-table: 0x%08x..0x%08x\n", |
| 1089 | table_offset, |
| 1090 | table_offset + table_size - 1); |
| 1091 | } |
| 1092 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1093 | VariableIndentationOutputStream vios(&os); |
| 1094 | ScopedIndentation indent1(&vios); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 1095 | for (size_t class_def_index = 0; |
| 1096 | class_def_index < dex_file->NumClassDefs(); |
| 1097 | class_def_index++) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1098 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 1099 | const char* descriptor = dex_file->GetClassDescriptor(class_def); |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1100 | |
| 1101 | // TODO: Support regex |
| 1102 | if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) { |
| 1103 | continue; |
| 1104 | } |
| 1105 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1106 | uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1107 | const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1108 | os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)", |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1109 | class_def_index, descriptor, oat_class_offset, class_def.class_idx_.index_) |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 1110 | << " (" << oat_class.GetStatus() << ")" |
| 1111 | << " (" << oat_class.GetType() << ")\n"; |
| 1112 | // TODO: include bitmap here if type is kOatClassSomeCompiled? |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 1113 | if (options_.list_classes_) { |
| 1114 | continue; |
| 1115 | } |
| 1116 | if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1117 | success = false; |
| 1118 | } |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1119 | if (stop_analysis) { |
| 1120 | os << std::flush; |
| 1121 | return success; |
| 1122 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1123 | } |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 1124 | os << "\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1125 | os << std::flush; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1126 | return success; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 1129 | // Backwards compatible Dex file export. If dex_file is nullptr (valid Vdex file not present) the |
| 1130 | // Dex resource is extracted from the oat_dex_file and its checksum is repaired since it's not |
| 1131 | // unquickened. Otherwise the dex_file has been fully unquickened and is expected to verify the |
| 1132 | // original checksum. |
| 1133 | bool ExportDexFile(std::ostream& os, |
| 1134 | const OatFile::OatDexFile& oat_dex_file, |
| 1135 | const DexFile* dex_file) { |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1136 | std::string error_msg; |
| 1137 | std::string dex_file_location = oat_dex_file.GetDexFileLocation(); |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1138 | size_t fsize = oat_dex_file.FileSize(); |
| 1139 | |
| 1140 | // Some quick checks just in case |
| 1141 | if (fsize == 0 || fsize < sizeof(DexFile::Header)) { |
| 1142 | os << "Invalid dex file\n"; |
| 1143 | return false; |
| 1144 | } |
| 1145 | |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 1146 | if (dex_file == nullptr) { |
| 1147 | // Exported bytecode is quickened (dex-to-dex transformations present) |
| 1148 | dex_file = OpenDexFile(&oat_dex_file, &error_msg); |
| 1149 | if (dex_file == nullptr) { |
| 1150 | os << "Failed to open dex file '" << dex_file_location << "': " << error_msg; |
| 1151 | return false; |
| 1152 | } |
| 1153 | |
| 1154 | // Recompute checksum |
| 1155 | reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ = |
| 1156 | dex_file->CalculateChecksum(); |
| 1157 | } else { |
| 1158 | // Vdex unquicken output should match original input bytecode |
| 1159 | uint32_t orig_checksum = |
| 1160 | reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_; |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 1161 | CHECK_EQ(orig_checksum, dex_file->CalculateChecksum()); |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 1162 | if (orig_checksum != dex_file->CalculateChecksum()) { |
| 1163 | os << "Unexpected checksum from unquicken dex file '" << dex_file_location << "'\n"; |
| 1164 | return false; |
| 1165 | } |
| 1166 | } |
| 1167 | |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1168 | // Verify output directory exists |
| 1169 | if (!OS::DirectoryExists(options_.export_dex_location_)) { |
| 1170 | // TODO: Extend OS::DirectoryExists if symlink support is required |
| 1171 | os << options_.export_dex_location_ << " output directory not found or symlink\n"; |
| 1172 | return false; |
| 1173 | } |
| 1174 | |
| 1175 | // Beautify path names |
| 1176 | if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) { |
| 1177 | return false; |
| 1178 | } |
| 1179 | |
| 1180 | std::string dex_orig_name; |
| 1181 | size_t dex_orig_pos = dex_file_location.rfind('/'); |
| 1182 | if (dex_orig_pos == std::string::npos) |
| 1183 | dex_orig_name = dex_file_location; |
| 1184 | else |
| 1185 | dex_orig_name = dex_file_location.substr(dex_orig_pos + 1); |
| 1186 | |
| 1187 | // A more elegant approach to efficiently name user installed apps is welcome |
Andreas Gampe | f812d8c | 2017-02-17 10:19:44 -0800 | [diff] [blame] | 1188 | if (dex_orig_name.size() == 8 && |
| 1189 | dex_orig_name.compare("base.apk") == 0 && |
| 1190 | dex_orig_pos != std::string::npos) { |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1191 | dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1); |
| 1192 | size_t apk_orig_pos = dex_file_location.rfind('/'); |
| 1193 | if (apk_orig_pos != std::string::npos) { |
| 1194 | dex_orig_name = dex_file_location.substr(++apk_orig_pos); |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | std::string out_dex_path(options_.export_dex_location_); |
| 1199 | if (out_dex_path.back() != '/') { |
| 1200 | out_dex_path.append("/"); |
| 1201 | } |
| 1202 | out_dex_path.append(dex_orig_name); |
| 1203 | out_dex_path.append("_export.dex"); |
| 1204 | if (out_dex_path.length() > PATH_MAX) { |
| 1205 | return false; |
| 1206 | } |
| 1207 | |
| 1208 | std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str())); |
| 1209 | if (file.get() == nullptr) { |
| 1210 | os << "Failed to open output dex file " << out_dex_path; |
| 1211 | return false; |
| 1212 | } |
| 1213 | |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 1214 | bool success = false; |
| 1215 | success = file->WriteFully(dex_file->Begin(), fsize); |
| 1216 | // } |
| 1217 | |
| 1218 | if (!success) { |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1219 | os << "Failed to write dex file"; |
| 1220 | file->Erase(); |
| 1221 | return false; |
| 1222 | } |
| 1223 | |
| 1224 | if (file->FlushCloseOrErase() != 0) { |
| 1225 | os << "Flush and close failed"; |
| 1226 | return false; |
| 1227 | } |
| 1228 | |
| 1229 | os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize); |
| 1230 | os << std::flush; |
| 1231 | |
| 1232 | return true; |
| 1233 | } |
| 1234 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1235 | bool DumpOatClass(VariableIndentationOutputStream* vios, |
| 1236 | const OatFile::OatClass& oat_class, const DexFile& dex_file, |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 1237 | const DexFile::ClassDef& class_def, bool* stop_analysis) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1238 | bool success = true; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1239 | bool addr_found = false; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1240 | const uint8_t* class_data = dex_file.GetClassData(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1241 | if (class_data == nullptr) { // empty class such as a marker interface? |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1242 | vios->Stream() << std::flush; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1243 | return success; |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 1244 | } |
| 1245 | ClassDataItemIterator it(dex_file, class_data); |
Mathieu Chartier | e17cf24 | 2017-06-19 11:05:51 -0700 | [diff] [blame] | 1246 | it.SkipAllFields(); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1247 | uint32_t class_method_index = 0; |
Mathieu Chartier | b7c273c | 2017-11-10 18:07:56 -0800 | [diff] [blame] | 1248 | while (it.HasNextMethod()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1249 | if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file, |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1250 | it.GetMemberIndex(), it.GetMethodCodeItem(), |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 1251 | it.GetRawMemberAccessFlags(), &addr_found)) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1252 | success = false; |
| 1253 | } |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1254 | if (addr_found) { |
| 1255 | *stop_analysis = true; |
| 1256 | return success; |
| 1257 | } |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1258 | class_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1259 | it.Next(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1260 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1261 | DCHECK(!it.HasNext()); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1262 | vios->Stream() << std::flush; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1263 | return success; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1264 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 1265 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1266 | static constexpr uint32_t kPrologueBytes = 16; |
| 1267 | |
| 1268 | // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes. |
| 1269 | static constexpr uint32_t kMaxCodeSize = 100 * 1000; |
| 1270 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1271 | bool DumpOatMethod(VariableIndentationOutputStream* vios, |
| 1272 | const DexFile::ClassDef& class_def, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1273 | uint32_t class_method_index, |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1274 | const OatFile::OatClass& oat_class, |
| 1275 | const DexFile& dex_file, |
| 1276 | uint32_t dex_method_idx, |
| 1277 | const DexFile::CodeItem* code_item, |
| 1278 | uint32_t method_access_flags, |
| 1279 | bool* addr_found) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1280 | bool success = true; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1281 | |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 1282 | CodeItemDataAccessor code_item_accessor(dex_file, code_item); |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1283 | |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1284 | // TODO: Support regex |
| 1285 | std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx)); |
| 1286 | if (method_name.find(options_.method_filter_) == std::string::npos) { |
Nicolas Geoffray | 3fcd220 | 2014-11-12 18:02:36 +0000 | [diff] [blame] | 1287 | return success; |
| 1288 | } |
| 1289 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1290 | std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1291 | vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n", |
| 1292 | class_method_index, pretty_method.c_str(), |
| 1293 | dex_method_idx); |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1294 | if (options_.list_methods_) { |
| 1295 | return success; |
| 1296 | } |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1297 | |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1298 | uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index); |
| 1299 | const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index); |
| 1300 | const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index); |
| 1301 | uint32_t code_offset = oat_method.GetCodeOffset(); |
| 1302 | uint32_t code_size = oat_method.GetQuickCodeSize(); |
| 1303 | if (resolved_addr2instr_ != 0) { |
| 1304 | if (resolved_addr2instr_ > code_offset + code_size) { |
| 1305 | return success; |
| 1306 | } else { |
| 1307 | *addr_found = true; // stop analyzing file at next iteration |
| 1308 | } |
| 1309 | } |
| 1310 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1311 | // Everything below is indented at least once. |
| 1312 | ScopedIndentation indent1(vios); |
| 1313 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1314 | { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1315 | vios->Stream() << "DEX CODE:\n"; |
| 1316 | ScopedIndentation indent2(vios); |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1317 | if (code_item_accessor.HasCodeItem()) { |
| 1318 | for (const DexInstructionPcPair& inst : code_item_accessor) { |
| 1319 | vios->Stream() << StringPrintf("0x%04x: ", inst.DexPc()) << inst->DumpHexLE(5) |
| 1320 | << StringPrintf("\t| %s\n", inst->DumpString(&dex_file).c_str()); |
| 1321 | } |
| 1322 | } |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1323 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1324 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1325 | std::unique_ptr<StackHandleScope<1>> hs; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1326 | std::unique_ptr<verifier::MethodVerifier> verifier; |
| 1327 | if (Runtime::Current() != nullptr) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1328 | // We need to have the handle scope stay live until after the verifier since the verifier has |
| 1329 | // a handle to the dex cache from hs. |
| 1330 | hs.reset(new StackHandleScope<1>(Thread::Current())); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1331 | vios->Stream() << "VERIFIER TYPE ANALYSIS:\n"; |
| 1332 | ScopedIndentation indent2(vios); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1333 | verifier.reset(DumpVerifier(vios, hs.get(), |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1334 | dex_method_idx, &dex_file, class_def, code_item, |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1335 | method_access_flags)); |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1336 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1337 | { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1338 | vios->Stream() << "OatMethodOffsets "; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1339 | if (options_.absolute_addresses_) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1340 | vios->Stream() << StringPrintf("%p ", oat_method_offsets); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1341 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1342 | vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1343 | if (oat_method_offsets_offset > oat_file_.Size()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1344 | vios->Stream() << StringPrintf( |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1345 | "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n", |
| 1346 | oat_method_offsets_offset, oat_file_.Size()); |
| 1347 | // If we can't read OatMethodOffsets, the rest of the data is dangerous to read. |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1348 | vios->Stream() << std::flush; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1349 | return false; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1350 | } |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1351 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1352 | ScopedIndentation indent2(vios); |
| 1353 | vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1354 | uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset()); |
| 1355 | if (aligned_code_begin > oat_file_.Size()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1356 | vios->Stream() << StringPrintf("WARNING: " |
| 1357 | "code offset 0x%08x is past end of file 0x%08zx.\n", |
| 1358 | aligned_code_begin, oat_file_.Size()); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1359 | success = false; |
| 1360 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1361 | vios->Stream() << "\n"; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1362 | } |
| 1363 | { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1364 | vios->Stream() << "OatQuickMethodHeader "; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1365 | uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset(); |
| 1366 | const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader(); |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 1367 | stats_.AddBitsIfUnique(Stats::kByteKindQuickMethodHeader, |
| 1368 | sizeof(*method_header) * kBitsPerByte, |
| 1369 | method_header); |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1370 | if (options_.absolute_addresses_) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1371 | vios->Stream() << StringPrintf("%p ", method_header); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1372 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1373 | vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1374 | if (method_header_offset > oat_file_.Size()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1375 | vios->Stream() << StringPrintf( |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1376 | "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n", |
| 1377 | method_header_offset, oat_file_.Size()); |
| 1378 | // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read. |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1379 | vios->Stream() << std::flush; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1380 | return false; |
| 1381 | } |
| 1382 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1383 | ScopedIndentation indent2(vios); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1384 | vios->Stream() << "vmap_table: "; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1385 | if (options_.absolute_addresses_) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1386 | vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable()); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1387 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 1388 | uint32_t vmap_table_offset = method_header == |
| 1389 | nullptr ? 0 : method_header->GetVmapTableOffset(); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1390 | vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset); |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 1391 | |
| 1392 | size_t vmap_table_offset_limit = |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 1393 | IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor) |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 1394 | ? oat_file_.GetVdexFile()->Size() |
| 1395 | : method_header->GetCode() - oat_file_.Begin(); |
| 1396 | if (vmap_table_offset >= vmap_table_offset_limit) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1397 | vios->Stream() << StringPrintf("WARNING: " |
| 1398 | "vmap table offset 0x%08x is past end of file 0x%08zx. " |
| 1399 | "vmap table offset was loaded from offset 0x%08x.\n", |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 1400 | vmap_table_offset, |
| 1401 | vmap_table_offset_limit, |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1402 | oat_method.GetVmapTableOffsetOffset()); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1403 | success = false; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1404 | } else if (options_.dump_vmap_) { |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1405 | DumpVmapData(vios, oat_method, code_item_accessor); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1406 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1407 | } |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1408 | { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1409 | vios->Stream() << "QuickMethodFrameInfo\n"; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1410 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1411 | ScopedIndentation indent2(vios); |
| 1412 | vios->Stream() |
| 1413 | << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes()); |
| 1414 | vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask()); |
| 1415 | DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false); |
| 1416 | vios->Stream() << "\n"; |
| 1417 | vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask()); |
| 1418 | DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true); |
| 1419 | vios->Stream() << "\n"; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1420 | } |
| 1421 | { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1422 | // Based on spill masks from QuickMethodFrameInfo so placed |
| 1423 | // after it is dumped, but useful for understanding quick |
| 1424 | // code, so dumped here. |
| 1425 | ScopedIndentation indent2(vios); |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1426 | DumpVregLocations(vios->Stream(), oat_method, code_item_accessor); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1427 | } |
| 1428 | { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1429 | vios->Stream() << "CODE: "; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1430 | uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset(); |
| 1431 | if (code_size_offset > oat_file_.Size()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1432 | ScopedIndentation indent2(vios); |
| 1433 | vios->Stream() << StringPrintf("WARNING: " |
| 1434 | "code size offset 0x%08x is past end of file 0x%08zx.", |
| 1435 | code_size_offset, oat_file_.Size()); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1436 | success = false; |
| 1437 | } else { |
| 1438 | const void* code = oat_method.GetQuickCode(); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1439 | uint32_t aligned_code_begin = AlignCodeOffset(code_offset); |
| 1440 | uint64_t aligned_code_end = aligned_code_begin + code_size; |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 1441 | stats_.AddBitsIfUnique(Stats::kByteKindCode, code_size * kBitsPerByte, code); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1442 | |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1443 | if (options_.absolute_addresses_) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1444 | vios->Stream() << StringPrintf("%p ", code); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1445 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1446 | vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n", |
| 1447 | code_offset, |
| 1448 | code_size_offset, |
| 1449 | code_size, |
| 1450 | code != nullptr ? "..." : ""); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1451 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1452 | ScopedIndentation indent2(vios); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1453 | if (aligned_code_begin > oat_file_.Size()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1454 | vios->Stream() << StringPrintf("WARNING: " |
| 1455 | "start of code at 0x%08x is past end of file 0x%08zx.", |
| 1456 | aligned_code_begin, oat_file_.Size()); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1457 | success = false; |
| 1458 | } else if (aligned_code_end > oat_file_.Size()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1459 | vios->Stream() << StringPrintf( |
| 1460 | "WARNING: " |
| 1461 | "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. " |
| 1462 | "code size is 0x%08x loaded from offset 0x%08x.\n", |
| 1463 | aligned_code_end, oat_file_.Size(), |
| 1464 | code_size, code_size_offset); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1465 | success = false; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1466 | if (options_.disassemble_code_) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1467 | if (code_size_offset + kPrologueBytes <= oat_file_.Size()) { |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1468 | DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1469 | } |
| 1470 | } |
| 1471 | } else if (code_size > kMaxCodeSize) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1472 | vios->Stream() << StringPrintf( |
| 1473 | "WARNING: " |
| 1474 | "code size %d is bigger than max expected threshold of %d. " |
| 1475 | "code size is 0x%08x loaded from offset 0x%08x.\n", |
| 1476 | code_size, kMaxCodeSize, |
| 1477 | code_size, code_size_offset); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1478 | success = false; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1479 | if (options_.disassemble_code_) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1480 | if (code_size_offset + kPrologueBytes <= oat_file_.Size()) { |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1481 | DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1482 | } |
| 1483 | } |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1484 | } else if (options_.disassemble_code_) { |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1485 | DumpCode(vios, oat_method, code_item_accessor, !success, 0); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1486 | } |
| 1487 | } |
| 1488 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1489 | vios->Stream() << std::flush; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1490 | return success; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1491 | } |
Elliott Hughes | e3c845c | 2012-02-28 17:23:01 -0800 | [diff] [blame] | 1492 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1493 | void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) { |
| 1494 | if (spill_mask == 0) { |
| 1495 | return; |
| 1496 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1497 | os << "("; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1498 | for (size_t i = 0; i < 32; i++) { |
| 1499 | if ((spill_mask & (1 << i)) != 0) { |
| 1500 | if (is_float) { |
| 1501 | os << "fr" << i; |
| 1502 | } else { |
| 1503 | os << "r" << i; |
| 1504 | } |
| 1505 | spill_mask ^= 1 << i; // clear bit |
| 1506 | if (spill_mask != 0) { |
| 1507 | os << ", "; |
| 1508 | } else { |
| 1509 | break; |
| 1510 | } |
| 1511 | } |
| 1512 | } |
| 1513 | os << ")"; |
| 1514 | } |
| 1515 | |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 1516 | // Display data stored at the the vmap offset of an oat method. |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1517 | void DumpVmapData(VariableIndentationOutputStream* vios, |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 1518 | const OatFile::OatMethod& oat_method, |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1519 | const CodeItemDataAccessor& code_item_accessor) { |
| 1520 | if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item_accessor)) { |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1521 | // The optimizing compiler outputs its CodeInfo data in the vmap table. |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 1522 | const void* raw_code_info = oat_method.GetVmapTable(); |
| 1523 | if (raw_code_info != nullptr) { |
| 1524 | CodeInfo code_info(raw_code_info); |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1525 | DCHECK(code_item_accessor.HasCodeItem()); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1526 | ScopedIndentation indent1(vios); |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 1527 | MethodInfo method_info = oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo(); |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1528 | DumpCodeInfo(vios, code_info, oat_method, code_item_accessor, method_info); |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 1529 | } |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1530 | } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)) { |
Nicolas Geoffray | 0a5cd12 | 2015-07-16 14:15:05 +0100 | [diff] [blame] | 1531 | // We don't encode the size in the table, so just emit that we have quickened |
| 1532 | // information. |
| 1533 | ScopedIndentation indent(vios); |
| 1534 | vios->Stream() << "quickened data\n"; |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 1535 | } else { |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 1536 | // Otherwise, there is nothing to display. |
Nicolas Geoffray | 20d3eae | 2014-09-17 11:27:23 +0100 | [diff] [blame] | 1537 | } |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | // Display a CodeInfo object emitted by the optimizing compiler. |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1541 | void DumpCodeInfo(VariableIndentationOutputStream* vios, |
Roland Levillain | 442b46a | 2015-02-18 16:54:21 +0000 | [diff] [blame] | 1542 | const CodeInfo& code_info, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1543 | const OatFile::OatMethod& oat_method, |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1544 | const CodeItemDataAccessor& code_item_accessor, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 1545 | const MethodInfo& method_info) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1546 | code_info.Dump(vios, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1547 | oat_method.GetCodeOffset(), |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1548 | code_item_accessor.RegistersSize(), |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 1549 | options_.dump_code_info_stack_maps_, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 1550 | instruction_set_, |
| 1551 | method_info); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1552 | } |
| 1553 | |
Andreas Gampe | 36a296f | 2017-06-13 14:11:11 -0700 | [diff] [blame] | 1554 | static int GetOutVROffset(uint16_t out_num, InstructionSet isa) { |
| 1555 | // According to stack model, the first out is above the Method referernce. |
| 1556 | return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t); |
| 1557 | } |
| 1558 | |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1559 | static uint32_t GetVRegOffsetFromQuickCode(const CodeItemDataAccessor& code_item_accessor, |
Andreas Gampe | 36a296f | 2017-06-13 14:11:11 -0700 | [diff] [blame] | 1560 | uint32_t core_spills, |
| 1561 | uint32_t fp_spills, |
| 1562 | size_t frame_size, |
| 1563 | int reg, |
| 1564 | InstructionSet isa) { |
| 1565 | PointerSize pointer_size = InstructionSetPointerSize(isa); |
| 1566 | if (kIsDebugBuild) { |
| 1567 | auto* runtime = Runtime::Current(); |
| 1568 | if (runtime != nullptr) { |
| 1569 | CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size); |
| 1570 | } |
| 1571 | } |
| 1572 | DCHECK_ALIGNED(frame_size, kStackAlignment); |
| 1573 | DCHECK_NE(reg, -1); |
| 1574 | int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa) |
| 1575 | + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa) |
| 1576 | + sizeof(uint32_t); // Filler. |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1577 | int num_regs = code_item_accessor.RegistersSize() - code_item_accessor.InsSize(); |
| 1578 | int temp_threshold = code_item_accessor.RegistersSize(); |
Andreas Gampe | 36a296f | 2017-06-13 14:11:11 -0700 | [diff] [blame] | 1579 | const int max_num_special_temps = 1; |
| 1580 | if (reg == temp_threshold) { |
| 1581 | // The current method pointer corresponds to special location on stack. |
| 1582 | return 0; |
| 1583 | } else if (reg >= temp_threshold + max_num_special_temps) { |
| 1584 | /* |
| 1585 | * Special temporaries may have custom locations and the logic above deals with that. |
| 1586 | * However, non-special temporaries are placed relative to the outs. |
| 1587 | */ |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1588 | int temps_start = code_item_accessor.OutsSize() * sizeof(uint32_t) |
Andreas Gampe | 36a296f | 2017-06-13 14:11:11 -0700 | [diff] [blame] | 1589 | + static_cast<size_t>(pointer_size) /* art method */; |
| 1590 | int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t); |
| 1591 | return temps_start + relative_offset; |
| 1592 | } else if (reg < num_regs) { |
| 1593 | int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t); |
| 1594 | return locals_start + (reg * sizeof(uint32_t)); |
| 1595 | } else { |
| 1596 | // Handle ins. |
| 1597 | return frame_size + ((reg - num_regs) * sizeof(uint32_t)) |
| 1598 | + static_cast<size_t>(pointer_size) /* art method */; |
| 1599 | } |
| 1600 | } |
| 1601 | |
Razvan A Lupusoru | faf9f0d | 2014-08-29 17:56:46 -0700 | [diff] [blame] | 1602 | void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method, |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1603 | const CodeItemDataAccessor& code_item_accessor) { |
| 1604 | if (code_item_accessor.HasCodeItem()) { |
| 1605 | size_t num_locals_ins = code_item_accessor.RegistersSize(); |
| 1606 | size_t num_ins = code_item_accessor.InsSize(); |
Razvan A Lupusoru | faf9f0d | 2014-08-29 17:56:46 -0700 | [diff] [blame] | 1607 | size_t num_locals = num_locals_ins - num_ins; |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1608 | size_t num_outs = code_item_accessor.OutsSize(); |
Razvan A Lupusoru | faf9f0d | 2014-08-29 17:56:46 -0700 | [diff] [blame] | 1609 | |
| 1610 | os << "vr_stack_locations:"; |
| 1611 | for (size_t reg = 0; reg <= num_locals_ins; reg++) { |
| 1612 | // For readability, delimit the different kinds of VRs. |
| 1613 | if (reg == num_locals_ins) { |
| 1614 | os << "\n\tmethod*:"; |
| 1615 | } else if (reg == num_locals && num_ins > 0) { |
| 1616 | os << "\n\tins:"; |
| 1617 | } else if (reg == 0 && num_locals > 0) { |
| 1618 | os << "\n\tlocals:"; |
| 1619 | } |
| 1620 | |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1621 | uint32_t offset = GetVRegOffsetFromQuickCode(code_item_accessor, |
Andreas Gampe | 36a296f | 2017-06-13 14:11:11 -0700 | [diff] [blame] | 1622 | oat_method.GetCoreSpillMask(), |
| 1623 | oat_method.GetFpSpillMask(), |
| 1624 | oat_method.GetFrameSizeInBytes(), |
| 1625 | reg, |
| 1626 | GetInstructionSet()); |
Razvan A Lupusoru | faf9f0d | 2014-08-29 17:56:46 -0700 | [diff] [blame] | 1627 | os << " v" << reg << "[sp + #" << offset << "]"; |
| 1628 | } |
| 1629 | |
| 1630 | for (size_t out_reg = 0; out_reg < num_outs; out_reg++) { |
| 1631 | if (out_reg == 0) { |
| 1632 | os << "\n\touts:"; |
| 1633 | } |
| 1634 | |
Andreas Gampe | 36a296f | 2017-06-13 14:11:11 -0700 | [diff] [blame] | 1635 | uint32_t offset = GetOutVROffset(out_reg, GetInstructionSet()); |
Razvan A Lupusoru | faf9f0d | 2014-08-29 17:56:46 -0700 | [diff] [blame] | 1636 | os << " v" << out_reg << "[sp + #" << offset << "]"; |
| 1637 | } |
| 1638 | |
| 1639 | os << "\n"; |
| 1640 | } |
| 1641 | } |
| 1642 | |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1643 | // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by |
| 1644 | // the optimizing compiler? |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1645 | static bool IsMethodGeneratedByOptimizingCompiler( |
| 1646 | const OatFile::OatMethod& oat_method, |
| 1647 | const CodeItemDataAccessor& code_item_accessor) { |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1648 | // If the native GC map is null and the Dex `code_item` is not |
| 1649 | // null, then this method has been compiled with the optimizing |
| 1650 | // compiler. |
Nicolas Geoffray | 0a5cd12 | 2015-07-16 14:15:05 +0100 | [diff] [blame] | 1651 | return oat_method.GetQuickCode() != nullptr && |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 1652 | oat_method.GetVmapTable() != nullptr && |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1653 | code_item_accessor.HasCodeItem(); |
Nicolas Geoffray | 0a5cd12 | 2015-07-16 14:15:05 +0100 | [diff] [blame] | 1654 | } |
| 1655 | |
| 1656 | // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by |
| 1657 | // the dextodex compiler? |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1658 | static bool IsMethodGeneratedByDexToDexCompiler( |
| 1659 | const OatFile::OatMethod& oat_method, |
| 1660 | const CodeItemDataAccessor& code_item_accessor) { |
Nicolas Geoffray | 0a5cd12 | 2015-07-16 14:15:05 +0100 | [diff] [blame] | 1661 | // If the quick code is null, the Dex `code_item` is not |
| 1662 | // null, and the vmap table is not null, then this method has been compiled |
| 1663 | // with the dextodex compiler. |
| 1664 | return oat_method.GetQuickCode() == nullptr && |
| 1665 | oat_method.GetVmapTable() != nullptr && |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1666 | code_item_accessor.HasCodeItem(); |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 1667 | } |
| 1668 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1669 | verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios, |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1670 | StackHandleScope<1>* hs, |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1671 | uint32_t dex_method_idx, |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1672 | const DexFile* dex_file, |
| 1673 | const DexFile::ClassDef& class_def, |
| 1674 | const DexFile::CodeItem* code_item, |
| 1675 | uint32_t method_access_flags) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1676 | if ((method_access_flags & kAccNative) == 0) { |
| 1677 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 1678 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1679 | Handle<mirror::DexCache> dex_cache( |
Mathieu Chartier | f284d44 | 2016-06-02 11:48:30 -0700 | [diff] [blame] | 1680 | hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1681 | CHECK(dex_cache != nullptr); |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1682 | DCHECK(options_.class_loader_ != nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1683 | return verifier::MethodVerifier::VerifyMethodAndDump( |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1684 | soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 1685 | class_def, code_item, nullptr, method_access_flags); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1686 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1687 | |
| 1688 | return nullptr; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1689 | } |
| 1690 | |
Vladimir Marko | adbf93e | 2016-04-08 11:18:25 +0100 | [diff] [blame] | 1691 | // The StackMapsHelper provides the stack maps in the native PC order. |
| 1692 | // For identical native PCs, the order from the CodeInfo is preserved. |
| 1693 | class StackMapsHelper { |
| 1694 | public: |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 1695 | explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set) |
Vladimir Marko | adbf93e | 2016-04-08 11:18:25 +0100 | [diff] [blame] | 1696 | : code_info_(raw_code_info), |
| 1697 | encoding_(code_info_.ExtractEncoding()), |
| 1698 | number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)), |
| 1699 | indexes_(), |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 1700 | offset_(static_cast<uint32_t>(-1)), |
| 1701 | stack_map_index_(0u), |
| 1702 | instruction_set_(instruction_set) { |
Vladimir Marko | adbf93e | 2016-04-08 11:18:25 +0100 | [diff] [blame] | 1703 | if (number_of_stack_maps_ != 0u) { |
| 1704 | // Check if native PCs are ordered. |
| 1705 | bool ordered = true; |
| 1706 | StackMap last = code_info_.GetStackMapAt(0u, encoding_); |
| 1707 | for (size_t i = 1; i != number_of_stack_maps_; ++i) { |
| 1708 | StackMap current = code_info_.GetStackMapAt(i, encoding_); |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 1709 | if (last.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set) > |
| 1710 | current.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set)) { |
Vladimir Marko | adbf93e | 2016-04-08 11:18:25 +0100 | [diff] [blame] | 1711 | ordered = false; |
| 1712 | break; |
| 1713 | } |
| 1714 | last = current; |
| 1715 | } |
| 1716 | if (!ordered) { |
| 1717 | // Create indirection indexes for access in native PC order. We do not optimize |
| 1718 | // for the fact that there can currently be only two separately ordered ranges, |
| 1719 | // namely normal stack maps and catch-point stack maps. |
| 1720 | indexes_.resize(number_of_stack_maps_); |
| 1721 | std::iota(indexes_.begin(), indexes_.end(), 0u); |
| 1722 | std::sort(indexes_.begin(), |
| 1723 | indexes_.end(), |
| 1724 | [this](size_t lhs, size_t rhs) { |
| 1725 | StackMap left = code_info_.GetStackMapAt(lhs, encoding_); |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 1726 | uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map.encoding, |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 1727 | instruction_set_); |
Vladimir Marko | adbf93e | 2016-04-08 11:18:25 +0100 | [diff] [blame] | 1728 | StackMap right = code_info_.GetStackMapAt(rhs, encoding_); |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 1729 | uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map.encoding, |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 1730 | instruction_set_); |
Vladimir Marko | adbf93e | 2016-04-08 11:18:25 +0100 | [diff] [blame] | 1731 | // If the PCs are the same, compare indexes to preserve the original order. |
| 1732 | return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs); |
| 1733 | }); |
| 1734 | } |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 1735 | offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map.encoding, |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 1736 | instruction_set_); |
Vladimir Marko | adbf93e | 2016-04-08 11:18:25 +0100 | [diff] [blame] | 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | const CodeInfo& GetCodeInfo() const { |
| 1741 | return code_info_; |
| 1742 | } |
| 1743 | |
| 1744 | const CodeInfoEncoding& GetEncoding() const { |
| 1745 | return encoding_; |
| 1746 | } |
| 1747 | |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 1748 | uint32_t GetOffset() const { |
Vladimir Marko | adbf93e | 2016-04-08 11:18:25 +0100 | [diff] [blame] | 1749 | return offset_; |
| 1750 | } |
| 1751 | |
| 1752 | StackMap GetStackMap() const { |
| 1753 | return GetStackMapAt(stack_map_index_); |
| 1754 | } |
| 1755 | |
| 1756 | void Next() { |
| 1757 | ++stack_map_index_; |
| 1758 | offset_ = (stack_map_index_ == number_of_stack_maps_) |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 1759 | ? static_cast<uint32_t>(-1) |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 1760 | : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map.encoding, |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 1761 | instruction_set_); |
Vladimir Marko | adbf93e | 2016-04-08 11:18:25 +0100 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | private: |
| 1765 | StackMap GetStackMapAt(size_t i) const { |
| 1766 | if (!indexes_.empty()) { |
| 1767 | i = indexes_[i]; |
| 1768 | } |
| 1769 | DCHECK_LT(i, number_of_stack_maps_); |
| 1770 | return code_info_.GetStackMapAt(i, encoding_); |
| 1771 | } |
| 1772 | |
| 1773 | const CodeInfo code_info_; |
| 1774 | const CodeInfoEncoding encoding_; |
| 1775 | const size_t number_of_stack_maps_; |
| 1776 | dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered. |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 1777 | uint32_t offset_; |
Vladimir Marko | adbf93e | 2016-04-08 11:18:25 +0100 | [diff] [blame] | 1778 | size_t stack_map_index_; |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 1779 | const InstructionSet instruction_set_; |
Vladimir Marko | adbf93e | 2016-04-08 11:18:25 +0100 | [diff] [blame] | 1780 | }; |
| 1781 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1782 | void DumpCode(VariableIndentationOutputStream* vios, |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1783 | const OatFile::OatMethod& oat_method, |
| 1784 | const CodeItemDataAccessor& code_item_accessor, |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1785 | bool bad_input, size_t code_size) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1786 | const void* quick_code = oat_method.GetQuickCode(); |
| 1787 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1788 | if (code_size == 0) { |
| 1789 | code_size = oat_method.GetQuickCodeSize(); |
| 1790 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 1791 | if (code_size == 0 || quick_code == nullptr) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1792 | vios->Stream() << "NO CODE!\n"; |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1793 | return; |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1794 | } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, |
| 1795 | code_item_accessor)) { |
Vladimir Marko | adbf93e | 2016-04-08 11:18:25 +0100 | [diff] [blame] | 1796 | // The optimizing compiler outputs its CodeInfo data in the vmap table. |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 1797 | StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_); |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 1798 | MethodInfo method_info(oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo()); |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 1799 | { |
| 1800 | CodeInfoEncoding encoding(helper.GetEncoding()); |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 1801 | StackMapEncoding stack_map_encoding(encoding.stack_map.encoding); |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 1802 | const size_t num_stack_maps = encoding.stack_map.num_entries; |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 1803 | if (stats_.AddBitsIfUnique(Stats::kByteKindCodeInfoEncoding, |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 1804 | encoding.HeaderSize() * kBitsPerByte, |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 1805 | oat_method.GetVmapTable())) { |
Mathieu Chartier | 1e08379 | 2017-02-08 13:30:04 -0800 | [diff] [blame] | 1806 | // Stack maps |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 1807 | stats_.AddBits( |
| 1808 | Stats::kByteKindStackMapNativePc, |
| 1809 | stack_map_encoding.GetNativePcEncoding().BitSize() * num_stack_maps); |
| 1810 | stats_.AddBits( |
| 1811 | Stats::kByteKindStackMapDexPc, |
| 1812 | stack_map_encoding.GetDexPcEncoding().BitSize() * num_stack_maps); |
| 1813 | stats_.AddBits( |
| 1814 | Stats::kByteKindStackMapDexRegisterMap, |
| 1815 | stack_map_encoding.GetDexRegisterMapEncoding().BitSize() * num_stack_maps); |
| 1816 | stats_.AddBits( |
Mathieu Chartier | 1e08379 | 2017-02-08 13:30:04 -0800 | [diff] [blame] | 1817 | Stats::kByteKindStackMapInlineInfoIndex, |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 1818 | stack_map_encoding.GetInlineInfoEncoding().BitSize() * num_stack_maps); |
| 1819 | stats_.AddBits( |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 1820 | Stats::kByteKindStackMapRegisterMaskIndex, |
| 1821 | stack_map_encoding.GetRegisterMaskIndexEncoding().BitSize() * num_stack_maps); |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 1822 | stats_.AddBits( |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 1823 | Stats::kByteKindStackMapStackMaskIndex, |
| 1824 | stack_map_encoding.GetStackMaskIndexEncoding().BitSize() * num_stack_maps); |
Mathieu Chartier | 1e08379 | 2017-02-08 13:30:04 -0800 | [diff] [blame] | 1825 | |
| 1826 | // Stack masks |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 1827 | stats_.AddBits( |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 1828 | Stats::kByteKindCodeInfoStackMasks, |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 1829 | encoding.stack_mask.encoding.BitSize() * encoding.stack_mask.num_entries); |
Mathieu Chartier | 1e08379 | 2017-02-08 13:30:04 -0800 | [diff] [blame] | 1830 | |
| 1831 | // Register masks |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 1832 | stats_.AddBits( |
| 1833 | Stats::kByteKindCodeInfoRegisterMasks, |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 1834 | encoding.register_mask.encoding.BitSize() * encoding.register_mask.num_entries); |
Mathieu Chartier | 1e08379 | 2017-02-08 13:30:04 -0800 | [diff] [blame] | 1835 | |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 1836 | // Invoke infos |
| 1837 | if (encoding.invoke_info.num_entries > 0u) { |
| 1838 | stats_.AddBits( |
| 1839 | Stats::kByteKindCodeInfoInvokeInfo, |
| 1840 | encoding.invoke_info.encoding.BitSize() * encoding.invoke_info.num_entries); |
| 1841 | } |
| 1842 | |
Mathieu Chartier | 1e08379 | 2017-02-08 13:30:04 -0800 | [diff] [blame] | 1843 | // Location catalog |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 1844 | const size_t location_catalog_bytes = |
| 1845 | helper.GetCodeInfo().GetDexRegisterLocationCatalogSize(encoding); |
| 1846 | stats_.AddBits(Stats::kByteKindCodeInfoLocationCatalog, |
| 1847 | kBitsPerByte * location_catalog_bytes); |
Mathieu Chartier | 1e08379 | 2017-02-08 13:30:04 -0800 | [diff] [blame] | 1848 | // Dex register bytes. |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 1849 | const size_t dex_register_bytes = |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1850 | helper.GetCodeInfo().GetDexRegisterMapsSize(encoding, |
| 1851 | code_item_accessor.RegistersSize()); |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 1852 | stats_.AddBits( |
| 1853 | Stats::kByteKindCodeInfoDexRegisterMap, |
| 1854 | kBitsPerByte * dex_register_bytes); |
Mathieu Chartier | 1e08379 | 2017-02-08 13:30:04 -0800 | [diff] [blame] | 1855 | |
| 1856 | // Inline infos. |
| 1857 | const size_t num_inline_infos = encoding.inline_info.num_entries; |
| 1858 | if (num_inline_infos > 0u) { |
| 1859 | stats_.AddBits( |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 1860 | Stats::kByteKindInlineInfoMethodIndexIdx, |
| 1861 | encoding.inline_info.encoding.GetMethodIndexIdxEncoding().BitSize() * |
| 1862 | num_inline_infos); |
Mathieu Chartier | 1e08379 | 2017-02-08 13:30:04 -0800 | [diff] [blame] | 1863 | stats_.AddBits( |
| 1864 | Stats::kByteKindInlineInfoDexPc, |
| 1865 | encoding.inline_info.encoding.GetDexPcEncoding().BitSize() * num_inline_infos); |
| 1866 | stats_.AddBits( |
| 1867 | Stats::kByteKindInlineInfoExtraData, |
| 1868 | encoding.inline_info.encoding.GetExtraDataEncoding().BitSize() * num_inline_infos); |
| 1869 | stats_.AddBits( |
| 1870 | Stats::kByteKindInlineInfoDexRegisterMap, |
| 1871 | encoding.inline_info.encoding.GetDexRegisterMapEncoding().BitSize() * |
| 1872 | num_inline_infos); |
| 1873 | stats_.AddBits(Stats::kByteKindInlineInfoIsLast, num_inline_infos); |
| 1874 | } |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 1875 | } |
| 1876 | } |
Vladimir Marko | adbf93e | 2016-04-08 11:18:25 +0100 | [diff] [blame] | 1877 | const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code); |
| 1878 | size_t offset = 0; |
| 1879 | while (offset < code_size) { |
| 1880 | offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset); |
| 1881 | if (offset == helper.GetOffset()) { |
| 1882 | ScopedIndentation indent1(vios); |
| 1883 | StackMap stack_map = helper.GetStackMap(); |
| 1884 | DCHECK(stack_map.IsValid()); |
| 1885 | stack_map.Dump(vios, |
| 1886 | helper.GetCodeInfo(), |
| 1887 | helper.GetEncoding(), |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 1888 | method_info, |
Vladimir Marko | adbf93e | 2016-04-08 11:18:25 +0100 | [diff] [blame] | 1889 | oat_method.GetCodeOffset(), |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 1890 | code_item_accessor.RegistersSize(), |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 1891 | instruction_set_); |
Vladimir Marko | adbf93e | 2016-04-08 11:18:25 +0100 | [diff] [blame] | 1892 | do { |
| 1893 | helper.Next(); |
| 1894 | // There may be multiple stack maps at a given PC. We display only the first one. |
| 1895 | } while (offset == helper.GetOffset()); |
| 1896 | } |
| 1897 | DCHECK_LT(offset, helper.GetOffset()); |
| 1898 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 1899 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1900 | const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code); |
| 1901 | size_t offset = 0; |
| 1902 | while (offset < code_size) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1903 | offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1904 | } |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 1905 | } |
| 1906 | } |
| 1907 | |
Vladimir Marko | f3c52b4 | 2017-11-17 17:32:12 +0000 | [diff] [blame] | 1908 | template <typename NameGetter> |
| 1909 | void DumpBssEntries(std::ostream& os, |
| 1910 | const char* slot_type, |
| 1911 | const IndexBssMapping* mapping, |
| 1912 | uint32_t number_of_indexes, |
| 1913 | size_t slot_size, |
| 1914 | NameGetter name) { |
| 1915 | os << ".bss mapping for " << slot_type << ": "; |
| 1916 | if (mapping == nullptr) { |
| 1917 | os << "empty.\n"; |
| 1918 | return; |
| 1919 | } |
| 1920 | size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes); |
| 1921 | size_t num_valid_indexes = 0u; |
| 1922 | for (const IndexBssMappingEntry& entry : *mapping) { |
| 1923 | num_valid_indexes += 1u + POPCOUNT(entry.GetMask(index_bits)); |
| 1924 | } |
| 1925 | os << mapping->size() << " entries for " << num_valid_indexes << " valid indexes.\n"; |
| 1926 | os << std::hex; |
| 1927 | for (const IndexBssMappingEntry& entry : *mapping) { |
| 1928 | uint32_t index = entry.GetIndex(index_bits); |
| 1929 | uint32_t mask = entry.GetMask(index_bits); |
| 1930 | size_t bss_offset = entry.bss_offset - POPCOUNT(mask) * slot_size; |
| 1931 | for (uint32_t n : LowToHighBits(mask)) { |
| 1932 | size_t current_index = index - (32u - index_bits) + n; |
| 1933 | os << " 0x" << bss_offset << ": " << slot_type << ": " << name(current_index) << "\n"; |
| 1934 | bss_offset += slot_size; |
| 1935 | } |
| 1936 | DCHECK_EQ(bss_offset, entry.bss_offset); |
| 1937 | os << " 0x" << bss_offset << ": " << slot_type << ": " << name(index) << "\n"; |
| 1938 | } |
| 1939 | os << std::dec; |
| 1940 | } |
| 1941 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1942 | const OatFile& oat_file_; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1943 | const std::vector<const OatFile::OatDexFile*> oat_dex_files_; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 1944 | const OatDumperOptions& options_; |
| 1945 | uint32_t resolved_addr2instr_; |
Andreas Gampe | 372f3a3 | 2016-08-19 10:49:06 -0700 | [diff] [blame] | 1946 | const InstructionSet instruction_set_; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1947 | std::set<uintptr_t> offsets_; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1948 | Disassembler* disassembler_; |
Mathieu Chartier | 5e7c6a9 | 2017-01-17 16:38:30 -0800 | [diff] [blame] | 1949 | Stats stats_; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1950 | }; |
| 1951 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1952 | class ImageDumper { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1953 | public: |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1954 | ImageDumper(std::ostream* os, |
| 1955 | gc::space::ImageSpace& image_space, |
| 1956 | const ImageHeader& image_header, |
| 1957 | OatDumperOptions* oat_dumper_options) |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1958 | : os_(os), |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1959 | vios_(os), |
| 1960 | indent1_(&vios_), |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 1961 | image_space_(image_space), |
| 1962 | image_header_(image_header), |
| 1963 | oat_dumper_options_(oat_dumper_options) {} |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1964 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1965 | bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1966 | std::ostream& os = *os_; |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1967 | std::ostream& indent_os = vios_.Stream(); |
| 1968 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1969 | os << "MAGIC: " << image_header_.GetMagic() << "\n\n"; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 1970 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1971 | os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n"; |
| 1972 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1973 | os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1974 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1975 | os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n"; |
| 1976 | |
| 1977 | for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) { |
| 1978 | auto section = static_cast<ImageHeader::ImageSections>(i); |
| 1979 | os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n"; |
| 1980 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 1981 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1982 | os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum()); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 1983 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1984 | os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1985 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1986 | os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n"; |
| 1987 | |
| 1988 | os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n"; |
| 1989 | |
| 1990 | os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1991 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1992 | os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n"; |
| 1993 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1994 | os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n"; |
| 1995 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1996 | { |
| 1997 | os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n"; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1998 | static_assert(arraysize(image_roots_descriptions_) == |
| 1999 | static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match"); |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 2000 | DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax); |
| 2001 | for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2002 | ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i); |
| 2003 | const char* image_root_description = image_roots_descriptions_[i]; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2004 | mirror::Object* image_root_object = image_header_.GetImageRoot(image_root); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2005 | indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object); |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 2006 | if (image_root_object != nullptr && image_root_object->IsObjectArray()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2007 | mirror::ObjectArray<mirror::Object>* image_root_object_array |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 2008 | = image_root_object->AsObjectArray<mirror::Object>(); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2009 | ScopedIndentation indent2(&vios_); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2010 | for (int j = 0; j < image_root_object_array->GetLength(); j++) { |
| 2011 | mirror::Object* value = image_root_object_array->Get(j); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 2012 | size_t run = 0; |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2013 | for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) { |
| 2014 | if (value == image_root_object_array->Get(k)) { |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 2015 | run++; |
| 2016 | } else { |
| 2017 | break; |
| 2018 | } |
| 2019 | } |
| 2020 | if (run == 0) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2021 | indent_os << StringPrintf("%d: ", j); |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 2022 | } else { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2023 | indent_os << StringPrintf("%d to %zd: ", j, j + run); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2024 | j = j + run; |
Ian Rogers | fa82427 | 2013-11-05 16:12:57 -0800 | [diff] [blame] | 2025 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2026 | if (value != nullptr) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2027 | PrettyObjectValue(indent_os, value->GetClass(), value); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2028 | } else { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2029 | indent_os << j << ": null\n"; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2030 | } |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 2031 | } |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 2032 | } |
| 2033 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2034 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2035 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2036 | { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2037 | os << "METHOD ROOTS\n"; |
| 2038 | static_assert(arraysize(image_methods_descriptions_) == |
| 2039 | static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match"); |
| 2040 | for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) { |
| 2041 | auto image_root = static_cast<ImageHeader::ImageMethod>(i); |
| 2042 | const char* description = image_methods_descriptions_[i]; |
| 2043 | auto* image_method = image_header_.GetImageMethod(image_root); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2044 | indent_os << StringPrintf("%s: %p\n", description, image_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2045 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2046 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2047 | os << "\n"; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2048 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 2049 | Runtime* const runtime = Runtime::Current(); |
| 2050 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 2051 | std::string image_filename = image_space_.GetImageFilename(); |
| 2052 | std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2053 | os << "OAT LOCATION: " << oat_location; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2054 | os << "\n"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 2055 | std::string error_msg; |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2056 | const OatFile* oat_file = image_space_.GetOatFile(); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 2057 | if (oat_file == nullptr) { |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2058 | oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location); |
| 2059 | } |
| 2060 | if (oat_file == nullptr) { |
| 2061 | oat_file = OatFile::Open(oat_location, |
| 2062 | oat_location, |
| 2063 | nullptr, |
| 2064 | nullptr, |
| 2065 | false, |
| 2066 | /*low_4gb*/false, |
| 2067 | nullptr, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2068 | &error_msg); |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2069 | } |
| 2070 | if (oat_file == nullptr) { |
| 2071 | os << "OAT FILE NOT FOUND: " << error_msg << "\n"; |
| 2072 | return EXIT_FAILURE; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2073 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2074 | os << "\n"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2075 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2076 | stats_.oat_file_bytes = oat_file->Size(); |
| 2077 | |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 2078 | oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_)); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2079 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 2080 | for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2081 | CHECK(oat_dex_file != nullptr); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 2082 | stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(), |
| 2083 | oat_dex_file->FileSize())); |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 2084 | } |
| 2085 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2086 | os << "OBJECTS:\n" << std::flush; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 2087 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2088 | // Loop through the image space and dump its objects. |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 2089 | gc::Heap* heap = runtime->GetHeap(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2090 | Thread* self = Thread::Current(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 2091 | { |
Mathieu Chartier | c22c59e | 2014-02-24 15:16:06 -0800 | [diff] [blame] | 2092 | { |
| 2093 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 2094 | heap->FlushAllocStack(); |
| 2095 | } |
Hiroshi Yamauchi | 90d7068 | 2014-02-20 16:17:30 -0800 | [diff] [blame] | 2096 | // Since FlushAllocStack() above resets the (active) allocation |
| 2097 | // stack. Need to revoke the thread-local allocation stacks that |
| 2098 | // point into it. |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 2099 | ScopedThreadSuspension sts(self, kNative); |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 2100 | ScopedSuspendAll ssa(__FUNCTION__); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 2101 | heap->RevokeAllThreadLocalAllocationStacks(self); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 2102 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2103 | { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2104 | // Mark dex caches. |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2105 | dex_caches_.clear(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2106 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 2107 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 2108 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 2109 | ObjPtr<mirror::DexCache> dex_cache = |
| 2110 | ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2111 | if (dex_cache != nullptr) { |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 2112 | dex_caches_.insert(dex_cache.Ptr()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2113 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2114 | } |
| 2115 | } |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 2116 | auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2117 | DumpObject(obj); |
| 2118 | }; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2119 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2120 | // Dump the normal objects before ArtMethods. |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 2121 | image_space_.GetLiveBitmap()->Walk(dump_visitor); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2122 | indent_os << "\n"; |
| 2123 | // TODO: Dump fields. |
| 2124 | // Dump methods after. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2125 | DumpArtMethodVisitor visitor(this); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 2126 | image_header_.VisitPackedArtMethods(&visitor, |
| 2127 | image_space_.Begin(), |
| 2128 | image_header_.GetPointerSize()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2129 | // Dump the large objects separately. |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 2130 | heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2131 | indent_os << "\n"; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 2132 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2133 | os << "STATS:\n" << std::flush; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2134 | std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str())); |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2135 | size_t data_size = image_header_.GetDataSize(); // stored size in file. |
| 2136 | if (file == nullptr) { |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 2137 | LOG(WARNING) << "Failed to find image in " << image_filename; |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2138 | } else { |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 2139 | stats_.file_bytes = file->GetLength(); |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2140 | // If the image is compressed, adjust to decompressed size. |
| 2141 | size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader); |
| 2142 | if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) { |
| 2143 | DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image"; |
| 2144 | } |
| 2145 | stats_.file_bytes += uncompressed_size - data_size; |
Brian Carlstrom | 6f27775 | 2013-09-30 17:56:45 -0700 | [diff] [blame] | 2146 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2147 | size_t header_bytes = sizeof(ImageHeader); |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 2148 | const auto& object_section = image_header_.GetObjectsSection(); |
| 2149 | const auto& field_section = image_header_.GetFieldsSection(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2150 | const auto& method_section = image_header_.GetMethodsSection(); |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 2151 | const auto& dex_cache_arrays_section = image_header_.GetDexCacheArraysSection(); |
| 2152 | const auto& intern_section = image_header_.GetInternedStringsSection(); |
| 2153 | const auto& class_table_section = image_header_.GetClassTableSection(); |
| 2154 | const auto& bitmap_section = image_header_.GetImageBitmapSection(); |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 2155 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2156 | stats_.header_bytes = header_bytes; |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 2157 | |
| 2158 | // Objects are kObjectAlignment-aligned. |
| 2159 | // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset()); |
| 2160 | if (object_section.Offset() > header_bytes) { |
| 2161 | stats_.alignment_bytes += object_section.Offset() - header_bytes; |
| 2162 | } |
| 2163 | |
| 2164 | // Field section is 4-byte aligned. |
| 2165 | constexpr size_t kFieldSectionAlignment = 4U; |
| 2166 | uint32_t end_objects = object_section.Offset() + object_section.Size(); |
| 2167 | CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset()); |
| 2168 | stats_.alignment_bytes += field_section.Offset() - end_objects; |
| 2169 | |
| 2170 | // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment. |
| 2171 | uint32_t end_fields = field_section.Offset() + field_section.Size(); |
| 2172 | CHECK_ALIGNED(method_section.Offset(), 4); |
| 2173 | stats_.alignment_bytes += method_section.Offset() - end_fields; |
| 2174 | |
| 2175 | // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment. |
| 2176 | uint32_t end_methods = method_section.Offset() + method_section.Size(); |
| 2177 | CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4); |
| 2178 | stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods; |
| 2179 | |
| 2180 | // Intern table is 8-byte aligned. |
| 2181 | uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size(); |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 2182 | CHECK_ALIGNED(intern_section.Offset(), sizeof(uint64_t)); |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 2183 | stats_.alignment_bytes += intern_section.Offset() - end_caches; |
| 2184 | |
| 2185 | // Add space between intern table and class table. |
| 2186 | uint32_t end_intern = intern_section.Offset() + intern_section.Size(); |
| 2187 | stats_.alignment_bytes += class_table_section.Offset() - end_intern; |
| 2188 | |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2189 | // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned. |
| 2190 | const size_t bitmap_offset = sizeof(ImageHeader) + data_size; |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 2191 | CHECK_ALIGNED(bitmap_section.Offset(), kPageSize); |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2192 | stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset; |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 2193 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2194 | stats_.bitmap_bytes += bitmap_section.Size(); |
| 2195 | stats_.art_field_bytes += field_section.Size(); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 2196 | stats_.art_method_bytes += method_section.Size(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2197 | stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size(); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 2198 | stats_.interned_strings_bytes += intern_section.Size(); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 2199 | stats_.class_table_bytes += class_table_section.Size(); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2200 | stats_.Dump(os, indent_os); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2201 | os << "\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2202 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2203 | os << std::flush; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2204 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 2205 | return oat_dumper_->Dump(os); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 2206 | } |
| 2207 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2208 | private: |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2209 | class DumpArtMethodVisitor : public ArtMethodVisitor { |
| 2210 | public: |
| 2211 | explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {} |
| 2212 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2213 | virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2214 | std::ostream& indent_os = image_dumper_->vios_.Stream(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2215 | indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n"; |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2216 | image_dumper_->DumpMethod(method, indent_os); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2217 | indent_os << "\n"; |
| 2218 | } |
| 2219 | |
| 2220 | private: |
| 2221 | ImageDumper* const image_dumper_; |
| 2222 | }; |
| 2223 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2224 | static void PrettyObjectValue(std::ostream& os, |
| 2225 | ObjPtr<mirror::Class> type, |
| 2226 | ObjPtr<mirror::Object> value) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2227 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2228 | CHECK(type != nullptr); |
| 2229 | if (value == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2230 | os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 2231 | } else if (type->IsStringClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2232 | mirror::String* string = value->AsString(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2233 | os << StringPrintf("%p String: %s\n", string, |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2234 | PrintableString(string->ToModifiedUtf8().c_str()).c_str()); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 2235 | } else if (type->IsClassClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2236 | mirror::Class* klass = value->AsClass(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2237 | os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 2238 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2239 | os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 2240 | } |
| 2241 | } |
| 2242 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2243 | static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2244 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 2245 | os << StringPrintf("%s: ", field->GetName()); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 2246 | switch (field->GetTypeAsPrimitiveType()) { |
| 2247 | case Primitive::kPrimLong: |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2248 | os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj)); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 2249 | break; |
| 2250 | case Primitive::kPrimDouble: |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2251 | os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj)); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 2252 | break; |
| 2253 | case Primitive::kPrimFloat: |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2254 | os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj)); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 2255 | break; |
| 2256 | case Primitive::kPrimInt: |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2257 | os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj)); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 2258 | break; |
| 2259 | case Primitive::kPrimChar: |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2260 | os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj)); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 2261 | break; |
| 2262 | case Primitive::kPrimShort: |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2263 | os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj)); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 2264 | break; |
| 2265 | case Primitive::kPrimBoolean: |
Roland Levillain | 5e8d5f0 | 2016-10-18 18:03:43 +0100 | [diff] [blame] | 2266 | os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false", |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2267 | field->GetBoolean(obj)); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 2268 | break; |
| 2269 | case Primitive::kPrimByte: |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2270 | os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj)); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 2271 | break; |
| 2272 | case Primitive::kPrimNot: { |
| 2273 | // Get the value, don't compute the type unless it is non-null as we don't want |
| 2274 | // to cause class loading. |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2275 | ObjPtr<mirror::Object> value = field->GetObj(obj); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 2276 | if (value == nullptr) { |
| 2277 | os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str()); |
Ian Rogers | 50239c7 | 2013-06-17 14:53:22 -0700 | [diff] [blame] | 2278 | } else { |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 2279 | // Grab the field type without causing resolution. |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 2280 | ObjPtr<mirror::Class> field_type = field->LookupResolvedType(); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 2281 | if (field_type != nullptr) { |
| 2282 | PrettyObjectValue(os, field_type, value); |
| 2283 | } else { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2284 | os << StringPrintf("%p %s\n", |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 2285 | value.Ptr(), |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 2286 | PrettyDescriptor(field->GetTypeDescriptor()).c_str()); |
| 2287 | } |
Ian Rogers | 50239c7 | 2013-06-17 14:53:22 -0700 | [diff] [blame] | 2288 | } |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 2289 | break; |
Ian Rogers | 48efc2b | 2012-08-27 17:20:31 -0700 | [diff] [blame] | 2290 | } |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 2291 | default: |
| 2292 | os << "unexpected field type: " << field->GetTypeDescriptor() << "\n"; |
| 2293 | break; |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 2294 | } |
| 2295 | } |
| 2296 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2297 | static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2298 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2299 | mirror::Class* super = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2300 | if (super != nullptr) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2301 | DumpFields(os, obj, super); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 2302 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2303 | for (ArtField& field : klass->GetIFields()) { |
| 2304 | PrintField(os, &field, obj); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 2305 | } |
| 2306 | } |
| 2307 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2308 | bool InDumpSpace(const mirror::Object* object) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2309 | return image_space_.Contains(object); |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 2310 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2311 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2312 | const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | a7dd038 | 2014-11-20 17:08:58 -0800 | [diff] [blame] | 2313 | const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize( |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2314 | image_header_.GetPointerSize()); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2315 | if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2316 | quick_code = oat_dumper_->GetQuickOatCode(m); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2317 | } |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 2318 | if (oat_dumper_->GetInstructionSet() == InstructionSet::kThumb2) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2319 | quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1); |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 2320 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2321 | return quick_code; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2322 | } |
| 2323 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2324 | uint32_t GetQuickOatCodeSize(ArtMethod* m) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2325 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2326 | const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m)); |
| 2327 | if (oat_code_begin == nullptr) { |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 2328 | return 0; |
| 2329 | } |
| 2330 | return oat_code_begin[-1]; |
| 2331 | } |
| 2332 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2333 | const void* GetQuickOatCodeEnd(ArtMethod* m) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2334 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2335 | 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] | 2336 | if (oat_code_begin == nullptr) { |
| 2337 | return nullptr; |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 2338 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2339 | return oat_code_begin + GetQuickOatCodeSize(m); |
Brian Carlstrom | f8bbb84 | 2012-03-14 03:01:42 -0700 | [diff] [blame] | 2340 | } |
| 2341 | |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 2342 | void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2343 | DCHECK(obj != nullptr); |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 2344 | if (!InDumpSpace(obj)) { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 2345 | return; |
| 2346 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2347 | |
| 2348 | size_t object_bytes = obj->SizeOf(); |
| 2349 | size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes; |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 2350 | stats_.object_bytes += object_bytes; |
| 2351 | stats_.alignment_bytes += alignment_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2352 | |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 2353 | std::ostream& os = vios_.Stream(); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2354 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2355 | mirror::Class* obj_class = obj->GetClass(); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 2356 | if (obj_class->IsArrayClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2357 | os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2358 | obj->AsArray()->GetLength()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 2359 | } else if (obj->IsClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2360 | mirror::Class* klass = obj->AsClass(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2361 | os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, |
| 2362 | mirror::Class::PrettyDescriptor(klass).c_str()) |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2363 | << klass->GetStatus() << ")\n"; |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 2364 | } else if (obj_class->IsStringClass()) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2365 | os << StringPrintf("%p: java.lang.String %s\n", obj, |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2366 | PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 2367 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2368 | os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 2369 | } |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 2370 | ScopedIndentation indent1(&vios_); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2371 | DumpFields(os, obj, obj_class); |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 2372 | const PointerSize image_pointer_size = image_header_.GetPointerSize(); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 2373 | if (obj->IsObjectArray()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2374 | auto* obj_array = obj->AsObjectArray<mirror::Object>(); |
| 2375 | for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2376 | mirror::Object* value = obj_array->Get(i); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 2377 | size_t run = 0; |
| 2378 | for (int32_t j = i + 1; j < length; j++) { |
| 2379 | if (value == obj_array->Get(j)) { |
| 2380 | run++; |
| 2381 | } else { |
| 2382 | break; |
| 2383 | } |
| 2384 | } |
| 2385 | if (run == 0) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2386 | os << StringPrintf("%d: ", i); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 2387 | } else { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2388 | os << StringPrintf("%d to %zd: ", i, i + run); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 2389 | i = i + run; |
| 2390 | } |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 2391 | mirror::Class* value_class = |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2392 | (value == nullptr) ? obj_class->GetComponentType() : value->GetClass(); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2393 | PrettyObjectValue(os, value_class, value); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 2394 | } |
| 2395 | } else if (obj->IsClass()) { |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 2396 | ObjPtr<mirror::Class> klass = obj->AsClass(); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2397 | |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 2398 | if (kBitstringSubtypeCheckEnabled) { |
| 2399 | os << "SUBTYPE_CHECK_BITS: "; |
| 2400 | SubtypeCheck<ObjPtr<mirror::Class>>::Dump(klass, os); |
| 2401 | os << "\n"; |
| 2402 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2403 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2404 | if (klass->NumStaticFields() != 0) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2405 | os << "STATICS:\n"; |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 2406 | ScopedIndentation indent2(&vios_); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2407 | for (ArtField& field : klass->GetSFields()) { |
| 2408 | PrintField(os, &field, field.GetDeclaringClass()); |
Ian Rogers | d5b3260 | 2012-02-26 16:40:04 -0800 | [diff] [blame] | 2409 | } |
| 2410 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2411 | } else { |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 2412 | auto it = dex_caches_.find(obj); |
| 2413 | if (it != dex_caches_.end()) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2414 | auto* dex_cache = down_cast<mirror::DexCache*>(obj); |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 2415 | const auto& field_section = image_header_.GetFieldsSection(); |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 2416 | const auto& method_section = image_header_.GetMethodsSection(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2417 | size_t num_methods = dex_cache->NumResolvedMethods(); |
| 2418 | if (num_methods != 0u) { |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 2419 | os << "Methods (size=" << num_methods << "):\n"; |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 2420 | ScopedIndentation indent2(&vios_); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 2421 | mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2422 | for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 2423 | ArtMethod* elem = mirror::DexCache::GetNativePairPtrSize( |
| 2424 | resolved_methods, i, image_pointer_size).object; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2425 | size_t run = 0; |
| 2426 | for (size_t j = i + 1; |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 2427 | j != length && |
| 2428 | elem == mirror::DexCache::GetNativePairPtrSize( |
| 2429 | resolved_methods, j, image_pointer_size).object; |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 2430 | ++j) { |
| 2431 | ++run; |
| 2432 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2433 | if (run == 0) { |
| 2434 | os << StringPrintf("%zd: ", i); |
| 2435 | } else { |
| 2436 | os << StringPrintf("%zd to %zd: ", i, i + run); |
| 2437 | i = i + run; |
| 2438 | } |
| 2439 | std::string msg; |
| 2440 | if (elem == nullptr) { |
| 2441 | msg = "null"; |
| 2442 | } else if (method_section.Contains( |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 2443 | reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2444 | msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2445 | } else { |
| 2446 | msg = "<not in method section>"; |
| 2447 | } |
| 2448 | os << StringPrintf("%p %s\n", elem, msg.c_str()); |
Ian Rogers | 0d2d378 | 2012-04-10 11:09:18 -0700 | [diff] [blame] | 2449 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2450 | } |
| 2451 | size_t num_fields = dex_cache->NumResolvedFields(); |
| 2452 | if (num_fields != 0u) { |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 2453 | os << "Fields (size=" << num_fields << "):\n"; |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 2454 | ScopedIndentation indent2(&vios_); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2455 | auto* resolved_fields = dex_cache->GetResolvedFields(); |
| 2456 | for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 2457 | ArtField* elem = mirror::DexCache::GetNativePairPtrSize( |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 2458 | resolved_fields, i, image_pointer_size).object; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2459 | size_t run = 0; |
| 2460 | for (size_t j = i + 1; |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 2461 | j != length && |
| 2462 | elem == mirror::DexCache::GetNativePairPtrSize( |
| 2463 | resolved_fields, j, image_pointer_size).object; |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 2464 | ++j) { |
| 2465 | ++run; |
| 2466 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2467 | if (run == 0) { |
| 2468 | os << StringPrintf("%zd: ", i); |
| 2469 | } else { |
| 2470 | os << StringPrintf("%zd to %zd: ", i, i + run); |
| 2471 | i = i + run; |
| 2472 | } |
| 2473 | std::string msg; |
| 2474 | if (elem == nullptr) { |
| 2475 | msg = "null"; |
| 2476 | } else if (field_section.Contains( |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 2477 | reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2478 | msg = reinterpret_cast<ArtField*>(elem)->PrettyField(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2479 | } else { |
| 2480 | msg = "<not in field section>"; |
| 2481 | } |
| 2482 | os << StringPrintf("%p %s\n", elem, msg.c_str()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2483 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2484 | } |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 2485 | size_t num_types = dex_cache->NumResolvedTypes(); |
| 2486 | if (num_types != 0u) { |
| 2487 | os << "Types (size=" << num_types << "):\n"; |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 2488 | ScopedIndentation indent2(&vios_); |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 2489 | auto* resolved_types = dex_cache->GetResolvedTypes(); |
| 2490 | for (size_t i = 0; i < num_types; ++i) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 2491 | auto pair = resolved_types[i].load(std::memory_order_relaxed); |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 2492 | size_t run = 0; |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 2493 | for (size_t j = i + 1; j != num_types; ++j) { |
| 2494 | auto other_pair = resolved_types[j].load(std::memory_order_relaxed); |
| 2495 | if (pair.index != other_pair.index || |
| 2496 | pair.object.Read() != other_pair.object.Read()) { |
| 2497 | break; |
| 2498 | } |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 2499 | ++run; |
| 2500 | } |
| 2501 | if (run == 0) { |
| 2502 | os << StringPrintf("%zd: ", i); |
| 2503 | } else { |
| 2504 | os << StringPrintf("%zd to %zd: ", i, i + run); |
| 2505 | i = i + run; |
| 2506 | } |
| 2507 | std::string msg; |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 2508 | auto* elem = pair.object.Read(); |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 2509 | if (elem == nullptr) { |
| 2510 | msg = "null"; |
| 2511 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2512 | msg = elem->PrettyClass(); |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 2513 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 2514 | os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str()); |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 2515 | } |
| 2516 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 2517 | } |
| 2518 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2519 | std::string temp; |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 2520 | stats_.Update(obj_class->GetDescriptor(&temp), object_bytes); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 2521 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2522 | |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2523 | void DumpMethod(ArtMethod* method, std::ostream& indent_os) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2524 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2525 | DCHECK(method != nullptr); |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2526 | const void* quick_oat_code_begin = GetQuickOatCodeBegin(method); |
| 2527 | const void* quick_oat_code_end = GetQuickOatCodeEnd(method); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2528 | const PointerSize pointer_size = image_header_.GetPointerSize(); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 2529 | OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>( |
| 2530 | reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2531 | if (method->IsNative()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2532 | bool first_occurrence; |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2533 | uint32_t quick_oat_code_size = GetQuickOatCodeSize(method); |
| 2534 | ComputeOatSize(quick_oat_code_begin, &first_occurrence); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2535 | if (first_occurrence) { |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2536 | stats_.native_to_managed_code_bytes += quick_oat_code_size; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2537 | } |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2538 | if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize( |
| 2539 | image_header_.GetPointerSize())) { |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 2540 | indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2541 | } |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 2542 | } else if (method->IsAbstract() || method->IsClassInitializer()) { |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 2543 | // Don't print information for these. |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 2544 | } else if (method->IsRuntimeMethod()) { |
| 2545 | ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize()); |
| 2546 | if (table != nullptr) { |
| 2547 | indent_os << "IMT conflict table " << table << " method: "; |
| 2548 | for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2549 | indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size)) |
| 2550 | << " "; |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 2551 | } |
| 2552 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2553 | } else { |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 2554 | CodeItemDataAccessor code_item_accessor(method->DexInstructionData()); |
Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 2555 | size_t dex_instruction_bytes = code_item_accessor.InsnsSizeInCodeUnits() * 2; |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2556 | stats_.dex_instruction_bytes += dex_instruction_bytes; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2557 | |
| 2558 | bool first_occurrence; |
Roland Levillain | 6d7f179 | 2015-07-02 10:59:15 +0100 | [diff] [blame] | 2559 | size_t vmap_table_bytes = 0u; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 2560 | if (!method_header->IsOptimized()) { |
Roland Levillain | 6d7f179 | 2015-07-02 10:59:15 +0100 | [diff] [blame] | 2561 | // Method compiled with the optimizing compiler have no vmap table. |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2562 | vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence); |
Roland Levillain | 6d7f179 | 2015-07-02 10:59:15 +0100 | [diff] [blame] | 2563 | if (first_occurrence) { |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2564 | stats_.vmap_table_bytes += vmap_table_bytes; |
Roland Levillain | 6d7f179 | 2015-07-02 10:59:15 +0100 | [diff] [blame] | 2565 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2566 | } |
| 2567 | |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2568 | uint32_t quick_oat_code_size = GetQuickOatCodeSize(method); |
| 2569 | ComputeOatSize(quick_oat_code_begin, &first_occurrence); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2570 | if (first_occurrence) { |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2571 | stats_.managed_code_bytes += quick_oat_code_size; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2572 | if (method->IsConstructor()) { |
| 2573 | if (method->IsStatic()) { |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2574 | stats_.class_initializer_code_bytes += quick_oat_code_size; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2575 | } else if (dex_instruction_bytes > kLargeConstructorDexBytes) { |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2576 | stats_.large_initializer_code_bytes += quick_oat_code_size; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2577 | } |
| 2578 | } else if (dex_instruction_bytes > kLargeMethodDexBytes) { |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2579 | stats_.large_method_code_bytes += quick_oat_code_size; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2580 | } |
| 2581 | } |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2582 | stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2583 | |
Igor Murashkin | 7617abd | 2015-07-10 18:27:47 -0700 | [diff] [blame] | 2584 | uint32_t method_access_flags = method->GetAccessFlags(); |
| 2585 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2586 | indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end); |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 2587 | indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n", |
| 2588 | dex_instruction_bytes, |
| 2589 | vmap_table_bytes, |
Igor Murashkin | 7617abd | 2015-07-10 18:27:47 -0700 | [diff] [blame] | 2590 | method_access_flags); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2591 | |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 2592 | size_t total_size = dex_instruction_bytes + |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2593 | vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2594 | |
| 2595 | double expansion = |
| 2596 | static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes); |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2597 | stats_.ComputeOutliers(total_size, expansion, method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2598 | } |
| 2599 | } |
| 2600 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2601 | std::set<const void*> already_seen_; |
| 2602 | // Compute the size of the given data within the oat file and whether this is the first time |
| 2603 | // this data has been requested |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 2604 | size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2605 | if (already_seen_.count(oat_data) == 0) { |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 2606 | *first_occurrence = true; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2607 | already_seen_.insert(oat_data); |
| 2608 | } else { |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 2609 | *first_occurrence = false; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2610 | } |
| 2611 | return oat_dumper_->ComputeSize(oat_data); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2612 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2613 | |
| 2614 | public: |
| 2615 | struct Stats { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2616 | size_t oat_file_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2617 | size_t file_bytes; |
| 2618 | |
| 2619 | size_t header_bytes; |
| 2620 | size_t object_bytes; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2621 | size_t art_field_bytes; |
| 2622 | size_t art_method_bytes; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2623 | size_t dex_cache_arrays_bytes; |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 2624 | size_t interned_strings_bytes; |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 2625 | size_t class_table_bytes; |
Mathieu Chartier | 3232709 | 2013-08-30 14:04:08 -0700 | [diff] [blame] | 2626 | size_t bitmap_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2627 | size_t alignment_bytes; |
| 2628 | |
| 2629 | size_t managed_code_bytes; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2630 | size_t managed_code_bytes_ignoring_deduplication; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2631 | size_t native_to_managed_code_bytes; |
Ian Rogers | 0d2d378 | 2012-04-10 11:09:18 -0700 | [diff] [blame] | 2632 | size_t class_initializer_code_bytes; |
| 2633 | size_t large_initializer_code_bytes; |
| 2634 | size_t large_method_code_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2635 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2636 | size_t vmap_table_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2637 | |
| 2638 | size_t dex_instruction_bytes; |
| 2639 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2640 | std::vector<ArtMethod*> method_outlier; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2641 | std::vector<size_t> method_outlier_size; |
| 2642 | std::vector<double> method_outlier_expansion; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 2643 | std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2644 | |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 2645 | Stats() |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2646 | : oat_file_bytes(0), |
| 2647 | file_bytes(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2648 | header_bytes(0), |
| 2649 | object_bytes(0), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2650 | art_field_bytes(0), |
| 2651 | art_method_bytes(0), |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2652 | dex_cache_arrays_bytes(0), |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 2653 | interned_strings_bytes(0), |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 2654 | class_table_bytes(0), |
Mathieu Chartier | 3232709 | 2013-08-30 14:04:08 -0700 | [diff] [blame] | 2655 | bitmap_bytes(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2656 | alignment_bytes(0), |
| 2657 | managed_code_bytes(0), |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2658 | managed_code_bytes_ignoring_deduplication(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2659 | native_to_managed_code_bytes(0), |
Ian Rogers | 0d2d378 | 2012-04-10 11:09:18 -0700 | [diff] [blame] | 2660 | class_initializer_code_bytes(0), |
| 2661 | large_initializer_code_bytes(0), |
| 2662 | large_method_code_bytes(0), |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2663 | vmap_table_bytes(0), |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2664 | dex_instruction_bytes(0) {} |
| 2665 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 2666 | struct SizeAndCount { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2667 | 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] | 2668 | size_t bytes; |
| 2669 | size_t count; |
| 2670 | }; |
| 2671 | typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable; |
| 2672 | SizeAndCountTable sizes_and_counts; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2673 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2674 | void Update(const char* descriptor, size_t object_bytes_in) { |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 2675 | SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor); |
| 2676 | if (it != sizes_and_counts.end()) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2677 | it->second.bytes += object_bytes_in; |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 2678 | it->second.count += 1; |
| 2679 | } else { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2680 | sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1)); |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 2681 | } |
| 2682 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2683 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2684 | double PercentOfOatBytes(size_t size) { |
| 2685 | return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100; |
| 2686 | } |
| 2687 | |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2688 | double PercentOfFileBytes(size_t size) { |
| 2689 | return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100; |
| 2690 | } |
| 2691 | |
| 2692 | double PercentOfObjectBytes(size_t size) { |
| 2693 | return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100; |
| 2694 | } |
| 2695 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2696 | void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2697 | method_outlier_size.push_back(total_size); |
| 2698 | method_outlier_expansion.push_back(expansion); |
| 2699 | method_outlier.push_back(method); |
| 2700 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2701 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2702 | void DumpOutliers(std::ostream& os) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2703 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2704 | size_t sum_of_sizes = 0; |
| 2705 | size_t sum_of_sizes_squared = 0; |
| 2706 | size_t sum_of_expansion = 0; |
| 2707 | size_t sum_of_expansion_squared = 0; |
| 2708 | size_t n = method_outlier_size.size(); |
Mathieu Chartier | 1ebf8d3 | 2016-06-09 11:51:27 -0700 | [diff] [blame] | 2709 | if (n <= 1) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2710 | return; |
| 2711 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2712 | for (size_t i = 0; i < n; i++) { |
| 2713 | size_t cur_size = method_outlier_size[i]; |
| 2714 | sum_of_sizes += cur_size; |
| 2715 | sum_of_sizes_squared += cur_size * cur_size; |
| 2716 | double cur_expansion = method_outlier_expansion[i]; |
| 2717 | sum_of_expansion += cur_expansion; |
| 2718 | sum_of_expansion_squared += cur_expansion * cur_expansion; |
| 2719 | } |
| 2720 | size_t size_mean = sum_of_sizes / n; |
| 2721 | size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1); |
| 2722 | double expansion_mean = sum_of_expansion / n; |
| 2723 | double expansion_variance = |
| 2724 | (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1); |
| 2725 | |
| 2726 | // Dump methods whose size is a certain number of standard deviations from the mean |
| 2727 | size_t dumped_values = 0; |
| 2728 | size_t skipped_values = 0; |
| 2729 | for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations |
| 2730 | size_t cur_size_variance = i * i * size_variance; |
| 2731 | bool first = true; |
| 2732 | for (size_t j = 0; j < n; j++) { |
| 2733 | size_t cur_size = method_outlier_size[j]; |
| 2734 | if (cur_size > size_mean) { |
| 2735 | size_t cur_var = cur_size - size_mean; |
| 2736 | cur_var = cur_var * cur_var; |
| 2737 | if (cur_var > cur_size_variance) { |
| 2738 | if (dumped_values > 20) { |
| 2739 | if (i == 1) { |
| 2740 | skipped_values++; |
| 2741 | } else { |
| 2742 | i = 2; // jump to counting for 1 standard deviation |
| 2743 | break; |
| 2744 | } |
| 2745 | } else { |
| 2746 | if (first) { |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2747 | os << "\nBig methods (size > " << i << " standard deviations the norm):\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2748 | first = false; |
| 2749 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2750 | os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of " |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2751 | << PrettySize(cur_size) << "\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2752 | method_outlier_size[j] = 0; // don't consider this method again |
| 2753 | dumped_values++; |
| 2754 | } |
| 2755 | } |
| 2756 | } |
| 2757 | } |
| 2758 | } |
| 2759 | if (skipped_values > 0) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2760 | os << "... skipped " << skipped_values |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2761 | << " methods with size > 1 standard deviation from the norm\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2762 | } |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2763 | os << std::flush; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2764 | |
| 2765 | // Dump methods whose expansion is a certain number of standard deviations from the mean |
| 2766 | dumped_values = 0; |
| 2767 | skipped_values = 0; |
| 2768 | for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations |
| 2769 | double cur_expansion_variance = i * i * expansion_variance; |
| 2770 | bool first = true; |
| 2771 | for (size_t j = 0; j < n; j++) { |
| 2772 | double cur_expansion = method_outlier_expansion[j]; |
| 2773 | if (cur_expansion > expansion_mean) { |
| 2774 | size_t cur_var = cur_expansion - expansion_mean; |
| 2775 | cur_var = cur_var * cur_var; |
| 2776 | if (cur_var > cur_expansion_variance) { |
| 2777 | if (dumped_values > 20) { |
| 2778 | if (i == 1) { |
| 2779 | skipped_values++; |
| 2780 | } else { |
| 2781 | i = 2; // jump to counting for 1 standard deviation |
| 2782 | break; |
| 2783 | } |
| 2784 | } else { |
| 2785 | if (first) { |
| 2786 | os << "\nLarge expansion methods (size > " << i |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2787 | << " standard deviations the norm):\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2788 | first = false; |
| 2789 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2790 | os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by " |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2791 | << cur_expansion << "\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2792 | method_outlier_expansion[j] = 0.0; // don't consider this method again |
| 2793 | dumped_values++; |
| 2794 | } |
| 2795 | } |
| 2796 | } |
| 2797 | } |
| 2798 | } |
| 2799 | if (skipped_values > 0) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2800 | os << "... skipped " << skipped_values |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2801 | << " methods with expansion > 1 standard deviation from the norm\n"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2802 | } |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2803 | os << "\n" << std::flush; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2804 | } |
| 2805 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2806 | void Dump(std::ostream& os, std::ostream& indent_os) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2807 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2808 | { |
| 2809 | os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n" |
| 2810 | << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n"; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2811 | indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n" |
| 2812 | "object_bytes = %8zd (%2.0f%% of art file bytes)\n" |
| 2813 | "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n" |
| 2814 | "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n" |
| 2815 | "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n" |
| 2816 | "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n" |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 2817 | "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n" |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2818 | "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n" |
| 2819 | "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n", |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2820 | header_bytes, PercentOfFileBytes(header_bytes), |
| 2821 | object_bytes, PercentOfFileBytes(object_bytes), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2822 | art_field_bytes, PercentOfFileBytes(art_field_bytes), |
| 2823 | art_method_bytes, PercentOfFileBytes(art_method_bytes), |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2824 | dex_cache_arrays_bytes, |
| 2825 | PercentOfFileBytes(dex_cache_arrays_bytes), |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 2826 | interned_strings_bytes, |
| 2827 | PercentOfFileBytes(interned_strings_bytes), |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 2828 | class_table_bytes, PercentOfFileBytes(class_table_bytes), |
Mathieu Chartier | 3232709 | 2013-08-30 14:04:08 -0700 | [diff] [blame] | 2829 | bitmap_bytes, PercentOfFileBytes(bitmap_bytes), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2830 | alignment_bytes, PercentOfFileBytes(alignment_bytes)) |
| 2831 | << std::flush; |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 2832 | CHECK_EQ(file_bytes, |
| 2833 | header_bytes + object_bytes + art_field_bytes + art_method_bytes + |
| 2834 | dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes + |
| 2835 | bitmap_bytes + alignment_bytes); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2836 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2837 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2838 | os << "object_bytes breakdown:\n"; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2839 | size_t object_bytes_total = 0; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 2840 | for (const auto& sizes_and_count : sizes_and_counts) { |
| 2841 | const std::string& descriptor(sizes_and_count.first); |
| 2842 | double average = static_cast<double>(sizes_and_count.second.bytes) / |
| 2843 | static_cast<double>(sizes_and_count.second.count); |
| 2844 | double percent = PercentOfObjectBytes(sizes_and_count.second.bytes); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2845 | os << StringPrintf("%32s %8zd bytes %6zd instances " |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 2846 | "(%4.0f bytes/instance) %2.0f%% of object_bytes\n", |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 2847 | descriptor.c_str(), sizes_and_count.second.bytes, |
| 2848 | sizes_and_count.second.count, average, percent); |
| 2849 | object_bytes_total += sizes_and_count.second.bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2850 | } |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2851 | os << "\n" << std::flush; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2852 | CHECK_EQ(object_bytes, object_bytes_total); |
| 2853 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2854 | os << StringPrintf("oat_file_bytes = %8zd\n" |
| 2855 | "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2856 | "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n" |
| 2857 | "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" |
| 2858 | "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" |
| 2859 | "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] | 2860 | oat_file_bytes, |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 2861 | managed_code_bytes, |
| 2862 | PercentOfOatBytes(managed_code_bytes), |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 2863 | native_to_managed_code_bytes, |
| 2864 | PercentOfOatBytes(native_to_managed_code_bytes), |
| 2865 | class_initializer_code_bytes, |
| 2866 | PercentOfOatBytes(class_initializer_code_bytes), |
| 2867 | large_initializer_code_bytes, |
| 2868 | PercentOfOatBytes(large_initializer_code_bytes), |
| 2869 | large_method_code_bytes, |
| 2870 | PercentOfOatBytes(large_method_code_bytes)) |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2871 | << "DexFile sizes:\n"; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 2872 | 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] | 2873 | os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n", |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 2874 | oat_dex_file_size.first.c_str(), oat_dex_file_size.second, |
| 2875 | PercentOfOatBytes(oat_dex_file_size.second)); |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 2876 | } |
| 2877 | |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 2878 | os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n", |
Ian Rogers | 05f28c6 | 2012-10-23 18:12:13 -0700 | [diff] [blame] | 2879 | vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes)) |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2880 | << std::flush; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2881 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2882 | os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes) |
| 2883 | << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n", |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 2884 | static_cast<double>(managed_code_bytes) / |
| 2885 | static_cast<double>(dex_instruction_bytes), |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2886 | static_cast<double>(managed_code_bytes_ignoring_deduplication) / |
Elliott Hughes | cf44e6f | 2012-05-24 19:42:18 -0700 | [diff] [blame] | 2887 | static_cast<double>(dex_instruction_bytes)) |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2888 | << std::flush; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2889 | |
| 2890 | DumpOutliers(os); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 2891 | } |
| 2892 | } stats_; |
| 2893 | |
| 2894 | private: |
Ian Rogers | 0d2d378 | 2012-04-10 11:09:18 -0700 | [diff] [blame] | 2895 | enum { |
| 2896 | // Number of bytes for a constructor to be considered large. Based on the 1000 basic block |
| 2897 | // threshold, we assume 2 bytes per instruction and 2 instructions per block. |
| 2898 | kLargeConstructorDexBytes = 4000, |
| 2899 | // Number of bytes for a method to be considered large. Based on the 4000 basic block |
| 2900 | // threshold, we assume 2 bytes per instruction and 2 instructions per block. |
| 2901 | kLargeMethodDexBytes = 16000 |
| 2902 | }; |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2903 | |
| 2904 | // For performance, use the *os_ directly for anything that doesn't need indentation |
| 2905 | // and prepare an indentation stream with default indentation 1. |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2906 | std::ostream* os_; |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 2907 | VariableIndentationOutputStream vios_; |
| 2908 | ScopedIndentation indent1_; |
| 2909 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2910 | gc::space::ImageSpace& image_space_; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2911 | const ImageHeader& image_header_; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 2912 | std::unique_ptr<OatDumper> oat_dumper_; |
Andreas Gampe | df2bb1f | 2015-05-04 18:25:23 -0700 | [diff] [blame] | 2913 | OatDumperOptions* oat_dumper_options_; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2914 | std::set<mirror::Object*> dex_caches_; |
Elliott Hughes | d1bb4f6 | 2011-09-23 14:09:45 -0700 | [diff] [blame] | 2915 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 2916 | DISALLOW_COPY_AND_ASSIGN(ImageDumper); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 2917 | }; |
| 2918 | |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2919 | static int DumpImage(gc::space::ImageSpace* image_space, |
| 2920 | OatDumperOptions* options, |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2921 | std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2922 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 2923 | if (!image_header.IsValid()) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 2924 | LOG(ERROR) << "Invalid image header " << image_space->GetImageLocation(); |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2925 | return EXIT_FAILURE; |
| 2926 | } |
| 2927 | ImageDumper image_dumper(os, *image_space, image_header, options); |
| 2928 | if (!image_dumper.Dump()) { |
| 2929 | return EXIT_FAILURE; |
| 2930 | } |
| 2931 | return EXIT_SUCCESS; |
| 2932 | } |
| 2933 | |
| 2934 | static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) { |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2935 | // Dumping the image, no explicit class loader. |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 2936 | ScopedNullHandle<mirror::ClassLoader> null_class_loader; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2937 | options->class_loader_ = &null_class_loader; |
| 2938 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2939 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2940 | if (options->app_image_ != nullptr) { |
| 2941 | if (options->app_oat_ == nullptr) { |
| 2942 | LOG(ERROR) << "Can not dump app image without app oat file"; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2943 | return EXIT_FAILURE; |
| 2944 | } |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2945 | // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file. |
| 2946 | // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file |
| 2947 | // pointers into 32 bit pointer sized ArtMethods. |
| 2948 | std::string error_msg; |
| 2949 | std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_, |
| 2950 | options->app_oat_, |
| 2951 | nullptr, |
| 2952 | nullptr, |
| 2953 | false, |
| 2954 | /*low_4gb*/true, |
| 2955 | nullptr, |
| 2956 | &error_msg)); |
| 2957 | if (oat_file == nullptr) { |
| 2958 | LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2959 | return EXIT_FAILURE; |
| 2960 | } |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2961 | std::unique_ptr<gc::space::ImageSpace> space( |
| 2962 | gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg)); |
| 2963 | if (space == nullptr) { |
| 2964 | LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error " |
| 2965 | << error_msg; |
| 2966 | } |
| 2967 | // Open dex files for the image. |
| 2968 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 2969 | if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) { |
| 2970 | LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error " |
| 2971 | << error_msg; |
| 2972 | } |
| 2973 | // Dump the actual image. |
| 2974 | int result = DumpImage(space.get(), options, os); |
| 2975 | if (result != EXIT_SUCCESS) { |
| 2976 | return result; |
| 2977 | } |
| 2978 | // Fall through to dump the boot images. |
| 2979 | } |
| 2980 | |
| 2981 | gc::Heap* heap = runtime->GetHeap(); |
| 2982 | CHECK(heap->HasBootImageSpace()) << "No image spaces"; |
| 2983 | for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) { |
| 2984 | int result = DumpImage(image_space, options, os); |
| 2985 | if (result != EXIT_SUCCESS) { |
| 2986 | return result; |
| 2987 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 2988 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2989 | return EXIT_SUCCESS; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 2990 | } |
| 2991 | |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 2992 | static jobject InstallOatFile(Runtime* runtime, |
| 2993 | std::unique_ptr<OatFile> oat_file, |
| 2994 | std::vector<const DexFile*>* class_path) |
| 2995 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 2996 | Thread* self = Thread::Current(); |
| 2997 | CHECK(self != nullptr); |
| 2998 | // Need well-known-classes. |
| 2999 | WellKnownClasses::Init(self->GetJniEnv()); |
| 3000 | |
| 3001 | // Need to register dex files to get a working dex cache. |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3002 | OatFile* oat_file_ptr = oat_file.get(); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3003 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3004 | runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file)); |
| 3005 | for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) { |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3006 | std::string error_msg; |
Mathieu Chartier | ac8f439 | 2015-08-27 13:54:20 -0700 | [diff] [blame] | 3007 | const DexFile* const dex_file = OpenDexFile(odf, &error_msg); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3008 | CHECK(dex_file != nullptr) << error_msg; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3009 | ObjPtr<mirror::DexCache> dex_cache = |
| 3010 | class_linker->RegisterDexFile(*dex_file, nullptr); |
| 3011 | CHECK(dex_cache != nullptr); |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3012 | class_path->push_back(dex_file); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3013 | } |
| 3014 | |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3015 | // Need a class loader. Fake that we're a compiler. |
| 3016 | // Note: this will run initializers through the unstarted runtime, so make sure it's |
| 3017 | // initialized. |
| 3018 | interpreter::UnstartedRuntime::Initialize(); |
| 3019 | |
| 3020 | jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path); |
| 3021 | |
| 3022 | return class_loader; |
| 3023 | } |
| 3024 | |
| 3025 | static int DumpOatWithRuntime(Runtime* runtime, |
| 3026 | std::unique_ptr<OatFile> oat_file, |
| 3027 | OatDumperOptions* options, |
| 3028 | std::ostream* os) { |
| 3029 | CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr); |
| 3030 | ScopedObjectAccess soa(Thread::Current()); |
| 3031 | |
| 3032 | OatFile* oat_file_ptr = oat_file.get(); |
| 3033 | std::vector<const DexFile*> class_path; |
| 3034 | jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3035 | |
| 3036 | // Use the class loader while dumping. |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3037 | StackHandleScope<1> scope(soa.Self()); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3038 | Handle<mirror::ClassLoader> loader_handle = scope.NewHandle( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 3039 | soa.Decode<mirror::ClassLoader>(class_loader)); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3040 | options->class_loader_ = &loader_handle; |
| 3041 | |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3042 | OatDumper oat_dumper(*oat_file_ptr, *options); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3043 | bool success = oat_dumper.Dump(*os); |
| 3044 | return (success) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 3045 | } |
| 3046 | |
| 3047 | static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) { |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 3048 | CHECK(oat_file != nullptr && options != nullptr); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3049 | // No image = no class loader. |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 3050 | ScopedNullHandle<mirror::ClassLoader> null_class_loader; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3051 | options->class_loader_ = &null_class_loader; |
| 3052 | |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 3053 | OatDumper oat_dumper(*oat_file, *options); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3054 | bool success = oat_dumper.Dump(*os); |
| 3055 | return (success) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 3056 | } |
| 3057 | |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 3058 | static int DumpOat(Runtime* runtime, |
| 3059 | const char* oat_filename, |
| 3060 | const char* dex_filename, |
| 3061 | OatDumperOptions* options, |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3062 | std::ostream* os) { |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 3063 | if (dex_filename == nullptr) { |
| 3064 | LOG(WARNING) << "No dex filename provided, " |
| 3065 | << "oatdump might fail if the oat file does not contain the dex code."; |
| 3066 | } |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3067 | std::string error_msg; |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3068 | std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename, |
| 3069 | oat_filename, |
| 3070 | nullptr, |
| 3071 | nullptr, |
| 3072 | false, |
| 3073 | /*low_4gb*/false, |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 3074 | dex_filename, |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3075 | &error_msg)); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3076 | if (oat_file == nullptr) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 3077 | LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3078 | return EXIT_FAILURE; |
| 3079 | } |
| 3080 | |
| 3081 | if (runtime != nullptr) { |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3082 | return DumpOatWithRuntime(runtime, std::move(oat_file), options, os); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3083 | } else { |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3084 | return DumpOatWithoutRuntime(oat_file.get(), options, os); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3085 | } |
| 3086 | } |
| 3087 | |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 3088 | static int SymbolizeOat(const char* oat_filename, |
| 3089 | const char* dex_filename, |
| 3090 | std::string& output_name, |
| 3091 | bool no_bits) { |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3092 | std::string error_msg; |
Andreas Gampe | 08c277c | 2017-04-26 22:22:15 -0700 | [diff] [blame] | 3093 | std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename, |
| 3094 | oat_filename, |
| 3095 | nullptr, |
| 3096 | nullptr, |
| 3097 | false, |
| 3098 | /*low_4gb*/false, |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 3099 | dex_filename, |
Andreas Gampe | 08c277c | 2017-04-26 22:22:15 -0700 | [diff] [blame] | 3100 | &error_msg)); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3101 | if (oat_file == nullptr) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 3102 | LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3103 | return EXIT_FAILURE; |
| 3104 | } |
| 3105 | |
Andreas Gampe | 2d8614b | 2016-03-07 16:31:34 -0800 | [diff] [blame] | 3106 | bool result; |
| 3107 | // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF |
| 3108 | // files for 64-bit code in the past. |
| 3109 | if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) { |
Andreas Gampe | 08c277c | 2017-04-26 22:22:15 -0700 | [diff] [blame] | 3110 | OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits); |
Andreas Gampe | 2d8614b | 2016-03-07 16:31:34 -0800 | [diff] [blame] | 3111 | result = oat_symbolizer.Symbolize(); |
| 3112 | } else { |
Andreas Gampe | 08c277c | 2017-04-26 22:22:15 -0700 | [diff] [blame] | 3113 | OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits); |
Andreas Gampe | 2d8614b | 2016-03-07 16:31:34 -0800 | [diff] [blame] | 3114 | result = oat_symbolizer.Symbolize(); |
| 3115 | } |
| 3116 | if (!result) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 3117 | LOG(ERROR) << "Failed to symbolize"; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3118 | return EXIT_FAILURE; |
| 3119 | } |
| 3120 | |
| 3121 | return EXIT_SUCCESS; |
| 3122 | } |
| 3123 | |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3124 | class IMTDumper { |
| 3125 | public: |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3126 | static bool Dump(Runtime* runtime, |
| 3127 | const std::string& imt_file, |
| 3128 | bool dump_imt_stats, |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 3129 | const char* oat_filename, |
| 3130 | const char* dex_filename) { |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3131 | Thread* self = Thread::Current(); |
| 3132 | |
| 3133 | ScopedObjectAccess soa(self); |
| 3134 | StackHandleScope<1> scope(self); |
| 3135 | MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr); |
| 3136 | std::vector<const DexFile*> class_path; |
| 3137 | |
| 3138 | if (oat_filename != nullptr) { |
| 3139 | std::string error_msg; |
| 3140 | std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename, |
| 3141 | oat_filename, |
| 3142 | nullptr, |
| 3143 | nullptr, |
| 3144 | false, |
| 3145 | /*low_4gb*/false, |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 3146 | dex_filename, |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3147 | &error_msg)); |
| 3148 | if (oat_file == nullptr) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 3149 | LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg; |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3150 | return false; |
| 3151 | } |
| 3152 | |
| 3153 | class_loader.Assign(soa.Decode<mirror::ClassLoader>( |
| 3154 | InstallOatFile(runtime, std::move(oat_file), &class_path))); |
| 3155 | } else { |
| 3156 | class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation. |
| 3157 | class_path = runtime->GetClassLinker()->GetBootClassPath(); |
| 3158 | } |
| 3159 | |
| 3160 | if (!imt_file.empty()) { |
| 3161 | return DumpImt(runtime, imt_file, class_loader); |
| 3162 | } |
| 3163 | |
| 3164 | if (dump_imt_stats) { |
| 3165 | return DumpImtStats(runtime, class_path, class_loader); |
| 3166 | } |
| 3167 | |
| 3168 | LOG(FATAL) << "Should not reach here"; |
| 3169 | UNREACHABLE(); |
| 3170 | } |
| 3171 | |
| 3172 | private: |
| 3173 | static bool DumpImt(Runtime* runtime, |
| 3174 | const std::string& imt_file, |
| 3175 | Handle<mirror::ClassLoader> h_class_loader) |
| 3176 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3177 | std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file); |
| 3178 | std::unordered_set<std::string> prepared; |
| 3179 | |
| 3180 | for (const std::string& line : lines) { |
| 3181 | // A line should be either a class descriptor, in which case we will dump the complete IMT, |
| 3182 | // or a class descriptor and an interface method, in which case we will lookup the method, |
| 3183 | // determine its IMT slot, and check the class' IMT. |
| 3184 | size_t first_space = line.find(' '); |
| 3185 | if (first_space == std::string::npos) { |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3186 | DumpIMTForClass(runtime, line, h_class_loader, &prepared); |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3187 | } else { |
| 3188 | DumpIMTForMethod(runtime, |
| 3189 | line.substr(0, first_space), |
| 3190 | line.substr(first_space + 1, std::string::npos), |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3191 | h_class_loader, |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3192 | &prepared); |
| 3193 | } |
| 3194 | std::cerr << std::endl; |
| 3195 | } |
| 3196 | |
| 3197 | return true; |
| 3198 | } |
| 3199 | |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3200 | static bool DumpImtStats(Runtime* runtime, |
| 3201 | const std::vector<const DexFile*>& dex_files, |
| 3202 | Handle<mirror::ClassLoader> h_class_loader) |
| 3203 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3204 | size_t without_imt = 0; |
| 3205 | size_t with_imt = 0; |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3206 | std::map<size_t, size_t> histogram; |
| 3207 | |
| 3208 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 3209 | const PointerSize pointer_size = class_linker->GetImagePointerSize(); |
| 3210 | std::unordered_set<std::string> prepared; |
| 3211 | |
| 3212 | Thread* self = Thread::Current(); |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3213 | StackHandleScope<1> scope(self); |
| 3214 | MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr)); |
| 3215 | |
| 3216 | for (const DexFile* dex_file : dex_files) { |
| 3217 | for (uint32_t class_def_index = 0; |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3218 | class_def_index != dex_file->NumClassDefs(); |
| 3219 | ++class_def_index) { |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3220 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); |
| 3221 | const char* descriptor = dex_file->GetClassDescriptor(class_def); |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3222 | h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3223 | if (h_klass == nullptr) { |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3224 | std::cerr << "Warning: could not load " << descriptor << std::endl; |
| 3225 | continue; |
| 3226 | } |
| 3227 | |
| 3228 | if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) { |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3229 | without_imt++; |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3230 | continue; |
| 3231 | } |
| 3232 | |
| 3233 | ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared); |
| 3234 | if (im_table == nullptr) { |
| 3235 | // Should not happen, but accept. |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3236 | without_imt++; |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3237 | continue; |
| 3238 | } |
| 3239 | |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3240 | with_imt++; |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3241 | for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) { |
| 3242 | ArtMethod* ptr = im_table->Get(imt_index, pointer_size); |
| 3243 | if (ptr->IsRuntimeMethod()) { |
| 3244 | if (ptr->IsImtUnimplementedMethod()) { |
| 3245 | histogram[0]++; |
| 3246 | } else { |
| 3247 | ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size); |
| 3248 | histogram[current_table->NumEntries(pointer_size)]++; |
| 3249 | } |
| 3250 | } else { |
| 3251 | histogram[1]++; |
| 3252 | } |
| 3253 | } |
| 3254 | } |
| 3255 | } |
| 3256 | |
| 3257 | std::cerr << "IMT stats:" |
| 3258 | << std::endl << std::endl; |
| 3259 | |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3260 | std::cerr << " " << with_imt << " classes with IMT." |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3261 | << std::endl << std::endl; |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3262 | std::cerr << " " << without_imt << " classes without IMT (or copy from Object)." |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3263 | << std::endl << std::endl; |
| 3264 | |
| 3265 | double sum_one = 0; |
| 3266 | size_t count_one = 0; |
| 3267 | |
| 3268 | std::cerr << " " << "IMT histogram" << std::endl; |
| 3269 | for (auto& bucket : histogram) { |
| 3270 | std::cerr << " " << bucket.first << " " << bucket.second << std::endl; |
| 3271 | if (bucket.first > 0) { |
| 3272 | sum_one += bucket.second * bucket.first; |
| 3273 | count_one += bucket.second; |
| 3274 | } |
| 3275 | } |
| 3276 | |
| 3277 | double count_zero = count_one + histogram[0]; |
| 3278 | std::cerr << " Stats:" << std::endl; |
| 3279 | std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl; |
| 3280 | std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl; |
| 3281 | |
| 3282 | return true; |
| 3283 | } |
| 3284 | |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3285 | // Return whether the given class has no IMT (or the one shared with java.lang.Object). |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3286 | static bool HasNoIMT(Runtime* runtime, |
| 3287 | Handle<mirror::Class> klass, |
| 3288 | const PointerSize pointer_size, |
| 3289 | std::unordered_set<std::string>* prepared) |
| 3290 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3291 | if (klass->IsObjectClass() || !klass->ShouldHaveImt()) { |
| 3292 | return true; |
| 3293 | } |
| 3294 | |
| 3295 | if (klass->GetImt(pointer_size) == nullptr) { |
| 3296 | PrepareClass(runtime, klass, prepared); |
| 3297 | } |
| 3298 | |
| 3299 | mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass(); |
| 3300 | DCHECK(object_class->IsObjectClass()); |
| 3301 | |
| 3302 | bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size); |
| 3303 | |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 3304 | if (klass->GetIfTable()->Count() == 0) { |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3305 | DCHECK(result); |
| 3306 | } |
| 3307 | |
| 3308 | return result; |
| 3309 | } |
| 3310 | |
| 3311 | static void PrintTable(ImtConflictTable* table, PointerSize pointer_size) |
| 3312 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3313 | if (table == nullptr) { |
| 3314 | std::cerr << " <No IMT?>" << std::endl; |
| 3315 | return; |
| 3316 | } |
| 3317 | size_t table_index = 0; |
| 3318 | for (;;) { |
| 3319 | ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size); |
| 3320 | if (ptr == nullptr) { |
| 3321 | return; |
| 3322 | } |
| 3323 | table_index++; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3324 | std::cerr << " " << ptr->PrettyMethod(true) << std::endl; |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3325 | } |
| 3326 | } |
| 3327 | |
| 3328 | static ImTable* PrepareAndGetImTable(Runtime* runtime, |
| 3329 | Thread* self, |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3330 | Handle<mirror::ClassLoader> h_loader, |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3331 | const std::string& class_name, |
| 3332 | const PointerSize pointer_size, |
| 3333 | mirror::Class** klass_out, |
| 3334 | std::unordered_set<std::string>* prepared) |
| 3335 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3336 | if (class_name.empty()) { |
| 3337 | return nullptr; |
| 3338 | } |
| 3339 | |
| 3340 | std::string descriptor; |
| 3341 | if (class_name[0] == 'L') { |
| 3342 | descriptor = class_name; |
| 3343 | } else { |
| 3344 | descriptor = DotToDescriptor(class_name.c_str()); |
| 3345 | } |
| 3346 | |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3347 | mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader); |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3348 | |
| 3349 | if (klass == nullptr) { |
| 3350 | self->ClearException(); |
| 3351 | std::cerr << "Did not find " << class_name << std::endl; |
| 3352 | *klass_out = nullptr; |
| 3353 | return nullptr; |
| 3354 | } |
| 3355 | |
| 3356 | StackHandleScope<1> scope(Thread::Current()); |
| 3357 | Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass); |
| 3358 | |
| 3359 | ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared); |
| 3360 | *klass_out = h_klass.Get(); |
| 3361 | return ret; |
| 3362 | } |
| 3363 | |
| 3364 | static ImTable* PrepareAndGetImTable(Runtime* runtime, |
| 3365 | Handle<mirror::Class> h_klass, |
| 3366 | const PointerSize pointer_size, |
| 3367 | std::unordered_set<std::string>* prepared) |
| 3368 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3369 | PrepareClass(runtime, h_klass, prepared); |
| 3370 | return h_klass->GetImt(pointer_size); |
| 3371 | } |
| 3372 | |
| 3373 | static void DumpIMTForClass(Runtime* runtime, |
| 3374 | const std::string& class_name, |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3375 | Handle<mirror::ClassLoader> h_loader, |
| 3376 | std::unordered_set<std::string>* prepared) |
| 3377 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3378 | const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize(); |
| 3379 | mirror::Class* klass; |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3380 | ImTable* imt = PrepareAndGetImTable(runtime, |
| 3381 | Thread::Current(), |
| 3382 | h_loader, |
| 3383 | class_name, |
| 3384 | pointer_size, |
| 3385 | &klass, |
| 3386 | prepared); |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3387 | if (imt == nullptr) { |
| 3388 | return; |
| 3389 | } |
| 3390 | |
| 3391 | std::cerr << class_name << std::endl << " IMT:" << std::endl; |
| 3392 | for (size_t index = 0; index < ImTable::kSize; ++index) { |
| 3393 | std::cerr << " " << index << ":" << std::endl; |
| 3394 | ArtMethod* ptr = imt->Get(index, pointer_size); |
| 3395 | if (ptr->IsRuntimeMethod()) { |
| 3396 | if (ptr->IsImtUnimplementedMethod()) { |
| 3397 | std::cerr << " <empty>" << std::endl; |
| 3398 | } else { |
| 3399 | ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size); |
| 3400 | PrintTable(current_table, pointer_size); |
| 3401 | } |
| 3402 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3403 | std::cerr << " " << ptr->PrettyMethod(true) << std::endl; |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3404 | } |
| 3405 | } |
| 3406 | |
| 3407 | std::cerr << " Interfaces:" << std::endl; |
| 3408 | // Run through iftable, find methods that slot here, see if they fit. |
| 3409 | mirror::IfTable* if_table = klass->GetIfTable(); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 3410 | for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) { |
| 3411 | mirror::Class* iface = if_table->GetInterface(i); |
| 3412 | std::string iface_name; |
| 3413 | std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl; |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3414 | |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 3415 | for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) { |
| 3416 | uint32_t class_hash, name_hash, signature_hash; |
| 3417 | ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash); |
| 3418 | uint32_t imt_slot = ImTable::GetImtIndex(&iface_method); |
| 3419 | std::cerr << " " << iface_method.PrettyMethod(true) |
| 3420 | << " slot=" << imt_slot |
| 3421 | << std::hex |
| 3422 | << " class_hash=0x" << class_hash |
| 3423 | << " name_hash=0x" << name_hash |
| 3424 | << " signature_hash=0x" << signature_hash |
| 3425 | << std::dec |
| 3426 | << std::endl; |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3427 | } |
| 3428 | } |
| 3429 | } |
| 3430 | |
| 3431 | static void DumpIMTForMethod(Runtime* runtime, |
| 3432 | const std::string& class_name, |
| 3433 | const std::string& method, |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3434 | Handle<mirror::ClassLoader> h_loader, |
| 3435 | std::unordered_set<std::string>* prepared) |
| 3436 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3437 | const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize(); |
| 3438 | mirror::Class* klass; |
| 3439 | ImTable* imt = PrepareAndGetImTable(runtime, |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3440 | Thread::Current(), |
| 3441 | h_loader, |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3442 | class_name, |
| 3443 | pointer_size, |
| 3444 | &klass, |
| 3445 | prepared); |
| 3446 | if (imt == nullptr) { |
| 3447 | return; |
| 3448 | } |
| 3449 | |
| 3450 | std::cerr << class_name << " <" << method << ">" << std::endl; |
| 3451 | for (size_t index = 0; index < ImTable::kSize; ++index) { |
| 3452 | ArtMethod* ptr = imt->Get(index, pointer_size); |
| 3453 | if (ptr->IsRuntimeMethod()) { |
| 3454 | if (ptr->IsImtUnimplementedMethod()) { |
| 3455 | continue; |
| 3456 | } |
| 3457 | |
| 3458 | ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size); |
| 3459 | if (current_table == nullptr) { |
| 3460 | continue; |
| 3461 | } |
| 3462 | |
| 3463 | size_t table_index = 0; |
| 3464 | for (;;) { |
| 3465 | ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size); |
| 3466 | if (ptr2 == nullptr) { |
| 3467 | break; |
| 3468 | } |
| 3469 | table_index++; |
| 3470 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3471 | std::string p_name = ptr2->PrettyMethod(true); |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 3472 | if (android::base::StartsWith(p_name, method.c_str())) { |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3473 | std::cerr << " Slot " |
| 3474 | << index |
| 3475 | << " (" |
| 3476 | << current_table->NumEntries(pointer_size) |
| 3477 | << ")" |
| 3478 | << std::endl; |
| 3479 | PrintTable(current_table, pointer_size); |
| 3480 | return; |
| 3481 | } |
| 3482 | } |
| 3483 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3484 | std::string p_name = ptr->PrettyMethod(true); |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 3485 | if (android::base::StartsWith(p_name, method.c_str())) { |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3486 | std::cerr << " Slot " << index << " (1)" << std::endl; |
| 3487 | std::cerr << " " << p_name << std::endl; |
| 3488 | } else { |
| 3489 | // Run through iftable, find methods that slot here, see if they fit. |
| 3490 | mirror::IfTable* if_table = klass->GetIfTable(); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 3491 | for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) { |
| 3492 | mirror::Class* iface = if_table->GetInterface(i); |
| 3493 | size_t num_methods = iface->NumDeclaredVirtualMethods(); |
| 3494 | if (num_methods > 0) { |
| 3495 | for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) { |
| 3496 | if (ImTable::GetImtIndex(&iface_method) == index) { |
| 3497 | std::string i_name = iface_method.PrettyMethod(true); |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 3498 | if (android::base::StartsWith(i_name, method.c_str())) { |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 3499 | std::cerr << " Slot " << index << " (1)" << std::endl; |
| 3500 | std::cerr << " " << p_name << " (" << i_name << ")" << std::endl; |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3501 | } |
| 3502 | } |
| 3503 | } |
| 3504 | } |
| 3505 | } |
| 3506 | } |
| 3507 | } |
| 3508 | } |
| 3509 | } |
| 3510 | |
| 3511 | // Read lines from the given stream, dropping comments and empty lines |
| 3512 | static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) { |
| 3513 | std::vector<std::string> output; |
| 3514 | while (in_stream.good()) { |
| 3515 | std::string dot; |
| 3516 | std::getline(in_stream, dot); |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 3517 | if (android::base::StartsWith(dot, "#") || dot.empty()) { |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3518 | continue; |
| 3519 | } |
| 3520 | output.push_back(dot); |
| 3521 | } |
| 3522 | return output; |
| 3523 | } |
| 3524 | |
| 3525 | // Read lines from the given file, dropping comments and empty lines. |
| 3526 | static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) { |
| 3527 | std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in)); |
| 3528 | if (input_file.get() == nullptr) { |
| 3529 | LOG(ERROR) << "Failed to open input file " << input_filename; |
| 3530 | return std::vector<std::string>(); |
| 3531 | } |
| 3532 | std::vector<std::string> result = ReadCommentedInputStream(*input_file); |
| 3533 | input_file->close(); |
| 3534 | return result; |
| 3535 | } |
| 3536 | |
| 3537 | // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses, |
| 3538 | // and note in the given set that the work was done. |
| 3539 | static void PrepareClass(Runtime* runtime, |
| 3540 | Handle<mirror::Class> h_klass, |
| 3541 | std::unordered_set<std::string>* done) |
| 3542 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3543 | if (!h_klass->ShouldHaveImt()) { |
| 3544 | return; |
| 3545 | } |
| 3546 | |
| 3547 | std::string name; |
| 3548 | name = h_klass->GetDescriptor(&name); |
| 3549 | |
| 3550 | if (done->find(name) != done->end()) { |
| 3551 | return; |
| 3552 | } |
| 3553 | done->insert(name); |
| 3554 | |
| 3555 | if (h_klass->HasSuperClass()) { |
| 3556 | StackHandleScope<1> h(Thread::Current()); |
| 3557 | PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done); |
| 3558 | } |
| 3559 | |
| 3560 | if (!h_klass->IsTemp()) { |
| 3561 | runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get()); |
| 3562 | } |
| 3563 | } |
| 3564 | }; |
| 3565 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3566 | struct OatdumpArgs : public CmdlineArgs { |
| 3567 | protected: |
| 3568 | using Base = CmdlineArgs; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3569 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3570 | virtual ParseStatus ParseCustom(const StringPiece& option, |
| 3571 | std::string* error_msg) OVERRIDE { |
| 3572 | { |
| 3573 | ParseStatus base_parse = Base::ParseCustom(option, error_msg); |
| 3574 | if (base_parse != kParseUnknownArgument) { |
| 3575 | return base_parse; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3576 | } |
| 3577 | } |
| 3578 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3579 | if (option.starts_with("--oat-file=")) { |
| 3580 | oat_filename_ = option.substr(strlen("--oat-file=")).data(); |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 3581 | } else if (option.starts_with("--dex-file=")) { |
| 3582 | dex_filename_ = option.substr(strlen("--dex-file=")).data(); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3583 | } else if (option.starts_with("--image=")) { |
| 3584 | image_location_ = option.substr(strlen("--image=")).data(); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3585 | } else if (option == "--no-dump:vmap") { |
| 3586 | dump_vmap_ = false; |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 3587 | } else if (option =="--dump:code_info_stack_maps") { |
| 3588 | dump_code_info_stack_maps_ = true; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3589 | } else if (option == "--no-disassemble") { |
| 3590 | disassemble_code_ = false; |
David Brazdil | c03d7b6 | 2016-03-02 12:18:03 +0000 | [diff] [blame] | 3591 | } else if (option =="--header-only") { |
| 3592 | dump_header_only_ = true; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3593 | } else if (option.starts_with("--symbolize=")) { |
| 3594 | oat_filename_ = option.substr(strlen("--symbolize=")).data(); |
| 3595 | symbolize_ = true; |
David Srbecky | 2fdd03c | 2016-03-10 15:32:37 +0000 | [diff] [blame] | 3596 | } else if (option.starts_with("--only-keep-debug")) { |
| 3597 | only_keep_debug_ = true; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 3598 | } else if (option.starts_with("--class-filter=")) { |
| 3599 | class_filter_ = option.substr(strlen("--class-filter=")).data(); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3600 | } else if (option.starts_with("--method-filter=")) { |
| 3601 | method_filter_ = option.substr(strlen("--method-filter=")).data(); |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 3602 | } else if (option.starts_with("--list-classes")) { |
| 3603 | list_classes_ = true; |
| 3604 | } else if (option.starts_with("--list-methods")) { |
| 3605 | list_methods_ = true; |
| 3606 | } else if (option.starts_with("--export-dex-to=")) { |
| 3607 | export_dex_location_ = option.substr(strlen("--export-dex-to=")).data(); |
| 3608 | } else if (option.starts_with("--addr2instr=")) { |
| 3609 | if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) { |
| 3610 | *error_msg = "Address conversion failed"; |
| 3611 | return kParseError; |
| 3612 | } |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 3613 | } else if (option.starts_with("--app-image=")) { |
| 3614 | app_image_ = option.substr(strlen("--app-image=")).data(); |
| 3615 | } else if (option.starts_with("--app-oat=")) { |
| 3616 | app_oat_ = option.substr(strlen("--app-oat=")).data(); |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3617 | } else if (option.starts_with("--dump-imt=")) { |
| 3618 | imt_dump_ = option.substr(strlen("--dump-imt=")).data(); |
| 3619 | } else if (option == "--dump-imt-stats") { |
| 3620 | imt_stat_dump_ = true; |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3621 | } else { |
| 3622 | return kParseUnknownArgument; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3623 | } |
| 3624 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3625 | return kParseOk; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3626 | } |
| 3627 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3628 | virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE { |
| 3629 | // Infer boot image location from the image location if possible. |
| 3630 | if (boot_image_location_ == nullptr) { |
| 3631 | boot_image_location_ = image_location_; |
| 3632 | } |
| 3633 | |
| 3634 | // Perform the parent checks. |
| 3635 | ParseStatus parent_checks = Base::ParseChecks(error_msg); |
| 3636 | if (parent_checks != kParseOk) { |
| 3637 | return parent_checks; |
| 3638 | } |
| 3639 | |
| 3640 | // Perform our own checks. |
| 3641 | if (image_location_ == nullptr && oat_filename_ == nullptr) { |
| 3642 | *error_msg = "Either --image or --oat-file must be specified"; |
| 3643 | return kParseError; |
| 3644 | } else if (image_location_ != nullptr && oat_filename_ != nullptr) { |
| 3645 | *error_msg = "Either --image or --oat-file must be specified but not both"; |
| 3646 | return kParseError; |
| 3647 | } |
| 3648 | |
| 3649 | return kParseOk; |
| 3650 | } |
| 3651 | |
| 3652 | virtual std::string GetUsage() const { |
| 3653 | std::string usage; |
| 3654 | |
| 3655 | usage += |
| 3656 | "Usage: oatdump [options] ...\n" |
| 3657 | " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n" |
| 3658 | " Example: adb shell oatdump --image=/system/framework/boot.art\n" |
| 3659 | "\n" |
| 3660 | // Either oat-file or image is required. |
| 3661 | " --oat-file=<file.oat>: specifies an input oat filename.\n" |
| 3662 | " Example: --oat-file=/system/framework/boot.oat\n" |
| 3663 | "\n" |
| 3664 | " --image=<file.art>: specifies an input image location.\n" |
| 3665 | " Example: --image=/system/framework/boot.art\n" |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 3666 | "\n" |
| 3667 | " --app-image=<file.art>: specifies an input app image. Must also have a specified\n" |
Roland Levillain | 4f1c9e6 | 2017-06-28 16:44:30 +0100 | [diff] [blame] | 3668 | " boot image (with --image) and app oat file (with --app-oat).\n" |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 3669 | " Example: --app-image=app.art\n" |
| 3670 | "\n" |
| 3671 | " --app-oat=<file.odex>: specifies an input app oat.\n" |
| 3672 | " Example: --app-oat=app.odex\n" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3673 | "\n"; |
| 3674 | |
| 3675 | usage += Base::GetUsage(); |
| 3676 | |
| 3677 | usage += // Optional. |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3678 | " --no-dump:vmap may be used to disable vmap dumping.\n" |
| 3679 | " Example: --no-dump:vmap\n" |
| 3680 | "\n" |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 3681 | " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n" |
| 3682 | " Example: --dump:code_info_stack_maps\n" |
| 3683 | "\n" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3684 | " --no-disassemble may be used to disable disassembly.\n" |
| 3685 | " Example: --no-disassemble\n" |
| 3686 | "\n" |
David Brazdil | c03d7b6 | 2016-03-02 12:18:03 +0000 | [diff] [blame] | 3687 | " --header-only may be used to print only the oat header.\n" |
| 3688 | " Example: --header-only\n" |
| 3689 | "\n" |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 3690 | " --list-classes may be used to list target file classes (can be used with filters).\n" |
| 3691 | " Example: --list-classes\n" |
| 3692 | " Example: --list-classes --class-filter=com.example.foo\n" |
| 3693 | "\n" |
| 3694 | " --list-methods may be used to list target file methods (can be used with filters).\n" |
| 3695 | " Example: --list-methods\n" |
| 3696 | " Example: --list-methods --class-filter=com.example --method-filter=foo\n" |
| 3697 | "\n" |
| 3698 | " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n" |
| 3699 | " Example: --symbolize=/system/framework/boot.oat\n" |
| 3700 | "\n" |
David Srbecky | 2fdd03c | 2016-03-10 15:32:37 +0000 | [diff] [blame] | 3701 | " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n" |
| 3702 | " .rodata and .text sections are omitted in the output file to save space.\n" |
| 3703 | " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n" |
| 3704 | "\n" |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 3705 | " --class-filter=<class name>: only dumps classes that contain the filter.\n" |
| 3706 | " Example: --class-filter=com.example.foo\n" |
| 3707 | "\n" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3708 | " --method-filter=<method name>: only dumps methods that contain the filter.\n" |
| 3709 | " Example: --method-filter=foo\n" |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 3710 | "\n" |
| 3711 | " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n" |
| 3712 | " Example: --export-dex-to=/data/local/tmp\n" |
| 3713 | "\n" |
| 3714 | " --addr2instr=<address>: output matching method disassembled code from relative\n" |
| 3715 | " address (e.g. PC from crash dump)\n" |
| 3716 | " Example: --addr2instr=0x00001a3b\n" |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3717 | "\n" |
| 3718 | " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n" |
| 3719 | " types and interface methods in the given file. The file\n" |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3720 | " is read line-wise, where each line should either be a class\n" |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3721 | " name or descriptor, or a class name/descriptor and a prefix\n" |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3722 | " of a complete method name (separated by a whitespace).\n" |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3723 | " Example: --dump-imt=imt.txt\n" |
| 3724 | "\n" |
| 3725 | " --dump-imt-stats: output IMT statistics for the given boot image\n" |
| 3726 | " Example: --dump-imt-stats" |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3727 | "\n"; |
| 3728 | |
| 3729 | return usage; |
| 3730 | } |
| 3731 | |
| 3732 | public: |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3733 | const char* oat_filename_ = nullptr; |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 3734 | const char* dex_filename_ = nullptr; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 3735 | const char* class_filter_ = ""; |
Nicolas Geoffray | 3fcd220 | 2014-11-12 18:02:36 +0000 | [diff] [blame] | 3736 | const char* method_filter_ = ""; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3737 | const char* image_location_ = nullptr; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3738 | std::string elf_filename_prefix_; |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3739 | std::string imt_dump_; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3740 | bool dump_vmap_ = true; |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 3741 | bool dump_code_info_stack_maps_ = false; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3742 | bool disassemble_code_ = true; |
| 3743 | bool symbolize_ = false; |
David Srbecky | 2fdd03c | 2016-03-10 15:32:37 +0000 | [diff] [blame] | 3744 | bool only_keep_debug_ = false; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 3745 | bool list_classes_ = false; |
| 3746 | bool list_methods_ = false; |
David Brazdil | c03d7b6 | 2016-03-02 12:18:03 +0000 | [diff] [blame] | 3747 | bool dump_header_only_ = false; |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3748 | bool imt_stat_dump_ = false; |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 3749 | uint32_t addr2instr_ = 0; |
| 3750 | const char* export_dex_location_ = nullptr; |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 3751 | const char* app_image_ = nullptr; |
| 3752 | const char* app_oat_ = nullptr; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3753 | }; |
| 3754 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3755 | struct OatdumpMain : public CmdlineMain<OatdumpArgs> { |
| 3756 | virtual bool NeedsRuntime() OVERRIDE { |
| 3757 | CHECK(args_ != nullptr); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3758 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3759 | // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping. |
| 3760 | bool absolute_addresses = (args_->oat_filename_ == nullptr); |
| 3761 | |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 3762 | oat_dumper_options_.reset(new OatDumperOptions( |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3763 | args_->dump_vmap_, |
Roland Levillain | f2650d1 | 2015-05-28 14:53:28 +0100 | [diff] [blame] | 3764 | args_->dump_code_info_stack_maps_, |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3765 | args_->disassemble_code_, |
| 3766 | absolute_addresses, |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 3767 | args_->class_filter_, |
| 3768 | args_->method_filter_, |
| 3769 | args_->list_classes_, |
| 3770 | args_->list_methods_, |
David Brazdil | c03d7b6 | 2016-03-02 12:18:03 +0000 | [diff] [blame] | 3771 | args_->dump_header_only_, |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 3772 | args_->export_dex_location_, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 3773 | args_->app_image_, |
| 3774 | args_->app_oat_, |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 3775 | args_->addr2instr_)); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3776 | |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3777 | return (args_->boot_image_location_ != nullptr || |
| 3778 | args_->image_location_ != nullptr || |
| 3779 | !args_->imt_dump_.empty()) && |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3780 | !args_->symbolize_; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3781 | } |
| 3782 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3783 | virtual bool ExecuteWithoutRuntime() OVERRIDE { |
| 3784 | CHECK(args_ != nullptr); |
Andreas Gampe | c24f399 | 2014-12-17 20:40:11 -0800 | [diff] [blame] | 3785 | CHECK(args_->oat_filename_ != nullptr); |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3786 | |
Mathieu Chartier | d424d08 | 2014-10-15 10:31:46 -0700 | [diff] [blame] | 3787 | MemMap::Init(); |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3788 | |
Andreas Gampe | c24f399 | 2014-12-17 20:40:11 -0800 | [diff] [blame] | 3789 | if (args_->symbolize_) { |
David Srbecky | 2fdd03c | 2016-03-10 15:32:37 +0000 | [diff] [blame] | 3790 | // ELF has special kind of section called SHT_NOBITS which allows us to create |
| 3791 | // sections which exist but their data is omitted from the ELF file to save space. |
| 3792 | // This is what "strip --only-keep-debug" does when it creates separate ELF file |
| 3793 | // with only debug data. We use it in similar way to exclude .rodata and .text. |
| 3794 | bool no_bits = args_->only_keep_debug_; |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 3795 | return SymbolizeOat(args_->oat_filename_, args_->dex_filename_, args_->output_name_, no_bits) |
| 3796 | == EXIT_SUCCESS; |
Andreas Gampe | c24f399 | 2014-12-17 20:40:11 -0800 | [diff] [blame] | 3797 | } else { |
| 3798 | return DumpOat(nullptr, |
| 3799 | args_->oat_filename_, |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 3800 | args_->dex_filename_, |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 3801 | oat_dumper_options_.get(), |
Andreas Gampe | c24f399 | 2014-12-17 20:40:11 -0800 | [diff] [blame] | 3802 | args_->os_) == EXIT_SUCCESS; |
| 3803 | } |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3804 | } |
| 3805 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3806 | virtual bool ExecuteWithRuntime(Runtime* runtime) { |
| 3807 | CHECK(args_ != nullptr); |
| 3808 | |
Andreas Gampe | ebfc1ac | 2016-09-29 19:50:27 -0700 | [diff] [blame] | 3809 | if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) { |
| 3810 | return IMTDumper::Dump(runtime, |
| 3811 | args_->imt_dump_, |
| 3812 | args_->imt_stat_dump_, |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 3813 | args_->oat_filename_, |
| 3814 | args_->dex_filename_); |
Andreas Gampe | 9fded87 | 2016-09-25 16:08:35 -0700 | [diff] [blame] | 3815 | } |
| 3816 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3817 | if (args_->oat_filename_ != nullptr) { |
| 3818 | return DumpOat(runtime, |
| 3819 | args_->oat_filename_, |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 3820 | args_->dex_filename_, |
Anestis Bechtsoudis | 32f500d | 2015-02-22 22:32:57 -0800 | [diff] [blame] | 3821 | oat_dumper_options_.get(), |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3822 | args_->os_) == EXIT_SUCCESS; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3823 | } |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3824 | |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 3825 | return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3826 | } |
| 3827 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3828 | std::unique_ptr<OatDumperOptions> oat_dumper_options_; |
| 3829 | }; |
Andreas Gampe | 00b25f3 | 2014-09-17 21:49:05 -0700 | [diff] [blame] | 3830 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3831 | } // namespace art |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 3832 | |
| 3833 | int main(int argc, char** argv) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 3834 | // Output all logging to stderr. |
| 3835 | android::base::SetLogger(android::base::StderrLogger); |
| 3836 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 3837 | art::OatdumpMain main; |
| 3838 | return main.Main(argc, argv); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 3839 | } |