1//===-- DWARFDebugInfoEntry.h -----------------------------------*- 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#ifndef LLVM_LIB_DEBUGINFO_DWARFDEBUGINFOENTRY_H
11#define LLVM_LIB_DEBUGINFO_DWARFDEBUGINFOENTRY_H
12
13#include "llvm/ADT/SmallVector.h"
14#include "llvm/DebugInfo/DIContext.h"
15#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
16#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
17#include "llvm/Support/DataTypes.h"
18
19namespace llvm {
20
21class DWARFDebugAranges;
22class DWARFCompileUnit;
23class DWARFUnit;
24class DWARFContext;
25class DWARFFormValue;
26struct DWARFDebugInfoEntryInlinedChain;
27
28/// DWARFDebugInfoEntryMinimal - A DIE with only the minimum required data.
29class DWARFDebugInfoEntryMinimal {
30  /// Offset within the .debug_info of the start of this entry.
31  uint32_t Offset;
32
33  /// How many to add to "this" to get the sibling.
34  uint32_t SiblingIdx;
35
36  const DWARFAbbreviationDeclaration *AbbrevDecl;
37public:
38  DWARFDebugInfoEntryMinimal()
39    : Offset(0), SiblingIdx(0), AbbrevDecl(nullptr) {}
40
41  void dump(raw_ostream &OS, DWARFUnit *u, unsigned recurseDepth,
42            unsigned indent = 0) const;
43  void dumpAttribute(raw_ostream &OS, DWARFUnit *u, uint32_t *offset_ptr,
44                     uint16_t attr, uint16_t form, unsigned indent = 0) const;
45
46  /// Extracts a debug info entry, which is a child of a given unit,
47  /// starting at a given offset. If DIE can't be extracted, returns false and
48  /// doesn't change OffsetPtr.
49  bool extractFast(const DWARFUnit *U, uint32_t *OffsetPtr);
50
51  uint32_t getTag() const { return AbbrevDecl ? AbbrevDecl->getTag() : 0; }
52  bool isNULL() const { return AbbrevDecl == nullptr; }
53
54  /// Returns true if DIE represents a subprogram (not inlined).
55  bool isSubprogramDIE() const;
56  /// Returns true if DIE represents a subprogram or an inlined
57  /// subroutine.
58  bool isSubroutineDIE() const;
59
60  uint32_t getOffset() const { return Offset; }
61  bool hasChildren() const { return !isNULL() && AbbrevDecl->hasChildren(); }
62
63  // We know we are kept in a vector of contiguous entries, so we know
64  // our sibling will be some index after "this".
65  const DWARFDebugInfoEntryMinimal *getSibling() const {
66    return SiblingIdx > 0 ? this + SiblingIdx : nullptr;
67  }
68
69  // We know we are kept in a vector of contiguous entries, so we know
70  // we don't need to store our child pointer, if we have a child it will
71  // be the next entry in the list...
72  const DWARFDebugInfoEntryMinimal *getFirstChild() const {
73    return hasChildren() ? this + 1 : nullptr;
74  }
75
76  void setSibling(const DWARFDebugInfoEntryMinimal *Sibling) {
77    if (Sibling) {
78      // We know we are kept in a vector of contiguous entries, so we know
79      // our sibling will be some index after "this".
80      SiblingIdx = Sibling - this;
81    } else
82      SiblingIdx = 0;
83  }
84
85  const DWARFAbbreviationDeclaration *getAbbreviationDeclarationPtr() const {
86    return AbbrevDecl;
87  }
88
89  bool getAttributeValue(const DWARFUnit *U, const uint16_t Attr,
90                         DWARFFormValue &FormValue) const;
91
92  const char *getAttributeValueAsString(const DWARFUnit *U, const uint16_t Attr,
93                                        const char *FailValue) const;
94
95  uint64_t getAttributeValueAsAddress(const DWARFUnit *U, const uint16_t Attr,
96                                      uint64_t FailValue) const;
97
98  uint64_t getAttributeValueAsUnsignedConstant(const DWARFUnit *U,
99                                               const uint16_t Attr,
100                                               uint64_t FailValue) const;
101
102  uint64_t getAttributeValueAsReference(const DWARFUnit *U, const uint16_t Attr,
103                                        uint64_t FailValue) const;
104
105  uint64_t getAttributeValueAsSectionOffset(const DWARFUnit *U,
106                                            const uint16_t Attr,
107                                            uint64_t FailValue) const;
108
109  uint64_t getRangesBaseAttribute(const DWARFUnit *U, uint64_t FailValue) const;
110
111  /// Retrieves DW_AT_low_pc and DW_AT_high_pc from CU.
112  /// Returns true if both attributes are present.
113  bool getLowAndHighPC(const DWARFUnit *U, uint64_t &LowPC,
114                       uint64_t &HighPC) const;
115
116  DWARFAddressRangesVector getAddressRanges(const DWARFUnit *U) const;
117
118  void collectChildrenAddressRanges(const DWARFUnit *U,
119                                    DWARFAddressRangesVector &Ranges) const;
120
121  bool addressRangeContainsAddress(const DWARFUnit *U,
122                                   const uint64_t Address) const;
123
124  /// If a DIE represents a subprogram (or inlined subroutine),
125  /// returns its mangled name (or short name, if mangled is missing).
126  /// This name may be fetched from specification or abstract origin
127  /// for this subprogram. Returns null if no name is found.
128  const char *getSubroutineName(const DWARFUnit *U, DINameKind Kind) const;
129
130  /// Return the DIE name resolving DW_AT_sepcification or
131  /// DW_AT_abstract_origin references if necessary.
132  /// Returns null if no name is found.
133  const char *getName(const DWARFUnit *U, DINameKind Kind) const;
134
135  /// Retrieves values of DW_AT_call_file, DW_AT_call_line and
136  /// DW_AT_call_column from DIE (or zeroes if they are missing).
137  void getCallerFrame(const DWARFUnit *U, uint32_t &CallFile,
138                      uint32_t &CallLine, uint32_t &CallColumn) const;
139
140  /// Get inlined chain for a given address, rooted at the current DIE.
141  /// Returns empty chain if address is not contained in address range
142  /// of current DIE.
143  DWARFDebugInfoEntryInlinedChain
144  getInlinedChainForAddress(const DWARFUnit *U, const uint64_t Address) const;
145};
146
147/// DWARFDebugInfoEntryInlinedChain - represents a chain of inlined_subroutine
148/// DIEs, (possibly ending with subprogram DIE), all of which are contained
149/// in some concrete inlined instance tree. Address range for each DIE
150/// (except the last DIE) in this chain is contained in address
151/// range for next DIE in the chain.
152struct DWARFDebugInfoEntryInlinedChain {
153  DWARFDebugInfoEntryInlinedChain() : U(nullptr) {}
154  SmallVector<DWARFDebugInfoEntryMinimal, 4> DIEs;
155  const DWARFUnit *U;
156};
157
158}
159
160#endif
161