DWARFDebugLine.h 8.7 KB

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