Refactor fetching file/line info from DWARFContext to simplify the
[oota-llvm.git] / lib / DebugInfo / DWARFContext.cpp
1 //===-- DWARFContext.cpp --------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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"
16 #include <algorithm>
17 using namespace llvm;
18 using namespace dwarf;
19
20 void DWARFContext::dump(raw_ostream &OS) {
21   OS << ".debug_abbrev contents:\n";
22   getDebugAbbrev()->dump(OS);
23
24   OS << "\n.debug_info contents:\n";
25   for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
26     getCompileUnitAtIndex(i)->dump(OS);
27
28   OS << "\n.debug_aranges contents:\n";
29   DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
30   uint32_t offset = 0;
31   DWARFDebugArangeSet set;
32   while (set.extract(arangesData, &offset))
33     set.dump(OS);
34
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();
40     unsigned stmtOffset =
41       cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
42                                                            -1U);
43     if (stmtOffset != -1U) {
44       DataExtractor lineData(getLineSection(), isLittleEndian(),
45                              savedAddressByteSize);
46       DWARFDebugLine::DumpingState state(OS);
47       DWARFDebugLine::parseStatementTable(lineData, &stmtOffset, state);
48     }
49   }
50
51   OS << "\n.debug_str contents:\n";
52   DataExtractor strData(getStringSection(), isLittleEndian(), 0);
53   offset = 0;
54   uint32_t lastOffset = 0;
55   while (const char *s = strData.getCStr(&offset)) {
56     OS << format("0x%8.8x: \"%s\"\n", lastOffset, s);
57     lastOffset = offset;
58   }
59
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);
67   offset = 0;
68   DWARFDebugRangeList rangeList;
69   while (rangeList.extract(rangesData, &offset))
70     rangeList.dump(OS);
71 }
72
73 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
74   if (Abbrev)
75     return Abbrev.get();
76
77   DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
78
79   Abbrev.reset(new DWARFDebugAbbrev());
80   Abbrev->parse(abbrData);
81   return Abbrev.get();
82 }
83
84 const DWARFDebugAranges *DWARFContext::getDebugAranges() {
85   if (Aranges)
86     return Aranges.get();
87
88   DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
89
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);
94   return Aranges.get();
95 }
96
97 const DWARFDebugLine::LineTable *
98 DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
99   if (!Line)
100     Line.reset(new DWARFDebugLine());
101
102   unsigned stmtOffset =
103     cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
104                                                          -1U);
105   if (stmtOffset == -1U)
106     return 0; // No line table for this compile unit.
107
108   // See if the line table is cached.
109   if (const DWARFDebugLine::LineTable *lt = Line->getLineTable(stmtOffset))
110     return lt;
111
112   // We have to parse it first.
113   DataExtractor lineData(getLineSection(), isLittleEndian(),
114                          cu->getAddressByteSize());
115   return Line->getOrParseLineTable(lineData, stmtOffset);
116 }
117
118 void DWARFContext::parseCompileUnits() {
119   uint32_t offset = 0;
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)) {
125       CUs.pop_back();
126       break;
127     }
128
129     offset = CUs.back().getNextCompileUnitOffset();
130   }
131 }
132
133 namespace {
134   struct OffsetComparator {
135     bool operator()(const DWARFCompileUnit &LHS,
136                     const DWARFCompileUnit &RHS) const {
137       return LHS.getOffset() < RHS.getOffset();
138     }
139     bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const {
140       return LHS.getOffset() < RHS;
141     }
142     bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const {
143       return LHS < RHS.getOffset();
144     }
145   };
146 }
147
148 DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
149   if (CUs.empty())
150     parseCompileUnits();
151
152   DWARFCompileUnit *CU = std::lower_bound(CUs.begin(), CUs.end(), Offset,
153                                           OffsetComparator());
154   if (CU != CUs.end())
155     return &*CU;
156   return 0;
157 }
158
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);
164 }
165
166 static bool getFileNameForCompileUnit(
167     DWARFCompileUnit *CU, const DWARFDebugLine::LineTable *LineTable,
168     uint64_t FileIndex, bool NeedsAbsoluteFilePath, std::string &FileName) {
169   if (CU == 0 ||
170       LineTable == 0 ||
171       !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
172                                      FileName))
173     return false;
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);
179     }
180     sys::path::append(AbsolutePath, FileName);
181     FileName = AbsolutePath.str();
182   }
183   return true;
184 }
185
186 bool
187 DWARFContext::getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
188                                             uint64_t Address,
189                                             bool NeedsAbsoluteFilePath,
190                                             std::string &FileName,
191                                             uint32_t &Line, uint32_t &Column) {
192   // Get the line table for this compile unit.
193   const DWARFDebugLine::LineTable *LineTable = getLineTableForCompileUnit(CU);
194   if (!LineTable)
195     return false;
196   // Get the index of row we're looking for in the line table.
197   uint32_t RowIndex = LineTable->lookupAddress(Address);
198   if (RowIndex == -1U)
199     return false;
200   // Take file number and line/column from the row.
201   const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
202   if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
203                                  NeedsAbsoluteFilePath, FileName))
204     return false;
205   Line = Row.Line;
206   Column = Row.Column;
207   return true;
208 }
209
210 DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
211     DILineInfoSpecifier Specifier) {
212   DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
213   if (!CU)
214     return DILineInfo();
215   std::string FileName = "<invalid>";
216   std::string FunctionName = "<invalid>";
217   uint32_t Line = 0;
218   uint32_t Column = 0;
219   if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
220     const DWARFDebugInfoEntryMinimal *FunctionDIE =
221         CU->getFunctionDIEForAddress(Address);
222     if (FunctionDIE) {
223       if (const char *Name = FunctionDIE->getSubprogramName(CU))
224         FunctionName = Name;
225     }
226   }
227   if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
228     const bool NeedsAbsoluteFilePath =
229         Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
230     getFileLineInfoForCompileUnit(CU, Address, NeedsAbsoluteFilePath,
231                                   FileName, Line, Column);
232   }
233   return DILineInfo(StringRef(FileName), StringRef(FunctionName),
234                     Line, Column);
235 }
236
237 void DWARFContextInMemory::anchor() { }