David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | */ |
| 16 | |
| 17 | #ifndef ART_COMPILER_DEBUG_ELF_DEBUG_INFO_WRITER_H_ |
| 18 | #define ART_COMPILER_DEBUG_ELF_DEBUG_INFO_WRITER_H_ |
| 19 | |
| 20 | #include <map> |
| 21 | #include <unordered_set> |
| 22 | #include <vector> |
| 23 | |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 24 | #include "art_field-inl.h" |
Vladimir Marko | 79c4f83 | 2022-11-15 17:12:25 +0000 | [diff] [blame] | 25 | #include "base/macros.h" |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 26 | #include "debug/elf_compilation_unit.h" |
| 27 | #include "debug/elf_debug_loc_writer.h" |
| 28 | #include "debug/method_debug_info.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 29 | #include "dex/code_item_accessors-inl.h" |
| 30 | #include "dex/dex_file-inl.h" |
| 31 | #include "dex/dex_file.h" |
David Srbecky | 2faab00 | 2019-02-12 16:35:48 +0000 | [diff] [blame] | 32 | #include "dwarf/debug_abbrev_writer.h" |
| 33 | #include "dwarf/debug_info_entry_writer.h" |
| 34 | #include "elf/elf_builder.h" |
Andreas Gampe | 09659c2 | 2017-09-18 18:23:32 -0700 | [diff] [blame] | 35 | #include "heap_poisoning.h" |
Lokesh Gidra | 606bd94 | 2022-05-23 19:00:09 +0000 | [diff] [blame] | 36 | #include "linear_alloc-inl.h" |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 37 | #include "mirror/array.h" |
| 38 | #include "mirror/class-inl.h" |
| 39 | #include "mirror/class.h" |
Nicolas Geoffray | 58cc1cb | 2017-11-20 13:27:29 +0000 | [diff] [blame] | 40 | #include "oat_file.h" |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 41 | #include "obj_ptr-inl.h" |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 42 | |
Vladimir Marko | 79c4f83 | 2022-11-15 17:12:25 +0000 | [diff] [blame] | 43 | namespace art HIDDEN { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 44 | namespace debug { |
| 45 | |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 46 | static std::vector<const char*> GetParamNames(const MethodDebugInfo* mi) { |
| 47 | std::vector<const char*> names; |
Mathieu Chartier | 3e2e123 | 2018-09-11 12:35:30 -0700 | [diff] [blame] | 48 | DCHECK(mi->dex_file != nullptr); |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 49 | CodeItemDebugInfoAccessor accessor(*mi->dex_file, mi->code_item, mi->dex_method_index); |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 50 | if (accessor.HasCodeItem()) { |
Mathieu Chartier | 3e2e123 | 2018-09-11 12:35:30 -0700 | [diff] [blame] | 51 | accessor.VisitParameterNames([&](const dex::StringIndex& id) { |
| 52 | names.push_back(mi->dex_file->StringDataByIdx(id)); |
| 53 | }); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 54 | } |
| 55 | return names; |
| 56 | } |
| 57 | |
| 58 | // Helper class to write .debug_info and its supporting sections. |
| 59 | template<typename ElfTypes> |
| 60 | class ElfDebugInfoWriter { |
| 61 | using Elf_Addr = typename ElfTypes::Addr; |
| 62 | |
| 63 | public: |
David Srbecky | 2faab00 | 2019-02-12 16:35:48 +0000 | [diff] [blame] | 64 | explicit ElfDebugInfoWriter(ElfBuilder<ElfTypes>* builder) |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 65 | : builder_(builder), |
| 66 | debug_abbrev_(&debug_abbrev_buffer_) { |
| 67 | } |
| 68 | |
| 69 | void Start() { |
| 70 | builder_->GetDebugInfo()->Start(); |
| 71 | } |
| 72 | |
David Srbecky | 7370d92 | 2019-02-12 14:00:30 +0000 | [diff] [blame] | 73 | void End() { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 74 | builder_->GetDebugInfo()->End(); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 75 | builder_->WriteSection(".debug_abbrev", &debug_abbrev_buffer_); |
| 76 | if (!debug_loc_.empty()) { |
| 77 | builder_->WriteSection(".debug_loc", &debug_loc_); |
| 78 | } |
| 79 | if (!debug_ranges_.empty()) { |
| 80 | builder_->WriteSection(".debug_ranges", &debug_ranges_); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | private: |
David Srbecky | 2faab00 | 2019-02-12 16:35:48 +0000 | [diff] [blame] | 85 | ElfBuilder<ElfTypes>* builder_; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 86 | std::vector<uint8_t> debug_abbrev_buffer_; |
| 87 | dwarf::DebugAbbrevWriter<> debug_abbrev_; |
| 88 | std::vector<uint8_t> debug_loc_; |
| 89 | std::vector<uint8_t> debug_ranges_; |
| 90 | |
| 91 | std::unordered_set<const char*> defined_dex_classes_; // For CHECKs only. |
| 92 | |
| 93 | template<typename ElfTypes2> |
| 94 | friend class ElfCompilationUnitWriter; |
| 95 | }; |
| 96 | |
| 97 | // Helper class to write one compilation unit. |
| 98 | // It holds helper methods and temporary state. |
| 99 | template<typename ElfTypes> |
| 100 | class ElfCompilationUnitWriter { |
| 101 | using Elf_Addr = typename ElfTypes::Addr; |
| 102 | |
| 103 | public: |
| 104 | explicit ElfCompilationUnitWriter(ElfDebugInfoWriter<ElfTypes>* owner) |
| 105 | : owner_(owner), |
| 106 | info_(Is64BitInstructionSet(owner_->builder_->GetIsa()), &owner->debug_abbrev_) { |
| 107 | } |
| 108 | |
| 109 | void Write(const ElfCompilationUnit& compilation_unit) { |
| 110 | CHECK(!compilation_unit.methods.empty()); |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 111 | const Elf_Addr base_address = compilation_unit.is_code_address_text_relative |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 112 | ? owner_->builder_->GetText()->GetAddress() |
| 113 | : 0; |
David Srbecky | 56da23c | 2017-09-08 19:59:15 +0100 | [diff] [blame] | 114 | const bool is64bit = Is64BitInstructionSet(owner_->builder_->GetIsa()); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 115 | using namespace dwarf; // NOLINT. For easy access to DWARF constants. |
| 116 | |
| 117 | info_.StartTag(DW_TAG_compile_unit); |
| 118 | info_.WriteString(DW_AT_producer, "Android dex2oat"); |
| 119 | info_.WriteData1(DW_AT_language, DW_LANG_Java); |
| 120 | info_.WriteString(DW_AT_comp_dir, "$JAVA_SRC_ROOT"); |
David Srbecky | 56da23c | 2017-09-08 19:59:15 +0100 | [diff] [blame] | 121 | // The low_pc acts as base address for several other addresses/ranges. |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 122 | info_.WriteAddr(DW_AT_low_pc, base_address + compilation_unit.code_address); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 123 | info_.WriteSecOffset(DW_AT_stmt_list, compilation_unit.debug_line_offset); |
| 124 | |
David Srbecky | 56da23c | 2017-09-08 19:59:15 +0100 | [diff] [blame] | 125 | // Write .debug_ranges entries covering code ranges of the whole compilation unit. |
| 126 | dwarf::Writer<> debug_ranges(&owner_->debug_ranges_); |
| 127 | info_.WriteSecOffset(DW_AT_ranges, owner_->debug_ranges_.size()); |
| 128 | for (auto mi : compilation_unit.methods) { |
| 129 | uint64_t low_pc = mi->code_address - compilation_unit.code_address; |
| 130 | uint64_t high_pc = low_pc + mi->code_size; |
| 131 | if (is64bit) { |
| 132 | debug_ranges.PushUint64(low_pc); |
| 133 | debug_ranges.PushUint64(high_pc); |
| 134 | } else { |
| 135 | debug_ranges.PushUint32(low_pc); |
| 136 | debug_ranges.PushUint32(high_pc); |
| 137 | } |
| 138 | } |
| 139 | if (is64bit) { |
| 140 | debug_ranges.PushUint64(0); // End of list. |
| 141 | debug_ranges.PushUint64(0); |
| 142 | } else { |
| 143 | debug_ranges.PushUint32(0); // End of list. |
| 144 | debug_ranges.PushUint32(0); |
| 145 | } |
| 146 | |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 147 | const char* last_dex_class_desc = nullptr; |
| 148 | for (auto mi : compilation_unit.methods) { |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 149 | DCHECK(mi->dex_file != nullptr); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 150 | const DexFile* dex = mi->dex_file; |
Mathieu Chartier | 8892c6b | 2018-01-09 15:10:17 -0800 | [diff] [blame] | 151 | CodeItemDebugInfoAccessor accessor(*dex, mi->code_item, mi->dex_method_index); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 152 | const dex::MethodId& dex_method = dex->GetMethodId(mi->dex_method_index); |
| 153 | const dex::ProtoId& dex_proto = dex->GetMethodPrototype(dex_method); |
| 154 | const dex::TypeList* dex_params = dex->GetProtoParameters(dex_proto); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 155 | const char* dex_class_desc = dex->GetMethodDeclaringClassDescriptor(dex_method); |
| 156 | const bool is_static = (mi->access_flags & kAccStatic) != 0; |
| 157 | |
| 158 | // Enclose the method in correct class definition. |
| 159 | if (last_dex_class_desc != dex_class_desc) { |
| 160 | if (last_dex_class_desc != nullptr) { |
| 161 | EndClassTag(); |
| 162 | } |
| 163 | // Write reference tag for the class we are about to declare. |
| 164 | size_t reference_tag_offset = info_.StartTag(DW_TAG_reference_type); |
| 165 | type_cache_.emplace(std::string(dex_class_desc), reference_tag_offset); |
| 166 | size_t type_attrib_offset = info_.size(); |
| 167 | info_.WriteRef4(DW_AT_type, 0); |
| 168 | info_.EndTag(); |
| 169 | // Declare the class that owns this method. |
| 170 | size_t class_offset = StartClassTag(dex_class_desc); |
| 171 | info_.UpdateUint32(type_attrib_offset, class_offset); |
| 172 | info_.WriteFlagPresent(DW_AT_declaration); |
| 173 | // Check that each class is defined only once. |
| 174 | bool unique = owner_->defined_dex_classes_.insert(dex_class_desc).second; |
| 175 | CHECK(unique) << "Redefinition of " << dex_class_desc; |
| 176 | last_dex_class_desc = dex_class_desc; |
| 177 | } |
| 178 | |
| 179 | int start_depth = info_.Depth(); |
| 180 | info_.StartTag(DW_TAG_subprogram); |
| 181 | WriteName(dex->GetMethodName(dex_method)); |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 182 | info_.WriteAddr(DW_AT_low_pc, base_address + mi->code_address); |
| 183 | info_.WriteUdata(DW_AT_high_pc, mi->code_size); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 184 | std::vector<uint8_t> expr_buffer; |
| 185 | Expression expr(&expr_buffer); |
| 186 | expr.WriteOpCallFrameCfa(); |
| 187 | info_.WriteExprLoc(DW_AT_frame_base, expr); |
| 188 | WriteLazyType(dex->GetReturnTypeDescriptor(dex_proto)); |
| 189 | |
David Srbecky | 2ed15b6 | 2016-03-04 11:34:46 +0000 | [diff] [blame] | 190 | // Decode dex register locations for all stack maps. |
| 191 | // It might be expensive, so do it just once and reuse the result. |
David Srbecky | 9e4bbfb | 2018-05-31 08:52:16 +0100 | [diff] [blame] | 192 | std::unique_ptr<const CodeInfo> code_info; |
David Srbecky | 2ed15b6 | 2016-03-04 11:34:46 +0000 | [diff] [blame] | 193 | std::vector<DexRegisterMap> dex_reg_maps; |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 194 | if (accessor.HasCodeItem() && mi->code_info != nullptr) { |
David Srbecky | 9e4bbfb | 2018-05-31 08:52:16 +0100 | [diff] [blame] | 195 | code_info.reset(new CodeInfo(mi->code_info)); |
David Srbecky | 93bd361 | 2018-07-02 19:30:18 +0100 | [diff] [blame] | 196 | for (StackMap stack_map : code_info->GetStackMaps()) { |
David Srbecky | fd89b07 | 2018-06-03 12:00:22 +0100 | [diff] [blame] | 197 | dex_reg_maps.push_back(code_info->GetDexRegisterMapOf(stack_map)); |
David Srbecky | 2ed15b6 | 2016-03-04 11:34:46 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 201 | // Write parameters. DecodeDebugLocalInfo returns them as well, but it does not |
| 202 | // guarantee order or uniqueness so it is safer to iterate over them manually. |
| 203 | // DecodeDebugLocalInfo might not also be available if there is no debug info. |
| 204 | std::vector<const char*> param_names = GetParamNames(mi); |
| 205 | uint32_t arg_reg = 0; |
| 206 | if (!is_static) { |
| 207 | info_.StartTag(DW_TAG_formal_parameter); |
| 208 | WriteName("this"); |
| 209 | info_.WriteFlagPresent(DW_AT_artificial); |
| 210 | WriteLazyType(dex_class_desc); |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 211 | if (accessor.HasCodeItem()) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 212 | // Write the stack location of the parameter. |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 213 | const uint32_t vreg = accessor.RegistersSize() - accessor.InsSize() + arg_reg; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 214 | const bool is64bitValue = false; |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 215 | WriteRegLocation(mi, dex_reg_maps, vreg, is64bitValue, compilation_unit.code_address); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 216 | } |
| 217 | arg_reg++; |
| 218 | info_.EndTag(); |
| 219 | } |
| 220 | if (dex_params != nullptr) { |
| 221 | for (uint32_t i = 0; i < dex_params->Size(); ++i) { |
| 222 | info_.StartTag(DW_TAG_formal_parameter); |
| 223 | // Parameter names may not be always available. |
| 224 | if (i < param_names.size()) { |
| 225 | WriteName(param_names[i]); |
| 226 | } |
| 227 | // Write the type. |
| 228 | const char* type_desc = dex->StringByTypeIdx(dex_params->GetTypeItem(i).type_idx_); |
| 229 | WriteLazyType(type_desc); |
| 230 | const bool is64bitValue = type_desc[0] == 'D' || type_desc[0] == 'J'; |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 231 | if (accessor.HasCodeItem()) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 232 | // Write the stack location of the parameter. |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 233 | const uint32_t vreg = accessor.RegistersSize() - accessor.InsSize() + arg_reg; |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 234 | WriteRegLocation(mi, dex_reg_maps, vreg, is64bitValue, compilation_unit.code_address); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 235 | } |
| 236 | arg_reg += is64bitValue ? 2 : 1; |
| 237 | info_.EndTag(); |
| 238 | } |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 239 | if (accessor.HasCodeItem()) { |
| 240 | DCHECK_EQ(arg_reg, accessor.InsSize()); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | // Write local variables. |
Mathieu Chartier | e5afbf3 | 2018-09-12 17:51:54 -0700 | [diff] [blame] | 245 | std::vector<DexFile::LocalInfo> local_infos; |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 246 | if (accessor.DecodeDebugLocalInfo(is_static, |
| 247 | mi->dex_method_index, |
Mathieu Chartier | e5afbf3 | 2018-09-12 17:51:54 -0700 | [diff] [blame] | 248 | [&](const DexFile::LocalInfo& entry) { |
| 249 | local_infos.push_back(entry); |
| 250 | })) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 251 | for (const DexFile::LocalInfo& var : local_infos) { |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 252 | if (var.reg_ < accessor.RegistersSize() - accessor.InsSize()) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 253 | info_.StartTag(DW_TAG_variable); |
| 254 | WriteName(var.name_); |
| 255 | WriteLazyType(var.descriptor_); |
| 256 | bool is64bitValue = var.descriptor_[0] == 'D' || var.descriptor_[0] == 'J'; |
David Srbecky | 2ed15b6 | 2016-03-04 11:34:46 +0000 | [diff] [blame] | 257 | WriteRegLocation(mi, |
| 258 | dex_reg_maps, |
| 259 | var.reg_, |
| 260 | is64bitValue, |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 261 | compilation_unit.code_address, |
David Srbecky | 2ed15b6 | 2016-03-04 11:34:46 +0000 | [diff] [blame] | 262 | var.start_address_, |
| 263 | var.end_address_); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 264 | info_.EndTag(); |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | info_.EndTag(); |
| 270 | CHECK_EQ(info_.Depth(), start_depth); // Balanced start/end. |
| 271 | } |
| 272 | if (last_dex_class_desc != nullptr) { |
| 273 | EndClassTag(); |
| 274 | } |
| 275 | FinishLazyTypes(); |
| 276 | CloseNamespacesAboveDepth(0); |
| 277 | info_.EndTag(); // DW_TAG_compile_unit |
| 278 | CHECK_EQ(info_.Depth(), 0); |
| 279 | std::vector<uint8_t> buffer; |
| 280 | buffer.reserve(info_.data()->size() + KB); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 281 | // All compilation units share single table which is at the start of .debug_abbrev. |
| 282 | const size_t debug_abbrev_offset = 0; |
David Srbecky | 7370d92 | 2019-02-12 14:00:30 +0000 | [diff] [blame] | 283 | WriteDebugInfoCU(debug_abbrev_offset, info_, &buffer); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 284 | owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size()); |
| 285 | } |
| 286 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 287 | void Write(const ArrayRef<mirror::Class*>& types) REQUIRES_SHARED(Locks::mutator_lock_) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 288 | using namespace dwarf; // NOLINT. For easy access to DWARF constants. |
| 289 | |
| 290 | info_.StartTag(DW_TAG_compile_unit); |
| 291 | info_.WriteString(DW_AT_producer, "Android dex2oat"); |
| 292 | info_.WriteData1(DW_AT_language, DW_LANG_Java); |
| 293 | |
| 294 | // Base class references to be patched at the end. |
| 295 | std::map<size_t, mirror::Class*> base_class_references; |
| 296 | |
| 297 | // Already written declarations or definitions. |
| 298 | std::map<mirror::Class*, size_t> class_declarations; |
| 299 | |
| 300 | std::vector<uint8_t> expr_buffer; |
| 301 | for (mirror::Class* type : types) { |
| 302 | if (type->IsPrimitive()) { |
| 303 | // For primitive types the definition and the declaration is the same. |
| 304 | if (type->GetPrimitiveType() != Primitive::kPrimVoid) { |
| 305 | WriteTypeDeclaration(type->GetDescriptor(nullptr)); |
| 306 | } |
| 307 | } else if (type->IsArrayClass()) { |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 308 | ObjPtr<mirror::Class> element_type = type->GetComponentType(); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 309 | uint32_t component_size = type->GetComponentSize(); |
| 310 | uint32_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value(); |
| 311 | uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value(); |
| 312 | |
| 313 | CloseNamespacesAboveDepth(0); // Declare in root namespace. |
| 314 | info_.StartTag(DW_TAG_array_type); |
| 315 | std::string descriptor_string; |
| 316 | WriteLazyType(element_type->GetDescriptor(&descriptor_string)); |
| 317 | WriteLinkageName(type); |
| 318 | info_.WriteUdata(DW_AT_data_member_location, data_offset); |
| 319 | info_.StartTag(DW_TAG_subrange_type); |
| 320 | Expression count_expr(&expr_buffer); |
| 321 | count_expr.WriteOpPushObjectAddress(); |
| 322 | count_expr.WriteOpPlusUconst(length_offset); |
| 323 | count_expr.WriteOpDerefSize(4); // Array length is always 32-bit wide. |
| 324 | info_.WriteExprLoc(DW_AT_count, count_expr); |
| 325 | info_.EndTag(); // DW_TAG_subrange_type. |
| 326 | info_.EndTag(); // DW_TAG_array_type. |
| 327 | } else if (type->IsInterface()) { |
| 328 | // Skip. Variables cannot have an interface as a dynamic type. |
| 329 | // We do not expose the interface information to the debugger in any way. |
| 330 | } else { |
| 331 | std::string descriptor_string; |
| 332 | const char* desc = type->GetDescriptor(&descriptor_string); |
| 333 | size_t class_offset = StartClassTag(desc); |
| 334 | class_declarations.emplace(type, class_offset); |
| 335 | |
| 336 | if (!type->IsVariableSize()) { |
| 337 | info_.WriteUdata(DW_AT_byte_size, type->GetObjectSize()); |
| 338 | } |
| 339 | |
| 340 | WriteLinkageName(type); |
| 341 | |
| 342 | if (type->IsObjectClass()) { |
| 343 | // Generate artificial member which is used to get the dynamic type of variable. |
| 344 | // The run-time value of this field will correspond to linkage name of some type. |
| 345 | // We need to do it only once in j.l.Object since all other types inherit it. |
| 346 | info_.StartTag(DW_TAG_member); |
| 347 | WriteName(".dynamic_type"); |
| 348 | WriteLazyType(sizeof(uintptr_t) == 8 ? "J" : "I"); |
| 349 | info_.WriteFlagPresent(DW_AT_artificial); |
| 350 | // Create DWARF expression to get the value of the methods_ field. |
| 351 | Expression expr(&expr_buffer); |
| 352 | // The address of the object has been implicitly pushed on the stack. |
| 353 | // Dereference the klass_ field of Object (32-bit; possibly poisoned). |
| 354 | DCHECK_EQ(type->ClassOffset().Uint32Value(), 0u); |
| 355 | DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Class>), 4u); |
| 356 | expr.WriteOpDerefSize(4); |
| 357 | if (kPoisonHeapReferences) { |
| 358 | expr.WriteOpNeg(); |
| 359 | // DWARF stack is pointer sized. Ensure that the high bits are clear. |
| 360 | expr.WriteOpConstu(0xFFFFFFFF); |
| 361 | expr.WriteOpAnd(); |
| 362 | } |
| 363 | // Add offset to the methods_ field. |
| 364 | expr.WriteOpPlusUconst(mirror::Class::MethodsOffset().Uint32Value()); |
| 365 | // Top of stack holds the location of the field now. |
| 366 | info_.WriteExprLoc(DW_AT_data_member_location, expr); |
| 367 | info_.EndTag(); // DW_TAG_member. |
| 368 | } |
| 369 | |
| 370 | // Base class. |
Andreas Gampe | 9810499 | 2018-10-16 12:49:47 -0700 | [diff] [blame] | 371 | ObjPtr<mirror::Class> base_class = type->GetSuperClass(); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 372 | if (base_class != nullptr) { |
| 373 | info_.StartTag(DW_TAG_inheritance); |
Andreas Gampe | 9810499 | 2018-10-16 12:49:47 -0700 | [diff] [blame] | 374 | base_class_references.emplace(info_.size(), base_class.Ptr()); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 375 | info_.WriteRef4(DW_AT_type, 0); |
| 376 | info_.WriteUdata(DW_AT_data_member_location, 0); |
| 377 | info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public); |
| 378 | info_.EndTag(); // DW_TAG_inheritance. |
| 379 | } |
| 380 | |
| 381 | // Member variables. |
| 382 | for (uint32_t i = 0, count = type->NumInstanceFields(); i < count; ++i) { |
| 383 | ArtField* field = type->GetInstanceField(i); |
| 384 | info_.StartTag(DW_TAG_member); |
| 385 | WriteName(field->GetName()); |
| 386 | WriteLazyType(field->GetTypeDescriptor()); |
| 387 | info_.WriteUdata(DW_AT_data_member_location, field->GetOffset().Uint32Value()); |
| 388 | uint32_t access_flags = field->GetAccessFlags(); |
| 389 | if (access_flags & kAccPublic) { |
| 390 | info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public); |
| 391 | } else if (access_flags & kAccProtected) { |
| 392 | info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_protected); |
| 393 | } else if (access_flags & kAccPrivate) { |
| 394 | info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private); |
| 395 | } |
| 396 | info_.EndTag(); // DW_TAG_member. |
| 397 | } |
| 398 | |
| 399 | if (type->IsStringClass()) { |
| 400 | // Emit debug info about an artifical class member for java.lang.String which represents |
| 401 | // the first element of the data stored in a string instance. Consumers of the debug |
| 402 | // info will be able to read the content of java.lang.String based on the count (real |
| 403 | // field) and based on the location of this data member. |
| 404 | info_.StartTag(DW_TAG_member); |
| 405 | WriteName("value"); |
| 406 | // We don't support fields with C like array types so we just say its type is java char. |
| 407 | WriteLazyType("C"); // char. |
| 408 | info_.WriteUdata(DW_AT_data_member_location, |
| 409 | mirror::String::ValueOffset().Uint32Value()); |
| 410 | info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private); |
| 411 | info_.EndTag(); // DW_TAG_member. |
| 412 | } |
| 413 | |
| 414 | EndClassTag(); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | // Write base class declarations. |
| 419 | for (const auto& base_class_reference : base_class_references) { |
| 420 | size_t reference_offset = base_class_reference.first; |
| 421 | mirror::Class* base_class = base_class_reference.second; |
Vladimir Marko | 3bada4b | 2017-05-19 12:32:47 +0100 | [diff] [blame] | 422 | const auto it = class_declarations.find(base_class); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 423 | if (it != class_declarations.end()) { |
| 424 | info_.UpdateUint32(reference_offset, it->second); |
| 425 | } else { |
| 426 | // Declare base class. We can not use the standard WriteLazyType |
| 427 | // since we want to avoid the DW_TAG_reference_tag wrapping. |
| 428 | std::string tmp_storage; |
| 429 | const char* base_class_desc = base_class->GetDescriptor(&tmp_storage); |
| 430 | size_t base_class_declaration_offset = StartClassTag(base_class_desc); |
| 431 | info_.WriteFlagPresent(DW_AT_declaration); |
| 432 | WriteLinkageName(base_class); |
| 433 | EndClassTag(); |
| 434 | class_declarations.emplace(base_class, base_class_declaration_offset); |
| 435 | info_.UpdateUint32(reference_offset, base_class_declaration_offset); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | FinishLazyTypes(); |
| 440 | CloseNamespacesAboveDepth(0); |
| 441 | info_.EndTag(); // DW_TAG_compile_unit. |
| 442 | CHECK_EQ(info_.Depth(), 0); |
| 443 | std::vector<uint8_t> buffer; |
| 444 | buffer.reserve(info_.data()->size() + KB); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 445 | // All compilation units share single table which is at the start of .debug_abbrev. |
| 446 | const size_t debug_abbrev_offset = 0; |
David Srbecky | 7370d92 | 2019-02-12 14:00:30 +0000 | [diff] [blame] | 447 | WriteDebugInfoCU(debug_abbrev_offset, info_, &buffer); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 448 | owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size()); |
| 449 | } |
| 450 | |
| 451 | // Write table into .debug_loc which describes location of dex register. |
| 452 | // The dex register might be valid only at some points and it might |
| 453 | // move between machine registers and stack. |
| 454 | void WriteRegLocation(const MethodDebugInfo* method_info, |
David Srbecky | 2ed15b6 | 2016-03-04 11:34:46 +0000 | [diff] [blame] | 455 | const std::vector<DexRegisterMap>& dex_register_maps, |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 456 | uint16_t vreg, |
| 457 | bool is64bitValue, |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 458 | uint64_t compilation_unit_code_address, |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 459 | uint32_t dex_pc_low = 0, |
| 460 | uint32_t dex_pc_high = 0xFFFFFFFF) { |
| 461 | WriteDebugLocEntry(method_info, |
David Srbecky | 2ed15b6 | 2016-03-04 11:34:46 +0000 | [diff] [blame] | 462 | dex_register_maps, |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 463 | vreg, |
| 464 | is64bitValue, |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 465 | compilation_unit_code_address, |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 466 | dex_pc_low, |
| 467 | dex_pc_high, |
| 468 | owner_->builder_->GetIsa(), |
| 469 | &info_, |
| 470 | &owner_->debug_loc_, |
| 471 | &owner_->debug_ranges_); |
| 472 | } |
| 473 | |
| 474 | // Linkage name uniquely identifies type. |
| 475 | // It is used to determine the dynamic type of objects. |
| 476 | // We use the methods_ field of class since it is unique and it is not moved by the GC. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 477 | void WriteLinkageName(mirror::Class* type) REQUIRES_SHARED(Locks::mutator_lock_) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 478 | auto* methods_ptr = type->GetMethodsPtr(); |
| 479 | if (methods_ptr == nullptr) { |
| 480 | // Some types might have no methods. Allocate empty array instead. |
| 481 | LinearAlloc* allocator = Runtime::Current()->GetLinearAlloc(); |
Lokesh Gidra | 606bd94 | 2022-05-23 19:00:09 +0000 | [diff] [blame] | 482 | void* storage = allocator->Alloc(Thread::Current(), |
| 483 | sizeof(LengthPrefixedArray<ArtMethod>), |
| 484 | LinearAllocKind::kNoGCRoots); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 485 | methods_ptr = new (storage) LengthPrefixedArray<ArtMethod>(0); |
| 486 | type->SetMethodsPtr(methods_ptr, 0, 0); |
| 487 | DCHECK(type->GetMethodsPtr() != nullptr); |
| 488 | } |
| 489 | char name[32]; |
| 490 | snprintf(name, sizeof(name), "0x%" PRIXPTR, reinterpret_cast<uintptr_t>(methods_ptr)); |
| 491 | info_.WriteString(dwarf::DW_AT_linkage_name, name); |
| 492 | } |
| 493 | |
| 494 | // Some types are difficult to define as we go since they need |
| 495 | // to be enclosed in the right set of namespaces. Therefore we |
| 496 | // just define all types lazily at the end of compilation unit. |
| 497 | void WriteLazyType(const char* type_descriptor) { |
| 498 | if (type_descriptor != nullptr && type_descriptor[0] != 'V') { |
| 499 | lazy_types_.emplace(std::string(type_descriptor), info_.size()); |
| 500 | info_.WriteRef4(dwarf::DW_AT_type, 0); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | void FinishLazyTypes() { |
| 505 | for (const auto& lazy_type : lazy_types_) { |
| 506 | info_.UpdateUint32(lazy_type.second, WriteTypeDeclaration(lazy_type.first)); |
| 507 | } |
| 508 | lazy_types_.clear(); |
| 509 | } |
| 510 | |
| 511 | private: |
| 512 | void WriteName(const char* name) { |
| 513 | if (name != nullptr) { |
| 514 | info_.WriteString(dwarf::DW_AT_name, name); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | // Convert dex type descriptor to DWARF. |
| 519 | // Returns offset in the compilation unit. |
| 520 | size_t WriteTypeDeclaration(const std::string& desc) { |
| 521 | using namespace dwarf; // NOLINT. For easy access to DWARF constants. |
| 522 | |
| 523 | DCHECK(!desc.empty()); |
Vladimir Marko | 3bada4b | 2017-05-19 12:32:47 +0100 | [diff] [blame] | 524 | const auto it = type_cache_.find(desc); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 525 | if (it != type_cache_.end()) { |
| 526 | return it->second; |
| 527 | } |
| 528 | |
| 529 | size_t offset; |
| 530 | if (desc[0] == 'L') { |
| 531 | // Class type. For example: Lpackage/name; |
| 532 | size_t class_offset = StartClassTag(desc.c_str()); |
| 533 | info_.WriteFlagPresent(DW_AT_declaration); |
| 534 | EndClassTag(); |
| 535 | // Reference to the class type. |
| 536 | offset = info_.StartTag(DW_TAG_reference_type); |
| 537 | info_.WriteRef(DW_AT_type, class_offset); |
| 538 | info_.EndTag(); |
| 539 | } else if (desc[0] == '[') { |
| 540 | // Array type. |
| 541 | size_t element_type = WriteTypeDeclaration(desc.substr(1)); |
| 542 | CloseNamespacesAboveDepth(0); // Declare in root namespace. |
| 543 | size_t array_type = info_.StartTag(DW_TAG_array_type); |
| 544 | info_.WriteFlagPresent(DW_AT_declaration); |
| 545 | info_.WriteRef(DW_AT_type, element_type); |
| 546 | info_.EndTag(); |
| 547 | offset = info_.StartTag(DW_TAG_reference_type); |
| 548 | info_.WriteRef4(DW_AT_type, array_type); |
| 549 | info_.EndTag(); |
| 550 | } else { |
| 551 | // Primitive types. |
| 552 | DCHECK_EQ(desc.size(), 1u); |
| 553 | |
| 554 | const char* name; |
| 555 | uint32_t encoding; |
| 556 | uint32_t byte_size; |
| 557 | switch (desc[0]) { |
| 558 | case 'B': |
| 559 | name = "byte"; |
| 560 | encoding = DW_ATE_signed; |
| 561 | byte_size = 1; |
| 562 | break; |
| 563 | case 'C': |
| 564 | name = "char"; |
| 565 | encoding = DW_ATE_UTF; |
| 566 | byte_size = 2; |
| 567 | break; |
| 568 | case 'D': |
| 569 | name = "double"; |
| 570 | encoding = DW_ATE_float; |
| 571 | byte_size = 8; |
| 572 | break; |
| 573 | case 'F': |
| 574 | name = "float"; |
| 575 | encoding = DW_ATE_float; |
| 576 | byte_size = 4; |
| 577 | break; |
| 578 | case 'I': |
| 579 | name = "int"; |
| 580 | encoding = DW_ATE_signed; |
| 581 | byte_size = 4; |
| 582 | break; |
| 583 | case 'J': |
| 584 | name = "long"; |
| 585 | encoding = DW_ATE_signed; |
| 586 | byte_size = 8; |
| 587 | break; |
| 588 | case 'S': |
| 589 | name = "short"; |
| 590 | encoding = DW_ATE_signed; |
| 591 | byte_size = 2; |
| 592 | break; |
| 593 | case 'Z': |
| 594 | name = "boolean"; |
| 595 | encoding = DW_ATE_boolean; |
| 596 | byte_size = 1; |
| 597 | break; |
| 598 | case 'V': |
| 599 | LOG(FATAL) << "Void type should not be encoded"; |
| 600 | UNREACHABLE(); |
| 601 | default: |
| 602 | LOG(FATAL) << "Unknown dex type descriptor: \"" << desc << "\""; |
| 603 | UNREACHABLE(); |
| 604 | } |
| 605 | CloseNamespacesAboveDepth(0); // Declare in root namespace. |
| 606 | offset = info_.StartTag(DW_TAG_base_type); |
| 607 | WriteName(name); |
| 608 | info_.WriteData1(DW_AT_encoding, encoding); |
| 609 | info_.WriteData1(DW_AT_byte_size, byte_size); |
| 610 | info_.EndTag(); |
| 611 | } |
| 612 | |
| 613 | type_cache_.emplace(desc, offset); |
| 614 | return offset; |
| 615 | } |
| 616 | |
| 617 | // Start DW_TAG_class_type tag nested in DW_TAG_namespace tags. |
| 618 | // Returns offset of the class tag in the compilation unit. |
| 619 | size_t StartClassTag(const char* desc) { |
| 620 | std::string name = SetNamespaceForClass(desc); |
| 621 | size_t offset = info_.StartTag(dwarf::DW_TAG_class_type); |
| 622 | WriteName(name.c_str()); |
| 623 | return offset; |
| 624 | } |
| 625 | |
| 626 | void EndClassTag() { |
| 627 | info_.EndTag(); |
| 628 | } |
| 629 | |
| 630 | // Set the current namespace nesting to one required by the given class. |
| 631 | // Returns the class name with namespaces, 'L', and ';' stripped. |
| 632 | std::string SetNamespaceForClass(const char* desc) { |
| 633 | DCHECK(desc != nullptr && desc[0] == 'L'); |
| 634 | desc++; // Skip the initial 'L'. |
| 635 | size_t depth = 0; |
| 636 | for (const char* end; (end = strchr(desc, '/')) != nullptr; desc = end + 1, ++depth) { |
| 637 | // Check whether the name at this depth is already what we need. |
| 638 | if (depth < current_namespace_.size()) { |
| 639 | const std::string& name = current_namespace_[depth]; |
| 640 | if (name.compare(0, name.size(), desc, end - desc) == 0) { |
| 641 | continue; |
| 642 | } |
| 643 | } |
| 644 | // Otherwise we need to open a new namespace tag at this depth. |
| 645 | CloseNamespacesAboveDepth(depth); |
| 646 | info_.StartTag(dwarf::DW_TAG_namespace); |
| 647 | std::string name(desc, end - desc); |
| 648 | WriteName(name.c_str()); |
| 649 | current_namespace_.push_back(std::move(name)); |
| 650 | } |
| 651 | CloseNamespacesAboveDepth(depth); |
| 652 | return std::string(desc, strchr(desc, ';') - desc); |
| 653 | } |
| 654 | |
| 655 | // Close namespace tags to reach the given nesting depth. |
| 656 | void CloseNamespacesAboveDepth(size_t depth) { |
| 657 | DCHECK_LE(depth, current_namespace_.size()); |
| 658 | while (current_namespace_.size() > depth) { |
| 659 | info_.EndTag(); |
| 660 | current_namespace_.pop_back(); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | // For access to the ELF sections. |
| 665 | ElfDebugInfoWriter<ElfTypes>* owner_; |
| 666 | // Temporary buffer to create and store the entries. |
| 667 | dwarf::DebugInfoEntryWriter<> info_; |
| 668 | // Cache of already translated type descriptors. |
| 669 | std::map<std::string, size_t> type_cache_; // type_desc -> definition_offset. |
| 670 | // 32-bit references which need to be resolved to a type later. |
| 671 | // Given type may be used multiple times. Therefore we need a multimap. |
| 672 | std::multimap<std::string, size_t> lazy_types_; // type_desc -> patch_offset. |
| 673 | // The current set of open namespace tags which are active and not closed yet. |
| 674 | std::vector<std::string> current_namespace_; |
| 675 | }; |
| 676 | |
| 677 | } // namespace debug |
| 678 | } // namespace art |
| 679 | |
| 680 | #endif // ART_COMPILER_DEBUG_ELF_DEBUG_INFO_WRITER_H_ |
| 681 | |