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 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<DWARFDebugInfoEntry *, 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.GetDIE()).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() = default; 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 address) const { 144 if (!IsValid()) 145 return DWARFDIE(); 146 147 DWARFDIE result; 148 bool check_children = false; 149 bool match_addr_range = false; 150 switch (Tag()) { 151 case DW_TAG_class_type: 152 case DW_TAG_namespace: 153 case DW_TAG_structure_type: 154 case DW_TAG_common_block: 155 check_children = true; 156 break; 157 case DW_TAG_compile_unit: 158 case DW_TAG_module: 159 case DW_TAG_catch_block: 160 case DW_TAG_subprogram: 161 case DW_TAG_try_block: 162 case DW_TAG_partial_unit: 163 match_addr_range = true; 164 break; 165 case DW_TAG_lexical_block: 166 case DW_TAG_inlined_subroutine: 167 check_children = true; 168 match_addr_range = true; 169 break; 170 default: 171 break; 172 } 173 174 if (match_addr_range) { 175 DWARFRangeList ranges; 176 if (m_die->GetAttributeAddressRanges(m_cu, ranges, 177 /*check_hi_lo_pc=*/true) && 178 ranges.FindEntryThatContains(address)) { 179 check_children = true; 180 switch (Tag()) { 181 default: 182 break; 183 184 case DW_TAG_inlined_subroutine: // Inlined Function 185 case DW_TAG_lexical_block: // Block { } in code 186 result = *this; 187 break; 188 } 189 } else { 190 check_children = false; 191 } 192 } 193 194 if (check_children) { 195 for (DWARFDIE child : children()) { 196 if (DWARFDIE child_result = child.LookupDeepestBlock(address)) 197 return child_result; 198 } 199 } 200 return result; 201 } 202 203 const char *DWARFDIE::GetMangledName() const { 204 if (IsValid()) 205 return m_die->GetMangledName(m_cu); 206 else 207 return nullptr; 208 } 209 210 const char *DWARFDIE::GetPubname() const { 211 if (IsValid()) 212 return m_die->GetPubname(m_cu); 213 else 214 return nullptr; 215 } 216 217 const char *DWARFDIE::GetQualifiedName(std::string &storage) const { 218 if (IsValid()) 219 return m_die->GetQualifiedName(m_cu, storage); 220 else 221 return nullptr; 222 } 223 224 // GetName 225 // 226 // Get value of the DW_AT_name attribute and place that value into the supplied 227 // stream object. If the DIE is a NULL object "NULL" is placed into the stream, 228 // and if no DW_AT_name attribute exists for the DIE then nothing is printed. 229 void DWARFDIE::GetName(Stream &s) const { 230 if (!IsValid()) 231 return; 232 if (GetDIE()->IsNULL()) { 233 s.PutCString("NULL"); 234 return; 235 } 236 const char *name = GetDIE()->GetAttributeValueAsString(GetCU(), DW_AT_name, nullptr, true); 237 if (!name) 238 return; 239 s.PutCString(name); 240 } 241 242 // AppendTypeName 243 // 244 // Follows the type name definition down through all needed tags to end up with 245 // a fully qualified type name and dump the results to the supplied stream. 246 // This is used to show the name of types given a type identifier. 247 void DWARFDIE::AppendTypeName(Stream &s) const { 248 if (!IsValid()) 249 return; 250 if (GetDIE()->IsNULL()) { 251 s.PutCString("NULL"); 252 return; 253 } 254 if (const char *name = GetPubname()) { 255 s.PutCString(name); 256 return; 257 } 258 switch (Tag()) { 259 case DW_TAG_array_type: 260 break; // print out a "[]" after printing the full type of the element 261 // below 262 case DW_TAG_base_type: 263 s.PutCString("base "); 264 break; 265 case DW_TAG_class_type: 266 s.PutCString("class "); 267 break; 268 case DW_TAG_const_type: 269 s.PutCString("const "); 270 break; 271 case DW_TAG_enumeration_type: 272 s.PutCString("enum "); 273 break; 274 case DW_TAG_file_type: 275 s.PutCString("file "); 276 break; 277 case DW_TAG_interface_type: 278 s.PutCString("interface "); 279 break; 280 case DW_TAG_packed_type: 281 s.PutCString("packed "); 282 break; 283 case DW_TAG_pointer_type: 284 break; // print out a '*' after printing the full type below 285 case DW_TAG_ptr_to_member_type: 286 break; // print out a '*' after printing the full type below 287 case DW_TAG_reference_type: 288 break; // print out a '&' after printing the full type below 289 case DW_TAG_restrict_type: 290 s.PutCString("restrict "); 291 break; 292 case DW_TAG_set_type: 293 s.PutCString("set "); 294 break; 295 case DW_TAG_shared_type: 296 s.PutCString("shared "); 297 break; 298 case DW_TAG_string_type: 299 s.PutCString("string "); 300 break; 301 case DW_TAG_structure_type: 302 s.PutCString("struct "); 303 break; 304 case DW_TAG_subrange_type: 305 s.PutCString("subrange "); 306 break; 307 case DW_TAG_subroutine_type: 308 s.PutCString("function "); 309 break; 310 case DW_TAG_thrown_type: 311 s.PutCString("thrown "); 312 break; 313 case DW_TAG_union_type: 314 s.PutCString("union "); 315 break; 316 case DW_TAG_unspecified_type: 317 s.PutCString("unspecified "); 318 break; 319 case DW_TAG_volatile_type: 320 s.PutCString("volatile "); 321 break; 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 int &decl_file, int &decl_line, int &decl_column, int &call_file, 442 int &call_line, int &call_column, 443 lldb_private::DWARFExpression *frame_base) const { 444 if (IsValid()) { 445 return m_die->GetDIENamesAndRanges( 446 GetCU(), name, mangled, ranges, decl_file, decl_line, decl_column, 447 call_file, call_line, call_column, frame_base); 448 } else 449 return false; 450 } 451