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 OS << "\n.debug_lines contents:\n";
36 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
37 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
39 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
41 if (stmtOffset != -1U) {
42 DataExtractor lineData(getLineSection(), isLittleEndian(),
43 cu->getAddressByteSize());
44 DWARFDebugLine::DumpingState state(OS);
45 DWARFDebugLine::parseStatementTable(lineData, &stmtOffset, state);
49 OS << "\n.debug_str contents:\n";
50 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
52 uint32_t lastOffset = 0;
53 while (const char *s = strData.getCStr(&offset)) {
54 OS << format("0x%8.8x: \"%s\"\n", lastOffset, s);
59 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
63 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
65 Abbrev.reset(new DWARFDebugAbbrev());
66 Abbrev->parse(abbrData);
70 const DWARFDebugAranges *DWARFContext::getDebugAranges() {
74 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
76 Aranges.reset(new DWARFDebugAranges());
77 Aranges->extract(arangesData);
78 if (Aranges->isEmpty()) // No aranges in file, generate them from the DIEs.
79 Aranges->generate(this);
83 const DWARFDebugLine::LineTable *
84 DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
86 Line.reset(new DWARFDebugLine());
89 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
91 if (stmtOffset == -1U)
92 return 0; // No line table for this compile unit.
94 // See if the line table is cached.
95 if (const DWARFDebugLine::LineTable *lt = Line->getLineTable(stmtOffset))
98 // We have to parse it first.
99 DataExtractor lineData(getLineSection(), isLittleEndian(),
100 cu->getAddressByteSize());
101 return Line->getOrParseLineTable(lineData, stmtOffset);
104 void DWARFContext::parseCompileUnits() {
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)) {
115 offset = CUs.back().getNextCompileUnitOffset();
120 struct OffsetComparator {
121 bool operator()(const DWARFCompileUnit &LHS,
122 const DWARFCompileUnit &RHS) const {
123 return LHS.getOffset() < RHS.getOffset();
125 bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const {
126 return LHS.getOffset() < RHS;
128 bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const {
129 return LHS < RHS.getOffset();
134 DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t offset) {
138 DWARFCompileUnit *i = std::lower_bound(CUs.begin(), CUs.end(), offset,
145 DILineInfo DWARFContext::getLineInfoForAddress(uint64_t address,
146 DILineInfoSpecifier specifier) {
147 // First, get the offset of the compile unit.
148 uint32_t cuOffset = getDebugAranges()->findAddress(address);
149 // Retrieve the compile unit.
150 DWARFCompileUnit *cu = getCompileUnitForOffset(cuOffset);
153 SmallString<16> fileName("<invalid>");
154 SmallString<16> functionName("<invalid>");
157 if (specifier.needs(DILineInfoSpecifier::FunctionName)) {
158 const DWARFDebugInfoEntryMinimal *function_die =
159 cu->getFunctionDIEForAddress(address);
161 if (const char *name = function_die->getSubprogramName(cu))
165 if (specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
166 // Get the line table for this compile unit.
167 const DWARFDebugLine::LineTable *lineTable = getLineTableForCompileUnit(cu);
169 // Get the index of the row we're looking for in the line table.
170 uint32_t rowIndex = lineTable->lookupAddress(address);
171 if (rowIndex != -1U) {
172 const DWARFDebugLine::Row &row = lineTable->Rows[rowIndex];
173 // Take file/line info from the line table.
174 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];
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);
192 sys::path::append(absFileName, includeDir);
194 sys::path::append(absFileName, fileName.str());
195 fileName = absFileName;
202 return DILineInfo(fileName, functionName, line, column);
205 void DWARFContextInMemory::anchor() { }