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_DEBUGINFO_DWARFDEBUGINFOENTRY_H
11#define LLVM_DEBUGINFO_DWARFDEBUGINFOENTRY_H
12
13#include "DWARFAbbreviationDeclaration.h"
14#include "llvm/ADT/SmallVector.h"
15#include "llvm/Support/DataTypes.h"
16
17namespace llvm {
18
19class DWARFDebugAranges;
20class DWARFCompileUnit;
21class DWARFContext;
22class DWARFFormValue;
23class DWARFInlinedSubroutineChain;
24
25/// DWARFDebugInfoEntryMinimal - A DIE with only the minimum required data.
26class DWARFDebugInfoEntryMinimal {
27  /// Offset within the .debug_info of the start of this entry.
28  uint32_t Offset;
29
30  /// How many to subtract from "this" to get the parent.
31  /// If zero this die has no parent.
32  uint32_t ParentIdx;
33
34  /// How many to add to "this" to get the sibling.
35  uint32_t SiblingIdx;
36
37  const DWARFAbbreviationDeclaration *AbbrevDecl;
38public:
39  DWARFDebugInfoEntryMinimal()
40    : Offset(0), ParentIdx(0), SiblingIdx(0), AbbrevDecl(0) {}
41
42  void dump(raw_ostream &OS, const DWARFCompileUnit *cu,
43            unsigned recurseDepth, unsigned indent = 0) const;
44  void dumpAttribute(raw_ostream &OS, const DWARFCompileUnit *cu,
45                     uint32_t *offset_ptr, uint16_t attr, uint16_t form,
46                     unsigned indent = 0) const;
47
48  bool extractFast(const DWARFCompileUnit *cu, const uint8_t *fixed_form_sizes,
49                   uint32_t *offset_ptr);
50
51  /// Extract a debug info entry for a given compile unit from the
52  /// .debug_info and .debug_abbrev data starting at the given offset.
53  bool extract(const DWARFCompileUnit *cu, uint32_t *offset_ptr);
54
55  uint32_t getTag() const { return AbbrevDecl ? AbbrevDecl->getTag() : 0; }
56  bool isNULL() const { return AbbrevDecl == 0; }
57
58  /// Returns true if DIE represents a subprogram (not inlined).
59  bool isSubprogramDIE() const;
60  /// Returns true if DIE represents a subprogram or an inlined
61  /// subroutine.
62  bool isSubroutineDIE() const;
63
64  uint32_t getOffset() const { return Offset; }
65  uint32_t getNumAttributes() const {
66    return !isNULL() ? AbbrevDecl->getNumAttributes() : 0;
67  }
68  bool hasChildren() const { return !isNULL() && AbbrevDecl->hasChildren(); }
69
70  // We know we are kept in a vector of contiguous entries, so we know
71  // our parent will be some index behind "this".
72  DWARFDebugInfoEntryMinimal *getParent() {
73    return ParentIdx > 0 ? this - ParentIdx : 0;
74  }
75  const DWARFDebugInfoEntryMinimal *getParent() const {
76    return ParentIdx > 0 ? this - ParentIdx : 0;
77  }
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  DWARFDebugInfoEntryMinimal *getSibling() {
81    return SiblingIdx > 0 ? this + SiblingIdx : 0;
82  }
83  const DWARFDebugInfoEntryMinimal *getSibling() const {
84    return SiblingIdx > 0 ? this + SiblingIdx : 0;
85  }
86  // We know we are kept in a vector of contiguous entries, so we know
87  // we don't need to store our child pointer, if we have a child it will
88  // be the next entry in the list...
89  DWARFDebugInfoEntryMinimal *getFirstChild() {
90    return hasChildren() ? this + 1 : 0;
91  }
92  const DWARFDebugInfoEntryMinimal *getFirstChild() const {
93    return hasChildren() ? this + 1 : 0;
94  }
95
96  void setParent(DWARFDebugInfoEntryMinimal *parent) {
97    if (parent) {
98      // We know we are kept in a vector of contiguous entries, so we know
99      // our parent will be some index behind "this".
100      ParentIdx = this - parent;
101    } else
102      ParentIdx = 0;
103  }
104  void setSibling(DWARFDebugInfoEntryMinimal *sibling) {
105    if (sibling) {
106      // We know we are kept in a vector of contiguous entries, so we know
107      // our sibling will be some index after "this".
108      SiblingIdx = sibling - this;
109      sibling->setParent(getParent());
110    } else
111      SiblingIdx = 0;
112  }
113
114  const DWARFAbbreviationDeclaration *getAbbreviationDeclarationPtr() const {
115    return AbbrevDecl;
116  }
117
118  uint32_t getAttributeValue(const DWARFCompileUnit *cu,
119                             const uint16_t attr, DWARFFormValue &formValue,
120                             uint32_t *end_attr_offset_ptr = 0) const;
121
122  const char* getAttributeValueAsString(const DWARFCompileUnit* cu,
123                                        const uint16_t attr,
124                                        const char *fail_value) const;
125
126  uint64_t getAttributeValueAsUnsigned(const DWARFCompileUnit *cu,
127                                       const uint16_t attr,
128                                       uint64_t fail_value) const;
129
130  uint64_t getAttributeValueAsReference(const DWARFCompileUnit *cu,
131                                        const uint16_t attr,
132                                        uint64_t fail_value) const;
133
134  int64_t getAttributeValueAsSigned(const DWARFCompileUnit* cu,
135                                    const uint16_t attr,
136                                    int64_t fail_value) const;
137
138  /// Retrieves DW_AT_low_pc and DW_AT_high_pc from CU.
139  /// Returns true if both attributes are present.
140  bool getLowAndHighPC(const DWARFCompileUnit *CU,
141                       uint64_t &LowPC, uint64_t &HighPC) const;
142
143  void buildAddressRangeTable(const DWARFCompileUnit *CU,
144                              DWARFDebugAranges *DebugAranges) const;
145
146  bool addressRangeContainsAddress(const DWARFCompileUnit *CU,
147                                   const uint64_t Address) const;
148
149  /// If a DIE represents a subprogram (or inlined subroutine),
150  /// returns its mangled name (or short name, if mangled is missing).
151  /// This name may be fetched from specification or abstract origin
152  /// for this subprogram. Returns null if no name is found.
153  const char* getSubroutineName(const DWARFCompileUnit *CU) const;
154
155  /// Retrieves values of DW_AT_call_file, DW_AT_call_line and
156  /// DW_AT_call_column from DIE (or zeroes if they are missing).
157  void getCallerFrame(const DWARFCompileUnit *CU, uint32_t &CallFile,
158                      uint32_t &CallLine, uint32_t &CallColumn) const;
159
160  /// InlinedChain - represents a chain of inlined_subroutine
161  /// DIEs, (possibly ending with subprogram DIE), all of which are contained
162  /// in some concrete inlined instance tree. Address range for each DIE
163  /// (except the last DIE) in this chain is contained in address
164  /// range for next DIE in the chain.
165  typedef SmallVector<DWARFDebugInfoEntryMinimal, 4> InlinedChain;
166
167  /// Get inlined chain for a given address, rooted at the current DIE.
168  /// Returns empty chain if address is not contained in address range
169  /// of current DIE.
170  InlinedChain getInlinedChainForAddress(const DWARFCompileUnit *CU,
171                                         const uint64_t Address) const;
172};
173
174}
175
176#endif
177