LLVM  4.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_LIB_DEBUGINFO_DWARFDEBUGLINE_H
11 #define LLVM_LIB_DEBUGINFO_DWARFDEBUGLINE_H
12 
16 #include <map>
17 #include <string>
18 #include <vector>
19 
20 namespace llvm {
21 
22 class raw_ostream;
23 
25 public:
26  DWARFDebugLine(const RelocAddrMap* LineInfoRelocMap) : RelocMap(LineInfoRelocMap) {}
27  struct FileNameEntry {
28  FileNameEntry() : Name(nullptr), DirIdx(0), ModTime(0), Length(0) {}
29 
30  const char *Name;
31  uint64_t DirIdx;
32  uint64_t ModTime;
33  uint64_t Length;
34  };
35 
36  struct Prologue {
37  Prologue();
38 
39  // The size in bytes of the statement information for this compilation unit
40  // (not including the total_length field itself).
41  uint64_t TotalLength;
42  // Version identifier for the statement information format.
43  uint16_t Version;
44  // The number of bytes following the prologue_length field to the beginning
45  // of the first byte of the statement program itself.
46  uint64_t PrologueLength;
47  // The size in bytes of the smallest target machine instruction. Statement
48  // program opcodes that alter the address register first multiply their
49  // operands by this value.
50  uint8_t MinInstLength;
51  // The maximum number of individual operations that may be encoded in an
52  // instruction.
53  uint8_t MaxOpsPerInst;
54  // The initial value of theis_stmtregister.
55  uint8_t DefaultIsStmt;
56  // This parameter affects the meaning of the special opcodes. See below.
57  int8_t LineBase;
58  // This parameter affects the meaning of the special opcodes. See below.
59  uint8_t LineRange;
60  // The number assigned to the first special opcode.
61  uint8_t OpcodeBase;
62  std::vector<uint8_t> StandardOpcodeLengths;
63  std::vector<const char*> IncludeDirectories;
64  std::vector<FileNameEntry> FileNames;
65 
66  bool IsDWARF64;
68  return IsDWARF64 ? 12 : 4;
69  }
71  return IsDWARF64 ? 8 : 4;
72  }
73 
74  // Length of the prologue in bytes.
75  uint32_t getLength() const {
76  return PrologueLength + sizeofTotalLength() + sizeof(Version) +
78  }
79  // Length of the line table data in bytes (not including the prologue).
82  }
84  return LineBase + (int8_t)LineRange - 1;
85  }
86 
87  void clear();
88  void dump(raw_ostream &OS) const;
89  bool parse(DataExtractor debug_line_data, uint32_t *offset_ptr);
90  };
91 
92  // Standard .debug_line state machine structure.
93  struct Row {
94  explicit Row(bool default_is_stmt = false);
95 
96  /// Called after a row is appended to the matrix.
97  void postAppend();
98  void reset(bool default_is_stmt);
99  void dump(raw_ostream &OS) const;
100 
101  static bool orderByAddress(const Row& LHS, const Row& RHS) {
102  return LHS.Address < RHS.Address;
103  }
104 
105  // The program-counter value corresponding to a machine instruction
106  // generated by the compiler.
107  uint64_t Address;
108  // An unsigned integer indicating a source line number. Lines are numbered
109  // beginning at 1. The compiler may emit the value 0 in cases where an
110  // instruction cannot be attributed to any source line.
112  // An unsigned integer indicating a column number within a source line.
113  // Columns are numbered beginning at 1. The value 0 is reserved to indicate
114  // that a statement begins at the 'left edge' of the line.
115  uint16_t Column;
116  // An unsigned integer indicating the identity of the source file
117  // corresponding to a machine instruction.
118  uint16_t File;
119  // An unsigned integer representing the DWARF path discriminator value
120  // for this location.
122  // An unsigned integer whose value encodes the applicable instruction set
123  // architecture for the current instruction.
124  uint8_t Isa;
125  // A boolean indicating that the current instruction is the beginning of a
126  // statement.
127  uint8_t IsStmt:1,
128  // A boolean indicating that the current instruction is the
129  // beginning of a basic block.
130  BasicBlock:1,
131  // A boolean indicating that the current address is that of the
132  // first byte after the end of a sequence of target machine
133  // instructions.
134  EndSequence:1,
135  // A boolean indicating that the current address is one (of possibly
136  // many) where execution should be suspended for an entry breakpoint
137  // of a function.
138  PrologueEnd:1,
139  // A boolean indicating that the current address is one (of possibly
140  // many) where execution should be suspended for an exit breakpoint
141  // of a function.
142  EpilogueBegin:1;
143  };
144 
145  // Represents a series of contiguous machine instructions. Line table for each
146  // compilation unit may consist of multiple sequences, which are not
147  // guaranteed to be in the order of ascending instruction address.
148  struct Sequence {
149  // Sequence describes instructions at address range [LowPC, HighPC)
150  // and is described by line table rows [FirstRowIndex, LastRowIndex).
151  uint64_t LowPC;
152  uint64_t HighPC;
153  unsigned FirstRowIndex;
154  unsigned LastRowIndex;
155  bool Empty;
156 
157  Sequence();
158  void reset();
159 
160  static bool orderByLowPC(const Sequence& LHS, const Sequence& RHS) {
161  return LHS.LowPC < RHS.LowPC;
162  }
163  bool isValid() const {
164  return !Empty && (LowPC < HighPC) && (FirstRowIndex < LastRowIndex);
165  }
166  bool containsPC(uint64_t pc) const {
167  return (LowPC <= pc && pc < HighPC);
168  }
169  };
170 
171  struct LineTable {
172  LineTable();
173 
174  // Represents an invalid row
175  const uint32_t UnknownRowIndex = UINT32_MAX;
176 
178  Rows.push_back(R);
179  }
181  Sequences.push_back(S);
182  }
183 
184  // Returns the index of the row with file/line info for a given address,
185  // or UnknownRowIndex if there is no such row.
186  uint32_t lookupAddress(uint64_t address) const;
187 
188  bool lookupAddressRange(uint64_t address, uint64_t size,
189  std::vector<uint32_t> &result) const;
190 
191  bool hasFileAtIndex(uint64_t FileIndex) const;
192 
193  // Extracts filename by its index in filename table in prologue.
194  // Returns true on success.
195  bool getFileNameByIndex(uint64_t FileIndex, const char *CompDir,
197  std::string &Result) const;
198 
199  // Fills the Result argument with the file and line information
200  // corresponding to Address. Returns true on success.
201  bool getFileLineInfoForAddress(uint64_t Address, const char *CompDir,
203  DILineInfo &Result) const;
204 
205  void dump(raw_ostream &OS) const;
206  void clear();
207 
208  /// Parse prologue and all rows.
209  bool parse(DataExtractor debug_line_data, const RelocAddrMap *RMap,
210  uint32_t *offset_ptr);
211 
213  typedef std::vector<Row> RowVector;
214  typedef RowVector::const_iterator RowIter;
215  typedef std::vector<Sequence> SequenceVector;
216  typedef SequenceVector::const_iterator SequenceIter;
219 
220  private:
221  uint32_t findRowInSeq(const DWARFDebugLine::Sequence &seq,
222  uint64_t address) const;
223  };
224 
225  const LineTable *getLineTable(uint32_t offset) const;
226  const LineTable *getOrParseLineTable(DataExtractor debug_line_data,
227  uint32_t offset);
228 
229 private:
230  struct ParsingState {
231  ParsingState(struct LineTable *LT);
232 
233  void resetRowAndSequence();
234  void appendRowToMatrix(uint32_t offset);
235 
236  // Line table we're currently parsing.
237  struct LineTable *LineTable;
238  // The row number that starts at zero for the prologue, and increases for
239  // each row added to the matrix.
240  unsigned RowNumber;
241  struct Row Row;
242  struct Sequence Sequence;
243  };
244 
245  typedef std::map<uint32_t, LineTable> LineTableMapTy;
246  typedef LineTableMapTy::iterator LineTableIter;
247  typedef LineTableMapTy::const_iterator LineTableConstIter;
248 
249  const RelocAddrMap *RelocMap;
250  LineTableMapTy LineTableMap;
251 };
252 }
253 
254 #endif
DWARFDebugLine(const RelocAddrMap *LineInfoRelocMap)
RowVector::const_iterator RowIter
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
SequenceVector::const_iterator SequenceIter
const LineTable * getLineTable(uint32_t offset) const
uint32_t getStatementTableLength() const
DILineInfo - a format-neutral container for source line information.
Definition: DIContext.h:32
void postAppend()
Called after a row is appended to the matrix.
Row(bool default_is_stmt=false)
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
uint32_t sizeofPrologueLength() const
void dump(raw_ostream &OS) const
void dump(raw_ostream &OS) const
bool parse(DataExtractor debug_line_data, uint32_t *offset_ptr)
int32_t getMaxLineIncrementForSpecialOpcode() const
void dump(raw_ostream &OS) const
bool parse(DataExtractor debug_line_data, const RelocAddrMap *RMap, uint32_t *offset_ptr)
Parse prologue and all rows.
const LineTable * getOrParseLineTable(DataExtractor debug_line_data, uint32_t offset)
bool lookupAddressRange(uint64_t address, uint64_t size, std::vector< uint32_t > &result) const
void appendSequence(const DWARFDebugLine::Sequence &S)
uint32_t lookupAddress(uint64_t address) const
std::vector< const char * > IncludeDirectories
bool hasFileAtIndex(uint64_t FileIndex) const
uint32_t sizeofTotalLength() const
void appendRow(const DWARFDebugLine::Row &R)
iterator_range< detail::value_sequence_iterator< ValueT > > seq(ValueT Begin, ValueT End)
Definition: Sequence.h:71
std::vector< uint8_t > StandardOpcodeLengths
void reset(bool default_is_stmt)
bool containsPC(uint64_t pc) const
std::vector< FileNameEntry > FileNames
static bool orderByLowPC(const Sequence &LHS, const Sequence &RHS)
bool getFileLineInfoForAddress(uint64_t Address, const char *CompDir, DILineInfoSpecifier::FileLineInfoKind Kind, DILineInfo &Result) const
const unsigned Kind
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44