blob: 241f55eaed2de2ea881c23138951953c4617e67c [file] [log] [blame]
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +00001//===-- DWARFContext.cpp --------------------------------------------------===//
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#include "DWARFContext.h"
Alexey Samsonov71d94f82012-07-19 07:03:58 +000011#include "llvm/ADT/SmallString.h"
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000012#include "llvm/Support/Dwarf.h"
Benjamin Kramer34f864f2011-09-15 16:57:13 +000013#include "llvm/Support/Format.h"
Alexey Samsonov71d94f82012-07-19 07:03:58 +000014#include "llvm/Support/Path.h"
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000015#include "llvm/Support/raw_ostream.h"
Benjamin Kramer101b1c52011-09-15 20:43:22 +000016#include <algorithm>
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000017using namespace llvm;
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000018using namespace dwarf;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000019
20void DWARFContext::dump(raw_ostream &OS) {
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000021 OS << ".debug_abbrev contents:\n";
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000022 getDebugAbbrev()->dump(OS);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000023
24 OS << "\n.debug_info contents:\n";
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000025 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
26 getCompileUnitAtIndex(i)->dump(OS);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000027
28 OS << "\n.debug_aranges contents:\n";
29 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
30 uint32_t offset = 0;
31 DWARFDebugArangeSet set;
32 while (set.extract(arangesData, &offset))
33 set.dump(OS);
Benjamin Kramerb848e972011-09-15 02:12:05 +000034
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000035 uint8_t savedAddressByteSize = 0;
Benjamin Kramerb848e972011-09-15 02:12:05 +000036 OS << "\n.debug_lines contents:\n";
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000037 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
38 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000039 savedAddressByteSize = cu->getAddressByteSize();
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000040 unsigned stmtOffset =
41 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
42 -1U);
43 if (stmtOffset != -1U) {
44 DataExtractor lineData(getLineSection(), isLittleEndian(),
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000045 savedAddressByteSize);
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000046 DWARFDebugLine::DumpingState state(OS);
47 DWARFDebugLine::parseStatementTable(lineData, &stmtOffset, state);
48 }
49 }
Benjamin Kramer34f864f2011-09-15 16:57:13 +000050
51 OS << "\n.debug_str contents:\n";
52 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
53 offset = 0;
54 uint32_t lastOffset = 0;
55 while (const char *s = strData.getCStr(&offset)) {
56 OS << format("0x%8.8x: \"%s\"\n", lastOffset, s);
57 lastOffset = offset;
58 }
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000059
60 OS << "\n.debug_ranges contents:\n";
61 // In fact, different compile units may have different address byte
62 // sizes, but for simplicity we just use the address byte size of the last
63 // compile unit (there is no easy and fast way to associate address range
64 // list and the compile unit it describes).
65 DataExtractor rangesData(getRangeSection(), isLittleEndian(),
66 savedAddressByteSize);
67 offset = 0;
68 DWARFDebugRangeList rangeList;
69 while (rangeList.extract(rangesData, &offset))
70 rangeList.dump(OS);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000071}
72
73const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
74 if (Abbrev)
75 return Abbrev.get();
76
77 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
78
79 Abbrev.reset(new DWARFDebugAbbrev());
80 Abbrev->parse(abbrData);
81 return Abbrev.get();
82}
83
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000084const DWARFDebugAranges *DWARFContext::getDebugAranges() {
85 if (Aranges)
86 return Aranges.get();
87
88 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
89
90 Aranges.reset(new DWARFDebugAranges());
91 Aranges->extract(arangesData);
Benjamin Kramer10df8062011-09-14 20:52:27 +000092 if (Aranges->isEmpty()) // No aranges in file, generate them from the DIEs.
93 Aranges->generate(this);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000094 return Aranges.get();
95}
96
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +000097const DWARFDebugLine::LineTable *
98DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
99 if (!Line)
100 Line.reset(new DWARFDebugLine());
Benjamin Kramerb848e972011-09-15 02:12:05 +0000101
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000102 unsigned stmtOffset =
103 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
104 -1U);
105 if (stmtOffset == -1U)
106 return 0; // No line table for this compile unit.
Benjamin Kramerb848e972011-09-15 02:12:05 +0000107
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000108 // See if the line table is cached.
109 if (const DWARFDebugLine::LineTable *lt = Line->getLineTable(stmtOffset))
110 return lt;
111
112 // We have to parse it first.
113 DataExtractor lineData(getLineSection(), isLittleEndian(),
114 cu->getAddressByteSize());
115 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramerb848e972011-09-15 02:12:05 +0000116}
117
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000118void DWARFContext::parseCompileUnits() {
119 uint32_t offset = 0;
120 const DataExtractor &debug_info_data = DataExtractor(getInfoSection(),
121 isLittleEndian(), 0);
122 while (debug_info_data.isValidOffset(offset)) {
123 CUs.push_back(DWARFCompileUnit(*this));
124 if (!CUs.back().extract(debug_info_data, &offset)) {
125 CUs.pop_back();
126 break;
127 }
128
129 offset = CUs.back().getNextCompileUnitOffset();
130 }
131}
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000132
133namespace {
134 struct OffsetComparator {
135 bool operator()(const DWARFCompileUnit &LHS,
136 const DWARFCompileUnit &RHS) const {
137 return LHS.getOffset() < RHS.getOffset();
138 }
139 bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const {
140 return LHS.getOffset() < RHS;
141 }
142 bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const {
143 return LHS < RHS.getOffset();
144 }
145 };
146}
147
Alexey Samsonov38a63812012-08-30 07:49:50 +0000148DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000149 if (CUs.empty())
150 parseCompileUnits();
151
Alexey Samsonov38a63812012-08-30 07:49:50 +0000152 DWARFCompileUnit *CU = std::lower_bound(CUs.begin(), CUs.end(), Offset,
153 OffsetComparator());
154 if (CU != CUs.end())
155 return &*CU;
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000156 return 0;
157}
158
Alexey Samsonov38a63812012-08-30 07:49:50 +0000159DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer9013db32011-09-15 21:59:13 +0000160 // First, get the offset of the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000161 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000162 // Retrieve the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000163 return getCompileUnitForOffset(CUOffset);
164}
165
166static bool getFileNameForCompileUnit(
167 DWARFCompileUnit *CU, const DWARFDebugLine::LineTable *LineTable,
168 uint64_t FileIndex, bool NeedsAbsoluteFilePath, std::string &FileName) {
169 if (CU == 0 ||
170 LineTable == 0 ||
171 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
172 FileName))
173 return false;
174 if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
175 // We may still need to append compilation directory of compile unit.
176 SmallString<16> AbsolutePath;
177 if (const char *CompilationDir = CU->getCompilationDir()) {
178 sys::path::append(AbsolutePath, CompilationDir);
179 }
180 sys::path::append(AbsolutePath, FileName);
181 FileName = AbsolutePath.str();
182 }
183 return true;
184}
185
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000186static bool getFileLineInfoForCompileUnit(
187 DWARFCompileUnit *CU, const DWARFDebugLine::LineTable *LineTable,
188 uint64_t Address, bool NeedsAbsoluteFilePath, std::string &FileName,
189 uint32_t &Line, uint32_t &Column) {
190 if (CU == 0 || LineTable == 0)
Alexey Samsonov38a63812012-08-30 07:49:50 +0000191 return false;
192 // Get the index of row we're looking for in the line table.
193 uint32_t RowIndex = LineTable->lookupAddress(Address);
194 if (RowIndex == -1U)
195 return false;
196 // Take file number and line/column from the row.
197 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
198 if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
199 NeedsAbsoluteFilePath, FileName))
200 return false;
201 Line = Row.Line;
202 Column = Row.Column;
203 return true;
204}
205
206DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
207 DILineInfoSpecifier Specifier) {
208 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
209 if (!CU)
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000210 return DILineInfo();
Alexey Samsonov38a63812012-08-30 07:49:50 +0000211 std::string FileName = "<invalid>";
212 std::string FunctionName = "<invalid>";
213 uint32_t Line = 0;
214 uint32_t Column = 0;
215 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000216 // The address may correspond to instruction in some inlined function,
217 // so we have to build the chain of inlined functions and take the
218 // name of the topmost function in it.
219 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
220 CU->getInlinedChainForAddress(Address);
221 if (InlinedChain.size() > 0) {
222 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain[0];
223 if (const char *Name = TopFunctionDIE.getSubroutineName(CU))
Alexey Samsonov38a63812012-08-30 07:49:50 +0000224 FunctionName = Name;
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000225 }
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000226 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000227 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000228 const DWARFDebugLine::LineTable *LineTable =
229 getLineTableForCompileUnit(CU);
Alexey Samsonov38a63812012-08-30 07:49:50 +0000230 const bool NeedsAbsoluteFilePath =
231 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000232 getFileLineInfoForCompileUnit(CU, LineTable, Address,
233 NeedsAbsoluteFilePath,
Alexey Samsonov38a63812012-08-30 07:49:50 +0000234 FileName, Line, Column);
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000235 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000236 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
237 Line, Column);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000238}
David Blaikie2d24e2a2011-12-20 02:50:00 +0000239
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000240DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
241 DILineInfoSpecifier Specifier) {
242 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
243 if (!CU)
244 return DIInliningInfo();
245
246 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
247 CU->getInlinedChainForAddress(Address);
248 if (InlinedChain.size() == 0)
249 return DIInliningInfo();
250
251 DIInliningInfo InliningInfo;
252 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
253 const DWARFDebugLine::LineTable *LineTable = 0;
254 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
255 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain[i];
256 std::string FileName = "<invalid>";
257 std::string FunctionName = "<invalid>";
258 uint32_t Line = 0;
259 uint32_t Column = 0;
260 // Get function name if necessary.
261 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
262 if (const char *Name = FunctionDIE.getSubroutineName(CU))
263 FunctionName = Name;
264 }
265 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
266 const bool NeedsAbsoluteFilePath =
267 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
268 if (i == 0) {
269 // For the topmost frame, initialize the line table of this
270 // compile unit and fetch file/line info from it.
271 LineTable = getLineTableForCompileUnit(CU);
272 // For the topmost routine, get file/line info from line table.
273 getFileLineInfoForCompileUnit(CU, LineTable, Address,
274 NeedsAbsoluteFilePath,
275 FileName, Line, Column);
276 } else {
277 // Otherwise, use call file, call line and call column from
278 // previous DIE in inlined chain.
279 getFileNameForCompileUnit(CU, LineTable, CallFile,
280 NeedsAbsoluteFilePath, FileName);
281 Line = CallLine;
282 Column = CallColumn;
283 }
284 // Get call file/line/column of a current DIE.
285 if (i + 1 < n) {
286 FunctionDIE.getCallerFrame(CU, CallFile, CallLine, CallColumn);
287 }
288 }
289 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
290 Line, Column);
291 InliningInfo.addFrame(Frame);
292 }
293 return InliningInfo;
294}
295
David Blaikie2d24e2a2011-12-20 02:50:00 +0000296void DWARFContextInMemory::anchor() { }