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