LLVM  10.0.0svn
DWARFDebugLine.h
Go to the documentation of this file.
1 //===- DWARFDebugLine.h -----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_DEBUGINFO_DWARFDEBUGLINE_H
10 #define LLVM_DEBUGINFO_DWARFDEBUGLINE_H
11 
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/MD5.h"
21 #include "llvm/Support/Path.h"
22 #include <cstdint>
23 #include <map>
24 #include <string>
25 #include <vector>
26 
27 namespace llvm {
28 
29 class DWARFUnit;
30 class raw_ostream;
31 
33 public:
34  struct FileNameEntry {
35  FileNameEntry() = default;
36 
38  uint64_t DirIdx = 0;
39  uint64_t ModTime = 0;
40  uint64_t Length = 0;
43  };
44 
45  /// Tracks which optional content types are present in a DWARF file name
46  /// entry format.
48  ContentTypeTracker() = default;
49 
50  /// Whether filename entries provide a modification timestamp.
51  bool HasModTime = false;
52  /// Whether filename entries provide a file size.
53  bool HasLength = false;
54  /// For v5, whether filename entries provide an MD5 checksum.
55  bool HasMD5 = false;
56  /// For v5, whether filename entries provide source text.
57  bool HasSource = false;
58 
59  /// Update tracked content types with \p ContentType.
60  void trackContentType(dwarf::LineNumberEntryFormat ContentType);
61  };
62 
63  struct Prologue {
64  Prologue();
65 
66  /// The size in bytes of the statement information for this compilation unit
67  /// (not including the total_length field itself).
68  uint64_t TotalLength;
69  /// Version, address size (starting in v5), and DWARF32/64 format; these
70  /// parameters affect interpretation of forms (used in the directory and
71  /// file tables starting with v5).
73  /// The number of bytes following the prologue_length field to the beginning
74  /// of the first byte of the statement program itself.
75  uint64_t PrologueLength;
76  /// In v5, size in bytes of a segment selector.
77  uint8_t SegSelectorSize;
78  /// The size in bytes of the smallest target machine instruction. Statement
79  /// program opcodes that alter the address register first multiply their
80  /// operands by this value.
81  uint8_t MinInstLength;
82  /// The maximum number of individual operations that may be encoded in an
83  /// instruction.
84  uint8_t MaxOpsPerInst;
85  /// The initial value of theis_stmtregister.
86  uint8_t DefaultIsStmt;
87  /// This parameter affects the meaning of the special opcodes. See below.
88  int8_t LineBase;
89  /// This parameter affects the meaning of the special opcodes. See below.
90  uint8_t LineRange;
91  /// The number assigned to the first special opcode.
92  uint8_t OpcodeBase;
93  /// This tracks which optional file format content types are present.
95  std::vector<uint8_t> StandardOpcodeLengths;
96  std::vector<DWARFFormValue> IncludeDirectories;
97  std::vector<FileNameEntry> FileNames;
98 
99  const dwarf::FormParams getFormParams() const { return FormParams; }
100  uint16_t getVersion() const { return FormParams.Version; }
101  uint8_t getAddressSize() const { return FormParams.AddrSize; }
102  bool isDWARF64() const { return FormParams.Format == dwarf::DWARF64; }
103 
104  uint32_t sizeofTotalLength() const { return isDWARF64() ? 12 : 4; }
105 
106  uint32_t sizeofPrologueLength() const { return isDWARF64() ? 8 : 4; }
107 
108  bool totalLengthIsValid() const;
109 
110  /// Length of the prologue in bytes.
111  uint32_t getLength() const {
112  return PrologueLength + sizeofTotalLength() + sizeof(getVersion()) +
113  sizeofPrologueLength();
114  }
115 
116  /// Length of the line table data in bytes (not including the prologue).
118  return TotalLength + sizeofTotalLength() - getLength();
119  }
120 
122  return LineBase + (int8_t)LineRange - 1;
123  }
124 
125  /// Get DWARF-version aware access to the file name entry at the provided
126  /// index.
128  getFileNameEntry(uint64_t Index) const;
129 
130  bool hasFileAtIndex(uint64_t FileIndex) const;
131 
132  bool
133  getFileNameByIndex(uint64_t FileIndex, StringRef CompDir,
135  std::string &Result,
137 
138  void clear();
139  void dump(raw_ostream &OS, DIDumpOptions DumpOptions) const;
140  Error parse(const DWARFDataExtractor &DebugLineData, uint64_t *OffsetPtr,
141  const DWARFContext &Ctx, const DWARFUnit *U = nullptr);
142  };
143 
144  /// Standard .debug_line state machine structure.
145  struct Row {
146  explicit Row(bool DefaultIsStmt = false);
147 
148  /// Called after a row is appended to the matrix.
149  void postAppend();
150  void reset(bool DefaultIsStmt);
151  void dump(raw_ostream &OS) const;
152 
153  static void dumpTableHeader(raw_ostream &OS);
154 
155  static bool orderByAddress(const Row &LHS, const Row &RHS) {
156  return std::tie(LHS.Address.SectionIndex, LHS.Address.Address) <
157  std::tie(RHS.Address.SectionIndex, RHS.Address.Address);
158  }
159 
160  /// The program-counter value corresponding to a machine instruction
161  /// generated by the compiler and section index pointing to the section
162  /// containg this PC. If relocation information is present then section
163  /// index is the index of the section which contains above address.
164  /// Otherwise this is object::SectionedAddress::Undef value.
166  /// An unsigned integer indicating a source line number. Lines are numbered
167  /// beginning at 1. The compiler may emit the value 0 in cases where an
168  /// instruction cannot be attributed to any source line.
170  /// An unsigned integer indicating a column number within a source line.
171  /// Columns are numbered beginning at 1. The value 0 is reserved to indicate
172  /// that a statement begins at the 'left edge' of the line.
173  uint16_t Column;
174  /// An unsigned integer indicating the identity of the source file
175  /// corresponding to a machine instruction.
176  uint16_t File;
177  /// An unsigned integer representing the DWARF path discriminator value
178  /// for this location.
180  /// An unsigned integer whose value encodes the applicable instruction set
181  /// architecture for the current instruction.
182  uint8_t Isa;
183  /// A boolean indicating that the current instruction is the beginning of a
184  /// statement.
185  uint8_t IsStmt : 1,
186  /// A boolean indicating that the current instruction is the
187  /// beginning of a basic block.
188  BasicBlock : 1,
189  /// A boolean indicating that the current address is that of the
190  /// first byte after the end of a sequence of target machine
191  /// instructions.
192  EndSequence : 1,
193  /// A boolean indicating that the current address is one (of possibly
194  /// many) where execution should be suspended for an entry breakpoint
195  /// of a function.
196  PrologueEnd : 1,
197  /// A boolean indicating that the current address is one (of possibly
198  /// many) where execution should be suspended for an exit breakpoint
199  /// of a function.
200  EpilogueBegin : 1;
201  };
202 
203  /// Represents a series of contiguous machine instructions. Line table for
204  /// each compilation unit may consist of multiple sequences, which are not
205  /// guaranteed to be in the order of ascending instruction address.
206  struct Sequence {
207  Sequence();
208 
209  /// Sequence describes instructions at address range [LowPC, HighPC)
210  /// and is described by line table rows [FirstRowIndex, LastRowIndex).
211  uint64_t LowPC;
212  uint64_t HighPC;
213  /// If relocation information is present then this is the index of the
214  /// section which contains above addresses. Otherwise this is
215  /// object::SectionedAddress::Undef value.
216  uint64_t SectionIndex;
217  unsigned FirstRowIndex;
218  unsigned LastRowIndex;
219  bool Empty;
220 
221  void reset();
222 
223  static bool orderByHighPC(const Sequence &LHS, const Sequence &RHS) {
224  return std::tie(LHS.SectionIndex, LHS.HighPC) <
225  std::tie(RHS.SectionIndex, RHS.HighPC);
226  }
227 
228  bool isValid() const {
229  return !Empty && (LowPC < HighPC) && (FirstRowIndex < LastRowIndex);
230  }
231 
233  return SectionIndex == PC.SectionIndex &&
234  (LowPC <= PC.Address && PC.Address < HighPC);
235  }
236  };
237 
238  struct LineTable {
239  LineTable();
240 
241  /// Represents an invalid row
242  const uint32_t UnknownRowIndex = UINT32_MAX;
243 
244  void appendRow(const DWARFDebugLine::Row &R) { Rows.push_back(R); }
245 
247  Sequences.push_back(S);
248  }
249 
250  /// Returns the index of the row with file/line info for a given address,
251  /// or UnknownRowIndex if there is no such row.
252  uint32_t lookupAddress(object::SectionedAddress Address) const;
253 
254  bool lookupAddressRange(object::SectionedAddress Address, uint64_t Size,
255  std::vector<uint32_t> &Result) const;
256 
257  bool hasFileAtIndex(uint64_t FileIndex) const {
258  return Prologue.hasFileAtIndex(FileIndex);
259  }
260 
261  /// Extracts filename by its index in filename table in prologue.
262  /// In Dwarf 4, the files are 1-indexed and the current compilation file
263  /// name is not represented in the list. In DWARF v5, the files are
264  /// 0-indexed and the primary source file has the index 0.
265  /// Returns true on success.
266  bool getFileNameByIndex(uint64_t FileIndex, StringRef CompDir,
268  std::string &Result) const {
269  return Prologue.getFileNameByIndex(FileIndex, CompDir, Kind, Result);
270  }
271 
272  /// Fills the Result argument with the file and line information
273  /// corresponding to Address. Returns true on success.
274  bool getFileLineInfoForAddress(object::SectionedAddress Address,
275  const char *CompDir,
277  DILineInfo &Result) const;
278 
279  void dump(raw_ostream &OS, DIDumpOptions DumpOptions) const;
280  void clear();
281 
282  /// Parse prologue and all rows.
283  Error parse(
284  DWARFDataExtractor &DebugLineData, uint64_t *OffsetPtr,
285  const DWARFContext &Ctx, const DWARFUnit *U,
286  std::function<void(Error)> RecoverableErrorCallback,
287  raw_ostream *OS = nullptr);
288 
289  using RowVector = std::vector<Row>;
290  using RowIter = RowVector::const_iterator;
291  using SequenceVector = std::vector<Sequence>;
292  using SequenceIter = SequenceVector::const_iterator;
293 
297 
298  private:
299  uint32_t findRowInSeq(const DWARFDebugLine::Sequence &Seq,
300  object::SectionedAddress Address) const;
302  getSourceByIndex(uint64_t FileIndex,
304 
305  uint32_t lookupAddressImpl(object::SectionedAddress Address) const;
306 
307  bool lookupAddressRangeImpl(object::SectionedAddress Address, uint64_t Size,
308  std::vector<uint32_t> &Result) const;
309  };
310 
311  const LineTable *getLineTable(uint64_t Offset) const;
313  DWARFDataExtractor &DebugLineData, uint64_t Offset,
314  const DWARFContext &Ctx, const DWARFUnit *U,
315  std::function<void(Error)> RecoverableErrorCallback);
316 
317  /// Helper to allow for parsing of an entire .debug_line section in sequence.
319  public:
322  using LineToUnitMap = std::map<uint64_t, DWARFUnit *>;
323 
325  tu_range TUs);
326 
327  /// Get the next line table from the section. Report any issues via the
328  /// callbacks.
329  ///
330  /// \param RecoverableErrorCallback - any issues that don't prevent further
331  /// parsing of the table will be reported through this callback.
332  /// \param UnrecoverableErrorCallback - any issues that prevent further
333  /// parsing of the table will be reported through this callback.
334  /// \param OS - if not null, the parser will print information about the
335  /// table as it parses it.
336  LineTable
337  parseNext(
338  function_ref<void(Error)> RecoverableErrorCallback,
339  function_ref<void(Error)> UnrecoverableErrorCallback,
340  raw_ostream *OS = nullptr);
341 
342  /// Skip the current line table and go to the following line table (if
343  /// present) immediately.
344  ///
345  /// \param ErrorCallback - report any prologue parsing issues via this
346  /// callback.
347  void skip(function_ref<void(Error)> ErrorCallback);
348 
349  /// Indicates if the parser has parsed as much as possible.
350  ///
351  /// \note Certain problems with the line table structure might mean that
352  /// parsing stops before the end of the section is reached.
353  bool done() const { return Done; }
354 
355  /// Get the offset the parser has reached.
356  uint64_t getOffset() const { return Offset; }
357 
358  private:
359  DWARFUnit *prepareToParse(uint64_t Offset);
360  void moveToNextTable(uint64_t OldOffset, const Prologue &P);
361 
362  LineToUnitMap LineToUnit;
363 
364  DWARFDataExtractor &DebugLineData;
365  const DWARFContext &Context;
366  uint64_t Offset = 0;
367  bool Done = false;
368  };
369 
370 private:
371  struct ParsingState {
372  ParsingState(struct LineTable *LT);
373 
374  void resetRowAndSequence();
375  void appendRowToMatrix();
376 
377  /// Line table we're currently parsing.
378  struct LineTable *LineTable;
379  struct Row Row;
380  struct Sequence Sequence;
381  };
382 
383  using LineTableMapTy = std::map<uint64_t, LineTable>;
384  using LineTableIter = LineTableMapTy::iterator;
385  using LineTableConstIter = LineTableMapTy::const_iterator;
386 
387  LineTableMapTy LineTableMap;
388 };
389 
390 } // end namespace llvm
391 
392 #endif // LLVM_DEBUGINFO_DWARFDEBUGLINE_H
SequenceVector::const_iterator SequenceIter
uint64_t CallInst * C
A helper struct providing information about the byte size of DW_FORM values that vary in size dependi...
Definition: Dwarf.h:536
uint64_t LowPC
Sequence describes instructions at address range [LowPC, HighPC) and is described by line table rows ...
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Various leaf nodes.
Definition: ISDOpcodes.h:59
std::vector< Sequence > SequenceVector
uint32_t sizeofTotalLength() const
static bool orderByAddress(const Row &LHS, const Row &RHS)
uint32_t getLength() const
Length of the prologue in bytes.
uint32_t getStatementTableLength() const
Length of the line table data in bytes (not including the prologue).
uint8_t MinInstLength
The size in bytes of the smallest target machine instruction.
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:104
uint32_t Line
An unsigned integer indicating a source line number.
bool getFileNameByIndex(uint64_t FileIndex, StringRef CompDir, DILineInfoSpecifier::FileLineInfoKind Kind, std::string &Result) const
Extracts filename by its index in filename table in prologue.
void skip(CollectionType &C)
Definition: YAMLParser.h:388
const dwarf::FormParams getFormParams() const
std::vector< DWARFFormValue > IncludeDirectories
uint64_t getOffset() const
Get the offset the parser has reached.
A format-neutral container for source line information.
Definition: DIContext.h:30
bool hasFileAtIndex(uint64_t FileIndex) const
object::SectionedAddress Address
The program-counter value corresponding to a machine instruction generated by the compiler and sectio...
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.
uint64_t SectionIndex
If relocation information is present then this is the index of the section which contains above addre...
dwarf::FormParams FormParams
Version, address size (starting in v5), and DWARF32/64 format; these parameters affect interpretation...
ContentTypeTracker ContentTypes
This tracks which optional file format content types are present.
Represents a series of contiguous machine instructions.
Expected< const LineTable * > getOrParseLineTable(DWARFDataExtractor &DebugLineData, uint64_t Offset, const DWARFContext &Ctx, const DWARFUnit *U, std::function< void(Error)> RecoverableErrorCallback)
uint32_t sizeofPrologueLength() const
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
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.
llvm::iterator_range< typename UnitVector::iterator > iterator_range
Definition: DWARFUnit.h:123
uint64_t TotalLength
The size in bytes of the statement information for this compilation unit (not including the total_len...
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:171
bool getFileNameByIndex(uint64_t FileIndex, StringRef CompDir, DILineInfoSpecifier::FileLineInfoKind Kind, std::string &Result, sys::path::Style Style=sys::path::Style::native) const
#define P(N)
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
uint8_t SegSelectorSize
In v5, size in bytes of a segment selector.
uint16_t File
An unsigned integer indicating the identity of the source file corresponding to a machine instruction...
uint64_t PrologueLength
The number of bytes following the prologue_length field to the beginning of the first byte of the sta...
bool hasFileAtIndex(uint64_t FileIndex) const
static bool orderByHighPC(const Sequence &LHS, const Sequence &RHS)
int32_t getMaxLineIncrementForSpecialOpcode() 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.
std::map< uint64_t, DWARFUnit * > LineToUnitMap
End of the line table.
Definition: LineTable.cpp:17
void appendRow(const DWARFDebugLine::Row &R)
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:58
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
A range adaptor for a pair of iterators.
const LineTable * getLineTable(uint64_t Offset) const
static void clear(coro::Shape &Shape)
Definition: Coroutines.cpp:225
Tracks which optional content types are present in a DWARF file name entry format.
std::vector< FileNameEntry > FileNames
uint32_t Discriminator
An unsigned integer representing the DWARF path discriminator value for this location.
RowVector::const_iterator RowIter
Sequence
A sequence of states that a pointer may go through in which an objc_retain and objc_release are actua...
Definition: PtrState.h:40
uint32_t Size
Definition: Profile.cpp:46
uint8_t DefaultIsStmt
The initial value of theis_stmtregister.
DwarfFormat Format
Definition: Dwarf.h:539
bool containsPC(object::SectionedAddress PC) const
int8_t LineBase
This parameter affects the meaning of the special opcodes. See below.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
print Print MemDeps of function
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
static llvm::Error parse(DataExtractor &Data, uint64_t BaseAddr, LineEntryCallback const &Callback)
Definition: LineTable.cpp:54
LineNumberEntryFormat
Definition: Dwarf.h:287
Helper to allow for parsing of an entire .debug_line section in sequence.
bool done() const
Indicates if the parser has parsed as much as possible.