215effac41ffc6dc3795bb82e0d9c630f622553a
[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/Support/Format.h"
12 #include "llvm/Support/raw_ostream.h"
13 using namespace llvm;
14
15 void DWARFContext::dump(raw_ostream &OS) {
16   OS << ".debug_abbrev contents:\n";
17   getDebugAbbrev()->dump(OS);
18
19   OS << "\n.debug_info contents:\n";
20   for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
21     getCompileUnitAtIndex(i)->dump(OS);
22
23   OS << "\n.debug_aranges contents:\n";
24   DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
25   uint32_t offset = 0;
26   DWARFDebugArangeSet set;
27   while (set.extract(arangesData, &offset))
28     set.dump(OS);
29
30   OS << "\n.debug_lines contents:\n";
31   // FIXME: must be done per CU.
32   DataExtractor lineData(getLineSection(), isLittleEndian(), /*FIXME*/8);
33   DWARFDebugLine::dump(lineData, OS);
34
35   OS << "\n.debug_str contents:\n";
36   DataExtractor strData(getStringSection(), isLittleEndian(), 0);
37   offset = 0;
38   uint32_t lastOffset = 0;
39   while (const char *s = strData.getCStr(&offset)) {
40     OS << format("0x%8.8x: \"%s\"\n", lastOffset, s);
41     lastOffset = offset;
42   }
43 }
44
45 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
46   if (Abbrev)
47     return Abbrev.get();
48
49   DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
50
51   Abbrev.reset(new DWARFDebugAbbrev());
52   Abbrev->parse(abbrData);
53   return Abbrev.get();
54 }
55
56 const DWARFDebugAranges *DWARFContext::getDebugAranges() {
57   if (Aranges)
58     return Aranges.get();
59
60   DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
61
62   Aranges.reset(new DWARFDebugAranges());
63   Aranges->extract(arangesData);
64   if (Aranges->isEmpty()) // No aranges in file, generate them from the DIEs.
65     Aranges->generate(this);
66   return Aranges.get();
67 }
68
69 const DWARFDebugLine *DWARFContext::getDebugLine() {
70   if (Line)
71     return Line.get();
72
73   DataExtractor lineData(getLineSection(), isLittleEndian(), 0);
74
75   Line.reset(new DWARFDebugLine());
76   Line->parse(lineData);
77   return Line.get();
78 }
79
80 void DWARFContext::parseCompileUnits() {
81   uint32_t offset = 0;
82   const DataExtractor &debug_info_data = DataExtractor(getInfoSection(),
83                                                        isLittleEndian(), 0);
84   while (debug_info_data.isValidOffset(offset)) {
85     CUs.push_back(DWARFCompileUnit(*this));
86     if (!CUs.back().extract(debug_info_data, &offset)) {
87       CUs.pop_back();
88       break;
89     }
90
91     offset = CUs.back().getNextCompileUnitOffset();
92   }
93 }