DWARFDebugLine.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DWARFDebugLine.h -----------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DEBUGINFO_DWARFDEBUGLINE_H
  14. #define LLVM_DEBUGINFO_DWARFDEBUGLINE_H
  15. #include "llvm/ADT/Optional.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/DebugInfo/DIContext.h"
  18. #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
  19. #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
  20. #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
  21. #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
  22. #include "llvm/DebugInfo/DWARF/DWARFTypeUnit.h"
  23. #include "llvm/Support/MD5.h"
  24. #include "llvm/Support/Path.h"
  25. #include <cstdint>
  26. #include <map>
  27. #include <string>
  28. #include <vector>
  29. namespace llvm {
  30. class DWARFUnit;
  31. class raw_ostream;
  32. class DWARFDebugLine {
  33. public:
  34. struct FileNameEntry {
  35. FileNameEntry() = default;
  36. DWARFFormValue Name;
  37. uint64_t DirIdx = 0;
  38. uint64_t ModTime = 0;
  39. uint64_t Length = 0;
  40. MD5::MD5Result Checksum;
  41. DWARFFormValue Source;
  42. };
  43. /// Tracks which optional content types are present in a DWARF file name
  44. /// entry format.
  45. struct ContentTypeTracker {
  46. ContentTypeTracker() = default;
  47. /// Whether filename entries provide a modification timestamp.
  48. bool HasModTime = false;
  49. /// Whether filename entries provide a file size.
  50. bool HasLength = false;
  51. /// For v5, whether filename entries provide an MD5 checksum.
  52. bool HasMD5 = false;
  53. /// For v5, whether filename entries provide source text.
  54. bool HasSource = false;
  55. /// Update tracked content types with \p ContentType.
  56. void trackContentType(dwarf::LineNumberEntryFormat ContentType);
  57. };
  58. struct Prologue {
  59. Prologue();
  60. /// The size in bytes of the statement information for this compilation unit
  61. /// (not including the total_length field itself).
  62. uint64_t TotalLength;
  63. /// Version, address size (starting in v5), and DWARF32/64 format; these
  64. /// parameters affect interpretation of forms (used in the directory and
  65. /// file tables starting with v5).
  66. dwarf::FormParams FormParams;
  67. /// The number of bytes following the prologue_length field to the beginning
  68. /// of the first byte of the statement program itself.
  69. uint64_t PrologueLength;
  70. /// In v5, size in bytes of a segment selector.
  71. uint8_t SegSelectorSize;
  72. /// The size in bytes of the smallest target machine instruction. Statement
  73. /// program opcodes that alter the address register first multiply their
  74. /// operands by this value.
  75. uint8_t MinInstLength;
  76. /// The maximum number of individual operations that may be encoded in an
  77. /// instruction.
  78. uint8_t MaxOpsPerInst;
  79. /// The initial value of theis_stmtregister.
  80. uint8_t DefaultIsStmt;
  81. /// This parameter affects the meaning of the special opcodes. See below.
  82. int8_t LineBase;
  83. /// This parameter affects the meaning of the special opcodes. See below.
  84. uint8_t LineRange;
  85. /// The number assigned to the first special opcode.
  86. uint8_t OpcodeBase;
  87. /// This tracks which optional file format content types are present.
  88. ContentTypeTracker ContentTypes;
  89. std::vector<uint8_t> StandardOpcodeLengths;
  90. std::vector<DWARFFormValue> IncludeDirectories;
  91. std::vector<FileNameEntry> FileNames;
  92. const dwarf::FormParams getFormParams() const { return FormParams; }
  93. uint16_t getVersion() const { return FormParams.Version; }
  94. uint8_t getAddressSize() const { return FormParams.AddrSize; }
  95. bool isDWARF64() const { return FormParams.Format == dwarf::DWARF64; }
  96. uint32_t sizeofTotalLength() const { return isDWARF64() ? 12 : 4; }
  97. uint32_t sizeofPrologueLength() const { return isDWARF64() ? 8 : 4; }
  98. bool totalLengthIsValid() const;
  99. /// Length of the prologue in bytes.
  100. uint64_t getLength() const;
  101. int32_t getMaxLineIncrementForSpecialOpcode() const {
  102. return LineBase + (int8_t)LineRange - 1;
  103. }
  104. /// Get DWARF-version aware access to the file name entry at the provided
  105. /// index.
  106. const llvm::DWARFDebugLine::FileNameEntry &
  107. getFileNameEntry(uint64_t Index) const;
  108. bool hasFileAtIndex(uint64_t FileIndex) const;
  109. Optional<uint64_t> getLastValidFileIndex() const;
  110. bool
  111. getFileNameByIndex(uint64_t FileIndex, StringRef CompDir,
  112. DILineInfoSpecifier::FileLineInfoKind Kind,
  113. std::string &Result,
  114. sys::path::Style Style = sys::path::Style::native) const;
  115. void clear();
  116. void dump(raw_ostream &OS, DIDumpOptions DumpOptions) const;
  117. Error parse(DWARFDataExtractor Data, uint64_t *OffsetPtr,
  118. function_ref<void(Error)> RecoverableErrorHandler,
  119. const DWARFContext &Ctx, const DWARFUnit *U = nullptr);
  120. };
  121. /// Standard .debug_line state machine structure.
  122. struct Row {
  123. explicit Row(bool DefaultIsStmt = false);
  124. /// Called after a row is appended to the matrix.
  125. void postAppend();
  126. void reset(bool DefaultIsStmt);
  127. void dump(raw_ostream &OS) const;
  128. static void dumpTableHeader(raw_ostream &OS, unsigned Indent);
  129. static bool orderByAddress(const Row &LHS, const Row &RHS) {
  130. return std::tie(LHS.Address.SectionIndex, LHS.Address.Address) <
  131. std::tie(RHS.Address.SectionIndex, RHS.Address.Address);
  132. }
  133. /// The program-counter value corresponding to a machine instruction
  134. /// generated by the compiler and section index pointing to the section
  135. /// containg this PC. If relocation information is present then section
  136. /// index is the index of the section which contains above address.
  137. /// Otherwise this is object::SectionedAddress::Undef value.
  138. object::SectionedAddress Address;
  139. /// An unsigned integer indicating a source line number. Lines are numbered
  140. /// beginning at 1. The compiler may emit the value 0 in cases where an
  141. /// instruction cannot be attributed to any source line.
  142. uint32_t Line;
  143. /// An unsigned integer indicating a column number within a source line.
  144. /// Columns are numbered beginning at 1. The value 0 is reserved to indicate
  145. /// that a statement begins at the 'left edge' of the line.
  146. uint16_t Column;
  147. /// An unsigned integer indicating the identity of the source file
  148. /// corresponding to a machine instruction.
  149. uint16_t File;
  150. /// An unsigned integer representing the DWARF path discriminator value
  151. /// for this location.
  152. uint32_t Discriminator;
  153. /// An unsigned integer whose value encodes the applicable instruction set
  154. /// architecture for the current instruction.
  155. uint8_t Isa;
  156. /// A boolean indicating that the current instruction is the beginning of a
  157. /// statement.
  158. uint8_t IsStmt : 1,
  159. /// A boolean indicating that the current instruction is the
  160. /// beginning of a basic block.
  161. BasicBlock : 1,
  162. /// A boolean indicating that the current address is that of the
  163. /// first byte after the end of a sequence of target machine
  164. /// instructions.
  165. EndSequence : 1,
  166. /// A boolean indicating that the current address is one (of possibly
  167. /// many) where execution should be suspended for an entry breakpoint
  168. /// of a function.
  169. PrologueEnd : 1,
  170. /// A boolean indicating that the current address is one (of possibly
  171. /// many) where execution should be suspended for an exit breakpoint
  172. /// of a function.
  173. EpilogueBegin : 1;
  174. };
  175. /// Represents a series of contiguous machine instructions. Line table for
  176. /// each compilation unit may consist of multiple sequences, which are not
  177. /// guaranteed to be in the order of ascending instruction address.
  178. struct Sequence {
  179. Sequence();
  180. /// Sequence describes instructions at address range [LowPC, HighPC)
  181. /// and is described by line table rows [FirstRowIndex, LastRowIndex).
  182. uint64_t LowPC;
  183. uint64_t HighPC;
  184. /// If relocation information is present then this is the index of the
  185. /// section which contains above addresses. Otherwise this is
  186. /// object::SectionedAddress::Undef value.
  187. uint64_t SectionIndex;
  188. unsigned FirstRowIndex;
  189. unsigned LastRowIndex;
  190. bool Empty;
  191. void reset();
  192. static bool orderByHighPC(const Sequence &LHS, const Sequence &RHS) {
  193. return std::tie(LHS.SectionIndex, LHS.HighPC) <
  194. std::tie(RHS.SectionIndex, RHS.HighPC);
  195. }
  196. bool isValid() const {
  197. return !Empty && (LowPC < HighPC) && (FirstRowIndex < LastRowIndex);
  198. }
  199. bool containsPC(object::SectionedAddress PC) const {
  200. return SectionIndex == PC.SectionIndex &&
  201. (LowPC <= PC.Address && PC.Address < HighPC);
  202. }
  203. };
  204. struct LineTable {
  205. LineTable();
  206. /// Represents an invalid row
  207. const uint32_t UnknownRowIndex = UINT32_MAX;
  208. void appendRow(const DWARFDebugLine::Row &R) { Rows.push_back(R); }
  209. void appendSequence(const DWARFDebugLine::Sequence &S) {
  210. Sequences.push_back(S);
  211. }
  212. /// Returns the index of the row with file/line info for a given address,
  213. /// or UnknownRowIndex if there is no such row.
  214. uint32_t lookupAddress(object::SectionedAddress Address) const;
  215. bool lookupAddressRange(object::SectionedAddress Address, uint64_t Size,
  216. std::vector<uint32_t> &Result) const;
  217. bool hasFileAtIndex(uint64_t FileIndex) const {
  218. return Prologue.hasFileAtIndex(FileIndex);
  219. }
  220. Optional<uint64_t> getLastValidFileIndex() const {
  221. return Prologue.getLastValidFileIndex();
  222. }
  223. /// Extracts filename by its index in filename table in prologue.
  224. /// In Dwarf 4, the files are 1-indexed and the current compilation file
  225. /// name is not represented in the list. In DWARF v5, the files are
  226. /// 0-indexed and the primary source file has the index 0.
  227. /// Returns true on success.
  228. bool getFileNameByIndex(uint64_t FileIndex, StringRef CompDir,
  229. DILineInfoSpecifier::FileLineInfoKind Kind,
  230. std::string &Result) const {
  231. return Prologue.getFileNameByIndex(FileIndex, CompDir, Kind, Result);
  232. }
  233. /// Fills the Result argument with the file and line information
  234. /// corresponding to Address. Returns true on success.
  235. bool getFileLineInfoForAddress(object::SectionedAddress Address,
  236. const char *CompDir,
  237. DILineInfoSpecifier::FileLineInfoKind Kind,
  238. DILineInfo &Result) const;
  239. void dump(raw_ostream &OS, DIDumpOptions DumpOptions) const;
  240. void clear();
  241. /// Parse prologue and all rows.
  242. Error parse(DWARFDataExtractor &DebugLineData, uint64_t *OffsetPtr,
  243. const DWARFContext &Ctx, const DWARFUnit *U,
  244. function_ref<void(Error)> RecoverableErrorHandler,
  245. raw_ostream *OS = nullptr, bool Verbose = false);
  246. using RowVector = std::vector<Row>;
  247. using RowIter = RowVector::const_iterator;
  248. using SequenceVector = std::vector<Sequence>;
  249. using SequenceIter = SequenceVector::const_iterator;
  250. struct Prologue Prologue;
  251. RowVector Rows;
  252. SequenceVector Sequences;
  253. private:
  254. uint32_t findRowInSeq(const DWARFDebugLine::Sequence &Seq,
  255. object::SectionedAddress Address) const;
  256. Optional<StringRef>
  257. getSourceByIndex(uint64_t FileIndex,
  258. DILineInfoSpecifier::FileLineInfoKind Kind) const;
  259. uint32_t lookupAddressImpl(object::SectionedAddress Address) const;
  260. bool lookupAddressRangeImpl(object::SectionedAddress Address, uint64_t Size,
  261. std::vector<uint32_t> &Result) const;
  262. };
  263. const LineTable *getLineTable(uint64_t Offset) const;
  264. Expected<const LineTable *>
  265. getOrParseLineTable(DWARFDataExtractor &DebugLineData, uint64_t Offset,
  266. const DWARFContext &Ctx, const DWARFUnit *U,
  267. function_ref<void(Error)> RecoverableErrorHandler);
  268. /// Helper to allow for parsing of an entire .debug_line section in sequence.
  269. class SectionParser {
  270. public:
  271. using LineToUnitMap = std::map<uint64_t, DWARFUnit *>;
  272. SectionParser(DWARFDataExtractor &Data, const DWARFContext &C,
  273. DWARFUnitVector::iterator_range Units);
  274. /// Get the next line table from the section. Report any issues via the
  275. /// handlers.
  276. ///
  277. /// \param RecoverableErrorHandler - any issues that don't prevent further
  278. /// parsing of the table will be reported through this handler.
  279. /// \param UnrecoverableErrorHandler - any issues that prevent further
  280. /// parsing of the table will be reported through this handler.
  281. /// \param OS - if not null, the parser will print information about the
  282. /// table as it parses it.
  283. /// \param Verbose - if true, the parser will print verbose information when
  284. /// printing to the output.
  285. LineTable parseNext(function_ref<void(Error)> RecoverableErrorHandler,
  286. function_ref<void(Error)> UnrecoverableErrorHandler,
  287. raw_ostream *OS = nullptr, bool Verbose = false);
  288. /// Skip the current line table and go to the following line table (if
  289. /// present) immediately.
  290. ///
  291. /// \param RecoverableErrorHandler - report any recoverable prologue
  292. /// parsing issues via this handler.
  293. /// \param UnrecoverableErrorHandler - report any unrecoverable prologue
  294. /// parsing issues via this handler.
  295. void skip(function_ref<void(Error)> RecoverableErrorHandler,
  296. function_ref<void(Error)> UnrecoverableErrorHandler);
  297. /// Indicates if the parser has parsed as much as possible.
  298. ///
  299. /// \note Certain problems with the line table structure might mean that
  300. /// parsing stops before the end of the section is reached.
  301. bool done() const { return Done; }
  302. /// Get the offset the parser has reached.
  303. uint64_t getOffset() const { return Offset; }
  304. private:
  305. DWARFUnit *prepareToParse(uint64_t Offset);
  306. void moveToNextTable(uint64_t OldOffset, const Prologue &P);
  307. LineToUnitMap LineToUnit;
  308. DWARFDataExtractor &DebugLineData;
  309. const DWARFContext &Context;
  310. uint64_t Offset = 0;
  311. bool Done = false;
  312. };
  313. private:
  314. struct ParsingState {
  315. ParsingState(struct LineTable *LT, uint64_t TableOffset,
  316. function_ref<void(Error)> ErrorHandler);
  317. void resetRowAndSequence();
  318. void appendRowToMatrix();
  319. /// Advance the address by the \p OperationAdvance value. \returns the
  320. /// amount advanced by.
  321. uint64_t advanceAddr(uint64_t OperationAdvance, uint8_t Opcode,
  322. uint64_t OpcodeOffset);
  323. struct AddrAndAdjustedOpcode {
  324. uint64_t AddrDelta;
  325. uint8_t AdjustedOpcode;
  326. };
  327. /// Advance the address as required by the specified \p Opcode.
  328. /// \returns the amount advanced by and the calculated adjusted opcode.
  329. AddrAndAdjustedOpcode advanceAddrForOpcode(uint8_t Opcode,
  330. uint64_t OpcodeOffset);
  331. struct AddrAndLineDelta {
  332. uint64_t Address;
  333. int32_t Line;
  334. };
  335. /// Advance the line and address as required by the specified special \p
  336. /// Opcode. \returns the address and line delta.
  337. AddrAndLineDelta handleSpecialOpcode(uint8_t Opcode, uint64_t OpcodeOffset);
  338. /// Line table we're currently parsing.
  339. struct LineTable *LineTable;
  340. struct Row Row;
  341. struct Sequence Sequence;
  342. private:
  343. uint64_t LineTableOffset;
  344. bool ReportAdvanceAddrProblem = true;
  345. bool ReportBadLineRange = true;
  346. function_ref<void(Error)> ErrorHandler;
  347. };
  348. using LineTableMapTy = std::map<uint64_t, LineTable>;
  349. using LineTableIter = LineTableMapTy::iterator;
  350. using LineTableConstIter = LineTableMapTy::const_iterator;
  351. LineTableMapTy LineTableMap;
  352. };
  353. } // end namespace llvm
  354. #endif // LLVM_DEBUGINFO_DWARFDEBUGLINE_H
  355. #ifdef __GNUC__
  356. #pragma GCC diagnostic pop
  357. #endif