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 typedef DWARFDebugLine::LineTable DWARFLineTable;
22 void DWARFContext::dump(raw_ostream &OS) {
23 OS << ".debug_abbrev contents:\n";
24 getDebugAbbrev()->dump(OS);
26 OS << "\n.debug_info contents:\n";
27 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
28 getCompileUnitAtIndex(i)->dump(OS);
30 OS << "\n.debug_aranges contents:\n";
31 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
33 DWARFDebugArangeSet set;
34 while (set.extract(arangesData, &offset))
37 uint8_t savedAddressByteSize = 0;
38 OS << "\n.debug_lines contents:\n";
39 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
40 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
41 savedAddressByteSize = cu->getAddressByteSize();
43 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
45 if (stmtOffset != -1U) {
46 DataExtractor lineData(getLineSection(), isLittleEndian(),
47 savedAddressByteSize);
48 DWARFDebugLine::DumpingState state(OS);
49 DWARFDebugLine::parseStatementTable(lineData, &stmtOffset, state);
53 OS << "\n.debug_str contents:\n";
54 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
56 uint32_t lastOffset = 0;
57 while (const char *s = strData.getCStr(&offset)) {
58 OS << format("0x%8.8x: \"%s\"\n", lastOffset, s);
62 OS << "\n.debug_ranges contents:\n";
63 // In fact, different compile units may have different address byte
64 // sizes, but for simplicity we just use the address byte size of the last
65 // compile unit (there is no easy and fast way to associate address range
66 // list and the compile unit it describes).
67 DataExtractor rangesData(getRangeSection(), isLittleEndian(),
68 savedAddressByteSize);
70 DWARFDebugRangeList rangeList;
71 while (rangeList.extract(rangesData, &offset))
75 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
79 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
81 Abbrev.reset(new DWARFDebugAbbrev());
82 Abbrev->parse(abbrData);
86 const DWARFDebugAranges *DWARFContext::getDebugAranges() {
90 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
92 Aranges.reset(new DWARFDebugAranges());
93 Aranges->extract(arangesData);
94 if (Aranges->isEmpty()) // No aranges in file, generate them from the DIEs.
95 Aranges->generate(this);
99 const DWARFLineTable *
100 DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
102 Line.reset(new DWARFDebugLine());
104 unsigned stmtOffset =
105 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
107 if (stmtOffset == -1U)
108 return 0; // No line table for this compile unit.
110 // See if the line table is cached.
111 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
114 // We have to parse it first.
115 DataExtractor lineData(getLineSection(), isLittleEndian(),
116 cu->getAddressByteSize());
117 return Line->getOrParseLineTable(lineData, stmtOffset);
120 void DWARFContext::parseCompileUnits() {
122 const DataExtractor &DIData = DataExtractor(getInfoSection(),
123 isLittleEndian(), 0);
124 while (DIData.isValidOffset(offset)) {
125 CUs.push_back(DWARFCompileUnit(*this));
126 if (!CUs.back().extract(DIData, &offset)) {
131 offset = CUs.back().getNextCompileUnitOffset();
136 struct OffsetComparator {
137 bool operator()(const DWARFCompileUnit &LHS,
138 const DWARFCompileUnit &RHS) const {
139 return LHS.getOffset() < RHS.getOffset();
141 bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const {
142 return LHS.getOffset() < RHS;
144 bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const {
145 return LHS < RHS.getOffset();
150 DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
154 DWARFCompileUnit *CU = std::lower_bound(CUs.begin(), CUs.end(), Offset,
161 DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
162 // First, get the offset of the compile unit.
163 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
164 // Retrieve the compile unit.
165 return getCompileUnitForOffset(CUOffset);
168 static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
169 const DWARFLineTable *LineTable,
171 bool NeedsAbsoluteFilePath,
172 std::string &FileName) {
175 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
178 if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
179 // We may still need to append compilation directory of compile unit.
180 SmallString<16> AbsolutePath;
181 if (const char *CompilationDir = CU->getCompilationDir()) {
182 sys::path::append(AbsolutePath, CompilationDir);
184 sys::path::append(AbsolutePath, FileName);
185 FileName = AbsolutePath.str();
190 static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
191 const DWARFLineTable *LineTable,
193 bool NeedsAbsoluteFilePath,
194 std::string &FileName,
195 uint32_t &Line, uint32_t &Column) {
196 if (CU == 0 || LineTable == 0)
198 // Get the index of row we're looking for in the line table.
199 uint32_t RowIndex = LineTable->lookupAddress(Address);
202 // Take file number and line/column from the row.
203 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
204 if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
205 NeedsAbsoluteFilePath, FileName))
212 DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
213 DILineInfoSpecifier Specifier) {
214 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
217 std::string FileName = "<invalid>";
218 std::string FunctionName = "<invalid>";
221 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
222 // The address may correspond to instruction in some inlined function,
223 // so we have to build the chain of inlined functions and take the
224 // name of the topmost function in it.
225 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
226 CU->getInlinedChainForAddress(Address);
227 if (InlinedChain.size() > 0) {
228 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain[0];
229 if (const char *Name = TopFunctionDIE.getSubroutineName(CU))
233 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
234 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
235 const bool NeedsAbsoluteFilePath =
236 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
237 getFileLineInfoForCompileUnit(CU, LineTable, Address,
238 NeedsAbsoluteFilePath,
239 FileName, Line, Column);
241 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
245 DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
246 DILineInfoSpecifier Specifier) {
247 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
249 return DIInliningInfo();
251 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
252 CU->getInlinedChainForAddress(Address);
253 if (InlinedChain.size() == 0)
254 return DIInliningInfo();
256 DIInliningInfo InliningInfo;
257 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
258 const DWARFLineTable *LineTable = 0;
259 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
260 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain[i];
261 std::string FileName = "<invalid>";
262 std::string FunctionName = "<invalid>";
265 // Get function name if necessary.
266 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
267 if (const char *Name = FunctionDIE.getSubroutineName(CU))
270 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
271 const bool NeedsAbsoluteFilePath =
272 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
274 // For the topmost frame, initialize the line table of this
275 // compile unit and fetch file/line info from it.
276 LineTable = getLineTableForCompileUnit(CU);
277 // For the topmost routine, get file/line info from line table.
278 getFileLineInfoForCompileUnit(CU, LineTable, Address,
279 NeedsAbsoluteFilePath,
280 FileName, Line, Column);
282 // Otherwise, use call file, call line and call column from
283 // previous DIE in inlined chain.
284 getFileNameForCompileUnit(CU, LineTable, CallFile,
285 NeedsAbsoluteFilePath, FileName);
289 // Get call file/line/column of a current DIE.
291 FunctionDIE.getCallerFrame(CU, CallFile, CallLine, CallColumn);
294 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
296 InliningInfo.addFrame(Frame);
301 void DWARFContextInMemory::anchor() { }