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