LLVM  5.0.0
DWARFDebugLine.h
Go to the documentation of this file.
1 //===- DWARFDebugLine.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_DWARFDEBUGLINE_H
11 #define LLVM_DEBUGINFO_DWARFDEBUGLINE_H
12 
13 #include "llvm/ADT/StringRef.h"
18 #include <cstdint>
19 #include <map>
20 #include <string>
21 #include <vector>
22 
23 namespace llvm {
24 
25 class raw_ostream;
26 
28 public:
29  struct FileNameEntry {
30  FileNameEntry() = default;
31 
33  uint64_t DirIdx = 0;
34  uint64_t ModTime = 0;
35  uint64_t Length = 0;
36  };
37 
38  struct Prologue {
39  Prologue();
40 
41  /// The size in bytes of the statement information for this compilation unit
42  /// (not including the total_length field itself).
43  uint64_t TotalLength;
44  /// Version, address size (starting in v5), and DWARF32/64 format; these
45  /// parameters affect interpretation of forms (used in the directory and
46  /// file tables starting with v5).
48  /// In v5, size in bytes of a segment selector.
49  uint8_t SegSelectorSize;
50  /// The number of bytes following the prologue_length field to the beginning
51  /// of the first byte of the statement program itself.
52  uint64_t PrologueLength;
53  /// The size in bytes of the smallest target machine instruction. Statement
54  /// program opcodes that alter the address register first multiply their
55  /// operands by this value.
56  uint8_t MinInstLength;
57  /// The maximum number of individual operations that may be encoded in an
58  /// instruction.
59  uint8_t MaxOpsPerInst;
60  /// The initial value of theis_stmtregister.
61  uint8_t DefaultIsStmt;
62  /// This parameter affects the meaning of the special opcodes. See below.
63  int8_t LineBase;
64  /// This parameter affects the meaning of the special opcodes. See below.
65  uint8_t LineRange;
66  /// The number assigned to the first special opcode.
67  uint8_t OpcodeBase;
68  std::vector<uint8_t> StandardOpcodeLengths;
69  std::vector<StringRef> IncludeDirectories;
70  std::vector<FileNameEntry> FileNames;
71 
72  const DWARFFormParams getFormParams() const { return FormParams; }
73  uint16_t getVersion() const { return FormParams.Version; }
74  uint8_t getAddressSize() const { return FormParams.AddrSize; }
75  bool isDWARF64() const { return FormParams.Format == dwarf::DWARF64; }
76 
77  uint32_t sizeofTotalLength() const { return isDWARF64() ? 12 : 4; }
78 
79  uint32_t sizeofPrologueLength() const { return isDWARF64() ? 8 : 4; }
80 
81  /// Length of the prologue in bytes.
82  uint32_t getLength() const {
83  return PrologueLength + sizeofTotalLength() + sizeof(getVersion()) +
85  }
86 
87  /// Length of the line table data in bytes (not including the prologue).
90  }
91 
93  return LineBase + (int8_t)LineRange - 1;
94  }
95 
96  void clear();
97  void dump(raw_ostream &OS) const;
98  bool parse(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr);
99  };
100 
101  /// Standard .debug_line state machine structure.
102  struct Row {
103  explicit Row(bool DefaultIsStmt = false);
104 
105  /// Called after a row is appended to the matrix.
106  void postAppend();
107  void reset(bool DefaultIsStmt);
108  void dump(raw_ostream &OS) const;
109 
110  static void dumpTableHeader(raw_ostream &OS);
111 
112  static bool orderByAddress(const Row &LHS, const Row &RHS) {
113  return LHS.Address < RHS.Address;
114  }
115 
116  /// The program-counter value corresponding to a machine instruction
117  /// generated by the compiler.
118  uint64_t Address;
119  /// An unsigned integer indicating a source line number. Lines are numbered
120  /// beginning at 1. The compiler may emit the value 0 in cases where an
121  /// instruction cannot be attributed to any source line.
123  /// An unsigned integer indicating a column number within a source line.
124  /// Columns are numbered beginning at 1. The value 0 is reserved to indicate
125  /// that a statement begins at the 'left edge' of the line.
126  uint16_t Column;
127  /// An unsigned integer indicating the identity of the source file
128  /// corresponding to a machine instruction.
129  uint16_t File;
130  /// An unsigned integer representing the DWARF path discriminator value
131  /// for this location.
133  /// An unsigned integer whose value encodes the applicable instruction set
134  /// architecture for the current instruction.
135  uint8_t Isa;
136  /// A boolean indicating that the current instruction is the beginning of a
137  /// statement.
138  uint8_t IsStmt : 1,
139  /// A boolean indicating that the current instruction is the
140  /// beginning of a basic block.
141  BasicBlock : 1,
142  /// A boolean indicating that the current address is that of the
143  /// first byte after the end of a sequence of target machine
144  /// instructions.
145  EndSequence : 1,
146  /// A boolean indicating that the current address is one (of possibly
147  /// many) where execution should be suspended for an entry breakpoint
148  /// of a function.
149  PrologueEnd : 1,
150  /// A boolean indicating that the current address is one (of possibly
151  /// many) where execution should be suspended for an exit breakpoint
152  /// of a function.
153  EpilogueBegin : 1;
154  };
155 
156  /// Represents a series of contiguous machine instructions. Line table for
157  /// each compilation unit may consist of multiple sequences, which are not
158  /// guaranteed to be in the order of ascending instruction address.
159  struct Sequence {
160  Sequence();
161 
162  /// Sequence describes instructions at address range [LowPC, HighPC)
163  /// and is described by line table rows [FirstRowIndex, LastRowIndex).
164  uint64_t LowPC;
165  uint64_t HighPC;
166  unsigned FirstRowIndex;
167  unsigned LastRowIndex;
168  bool Empty;
169 
170  void reset();
171 
172  static bool orderByLowPC(const Sequence &LHS, const Sequence &RHS) {
173  return LHS.LowPC < RHS.LowPC;
174  }
175 
176  bool isValid() const {
177  return !Empty && (LowPC < HighPC) && (FirstRowIndex < LastRowIndex);
178  }
179 
180  bool containsPC(uint64_t PC) const { return (LowPC <= PC && PC < HighPC); }
181  };
182 
183  struct LineTable {
184  LineTable();
185 
186  /// Represents an invalid row
187  const uint32_t UnknownRowIndex = UINT32_MAX;
188 
189  void appendRow(const DWARFDebugLine::Row &R) { Rows.push_back(R); }
190 
192  Sequences.push_back(S);
193  }
194 
195  /// Returns the index of the row with file/line info for a given address,
196  /// or UnknownRowIndex if there is no such row.
197  uint32_t lookupAddress(uint64_t Address) const;
198 
199  bool lookupAddressRange(uint64_t Address, uint64_t Size,
200  std::vector<uint32_t> &Result) const;
201 
202  bool hasFileAtIndex(uint64_t FileIndex) const;
203 
204  /// Extracts filename by its index in filename table in prologue.
205  /// Returns true on success.
206  bool getFileNameByIndex(uint64_t FileIndex, const char *CompDir,
208  std::string &Result) const;
209 
210  /// Fills the Result argument with the file and line information
211  /// corresponding to Address. Returns true on success.
212  bool getFileLineInfoForAddress(uint64_t Address, const char *CompDir,
214  DILineInfo &Result) const;
215 
216  void dump(raw_ostream &OS) const;
217  void clear();
218 
219  /// Parse prologue and all rows.
220  bool parse(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr);
221 
222  using RowVector = std::vector<Row>;
223  using RowIter = RowVector::const_iterator;
224  using SequenceVector = std::vector<Sequence>;
225  using SequenceIter = SequenceVector::const_iterator;
226 
230 
231  private:
232  uint32_t findRowInSeq(const DWARFDebugLine::Sequence &Seq,
233  uint64_t Address) const;
234  };
235 
236  const LineTable *getLineTable(uint32_t Offset) const;
237  const LineTable *getOrParseLineTable(const DWARFDataExtractor &DebugLineData,
238  uint32_t Offset);
239 
240 private:
241  struct ParsingState {
242  ParsingState(struct LineTable *LT);
243 
244  void resetRowAndSequence();
245  void appendRowToMatrix(uint32_t Offset);
246 
247  /// Line table we're currently parsing.
248  struct LineTable *LineTable;
249  /// The row number that starts at zero for the prologue, and increases for
250  /// each row added to the matrix.
251  unsigned RowNumber = 0;
252  struct Row Row;
253  struct Sequence Sequence;
254  };
255 
256  using LineTableMapTy = std::map<uint32_t, LineTable>;
257  using LineTableIter = LineTableMapTy::iterator;
258  using LineTableConstIter = LineTableMapTy::const_iterator;
259 
260  LineTableMapTy LineTableMap;
261 };
262 
263 } // end namespace llvm
264 
265 #endif // LLVM_DEBUGINFO_DWARFDEBUGLINE_H
SequenceVector::const_iterator SequenceIter
uint64_t LowPC
Sequence describes instructions at address range [LowPC, HighPC) and is described by line table rows ...
const LineTable * getLineTable(uint32_t Offset) const
Row(bool DefaultIsStmt=false)
bool parse(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr)
Parse prologue and all rows.
std::vector< Sequence > SequenceVector
static bool orderByAddress(const Row &LHS, const Row &RHS)
bool getFileNameByIndex(uint64_t FileIndex, const char *CompDir, DILineInfoSpecifier::FileLineInfoKind Kind, std::string &Result) const
Extracts filename by its index in filename table in prologue.
uint8_t MinInstLength
The size in bytes of the smallest target machine instruction.
uint32_t Line
An unsigned integer indicating a source line number.
uint32_t getStatementTableLength() const
Length of the line table data in bytes (not including the prologue).
dwarf::DwarfFormat Format
DILineInfo - a format-neutral container for source line information.
Definition: DIContext.h:32
uint8_t Isa
An unsigned integer whose value encodes the applicable instruction set architecture for the current i...
uint8_t MaxOpsPerInst
The maximum number of individual operations that may be encoded in an instruction.
Represents a series of contiguous machine instructions.
uint64_t Address
The program-counter value corresponding to a machine instruction generated by the compiler...
void postAppend()
Called after a row is appended to the matrix.
uint8_t LineRange
This parameter affects the meaning of the special opcodes. See below.
uint8_t OpcodeBase
The number assigned to the first special opcode.
void reset(bool DefaultIsStmt)
const DWARFFormParams getFormParams() const
bool parse(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr)
uint64_t TotalLength
The size in bytes of the statement information for this compilation unit (not including the total_len...
uint8_t IsStmt
A boolean indicating that the current instruction is the beginning of a statement.
uint8_t EndSequence
A boolean indicating that the current address is that of the first byte after the end of a sequence o...
uint8_t SegSelectorSize
In v5, size in bytes of a segment selector.
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
uint16_t File
An unsigned integer indicating the identity of the source file corresponding to a machine instruction...
uint32_t sizeofPrologueLength() const
uint64_t PrologueLength
The number of bytes following the prologue_length field to the beginning of the first byte of the sta...
DWARFFormParams FormParams
Version, address size (starting in v5), and DWARF32/64 format; these parameters affect interpretation...
void dump(raw_ostream &OS) const
void dump(raw_ostream &OS) const
int32_t getMaxLineIncrementForSpecialOpcode() const
void dump(raw_ostream &OS) const
void appendSequence(const DWARFDebugLine::Sequence &S)
Standard .debug_line state machine structure.
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
uint16_t Column
An unsigned integer indicating a column number within a source line.
bool hasFileAtIndex(uint64_t FileIndex) const
uint32_t sizeofTotalLength() const
void appendRow(const DWARFDebugLine::Row &R)
bool lookupAddressRange(uint64_t Address, uint64_t Size, std::vector< uint32_t > &Result) const
A helper struct for DWARFFormValue methods, providing information that allows it to know the byte siz...
static void dumpTableHeader(raw_ostream &OS)
uint8_t PrologueEnd
A boolean indicating that the current address is one (of possibly many) where execution should be sus...
std::vector< uint8_t > StandardOpcodeLengths
const uint32_t UnknownRowIndex
Represents an invalid row.
uint32_t getLength() const
Length of the prologue in bytes.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
std::vector< FileNameEntry > FileNames
uint32_t lookupAddress(uint64_t Address) const
Returns the index of the row with file/line info for a given address, or UnknownRowIndex if there is ...
uint32_t Discriminator
An unsigned integer representing the DWARF path discriminator value for this location.
RowVector::const_iterator RowIter
static bool orderByLowPC(const Sequence &LHS, const Sequence &RHS)
bool getFileLineInfoForAddress(uint64_t Address, const char *CompDir, DILineInfoSpecifier::FileLineInfoKind Kind, DILineInfo &Result) const
Fills the Result argument with the file and line information corresponding to Address.
uint8_t DefaultIsStmt
The initial value of theis_stmtregister.
const unsigned Kind
int8_t LineBase
This parameter affects the meaning of the special opcodes. See below.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
bool containsPC(uint64_t PC) const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
uint8_t EpilogueBegin
A boolean indicating that the current address is one (of possibly many) where execution should be sus...
const LineTable * getOrParseLineTable(const DWARFDataExtractor &DebugLineData, uint32_t Offset)
std::vector< StringRef > IncludeDirectories