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