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