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