1 //===-- DWARFContext.cpp --------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DWARFContext.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Support/Dwarf.h"
13 #include "llvm/Support/Format.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Support/raw_ostream.h"
18 using namespace dwarf;
20 void DWARFContext::dump(raw_ostream &OS) {
21 OS << ".debug_abbrev contents:\n";
22 getDebugAbbrev()->dump(OS);
24 OS << "\n.debug_info contents:\n";
25 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
26 getCompileUnitAtIndex(i)->dump(OS);
28 OS << "\n.debug_aranges contents:\n";
29 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
31 DWARFDebugArangeSet set;
32 while (set.extract(arangesData, &offset))
35 uint8_t savedAddressByteSize = 0;
36 OS << "\n.debug_lines contents:\n";
37 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
38 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
39 savedAddressByteSize = cu->getAddressByteSize();
41 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
43 if (stmtOffset != -1U) {
44 DataExtractor lineData(getLineSection(), isLittleEndian(),
45 savedAddressByteSize);
46 DWARFDebugLine::DumpingState state(OS);
47 DWARFDebugLine::parseStatementTable(lineData, &stmtOffset, state);
51 OS << "\n.debug_str contents:\n";
52 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
54 uint32_t lastOffset = 0;
55 while (const char *s = strData.getCStr(&offset)) {
56 OS << format("0x%8.8x: \"%s\"\n", lastOffset, s);
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);
68 DWARFDebugRangeList rangeList;
69 while (rangeList.extract(rangesData, &offset))
73 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
77 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
79 Abbrev.reset(new DWARFDebugAbbrev());
80 Abbrev->parse(abbrData);
84 const DWARFDebugAranges *DWARFContext::getDebugAranges() {
88 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
90 Aranges.reset(new DWARFDebugAranges());
91 Aranges->extract(arangesData);
92 if (Aranges->isEmpty()) // No aranges in file, generate them from the DIEs.
93 Aranges->generate(this);
97 const DWARFDebugLine::LineTable *
98 DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
100 Line.reset(new DWARFDebugLine());
102 unsigned stmtOffset =
103 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
105 if (stmtOffset == -1U)
106 return 0; // No line table for this compile unit.
108 // See if the line table is cached.
109 if (const DWARFDebugLine::LineTable *lt = Line->getLineTable(stmtOffset))
112 // We have to parse it first.
113 DataExtractor lineData(getLineSection(), isLittleEndian(),
114 cu->getAddressByteSize());
115 return Line->getOrParseLineTable(lineData, stmtOffset);
118 void DWARFContext::parseCompileUnits() {
120 const DataExtractor &DIData = DataExtractor(getInfoSection(),
121 isLittleEndian(), 0);
122 while (DIData.isValidOffset(offset)) {
123 CUs.push_back(DWARFCompileUnit(*this));
124 if (!CUs.back().extract(DIData, &offset)) {
129 offset = CUs.back().getNextCompileUnitOffset();
134 struct OffsetComparator {
135 bool operator()(const DWARFCompileUnit &LHS,
136 const DWARFCompileUnit &RHS) const {
137 return LHS.getOffset() < RHS.getOffset();
139 bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const {
140 return LHS.getOffset() < RHS;
142 bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const {
143 return LHS < RHS.getOffset();
148 DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
152 DWARFCompileUnit *CU = std::lower_bound(CUs.begin(), CUs.end(), Offset,
159 DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
160 // First, get the offset of the compile unit.
161 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
162 // Retrieve the compile unit.
163 return getCompileUnitForOffset(CUOffset);
166 static bool getFileNameForCompileUnit(
167 DWARFCompileUnit *CU, const DWARFDebugLine::LineTable *LineTable,
168 uint64_t FileIndex, bool NeedsAbsoluteFilePath, std::string &FileName) {
171 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
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);
180 sys::path::append(AbsolutePath, FileName);
181 FileName = AbsolutePath.str();
186 static 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)
192 // Get the index of row we're looking for in the line table.
193 uint32_t RowIndex = LineTable->lookupAddress(Address);
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))
206 DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
207 DILineInfoSpecifier Specifier) {
208 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
211 std::string FileName = "<invalid>";
212 std::string FunctionName = "<invalid>";
215 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
216 // 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))
227 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
228 const DWARFDebugLine::LineTable *LineTable =
229 getLineTableForCompileUnit(CU);
230 const bool NeedsAbsoluteFilePath =
231 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
232 getFileLineInfoForCompileUnit(CU, LineTable, Address,
233 NeedsAbsoluteFilePath,
234 FileName, Line, Column);
236 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
240 DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
241 DILineInfoSpecifier Specifier) {
242 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
244 return DIInliningInfo();
246 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
247 CU->getInlinedChainForAddress(Address);
248 if (InlinedChain.size() == 0)
249 return DIInliningInfo();
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>";
260 // Get function name if necessary.
261 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
262 if (const char *Name = FunctionDIE.getSubroutineName(CU))
265 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
266 const bool NeedsAbsoluteFilePath =
267 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
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);
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);
284 // Get call file/line/column of a current DIE.
286 FunctionDIE.getCallerFrame(CU, CallFile, CallLine, CallColumn);
289 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
291 InliningInfo.addFrame(Frame);
296 void DWARFContextInMemory::anchor() { }