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