a336f49b29b24f706ce5928c17c49c1a2b102f8e
[oota-llvm.git] / lib / DebugInfo / DWARFDebugLine.h
1 //===-- 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 "DWARFRelocMap.h"
14 #include "llvm/Support/DataExtractor.h"
15 #include <map>
16 #include <string>
17 #include <vector>
18
19 namespace llvm {
20
21 class raw_ostream;
22
23 class DWARFDebugLine {
24 public:
25   DWARFDebugLine(const RelocAddrMap* LineInfoRelocMap) : RelocMap(LineInfoRelocMap) {}
26   struct FileNameEntry {
27     FileNameEntry() : Name(0), DirIdx(0), ModTime(0), Length(0) {}
28
29     const char *Name;
30     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           MaxOpsPerInst(0), DefaultIsStmt(0), LineBase(0), LineRange(0),
39           OpcodeBase(0) {}
40
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;
53     // The maximum number of individual operations that may be encoded in an
54     // instruction.
55     uint8_t MaxOpsPerInst;
56     // 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;
65     std::vector<const char*> IncludeDirectories;
66     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     }
88   };
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
98     static bool orderByAddress(const Row& LHS, const Row& RHS) {
99       return LHS.Address < RHS.Address;
100     }
101
102     // 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;
119     // An unsigned integer representing the DWARF path discriminator value
120     // for this location.
121     uint32_t Discriminator;
122     // 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
142   // 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
173   struct LineTable {
174     void appendRow(const DWARFDebugLine::Row &state) { Rows.push_back(state); }
175     void appendSequence(const DWARFDebugLine::Sequence &sequence) {
176       Sequences.push_back(sequence);
177     }
178     void clear() {
179       Prologue.clear();
180       Rows.clear();
181       Sequences.clear();
182     }
183
184     // 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;
187
188     bool lookupAddressRange(uint64_t address,
189                             uint64_t size, 
190                             std::vector<uint32_t>& result) const;
191
192     // 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
198     void dump(raw_ostream &OS) const;
199
200     struct Prologue Prologue;
201     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;
207   };
208
209   struct State : public Row, public Sequence, public LineTable {
210     // Special row codes.
211     enum {
212       StartParsingLineTable = 0,
213       DoneParsingLineTable = -1
214     };
215
216     State() : row(StartParsingLineTable) {}
217     virtual ~State();
218
219     virtual void appendRowToMatrix(uint32_t offset);
220     virtual void finalize();
221     virtual void reset() {
222       Row::reset(Prologue.DefaultIsStmt);
223       Sequence::reset();
224     }
225
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) {}
233     virtual ~DumpingState();
234     void finalize() override;
235   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,
243                                   const RelocAddrMap *RMap,
244                                   uint32_t *offset_ptr, State &state);
245
246   const LineTable *getLineTable(uint32_t offset) const;
247   const LineTable *getOrParseLineTable(DataExtractor debug_line_data,
248                                        uint32_t offset);
249
250 private:
251   typedef std::map<uint32_t, LineTable> LineTableMapTy;
252   typedef LineTableMapTy::iterator LineTableIter;
253   typedef LineTableMapTy::const_iterator LineTableConstIter;
254
255   const RelocAddrMap *RelocMap;
256   LineTableMapTy LineTableMap;
257 };
258
259 }
260
261 #endif