1 //===-- DWARFDebugLine.h ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_LIB_DEBUGINFO_DWARFDEBUGLINE_H
11 #define LLVM_LIB_DEBUGINFO_DWARFDEBUGLINE_H
13 #include "llvm/DebugInfo/DIContext.h"
14 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
15 #include "llvm/Support/DataExtractor.h"
24 class DWARFDebugLine {
26 DWARFDebugLine(const RelocAddrMap* LineInfoRelocMap) : RelocMap(LineInfoRelocMap) {}
27 struct FileNameEntry {
28 FileNameEntry() : Name(nullptr), DirIdx(0), ModTime(0), Length(0) {}
39 // The size in bytes of the statement information for this compilation unit
40 // (not including the total_length field itself).
42 // Version identifier for the statement information format.
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
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.
58 // This parameter affects the meaning of the special opcodes. See below.
60 // The number assigned to the first special opcode.
62 std::vector<uint8_t> StandardOpcodeLengths;
63 std::vector<const char*> IncludeDirectories;
64 std::vector<FileNameEntry> FileNames;
67 uint32_t sizeofTotalLength() const {
68 return IsDWARF64 ? 12 : 4;
70 uint32_t sizeofPrologueLength() const {
71 return IsDWARF64 ? 8 : 4;
74 // Length of the prologue in bytes.
75 uint32_t getLength() const {
76 return PrologueLength + sizeofTotalLength() + sizeof(Version) +
77 sizeofPrologueLength();
79 // Length of the line table data in bytes (not including the prologue).
80 uint32_t getStatementTableLength() const {
81 return TotalLength + sizeofTotalLength() - getLength();
83 int32_t getMaxLineIncrementForSpecialOpcode() const {
84 return LineBase + (int8_t)LineRange - 1;
88 void dump(raw_ostream &OS) const;
89 bool parse(DataExtractor debug_line_data, uint32_t *offset_ptr);
92 // Standard .debug_line state machine structure.
94 explicit Row(bool default_is_stmt = false);
96 /// Called after a row is appended to the matrix.
98 void reset(bool default_is_stmt);
99 void dump(raw_ostream &OS) const;
101 static bool orderByAddress(const Row& LHS, const Row& RHS) {
102 return LHS.Address < RHS.Address;
105 // The program-counter value corresponding to a machine instruction
106 // generated by the compiler.
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.
116 // An unsigned integer indicating the identity of the source file
117 // corresponding to a machine instruction.
119 // An unsigned integer whose value encodes the applicable instruction set
120 // architecture for the current instruction.
122 // An unsigned integer representing the DWARF path discriminator value
123 // for this location.
124 uint32_t Discriminator;
125 // A boolean indicating that the current instruction is the beginning of a
128 // A boolean indicating that the current instruction is the
129 // beginning of a basic block.
131 // A boolean indicating that the current address is that of the
132 // first byte after the end of a sequence of target machine
135 // A boolean indicating that the current address is one (of possibly
136 // many) where execution should be suspended for an entry breakpoint
139 // A boolean indicating that the current address is one (of possibly
140 // many) where execution should be suspended for an exit breakpoint
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.
149 // Sequence describes instructions at address range [LowPC, HighPC)
150 // and is described by line table rows [FirstRowIndex, LastRowIndex).
153 unsigned FirstRowIndex;
154 unsigned LastRowIndex;
160 static bool orderByLowPC(const Sequence& LHS, const Sequence& RHS) {
161 return LHS.LowPC < RHS.LowPC;
163 bool isValid() const {
164 return !Empty && (LowPC < HighPC) && (FirstRowIndex < LastRowIndex);
166 bool containsPC(uint64_t pc) const {
167 return (LowPC <= pc && pc < HighPC);
174 // Represents an invalid row
175 const uint32_t UnknownRowIndex = UINT32_MAX;
177 void appendRow(const DWARFDebugLine::Row &R) {
180 void appendSequence(const DWARFDebugLine::Sequence &S) {
181 Sequences.push_back(S);
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;
188 bool lookupAddressRange(uint64_t address, uint64_t size,
189 std::vector<uint32_t> &result) const;
191 // Extracts filename by its index in filename table in prologue.
192 // Returns true on success.
193 bool getFileNameByIndex(uint64_t FileIndex, const char *CompDir,
194 DILineInfoSpecifier::FileLineInfoKind Kind,
195 std::string &Result) const;
197 // Fills the Result argument with the file and line information
198 // corresponding to Address. Returns true on success.
199 bool getFileLineInfoForAddress(uint64_t Address, const char *CompDir,
200 DILineInfoSpecifier::FileLineInfoKind Kind,
201 DILineInfo &Result) const;
203 void dump(raw_ostream &OS) const;
206 /// Parse prologue and all rows.
207 bool parse(DataExtractor debug_line_data, const RelocAddrMap *RMap,
208 uint32_t *offset_ptr);
210 struct Prologue Prologue;
211 typedef std::vector<Row> RowVector;
212 typedef RowVector::const_iterator RowIter;
213 typedef std::vector<Sequence> SequenceVector;
214 typedef SequenceVector::const_iterator SequenceIter;
216 SequenceVector Sequences;
219 uint32_t findRowInSeq(const DWARFDebugLine::Sequence &seq,
220 uint64_t address) const;
223 const LineTable *getLineTable(uint32_t offset) const;
224 const LineTable *getOrParseLineTable(DataExtractor debug_line_data,
228 struct ParsingState {
229 ParsingState(struct LineTable *LT);
231 void resetRowAndSequence();
232 void appendRowToMatrix(uint32_t offset);
234 // Line table we're currently parsing.
235 struct LineTable *LineTable;
236 // The row number that starts at zero for the prologue, and increases for
237 // each row added to the matrix.
240 struct Sequence Sequence;
243 typedef std::map<uint32_t, LineTable> LineTableMapTy;
244 typedef LineTableMapTy::iterator LineTableIter;
245 typedef LineTableMapTy::const_iterator LineTableConstIter;
247 const RelocAddrMap *RelocMap;
248 LineTableMapTy LineTableMap;