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