blob: 797662b083f1b904727fbec8c51031158e3f3d24 [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
35 OS << "\n.debug_lines contents:\n";
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000036 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
37 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
38 unsigned stmtOffset =
39 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
40 -1U);
41 if (stmtOffset != -1U) {
42 DataExtractor lineData(getLineSection(), isLittleEndian(),
43 cu->getAddressByteSize());
44 DWARFDebugLine::DumpingState state(OS);
45 DWARFDebugLine::parseStatementTable(lineData, &stmtOffset, state);
46 }
47 }
Benjamin Kramer34f864f2011-09-15 16:57:13 +000048
49 OS << "\n.debug_str contents:\n";
50 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
51 offset = 0;
52 uint32_t lastOffset = 0;
53 while (const char *s = strData.getCStr(&offset)) {
54 OS << format("0x%8.8x: \"%s\"\n", lastOffset, s);
55 lastOffset = offset;
56 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000057}
58
59const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
60 if (Abbrev)
61 return Abbrev.get();
62
63 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
64
65 Abbrev.reset(new DWARFDebugAbbrev());
66 Abbrev->parse(abbrData);
67 return Abbrev.get();
68}
69
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000070const DWARFDebugAranges *DWARFContext::getDebugAranges() {
71 if (Aranges)
72 return Aranges.get();
73
74 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
75
76 Aranges.reset(new DWARFDebugAranges());
77 Aranges->extract(arangesData);
Benjamin Kramer10df8062011-09-14 20:52:27 +000078 if (Aranges->isEmpty()) // No aranges in file, generate them from the DIEs.
79 Aranges->generate(this);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000080 return Aranges.get();
81}
82
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +000083const DWARFDebugLine::LineTable *
84DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
85 if (!Line)
86 Line.reset(new DWARFDebugLine());
Benjamin Kramerb848e972011-09-15 02:12:05 +000087
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +000088 unsigned stmtOffset =
89 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
90 -1U);
91 if (stmtOffset == -1U)
92 return 0; // No line table for this compile unit.
Benjamin Kramerb848e972011-09-15 02:12:05 +000093
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +000094 // See if the line table is cached.
95 if (const DWARFDebugLine::LineTable *lt = Line->getLineTable(stmtOffset))
96 return lt;
97
98 // We have to parse it first.
99 DataExtractor lineData(getLineSection(), isLittleEndian(),
100 cu->getAddressByteSize());
101 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramerb848e972011-09-15 02:12:05 +0000102}
103
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000104void DWARFContext::parseCompileUnits() {
105 uint32_t offset = 0;
106 const DataExtractor &debug_info_data = DataExtractor(getInfoSection(),
107 isLittleEndian(), 0);
108 while (debug_info_data.isValidOffset(offset)) {
109 CUs.push_back(DWARFCompileUnit(*this));
110 if (!CUs.back().extract(debug_info_data, &offset)) {
111 CUs.pop_back();
112 break;
113 }
114
115 offset = CUs.back().getNextCompileUnitOffset();
116 }
117}
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000118
119namespace {
120 struct OffsetComparator {
121 bool operator()(const DWARFCompileUnit &LHS,
122 const DWARFCompileUnit &RHS) const {
123 return LHS.getOffset() < RHS.getOffset();
124 }
125 bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const {
126 return LHS.getOffset() < RHS;
127 }
128 bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const {
129 return LHS < RHS.getOffset();
130 }
131 };
132}
133
134DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t offset) {
135 if (CUs.empty())
136 parseCompileUnits();
137
138 DWARFCompileUnit *i = std::lower_bound(CUs.begin(), CUs.end(), offset,
139 OffsetComparator());
140 if (i != CUs.end())
141 return &*i;
142 return 0;
143}
144
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000145DILineInfo DWARFContext::getLineInfoForAddress(uint64_t address,
146 DILineInfoSpecifier specifier) {
Benjamin Kramer9013db32011-09-15 21:59:13 +0000147 // First, get the offset of the compile unit.
148 uint32_t cuOffset = getDebugAranges()->findAddress(address);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000149 // Retrieve the compile unit.
150 DWARFCompileUnit *cu = getCompileUnitForOffset(cuOffset);
Benjamin Kramerf5b0acc2011-09-15 21:08:54 +0000151 if (!cu)
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000152 return DILineInfo();
Alexey Samsonov71d94f82012-07-19 07:03:58 +0000153 SmallString<16> fileName("<invalid>");
154 SmallString<16> functionName("<invalid>");
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000155 uint32_t line = 0;
156 uint32_t column = 0;
157 if (specifier.needs(DILineInfoSpecifier::FunctionName)) {
158 const DWARFDebugInfoEntryMinimal *function_die =
159 cu->getFunctionDIEForAddress(address);
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000160 if (function_die) {
161 if (const char *name = function_die->getSubprogramName(cu))
162 functionName = name;
163 }
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000164 }
165 if (specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
166 // Get the line table for this compile unit.
167 const DWARFDebugLine::LineTable *lineTable = getLineTableForCompileUnit(cu);
168 if (lineTable) {
169 // Get the index of the row we're looking for in the line table.
Alexey Samsonov351f83b2012-08-07 11:46:57 +0000170 uint32_t rowIndex = lineTable->lookupAddress(address);
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000171 if (rowIndex != -1U) {
172 const DWARFDebugLine::Row &row = lineTable->Rows[rowIndex];
173 // Take file/line info from the line table.
Alexey Samsonov71d94f82012-07-19 07:03:58 +0000174 const DWARFDebugLine::FileNameEntry &fileNameEntry =
175 lineTable->Prologue.FileNames[row.File - 1];
176 fileName = fileNameEntry.Name;
177 if (specifier.needs(DILineInfoSpecifier::AbsoluteFilePath) &&
178 sys::path::is_relative(fileName.str())) {
179 // Append include directory of file (if it is present in line table)
180 // and compilation directory of compile unit to make path absolute.
181 const char *includeDir = 0;
182 if (uint64_t includeDirIndex = fileNameEntry.DirIdx) {
183 includeDir = lineTable->Prologue
184 .IncludeDirectories[includeDirIndex - 1];
185 }
186 SmallString<16> absFileName;
187 if (includeDir == 0 || sys::path::is_relative(includeDir)) {
188 if (const char *compilationDir = cu->getCompilationDir())
189 sys::path::append(absFileName, compilationDir);
190 }
191 if (includeDir) {
192 sys::path::append(absFileName, includeDir);
193 }
194 sys::path::append(absFileName, fileName.str());
195 fileName = absFileName;
196 }
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000197 line = row.Line;
198 column = row.Column;
199 }
200 }
201 }
202 return DILineInfo(fileName, functionName, line, column);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000203}
David Blaikie2d24e2a2011-12-20 02:50:00 +0000204
205void DWARFContextInMemory::anchor() { }