Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 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 | |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 13 | #include "DWARFRelocMap.h" |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 14 | #include "llvm/Support/DataExtractor.h" |
| 15 | #include <map> |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 16 | #include <string> |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
| 19 | namespace llvm { |
| 20 | |
| 21 | class raw_ostream; |
| 22 | |
| 23 | class DWARFDebugLine { |
| 24 | public: |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 25 | DWARFDebugLine(const RelocAddrMap* LineInfoRelocMap) : RelocMap(LineInfoRelocMap) {} |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 26 | struct FileNameEntry { |
Alexey Samsonov | e16e16a | 2012-07-19 07:03:58 +0000 | [diff] [blame] | 27 | FileNameEntry() : Name(0), DirIdx(0), ModTime(0), Length(0) {} |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 28 | |
Alexey Samsonov | e16e16a | 2012-07-19 07:03:58 +0000 | [diff] [blame] | 29 | const char *Name; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 30 | uint64_t DirIdx; |
| 31 | uint64_t ModTime; |
| 32 | uint64_t Length; |
| 33 | }; |
| 34 | |
| 35 | struct Prologue { |
| 36 | Prologue() |
David Blaikie | 1d4736e | 2014-02-24 23:58:54 +0000 | [diff] [blame] | 37 | : TotalLength(0), Version(0), PrologueLength(0), MinInstLength(0), |
| 38 | MaxOpsPerInst(0), DefaultIsStmt(0), LineBase(0), LineRange(0), |
| 39 | OpcodeBase(0) {} |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 40 | |
| 41 | // The size in bytes of the statement information for this compilation unit |
| 42 | // (not including the total_length field itself). |
| 43 | uint32_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 | 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; |
David Blaikie | 1d4736e | 2014-02-24 23:58:54 +0000 | [diff] [blame] | 53 | // The maximum number of individual operations that may be encoded in an |
| 54 | // instruction. |
| 55 | uint8_t MaxOpsPerInst; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 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; |
Alexey Samsonov | e16e16a | 2012-07-19 07:03:58 +0000 | [diff] [blame] | 65 | std::vector<const char*> IncludeDirectories; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 66 | std::vector<FileNameEntry> FileNames; |
| 67 | |
| 68 | // Length of the prologue in bytes. |
| 69 | uint32_t getLength() const { |
| 70 | return PrologueLength + sizeof(TotalLength) + sizeof(Version) + |
| 71 | sizeof(PrologueLength); |
| 72 | } |
| 73 | // Length of the line table data in bytes (not including the prologue). |
| 74 | uint32_t getStatementTableLength() const { |
| 75 | return TotalLength + sizeof(TotalLength) - getLength(); |
| 76 | } |
| 77 | int32_t getMaxLineIncrementForSpecialOpcode() const { |
| 78 | return LineBase + (int8_t)LineRange - 1; |
| 79 | } |
| 80 | void dump(raw_ostream &OS) const; |
| 81 | void clear() { |
| 82 | TotalLength = Version = PrologueLength = 0; |
| 83 | MinInstLength = LineBase = LineRange = OpcodeBase = 0; |
| 84 | StandardOpcodeLengths.clear(); |
| 85 | IncludeDirectories.clear(); |
| 86 | FileNames.clear(); |
| 87 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | // Standard .debug_line state machine structure. |
| 91 | struct Row { |
| 92 | Row(bool default_is_stmt = false) { reset(default_is_stmt); } |
| 93 | /// Called after a row is appended to the matrix. |
| 94 | void postAppend(); |
| 95 | void reset(bool default_is_stmt); |
| 96 | void dump(raw_ostream &OS) const; |
| 97 | |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 98 | static bool orderByAddress(const Row& LHS, const Row& RHS) { |
| 99 | return LHS.Address < RHS.Address; |
| 100 | } |
| 101 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 102 | // The program-counter value corresponding to a machine instruction |
| 103 | // generated by the compiler. |
| 104 | uint64_t Address; |
| 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. |
| 108 | uint32_t 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. |
| 112 | uint16_t Column; |
| 113 | // An unsigned integer indicating the identity of the source file |
| 114 | // corresponding to a machine instruction. |
| 115 | uint16_t File; |
| 116 | // An unsigned integer whose value encodes the applicable instruction set |
| 117 | // architecture for the current instruction. |
| 118 | uint8_t Isa; |
Diego Novillo | 5b5cf50 | 2014-02-14 19:27:53 +0000 | [diff] [blame] | 119 | // An unsigned integer representing the DWARF path discriminator value |
| 120 | // for this location. |
| 121 | uint32_t Discriminator; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 122 | // A boolean indicating that the current instruction is the beginning of a |
| 123 | // statement. |
| 124 | uint8_t IsStmt:1, |
| 125 | // A boolean indicating that the current instruction is the |
| 126 | // beginning of a basic block. |
| 127 | BasicBlock:1, |
| 128 | // A boolean indicating that the current address is that of the |
| 129 | // first byte after the end of a sequence of target machine |
| 130 | // instructions. |
| 131 | EndSequence:1, |
| 132 | // A boolean indicating that the current address is one (of possibly |
| 133 | // many) where execution should be suspended for an entry breakpoint |
| 134 | // of a function. |
| 135 | PrologueEnd:1, |
| 136 | // A boolean indicating that the current address is one (of possibly |
| 137 | // many) where execution should be suspended for an exit breakpoint |
| 138 | // of a function. |
| 139 | EpilogueBegin:1; |
| 140 | }; |
| 141 | |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 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. |
| 145 | struct Sequence { |
| 146 | // Sequence describes instructions at address range [LowPC, HighPC) |
| 147 | // and is described by line table rows [FirstRowIndex, LastRowIndex). |
| 148 | uint64_t LowPC; |
| 149 | uint64_t HighPC; |
| 150 | unsigned FirstRowIndex; |
| 151 | unsigned LastRowIndex; |
| 152 | bool Empty; |
| 153 | |
| 154 | Sequence() { reset(); } |
| 155 | void reset() { |
| 156 | LowPC = 0; |
| 157 | HighPC = 0; |
| 158 | FirstRowIndex = 0; |
| 159 | LastRowIndex = 0; |
| 160 | Empty = true; |
| 161 | } |
| 162 | static bool orderByLowPC(const Sequence& LHS, const Sequence& RHS) { |
| 163 | return LHS.LowPC < RHS.LowPC; |
| 164 | } |
| 165 | bool isValid() const { |
| 166 | return !Empty && (LowPC < HighPC) && (FirstRowIndex < LastRowIndex); |
| 167 | } |
| 168 | bool containsPC(uint64_t pc) const { |
| 169 | return (LowPC <= pc && pc < HighPC); |
| 170 | } |
| 171 | }; |
| 172 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 173 | struct LineTable { |
| 174 | void appendRow(const DWARFDebugLine::Row &state) { Rows.push_back(state); } |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 175 | void appendSequence(const DWARFDebugLine::Sequence &sequence) { |
| 176 | Sequences.push_back(sequence); |
| 177 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 178 | void clear() { |
| 179 | Prologue.clear(); |
| 180 | Rows.clear(); |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 181 | Sequences.clear(); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 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; |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 187 | |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 188 | bool lookupAddressRange(uint64_t address, |
| 189 | uint64_t size, |
| 190 | std::vector<uint32_t>& result) const; |
| 191 | |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 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; |
| 197 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 198 | void dump(raw_ostream &OS) const; |
| 199 | |
| 200 | struct Prologue Prologue; |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 201 | typedef std::vector<Row> RowVector; |
| 202 | typedef RowVector::const_iterator RowIter; |
| 203 | typedef std::vector<Sequence> SequenceVector; |
| 204 | typedef SequenceVector::const_iterator SequenceIter; |
| 205 | RowVector Rows; |
| 206 | SequenceVector Sequences; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 209 | struct State : public Row, public Sequence, public LineTable { |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 210 | // Special row codes. |
| 211 | enum { |
| 212 | StartParsingLineTable = 0, |
| 213 | DoneParsingLineTable = -1 |
| 214 | }; |
| 215 | |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 216 | State() : row(StartParsingLineTable) {} |
Nick Lewycky | 4d04492 | 2011-09-15 03:41:51 +0000 | [diff] [blame] | 217 | virtual ~State(); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 218 | |
| 219 | virtual void appendRowToMatrix(uint32_t offset); |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 220 | virtual void finalize(); |
| 221 | virtual void reset() { |
| 222 | Row::reset(Prologue.DefaultIsStmt); |
| 223 | Sequence::reset(); |
| 224 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 225 | |
| 226 | // The row number that starts at zero for the prologue, and increases for |
| 227 | // each row added to the matrix. |
| 228 | unsigned row; |
| 229 | }; |
| 230 | |
| 231 | struct DumpingState : public State { |
| 232 | DumpingState(raw_ostream &OS) : OS(OS) {} |
Nick Lewycky | 4d04492 | 2011-09-15 03:41:51 +0000 | [diff] [blame] | 233 | virtual ~DumpingState(); |
Craig Topper | 8548299 | 2014-03-05 07:52:44 +0000 | [diff] [blame] | 234 | void finalize() override; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 235 | private: |
| 236 | raw_ostream &OS; |
| 237 | }; |
| 238 | |
| 239 | static bool parsePrologue(DataExtractor debug_line_data, uint32_t *offset_ptr, |
| 240 | Prologue *prologue); |
| 241 | /// Parse a single line table (prologue and all rows). |
| 242 | static bool parseStatementTable(DataExtractor debug_line_data, |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 243 | const RelocAddrMap *RMap, |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 244 | uint32_t *offset_ptr, State &state); |
| 245 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 246 | const LineTable *getLineTable(uint32_t offset) const; |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 247 | const LineTable *getOrParseLineTable(DataExtractor debug_line_data, |
| 248 | uint32_t offset); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 249 | |
Benjamin Kramer | 123bfbb | 2011-09-15 03:11:09 +0000 | [diff] [blame] | 250 | private: |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 251 | typedef std::map<uint32_t, LineTable> LineTableMapTy; |
| 252 | typedef LineTableMapTy::iterator LineTableIter; |
| 253 | typedef LineTableMapTy::const_iterator LineTableConstIter; |
| 254 | |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 255 | const RelocAddrMap *RelocMap; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 256 | LineTableMapTy LineTableMap; |
| 257 | }; |
| 258 | |
| 259 | } |
| 260 | |
| 261 | #endif |