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 { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 27 | FileNameEntry() : Name(nullptr), 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 { |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame^] | 36 | Prologue(); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 37 | |
| 38 | // The size in bytes of the statement information for this compilation unit |
| 39 | // (not including the total_length field itself). |
| 40 | uint32_t TotalLength; |
| 41 | // Version identifier for the statement information format. |
| 42 | uint16_t Version; |
| 43 | // The number of bytes following the prologue_length field to the beginning |
| 44 | // of the first byte of the statement program itself. |
| 45 | uint32_t PrologueLength; |
| 46 | // The size in bytes of the smallest target machine instruction. Statement |
| 47 | // program opcodes that alter the address register first multiply their |
| 48 | // operands by this value. |
| 49 | uint8_t MinInstLength; |
David Blaikie | 1d4736e | 2014-02-24 23:58:54 +0000 | [diff] [blame] | 50 | // The maximum number of individual operations that may be encoded in an |
| 51 | // instruction. |
| 52 | uint8_t MaxOpsPerInst; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 53 | // The initial value of theis_stmtregister. |
| 54 | uint8_t DefaultIsStmt; |
| 55 | // This parameter affects the meaning of the special opcodes. See below. |
| 56 | int8_t LineBase; |
| 57 | // This parameter affects the meaning of the special opcodes. See below. |
| 58 | uint8_t LineRange; |
| 59 | // The number assigned to the first special opcode. |
| 60 | uint8_t OpcodeBase; |
| 61 | std::vector<uint8_t> StandardOpcodeLengths; |
Alexey Samsonov | e16e16a | 2012-07-19 07:03:58 +0000 | [diff] [blame] | 62 | std::vector<const char*> IncludeDirectories; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 63 | std::vector<FileNameEntry> FileNames; |
| 64 | |
| 65 | // Length of the prologue in bytes. |
| 66 | uint32_t getLength() const { |
| 67 | return PrologueLength + sizeof(TotalLength) + sizeof(Version) + |
| 68 | sizeof(PrologueLength); |
| 69 | } |
| 70 | // Length of the line table data in bytes (not including the prologue). |
| 71 | uint32_t getStatementTableLength() const { |
| 72 | return TotalLength + sizeof(TotalLength) - getLength(); |
| 73 | } |
| 74 | int32_t getMaxLineIncrementForSpecialOpcode() const { |
| 75 | return LineBase + (int8_t)LineRange - 1; |
| 76 | } |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame^] | 77 | |
| 78 | void clear(); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 79 | void dump(raw_ostream &OS) const; |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame^] | 80 | bool parse(DataExtractor debug_line_data, uint32_t *offset_ptr); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | // Standard .debug_line state machine structure. |
| 84 | struct Row { |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame^] | 85 | explicit Row(bool default_is_stmt = false); |
| 86 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 87 | /// Called after a row is appended to the matrix. |
| 88 | void postAppend(); |
| 89 | void reset(bool default_is_stmt); |
| 90 | void dump(raw_ostream &OS) const; |
| 91 | |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 92 | static bool orderByAddress(const Row& LHS, const Row& RHS) { |
| 93 | return LHS.Address < RHS.Address; |
| 94 | } |
| 95 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 96 | // The program-counter value corresponding to a machine instruction |
| 97 | // generated by the compiler. |
| 98 | uint64_t Address; |
| 99 | // An unsigned integer indicating a source line number. Lines are numbered |
| 100 | // beginning at 1. The compiler may emit the value 0 in cases where an |
| 101 | // instruction cannot be attributed to any source line. |
| 102 | uint32_t Line; |
| 103 | // An unsigned integer indicating a column number within a source line. |
| 104 | // Columns are numbered beginning at 1. The value 0 is reserved to indicate |
| 105 | // that a statement begins at the 'left edge' of the line. |
| 106 | uint16_t Column; |
| 107 | // An unsigned integer indicating the identity of the source file |
| 108 | // corresponding to a machine instruction. |
| 109 | uint16_t File; |
| 110 | // An unsigned integer whose value encodes the applicable instruction set |
| 111 | // architecture for the current instruction. |
| 112 | uint8_t Isa; |
Diego Novillo | 5b5cf50 | 2014-02-14 19:27:53 +0000 | [diff] [blame] | 113 | // An unsigned integer representing the DWARF path discriminator value |
| 114 | // for this location. |
| 115 | uint32_t Discriminator; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 116 | // A boolean indicating that the current instruction is the beginning of a |
| 117 | // statement. |
| 118 | uint8_t IsStmt:1, |
| 119 | // A boolean indicating that the current instruction is the |
| 120 | // beginning of a basic block. |
| 121 | BasicBlock:1, |
| 122 | // A boolean indicating that the current address is that of the |
| 123 | // first byte after the end of a sequence of target machine |
| 124 | // instructions. |
| 125 | EndSequence:1, |
| 126 | // A boolean indicating that the current address is one (of possibly |
| 127 | // many) where execution should be suspended for an entry breakpoint |
| 128 | // of a function. |
| 129 | PrologueEnd:1, |
| 130 | // A boolean indicating that the current address is one (of possibly |
| 131 | // many) where execution should be suspended for an exit breakpoint |
| 132 | // of a function. |
| 133 | EpilogueBegin:1; |
| 134 | }; |
| 135 | |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 136 | // Represents a series of contiguous machine instructions. Line table for each |
| 137 | // compilation unit may consist of multiple sequences, which are not |
| 138 | // guaranteed to be in the order of ascending instruction address. |
| 139 | struct Sequence { |
| 140 | // Sequence describes instructions at address range [LowPC, HighPC) |
| 141 | // and is described by line table rows [FirstRowIndex, LastRowIndex). |
| 142 | uint64_t LowPC; |
| 143 | uint64_t HighPC; |
| 144 | unsigned FirstRowIndex; |
| 145 | unsigned LastRowIndex; |
| 146 | bool Empty; |
| 147 | |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame^] | 148 | Sequence(); |
| 149 | void reset(); |
| 150 | |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 151 | static bool orderByLowPC(const Sequence& LHS, const Sequence& RHS) { |
| 152 | return LHS.LowPC < RHS.LowPC; |
| 153 | } |
| 154 | bool isValid() const { |
| 155 | return !Empty && (LowPC < HighPC) && (FirstRowIndex < LastRowIndex); |
| 156 | } |
| 157 | bool containsPC(uint64_t pc) const { |
| 158 | return (LowPC <= pc && pc < HighPC); |
| 159 | } |
| 160 | }; |
| 161 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 162 | struct LineTable { |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame^] | 163 | LineTable(); |
| 164 | |
| 165 | void appendRow(const DWARFDebugLine::Row &R) { |
| 166 | Rows.push_back(R); |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 167 | } |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame^] | 168 | void appendSequence(const DWARFDebugLine::Sequence &S) { |
| 169 | Sequences.push_back(S); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 172 | // Returns the index of the row with file/line info for a given address, |
| 173 | // or -1 if there is no such row. |
| 174 | uint32_t lookupAddress(uint64_t address) const; |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 175 | |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame^] | 176 | bool lookupAddressRange(uint64_t address, uint64_t size, |
| 177 | std::vector<uint32_t> &result) const; |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 178 | |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 179 | // Extracts filename by its index in filename table in prologue. |
| 180 | // Returns true on success. |
| 181 | bool getFileNameByIndex(uint64_t FileIndex, |
| 182 | bool NeedsAbsoluteFilePath, |
| 183 | std::string &Result) const; |
| 184 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 185 | void dump(raw_ostream &OS) const; |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame^] | 186 | void clear(); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 187 | |
| 188 | struct Prologue Prologue; |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 189 | typedef std::vector<Row> RowVector; |
| 190 | typedef RowVector::const_iterator RowIter; |
| 191 | typedef std::vector<Sequence> SequenceVector; |
| 192 | typedef SequenceVector::const_iterator SequenceIter; |
| 193 | RowVector Rows; |
| 194 | SequenceVector Sequences; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 195 | }; |
| 196 | |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 197 | struct State : public Row, public Sequence, public LineTable { |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 198 | // Special row codes. |
| 199 | enum { |
| 200 | StartParsingLineTable = 0, |
| 201 | DoneParsingLineTable = -1 |
| 202 | }; |
| 203 | |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 204 | State() : row(StartParsingLineTable) {} |
Nick Lewycky | 4d04492 | 2011-09-15 03:41:51 +0000 | [diff] [blame] | 205 | virtual ~State(); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 206 | |
| 207 | virtual void appendRowToMatrix(uint32_t offset); |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 208 | virtual void finalize(); |
| 209 | virtual void reset() { |
| 210 | Row::reset(Prologue.DefaultIsStmt); |
| 211 | Sequence::reset(); |
| 212 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 213 | |
| 214 | // The row number that starts at zero for the prologue, and increases for |
| 215 | // each row added to the matrix. |
| 216 | unsigned row; |
| 217 | }; |
| 218 | |
| 219 | struct DumpingState : public State { |
| 220 | DumpingState(raw_ostream &OS) : OS(OS) {} |
Nick Lewycky | 4d04492 | 2011-09-15 03:41:51 +0000 | [diff] [blame] | 221 | virtual ~DumpingState(); |
Craig Topper | 8548299 | 2014-03-05 07:52:44 +0000 | [diff] [blame] | 222 | void finalize() override; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 223 | private: |
| 224 | raw_ostream &OS; |
| 225 | }; |
| 226 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 227 | /// Parse a single line table (prologue and all rows). |
| 228 | static bool parseStatementTable(DataExtractor debug_line_data, |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 229 | const RelocAddrMap *RMap, |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 230 | uint32_t *offset_ptr, State &state); |
| 231 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 232 | const LineTable *getLineTable(uint32_t offset) const; |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 233 | const LineTable *getOrParseLineTable(DataExtractor debug_line_data, |
| 234 | uint32_t offset); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 235 | |
Benjamin Kramer | 123bfbb | 2011-09-15 03:11:09 +0000 | [diff] [blame] | 236 | private: |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 237 | typedef std::map<uint32_t, LineTable> LineTableMapTy; |
| 238 | typedef LineTableMapTy::iterator LineTableIter; |
| 239 | typedef LineTableMapTy::const_iterator LineTableConstIter; |
| 240 | |
Andrew Kaylor | d55d701 | 2013-01-25 22:50:58 +0000 | [diff] [blame] | 241 | const RelocAddrMap *RelocMap; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 242 | LineTableMapTy LineTableMap; |
| 243 | }; |
| 244 | |
| 245 | } |
| 246 | |
| 247 | #endif |