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