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_DEBUGINFO_DWARFDEBUGLINE_H
11 #define LLVM_DEBUGINFO_DWARFDEBUGLINE_H
13 #include "DWARFRelocMap.h"
14 #include "llvm/Support/DataExtractor.h"
23 class DWARFDebugLine {
25 DWARFDebugLine(const RelocAddrMap* LineInfoRelocMap) : RelocMap(LineInfoRelocMap) {}
26 struct FileNameEntry {
27 FileNameEntry() : Name(nullptr), DirIdx(0), ModTime(0), Length(0) {}
37 : TotalLength(0), Version(0), PrologueLength(0), MinInstLength(0),
38 MaxOpsPerInst(0), DefaultIsStmt(0), LineBase(0), LineRange(0),
41 // The size in bytes of the statement information for this compilation unit
42 // (not including the total_length field itself).
44 // Version identifier for the statement information format.
46 // The number of bytes following the prologue_length field to the beginning
47 // of the first byte of the statement program itself.
48 uint32_t PrologueLength;
49 // The size in bytes of the smallest target machine instruction. Statement
50 // program opcodes that alter the address register first multiply their
51 // operands by this value.
52 uint8_t MinInstLength;
53 // The maximum number of individual operations that may be encoded in an
55 uint8_t MaxOpsPerInst;
56 // The initial value of theis_stmtregister.
57 uint8_t DefaultIsStmt;
58 // This parameter affects the meaning of the special opcodes. See below.
60 // This parameter affects the meaning of the special opcodes. See below.
62 // The number assigned to the first special opcode.
64 std::vector<uint8_t> StandardOpcodeLengths;
65 std::vector<const char*> IncludeDirectories;
66 std::vector<FileNameEntry> FileNames;
68 // Length of the prologue in bytes.
69 uint32_t getLength() const {
70 return PrologueLength + sizeof(TotalLength) + sizeof(Version) +
71 sizeof(PrologueLength);
73 // Length of the line table data in bytes (not including the prologue).
74 uint32_t getStatementTableLength() const {
75 return TotalLength + sizeof(TotalLength) - getLength();
77 int32_t getMaxLineIncrementForSpecialOpcode() const {
78 return LineBase + (int8_t)LineRange - 1;
80 void dump(raw_ostream &OS) const;
82 TotalLength = Version = PrologueLength = 0;
83 MinInstLength = LineBase = LineRange = OpcodeBase = 0;
84 StandardOpcodeLengths.clear();
85 IncludeDirectories.clear();
90 // Standard .debug_line state machine structure.
92 Row(bool default_is_stmt = false) { reset(default_is_stmt); }
93 /// Called after a row is appended to the matrix.
95 void reset(bool default_is_stmt);
96 void dump(raw_ostream &OS) const;
98 static bool orderByAddress(const Row& LHS, const Row& RHS) {
99 return LHS.Address < RHS.Address;
102 // The program-counter value corresponding to a machine instruction
103 // generated by the compiler.
105 // An unsigned integer indicating a source line number. Lines are numbered
106 // beginning at 1. The compiler may emit the value 0 in cases where an
107 // instruction cannot be attributed to any source line.
109 // An unsigned integer indicating a column number within a source line.
110 // Columns are numbered beginning at 1. The value 0 is reserved to indicate
111 // that a statement begins at the 'left edge' of the line.
113 // An unsigned integer indicating the identity of the source file
114 // corresponding to a machine instruction.
116 // An unsigned integer whose value encodes the applicable instruction set
117 // architecture for the current instruction.
119 // An unsigned integer representing the DWARF path discriminator value
120 // for this location.
121 uint32_t Discriminator;
122 // A boolean indicating that the current instruction is the beginning of a
125 // A boolean indicating that the current instruction is the
126 // beginning of a basic block.
128 // A boolean indicating that the current address is that of the
129 // first byte after the end of a sequence of target machine
132 // A boolean indicating that the current address is one (of possibly
133 // many) where execution should be suspended for an entry breakpoint
136 // A boolean indicating that the current address is one (of possibly
137 // many) where execution should be suspended for an exit breakpoint
142 // Represents a series of contiguous machine instructions. Line table for each
143 // compilation unit may consist of multiple sequences, which are not
144 // guaranteed to be in the order of ascending instruction address.
146 // Sequence describes instructions at address range [LowPC, HighPC)
147 // and is described by line table rows [FirstRowIndex, LastRowIndex).
150 unsigned FirstRowIndex;
151 unsigned LastRowIndex;
154 Sequence() { reset(); }
162 static bool orderByLowPC(const Sequence& LHS, const Sequence& RHS) {
163 return LHS.LowPC < RHS.LowPC;
165 bool isValid() const {
166 return !Empty && (LowPC < HighPC) && (FirstRowIndex < LastRowIndex);
168 bool containsPC(uint64_t pc) const {
169 return (LowPC <= pc && pc < HighPC);
174 void appendRow(const DWARFDebugLine::Row &state) { Rows.push_back(state); }
175 void appendSequence(const DWARFDebugLine::Sequence &sequence) {
176 Sequences.push_back(sequence);
184 // Returns the index of the row with file/line info for a given address,
185 // or -1 if there is no such row.
186 uint32_t lookupAddress(uint64_t address) const;
188 bool lookupAddressRange(uint64_t address,
190 std::vector<uint32_t>& result) const;
192 // Extracts filename by its index in filename table in prologue.
193 // Returns true on success.
194 bool getFileNameByIndex(uint64_t FileIndex,
195 bool NeedsAbsoluteFilePath,
196 std::string &Result) const;
198 void dump(raw_ostream &OS) const;
200 struct Prologue Prologue;
201 typedef std::vector<Row> RowVector;
202 typedef RowVector::const_iterator RowIter;
203 typedef std::vector<Sequence> SequenceVector;
204 typedef SequenceVector::const_iterator SequenceIter;
206 SequenceVector Sequences;
209 struct State : public Row, public Sequence, public LineTable {
210 // Special row codes.
212 StartParsingLineTable = 0,
213 DoneParsingLineTable = -1
216 State() : row(StartParsingLineTable) {}
219 virtual void appendRowToMatrix(uint32_t offset);
220 virtual void finalize();
221 virtual void reset() {
222 Row::reset(Prologue.DefaultIsStmt);
226 // The row number that starts at zero for the prologue, and increases for
227 // each row added to the matrix.
231 struct DumpingState : public State {
232 DumpingState(raw_ostream &OS) : OS(OS) {}
233 virtual ~DumpingState();
234 void finalize() override;
239 static bool parsePrologue(DataExtractor debug_line_data, uint32_t *offset_ptr,
241 /// Parse a single line table (prologue and all rows).
242 static bool parseStatementTable(DataExtractor debug_line_data,
243 const RelocAddrMap *RMap,
244 uint32_t *offset_ptr, State &state);
246 const LineTable *getLineTable(uint32_t offset) const;
247 const LineTable *getOrParseLineTable(DataExtractor debug_line_data,
251 typedef std::map<uint32_t, LineTable> LineTableMapTy;
252 typedef LineTableMapTy::iterator LineTableIter;
253 typedef LineTableMapTy::const_iterator LineTableConstIter;
255 const RelocAddrMap *RelocMap;
256 LineTableMapTy LineTableMap;