1 //===-- DWARFDIE.cpp ------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "DWARFDIE.h" 10 11 #include "DWARFASTParser.h" 12 #include "DWARFDebugInfo.h" 13 #include "DWARFDebugInfoEntry.h" 14 #include "DWARFDeclContext.h" 15 #include "DWARFUnit.h" 16 17 #include "llvm/ADT/iterator.h" 18 19 using namespace lldb_private; 20 using namespace lldb_private::dwarf; 21 22 namespace { 23 24 /// Iterate through all DIEs elaborating (i.e. reachable by a chain of 25 /// DW_AT_specification and DW_AT_abstract_origin attributes) a given DIE. For 26 /// convenience, the starting die is included in the sequence as the first 27 /// item. 28 class ElaboratingDIEIterator 29 : public llvm::iterator_facade_base< 30 ElaboratingDIEIterator, std::input_iterator_tag, DWARFDIE, 31 std::ptrdiff_t, DWARFDIE *, DWARFDIE *> { 32 33 // The operating invariant is: top of m_worklist contains the "current" item 34 // and the rest of the list are items yet to be visited. An empty worklist 35 // means we've reached the end. 36 // Infinite recursion is prevented by maintaining a list of seen DIEs. 37 // Container sizes are optimized for the case of following DW_AT_specification 38 // and DW_AT_abstract_origin just once. 39 llvm::SmallVector<DWARFDIE, 2> m_worklist; 40 llvm::SmallSet<DWARFDebugInfoEntry *, 3> m_seen; 41 42 void Next() { 43 assert(!m_worklist.empty() && "Incrementing end iterator?"); 44 45 // Pop the current item from the list. 46 DWARFDIE die = m_worklist.back(); 47 m_worklist.pop_back(); 48 49 // And add back any items that elaborate it. 50 for (dw_attr_t attr : {DW_AT_specification, DW_AT_abstract_origin}) { 51 if (DWARFDIE d = die.GetReferencedDIE(attr)) 52 if (m_seen.insert(die.GetDIE()).second) 53 m_worklist.push_back(d); 54 } 55 } 56 57 public: 58 /// An iterator starting at die d. 59 explicit ElaboratingDIEIterator(DWARFDIE d) : m_worklist(1, d) {} 60 61 /// End marker 62 ElaboratingDIEIterator() = default; 63 64 const DWARFDIE &operator*() const { return m_worklist.back(); } 65 ElaboratingDIEIterator &operator++() { 66 Next(); 67 return *this; 68 } 69 70 friend bool operator==(const ElaboratingDIEIterator &a, 71 const ElaboratingDIEIterator &b) { 72 if (a.m_worklist.empty() || b.m_worklist.empty()) 73 return a.m_worklist.empty() == b.m_worklist.empty(); 74 return a.m_worklist.back() == b.m_worklist.back(); 75 } 76 }; 77 78 llvm::iterator_range<ElaboratingDIEIterator> 79 elaborating_dies(const DWARFDIE &die) { 80 return llvm::make_range(ElaboratingDIEIterator(die), 81 ElaboratingDIEIterator()); 82 } 83 } // namespace 84 85 DWARFDIE 86 DWARFDIE::GetParent() const { 87 if (IsValid()) 88 return DWARFDIE(m_cu, m_die->GetParent()); 89 else 90 return DWARFDIE(); 91 } 92 93 DWARFDIE 94 DWARFDIE::GetFirstChild() const { 95 if (IsValid()) 96 return DWARFDIE(m_cu, m_die->GetFirstChild()); 97 else 98 return DWARFDIE(); 99 } 100 101 DWARFDIE 102 DWARFDIE::GetSibling() const { 103 if (IsValid()) 104 return DWARFDIE(m_cu, m_die->GetSibling()); 105 else 106 return DWARFDIE(); 107 } 108 109 DWARFDIE 110 DWARFDIE::GetReferencedDIE(const dw_attr_t attr) const { 111 if (IsValid()) 112 return m_die->GetAttributeValueAsReference(GetCU(), attr); 113 else 114 return {}; 115 } 116 117 DWARFDIE 118 DWARFDIE::GetDIE(dw_offset_t die_offset) const { 119 if (IsValid()) 120 return m_cu->GetDIE(die_offset); 121 else 122 return DWARFDIE(); 123 } 124 125 DWARFDIE 126 DWARFDIE::GetAttributeValueAsReferenceDIE(const dw_attr_t attr) const { 127 if (IsValid()) { 128 DWARFUnit *cu = GetCU(); 129 const bool check_specification_or_abstract_origin = true; 130 DWARFFormValue form_value; 131 if (m_die->GetAttributeValue(cu, attr, form_value, nullptr, 132 check_specification_or_abstract_origin)) 133 return form_value.Reference(); 134 } 135 return DWARFDIE(); 136 } 137 138 DWARFDIE 139 DWARFDIE::LookupDeepestBlock(lldb::addr_t address) const { 140 if (!IsValid()) 141 return DWARFDIE(); 142 143 DWARFDIE result; 144 bool check_children = false; 145 bool match_addr_range = false; 146 switch (Tag()) { 147 case DW_TAG_class_type: 148 case DW_TAG_namespace: 149 case DW_TAG_structure_type: 150 case DW_TAG_common_block: 151 check_children = true; 152 break; 153 case DW_TAG_compile_unit: 154 case DW_TAG_module: 155 case DW_TAG_catch_block: 156 case DW_TAG_subprogram: 157 case DW_TAG_try_block: 158 case DW_TAG_partial_unit: 159 match_addr_range = true; 160 break; 161 case DW_TAG_lexical_block: 162 case DW_TAG_inlined_subroutine: 163 check_children = true; 164 match_addr_range = true; 165 break; 166 default: 167 break; 168 } 169 170 if (match_addr_range) { 171 DWARFRangeList ranges = 172 m_die->GetAttributeAddressRanges(m_cu, /*check_hi_lo_pc=*/true); 173 if (ranges.FindEntryThatContains(address)) { 174 check_children = true; 175 switch (Tag()) { 176 default: 177 break; 178 179 case DW_TAG_inlined_subroutine: // Inlined Function 180 case DW_TAG_lexical_block: // Block { } in code 181 result = *this; 182 break; 183 } 184 } else { 185 check_children = false; 186 } 187 } 188 189 if (check_children) { 190 for (DWARFDIE child : children()) { 191 if (DWARFDIE child_result = child.LookupDeepestBlock(address)) 192 return child_result; 193 } 194 } 195 return result; 196 } 197 198 const char *DWARFDIE::GetMangledName() const { 199 if (IsValid()) 200 return m_die->GetMangledName(m_cu); 201 else 202 return nullptr; 203 } 204 205 const char *DWARFDIE::GetPubname() const { 206 if (IsValid()) 207 return m_die->GetPubname(m_cu); 208 else 209 return nullptr; 210 } 211 212 // GetName 213 // 214 // Get value of the DW_AT_name attribute and place that value into the supplied 215 // stream object. If the DIE is a NULL object "NULL" is placed into the stream, 216 // and if no DW_AT_name attribute exists for the DIE then nothing is printed. 217 void DWARFDIE::GetName(Stream &s) const { 218 if (!IsValid()) 219 return; 220 if (GetDIE()->IsNULL()) { 221 s.PutCString("NULL"); 222 return; 223 } 224 const char *name = GetDIE()->GetAttributeValueAsString(GetCU(), DW_AT_name, nullptr, true); 225 if (!name) 226 return; 227 s.PutCString(name); 228 } 229 230 // AppendTypeName 231 // 232 // Follows the type name definition down through all needed tags to end up with 233 // a fully qualified type name and dump the results to the supplied stream. 234 // This is used to show the name of types given a type identifier. 235 void DWARFDIE::AppendTypeName(Stream &s) const { 236 if (!IsValid()) 237 return; 238 if (GetDIE()->IsNULL()) { 239 s.PutCString("NULL"); 240 return; 241 } 242 if (const char *name = GetPubname()) { 243 s.PutCString(name); 244 return; 245 } 246 switch (Tag()) { 247 case DW_TAG_array_type: 248 break; // print out a "[]" after printing the full type of the element 249 // below 250 case DW_TAG_base_type: 251 s.PutCString("base "); 252 break; 253 case DW_TAG_class_type: 254 s.PutCString("class "); 255 break; 256 case DW_TAG_const_type: 257 s.PutCString("const "); 258 break; 259 case DW_TAG_enumeration_type: 260 s.PutCString("enum "); 261 break; 262 case DW_TAG_file_type: 263 s.PutCString("file "); 264 break; 265 case DW_TAG_interface_type: 266 s.PutCString("interface "); 267 break; 268 case DW_TAG_packed_type: 269 s.PutCString("packed "); 270 break; 271 case DW_TAG_pointer_type: 272 break; // print out a '*' after printing the full type below 273 case DW_TAG_ptr_to_member_type: 274 break; // print out a '*' after printing the full type below 275 case DW_TAG_reference_type: 276 break; // print out a '&' after printing the full type below 277 case DW_TAG_restrict_type: 278 s.PutCString("restrict "); 279 break; 280 case DW_TAG_set_type: 281 s.PutCString("set "); 282 break; 283 case DW_TAG_shared_type: 284 s.PutCString("shared "); 285 break; 286 case DW_TAG_string_type: 287 s.PutCString("string "); 288 break; 289 case DW_TAG_structure_type: 290 s.PutCString("struct "); 291 break; 292 case DW_TAG_subrange_type: 293 s.PutCString("subrange "); 294 break; 295 case DW_TAG_subroutine_type: 296 s.PutCString("function "); 297 break; 298 case DW_TAG_thrown_type: 299 s.PutCString("thrown "); 300 break; 301 case DW_TAG_union_type: 302 s.PutCString("union "); 303 break; 304 case DW_TAG_unspecified_type: 305 s.PutCString("unspecified "); 306 break; 307 case DW_TAG_volatile_type: 308 s.PutCString("volatile "); 309 break; 310 case DW_TAG_LLVM_ptrauth_type: { 311 unsigned key = GetAttributeValueAsUnsigned(DW_AT_LLVM_ptrauth_key, 0); 312 bool isAddressDiscriminated = GetAttributeValueAsUnsigned( 313 DW_AT_LLVM_ptrauth_address_discriminated, 0); 314 unsigned extraDiscriminator = 315 GetAttributeValueAsUnsigned(DW_AT_LLVM_ptrauth_extra_discriminator, 0); 316 bool isaPointer = 317 GetAttributeValueAsUnsigned(DW_AT_LLVM_ptrauth_isa_pointer, 0); 318 s.Printf("__ptrauth(%d, %d, 0x0%x, %d)", key, isAddressDiscriminated, 319 extraDiscriminator, isaPointer); 320 break; 321 } 322 default: 323 return; 324 } 325 326 // Follow the DW_AT_type if possible 327 if (DWARFDIE next_die = GetAttributeValueAsReferenceDIE(DW_AT_type)) 328 next_die.AppendTypeName(s); 329 330 switch (Tag()) { 331 case DW_TAG_array_type: 332 s.PutCString("[]"); 333 break; 334 case DW_TAG_pointer_type: 335 s.PutChar('*'); 336 break; 337 case DW_TAG_ptr_to_member_type: 338 s.PutChar('*'); 339 break; 340 case DW_TAG_reference_type: 341 s.PutChar('&'); 342 break; 343 default: 344 break; 345 } 346 } 347 348 lldb_private::Type *DWARFDIE::ResolveType() const { 349 if (IsValid()) 350 return GetDWARF()->ResolveType(*this, true); 351 else 352 return nullptr; 353 } 354 355 lldb_private::Type *DWARFDIE::ResolveTypeUID(const DWARFDIE &die) const { 356 if (SymbolFileDWARF *dwarf = GetDWARF()) 357 return dwarf->ResolveTypeUID(die, true); 358 return nullptr; 359 } 360 361 std::vector<DWARFDIE> DWARFDIE::GetDeclContextDIEs() const { 362 if (!IsValid()) 363 return {}; 364 365 std::vector<DWARFDIE> result; 366 DWARFDIE parent = GetParentDeclContextDIE(); 367 while (parent.IsValid() && parent.GetDIE() != GetDIE()) { 368 result.push_back(std::move(parent)); 369 parent = parent.GetParentDeclContextDIE(); 370 } 371 372 return result; 373 } 374 375 void DWARFDIE::GetDeclContext( 376 llvm::SmallVectorImpl<lldb_private::CompilerContext> &context) const { 377 const dw_tag_t tag = Tag(); 378 if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit) 379 return; 380 DWARFDIE parent = GetParent(); 381 if (parent) 382 parent.GetDeclContext(context); 383 switch (tag) { 384 case DW_TAG_module: 385 context.push_back({CompilerContextKind::Module, ConstString(GetName())}); 386 break; 387 case DW_TAG_namespace: 388 context.push_back({CompilerContextKind::Namespace, ConstString(GetName())}); 389 break; 390 case DW_TAG_structure_type: 391 context.push_back({CompilerContextKind::Struct, ConstString(GetName())}); 392 break; 393 case DW_TAG_union_type: 394 context.push_back({CompilerContextKind::Union, ConstString(GetName())}); 395 break; 396 case DW_TAG_class_type: 397 context.push_back({CompilerContextKind::Class, ConstString(GetName())}); 398 break; 399 case DW_TAG_enumeration_type: 400 context.push_back({CompilerContextKind::Enum, ConstString(GetName())}); 401 break; 402 case DW_TAG_subprogram: 403 context.push_back( 404 {CompilerContextKind::Function, ConstString(GetPubname())}); 405 break; 406 case DW_TAG_variable: 407 context.push_back( 408 {CompilerContextKind::Variable, ConstString(GetPubname())}); 409 break; 410 case DW_TAG_typedef: 411 context.push_back({CompilerContextKind::Typedef, ConstString(GetName())}); 412 break; 413 default: 414 break; 415 } 416 } 417 418 DWARFDIE 419 DWARFDIE::GetParentDeclContextDIE() const { 420 if (IsValid()) 421 return m_die->GetParentDeclContextDIE(m_cu); 422 else 423 return DWARFDIE(); 424 } 425 426 bool DWARFDIE::IsStructUnionOrClass() const { 427 const dw_tag_t tag = Tag(); 428 return tag == DW_TAG_class_type || tag == DW_TAG_structure_type || 429 tag == DW_TAG_union_type; 430 } 431 432 bool DWARFDIE::IsMethod() const { 433 for (DWARFDIE d : elaborating_dies(*this)) 434 if (d.GetParent().IsStructUnionOrClass()) 435 return true; 436 return false; 437 } 438 439 bool DWARFDIE::GetDIENamesAndRanges( 440 const char *&name, const char *&mangled, DWARFRangeList &ranges, 441 std::optional<int> &decl_file, std::optional<int> &decl_line, 442 std::optional<int> &decl_column, std::optional<int> &call_file, 443 std::optional<int> &call_line, std::optional<int> &call_column, 444 lldb_private::DWARFExpressionList *frame_base) const { 445 if (IsValid()) { 446 return m_die->GetDIENamesAndRanges( 447 GetCU(), name, mangled, ranges, decl_file, decl_line, decl_column, 448 call_file, call_line, call_column, frame_base); 449 } else 450 return false; 451 } 452 453 llvm::iterator_range<DWARFDIE::child_iterator> DWARFDIE::children() const { 454 return llvm::make_range(child_iterator(*this), child_iterator()); 455 } 456