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