1 //===-- DWARFDebugInfoEntry.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 "DWARFDebugInfoEntry.h"
10 
11 #include <assert.h>
12 
13 #include <algorithm>
14 
15 #include "lldb/Core/Module.h"
16 #include "lldb/Expression/DWARFExpression.h"
17 #include "lldb/Symbol/ObjectFile.h"
18 #include "lldb/Utility/Stream.h"
19 
20 #include "DWARFUnit.h"
21 #include "DWARFDIECollection.h"
22 #include "DWARFDebugAbbrev.h"
23 #include "DWARFDebugAranges.h"
24 #include "DWARFDebugInfo.h"
25 #include "DWARFDebugRanges.h"
26 #include "DWARFDeclContext.h"
27 #include "DWARFFormValue.h"
28 #include "SymbolFileDWARF.h"
29 #include "SymbolFileDWARFDwo.h"
30 
31 using namespace lldb_private;
32 using namespace std;
33 extern int g_verbose;
34 
35 bool DWARFDebugInfoEntry::FastExtract(
36     const DWARFDataExtractor &debug_info_data, const DWARFUnit *cu,
37     const DWARFFormValue::FixedFormSizes &fixed_form_sizes,
38     lldb::offset_t *offset_ptr) {
39   m_offset = *offset_ptr;
40   m_parent_idx = 0;
41   m_sibling_idx = 0;
42   const uint64_t abbr_idx = debug_info_data.GetULEB128(offset_ptr);
43   lldbassert(abbr_idx <= UINT16_MAX);
44   m_abbr_idx = abbr_idx;
45 
46   // assert (fixed_form_sizes);  // For best performance this should be
47   // specified!
48 
49   if (m_abbr_idx) {
50     lldb::offset_t offset = *offset_ptr;
51 
52     const DWARFAbbreviationDeclaration *abbrevDecl =
53         cu->GetAbbreviations()->GetAbbreviationDeclaration(m_abbr_idx);
54 
55     if (abbrevDecl == NULL) {
56       cu->GetSymbolFileDWARF()->GetObjectFile()->GetModule()->ReportError(
57           "{0x%8.8x}: invalid abbreviation code %u, please file a bug and "
58           "attach the file at the start of this error message",
59           m_offset, (unsigned)abbr_idx);
60       // WE can't parse anymore if the DWARF is borked...
61       *offset_ptr = UINT32_MAX;
62       return false;
63     }
64     m_tag = abbrevDecl->Tag();
65     m_has_children = abbrevDecl->HasChildren();
66     // Skip all data in the .debug_info for the attributes
67     const uint32_t numAttributes = abbrevDecl->NumAttributes();
68     uint32_t i;
69     dw_form_t form;
70     for (i = 0; i < numAttributes; ++i) {
71       form = abbrevDecl->GetFormByIndexUnchecked(i);
72 
73       const uint8_t fixed_skip_size = fixed_form_sizes.GetSize(form);
74       if (fixed_skip_size)
75         offset += fixed_skip_size;
76       else {
77         bool form_is_indirect = false;
78         do {
79           form_is_indirect = false;
80           uint32_t form_size = 0;
81           switch (form) {
82           // Blocks if inlined data that have a length field and the data bytes
83           // inlined in the .debug_info
84           case DW_FORM_exprloc:
85           case DW_FORM_block:
86             form_size = debug_info_data.GetULEB128(&offset);
87             break;
88           case DW_FORM_block1:
89             form_size = debug_info_data.GetU8_unchecked(&offset);
90             break;
91           case DW_FORM_block2:
92             form_size = debug_info_data.GetU16_unchecked(&offset);
93             break;
94           case DW_FORM_block4:
95             form_size = debug_info_data.GetU32_unchecked(&offset);
96             break;
97 
98           // Inlined NULL terminated C-strings
99           case DW_FORM_string:
100             debug_info_data.GetCStr(&offset);
101             break;
102 
103           // Compile unit address sized values
104           case DW_FORM_addr:
105             form_size = cu->GetAddressByteSize();
106             break;
107           case DW_FORM_ref_addr:
108             if (cu->GetVersion() <= 2)
109               form_size = cu->GetAddressByteSize();
110             else
111               form_size = cu->IsDWARF64() ? 8 : 4;
112             break;
113 
114           // 0 sized form
115           case DW_FORM_flag_present:
116             form_size = 0;
117             break;
118 
119           // 1 byte values
120           case DW_FORM_addrx1:
121           case DW_FORM_data1:
122           case DW_FORM_flag:
123           case DW_FORM_ref1:
124           case DW_FORM_strx1:
125             form_size = 1;
126             break;
127 
128           // 2 byte values
129           case DW_FORM_addrx2:
130           case DW_FORM_data2:
131           case DW_FORM_ref2:
132           case DW_FORM_strx2:
133             form_size = 2;
134             break;
135 
136           // 3 byte values
137           case DW_FORM_addrx3:
138           case DW_FORM_strx3:
139             form_size = 3;
140             break;
141 
142           // 4 byte values
143           case DW_FORM_addrx4:
144           case DW_FORM_data4:
145           case DW_FORM_ref4:
146           case DW_FORM_strx4:
147             form_size = 4;
148             break;
149 
150           // 8 byte values
151           case DW_FORM_data8:
152           case DW_FORM_ref8:
153           case DW_FORM_ref_sig8:
154             form_size = 8;
155             break;
156 
157           // signed or unsigned LEB 128 values
158           case DW_FORM_addrx:
159           case DW_FORM_rnglistx:
160           case DW_FORM_sdata:
161           case DW_FORM_udata:
162           case DW_FORM_ref_udata:
163           case DW_FORM_GNU_addr_index:
164           case DW_FORM_GNU_str_index:
165           case DW_FORM_strx:
166             debug_info_data.Skip_LEB128(&offset);
167             break;
168 
169           case DW_FORM_indirect:
170             form_is_indirect = true;
171             form = debug_info_data.GetULEB128(&offset);
172             break;
173 
174           case DW_FORM_strp:
175           case DW_FORM_sec_offset:
176             if (cu->IsDWARF64())
177               debug_info_data.GetU64(&offset);
178             else
179               debug_info_data.GetU32(&offset);
180             break;
181 
182           case DW_FORM_implicit_const:
183             form_size = 0;
184             break;
185 
186           default:
187             *offset_ptr = m_offset;
188             return false;
189           }
190           offset += form_size;
191 
192         } while (form_is_indirect);
193       }
194     }
195     *offset_ptr = offset;
196     return true;
197   } else {
198     m_tag = 0;
199     m_has_children = false;
200     return true; // NULL debug tag entry
201   }
202 
203   return false;
204 }
205 
206 //----------------------------------------------------------------------
207 // Extract
208 //
209 // Extract a debug info entry for a given compile unit from the .debug_info and
210 // .debug_abbrev data within the SymbolFileDWARF class starting at the given
211 // offset
212 //----------------------------------------------------------------------
213 bool DWARFDebugInfoEntry::Extract(SymbolFileDWARF *dwarf2Data,
214                                   const DWARFUnit *cu,
215                                   lldb::offset_t *offset_ptr) {
216   const DWARFDataExtractor &debug_info_data = cu->GetData();
217   //    const DWARFDataExtractor& debug_str_data =
218   //    dwarf2Data->get_debug_str_data();
219   const uint32_t cu_end_offset = cu->GetNextCompileUnitOffset();
220   lldb::offset_t offset = *offset_ptr;
221   //  if (offset >= cu_end_offset)
222   //      Log::Status("DIE at offset 0x%8.8x is beyond the end of the current
223   //      compile unit (0x%8.8x)", m_offset, cu_end_offset);
224   if ((offset < cu_end_offset) && debug_info_data.ValidOffset(offset)) {
225     m_offset = offset;
226 
227     const uint64_t abbr_idx = debug_info_data.GetULEB128(&offset);
228     lldbassert(abbr_idx <= UINT16_MAX);
229     m_abbr_idx = abbr_idx;
230     if (abbr_idx) {
231       const DWARFAbbreviationDeclaration *abbrevDecl =
232           cu->GetAbbreviations()->GetAbbreviationDeclaration(abbr_idx);
233 
234       if (abbrevDecl) {
235         m_tag = abbrevDecl->Tag();
236         m_has_children = abbrevDecl->HasChildren();
237 
238         bool isCompileUnitTag = (m_tag == DW_TAG_compile_unit ||
239                                  m_tag == DW_TAG_partial_unit);
240         if (cu && isCompileUnitTag)
241           const_cast<DWARFUnit *>(cu)->SetBaseAddress(0);
242 
243         // Skip all data in the .debug_info for the attributes
244         const uint32_t numAttributes = abbrevDecl->NumAttributes();
245         for (uint32_t i = 0; i < numAttributes; ++i) {
246           DWARFFormValue form_value(cu);
247           dw_attr_t attr;
248           abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value);
249           dw_form_t form = form_value.Form();
250 
251           if (isCompileUnitTag &&
252               ((attr == DW_AT_entry_pc) || (attr == DW_AT_low_pc))) {
253             if (form_value.ExtractValue(debug_info_data, &offset)) {
254               if (attr == DW_AT_low_pc || attr == DW_AT_entry_pc)
255                 const_cast<DWARFUnit *>(cu)->SetBaseAddress(
256                     form_value.Address());
257             }
258           } else {
259             bool form_is_indirect = false;
260             do {
261               form_is_indirect = false;
262               uint32_t form_size = 0;
263               switch (form) {
264               // Blocks if inlined data that have a length field and the data
265               // bytes inlined in the .debug_info
266               case DW_FORM_exprloc:
267               case DW_FORM_block:
268                 form_size = debug_info_data.GetULEB128(&offset);
269                 break;
270               case DW_FORM_block1:
271                 form_size = debug_info_data.GetU8(&offset);
272                 break;
273               case DW_FORM_block2:
274                 form_size = debug_info_data.GetU16(&offset);
275                 break;
276               case DW_FORM_block4:
277                 form_size = debug_info_data.GetU32(&offset);
278                 break;
279 
280               // Inlined NULL terminated C-strings
281               case DW_FORM_string:
282                 debug_info_data.GetCStr(&offset);
283                 break;
284 
285               // Compile unit address sized values
286               case DW_FORM_addr:
287                 form_size = cu->GetAddressByteSize();
288                 break;
289               case DW_FORM_ref_addr:
290                 if (cu->GetVersion() <= 2)
291                   form_size = cu->GetAddressByteSize();
292                 else
293                   form_size = cu->IsDWARF64() ? 8 : 4;
294                 break;
295 
296               // 0 sized form
297               case DW_FORM_flag_present:
298               case DW_FORM_implicit_const:
299                 form_size = 0;
300                 break;
301 
302               // 1 byte values
303               case DW_FORM_data1:
304               case DW_FORM_flag:
305               case DW_FORM_ref1:
306                 form_size = 1;
307                 break;
308 
309               // 2 byte values
310               case DW_FORM_data2:
311               case DW_FORM_ref2:
312                 form_size = 2;
313                 break;
314 
315               // 4 byte values
316               case DW_FORM_data4:
317               case DW_FORM_ref4:
318                 form_size = 4;
319                 break;
320 
321               // 8 byte values
322               case DW_FORM_data8:
323               case DW_FORM_ref8:
324               case DW_FORM_ref_sig8:
325                 form_size = 8;
326                 break;
327 
328               // signed or unsigned LEB 128 values
329               case DW_FORM_sdata:
330               case DW_FORM_udata:
331               case DW_FORM_ref_udata:
332               case DW_FORM_GNU_addr_index:
333               case DW_FORM_GNU_str_index:
334                 debug_info_data.Skip_LEB128(&offset);
335                 break;
336 
337               case DW_FORM_indirect:
338                 form = debug_info_data.GetULEB128(&offset);
339                 form_is_indirect = true;
340                 break;
341 
342               case DW_FORM_strp:
343               case DW_FORM_sec_offset:
344                 if (cu->IsDWARF64())
345                   debug_info_data.GetU64(&offset);
346                 else
347                   debug_info_data.GetU32(&offset);
348                 break;
349 
350               default:
351                 *offset_ptr = offset;
352                 return false;
353               }
354 
355               offset += form_size;
356             } while (form_is_indirect);
357           }
358         }
359         *offset_ptr = offset;
360         return true;
361       }
362     } else {
363       m_tag = 0;
364       m_has_children = false;
365       *offset_ptr = offset;
366       return true; // NULL debug tag entry
367     }
368   }
369 
370   return false;
371 }
372 
373 //----------------------------------------------------------------------
374 // DumpAncestry
375 //
376 // Dumps all of a debug information entries parents up until oldest and all of
377 // it's attributes to the specified stream.
378 //----------------------------------------------------------------------
379 void DWARFDebugInfoEntry::DumpAncestry(SymbolFileDWARF *dwarf2Data,
380                                        const DWARFUnit *cu,
381                                        const DWARFDebugInfoEntry *oldest,
382                                        Stream &s,
383                                        uint32_t recurse_depth) const {
384   const DWARFDebugInfoEntry *parent = GetParent();
385   if (parent && parent != oldest)
386     parent->DumpAncestry(dwarf2Data, cu, oldest, s, 0);
387   Dump(dwarf2Data, cu, s, recurse_depth);
388 }
389 
390 static dw_offset_t GetRangesOffset(const DWARFDebugRangesBase *debug_ranges,
391                                    DWARFFormValue &form_value) {
392   if (form_value.Form() == DW_FORM_rnglistx)
393     return debug_ranges->GetOffset(form_value.Unsigned());
394   return form_value.Unsigned();
395 }
396 
397 //----------------------------------------------------------------------
398 // GetDIENamesAndRanges
399 //
400 // Gets the valid address ranges for a given DIE by looking for a
401 // DW_AT_low_pc/DW_AT_high_pc pair, DW_AT_entry_pc, or DW_AT_ranges attributes.
402 //----------------------------------------------------------------------
403 bool DWARFDebugInfoEntry::GetDIENamesAndRanges(
404     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu, const char *&name,
405     const char *&mangled, DWARFRangeList &ranges, int &decl_file,
406     int &decl_line, int &decl_column, int &call_file, int &call_line,
407     int &call_column, DWARFExpression *frame_base) const {
408   if (dwarf2Data == nullptr)
409     return false;
410 
411   SymbolFileDWARFDwo *dwo_symbol_file = cu->GetDwoSymbolFile();
412   if (dwo_symbol_file)
413     return GetDIENamesAndRanges(
414         dwo_symbol_file, dwo_symbol_file->GetCompileUnit(), name, mangled,
415         ranges, decl_file, decl_line, decl_column, call_file, call_line,
416         call_column, frame_base);
417 
418   dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
419   dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
420   std::vector<DIERef> die_refs;
421   bool set_frame_base_loclist_addr = false;
422 
423   lldb::offset_t offset;
424   const DWARFAbbreviationDeclaration *abbrevDecl =
425       GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset);
426 
427   lldb::ModuleSP module = dwarf2Data->GetObjectFile()->GetModule();
428 
429   if (abbrevDecl) {
430     const DWARFDataExtractor &debug_info_data = cu->GetData();
431 
432     if (!debug_info_data.ValidOffset(offset))
433       return false;
434 
435     const uint32_t numAttributes = abbrevDecl->NumAttributes();
436     bool do_offset = false;
437 
438     for (uint32_t i = 0; i < numAttributes; ++i) {
439       DWARFFormValue form_value(cu);
440       dw_attr_t attr;
441       abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value);
442 
443       if (form_value.ExtractValue(debug_info_data, &offset)) {
444         switch (attr) {
445         case DW_AT_low_pc:
446           lo_pc = form_value.Address();
447 
448           if (do_offset)
449             hi_pc += lo_pc;
450           do_offset = false;
451           break;
452 
453         case DW_AT_entry_pc:
454           lo_pc = form_value.Address();
455           break;
456 
457         case DW_AT_high_pc:
458           if (form_value.Form() == DW_FORM_addr ||
459               form_value.Form() == DW_FORM_GNU_addr_index) {
460             hi_pc = form_value.Address();
461           } else {
462             hi_pc = form_value.Unsigned();
463             if (lo_pc == LLDB_INVALID_ADDRESS)
464               do_offset = hi_pc != LLDB_INVALID_ADDRESS;
465             else
466               hi_pc += lo_pc; // DWARF 4 introduces <offset-from-lo-pc> to save
467                               // on relocations
468           }
469           break;
470 
471         case DW_AT_ranges: {
472           const DWARFDebugRangesBase *debug_ranges = dwarf2Data->DebugRanges();
473           if (debug_ranges)
474             debug_ranges->FindRanges(cu, GetRangesOffset(debug_ranges, form_value), ranges);
475           else
476             cu->GetSymbolFileDWARF()->GetObjectFile()->GetModule()->ReportError(
477                 "{0x%8.8x}: DIE has DW_AT_ranges(0x%" PRIx64
478                 ") attribute yet DWARF has no .debug_ranges, please file a bug "
479                 "and attach the file at the start of this error message",
480                 m_offset, form_value.Unsigned());
481         } break;
482 
483         case DW_AT_name:
484           if (name == NULL)
485             name = form_value.AsCString();
486           break;
487 
488         case DW_AT_MIPS_linkage_name:
489         case DW_AT_linkage_name:
490           if (mangled == NULL)
491             mangled = form_value.AsCString();
492           break;
493 
494         case DW_AT_abstract_origin:
495           die_refs.emplace_back(form_value);
496           break;
497 
498         case DW_AT_specification:
499           die_refs.emplace_back(form_value);
500           break;
501 
502         case DW_AT_decl_file:
503           if (decl_file == 0)
504             decl_file = form_value.Unsigned();
505           break;
506 
507         case DW_AT_decl_line:
508           if (decl_line == 0)
509             decl_line = form_value.Unsigned();
510           break;
511 
512         case DW_AT_decl_column:
513           if (decl_column == 0)
514             decl_column = form_value.Unsigned();
515           break;
516 
517         case DW_AT_call_file:
518           if (call_file == 0)
519             call_file = form_value.Unsigned();
520           break;
521 
522         case DW_AT_call_line:
523           if (call_line == 0)
524             call_line = form_value.Unsigned();
525           break;
526 
527         case DW_AT_call_column:
528           if (call_column == 0)
529             call_column = form_value.Unsigned();
530           break;
531 
532         case DW_AT_frame_base:
533           if (frame_base) {
534             if (form_value.BlockData()) {
535               uint32_t block_offset =
536                   form_value.BlockData() - debug_info_data.GetDataStart();
537               uint32_t block_length = form_value.Unsigned();
538               frame_base->SetOpcodeData(module, debug_info_data, block_offset,
539                                         block_length);
540             } else {
541               const DWARFDataExtractor &debug_loc_data =
542                   dwarf2Data->DebugLocData();
543               const dw_offset_t debug_loc_offset = form_value.Unsigned();
544 
545               size_t loc_list_length = DWARFExpression::LocationListSize(
546                   cu, debug_loc_data, debug_loc_offset);
547               if (loc_list_length > 0) {
548                 frame_base->SetOpcodeData(module, debug_loc_data,
549                                           debug_loc_offset, loc_list_length);
550                 if (lo_pc != LLDB_INVALID_ADDRESS) {
551                   assert(lo_pc >= cu->GetBaseAddress());
552                   frame_base->SetLocationListSlide(lo_pc -
553                                                    cu->GetBaseAddress());
554                 } else {
555                   set_frame_base_loclist_addr = true;
556                 }
557               }
558             }
559           }
560           break;
561 
562         default:
563           break;
564         }
565       }
566     }
567   }
568 
569   if (ranges.IsEmpty()) {
570     if (lo_pc != LLDB_INVALID_ADDRESS) {
571       if (hi_pc != LLDB_INVALID_ADDRESS && hi_pc > lo_pc)
572         ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc));
573       else
574         ranges.Append(DWARFRangeList::Entry(lo_pc, 0));
575     }
576   }
577 
578   if (set_frame_base_loclist_addr) {
579     dw_addr_t lowest_range_pc = ranges.GetMinRangeBase(0);
580     assert(lowest_range_pc >= cu->GetBaseAddress());
581     frame_base->SetLocationListSlide(lowest_range_pc - cu->GetBaseAddress());
582   }
583 
584   if (ranges.IsEmpty() || name == NULL || mangled == NULL) {
585     for (const DIERef &die_ref : die_refs) {
586       if (die_ref.die_offset != DW_INVALID_OFFSET) {
587         DWARFDIE die = dwarf2Data->GetDIE(die_ref);
588         if (die)
589           die.GetDIE()->GetDIENamesAndRanges(
590               die.GetDWARF(), die.GetCU(), name, mangled, ranges, decl_file,
591               decl_line, decl_column, call_file, call_line, call_column);
592       }
593     }
594   }
595   return !ranges.IsEmpty();
596 }
597 
598 //----------------------------------------------------------------------
599 // Dump
600 //
601 // Dumps a debug information entry and all of it's attributes to the specified
602 // stream.
603 //----------------------------------------------------------------------
604 void DWARFDebugInfoEntry::Dump(SymbolFileDWARF *dwarf2Data,
605                                const DWARFUnit *cu, Stream &s,
606                                uint32_t recurse_depth) const {
607   const DWARFDataExtractor &debug_info_data = cu->GetData();
608   lldb::offset_t offset = m_offset;
609 
610   if (debug_info_data.ValidOffset(offset)) {
611     dw_uleb128_t abbrCode = debug_info_data.GetULEB128(&offset);
612 
613     s.Printf("\n0x%8.8x: ", m_offset);
614     s.Indent();
615     if (abbrCode != m_abbr_idx) {
616       s.Printf("error: DWARF has been modified\n");
617     } else if (abbrCode) {
618       const DWARFAbbreviationDeclaration *abbrevDecl =
619           cu->GetAbbreviations()->GetAbbreviationDeclaration(abbrCode);
620 
621       if (abbrevDecl) {
622         s.PutCString(DW_TAG_value_to_name(abbrevDecl->Tag()));
623         s.Printf(" [%u] %c\n", abbrCode, abbrevDecl->HasChildren() ? '*' : ' ');
624 
625         // Dump all data in the .debug_info for the attributes
626         const uint32_t numAttributes = abbrevDecl->NumAttributes();
627         for (uint32_t i = 0; i < numAttributes; ++i) {
628           DWARFFormValue form_value(cu);
629           dw_attr_t attr;
630           abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value);
631 
632           DumpAttribute(dwarf2Data, cu, debug_info_data, &offset, s, attr,
633                         form_value);
634         }
635 
636         const DWARFDebugInfoEntry *child = GetFirstChild();
637         if (recurse_depth > 0 && child) {
638           s.IndentMore();
639 
640           while (child) {
641             child->Dump(dwarf2Data, cu, s, recurse_depth - 1);
642             child = child->GetSibling();
643           }
644           s.IndentLess();
645         }
646       } else
647         s.Printf("Abbreviation code note found in 'debug_abbrev' class for "
648                  "code: %u\n",
649                  abbrCode);
650     } else {
651       s.Printf("NULL\n");
652     }
653   }
654 }
655 
656 void DWARFDebugInfoEntry::DumpLocation(SymbolFileDWARF *dwarf2Data,
657                                        DWARFUnit *cu, Stream &s) const {
658   const DWARFBaseDIE cu_die = cu->GetUnitDIEOnly();
659   const char *cu_name = NULL;
660   if (cu_die)
661     cu_name = cu_die.GetName();
662   const char *obj_file_name = NULL;
663   ObjectFile *obj_file = dwarf2Data->GetObjectFile();
664   if (obj_file)
665     obj_file_name =
666         obj_file->GetFileSpec().GetFilename().AsCString("<Unknown>");
667   const char *die_name = GetName(dwarf2Data, cu);
668   s.Printf("0x%8.8x/0x%8.8x: %-30s (from %s in %s)", cu->GetOffset(),
669            GetOffset(), die_name ? die_name : "", cu_name ? cu_name : "<NULL>",
670            obj_file_name ? obj_file_name : "<NULL>");
671 }
672 
673 //----------------------------------------------------------------------
674 // DumpAttribute
675 //
676 // Dumps a debug information entry attribute along with it's form. Any special
677 // display of attributes is done (disassemble location lists, show enumeration
678 // values for attributes, etc).
679 //----------------------------------------------------------------------
680 void DWARFDebugInfoEntry::DumpAttribute(
681     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
682     const DWARFDataExtractor &debug_info_data, lldb::offset_t *offset_ptr,
683     Stream &s, dw_attr_t attr, DWARFFormValue &form_value) {
684   bool show_form = s.GetFlags().Test(DWARFDebugInfo::eDumpFlag_ShowForm);
685 
686   s.Printf("            ");
687   s.Indent(DW_AT_value_to_name(attr));
688 
689   if (show_form) {
690     s.Printf("[%s", DW_FORM_value_to_name(form_value.Form()));
691   }
692 
693   if (!form_value.ExtractValue(debug_info_data, offset_ptr))
694     return;
695 
696   if (show_form) {
697     if (form_value.Form() == DW_FORM_indirect) {
698       s.Printf(" [%s]", DW_FORM_value_to_name(form_value.Form()));
699     }
700 
701     s.PutCString("] ");
702   }
703 
704   s.PutCString("( ");
705 
706   // Check to see if we have any special attribute formatters
707   switch (attr) {
708   case DW_AT_stmt_list:
709     s.Printf("0x%8.8" PRIx64, form_value.Unsigned());
710     break;
711 
712   case DW_AT_language:
713     s.PutCString(DW_LANG_value_to_name(form_value.Unsigned()));
714     break;
715 
716   case DW_AT_encoding:
717     s.PutCString(DW_ATE_value_to_name(form_value.Unsigned()));
718     break;
719 
720   case DW_AT_frame_base:
721   case DW_AT_location:
722   case DW_AT_data_member_location: {
723     const uint8_t *blockData = form_value.BlockData();
724     if (blockData) {
725       // Location description is inlined in data in the form value
726       DWARFDataExtractor locationData(debug_info_data,
727                                       (*offset_ptr) - form_value.Unsigned(),
728                                       form_value.Unsigned());
729       DWARFExpression::PrintDWARFExpression(
730           s, locationData, DWARFUnit::GetAddressByteSize(cu), 4, false);
731     } else {
732       // We have a location list offset as the value that is the offset into
733       // the .debug_loc section that describes the value over it's lifetime
734       uint64_t debug_loc_offset = form_value.Unsigned();
735       if (dwarf2Data) {
736         DWARFExpression::PrintDWARFLocationList(
737             s, cu, dwarf2Data->DebugLocData(), debug_loc_offset);
738       }
739     }
740   } break;
741 
742   case DW_AT_abstract_origin:
743   case DW_AT_specification: {
744     uint64_t abstract_die_offset = form_value.Reference();
745     form_value.Dump(s);
746     //  *ostrm_ptr << HEX32 << abstract_die_offset << " ( ";
747     GetName(dwarf2Data, cu, abstract_die_offset, s);
748   } break;
749 
750   case DW_AT_type: {
751     uint64_t type_die_offset = form_value.Reference();
752     s.PutCString(" ( ");
753     AppendTypeName(dwarf2Data, cu, type_die_offset, s);
754     s.PutCString(" )");
755   } break;
756 
757   case DW_AT_ranges: {
758     if (!dwarf2Data)
759       break;
760     lldb::offset_t ranges_offset =
761         GetRangesOffset(dwarf2Data->DebugRanges(), form_value);
762     dw_addr_t base_addr = cu ? cu->GetBaseAddress() : 0;
763     DWARFDebugRanges::Dump(s, dwarf2Data->get_debug_ranges_data(),
764                            &ranges_offset, base_addr);
765   } break;
766 
767   default:
768     break;
769   }
770 
771   s.PutCString(" )\n");
772 }
773 
774 //----------------------------------------------------------------------
775 // Get all attribute values for a given DIE, including following any
776 // specification or abstract origin attributes and including those in the
777 // results. Any duplicate attributes will have the first instance take
778 // precedence (this can happen for declaration attributes).
779 //----------------------------------------------------------------------
780 size_t DWARFDebugInfoEntry::GetAttributes(
781     const DWARFUnit *cu, DWARFFormValue::FixedFormSizes fixed_form_sizes,
782     DWARFAttributes &attributes, uint32_t curr_depth) const {
783   SymbolFileDWARF *dwarf2Data = nullptr;
784   const DWARFAbbreviationDeclaration *abbrevDecl = nullptr;
785   lldb::offset_t offset = 0;
786   if (cu) {
787     if (m_tag != DW_TAG_compile_unit && m_tag != DW_TAG_partial_unit) {
788       SymbolFileDWARFDwo *dwo_symbol_file = cu->GetDwoSymbolFile();
789       if (dwo_symbol_file)
790         return GetAttributes(dwo_symbol_file->GetCompileUnit(),
791                              fixed_form_sizes, attributes, curr_depth);
792     }
793 
794     dwarf2Data = cu->GetSymbolFileDWARF();
795     abbrevDecl = GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset);
796   }
797 
798   if (abbrevDecl) {
799     const DWARFDataExtractor &debug_info_data = cu->GetData();
800 
801     if (fixed_form_sizes.Empty())
802       fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize(
803           cu->GetAddressByteSize(), cu->IsDWARF64());
804 
805     const uint32_t num_attributes = abbrevDecl->NumAttributes();
806     for (uint32_t i = 0; i < num_attributes; ++i) {
807       DWARFFormValue form_value(cu);
808       dw_attr_t attr;
809       abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value);
810       const dw_form_t form = form_value.Form();
811 
812       // If we are tracking down DW_AT_specification or DW_AT_abstract_origin
813       // attributes, the depth will be non-zero. We need to omit certain
814       // attributes that don't make sense.
815       switch (attr) {
816       case DW_AT_sibling:
817       case DW_AT_declaration:
818         if (curr_depth > 0) {
819           // This attribute doesn't make sense when combined with the DIE that
820           // references this DIE. We know a DIE is referencing this DIE because
821           // curr_depth is not zero
822           break;
823         }
824         LLVM_FALLTHROUGH;
825       default:
826         attributes.Append(cu, offset, attr, form);
827         break;
828       }
829 
830       if ((attr == DW_AT_specification) || (attr == DW_AT_abstract_origin)) {
831         if (form_value.ExtractValue(debug_info_data, &offset)) {
832           dw_offset_t die_offset = form_value.Reference();
833           DWARFDIE spec_die =
834               const_cast<DWARFUnit *>(cu)->GetDIE(die_offset);
835           if (spec_die)
836             spec_die.GetAttributes(attributes, curr_depth + 1);
837         }
838       } else {
839         const uint8_t fixed_skip_size = fixed_form_sizes.GetSize(form);
840         if (fixed_skip_size)
841           offset += fixed_skip_size;
842         else
843           DWARFFormValue::SkipValue(form, debug_info_data, &offset, cu);
844       }
845     }
846   } else {
847     attributes.Clear();
848   }
849   return attributes.Size();
850 }
851 
852 //----------------------------------------------------------------------
853 // GetAttributeValue
854 //
855 // Get the value of an attribute and return the .debug_info offset of the
856 // attribute if it was properly extracted into form_value, or zero if we fail
857 // since an offset of zero is invalid for an attribute (it would be a compile
858 // unit header).
859 //----------------------------------------------------------------------
860 dw_offset_t DWARFDebugInfoEntry::GetAttributeValue(
861     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
862     const dw_attr_t attr, DWARFFormValue &form_value,
863     dw_offset_t *end_attr_offset_ptr,
864     bool check_specification_or_abstract_origin) const {
865   SymbolFileDWARFDwo *dwo_symbol_file = cu->GetDwoSymbolFile();
866   if (dwo_symbol_file && m_tag != DW_TAG_compile_unit &&
867                          m_tag != DW_TAG_partial_unit)
868     return GetAttributeValue(dwo_symbol_file, dwo_symbol_file->GetCompileUnit(),
869                              attr, form_value, end_attr_offset_ptr,
870                              check_specification_or_abstract_origin);
871 
872   lldb::offset_t offset;
873   const DWARFAbbreviationDeclaration *abbrevDecl =
874       GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset);
875 
876   if (abbrevDecl) {
877     uint32_t attr_idx = abbrevDecl->FindAttributeIndex(attr);
878 
879     if (attr_idx != DW_INVALID_INDEX) {
880       const DWARFDataExtractor &debug_info_data = cu->GetData();
881 
882       uint32_t idx = 0;
883       while (idx < attr_idx)
884         DWARFFormValue::SkipValue(abbrevDecl->GetFormByIndex(idx++),
885                                   debug_info_data, &offset, cu);
886 
887       const dw_offset_t attr_offset = offset;
888       form_value.SetCompileUnit(cu);
889       form_value.SetForm(abbrevDecl->GetFormByIndex(idx));
890       if (form_value.ExtractValue(debug_info_data, &offset)) {
891         if (end_attr_offset_ptr)
892           *end_attr_offset_ptr = offset;
893         return attr_offset;
894       }
895     }
896   }
897 
898   if (check_specification_or_abstract_origin) {
899     if (GetAttributeValue(dwarf2Data, cu, DW_AT_specification, form_value)) {
900       DWARFDIE die =
901           const_cast<DWARFUnit *>(cu)->GetDIE(form_value.Reference());
902       if (die) {
903         dw_offset_t die_offset = die.GetDIE()->GetAttributeValue(
904             die.GetDWARF(), die.GetCU(), attr, form_value, end_attr_offset_ptr,
905             false);
906         if (die_offset)
907           return die_offset;
908       }
909     }
910 
911     if (GetAttributeValue(dwarf2Data, cu, DW_AT_abstract_origin, form_value)) {
912       DWARFDIE die =
913           const_cast<DWARFUnit *>(cu)->GetDIE(form_value.Reference());
914       if (die) {
915         dw_offset_t die_offset = die.GetDIE()->GetAttributeValue(
916             die.GetDWARF(), die.GetCU(), attr, form_value, end_attr_offset_ptr,
917             false);
918         if (die_offset)
919           return die_offset;
920       }
921     }
922   }
923 
924   if (!dwo_symbol_file)
925     return 0;
926 
927   DWARFUnit *dwo_cu = dwo_symbol_file->GetCompileUnit();
928   if (!dwo_cu)
929     return 0;
930 
931   DWARFBaseDIE dwo_cu_die = dwo_cu->GetUnitDIEOnly();
932   if (!dwo_cu_die.IsValid())
933     return 0;
934 
935   return dwo_cu_die.GetDIE()->GetAttributeValue(
936       dwo_symbol_file, dwo_cu, attr, form_value, end_attr_offset_ptr,
937       check_specification_or_abstract_origin);
938 }
939 
940 //----------------------------------------------------------------------
941 // GetAttributeValueAsString
942 //
943 // Get the value of an attribute as a string return it. The resulting pointer
944 // to the string data exists within the supplied SymbolFileDWARF and will only
945 // be available as long as the SymbolFileDWARF is still around and it's content
946 // doesn't change.
947 //----------------------------------------------------------------------
948 const char *DWARFDebugInfoEntry::GetAttributeValueAsString(
949     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
950     const dw_attr_t attr, const char *fail_value,
951     bool check_specification_or_abstract_origin) const {
952   DWARFFormValue form_value;
953   if (GetAttributeValue(dwarf2Data, cu, attr, form_value, nullptr,
954                         check_specification_or_abstract_origin))
955     return form_value.AsCString();
956   return fail_value;
957 }
958 
959 //----------------------------------------------------------------------
960 // GetAttributeValueAsUnsigned
961 //
962 // Get the value of an attribute as unsigned and return it.
963 //----------------------------------------------------------------------
964 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsUnsigned(
965     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
966     const dw_attr_t attr, uint64_t fail_value,
967     bool check_specification_or_abstract_origin) const {
968   DWARFFormValue form_value;
969   if (GetAttributeValue(dwarf2Data, cu, attr, form_value, nullptr,
970                         check_specification_or_abstract_origin))
971     return form_value.Unsigned();
972   return fail_value;
973 }
974 
975 //----------------------------------------------------------------------
976 // GetAttributeValueAsSigned
977 //
978 // Get the value of an attribute a signed value and return it.
979 //----------------------------------------------------------------------
980 int64_t DWARFDebugInfoEntry::GetAttributeValueAsSigned(
981     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
982     const dw_attr_t attr, int64_t fail_value,
983     bool check_specification_or_abstract_origin) const {
984   DWARFFormValue form_value;
985   if (GetAttributeValue(dwarf2Data, cu, attr, form_value, nullptr,
986                         check_specification_or_abstract_origin))
987     return form_value.Signed();
988   return fail_value;
989 }
990 
991 //----------------------------------------------------------------------
992 // GetAttributeValueAsReference
993 //
994 // Get the value of an attribute as reference and fix up and compile unit
995 // relative offsets as needed.
996 //----------------------------------------------------------------------
997 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsReference(
998     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
999     const dw_attr_t attr, uint64_t fail_value,
1000     bool check_specification_or_abstract_origin) const {
1001   DWARFFormValue form_value;
1002   if (GetAttributeValue(dwarf2Data, cu, attr, form_value, nullptr,
1003                         check_specification_or_abstract_origin))
1004     return form_value.Reference();
1005   return fail_value;
1006 }
1007 
1008 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsAddress(
1009     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
1010     const dw_attr_t attr, uint64_t fail_value,
1011     bool check_specification_or_abstract_origin) const {
1012   DWARFFormValue form_value;
1013   if (GetAttributeValue(dwarf2Data, cu, attr, form_value, nullptr,
1014                         check_specification_or_abstract_origin))
1015     return form_value.Address();
1016   return fail_value;
1017 }
1018 
1019 //----------------------------------------------------------------------
1020 // GetAttributeHighPC
1021 //
1022 // Get the hi_pc, adding hi_pc to lo_pc when specified as an <offset-from-low-
1023 // pc>.
1024 //
1025 // Returns the hi_pc or fail_value.
1026 //----------------------------------------------------------------------
1027 dw_addr_t DWARFDebugInfoEntry::GetAttributeHighPC(
1028     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu, dw_addr_t lo_pc,
1029     uint64_t fail_value, bool check_specification_or_abstract_origin) const {
1030   DWARFFormValue form_value;
1031   if (GetAttributeValue(dwarf2Data, cu, DW_AT_high_pc, form_value, nullptr,
1032                         check_specification_or_abstract_origin)) {
1033     dw_form_t form = form_value.Form();
1034     if (form == DW_FORM_addr || form == DW_FORM_GNU_addr_index)
1035       return form_value.Address();
1036 
1037     // DWARF4 can specify the hi_pc as an <offset-from-lowpc>
1038     return lo_pc + form_value.Unsigned();
1039   }
1040   return fail_value;
1041 }
1042 
1043 //----------------------------------------------------------------------
1044 // GetAttributeAddressRange
1045 //
1046 // Get the lo_pc and hi_pc, adding hi_pc to lo_pc when specified as an <offset-
1047 // from-low-pc>.
1048 //
1049 // Returns true or sets lo_pc and hi_pc to fail_value.
1050 //----------------------------------------------------------------------
1051 bool DWARFDebugInfoEntry::GetAttributeAddressRange(
1052     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu, dw_addr_t &lo_pc,
1053     dw_addr_t &hi_pc, uint64_t fail_value,
1054     bool check_specification_or_abstract_origin) const {
1055   lo_pc = GetAttributeValueAsAddress(dwarf2Data, cu, DW_AT_low_pc, fail_value,
1056                                      check_specification_or_abstract_origin);
1057   if (lo_pc != fail_value) {
1058     hi_pc = GetAttributeHighPC(dwarf2Data, cu, lo_pc, fail_value,
1059                                check_specification_or_abstract_origin);
1060     if (hi_pc != fail_value)
1061       return true;
1062   }
1063   lo_pc = fail_value;
1064   hi_pc = fail_value;
1065   return false;
1066 }
1067 
1068 size_t DWARFDebugInfoEntry::GetAttributeAddressRanges(
1069     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
1070     DWARFRangeList &ranges, bool check_hi_lo_pc,
1071     bool check_specification_or_abstract_origin) const {
1072   ranges.Clear();
1073 
1074   DWARFFormValue form_value;
1075   if (GetAttributeValue(dwarf2Data, cu, DW_AT_ranges, form_value)) {
1076     if (DWARFDebugRangesBase *debug_ranges = dwarf2Data->DebugRanges())
1077       debug_ranges->FindRanges(cu, GetRangesOffset(debug_ranges, form_value),
1078                                ranges);
1079   } else if (check_hi_lo_pc) {
1080     dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
1081     dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
1082     if (GetAttributeAddressRange(dwarf2Data, cu, lo_pc, hi_pc,
1083                                  LLDB_INVALID_ADDRESS,
1084                                  check_specification_or_abstract_origin)) {
1085       if (lo_pc < hi_pc)
1086         ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc));
1087     }
1088   }
1089   return ranges.GetSize();
1090 }
1091 
1092 //----------------------------------------------------------------------
1093 // GetName
1094 //
1095 // Get value of the DW_AT_name attribute and return it if one exists, else
1096 // return NULL.
1097 //----------------------------------------------------------------------
1098 const char *DWARFDebugInfoEntry::GetName(SymbolFileDWARF *dwarf2Data,
1099                                          const DWARFUnit *cu) const {
1100   return GetAttributeValueAsString(dwarf2Data, cu, DW_AT_name, nullptr, true);
1101 }
1102 
1103 //----------------------------------------------------------------------
1104 // GetMangledName
1105 //
1106 // Get value of the DW_AT_MIPS_linkage_name attribute and return it if one
1107 // exists, else return the value of the DW_AT_name attribute
1108 //----------------------------------------------------------------------
1109 const char *
1110 DWARFDebugInfoEntry::GetMangledName(SymbolFileDWARF *dwarf2Data,
1111                                     const DWARFUnit *cu,
1112                                     bool substitute_name_allowed) const {
1113   const char *name = nullptr;
1114 
1115   name = GetAttributeValueAsString(dwarf2Data, cu, DW_AT_MIPS_linkage_name,
1116                                    nullptr, true);
1117   if (name)
1118     return name;
1119 
1120   name = GetAttributeValueAsString(dwarf2Data, cu, DW_AT_linkage_name, nullptr,
1121                                    true);
1122   if (name)
1123     return name;
1124 
1125   if (!substitute_name_allowed)
1126     return nullptr;
1127 
1128   name = GetAttributeValueAsString(dwarf2Data, cu, DW_AT_name, nullptr, true);
1129   return name;
1130 }
1131 
1132 //----------------------------------------------------------------------
1133 // GetPubname
1134 //
1135 // Get value the name for a DIE as it should appear for a .debug_pubnames or
1136 // .debug_pubtypes section.
1137 //----------------------------------------------------------------------
1138 const char *DWARFDebugInfoEntry::GetPubname(SymbolFileDWARF *dwarf2Data,
1139                                             const DWARFUnit *cu) const {
1140   const char *name = nullptr;
1141   if (!dwarf2Data)
1142     return name;
1143 
1144   name = GetAttributeValueAsString(dwarf2Data, cu, DW_AT_MIPS_linkage_name,
1145                                    nullptr, true);
1146   if (name)
1147     return name;
1148 
1149   name = GetAttributeValueAsString(dwarf2Data, cu, DW_AT_linkage_name, nullptr,
1150                                    true);
1151   if (name)
1152     return name;
1153 
1154   name = GetAttributeValueAsString(dwarf2Data, cu, DW_AT_name, nullptr, true);
1155   return name;
1156 }
1157 
1158 //----------------------------------------------------------------------
1159 // GetName
1160 //
1161 // Get value of the DW_AT_name attribute for a debug information entry that
1162 // exists at offset "die_offset" and place that value into the supplied stream
1163 // object. If the DIE is a NULL object "NULL" is placed into the stream, and if
1164 // no DW_AT_name attribute exists for the DIE then nothing is printed.
1165 //----------------------------------------------------------------------
1166 bool DWARFDebugInfoEntry::GetName(SymbolFileDWARF *dwarf2Data,
1167                                   const DWARFUnit *cu,
1168                                   const dw_offset_t die_offset, Stream &s) {
1169   if (dwarf2Data == NULL) {
1170     s.PutCString("NULL");
1171     return false;
1172   }
1173 
1174   DWARFDebugInfoEntry die;
1175   lldb::offset_t offset = die_offset;
1176   if (die.Extract(dwarf2Data, cu, &offset)) {
1177     if (die.IsNULL()) {
1178       s.PutCString("NULL");
1179       return true;
1180     } else {
1181       const char *name = die.GetAttributeValueAsString(
1182           dwarf2Data, cu, DW_AT_name, nullptr, true);
1183       if (name) {
1184         s.PutCString(name);
1185         return true;
1186       }
1187     }
1188   }
1189   return false;
1190 }
1191 
1192 //----------------------------------------------------------------------
1193 // AppendTypeName
1194 //
1195 // Follows the type name definition down through all needed tags to end up with
1196 // a fully qualified type name and dump the results to the supplied stream.
1197 // This is used to show the name of types given a type identifier.
1198 //----------------------------------------------------------------------
1199 bool DWARFDebugInfoEntry::AppendTypeName(SymbolFileDWARF *dwarf2Data,
1200                                          const DWARFUnit *cu,
1201                                          const dw_offset_t die_offset,
1202                                          Stream &s) {
1203   if (dwarf2Data == NULL) {
1204     s.PutCString("NULL");
1205     return false;
1206   }
1207 
1208   DWARFDebugInfoEntry die;
1209   lldb::offset_t offset = die_offset;
1210   if (die.Extract(dwarf2Data, cu, &offset)) {
1211     if (die.IsNULL()) {
1212       s.PutCString("NULL");
1213       return true;
1214     } else {
1215       const char *name = die.GetPubname(dwarf2Data, cu);
1216       if (name)
1217         s.PutCString(name);
1218       else {
1219         bool result = true;
1220         const DWARFAbbreviationDeclaration *abbrevDecl =
1221             die.GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset);
1222 
1223         if (abbrevDecl == NULL)
1224           return false;
1225 
1226         switch (abbrevDecl->Tag()) {
1227         case DW_TAG_array_type:
1228           break; // print out a "[]" after printing the full type of the element
1229                  // below
1230         case DW_TAG_base_type:
1231           s.PutCString("base ");
1232           break;
1233         case DW_TAG_class_type:
1234           s.PutCString("class ");
1235           break;
1236         case DW_TAG_const_type:
1237           s.PutCString("const ");
1238           break;
1239         case DW_TAG_enumeration_type:
1240           s.PutCString("enum ");
1241           break;
1242         case DW_TAG_file_type:
1243           s.PutCString("file ");
1244           break;
1245         case DW_TAG_interface_type:
1246           s.PutCString("interface ");
1247           break;
1248         case DW_TAG_packed_type:
1249           s.PutCString("packed ");
1250           break;
1251         case DW_TAG_pointer_type:
1252           break; // print out a '*' after printing the full type below
1253         case DW_TAG_ptr_to_member_type:
1254           break; // print out a '*' after printing the full type below
1255         case DW_TAG_reference_type:
1256           break; // print out a '&' after printing the full type below
1257         case DW_TAG_restrict_type:
1258           s.PutCString("restrict ");
1259           break;
1260         case DW_TAG_set_type:
1261           s.PutCString("set ");
1262           break;
1263         case DW_TAG_shared_type:
1264           s.PutCString("shared ");
1265           break;
1266         case DW_TAG_string_type:
1267           s.PutCString("string ");
1268           break;
1269         case DW_TAG_structure_type:
1270           s.PutCString("struct ");
1271           break;
1272         case DW_TAG_subrange_type:
1273           s.PutCString("subrange ");
1274           break;
1275         case DW_TAG_subroutine_type:
1276           s.PutCString("function ");
1277           break;
1278         case DW_TAG_thrown_type:
1279           s.PutCString("thrown ");
1280           break;
1281         case DW_TAG_union_type:
1282           s.PutCString("union ");
1283           break;
1284         case DW_TAG_unspecified_type:
1285           s.PutCString("unspecified ");
1286           break;
1287         case DW_TAG_volatile_type:
1288           s.PutCString("volatile ");
1289           break;
1290         default:
1291           return false;
1292         }
1293 
1294         // Follow the DW_AT_type if possible
1295         DWARFFormValue form_value;
1296         if (die.GetAttributeValue(dwarf2Data, cu, DW_AT_type, form_value)) {
1297           uint64_t next_die_offset = form_value.Reference();
1298           result = AppendTypeName(dwarf2Data, cu, next_die_offset, s);
1299         }
1300 
1301         switch (abbrevDecl->Tag()) {
1302         case DW_TAG_array_type:
1303           s.PutCString("[]");
1304           break;
1305         case DW_TAG_pointer_type:
1306           s.PutChar('*');
1307           break;
1308         case DW_TAG_ptr_to_member_type:
1309           s.PutChar('*');
1310           break;
1311         case DW_TAG_reference_type:
1312           s.PutChar('&');
1313           break;
1314         default:
1315           break;
1316         }
1317         return result;
1318       }
1319     }
1320   }
1321   return false;
1322 }
1323 
1324 //----------------------------------------------------------------------
1325 // BuildAddressRangeTable
1326 //----------------------------------------------------------------------
1327 void DWARFDebugInfoEntry::BuildAddressRangeTable(
1328     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
1329     DWARFDebugAranges *debug_aranges) const {
1330   if (m_tag) {
1331     if (m_tag == DW_TAG_subprogram) {
1332       dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
1333       dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
1334       if (GetAttributeAddressRange(dwarf2Data, cu, lo_pc, hi_pc,
1335                                    LLDB_INVALID_ADDRESS)) {
1336         /// printf("BuildAddressRangeTable() 0x%8.8x: %30s: [0x%8.8x -
1337         /// 0x%8.8x)\n", m_offset, DW_TAG_value_to_name(tag), lo_pc, hi_pc);
1338         debug_aranges->AppendRange(cu->GetOffset(), lo_pc, hi_pc);
1339       }
1340     }
1341 
1342     const DWARFDebugInfoEntry *child = GetFirstChild();
1343     while (child) {
1344       child->BuildAddressRangeTable(dwarf2Data, cu, debug_aranges);
1345       child = child->GetSibling();
1346     }
1347   }
1348 }
1349 
1350 //----------------------------------------------------------------------
1351 // BuildFunctionAddressRangeTable
1352 //
1353 // This function is very similar to the BuildAddressRangeTable function except
1354 // that the actual DIE offset for the function is placed in the table instead
1355 // of the compile unit offset (which is the way the standard .debug_aranges
1356 // section does it).
1357 //----------------------------------------------------------------------
1358 void DWARFDebugInfoEntry::BuildFunctionAddressRangeTable(
1359     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
1360     DWARFDebugAranges *debug_aranges) const {
1361   if (m_tag) {
1362     if (m_tag == DW_TAG_subprogram) {
1363       dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
1364       dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
1365       if (GetAttributeAddressRange(dwarf2Data, cu, lo_pc, hi_pc,
1366                                    LLDB_INVALID_ADDRESS)) {
1367         //  printf("BuildAddressRangeTable() 0x%8.8x: [0x%16.16" PRIx64 " -
1368         //  0x%16.16" PRIx64 ")\n", m_offset, lo_pc, hi_pc); // DEBUG ONLY
1369         debug_aranges->AppendRange(GetOffset(), lo_pc, hi_pc);
1370       }
1371     }
1372 
1373     const DWARFDebugInfoEntry *child = GetFirstChild();
1374     while (child) {
1375       child->BuildFunctionAddressRangeTable(dwarf2Data, cu, debug_aranges);
1376       child = child->GetSibling();
1377     }
1378   }
1379 }
1380 
1381 void DWARFDebugInfoEntry::GetDeclContextDIEs(
1382     DWARFUnit *cu, DWARFDIECollection &decl_context_dies) const {
1383 
1384   DWARFDIE die(cu, const_cast<DWARFDebugInfoEntry *>(this));
1385   die.GetDeclContextDIEs(decl_context_dies);
1386 }
1387 
1388 void DWARFDebugInfoEntry::GetDWARFDeclContext(
1389     SymbolFileDWARF *dwarf2Data, DWARFUnit *cu,
1390     DWARFDeclContext &dwarf_decl_ctx) const {
1391   const dw_tag_t tag = Tag();
1392   if (tag != DW_TAG_compile_unit && tag != DW_TAG_partial_unit) {
1393     dwarf_decl_ctx.AppendDeclContext(tag, GetName(dwarf2Data, cu));
1394     DWARFDIE parent_decl_ctx_die = GetParentDeclContextDIE(dwarf2Data, cu);
1395     if (parent_decl_ctx_die && parent_decl_ctx_die.GetDIE() != this) {
1396       if (parent_decl_ctx_die.Tag() != DW_TAG_compile_unit &&
1397           parent_decl_ctx_die.Tag() != DW_TAG_partial_unit)
1398         parent_decl_ctx_die.GetDIE()->GetDWARFDeclContext(
1399             parent_decl_ctx_die.GetDWARF(), parent_decl_ctx_die.GetCU(),
1400             dwarf_decl_ctx);
1401     }
1402   }
1403 }
1404 
1405 bool DWARFDebugInfoEntry::MatchesDWARFDeclContext(
1406     SymbolFileDWARF *dwarf2Data, DWARFUnit *cu,
1407     const DWARFDeclContext &dwarf_decl_ctx) const {
1408 
1409   DWARFDeclContext this_dwarf_decl_ctx;
1410   GetDWARFDeclContext(dwarf2Data, cu, this_dwarf_decl_ctx);
1411   return this_dwarf_decl_ctx == dwarf_decl_ctx;
1412 }
1413 
1414 DWARFDIE
1415 DWARFDebugInfoEntry::GetParentDeclContextDIE(SymbolFileDWARF *dwarf2Data,
1416                                              DWARFUnit *cu) const {
1417   DWARFAttributes attributes;
1418   GetAttributes(cu, DWARFFormValue::FixedFormSizes(), attributes);
1419   return GetParentDeclContextDIE(dwarf2Data, cu, attributes);
1420 }
1421 
1422 DWARFDIE
1423 DWARFDebugInfoEntry::GetParentDeclContextDIE(
1424     SymbolFileDWARF *dwarf2Data, DWARFUnit *cu,
1425     const DWARFAttributes &attributes) const {
1426   DWARFDIE die(cu, const_cast<DWARFDebugInfoEntry *>(this));
1427 
1428   while (die) {
1429     // If this is the original DIE that we are searching for a declaration for,
1430     // then don't look in the cache as we don't want our own decl context to be
1431     // our decl context...
1432     if (die.GetDIE() != this) {
1433       switch (die.Tag()) {
1434       case DW_TAG_compile_unit:
1435       case DW_TAG_partial_unit:
1436       case DW_TAG_namespace:
1437       case DW_TAG_structure_type:
1438       case DW_TAG_union_type:
1439       case DW_TAG_class_type:
1440         return die;
1441 
1442       default:
1443         break;
1444       }
1445     }
1446 
1447     dw_offset_t die_offset;
1448 
1449     die_offset =
1450         attributes.FormValueAsUnsigned(DW_AT_specification, DW_INVALID_OFFSET);
1451     if (die_offset != DW_INVALID_OFFSET) {
1452       DWARFDIE spec_die = cu->GetDIE(die_offset);
1453       if (spec_die) {
1454         DWARFDIE decl_ctx_die = spec_die.GetParentDeclContextDIE();
1455         if (decl_ctx_die)
1456           return decl_ctx_die;
1457       }
1458     }
1459 
1460     die_offset = attributes.FormValueAsUnsigned(DW_AT_abstract_origin,
1461                                                 DW_INVALID_OFFSET);
1462     if (die_offset != DW_INVALID_OFFSET) {
1463       DWARFDIE abs_die = cu->GetDIE(die_offset);
1464       if (abs_die) {
1465         DWARFDIE decl_ctx_die = abs_die.GetParentDeclContextDIE();
1466         if (decl_ctx_die)
1467           return decl_ctx_die;
1468       }
1469     }
1470 
1471     die = die.GetParent();
1472   }
1473   return DWARFDIE();
1474 }
1475 
1476 const char *DWARFDebugInfoEntry::GetQualifiedName(SymbolFileDWARF *dwarf2Data,
1477                                                   DWARFUnit *cu,
1478                                                   std::string &storage) const {
1479   DWARFAttributes attributes;
1480   GetAttributes(cu, DWARFFormValue::FixedFormSizes(), attributes);
1481   return GetQualifiedName(dwarf2Data, cu, attributes, storage);
1482 }
1483 
1484 const char *DWARFDebugInfoEntry::GetQualifiedName(
1485     SymbolFileDWARF *dwarf2Data, DWARFUnit *cu,
1486     const DWARFAttributes &attributes, std::string &storage) const {
1487 
1488   const char *name = GetName(dwarf2Data, cu);
1489 
1490   if (name) {
1491     DWARFDIE parent_decl_ctx_die = GetParentDeclContextDIE(dwarf2Data, cu);
1492     storage.clear();
1493     // TODO: change this to get the correct decl context parent....
1494     while (parent_decl_ctx_die) {
1495       const dw_tag_t parent_tag = parent_decl_ctx_die.Tag();
1496       switch (parent_tag) {
1497       case DW_TAG_namespace: {
1498         const char *namespace_name = parent_decl_ctx_die.GetName();
1499         if (namespace_name) {
1500           storage.insert(0, "::");
1501           storage.insert(0, namespace_name);
1502         } else {
1503           storage.insert(0, "(anonymous namespace)::");
1504         }
1505         parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE();
1506       } break;
1507 
1508       case DW_TAG_class_type:
1509       case DW_TAG_structure_type:
1510       case DW_TAG_union_type: {
1511         const char *class_union_struct_name = parent_decl_ctx_die.GetName();
1512 
1513         if (class_union_struct_name) {
1514           storage.insert(0, "::");
1515           storage.insert(0, class_union_struct_name);
1516         }
1517         parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE();
1518       } break;
1519 
1520       default:
1521         parent_decl_ctx_die.Clear();
1522         break;
1523       }
1524     }
1525 
1526     if (storage.empty())
1527       storage.append("::");
1528 
1529     storage.append(name);
1530   }
1531   if (storage.empty())
1532     return NULL;
1533   return storage.c_str();
1534 }
1535 
1536 //----------------------------------------------------------------------
1537 // LookupAddress
1538 //----------------------------------------------------------------------
1539 bool DWARFDebugInfoEntry::LookupAddress(const dw_addr_t address,
1540                                         SymbolFileDWARF *dwarf2Data,
1541                                         const DWARFUnit *cu,
1542                                         DWARFDebugInfoEntry **function_die,
1543                                         DWARFDebugInfoEntry **block_die) {
1544   bool found_address = false;
1545   if (m_tag) {
1546     bool check_children = false;
1547     bool match_addr_range = false;
1548     //  printf("0x%8.8x: %30s: address = 0x%8.8x - ", m_offset,
1549     //  DW_TAG_value_to_name(tag), address);
1550     switch (m_tag) {
1551     case DW_TAG_array_type:
1552       break;
1553     case DW_TAG_class_type:
1554       check_children = true;
1555       break;
1556     case DW_TAG_entry_point:
1557       break;
1558     case DW_TAG_enumeration_type:
1559       break;
1560     case DW_TAG_formal_parameter:
1561       break;
1562     case DW_TAG_imported_declaration:
1563       break;
1564     case DW_TAG_label:
1565       break;
1566     case DW_TAG_lexical_block:
1567       check_children = true;
1568       match_addr_range = true;
1569       break;
1570     case DW_TAG_member:
1571       break;
1572     case DW_TAG_pointer_type:
1573       break;
1574     case DW_TAG_reference_type:
1575       break;
1576     case DW_TAG_compile_unit:
1577       match_addr_range = true;
1578       break;
1579     case DW_TAG_string_type:
1580       break;
1581     case DW_TAG_structure_type:
1582       check_children = true;
1583       break;
1584     case DW_TAG_subroutine_type:
1585       break;
1586     case DW_TAG_typedef:
1587       break;
1588     case DW_TAG_union_type:
1589       break;
1590     case DW_TAG_unspecified_parameters:
1591       break;
1592     case DW_TAG_variant:
1593       break;
1594     case DW_TAG_common_block:
1595       check_children = true;
1596       break;
1597     case DW_TAG_common_inclusion:
1598       break;
1599     case DW_TAG_inheritance:
1600       break;
1601     case DW_TAG_inlined_subroutine:
1602       check_children = true;
1603       match_addr_range = true;
1604       break;
1605     case DW_TAG_module:
1606       match_addr_range = true;
1607       break;
1608     case DW_TAG_ptr_to_member_type:
1609       break;
1610     case DW_TAG_set_type:
1611       break;
1612     case DW_TAG_subrange_type:
1613       break;
1614     case DW_TAG_with_stmt:
1615       break;
1616     case DW_TAG_access_declaration:
1617       break;
1618     case DW_TAG_base_type:
1619       break;
1620     case DW_TAG_catch_block:
1621       match_addr_range = true;
1622       break;
1623     case DW_TAG_const_type:
1624       break;
1625     case DW_TAG_constant:
1626       break;
1627     case DW_TAG_enumerator:
1628       break;
1629     case DW_TAG_file_type:
1630       break;
1631     case DW_TAG_friend:
1632       break;
1633     case DW_TAG_namelist:
1634       break;
1635     case DW_TAG_namelist_item:
1636       break;
1637     case DW_TAG_packed_type:
1638       break;
1639     case DW_TAG_subprogram:
1640       match_addr_range = true;
1641       break;
1642     case DW_TAG_template_type_parameter:
1643       break;
1644     case DW_TAG_template_value_parameter:
1645       break;
1646     case DW_TAG_GNU_template_parameter_pack:
1647       break;
1648     case DW_TAG_thrown_type:
1649       break;
1650     case DW_TAG_try_block:
1651       match_addr_range = true;
1652       break;
1653     case DW_TAG_variant_part:
1654       break;
1655     case DW_TAG_variable:
1656       break;
1657     case DW_TAG_volatile_type:
1658       break;
1659     case DW_TAG_dwarf_procedure:
1660       break;
1661     case DW_TAG_restrict_type:
1662       break;
1663     case DW_TAG_interface_type:
1664       break;
1665     case DW_TAG_namespace:
1666       check_children = true;
1667       break;
1668     case DW_TAG_imported_module:
1669       break;
1670     case DW_TAG_unspecified_type:
1671       break;
1672     case DW_TAG_partial_unit:
1673       match_addr_range = true;
1674       break;
1675     case DW_TAG_imported_unit:
1676       break;
1677     case DW_TAG_shared_type:
1678       break;
1679     default:
1680       break;
1681     }
1682 
1683     if (match_addr_range) {
1684       dw_addr_t lo_pc = GetAttributeValueAsAddress(dwarf2Data, cu, DW_AT_low_pc,
1685                                                    LLDB_INVALID_ADDRESS);
1686       if (lo_pc != LLDB_INVALID_ADDRESS) {
1687         dw_addr_t hi_pc =
1688             GetAttributeHighPC(dwarf2Data, cu, lo_pc, LLDB_INVALID_ADDRESS);
1689         if (hi_pc != LLDB_INVALID_ADDRESS) {
1690           //  printf("\n0x%8.8x: %30s: address = 0x%8.8x  [0x%8.8x - 0x%8.8x) ",
1691           //  m_offset, DW_TAG_value_to_name(tag), address, lo_pc, hi_pc);
1692           if ((lo_pc <= address) && (address < hi_pc)) {
1693             found_address = true;
1694             //  puts("***MATCH***");
1695             switch (m_tag) {
1696             case DW_TAG_compile_unit: // File
1697             case DW_TAG_partial_unit: // File
1698               check_children = ((function_die != NULL) || (block_die != NULL));
1699               break;
1700 
1701             case DW_TAG_subprogram: // Function
1702               if (function_die)
1703                 *function_die = this;
1704               check_children = (block_die != NULL);
1705               break;
1706 
1707             case DW_TAG_inlined_subroutine: // Inlined Function
1708             case DW_TAG_lexical_block:      // Block { } in code
1709               if (block_die) {
1710                 *block_die = this;
1711                 check_children = true;
1712               }
1713               break;
1714 
1715             default:
1716               check_children = true;
1717               break;
1718             }
1719           }
1720         } else {
1721           // Compile units may not have a valid high/low pc when there
1722           // are address gaps in subroutines so we must always search
1723           // if there is no valid high and low PC.
1724           check_children = (m_tag == DW_TAG_compile_unit ||
1725                             m_tag == DW_TAG_partial_unit) &&
1726                            ((function_die != NULL) || (block_die != NULL));
1727         }
1728       } else {
1729         DWARFFormValue form_value;
1730         if (GetAttributeValue(dwarf2Data, cu, DW_AT_ranges, form_value)) {
1731           DWARFRangeList ranges;
1732           DWARFDebugRangesBase *debug_ranges = dwarf2Data->DebugRanges();
1733           debug_ranges->FindRanges(
1734               cu, GetRangesOffset(debug_ranges, form_value), ranges);
1735 
1736           if (ranges.FindEntryThatContains(address)) {
1737             found_address = true;
1738             //  puts("***MATCH***");
1739             switch (m_tag) {
1740             case DW_TAG_compile_unit: // File
1741             case DW_TAG_partial_unit: // File
1742               check_children = ((function_die != NULL) || (block_die != NULL));
1743               break;
1744 
1745             case DW_TAG_subprogram: // Function
1746               if (function_die)
1747                 *function_die = this;
1748               check_children = (block_die != NULL);
1749               break;
1750 
1751             case DW_TAG_inlined_subroutine: // Inlined Function
1752             case DW_TAG_lexical_block:      // Block { } in code
1753               if (block_die) {
1754                 *block_die = this;
1755                 check_children = true;
1756               }
1757               break;
1758 
1759             default:
1760               check_children = true;
1761               break;
1762             }
1763           } else {
1764             check_children = false;
1765           }
1766         }
1767       }
1768     }
1769 
1770     if (check_children) {
1771       //  printf("checking children\n");
1772       DWARFDebugInfoEntry *child = GetFirstChild();
1773       while (child) {
1774         if (child->LookupAddress(address, dwarf2Data, cu, function_die,
1775                                  block_die))
1776           return true;
1777         child = child->GetSibling();
1778       }
1779     }
1780   }
1781   return found_address;
1782 }
1783 
1784 const DWARFAbbreviationDeclaration *
1785 DWARFDebugInfoEntry::GetAbbreviationDeclarationPtr(
1786     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
1787     lldb::offset_t &offset) const {
1788   if (dwarf2Data) {
1789     offset = GetOffset();
1790 
1791     const DWARFAbbreviationDeclarationSet *abbrev_set = cu->GetAbbreviations();
1792     if (abbrev_set) {
1793       const DWARFAbbreviationDeclaration *abbrev_decl =
1794           abbrev_set->GetAbbreviationDeclaration(m_abbr_idx);
1795       if (abbrev_decl) {
1796         // Make sure the abbreviation code still matches. If it doesn't and the
1797         // DWARF data was mmap'ed, the backing file might have been modified
1798         // which is bad news.
1799         const uint64_t abbrev_code = cu->GetData().GetULEB128(&offset);
1800 
1801         if (abbrev_decl->Code() == abbrev_code)
1802           return abbrev_decl;
1803 
1804         dwarf2Data->GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
1805             "0x%8.8x: the DWARF debug information has been modified (abbrev "
1806             "code was %u, and is now %u)",
1807             GetOffset(), (uint32_t)abbrev_decl->Code(), (uint32_t)abbrev_code);
1808       }
1809     }
1810   }
1811   offset = DW_INVALID_OFFSET;
1812   return NULL;
1813 }
1814 
1815 bool DWARFDebugInfoEntry::OffsetLessThan(const DWARFDebugInfoEntry &a,
1816                                          const DWARFDebugInfoEntry &b) {
1817   return a.GetOffset() < b.GetOffset();
1818 }
1819 
1820 void DWARFDebugInfoEntry::DumpDIECollection(
1821     Stream &strm, DWARFDebugInfoEntry::collection &die_collection) {
1822   DWARFDebugInfoEntry::const_iterator pos;
1823   DWARFDebugInfoEntry::const_iterator end = die_collection.end();
1824   strm.PutCString("\noffset    parent   sibling  child\n");
1825   strm.PutCString("--------  -------- -------- --------\n");
1826   for (pos = die_collection.begin(); pos != end; ++pos) {
1827     const DWARFDebugInfoEntry &die_ref = *pos;
1828     const DWARFDebugInfoEntry *p = die_ref.GetParent();
1829     const DWARFDebugInfoEntry *s = die_ref.GetSibling();
1830     const DWARFDebugInfoEntry *c = die_ref.GetFirstChild();
1831     strm.Printf("%.8x: %.8x %.8x %.8x 0x%4.4x %s%s\n", die_ref.GetOffset(),
1832                 p ? p->GetOffset() : 0, s ? s->GetOffset() : 0,
1833                 c ? c->GetOffset() : 0, die_ref.Tag(),
1834                 DW_TAG_value_to_name(die_ref.Tag()),
1835                 die_ref.HasChildren() ? " *" : "");
1836   }
1837 }
1838 
1839 bool DWARFDebugInfoEntry::operator==(const DWARFDebugInfoEntry &rhs) const {
1840   return m_offset == rhs.m_offset && m_parent_idx == rhs.m_parent_idx &&
1841          m_sibling_idx == rhs.m_sibling_idx &&
1842          m_abbr_idx == rhs.m_abbr_idx && m_has_children == rhs.m_has_children &&
1843          m_tag == rhs.m_tag;
1844 }
1845 
1846 bool DWARFDebugInfoEntry::operator!=(const DWARFDebugInfoEntry &rhs) const {
1847   return !(*this == rhs);
1848 }
1849