blob: a8c0669b738b7055d1954294a6d8ac825dec561d [file] [log] [blame]
Benjamin Kramerb848e972011-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
13#include "llvm/Support/DataExtractor.h"
14#include <map>
Benjamin Kramerb848e972011-09-15 02:12:05 +000015#include <vector>
16
17namespace llvm {
18
19class raw_ostream;
20
21class DWARFDebugLine {
22public:
23 struct FileNameEntry {
Alexey Samsonov71d94f82012-07-19 07:03:58 +000024 FileNameEntry() : Name(0), DirIdx(0), ModTime(0), Length(0) {}
Benjamin Kramerb848e972011-09-15 02:12:05 +000025
Alexey Samsonov71d94f82012-07-19 07:03:58 +000026 const char *Name;
Benjamin Kramerb848e972011-09-15 02:12:05 +000027 uint64_t DirIdx;
28 uint64_t ModTime;
29 uint64_t Length;
30 };
31
32 struct Prologue {
33 Prologue()
34 : TotalLength(0), Version(0), PrologueLength(0), MinInstLength(0),
35 DefaultIsStmt(0), LineBase(0), LineRange(0), OpcodeBase(0) {}
36
37 // The size in bytes of the statement information for this compilation unit
38 // (not including the total_length field itself).
39 uint32_t TotalLength;
40 // Version identifier for the statement information format.
41 uint16_t Version;
42 // The number of bytes following the prologue_length field to the beginning
43 // of the first byte of the statement program itself.
44 uint32_t PrologueLength;
45 // The size in bytes of the smallest target machine instruction. Statement
46 // program opcodes that alter the address register first multiply their
47 // operands by this value.
48 uint8_t MinInstLength;
49 // The initial value of theis_stmtregister.
50 uint8_t DefaultIsStmt;
51 // This parameter affects the meaning of the special opcodes. See below.
52 int8_t LineBase;
53 // This parameter affects the meaning of the special opcodes. See below.
54 uint8_t LineRange;
55 // The number assigned to the first special opcode.
56 uint8_t OpcodeBase;
57 std::vector<uint8_t> StandardOpcodeLengths;
Alexey Samsonov71d94f82012-07-19 07:03:58 +000058 std::vector<const char*> IncludeDirectories;
Benjamin Kramerb848e972011-09-15 02:12:05 +000059 std::vector<FileNameEntry> FileNames;
60
61 // Length of the prologue in bytes.
62 uint32_t getLength() const {
63 return PrologueLength + sizeof(TotalLength) + sizeof(Version) +
64 sizeof(PrologueLength);
65 }
66 // Length of the line table data in bytes (not including the prologue).
67 uint32_t getStatementTableLength() const {
68 return TotalLength + sizeof(TotalLength) - getLength();
69 }
70 int32_t getMaxLineIncrementForSpecialOpcode() const {
71 return LineBase + (int8_t)LineRange - 1;
72 }
73 void dump(raw_ostream &OS) const;
74 void clear() {
75 TotalLength = Version = PrologueLength = 0;
76 MinInstLength = LineBase = LineRange = OpcodeBase = 0;
77 StandardOpcodeLengths.clear();
78 IncludeDirectories.clear();
79 FileNames.clear();
80 }
Benjamin Kramerb848e972011-09-15 02:12:05 +000081 };
82
83 // Standard .debug_line state machine structure.
84 struct Row {
85 Row(bool default_is_stmt = false) { reset(default_is_stmt); }
86 /// Called after a row is appended to the matrix.
87 void postAppend();
88 void reset(bool default_is_stmt);
89 void dump(raw_ostream &OS) const;
90
91 // The program-counter value corresponding to a machine instruction
92 // generated by the compiler.
93 uint64_t Address;
94 // An unsigned integer indicating a source line number. Lines are numbered
95 // beginning at 1. The compiler may emit the value 0 in cases where an
96 // instruction cannot be attributed to any source line.
97 uint32_t Line;
98 // An unsigned integer indicating a column number within a source line.
99 // Columns are numbered beginning at 1. The value 0 is reserved to indicate
100 // that a statement begins at the 'left edge' of the line.
101 uint16_t Column;
102 // An unsigned integer indicating the identity of the source file
103 // corresponding to a machine instruction.
104 uint16_t File;
105 // An unsigned integer whose value encodes the applicable instruction set
106 // architecture for the current instruction.
107 uint8_t Isa;
108 // A boolean indicating that the current instruction is the beginning of a
109 // statement.
110 uint8_t IsStmt:1,
111 // A boolean indicating that the current instruction is the
112 // beginning of a basic block.
113 BasicBlock:1,
114 // A boolean indicating that the current address is that of the
115 // first byte after the end of a sequence of target machine
116 // instructions.
117 EndSequence:1,
118 // A boolean indicating that the current address is one (of possibly
119 // many) where execution should be suspended for an entry breakpoint
120 // of a function.
121 PrologueEnd:1,
122 // A boolean indicating that the current address is one (of possibly
123 // many) where execution should be suspended for an exit breakpoint
124 // of a function.
125 EpilogueBegin:1;
126 };
127
128 struct LineTable {
129 void appendRow(const DWARFDebugLine::Row &state) { Rows.push_back(state); }
130 void clear() {
131 Prologue.clear();
132 Rows.clear();
133 }
134
135 uint32_t lookupAddress(uint64_t address, uint64_t cu_high_pc) const;
136 void dump(raw_ostream &OS) const;
137
138 struct Prologue Prologue;
139 std::vector<Row> Rows;
140 };
141
142 struct State : public Row, public LineTable {
143 // Special row codes.
144 enum {
145 StartParsingLineTable = 0,
146 DoneParsingLineTable = -1
147 };
148
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000149 State() : row(StartParsingLineTable) {}
Nick Lewycky6bc4e712011-09-15 03:41:51 +0000150 virtual ~State();
Benjamin Kramerb848e972011-09-15 02:12:05 +0000151
152 virtual void appendRowToMatrix(uint32_t offset);
153 virtual void finalize(uint32_t offset) { row = DoneParsingLineTable; }
154 virtual void reset() { Row::reset(Prologue.DefaultIsStmt); }
155
156 // The row number that starts at zero for the prologue, and increases for
157 // each row added to the matrix.
158 unsigned row;
159 };
160
161 struct DumpingState : public State {
162 DumpingState(raw_ostream &OS) : OS(OS) {}
Nick Lewycky6bc4e712011-09-15 03:41:51 +0000163 virtual ~DumpingState();
Benjamin Kramerb848e972011-09-15 02:12:05 +0000164 virtual void finalize(uint32_t offset);
165 private:
166 raw_ostream &OS;
167 };
168
169 static bool parsePrologue(DataExtractor debug_line_data, uint32_t *offset_ptr,
170 Prologue *prologue);
171 /// Parse a single line table (prologue and all rows).
172 static bool parseStatementTable(DataExtractor debug_line_data,
173 uint32_t *offset_ptr, State &state);
174
Benjamin Kramerb848e972011-09-15 02:12:05 +0000175 const LineTable *getLineTable(uint32_t offset) const;
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000176 const LineTable *getOrParseLineTable(DataExtractor debug_line_data,
177 uint32_t offset);
Benjamin Kramerb848e972011-09-15 02:12:05 +0000178
Benjamin Kramer89aedba2011-09-15 03:11:09 +0000179private:
Benjamin Kramerb848e972011-09-15 02:12:05 +0000180 typedef std::map<uint32_t, LineTable> LineTableMapTy;
181 typedef LineTableMapTy::iterator LineTableIter;
182 typedef LineTableMapTy::const_iterator LineTableConstIter;
183
184 LineTableMapTy LineTableMap;
185};
186
187}
188
189#endif