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