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(0), DirIdx(0), ModTime(0), Length(0) {}
37 : TotalLength(0), Version(0), PrologueLength(0), MinInstLength(0),
38 DefaultIsStmt(0), LineBase(0), LineRange(0), OpcodeBase(0) {}
40 // The size in bytes of the statement information for this compilation unit
41 // (not including the total_length field itself).
43 // Version identifier for the statement information format.
45 // The number of bytes following the prologue_length field to the beginning
46 // of the first byte of the statement program itself.
47 uint32_t PrologueLength;
48 // The size in bytes of the smallest target machine instruction. Statement
49 // program opcodes that alter the address register first multiply their
50 // operands by this value.
51 uint8_t MinInstLength;
52 // The initial value of theis_stmtregister.
53 uint8_t DefaultIsStmt;
54 // This parameter affects the meaning of the special opcodes. See below.
56 // This parameter affects the meaning of the special opcodes. See below.
58 // The number assigned to the first special opcode.
60 std::vector<uint8_t> StandardOpcodeLengths;
61 std::vector<const char*> IncludeDirectories;
62 std::vector<FileNameEntry> FileNames;
64 // Length of the prologue in bytes.
65 uint32_t getLength() const {
66 return PrologueLength + sizeof(TotalLength) + sizeof(Version) +
67 sizeof(PrologueLength);
69 // Length of the line table data in bytes (not including the prologue).
70 uint32_t getStatementTableLength() const {
71 return TotalLength + sizeof(TotalLength) - getLength();
73 int32_t getMaxLineIncrementForSpecialOpcode() const {
74 return LineBase + (int8_t)LineRange - 1;
76 void dump(raw_ostream &OS) const;
78 TotalLength = Version = PrologueLength = 0;
79 MinInstLength = LineBase = LineRange = OpcodeBase = 0;
80 StandardOpcodeLengths.clear();
81 IncludeDirectories.clear();
86 // Standard .debug_line state machine structure.
88 Row(bool default_is_stmt = false) { reset(default_is_stmt); }
89 /// Called after a row is appended to the matrix.
91 void reset(bool default_is_stmt);
92 void dump(raw_ostream &OS) const;
94 static bool orderByAddress(const Row& LHS, const Row& RHS) {
95 return LHS.Address < RHS.Address;
98 // The program-counter value corresponding to a machine instruction
99 // generated by the compiler.
101 // An unsigned integer indicating a source line number. Lines are numbered
102 // beginning at 1. The compiler may emit the value 0 in cases where an
103 // instruction cannot be attributed to any source line.
105 // An unsigned integer indicating a column number within a source line.
106 // Columns are numbered beginning at 1. The value 0 is reserved to indicate
107 // that a statement begins at the 'left edge' of the line.
109 // An unsigned integer indicating the identity of the source file
110 // corresponding to a machine instruction.
112 // An unsigned integer whose value encodes the applicable instruction set
113 // architecture for the current instruction.
115 // A boolean indicating that the current instruction is the beginning of a
118 // A boolean indicating that the current instruction is the
119 // beginning of a basic block.
121 // A boolean indicating that the current address is that of the
122 // first byte after the end of a sequence of target machine
125 // A boolean indicating that the current address is one (of possibly
126 // many) where execution should be suspended for an entry breakpoint
129 // A boolean indicating that the current address is one (of possibly
130 // many) where execution should be suspended for an exit breakpoint
135 // Represents a series of contiguous machine instructions. Line table for each
136 // compilation unit may consist of multiple sequences, which are not
137 // guaranteed to be in the order of ascending instruction address.
139 // Sequence describes instructions at address range [LowPC, HighPC)
140 // and is described by line table rows [FirstRowIndex, LastRowIndex).
143 unsigned FirstRowIndex;
144 unsigned LastRowIndex;
147 Sequence() { reset(); }
155 static bool orderByLowPC(const Sequence& LHS, const Sequence& RHS) {
156 return LHS.LowPC < RHS.LowPC;
158 bool isValid() const {
159 return !Empty && (LowPC < HighPC) && (FirstRowIndex < LastRowIndex);
161 bool containsPC(uint64_t pc) const {
162 return (LowPC <= pc && pc < HighPC);
167 void appendRow(const DWARFDebugLine::Row &state) { Rows.push_back(state); }
168 void appendSequence(const DWARFDebugLine::Sequence &sequence) {
169 Sequences.push_back(sequence);
177 // Returns the index of the row with file/line info for a given address,
178 // or -1 if there is no such row.
179 uint32_t lookupAddress(uint64_t address) const;
181 bool lookupAddressRange(uint64_t address,
183 std::vector<uint32_t>& result) const;
185 // Extracts filename by its index in filename table in prologue.
186 // Returns true on success.
187 bool getFileNameByIndex(uint64_t FileIndex,
188 bool NeedsAbsoluteFilePath,
189 std::string &Result) const;
191 void dump(raw_ostream &OS) const;
193 struct Prologue Prologue;
194 typedef std::vector<Row> RowVector;
195 typedef RowVector::const_iterator RowIter;
196 typedef std::vector<Sequence> SequenceVector;
197 typedef SequenceVector::const_iterator SequenceIter;
199 SequenceVector Sequences;
202 struct State : public Row, public Sequence, public LineTable {
203 // Special row codes.
205 StartParsingLineTable = 0,
206 DoneParsingLineTable = -1
209 State() : row(StartParsingLineTable) {}
212 virtual void appendRowToMatrix(uint32_t offset);
213 virtual void finalize();
214 virtual void reset() {
215 Row::reset(Prologue.DefaultIsStmt);
219 // The row number that starts at zero for the prologue, and increases for
220 // each row added to the matrix.
224 struct DumpingState : public State {
225 DumpingState(raw_ostream &OS) : OS(OS) {}
226 virtual ~DumpingState();
227 virtual void finalize();
232 static bool parsePrologue(DataExtractor debug_line_data, uint32_t *offset_ptr,
234 /// Parse a single line table (prologue and all rows).
235 static bool parseStatementTable(DataExtractor debug_line_data,
236 const RelocAddrMap *RMap,
237 uint32_t *offset_ptr, State &state);
239 const LineTable *getLineTable(uint32_t offset) const;
240 const LineTable *getOrParseLineTable(DataExtractor debug_line_data,
244 typedef std::map<uint32_t, LineTable> LineTableMapTy;
245 typedef LineTableMapTy::iterator LineTableIter;
246 typedef LineTableMapTy::const_iterator LineTableConstIter;
248 const RelocAddrMap *RelocMap;
249 LineTableMapTy LineTableMap;