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 &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)) {
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 *i = std::lower_bound(CUs.begin(), CUs.end(), offset,
159 DILineInfo DWARFContext::getLineInfoForAddress(uint64_t address,
160 DILineInfoSpecifier specifier) {
161 // First, get the offset of the compile unit.
162 uint32_t cuOffset = getDebugAranges()->findAddress(address);
163 // Retrieve the compile unit.
164 DWARFCompileUnit *cu = getCompileUnitForOffset(cuOffset);
167 SmallString<16> fileName("<invalid>");
168 SmallString<16> functionName("<invalid>");
171 if (specifier.needs(DILineInfoSpecifier::FunctionName)) {
172 const DWARFDebugInfoEntryMinimal *function_die =
173 cu->getFunctionDIEForAddress(address);
175 if (const char *name = function_die->getSubprogramName(cu))
179 if (specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
180 // Get the line table for this compile unit.
181 const DWARFDebugLine::LineTable *lineTable = getLineTableForCompileUnit(cu);
183 // Get the index of the row we're looking for in the line table.
184 uint32_t rowIndex = lineTable->lookupAddress(address);
185 if (rowIndex != -1U) {
186 const DWARFDebugLine::Row &row = lineTable->Rows[rowIndex];
187 // Take file/line info from the line table.
188 const DWARFDebugLine::FileNameEntry &fileNameEntry =
189 lineTable->Prologue.FileNames[row.File - 1];
190 fileName = fileNameEntry.Name;
191 if (specifier.needs(DILineInfoSpecifier::AbsoluteFilePath) &&
192 sys::path::is_relative(fileName.str())) {
193 // Append include directory of file (if it is present in line table)
194 // and compilation directory of compile unit to make path absolute.
195 const char *includeDir = 0;
196 if (uint64_t includeDirIndex = fileNameEntry.DirIdx) {
197 includeDir = lineTable->Prologue
198 .IncludeDirectories[includeDirIndex - 1];
200 SmallString<16> absFileName;
201 if (includeDir == 0 || sys::path::is_relative(includeDir)) {
202 if (const char *compilationDir = cu->getCompilationDir())
203 sys::path::append(absFileName, compilationDir);
206 sys::path::append(absFileName, includeDir);
208 sys::path::append(absFileName, fileName.str());
209 fileName = absFileName;
216 return DILineInfo(fileName, functionName, line, column);
219 void DWARFContextInMemory::anchor() { }