blob: c24364a288419dc4615978b6a29436b3af879705 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- 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
13#include "llvm/ADT/Optional.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/DebugInfo/DIContext.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/Support/MD5.h"
20#include <cstdint>
21#include <map>
22#include <string>
23#include <vector>
24
25namespace llvm {
26
27class DWARFUnit;
28class raw_ostream;
29
30class DWARFDebugLine {
31public:
32 struct FileNameEntry {
33 FileNameEntry() = default;
34
35 DWARFFormValue Name;
36 uint64_t DirIdx = 0;
37 uint64_t ModTime = 0;
38 uint64_t Length = 0;
39 MD5::MD5Result Checksum;
40 DWARFFormValue Source;
41 };
42
43 /// Tracks which optional content types are present in a DWARF file name
44 /// entry format.
45 struct ContentTypeTracker {
46 ContentTypeTracker() = default;
47
48 /// Whether filename entries provide a modification timestamp.
49 bool HasModTime = false;
50 /// Whether filename entries provide a file size.
51 bool HasLength = false;
52 /// For v5, whether filename entries provide an MD5 checksum.
53 bool HasMD5 = false;
54 /// For v5, whether filename entries provide source text.
55 bool HasSource = false;
56
57 /// Update tracked content types with \p ContentType.
58 void trackContentType(dwarf::LineNumberEntryFormat ContentType);
59 };
60
61 struct Prologue {
62 Prologue();
63
64 /// The size in bytes of the statement information for this compilation unit
65 /// (not including the total_length field itself).
66 uint64_t TotalLength;
67 /// Version, address size (starting in v5), and DWARF32/64 format; these
68 /// parameters affect interpretation of forms (used in the directory and
69 /// file tables starting with v5).
70 dwarf::FormParams FormParams;
71 /// The number of bytes following the prologue_length field to the beginning
72 /// of the first byte of the statement program itself.
73 uint64_t PrologueLength;
74 /// In v5, size in bytes of a segment selector.
75 uint8_t SegSelectorSize;
76 /// The size in bytes of the smallest target machine instruction. Statement
77 /// program opcodes that alter the address register first multiply their
78 /// operands by this value.
79 uint8_t MinInstLength;
80 /// The maximum number of individual operations that may be encoded in an
81 /// instruction.
82 uint8_t MaxOpsPerInst;
83 /// The initial value of theis_stmtregister.
84 uint8_t DefaultIsStmt;
85 /// This parameter affects the meaning of the special opcodes. See below.
86 int8_t LineBase;
87 /// This parameter affects the meaning of the special opcodes. See below.
88 uint8_t LineRange;
89 /// The number assigned to the first special opcode.
90 uint8_t OpcodeBase;
91 /// This tracks which optional file format content types are present.
92 ContentTypeTracker ContentTypes;
93 std::vector<uint8_t> StandardOpcodeLengths;
94 std::vector<DWARFFormValue> IncludeDirectories;
95 std::vector<FileNameEntry> FileNames;
96
97 const dwarf::FormParams getFormParams() const { return FormParams; }
98 uint16_t getVersion() const { return FormParams.Version; }
99 uint8_t getAddressSize() const { return FormParams.AddrSize; }
100 bool isDWARF64() const { return FormParams.Format == dwarf::DWARF64; }
101
102 uint32_t sizeofTotalLength() const { return isDWARF64() ? 12 : 4; }
103
104 uint32_t sizeofPrologueLength() const { return isDWARF64() ? 8 : 4; }
105
106 /// Length of the prologue in bytes.
107 uint32_t getLength() const {
108 return PrologueLength + sizeofTotalLength() + sizeof(getVersion()) +
109 sizeofPrologueLength();
110 }
111
112 /// Length of the line table data in bytes (not including the prologue).
113 uint32_t getStatementTableLength() const {
114 return TotalLength + sizeofTotalLength() - getLength();
115 }
116
117 int32_t getMaxLineIncrementForSpecialOpcode() const {
118 return LineBase + (int8_t)LineRange - 1;
119 }
120
121 void clear();
122 void dump(raw_ostream &OS, DIDumpOptions DumpOptions) const;
123 bool parse(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr,
124 const DWARFContext &Ctx, const DWARFUnit *U = nullptr);
125 };
126
127 /// Standard .debug_line state machine structure.
128 struct Row {
129 explicit Row(bool DefaultIsStmt = false);
130
131 /// Called after a row is appended to the matrix.
132 void postAppend();
133 void reset(bool DefaultIsStmt);
134 void dump(raw_ostream &OS) const;
135
136 static void dumpTableHeader(raw_ostream &OS);
137
138 static bool orderByAddress(const Row &LHS, const Row &RHS) {
139 return LHS.Address < RHS.Address;
140 }
141
142 /// The program-counter value corresponding to a machine instruction
143 /// generated by the compiler.
144 uint64_t Address;
145 /// An unsigned integer indicating a source line number. Lines are numbered
146 /// beginning at 1. The compiler may emit the value 0 in cases where an
147 /// instruction cannot be attributed to any source line.
148 uint32_t Line;
149 /// An unsigned integer indicating a column number within a source line.
150 /// Columns are numbered beginning at 1. The value 0 is reserved to indicate
151 /// that a statement begins at the 'left edge' of the line.
152 uint16_t Column;
153 /// An unsigned integer indicating the identity of the source file
154 /// corresponding to a machine instruction.
155 uint16_t File;
156 /// An unsigned integer representing the DWARF path discriminator value
157 /// for this location.
158 uint32_t Discriminator;
159 /// An unsigned integer whose value encodes the applicable instruction set
160 /// architecture for the current instruction.
161 uint8_t Isa;
162 /// A boolean indicating that the current instruction is the beginning of a
163 /// statement.
164 uint8_t IsStmt : 1,
165 /// A boolean indicating that the current instruction is the
166 /// beginning of a basic block.
167 BasicBlock : 1,
168 /// A boolean indicating that the current address is that of the
169 /// first byte after the end of a sequence of target machine
170 /// instructions.
171 EndSequence : 1,
172 /// A boolean indicating that the current address is one (of possibly
173 /// many) where execution should be suspended for an entry breakpoint
174 /// of a function.
175 PrologueEnd : 1,
176 /// A boolean indicating that the current address is one (of possibly
177 /// many) where execution should be suspended for an exit breakpoint
178 /// of a function.
179 EpilogueBegin : 1;
180 };
181
182 /// Represents a series of contiguous machine instructions. Line table for
183 /// each compilation unit may consist of multiple sequences, which are not
184 /// guaranteed to be in the order of ascending instruction address.
185 struct Sequence {
186 Sequence();
187
188 /// Sequence describes instructions at address range [LowPC, HighPC)
189 /// and is described by line table rows [FirstRowIndex, LastRowIndex).
190 uint64_t LowPC;
191 uint64_t HighPC;
192 unsigned FirstRowIndex;
193 unsigned LastRowIndex;
194 bool Empty;
195
196 void reset();
197
198 static bool orderByLowPC(const Sequence &LHS, const Sequence &RHS) {
199 return LHS.LowPC < RHS.LowPC;
200 }
201
202 bool isValid() const {
203 return !Empty && (LowPC < HighPC) && (FirstRowIndex < LastRowIndex);
204 }
205
206 bool containsPC(uint64_t PC) const { return (LowPC <= PC && PC < HighPC); }
207 };
208
209 struct LineTable {
210 LineTable();
211
212 /// Represents an invalid row
213 const uint32_t UnknownRowIndex = UINT32_MAX;
214
215 void appendRow(const DWARFDebugLine::Row &R) { Rows.push_back(R); }
216
217 void appendSequence(const DWARFDebugLine::Sequence &S) {
218 Sequences.push_back(S);
219 }
220
221 /// Returns the index of the row with file/line info for a given address,
222 /// or UnknownRowIndex if there is no such row.
223 uint32_t lookupAddress(uint64_t Address) const;
224
225 bool lookupAddressRange(uint64_t Address, uint64_t Size,
226 std::vector<uint32_t> &Result) const;
227
228 bool hasFileAtIndex(uint64_t FileIndex) const;
229
230 /// Extracts filename by its index in filename table in prologue.
231 /// Returns true on success.
232 bool getFileNameByIndex(uint64_t FileIndex, const char *CompDir,
233 DILineInfoSpecifier::FileLineInfoKind Kind,
234 std::string &Result) const;
235
236 /// Fills the Result argument with the file and line information
237 /// corresponding to Address. Returns true on success.
238 bool getFileLineInfoForAddress(uint64_t Address, const char *CompDir,
239 DILineInfoSpecifier::FileLineInfoKind Kind,
240 DILineInfo &Result) const;
241
242 void dump(raw_ostream &OS, DIDumpOptions DumpOptions) const;
243 void clear();
244
245 /// Parse prologue and all rows.
246 bool parse(DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr,
247 const DWARFContext &Ctx, const DWARFUnit *U,
248 raw_ostream *OS = nullptr);
249
250 using RowVector = std::vector<Row>;
251 using RowIter = RowVector::const_iterator;
252 using SequenceVector = std::vector<Sequence>;
253 using SequenceIter = SequenceVector::const_iterator;
254
255 struct Prologue Prologue;
256 RowVector Rows;
257 SequenceVector Sequences;
258
259 private:
260 uint32_t findRowInSeq(const DWARFDebugLine::Sequence &Seq,
261 uint64_t Address) const;
262 Optional<StringRef> getSourceByIndex(uint64_t FileIndex,
263 DILineInfoSpecifier::FileLineInfoKind Kind) const;
264 };
265
266 const LineTable *getLineTable(uint32_t Offset) const;
267 const LineTable *getOrParseLineTable(DWARFDataExtractor &DebugLineData,
268 uint32_t Offset, const DWARFContext &C,
269 const DWARFUnit *U);
270
271private:
272 struct ParsingState {
273 ParsingState(struct LineTable *LT);
274
275 void resetRowAndSequence();
276 void appendRowToMatrix(uint32_t Offset);
277
278 /// Line table we're currently parsing.
279 struct LineTable *LineTable;
280 /// The row number that starts at zero for the prologue, and increases for
281 /// each row added to the matrix.
282 unsigned RowNumber = 0;
283 struct Row Row;
284 struct Sequence Sequence;
285 };
286
287 using LineTableMapTy = std::map<uint32_t, LineTable>;
288 using LineTableIter = LineTableMapTy::iterator;
289 using LineTableConstIter = LineTableMapTy::const_iterator;
290
291 LineTableMapTy LineTableMap;
292};
293
294} // end namespace llvm
295
296#endif // LLVM_DEBUGINFO_DWARFDEBUGLINE_H