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/Support/Dwarf.h"
12 #include "llvm/Support/Format.h"
13 #include "llvm/Support/raw_ostream.h"
16 using namespace dwarf;
18 void DWARFContext::dump(raw_ostream &OS) {
19 OS << ".debug_abbrev contents:\n";
20 getDebugAbbrev()->dump(OS);
22 OS << "\n.debug_info contents:\n";
23 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
24 getCompileUnitAtIndex(i)->dump(OS);
26 OS << "\n.debug_aranges contents:\n";
27 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
29 DWARFDebugArangeSet set;
30 while (set.extract(arangesData, &offset))
33 OS << "\n.debug_lines contents:\n";
34 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
35 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
37 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
39 if (stmtOffset != -1U) {
40 DataExtractor lineData(getLineSection(), isLittleEndian(),
41 cu->getAddressByteSize());
42 DWARFDebugLine::DumpingState state(OS);
43 DWARFDebugLine::parseStatementTable(lineData, &stmtOffset, state);
47 OS << "\n.debug_str contents:\n";
48 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
50 uint32_t lastOffset = 0;
51 while (const char *s = strData.getCStr(&offset)) {
52 OS << format("0x%8.8x: \"%s\"\n", lastOffset, s);
57 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
61 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
63 Abbrev.reset(new DWARFDebugAbbrev());
64 Abbrev->parse(abbrData);
68 const DWARFDebugAranges *DWARFContext::getDebugAranges() {
72 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
74 Aranges.reset(new DWARFDebugAranges());
75 Aranges->extract(arangesData);
76 if (Aranges->isEmpty()) // No aranges in file, generate them from the DIEs.
77 Aranges->generate(this);
81 const DWARFDebugLine::LineTable *
82 DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
84 Line.reset(new DWARFDebugLine());
87 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
89 if (stmtOffset == -1U)
90 return 0; // No line table for this compile unit.
92 // See if the line table is cached.
93 if (const DWARFDebugLine::LineTable *lt = Line->getLineTable(stmtOffset))
96 // We have to parse it first.
97 DataExtractor lineData(getLineSection(), isLittleEndian(),
98 cu->getAddressByteSize());
99 return Line->getOrParseLineTable(lineData, stmtOffset);
102 void DWARFContext::parseCompileUnits() {
104 const DataExtractor &debug_info_data = DataExtractor(getInfoSection(),
105 isLittleEndian(), 0);
106 while (debug_info_data.isValidOffset(offset)) {
107 CUs.push_back(DWARFCompileUnit(*this));
108 if (!CUs.back().extract(debug_info_data, &offset)) {
113 offset = CUs.back().getNextCompileUnitOffset();
118 struct OffsetComparator {
119 bool operator()(const DWARFCompileUnit &LHS,
120 const DWARFCompileUnit &RHS) const {
121 return LHS.getOffset() < RHS.getOffset();
123 bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const {
124 return LHS.getOffset() < RHS;
126 bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const {
127 return LHS < RHS.getOffset();
132 DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t offset) {
136 DWARFCompileUnit *i = std::lower_bound(CUs.begin(), CUs.end(), offset,
143 DILineInfo DWARFContext::getLineInfoForAddress(uint64_t address) {
144 // First, get the offset of the compile unit.
145 uint32_t cuOffset = getDebugAranges()->findAddress(address);
146 // Retrieve the compile unit.
147 DWARFCompileUnit *cu = getCompileUnitForOffset(cuOffset);
149 return DILineInfo("<invalid>", 0, 0);
150 // Get the line table for this compile unit.
151 const DWARFDebugLine::LineTable *lineTable = getLineTableForCompileUnit(cu);
153 return DILineInfo("<invalid>", 0, 0);
154 // Get the index of the row we're looking for in the line table.
156 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_high_pc,
158 uint32_t rowIndex = lineTable->lookupAddress(address, hiPC);
160 return DILineInfo("<invalid>", 0, 0);
162 // From here, contruct the DILineInfo.
163 const DWARFDebugLine::Row &row = lineTable->Rows[rowIndex];
164 const std::string &fileName = lineTable->Prologue.FileNames[row.File-1].Name;
166 return DILineInfo(fileName.c_str(), row.Line, row.Column);
169 void DWARFContextInMemory::anchor() { }