blob: 890eb28c1e00d86fb3dd375111073c9455e52dec [file] [log] [blame]
Benjamin Kramer5acab502011-09-15 02:12:05 +00001//===-- 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 Kaylord55d7012013-01-25 22:50:58 +000013#include "DWARFRelocMap.h"
Benjamin Kramer5acab502011-09-15 02:12:05 +000014#include "llvm/Support/DataExtractor.h"
15#include <map>
Alexey Samsonov45be7932012-08-30 07:49:50 +000016#include <string>
Benjamin Kramer5acab502011-09-15 02:12:05 +000017#include <vector>
18
19namespace llvm {
20
21class raw_ostream;
22
23class DWARFDebugLine {
24public:
Andrew Kaylord55d7012013-01-25 22:50:58 +000025 DWARFDebugLine(const RelocAddrMap* LineInfoRelocMap) : RelocMap(LineInfoRelocMap) {}
Benjamin Kramer5acab502011-09-15 02:12:05 +000026 struct FileNameEntry {
Craig Toppere73658d2014-04-28 04:05:08 +000027 FileNameEntry() : Name(nullptr), DirIdx(0), ModTime(0), Length(0) {}
Benjamin Kramer5acab502011-09-15 02:12:05 +000028
Alexey Samsonove16e16a2012-07-19 07:03:58 +000029 const char *Name;
Benjamin Kramer5acab502011-09-15 02:12:05 +000030 uint64_t DirIdx;
31 uint64_t ModTime;
32 uint64_t Length;
33 };
34
35 struct Prologue {
36 Prologue()
David Blaikie1d4736e2014-02-24 23:58:54 +000037 : TotalLength(0), Version(0), PrologueLength(0), MinInstLength(0),
38 MaxOpsPerInst(0), DefaultIsStmt(0), LineBase(0), LineRange(0),
39 OpcodeBase(0) {}
Benjamin Kramer5acab502011-09-15 02:12:05 +000040
41 // The size in bytes of the statement information for this compilation unit
42 // (not including the total_length field itself).
43 uint32_t TotalLength;
44 // Version identifier for the statement information format.
45 uint16_t Version;
46 // The number of bytes following the prologue_length field to the beginning
47 // of the first byte of the statement program itself.
48 uint32_t PrologueLength;
49 // The size in bytes of the smallest target machine instruction. Statement
50 // program opcodes that alter the address register first multiply their
51 // operands by this value.
52 uint8_t MinInstLength;
David Blaikie1d4736e2014-02-24 23:58:54 +000053 // The maximum number of individual operations that may be encoded in an
54 // instruction.
55 uint8_t MaxOpsPerInst;
Benjamin Kramer5acab502011-09-15 02:12:05 +000056 // The initial value of theis_stmtregister.
57 uint8_t DefaultIsStmt;
58 // This parameter affects the meaning of the special opcodes. See below.
59 int8_t LineBase;
60 // This parameter affects the meaning of the special opcodes. See below.
61 uint8_t LineRange;
62 // The number assigned to the first special opcode.
63 uint8_t OpcodeBase;
64 std::vector<uint8_t> StandardOpcodeLengths;
Alexey Samsonove16e16a2012-07-19 07:03:58 +000065 std::vector<const char*> IncludeDirectories;
Benjamin Kramer5acab502011-09-15 02:12:05 +000066 std::vector<FileNameEntry> FileNames;
67
68 // Length of the prologue in bytes.
69 uint32_t getLength() const {
70 return PrologueLength + sizeof(TotalLength) + sizeof(Version) +
71 sizeof(PrologueLength);
72 }
73 // Length of the line table data in bytes (not including the prologue).
74 uint32_t getStatementTableLength() const {
75 return TotalLength + sizeof(TotalLength) - getLength();
76 }
77 int32_t getMaxLineIncrementForSpecialOpcode() const {
78 return LineBase + (int8_t)LineRange - 1;
79 }
80 void dump(raw_ostream &OS) const;
81 void clear() {
82 TotalLength = Version = PrologueLength = 0;
83 MinInstLength = LineBase = LineRange = OpcodeBase = 0;
84 StandardOpcodeLengths.clear();
85 IncludeDirectories.clear();
86 FileNames.clear();
87 }
Benjamin Kramer5acab502011-09-15 02:12:05 +000088 };
89
90 // Standard .debug_line state machine structure.
91 struct Row {
92 Row(bool default_is_stmt = false) { reset(default_is_stmt); }
93 /// Called after a row is appended to the matrix.
94 void postAppend();
95 void reset(bool default_is_stmt);
96 void dump(raw_ostream &OS) const;
97
Alexey Samsonov947228c2012-08-07 11:46:57 +000098 static bool orderByAddress(const Row& LHS, const Row& RHS) {
99 return LHS.Address < RHS.Address;
100 }
101
Benjamin Kramer5acab502011-09-15 02:12:05 +0000102 // The program-counter value corresponding to a machine instruction
103 // generated by the compiler.
104 uint64_t Address;
105 // An unsigned integer indicating a source line number. Lines are numbered
106 // beginning at 1. The compiler may emit the value 0 in cases where an
107 // instruction cannot be attributed to any source line.
108 uint32_t Line;
109 // An unsigned integer indicating a column number within a source line.
110 // Columns are numbered beginning at 1. The value 0 is reserved to indicate
111 // that a statement begins at the 'left edge' of the line.
112 uint16_t Column;
113 // An unsigned integer indicating the identity of the source file
114 // corresponding to a machine instruction.
115 uint16_t File;
116 // An unsigned integer whose value encodes the applicable instruction set
117 // architecture for the current instruction.
118 uint8_t Isa;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000119 // An unsigned integer representing the DWARF path discriminator value
120 // for this location.
121 uint32_t Discriminator;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000122 // A boolean indicating that the current instruction is the beginning of a
123 // statement.
124 uint8_t IsStmt:1,
125 // A boolean indicating that the current instruction is the
126 // beginning of a basic block.
127 BasicBlock:1,
128 // A boolean indicating that the current address is that of the
129 // first byte after the end of a sequence of target machine
130 // instructions.
131 EndSequence:1,
132 // A boolean indicating that the current address is one (of possibly
133 // many) where execution should be suspended for an entry breakpoint
134 // of a function.
135 PrologueEnd:1,
136 // A boolean indicating that the current address is one (of possibly
137 // many) where execution should be suspended for an exit breakpoint
138 // of a function.
139 EpilogueBegin:1;
140 };
141
Alexey Samsonov947228c2012-08-07 11:46:57 +0000142 // Represents a series of contiguous machine instructions. Line table for each
143 // compilation unit may consist of multiple sequences, which are not
144 // guaranteed to be in the order of ascending instruction address.
145 struct Sequence {
146 // Sequence describes instructions at address range [LowPC, HighPC)
147 // and is described by line table rows [FirstRowIndex, LastRowIndex).
148 uint64_t LowPC;
149 uint64_t HighPC;
150 unsigned FirstRowIndex;
151 unsigned LastRowIndex;
152 bool Empty;
153
154 Sequence() { reset(); }
155 void reset() {
156 LowPC = 0;
157 HighPC = 0;
158 FirstRowIndex = 0;
159 LastRowIndex = 0;
160 Empty = true;
161 }
162 static bool orderByLowPC(const Sequence& LHS, const Sequence& RHS) {
163 return LHS.LowPC < RHS.LowPC;
164 }
165 bool isValid() const {
166 return !Empty && (LowPC < HighPC) && (FirstRowIndex < LastRowIndex);
167 }
168 bool containsPC(uint64_t pc) const {
169 return (LowPC <= pc && pc < HighPC);
170 }
171 };
172
Benjamin Kramer5acab502011-09-15 02:12:05 +0000173 struct LineTable {
174 void appendRow(const DWARFDebugLine::Row &state) { Rows.push_back(state); }
Alexey Samsonov947228c2012-08-07 11:46:57 +0000175 void appendSequence(const DWARFDebugLine::Sequence &sequence) {
176 Sequences.push_back(sequence);
177 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000178 void clear() {
179 Prologue.clear();
180 Rows.clear();
Alexey Samsonov947228c2012-08-07 11:46:57 +0000181 Sequences.clear();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000182 }
183
Alexey Samsonov947228c2012-08-07 11:46:57 +0000184 // Returns the index of the row with file/line info for a given address,
185 // or -1 if there is no such row.
186 uint32_t lookupAddress(uint64_t address) const;
Alexey Samsonov45be7932012-08-30 07:49:50 +0000187
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000188 bool lookupAddressRange(uint64_t address,
189 uint64_t size,
190 std::vector<uint32_t>& result) const;
191
Alexey Samsonov45be7932012-08-30 07:49:50 +0000192 // Extracts filename by its index in filename table in prologue.
193 // Returns true on success.
194 bool getFileNameByIndex(uint64_t FileIndex,
195 bool NeedsAbsoluteFilePath,
196 std::string &Result) const;
197
Benjamin Kramer5acab502011-09-15 02:12:05 +0000198 void dump(raw_ostream &OS) const;
199
200 struct Prologue Prologue;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000201 typedef std::vector<Row> RowVector;
202 typedef RowVector::const_iterator RowIter;
203 typedef std::vector<Sequence> SequenceVector;
204 typedef SequenceVector::const_iterator SequenceIter;
205 RowVector Rows;
206 SequenceVector Sequences;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000207 };
208
Alexey Samsonov947228c2012-08-07 11:46:57 +0000209 struct State : public Row, public Sequence, public LineTable {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000210 // Special row codes.
211 enum {
212 StartParsingLineTable = 0,
213 DoneParsingLineTable = -1
214 };
215
Benjamin Kramer679e1752011-09-15 20:43:18 +0000216 State() : row(StartParsingLineTable) {}
Nick Lewycky4d044922011-09-15 03:41:51 +0000217 virtual ~State();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000218
219 virtual void appendRowToMatrix(uint32_t offset);
Alexey Samsonov947228c2012-08-07 11:46:57 +0000220 virtual void finalize();
221 virtual void reset() {
222 Row::reset(Prologue.DefaultIsStmt);
223 Sequence::reset();
224 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000225
226 // The row number that starts at zero for the prologue, and increases for
227 // each row added to the matrix.
228 unsigned row;
229 };
230
231 struct DumpingState : public State {
232 DumpingState(raw_ostream &OS) : OS(OS) {}
Nick Lewycky4d044922011-09-15 03:41:51 +0000233 virtual ~DumpingState();
Craig Topper85482992014-03-05 07:52:44 +0000234 void finalize() override;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000235 private:
236 raw_ostream &OS;
237 };
238
239 static bool parsePrologue(DataExtractor debug_line_data, uint32_t *offset_ptr,
240 Prologue *prologue);
241 /// Parse a single line table (prologue and all rows).
242 static bool parseStatementTable(DataExtractor debug_line_data,
Andrew Kaylord55d7012013-01-25 22:50:58 +0000243 const RelocAddrMap *RMap,
Benjamin Kramer5acab502011-09-15 02:12:05 +0000244 uint32_t *offset_ptr, State &state);
245
Benjamin Kramer5acab502011-09-15 02:12:05 +0000246 const LineTable *getLineTable(uint32_t offset) const;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000247 const LineTable *getOrParseLineTable(DataExtractor debug_line_data,
248 uint32_t offset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000249
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000250private:
Benjamin Kramer5acab502011-09-15 02:12:05 +0000251 typedef std::map<uint32_t, LineTable> LineTableMapTy;
252 typedef LineTableMapTy::iterator LineTableIter;
253 typedef LineTableMapTy::const_iterator LineTableConstIter;
254
Andrew Kaylord55d7012013-01-25 22:50:58 +0000255 const RelocAddrMap *RelocMap;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000256 LineTableMapTy LineTableMap;
257};
258
259}
260
261#endif