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"
15 using namespace dwarf;
17 void DWARFContext::dump(raw_ostream &OS) {
18 OS << ".debug_abbrev contents:\n";
19 getDebugAbbrev()->dump(OS);
21 OS << "\n.debug_info contents:\n";
22 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
23 getCompileUnitAtIndex(i)->dump(OS);
25 OS << "\n.debug_aranges contents:\n";
26 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
28 DWARFDebugArangeSet set;
29 while (set.extract(arangesData, &offset))
32 OS << "\n.debug_lines contents:\n";
33 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
34 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
36 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
38 if (stmtOffset != -1U) {
39 DataExtractor lineData(getLineSection(), isLittleEndian(),
40 cu->getAddressByteSize());
41 DWARFDebugLine::DumpingState state(OS);
42 DWARFDebugLine::parseStatementTable(lineData, &stmtOffset, state);
46 OS << "\n.debug_str contents:\n";
47 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
49 uint32_t lastOffset = 0;
50 while (const char *s = strData.getCStr(&offset)) {
51 OS << format("0x%8.8x: \"%s\"\n", lastOffset, s);
56 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
60 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
62 Abbrev.reset(new DWARFDebugAbbrev());
63 Abbrev->parse(abbrData);
67 const DWARFDebugAranges *DWARFContext::getDebugAranges() {
71 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
73 Aranges.reset(new DWARFDebugAranges());
74 Aranges->extract(arangesData);
75 if (Aranges->isEmpty()) // No aranges in file, generate them from the DIEs.
76 Aranges->generate(this);
80 const DWARFDebugLine::LineTable *
81 DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
83 Line.reset(new DWARFDebugLine());
86 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
88 if (stmtOffset == -1U)
89 return 0; // No line table for this compile unit.
91 // See if the line table is cached.
92 if (const DWARFDebugLine::LineTable *lt = Line->getLineTable(stmtOffset))
95 // We have to parse it first.
96 DataExtractor lineData(getLineSection(), isLittleEndian(),
97 cu->getAddressByteSize());
98 return Line->getOrParseLineTable(lineData, stmtOffset);
101 void DWARFContext::parseCompileUnits() {
103 const DataExtractor &debug_info_data = DataExtractor(getInfoSection(),
104 isLittleEndian(), 0);
105 while (debug_info_data.isValidOffset(offset)) {
106 CUs.push_back(DWARFCompileUnit(*this));
107 if (!CUs.back().extract(debug_info_data, &offset)) {
112 offset = CUs.back().getNextCompileUnitOffset();