blob: 9e93cc86dd20a3b33abecb8ab57bcfa51b6ed558 [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 {
Alexey Samsonove16e16a2012-07-19 07:03:58 +000027 FileNameEntry() : Name(0), 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()
37 : TotalLength(0), Version(0), PrologueLength(0), MinInstLength(0),
38 DefaultIsStmt(0), LineBase(0), LineRange(0), OpcodeBase(0) {}
39
40 // The size in bytes of the statement information for this compilation unit
41 // (not including the total_length field itself).
42 uint32_t TotalLength;
43 // Version identifier for the statement information format.
44 uint16_t Version;
45 // The number of bytes following the prologue_length field to the beginning
46 // of the first byte of the statement program itself.
47 uint32_t PrologueLength;
48 // The size in bytes of the smallest target machine instruction. Statement
49 // program opcodes that alter the address register first multiply their
50 // operands by this value.
51 uint8_t MinInstLength;
52 // The initial value of theis_stmtregister.
53 uint8_t DefaultIsStmt;
54 // This parameter affects the meaning of the special opcodes. See below.
55 int8_t LineBase;
56 // This parameter affects the meaning of the special opcodes. See below.
57 uint8_t LineRange;
58 // The number assigned to the first special opcode.
59 uint8_t OpcodeBase;
60 std::vector<uint8_t> StandardOpcodeLengths;
Alexey Samsonove16e16a2012-07-19 07:03:58 +000061 std::vector<const char*> IncludeDirectories;
Benjamin Kramer5acab502011-09-15 02:12:05 +000062 std::vector<FileNameEntry> FileNames;
63
64 // Length of the prologue in bytes.
65 uint32_t getLength() const {
66 return PrologueLength + sizeof(TotalLength) + sizeof(Version) +
67 sizeof(PrologueLength);
68 }
69 // Length of the line table data in bytes (not including the prologue).
70 uint32_t getStatementTableLength() const {
71 return TotalLength + sizeof(TotalLength) - getLength();
72 }
73 int32_t getMaxLineIncrementForSpecialOpcode() const {
74 return LineBase + (int8_t)LineRange - 1;
75 }
76 void dump(raw_ostream &OS) const;
77 void clear() {
78 TotalLength = Version = PrologueLength = 0;
79 MinInstLength = LineBase = LineRange = OpcodeBase = 0;
80 StandardOpcodeLengths.clear();
81 IncludeDirectories.clear();
82 FileNames.clear();
83 }
Benjamin Kramer5acab502011-09-15 02:12:05 +000084 };
85
86 // Standard .debug_line state machine structure.
87 struct Row {
88 Row(bool default_is_stmt = false) { reset(default_is_stmt); }
89 /// Called after a row is appended to the matrix.
90 void postAppend();
91 void reset(bool default_is_stmt);
92 void dump(raw_ostream &OS) const;
93
Alexey Samsonov947228c2012-08-07 11:46:57 +000094 static bool orderByAddress(const Row& LHS, const Row& RHS) {
95 return LHS.Address < RHS.Address;
96 }
97
Benjamin Kramer5acab502011-09-15 02:12:05 +000098 // The program-counter value corresponding to a machine instruction
99 // generated by the compiler.
100 uint64_t Address;
101 // An unsigned integer indicating a source line number. Lines are numbered
102 // beginning at 1. The compiler may emit the value 0 in cases where an
103 // instruction cannot be attributed to any source line.
104 uint32_t Line;
105 // An unsigned integer indicating a column number within a source line.
106 // Columns are numbered beginning at 1. The value 0 is reserved to indicate
107 // that a statement begins at the 'left edge' of the line.
108 uint16_t Column;
109 // An unsigned integer indicating the identity of the source file
110 // corresponding to a machine instruction.
111 uint16_t File;
112 // An unsigned integer whose value encodes the applicable instruction set
113 // architecture for the current instruction.
114 uint8_t Isa;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000115 // An unsigned integer representing the DWARF path discriminator value
116 // for this location.
117 uint32_t Discriminator;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000118 // A boolean indicating that the current instruction is the beginning of a
119 // statement.
120 uint8_t IsStmt:1,
121 // A boolean indicating that the current instruction is the
122 // beginning of a basic block.
123 BasicBlock:1,
124 // A boolean indicating that the current address is that of the
125 // first byte after the end of a sequence of target machine
126 // instructions.
127 EndSequence:1,
128 // A boolean indicating that the current address is one (of possibly
129 // many) where execution should be suspended for an entry breakpoint
130 // of a function.
131 PrologueEnd:1,
132 // A boolean indicating that the current address is one (of possibly
133 // many) where execution should be suspended for an exit breakpoint
134 // of a function.
135 EpilogueBegin:1;
136 };
137
Alexey Samsonov947228c2012-08-07 11:46:57 +0000138 // Represents a series of contiguous machine instructions. Line table for each
139 // compilation unit may consist of multiple sequences, which are not
140 // guaranteed to be in the order of ascending instruction address.
141 struct Sequence {
142 // Sequence describes instructions at address range [LowPC, HighPC)
143 // and is described by line table rows [FirstRowIndex, LastRowIndex).
144 uint64_t LowPC;
145 uint64_t HighPC;
146 unsigned FirstRowIndex;
147 unsigned LastRowIndex;
148 bool Empty;
149
150 Sequence() { reset(); }
151 void reset() {
152 LowPC = 0;
153 HighPC = 0;
154 FirstRowIndex = 0;
155 LastRowIndex = 0;
156 Empty = true;
157 }
158 static bool orderByLowPC(const Sequence& LHS, const Sequence& RHS) {
159 return LHS.LowPC < RHS.LowPC;
160 }
161 bool isValid() const {
162 return !Empty && (LowPC < HighPC) && (FirstRowIndex < LastRowIndex);
163 }
164 bool containsPC(uint64_t pc) const {
165 return (LowPC <= pc && pc < HighPC);
166 }
167 };
168
Benjamin Kramer5acab502011-09-15 02:12:05 +0000169 struct LineTable {
170 void appendRow(const DWARFDebugLine::Row &state) { Rows.push_back(state); }
Alexey Samsonov947228c2012-08-07 11:46:57 +0000171 void appendSequence(const DWARFDebugLine::Sequence &sequence) {
172 Sequences.push_back(sequence);
173 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000174 void clear() {
175 Prologue.clear();
176 Rows.clear();
Alexey Samsonov947228c2012-08-07 11:46:57 +0000177 Sequences.clear();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000178 }
179
Alexey Samsonov947228c2012-08-07 11:46:57 +0000180 // Returns the index of the row with file/line info for a given address,
181 // or -1 if there is no such row.
182 uint32_t lookupAddress(uint64_t address) const;
Alexey Samsonov45be7932012-08-30 07:49:50 +0000183
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000184 bool lookupAddressRange(uint64_t address,
185 uint64_t size,
186 std::vector<uint32_t>& result) const;
187
Alexey Samsonov45be7932012-08-30 07:49:50 +0000188 // Extracts filename by its index in filename table in prologue.
189 // Returns true on success.
190 bool getFileNameByIndex(uint64_t FileIndex,
191 bool NeedsAbsoluteFilePath,
192 std::string &Result) const;
193
Benjamin Kramer5acab502011-09-15 02:12:05 +0000194 void dump(raw_ostream &OS) const;
195
196 struct Prologue Prologue;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000197 typedef std::vector<Row> RowVector;
198 typedef RowVector::const_iterator RowIter;
199 typedef std::vector<Sequence> SequenceVector;
200 typedef SequenceVector::const_iterator SequenceIter;
201 RowVector Rows;
202 SequenceVector Sequences;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000203 };
204
Alexey Samsonov947228c2012-08-07 11:46:57 +0000205 struct State : public Row, public Sequence, public LineTable {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000206 // Special row codes.
207 enum {
208 StartParsingLineTable = 0,
209 DoneParsingLineTable = -1
210 };
211
Benjamin Kramer679e1752011-09-15 20:43:18 +0000212 State() : row(StartParsingLineTable) {}
Nick Lewycky4d044922011-09-15 03:41:51 +0000213 virtual ~State();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000214
215 virtual void appendRowToMatrix(uint32_t offset);
Alexey Samsonov947228c2012-08-07 11:46:57 +0000216 virtual void finalize();
217 virtual void reset() {
218 Row::reset(Prologue.DefaultIsStmt);
219 Sequence::reset();
220 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000221
222 // The row number that starts at zero for the prologue, and increases for
223 // each row added to the matrix.
224 unsigned row;
225 };
226
227 struct DumpingState : public State {
228 DumpingState(raw_ostream &OS) : OS(OS) {}
Nick Lewycky4d044922011-09-15 03:41:51 +0000229 virtual ~DumpingState();
Alexey Samsonov947228c2012-08-07 11:46:57 +0000230 virtual void finalize();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000231 private:
232 raw_ostream &OS;
233 };
234
235 static bool parsePrologue(DataExtractor debug_line_data, uint32_t *offset_ptr,
236 Prologue *prologue);
237 /// Parse a single line table (prologue and all rows).
238 static bool parseStatementTable(DataExtractor debug_line_data,
Andrew Kaylord55d7012013-01-25 22:50:58 +0000239 const RelocAddrMap *RMap,
Benjamin Kramer5acab502011-09-15 02:12:05 +0000240 uint32_t *offset_ptr, State &state);
241
Benjamin Kramer5acab502011-09-15 02:12:05 +0000242 const LineTable *getLineTable(uint32_t offset) const;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000243 const LineTable *getOrParseLineTable(DataExtractor debug_line_data,
244 uint32_t offset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000245
Benjamin Kramer123bfbb2011-09-15 03:11:09 +0000246private:
Benjamin Kramer5acab502011-09-15 02:12:05 +0000247 typedef std::map<uint32_t, LineTable> LineTableMapTy;
248 typedef LineTableMapTy::iterator LineTableIter;
249 typedef LineTableMapTy::const_iterator LineTableConstIter;
250
Andrew Kaylord55d7012013-01-25 22:50:58 +0000251 const RelocAddrMap *RelocMap;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000252 LineTableMapTy LineTableMap;
253};
254
255}
256
257#endif