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 DWARFDebugRangesBase *debug_ranges = dwarf2Data->DebugRanges();
460           if (debug_ranges)
461             debug_ranges->FindRanges(cu, form_value.Unsigned(), ranges);
462           else
463             cu->GetSymbolFileDWARF()->GetObjectFile()->GetModule()->ReportError(
464                 "{0x%8.8x}: DIE has DW_AT_ranges(0x%" PRIx64
465                 ") attribute yet DWARF has no .debug_ranges, please file a bug "
466                 "and attach the file at the start of this error message",
467                 m_offset, form_value.Unsigned());
468         } break;
469 
470         case DW_AT_name:
471           if (name == NULL)
472             name = form_value.AsCString();
473           break;
474 
475         case DW_AT_MIPS_linkage_name:
476         case DW_AT_linkage_name:
477           if (mangled == NULL)
478             mangled = form_value.AsCString();
479           break;
480 
481         case DW_AT_abstract_origin:
482           die_refs.emplace_back(form_value);
483           break;
484 
485         case DW_AT_specification:
486           die_refs.emplace_back(form_value);
487           break;
488 
489         case DW_AT_decl_file:
490           if (decl_file == 0)
491             decl_file = form_value.Unsigned();
492           break;
493 
494         case DW_AT_decl_line:
495           if (decl_line == 0)
496             decl_line = form_value.Unsigned();
497           break;
498 
499         case DW_AT_decl_column:
500           if (decl_column == 0)
501             decl_column = form_value.Unsigned();
502           break;
503 
504         case DW_AT_call_file:
505           if (call_file == 0)
506             call_file = form_value.Unsigned();
507           break;
508 
509         case DW_AT_call_line:
510           if (call_line == 0)
511             call_line = form_value.Unsigned();
512           break;
513 
514         case DW_AT_call_column:
515           if (call_column == 0)
516             call_column = form_value.Unsigned();
517           break;
518 
519         case DW_AT_frame_base:
520           if (frame_base) {
521             if (form_value.BlockData()) {
522               uint32_t block_offset =
523                   form_value.BlockData() - debug_info_data.GetDataStart();
524               uint32_t block_length = form_value.Unsigned();
525               frame_base->SetOpcodeData(module, debug_info_data, block_offset,
526                                         block_length);
527             } else {
528               const DWARFDataExtractor &debug_loc_data =
529                   dwarf2Data->DebugLocData();
530               const dw_offset_t debug_loc_offset = form_value.Unsigned();
531 
532               size_t loc_list_length = DWARFExpression::LocationListSize(
533                   cu, debug_loc_data, debug_loc_offset);
534               if (loc_list_length > 0) {
535                 frame_base->SetOpcodeData(module, debug_loc_data,
536                                           debug_loc_offset, loc_list_length);
537                 if (lo_pc != LLDB_INVALID_ADDRESS) {
538                   assert(lo_pc >= cu->GetBaseAddress());
539                   frame_base->SetLocationListSlide(lo_pc -
540                                                    cu->GetBaseAddress());
541                 } else {
542                   set_frame_base_loclist_addr = true;
543                 }
544               }
545             }
546           }
547           break;
548 
549         default:
550           break;
551         }
552       }
553     }
554   }
555 
556   if (ranges.IsEmpty()) {
557     if (lo_pc != LLDB_INVALID_ADDRESS) {
558       if (hi_pc != LLDB_INVALID_ADDRESS && hi_pc > lo_pc)
559         ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc));
560       else
561         ranges.Append(DWARFRangeList::Entry(lo_pc, 0));
562     }
563   }
564 
565   if (set_frame_base_loclist_addr) {
566     dw_addr_t lowest_range_pc = ranges.GetMinRangeBase(0);
567     assert(lowest_range_pc >= cu->GetBaseAddress());
568     frame_base->SetLocationListSlide(lowest_range_pc - cu->GetBaseAddress());
569   }
570 
571   if (ranges.IsEmpty() || name == NULL || mangled == NULL) {
572     for (const DIERef &die_ref : die_refs) {
573       if (die_ref.die_offset != DW_INVALID_OFFSET) {
574         DWARFDIE die = dwarf2Data->GetDIE(die_ref);
575         if (die)
576           die.GetDIE()->GetDIENamesAndRanges(
577               die.GetDWARF(), die.GetCU(), name, mangled, ranges, decl_file,
578               decl_line, decl_column, call_file, call_line, call_column);
579       }
580     }
581   }
582   return !ranges.IsEmpty();
583 }
584 
585 //----------------------------------------------------------------------
586 // Dump
587 //
588 // Dumps a debug information entry and all of it's attributes to the specified
589 // stream.
590 //----------------------------------------------------------------------
591 void DWARFDebugInfoEntry::Dump(SymbolFileDWARF *dwarf2Data,
592                                const DWARFUnit *cu, Stream &s,
593                                uint32_t recurse_depth) const {
594   const DWARFDataExtractor &debug_info_data = cu->GetData();
595   lldb::offset_t offset = m_offset;
596 
597   if (debug_info_data.ValidOffset(offset)) {
598     dw_uleb128_t abbrCode = debug_info_data.GetULEB128(&offset);
599 
600     s.Printf("\n0x%8.8x: ", m_offset);
601     s.Indent();
602     if (abbrCode != m_abbr_idx) {
603       s.Printf("error: DWARF has been modified\n");
604     } else if (abbrCode) {
605       const DWARFAbbreviationDeclaration *abbrevDecl =
606           cu->GetAbbreviations()->GetAbbreviationDeclaration(abbrCode);
607 
608       if (abbrevDecl) {
609         s.PutCString(DW_TAG_value_to_name(abbrevDecl->Tag()));
610         s.Printf(" [%u] %c\n", abbrCode, abbrevDecl->HasChildren() ? '*' : ' ');
611 
612         // Dump all data in the .debug_info for the attributes
613         const uint32_t numAttributes = abbrevDecl->NumAttributes();
614         for (uint32_t i = 0; i < numAttributes; ++i) {
615           DWARFFormValue form_value(cu);
616           dw_attr_t attr;
617           abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value);
618 
619           DumpAttribute(dwarf2Data, cu, debug_info_data, &offset, s, attr,
620                         form_value);
621         }
622 
623         const DWARFDebugInfoEntry *child = GetFirstChild();
624         if (recurse_depth > 0 && child) {
625           s.IndentMore();
626 
627           while (child) {
628             child->Dump(dwarf2Data, cu, s, recurse_depth - 1);
629             child = child->GetSibling();
630           }
631           s.IndentLess();
632         }
633       } else
634         s.Printf("Abbreviation code note found in 'debug_abbrev' class for "
635                  "code: %u\n",
636                  abbrCode);
637     } else {
638       s.Printf("NULL\n");
639     }
640   }
641 }
642 
643 void DWARFDebugInfoEntry::DumpLocation(SymbolFileDWARF *dwarf2Data,
644                                        DWARFUnit *cu, Stream &s) const {
645   const DWARFBaseDIE cu_die = cu->GetUnitDIEOnly();
646   const char *cu_name = NULL;
647   if (cu_die)
648     cu_name = cu_die.GetName();
649   const char *obj_file_name = NULL;
650   ObjectFile *obj_file = dwarf2Data->GetObjectFile();
651   if (obj_file)
652     obj_file_name =
653         obj_file->GetFileSpec().GetFilename().AsCString("<Unknown>");
654   const char *die_name = GetName(dwarf2Data, cu);
655   s.Printf("0x%8.8x/0x%8.8x: %-30s (from %s in %s)", cu->GetOffset(),
656            GetOffset(), die_name ? die_name : "", cu_name ? cu_name : "<NULL>",
657            obj_file_name ? obj_file_name : "<NULL>");
658 }
659 
660 //----------------------------------------------------------------------
661 // DumpAttribute
662 //
663 // Dumps a debug information entry attribute along with it's form. Any special
664 // display of attributes is done (disassemble location lists, show enumeration
665 // values for attributes, etc).
666 //----------------------------------------------------------------------
667 void DWARFDebugInfoEntry::DumpAttribute(
668     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
669     const DWARFDataExtractor &debug_info_data, lldb::offset_t *offset_ptr,
670     Stream &s, dw_attr_t attr, DWARFFormValue &form_value) {
671   bool show_form = s.GetFlags().Test(DWARFDebugInfo::eDumpFlag_ShowForm);
672 
673   s.Printf("            ");
674   s.Indent(DW_AT_value_to_name(attr));
675 
676   if (show_form) {
677     s.Printf("[%s", DW_FORM_value_to_name(form_value.Form()));
678   }
679 
680   if (!form_value.ExtractValue(debug_info_data, offset_ptr))
681     return;
682 
683   if (show_form) {
684     if (form_value.Form() == DW_FORM_indirect) {
685       s.Printf(" [%s]", DW_FORM_value_to_name(form_value.Form()));
686     }
687 
688     s.PutCString("] ");
689   }
690 
691   s.PutCString("( ");
692 
693   // Check to see if we have any special attribute formatters
694   switch (attr) {
695   case DW_AT_stmt_list:
696     s.Printf("0x%8.8" PRIx64, form_value.Unsigned());
697     break;
698 
699   case DW_AT_language:
700     s.PutCString(DW_LANG_value_to_name(form_value.Unsigned()));
701     break;
702 
703   case DW_AT_encoding:
704     s.PutCString(DW_ATE_value_to_name(form_value.Unsigned()));
705     break;
706 
707   case DW_AT_frame_base:
708   case DW_AT_location:
709   case DW_AT_data_member_location: {
710     const uint8_t *blockData = form_value.BlockData();
711     if (blockData) {
712       // Location description is inlined in data in the form value
713       DWARFDataExtractor locationData(debug_info_data,
714                                       (*offset_ptr) - form_value.Unsigned(),
715                                       form_value.Unsigned());
716       DWARFExpression::PrintDWARFExpression(
717           s, locationData, DWARFUnit::GetAddressByteSize(cu), 4, false);
718     } else {
719       // We have a location list offset as the value that is the offset into
720       // the .debug_loc section that describes the value over it's lifetime
721       uint64_t debug_loc_offset = form_value.Unsigned();
722       if (dwarf2Data) {
723         DWARFExpression::PrintDWARFLocationList(
724             s, cu, dwarf2Data->DebugLocData(), debug_loc_offset);
725       }
726     }
727   } break;
728 
729   case DW_AT_abstract_origin:
730   case DW_AT_specification: {
731     uint64_t abstract_die_offset = form_value.Reference();
732     form_value.Dump(s);
733     //  *ostrm_ptr << HEX32 << abstract_die_offset << " ( ";
734     GetName(dwarf2Data, cu, abstract_die_offset, s);
735   } break;
736 
737   case DW_AT_type: {
738     uint64_t type_die_offset = form_value.Reference();
739     s.PutCString(" ( ");
740     AppendTypeName(dwarf2Data, cu, type_die_offset, s);
741     s.PutCString(" )");
742   } break;
743 
744   case DW_AT_ranges: {
745     lldb::offset_t ranges_offset = form_value.Unsigned();
746     dw_addr_t base_addr = cu ? cu->GetBaseAddress() : 0;
747     if (dwarf2Data)
748       DWARFDebugRanges::Dump(s, dwarf2Data->get_debug_ranges_data(),
749                              &ranges_offset, base_addr);
750   } break;
751 
752   default:
753     break;
754   }
755 
756   s.PutCString(" )\n");
757 }
758 
759 //----------------------------------------------------------------------
760 // Get all attribute values for a given DIE, including following any
761 // specification or abstract origin attributes and including those in the
762 // results. Any duplicate attributes will have the first instance take
763 // precedence (this can happen for declaration attributes).
764 //----------------------------------------------------------------------
765 size_t DWARFDebugInfoEntry::GetAttributes(
766     const DWARFUnit *cu, DWARFFormValue::FixedFormSizes fixed_form_sizes,
767     DWARFAttributes &attributes, uint32_t curr_depth) const {
768   SymbolFileDWARF *dwarf2Data = nullptr;
769   const DWARFAbbreviationDeclaration *abbrevDecl = nullptr;
770   lldb::offset_t offset = 0;
771   if (cu) {
772     if (m_tag != DW_TAG_compile_unit && m_tag != DW_TAG_partial_unit) {
773       SymbolFileDWARFDwo *dwo_symbol_file = cu->GetDwoSymbolFile();
774       if (dwo_symbol_file)
775         return GetAttributes(dwo_symbol_file->GetCompileUnit(),
776                              fixed_form_sizes, attributes, curr_depth);
777     }
778 
779     dwarf2Data = cu->GetSymbolFileDWARF();
780     abbrevDecl = GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset);
781   }
782 
783   if (abbrevDecl) {
784     const DWARFDataExtractor &debug_info_data = cu->GetData();
785 
786     if (fixed_form_sizes.Empty())
787       fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize(
788           cu->GetAddressByteSize(), cu->IsDWARF64());
789 
790     const uint32_t num_attributes = abbrevDecl->NumAttributes();
791     for (uint32_t i = 0; i < num_attributes; ++i) {
792       DWARFFormValue form_value(cu);
793       dw_attr_t attr;
794       abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value);
795       const dw_form_t form = form_value.Form();
796 
797       // If we are tracking down DW_AT_specification or DW_AT_abstract_origin
798       // attributes, the depth will be non-zero. We need to omit certain
799       // attributes that don't make sense.
800       switch (attr) {
801       case DW_AT_sibling:
802       case DW_AT_declaration:
803         if (curr_depth > 0) {
804           // This attribute doesn't make sense when combined with the DIE that
805           // references this DIE. We know a DIE is referencing this DIE because
806           // curr_depth is not zero
807           break;
808         }
809         LLVM_FALLTHROUGH;
810       default:
811         attributes.Append(cu, offset, attr, form);
812         break;
813       }
814 
815       if ((attr == DW_AT_specification) || (attr == DW_AT_abstract_origin)) {
816         if (form_value.ExtractValue(debug_info_data, &offset)) {
817           dw_offset_t die_offset = form_value.Reference();
818           DWARFDIE spec_die =
819               const_cast<DWARFUnit *>(cu)->GetDIE(die_offset);
820           if (spec_die)
821             spec_die.GetAttributes(attributes, curr_depth + 1);
822         }
823       } else {
824         const uint8_t fixed_skip_size = fixed_form_sizes.GetSize(form);
825         if (fixed_skip_size)
826           offset += fixed_skip_size;
827         else
828           DWARFFormValue::SkipValue(form, debug_info_data, &offset, cu);
829       }
830     }
831   } else {
832     attributes.Clear();
833   }
834   return attributes.Size();
835 }
836 
837 //----------------------------------------------------------------------
838 // GetAttributeValue
839 //
840 // Get the value of an attribute and return the .debug_info offset of the
841 // attribute if it was properly extracted into form_value, or zero if we fail
842 // since an offset of zero is invalid for an attribute (it would be a compile
843 // unit header).
844 //----------------------------------------------------------------------
845 dw_offset_t DWARFDebugInfoEntry::GetAttributeValue(
846     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
847     const dw_attr_t attr, DWARFFormValue &form_value,
848     dw_offset_t *end_attr_offset_ptr,
849     bool check_specification_or_abstract_origin) const {
850   SymbolFileDWARFDwo *dwo_symbol_file = cu->GetDwoSymbolFile();
851   if (dwo_symbol_file && m_tag != DW_TAG_compile_unit &&
852                          m_tag != DW_TAG_partial_unit)
853     return GetAttributeValue(dwo_symbol_file, dwo_symbol_file->GetCompileUnit(),
854                              attr, form_value, end_attr_offset_ptr,
855                              check_specification_or_abstract_origin);
856 
857   lldb::offset_t offset;
858   const DWARFAbbreviationDeclaration *abbrevDecl =
859       GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset);
860 
861   if (abbrevDecl) {
862     uint32_t attr_idx = abbrevDecl->FindAttributeIndex(attr);
863 
864     if (attr_idx != DW_INVALID_INDEX) {
865       const DWARFDataExtractor &debug_info_data = cu->GetData();
866 
867       uint32_t idx = 0;
868       while (idx < attr_idx)
869         DWARFFormValue::SkipValue(abbrevDecl->GetFormByIndex(idx++),
870                                   debug_info_data, &offset, cu);
871 
872       const dw_offset_t attr_offset = offset;
873       form_value.SetCompileUnit(cu);
874       form_value.SetForm(abbrevDecl->GetFormByIndex(idx));
875       if (form_value.ExtractValue(debug_info_data, &offset)) {
876         if (end_attr_offset_ptr)
877           *end_attr_offset_ptr = offset;
878         return attr_offset;
879       }
880     }
881   }
882 
883   if (check_specification_or_abstract_origin) {
884     if (GetAttributeValue(dwarf2Data, cu, DW_AT_specification, form_value)) {
885       DWARFDIE die =
886           const_cast<DWARFUnit *>(cu)->GetDIE(form_value.Reference());
887       if (die) {
888         dw_offset_t die_offset = die.GetDIE()->GetAttributeValue(
889             die.GetDWARF(), die.GetCU(), attr, form_value, end_attr_offset_ptr,
890             false);
891         if (die_offset)
892           return die_offset;
893       }
894     }
895 
896     if (GetAttributeValue(dwarf2Data, cu, DW_AT_abstract_origin, form_value)) {
897       DWARFDIE die =
898           const_cast<DWARFUnit *>(cu)->GetDIE(form_value.Reference());
899       if (die) {
900         dw_offset_t die_offset = die.GetDIE()->GetAttributeValue(
901             die.GetDWARF(), die.GetCU(), attr, form_value, end_attr_offset_ptr,
902             false);
903         if (die_offset)
904           return die_offset;
905       }
906     }
907   }
908 
909   if (!dwo_symbol_file)
910     return 0;
911 
912   DWARFUnit *dwo_cu = dwo_symbol_file->GetCompileUnit();
913   if (!dwo_cu)
914     return 0;
915 
916   DWARFBaseDIE dwo_cu_die = dwo_cu->GetUnitDIEOnly();
917   if (!dwo_cu_die.IsValid())
918     return 0;
919 
920   return dwo_cu_die.GetDIE()->GetAttributeValue(
921       dwo_symbol_file, dwo_cu, attr, form_value, end_attr_offset_ptr,
922       check_specification_or_abstract_origin);
923 }
924 
925 //----------------------------------------------------------------------
926 // GetAttributeValueAsString
927 //
928 // Get the value of an attribute as a string return it. The resulting pointer
929 // to the string data exists within the supplied SymbolFileDWARF and will only
930 // be available as long as the SymbolFileDWARF is still around and it's content
931 // doesn't change.
932 //----------------------------------------------------------------------
933 const char *DWARFDebugInfoEntry::GetAttributeValueAsString(
934     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
935     const dw_attr_t attr, const char *fail_value,
936     bool check_specification_or_abstract_origin) const {
937   DWARFFormValue form_value;
938   if (GetAttributeValue(dwarf2Data, cu, attr, form_value, nullptr,
939                         check_specification_or_abstract_origin))
940     return form_value.AsCString();
941   return fail_value;
942 }
943 
944 //----------------------------------------------------------------------
945 // GetAttributeValueAsUnsigned
946 //
947 // Get the value of an attribute as unsigned and return it.
948 //----------------------------------------------------------------------
949 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsUnsigned(
950     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
951     const dw_attr_t attr, uint64_t fail_value,
952     bool check_specification_or_abstract_origin) const {
953   DWARFFormValue form_value;
954   if (GetAttributeValue(dwarf2Data, cu, attr, form_value, nullptr,
955                         check_specification_or_abstract_origin))
956     return form_value.Unsigned();
957   return fail_value;
958 }
959 
960 //----------------------------------------------------------------------
961 // GetAttributeValueAsSigned
962 //
963 // Get the value of an attribute a signed value and return it.
964 //----------------------------------------------------------------------
965 int64_t DWARFDebugInfoEntry::GetAttributeValueAsSigned(
966     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
967     const dw_attr_t attr, int64_t fail_value,
968     bool check_specification_or_abstract_origin) const {
969   DWARFFormValue form_value;
970   if (GetAttributeValue(dwarf2Data, cu, attr, form_value, nullptr,
971                         check_specification_or_abstract_origin))
972     return form_value.Signed();
973   return fail_value;
974 }
975 
976 //----------------------------------------------------------------------
977 // GetAttributeValueAsReference
978 //
979 // Get the value of an attribute as reference and fix up and compile unit
980 // relative offsets as needed.
981 //----------------------------------------------------------------------
982 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsReference(
983     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
984     const dw_attr_t attr, uint64_t fail_value,
985     bool check_specification_or_abstract_origin) const {
986   DWARFFormValue form_value;
987   if (GetAttributeValue(dwarf2Data, cu, attr, form_value, nullptr,
988                         check_specification_or_abstract_origin))
989     return form_value.Reference();
990   return fail_value;
991 }
992 
993 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsAddress(
994     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
995     const dw_attr_t attr, uint64_t fail_value,
996     bool check_specification_or_abstract_origin) const {
997   DWARFFormValue form_value;
998   if (GetAttributeValue(dwarf2Data, cu, attr, form_value, nullptr,
999                         check_specification_or_abstract_origin))
1000     return form_value.Address();
1001   return fail_value;
1002 }
1003 
1004 //----------------------------------------------------------------------
1005 // GetAttributeHighPC
1006 //
1007 // Get the hi_pc, adding hi_pc to lo_pc when specified as an <offset-from-low-
1008 // pc>.
1009 //
1010 // Returns the hi_pc or fail_value.
1011 //----------------------------------------------------------------------
1012 dw_addr_t DWARFDebugInfoEntry::GetAttributeHighPC(
1013     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu, dw_addr_t lo_pc,
1014     uint64_t fail_value, bool check_specification_or_abstract_origin) const {
1015   DWARFFormValue form_value;
1016   if (GetAttributeValue(dwarf2Data, cu, DW_AT_high_pc, form_value, nullptr,
1017                         check_specification_or_abstract_origin)) {
1018     dw_form_t form = form_value.Form();
1019     if (form == DW_FORM_addr || form == DW_FORM_GNU_addr_index)
1020       return form_value.Address();
1021 
1022     // DWARF4 can specify the hi_pc as an <offset-from-lowpc>
1023     return lo_pc + form_value.Unsigned();
1024   }
1025   return fail_value;
1026 }
1027 
1028 //----------------------------------------------------------------------
1029 // GetAttributeAddressRange
1030 //
1031 // Get the lo_pc and hi_pc, adding hi_pc to lo_pc when specified as an <offset-
1032 // from-low-pc>.
1033 //
1034 // Returns true or sets lo_pc and hi_pc to fail_value.
1035 //----------------------------------------------------------------------
1036 bool DWARFDebugInfoEntry::GetAttributeAddressRange(
1037     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu, dw_addr_t &lo_pc,
1038     dw_addr_t &hi_pc, uint64_t fail_value,
1039     bool check_specification_or_abstract_origin) const {
1040   lo_pc = GetAttributeValueAsAddress(dwarf2Data, cu, DW_AT_low_pc, fail_value,
1041                                      check_specification_or_abstract_origin);
1042   if (lo_pc != fail_value) {
1043     hi_pc = GetAttributeHighPC(dwarf2Data, cu, lo_pc, fail_value,
1044                                check_specification_or_abstract_origin);
1045     if (hi_pc != fail_value)
1046       return true;
1047   }
1048   lo_pc = fail_value;
1049   hi_pc = fail_value;
1050   return false;
1051 }
1052 
1053 size_t DWARFDebugInfoEntry::GetAttributeAddressRanges(
1054     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
1055     DWARFRangeList &ranges, bool check_hi_lo_pc,
1056     bool check_specification_or_abstract_origin) const {
1057   ranges.Clear();
1058 
1059   dw_offset_t debug_ranges_offset = GetAttributeValueAsUnsigned(
1060       dwarf2Data, cu, DW_AT_ranges, DW_INVALID_OFFSET,
1061       check_specification_or_abstract_origin);
1062   if (debug_ranges_offset != DW_INVALID_OFFSET) {
1063     if (DWARFDebugRangesBase *debug_ranges = dwarf2Data->DebugRanges())
1064       debug_ranges->FindRanges(cu, debug_ranges_offset, ranges);
1065   } else if (check_hi_lo_pc) {
1066     dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
1067     dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
1068     if (GetAttributeAddressRange(dwarf2Data, cu, lo_pc, hi_pc,
1069                                  LLDB_INVALID_ADDRESS,
1070                                  check_specification_or_abstract_origin)) {
1071       if (lo_pc < hi_pc)
1072         ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc));
1073     }
1074   }
1075   return ranges.GetSize();
1076 }
1077 
1078 //----------------------------------------------------------------------
1079 // GetName
1080 //
1081 // Get value of the DW_AT_name attribute and return it if one exists, else
1082 // return NULL.
1083 //----------------------------------------------------------------------
1084 const char *DWARFDebugInfoEntry::GetName(SymbolFileDWARF *dwarf2Data,
1085                                          const DWARFUnit *cu) const {
1086   return GetAttributeValueAsString(dwarf2Data, cu, DW_AT_name, nullptr, true);
1087 }
1088 
1089 //----------------------------------------------------------------------
1090 // GetMangledName
1091 //
1092 // Get value of the DW_AT_MIPS_linkage_name attribute and return it if one
1093 // exists, else return the value of the DW_AT_name attribute
1094 //----------------------------------------------------------------------
1095 const char *
1096 DWARFDebugInfoEntry::GetMangledName(SymbolFileDWARF *dwarf2Data,
1097                                     const DWARFUnit *cu,
1098                                     bool substitute_name_allowed) const {
1099   const char *name = nullptr;
1100 
1101   name = GetAttributeValueAsString(dwarf2Data, cu, DW_AT_MIPS_linkage_name,
1102                                    nullptr, true);
1103   if (name)
1104     return name;
1105 
1106   name = GetAttributeValueAsString(dwarf2Data, cu, DW_AT_linkage_name, nullptr,
1107                                    true);
1108   if (name)
1109     return name;
1110 
1111   if (!substitute_name_allowed)
1112     return nullptr;
1113 
1114   name = GetAttributeValueAsString(dwarf2Data, cu, DW_AT_name, nullptr, true);
1115   return name;
1116 }
1117 
1118 //----------------------------------------------------------------------
1119 // GetPubname
1120 //
1121 // Get value the name for a DIE as it should appear for a .debug_pubnames or
1122 // .debug_pubtypes section.
1123 //----------------------------------------------------------------------
1124 const char *DWARFDebugInfoEntry::GetPubname(SymbolFileDWARF *dwarf2Data,
1125                                             const DWARFUnit *cu) const {
1126   const char *name = nullptr;
1127   if (!dwarf2Data)
1128     return name;
1129 
1130   name = GetAttributeValueAsString(dwarf2Data, cu, DW_AT_MIPS_linkage_name,
1131                                    nullptr, true);
1132   if (name)
1133     return name;
1134 
1135   name = GetAttributeValueAsString(dwarf2Data, cu, DW_AT_linkage_name, nullptr,
1136                                    true);
1137   if (name)
1138     return name;
1139 
1140   name = GetAttributeValueAsString(dwarf2Data, cu, DW_AT_name, nullptr, true);
1141   return name;
1142 }
1143 
1144 //----------------------------------------------------------------------
1145 // GetName
1146 //
1147 // Get value of the DW_AT_name attribute for a debug information entry that
1148 // exists at offset "die_offset" and place that value into the supplied stream
1149 // object. If the DIE is a NULL object "NULL" is placed into the stream, and if
1150 // no DW_AT_name attribute exists for the DIE then nothing is printed.
1151 //----------------------------------------------------------------------
1152 bool DWARFDebugInfoEntry::GetName(SymbolFileDWARF *dwarf2Data,
1153                                   const DWARFUnit *cu,
1154                                   const dw_offset_t die_offset, Stream &s) {
1155   if (dwarf2Data == NULL) {
1156     s.PutCString("NULL");
1157     return false;
1158   }
1159 
1160   DWARFDebugInfoEntry die;
1161   lldb::offset_t offset = die_offset;
1162   if (die.Extract(dwarf2Data, cu, &offset)) {
1163     if (die.IsNULL()) {
1164       s.PutCString("NULL");
1165       return true;
1166     } else {
1167       const char *name = die.GetAttributeValueAsString(
1168           dwarf2Data, cu, DW_AT_name, nullptr, true);
1169       if (name) {
1170         s.PutCString(name);
1171         return true;
1172       }
1173     }
1174   }
1175   return false;
1176 }
1177 
1178 //----------------------------------------------------------------------
1179 // AppendTypeName
1180 //
1181 // Follows the type name definition down through all needed tags to end up with
1182 // a fully qualified type name and dump the results to the supplied stream.
1183 // This is used to show the name of types given a type identifier.
1184 //----------------------------------------------------------------------
1185 bool DWARFDebugInfoEntry::AppendTypeName(SymbolFileDWARF *dwarf2Data,
1186                                          const DWARFUnit *cu,
1187                                          const dw_offset_t die_offset,
1188                                          Stream &s) {
1189   if (dwarf2Data == NULL) {
1190     s.PutCString("NULL");
1191     return false;
1192   }
1193 
1194   DWARFDebugInfoEntry die;
1195   lldb::offset_t offset = die_offset;
1196   if (die.Extract(dwarf2Data, cu, &offset)) {
1197     if (die.IsNULL()) {
1198       s.PutCString("NULL");
1199       return true;
1200     } else {
1201       const char *name = die.GetPubname(dwarf2Data, cu);
1202       if (name)
1203         s.PutCString(name);
1204       else {
1205         bool result = true;
1206         const DWARFAbbreviationDeclaration *abbrevDecl =
1207             die.GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset);
1208 
1209         if (abbrevDecl == NULL)
1210           return false;
1211 
1212         switch (abbrevDecl->Tag()) {
1213         case DW_TAG_array_type:
1214           break; // print out a "[]" after printing the full type of the element
1215                  // below
1216         case DW_TAG_base_type:
1217           s.PutCString("base ");
1218           break;
1219         case DW_TAG_class_type:
1220           s.PutCString("class ");
1221           break;
1222         case DW_TAG_const_type:
1223           s.PutCString("const ");
1224           break;
1225         case DW_TAG_enumeration_type:
1226           s.PutCString("enum ");
1227           break;
1228         case DW_TAG_file_type:
1229           s.PutCString("file ");
1230           break;
1231         case DW_TAG_interface_type:
1232           s.PutCString("interface ");
1233           break;
1234         case DW_TAG_packed_type:
1235           s.PutCString("packed ");
1236           break;
1237         case DW_TAG_pointer_type:
1238           break; // print out a '*' after printing the full type below
1239         case DW_TAG_ptr_to_member_type:
1240           break; // print out a '*' after printing the full type below
1241         case DW_TAG_reference_type:
1242           break; // print out a '&' after printing the full type below
1243         case DW_TAG_restrict_type:
1244           s.PutCString("restrict ");
1245           break;
1246         case DW_TAG_set_type:
1247           s.PutCString("set ");
1248           break;
1249         case DW_TAG_shared_type:
1250           s.PutCString("shared ");
1251           break;
1252         case DW_TAG_string_type:
1253           s.PutCString("string ");
1254           break;
1255         case DW_TAG_structure_type:
1256           s.PutCString("struct ");
1257           break;
1258         case DW_TAG_subrange_type:
1259           s.PutCString("subrange ");
1260           break;
1261         case DW_TAG_subroutine_type:
1262           s.PutCString("function ");
1263           break;
1264         case DW_TAG_thrown_type:
1265           s.PutCString("thrown ");
1266           break;
1267         case DW_TAG_union_type:
1268           s.PutCString("union ");
1269           break;
1270         case DW_TAG_unspecified_type:
1271           s.PutCString("unspecified ");
1272           break;
1273         case DW_TAG_volatile_type:
1274           s.PutCString("volatile ");
1275           break;
1276         default:
1277           return false;
1278         }
1279 
1280         // Follow the DW_AT_type if possible
1281         DWARFFormValue form_value;
1282         if (die.GetAttributeValue(dwarf2Data, cu, DW_AT_type, form_value)) {
1283           uint64_t next_die_offset = form_value.Reference();
1284           result = AppendTypeName(dwarf2Data, cu, next_die_offset, s);
1285         }
1286 
1287         switch (abbrevDecl->Tag()) {
1288         case DW_TAG_array_type:
1289           s.PutCString("[]");
1290           break;
1291         case DW_TAG_pointer_type:
1292           s.PutChar('*');
1293           break;
1294         case DW_TAG_ptr_to_member_type:
1295           s.PutChar('*');
1296           break;
1297         case DW_TAG_reference_type:
1298           s.PutChar('&');
1299           break;
1300         default:
1301           break;
1302         }
1303         return result;
1304       }
1305     }
1306   }
1307   return false;
1308 }
1309 
1310 //----------------------------------------------------------------------
1311 // BuildAddressRangeTable
1312 //----------------------------------------------------------------------
1313 void DWARFDebugInfoEntry::BuildAddressRangeTable(
1314     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
1315     DWARFDebugAranges *debug_aranges) const {
1316   if (m_tag) {
1317     if (m_tag == DW_TAG_subprogram) {
1318       dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
1319       dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
1320       if (GetAttributeAddressRange(dwarf2Data, cu, lo_pc, hi_pc,
1321                                    LLDB_INVALID_ADDRESS)) {
1322         /// printf("BuildAddressRangeTable() 0x%8.8x: %30s: [0x%8.8x -
1323         /// 0x%8.8x)\n", m_offset, DW_TAG_value_to_name(tag), lo_pc, hi_pc);
1324         debug_aranges->AppendRange(cu->GetOffset(), lo_pc, hi_pc);
1325       }
1326     }
1327 
1328     const DWARFDebugInfoEntry *child = GetFirstChild();
1329     while (child) {
1330       child->BuildAddressRangeTable(dwarf2Data, cu, debug_aranges);
1331       child = child->GetSibling();
1332     }
1333   }
1334 }
1335 
1336 //----------------------------------------------------------------------
1337 // BuildFunctionAddressRangeTable
1338 //
1339 // This function is very similar to the BuildAddressRangeTable function except
1340 // that the actual DIE offset for the function is placed in the table instead
1341 // of the compile unit offset (which is the way the standard .debug_aranges
1342 // section does it).
1343 //----------------------------------------------------------------------
1344 void DWARFDebugInfoEntry::BuildFunctionAddressRangeTable(
1345     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
1346     DWARFDebugAranges *debug_aranges) const {
1347   if (m_tag) {
1348     if (m_tag == DW_TAG_subprogram) {
1349       dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
1350       dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
1351       if (GetAttributeAddressRange(dwarf2Data, cu, lo_pc, hi_pc,
1352                                    LLDB_INVALID_ADDRESS)) {
1353         //  printf("BuildAddressRangeTable() 0x%8.8x: [0x%16.16" PRIx64 " -
1354         //  0x%16.16" PRIx64 ")\n", m_offset, lo_pc, hi_pc); // DEBUG ONLY
1355         debug_aranges->AppendRange(GetOffset(), lo_pc, hi_pc);
1356       }
1357     }
1358 
1359     const DWARFDebugInfoEntry *child = GetFirstChild();
1360     while (child) {
1361       child->BuildFunctionAddressRangeTable(dwarf2Data, cu, debug_aranges);
1362       child = child->GetSibling();
1363     }
1364   }
1365 }
1366 
1367 void DWARFDebugInfoEntry::GetDeclContextDIEs(
1368     DWARFUnit *cu, DWARFDIECollection &decl_context_dies) const {
1369 
1370   DWARFDIE die(cu, const_cast<DWARFDebugInfoEntry *>(this));
1371   die.GetDeclContextDIEs(decl_context_dies);
1372 }
1373 
1374 void DWARFDebugInfoEntry::GetDWARFDeclContext(
1375     SymbolFileDWARF *dwarf2Data, DWARFUnit *cu,
1376     DWARFDeclContext &dwarf_decl_ctx) const {
1377   const dw_tag_t tag = Tag();
1378   if (tag != DW_TAG_compile_unit && tag != DW_TAG_partial_unit) {
1379     dwarf_decl_ctx.AppendDeclContext(tag, GetName(dwarf2Data, cu));
1380     DWARFDIE parent_decl_ctx_die = GetParentDeclContextDIE(dwarf2Data, cu);
1381     if (parent_decl_ctx_die && parent_decl_ctx_die.GetDIE() != this) {
1382       if (parent_decl_ctx_die.Tag() != DW_TAG_compile_unit &&
1383           parent_decl_ctx_die.Tag() != DW_TAG_partial_unit)
1384         parent_decl_ctx_die.GetDIE()->GetDWARFDeclContext(
1385             parent_decl_ctx_die.GetDWARF(), parent_decl_ctx_die.GetCU(),
1386             dwarf_decl_ctx);
1387     }
1388   }
1389 }
1390 
1391 bool DWARFDebugInfoEntry::MatchesDWARFDeclContext(
1392     SymbolFileDWARF *dwarf2Data, DWARFUnit *cu,
1393     const DWARFDeclContext &dwarf_decl_ctx) const {
1394 
1395   DWARFDeclContext this_dwarf_decl_ctx;
1396   GetDWARFDeclContext(dwarf2Data, cu, this_dwarf_decl_ctx);
1397   return this_dwarf_decl_ctx == dwarf_decl_ctx;
1398 }
1399 
1400 DWARFDIE
1401 DWARFDebugInfoEntry::GetParentDeclContextDIE(SymbolFileDWARF *dwarf2Data,
1402                                              DWARFUnit *cu) const {
1403   DWARFAttributes attributes;
1404   GetAttributes(cu, DWARFFormValue::FixedFormSizes(), attributes);
1405   return GetParentDeclContextDIE(dwarf2Data, cu, attributes);
1406 }
1407 
1408 DWARFDIE
1409 DWARFDebugInfoEntry::GetParentDeclContextDIE(
1410     SymbolFileDWARF *dwarf2Data, DWARFUnit *cu,
1411     const DWARFAttributes &attributes) const {
1412   DWARFDIE die(cu, const_cast<DWARFDebugInfoEntry *>(this));
1413 
1414   while (die) {
1415     // If this is the original DIE that we are searching for a declaration for,
1416     // then don't look in the cache as we don't want our own decl context to be
1417     // our decl context...
1418     if (die.GetDIE() != this) {
1419       switch (die.Tag()) {
1420       case DW_TAG_compile_unit:
1421       case DW_TAG_partial_unit:
1422       case DW_TAG_namespace:
1423       case DW_TAG_structure_type:
1424       case DW_TAG_union_type:
1425       case DW_TAG_class_type:
1426         return die;
1427 
1428       default:
1429         break;
1430       }
1431     }
1432 
1433     dw_offset_t die_offset;
1434 
1435     die_offset =
1436         attributes.FormValueAsUnsigned(DW_AT_specification, DW_INVALID_OFFSET);
1437     if (die_offset != DW_INVALID_OFFSET) {
1438       DWARFDIE spec_die = cu->GetDIE(die_offset);
1439       if (spec_die) {
1440         DWARFDIE decl_ctx_die = spec_die.GetParentDeclContextDIE();
1441         if (decl_ctx_die)
1442           return decl_ctx_die;
1443       }
1444     }
1445 
1446     die_offset = attributes.FormValueAsUnsigned(DW_AT_abstract_origin,
1447                                                 DW_INVALID_OFFSET);
1448     if (die_offset != DW_INVALID_OFFSET) {
1449       DWARFDIE abs_die = cu->GetDIE(die_offset);
1450       if (abs_die) {
1451         DWARFDIE decl_ctx_die = abs_die.GetParentDeclContextDIE();
1452         if (decl_ctx_die)
1453           return decl_ctx_die;
1454       }
1455     }
1456 
1457     die = die.GetParent();
1458   }
1459   return DWARFDIE();
1460 }
1461 
1462 const char *DWARFDebugInfoEntry::GetQualifiedName(SymbolFileDWARF *dwarf2Data,
1463                                                   DWARFUnit *cu,
1464                                                   std::string &storage) const {
1465   DWARFAttributes attributes;
1466   GetAttributes(cu, DWARFFormValue::FixedFormSizes(), attributes);
1467   return GetQualifiedName(dwarf2Data, cu, attributes, storage);
1468 }
1469 
1470 const char *DWARFDebugInfoEntry::GetQualifiedName(
1471     SymbolFileDWARF *dwarf2Data, DWARFUnit *cu,
1472     const DWARFAttributes &attributes, std::string &storage) const {
1473 
1474   const char *name = GetName(dwarf2Data, cu);
1475 
1476   if (name) {
1477     DWARFDIE parent_decl_ctx_die = GetParentDeclContextDIE(dwarf2Data, cu);
1478     storage.clear();
1479     // TODO: change this to get the correct decl context parent....
1480     while (parent_decl_ctx_die) {
1481       const dw_tag_t parent_tag = parent_decl_ctx_die.Tag();
1482       switch (parent_tag) {
1483       case DW_TAG_namespace: {
1484         const char *namespace_name = parent_decl_ctx_die.GetName();
1485         if (namespace_name) {
1486           storage.insert(0, "::");
1487           storage.insert(0, namespace_name);
1488         } else {
1489           storage.insert(0, "(anonymous namespace)::");
1490         }
1491         parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE();
1492       } break;
1493 
1494       case DW_TAG_class_type:
1495       case DW_TAG_structure_type:
1496       case DW_TAG_union_type: {
1497         const char *class_union_struct_name = parent_decl_ctx_die.GetName();
1498 
1499         if (class_union_struct_name) {
1500           storage.insert(0, "::");
1501           storage.insert(0, class_union_struct_name);
1502         }
1503         parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE();
1504       } break;
1505 
1506       default:
1507         parent_decl_ctx_die.Clear();
1508         break;
1509       }
1510     }
1511 
1512     if (storage.empty())
1513       storage.append("::");
1514 
1515     storage.append(name);
1516   }
1517   if (storage.empty())
1518     return NULL;
1519   return storage.c_str();
1520 }
1521 
1522 //----------------------------------------------------------------------
1523 // LookupAddress
1524 //----------------------------------------------------------------------
1525 bool DWARFDebugInfoEntry::LookupAddress(const dw_addr_t address,
1526                                         SymbolFileDWARF *dwarf2Data,
1527                                         const DWARFUnit *cu,
1528                                         DWARFDebugInfoEntry **function_die,
1529                                         DWARFDebugInfoEntry **block_die) {
1530   bool found_address = false;
1531   if (m_tag) {
1532     bool check_children = false;
1533     bool match_addr_range = false;
1534     //  printf("0x%8.8x: %30s: address = 0x%8.8x - ", m_offset,
1535     //  DW_TAG_value_to_name(tag), address);
1536     switch (m_tag) {
1537     case DW_TAG_array_type:
1538       break;
1539     case DW_TAG_class_type:
1540       check_children = true;
1541       break;
1542     case DW_TAG_entry_point:
1543       break;
1544     case DW_TAG_enumeration_type:
1545       break;
1546     case DW_TAG_formal_parameter:
1547       break;
1548     case DW_TAG_imported_declaration:
1549       break;
1550     case DW_TAG_label:
1551       break;
1552     case DW_TAG_lexical_block:
1553       check_children = true;
1554       match_addr_range = true;
1555       break;
1556     case DW_TAG_member:
1557       break;
1558     case DW_TAG_pointer_type:
1559       break;
1560     case DW_TAG_reference_type:
1561       break;
1562     case DW_TAG_compile_unit:
1563       match_addr_range = true;
1564       break;
1565     case DW_TAG_string_type:
1566       break;
1567     case DW_TAG_structure_type:
1568       check_children = true;
1569       break;
1570     case DW_TAG_subroutine_type:
1571       break;
1572     case DW_TAG_typedef:
1573       break;
1574     case DW_TAG_union_type:
1575       break;
1576     case DW_TAG_unspecified_parameters:
1577       break;
1578     case DW_TAG_variant:
1579       break;
1580     case DW_TAG_common_block:
1581       check_children = true;
1582       break;
1583     case DW_TAG_common_inclusion:
1584       break;
1585     case DW_TAG_inheritance:
1586       break;
1587     case DW_TAG_inlined_subroutine:
1588       check_children = true;
1589       match_addr_range = true;
1590       break;
1591     case DW_TAG_module:
1592       match_addr_range = true;
1593       break;
1594     case DW_TAG_ptr_to_member_type:
1595       break;
1596     case DW_TAG_set_type:
1597       break;
1598     case DW_TAG_subrange_type:
1599       break;
1600     case DW_TAG_with_stmt:
1601       break;
1602     case DW_TAG_access_declaration:
1603       break;
1604     case DW_TAG_base_type:
1605       break;
1606     case DW_TAG_catch_block:
1607       match_addr_range = true;
1608       break;
1609     case DW_TAG_const_type:
1610       break;
1611     case DW_TAG_constant:
1612       break;
1613     case DW_TAG_enumerator:
1614       break;
1615     case DW_TAG_file_type:
1616       break;
1617     case DW_TAG_friend:
1618       break;
1619     case DW_TAG_namelist:
1620       break;
1621     case DW_TAG_namelist_item:
1622       break;
1623     case DW_TAG_packed_type:
1624       break;
1625     case DW_TAG_subprogram:
1626       match_addr_range = true;
1627       break;
1628     case DW_TAG_template_type_parameter:
1629       break;
1630     case DW_TAG_template_value_parameter:
1631       break;
1632     case DW_TAG_GNU_template_parameter_pack:
1633       break;
1634     case DW_TAG_thrown_type:
1635       break;
1636     case DW_TAG_try_block:
1637       match_addr_range = true;
1638       break;
1639     case DW_TAG_variant_part:
1640       break;
1641     case DW_TAG_variable:
1642       break;
1643     case DW_TAG_volatile_type:
1644       break;
1645     case DW_TAG_dwarf_procedure:
1646       break;
1647     case DW_TAG_restrict_type:
1648       break;
1649     case DW_TAG_interface_type:
1650       break;
1651     case DW_TAG_namespace:
1652       check_children = true;
1653       break;
1654     case DW_TAG_imported_module:
1655       break;
1656     case DW_TAG_unspecified_type:
1657       break;
1658     case DW_TAG_partial_unit:
1659       match_addr_range = true;
1660       break;
1661     case DW_TAG_imported_unit:
1662       break;
1663     case DW_TAG_shared_type:
1664       break;
1665     default:
1666       break;
1667     }
1668 
1669     if (match_addr_range) {
1670       dw_addr_t lo_pc = GetAttributeValueAsAddress(dwarf2Data, cu, DW_AT_low_pc,
1671                                                    LLDB_INVALID_ADDRESS);
1672       if (lo_pc != LLDB_INVALID_ADDRESS) {
1673         dw_addr_t hi_pc =
1674             GetAttributeHighPC(dwarf2Data, cu, lo_pc, LLDB_INVALID_ADDRESS);
1675         if (hi_pc != LLDB_INVALID_ADDRESS) {
1676           //  printf("\n0x%8.8x: %30s: address = 0x%8.8x  [0x%8.8x - 0x%8.8x) ",
1677           //  m_offset, DW_TAG_value_to_name(tag), address, lo_pc, hi_pc);
1678           if ((lo_pc <= address) && (address < hi_pc)) {
1679             found_address = true;
1680             //  puts("***MATCH***");
1681             switch (m_tag) {
1682             case DW_TAG_compile_unit: // File
1683             case DW_TAG_partial_unit: // File
1684               check_children = ((function_die != NULL) || (block_die != NULL));
1685               break;
1686 
1687             case DW_TAG_subprogram: // Function
1688               if (function_die)
1689                 *function_die = this;
1690               check_children = (block_die != NULL);
1691               break;
1692 
1693             case DW_TAG_inlined_subroutine: // Inlined Function
1694             case DW_TAG_lexical_block:      // Block { } in code
1695               if (block_die) {
1696                 *block_die = this;
1697                 check_children = true;
1698               }
1699               break;
1700 
1701             default:
1702               check_children = true;
1703               break;
1704             }
1705           }
1706         } else {
1707           // Compile units may not have a valid high/low pc when there
1708           // are address gaps in subroutines so we must always search
1709           // if there is no valid high and low PC.
1710           check_children = (m_tag == DW_TAG_compile_unit ||
1711                             m_tag == DW_TAG_partial_unit) &&
1712                            ((function_die != NULL) || (block_die != NULL));
1713         }
1714       } else {
1715         dw_offset_t debug_ranges_offset = GetAttributeValueAsUnsigned(
1716             dwarf2Data, cu, DW_AT_ranges, DW_INVALID_OFFSET);
1717         if (debug_ranges_offset != DW_INVALID_OFFSET) {
1718           DWARFRangeList ranges;
1719           DWARFDebugRangesBase *debug_ranges = dwarf2Data->DebugRanges();
1720           debug_ranges->FindRanges(cu, debug_ranges_offset, ranges);
1721 
1722           if (ranges.FindEntryThatContains(address)) {
1723             found_address = true;
1724             //  puts("***MATCH***");
1725             switch (m_tag) {
1726             case DW_TAG_compile_unit: // File
1727             case DW_TAG_partial_unit: // File
1728               check_children = ((function_die != NULL) || (block_die != NULL));
1729               break;
1730 
1731             case DW_TAG_subprogram: // Function
1732               if (function_die)
1733                 *function_die = this;
1734               check_children = (block_die != NULL);
1735               break;
1736 
1737             case DW_TAG_inlined_subroutine: // Inlined Function
1738             case DW_TAG_lexical_block:      // Block { } in code
1739               if (block_die) {
1740                 *block_die = this;
1741                 check_children = true;
1742               }
1743               break;
1744 
1745             default:
1746               check_children = true;
1747               break;
1748             }
1749           } else {
1750             check_children = false;
1751           }
1752         }
1753       }
1754     }
1755 
1756     if (check_children) {
1757       //  printf("checking children\n");
1758       DWARFDebugInfoEntry *child = GetFirstChild();
1759       while (child) {
1760         if (child->LookupAddress(address, dwarf2Data, cu, function_die,
1761                                  block_die))
1762           return true;
1763         child = child->GetSibling();
1764       }
1765     }
1766   }
1767   return found_address;
1768 }
1769 
1770 const DWARFAbbreviationDeclaration *
1771 DWARFDebugInfoEntry::GetAbbreviationDeclarationPtr(
1772     SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
1773     lldb::offset_t &offset) const {
1774   if (dwarf2Data) {
1775     offset = GetOffset();
1776 
1777     const DWARFAbbreviationDeclarationSet *abbrev_set = cu->GetAbbreviations();
1778     if (abbrev_set) {
1779       const DWARFAbbreviationDeclaration *abbrev_decl =
1780           abbrev_set->GetAbbreviationDeclaration(m_abbr_idx);
1781       if (abbrev_decl) {
1782         // Make sure the abbreviation code still matches. If it doesn't and the
1783         // DWARF data was mmap'ed, the backing file might have been modified
1784         // which is bad news.
1785         const uint64_t abbrev_code = cu->GetData().GetULEB128(&offset);
1786 
1787         if (abbrev_decl->Code() == abbrev_code)
1788           return abbrev_decl;
1789 
1790         dwarf2Data->GetObjectFile()->GetModule()->ReportErrorIfModifyDetected(
1791             "0x%8.8x: the DWARF debug information has been modified (abbrev "
1792             "code was %u, and is now %u)",
1793             GetOffset(), (uint32_t)abbrev_decl->Code(), (uint32_t)abbrev_code);
1794       }
1795     }
1796   }
1797   offset = DW_INVALID_OFFSET;
1798   return NULL;
1799 }
1800 
1801 bool DWARFDebugInfoEntry::OffsetLessThan(const DWARFDebugInfoEntry &a,
1802                                          const DWARFDebugInfoEntry &b) {
1803   return a.GetOffset() < b.GetOffset();
1804 }
1805 
1806 void DWARFDebugInfoEntry::DumpDIECollection(
1807     Stream &strm, DWARFDebugInfoEntry::collection &die_collection) {
1808   DWARFDebugInfoEntry::const_iterator pos;
1809   DWARFDebugInfoEntry::const_iterator end = die_collection.end();
1810   strm.PutCString("\noffset    parent   sibling  child\n");
1811   strm.PutCString("--------  -------- -------- --------\n");
1812   for (pos = die_collection.begin(); pos != end; ++pos) {
1813     const DWARFDebugInfoEntry &die_ref = *pos;
1814     const DWARFDebugInfoEntry *p = die_ref.GetParent();
1815     const DWARFDebugInfoEntry *s = die_ref.GetSibling();
1816     const DWARFDebugInfoEntry *c = die_ref.GetFirstChild();
1817     strm.Printf("%.8x: %.8x %.8x %.8x 0x%4.4x %s%s\n", die_ref.GetOffset(),
1818                 p ? p->GetOffset() : 0, s ? s->GetOffset() : 0,
1819                 c ? c->GetOffset() : 0, die_ref.Tag(),
1820                 DW_TAG_value_to_name(die_ref.Tag()),
1821                 die_ref.HasChildren() ? " *" : "");
1822   }
1823 }
1824 
1825 bool DWARFDebugInfoEntry::operator==(const DWARFDebugInfoEntry &rhs) const {
1826   return m_offset == rhs.m_offset && m_parent_idx == rhs.m_parent_idx &&
1827          m_sibling_idx == rhs.m_sibling_idx &&
1828          m_abbr_idx == rhs.m_abbr_idx && m_has_children == rhs.m_has_children &&
1829          m_tag == rhs.m_tag;
1830 }
1831 
1832 bool DWARFDebugInfoEntry::operator!=(const DWARFDebugInfoEntry &rhs) const {
1833   return !(*this == rhs);
1834 }
1835