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