blob: 215effac41ffc6dc3795bb82e0d9c630f622553a [file] [log] [blame]
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +00001//===-- 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"
Benjamin Kramer34f864f2011-09-15 16:57:13 +000011#include "llvm/Support/Format.h"
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000012#include "llvm/Support/raw_ostream.h"
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000013using namespace llvm;
14
15void DWARFContext::dump(raw_ostream &OS) {
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000016 OS << ".debug_abbrev contents:\n";
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000017 getDebugAbbrev()->dump(OS);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000018
19 OS << "\n.debug_info contents:\n";
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000020 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
21 getCompileUnitAtIndex(i)->dump(OS);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000022
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);
Benjamin Kramerb848e972011-09-15 02:12:05 +000029
30 OS << "\n.debug_lines contents:\n";
Benjamin Kramer34f864f2011-09-15 16:57:13 +000031 // FIXME: must be done per CU.
32 DataExtractor lineData(getLineSection(), isLittleEndian(), /*FIXME*/8);
Benjamin Kramerb848e972011-09-15 02:12:05 +000033 DWARFDebugLine::dump(lineData, OS);
Benjamin Kramer34f864f2011-09-15 16:57:13 +000034
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 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000043}
44
45const 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
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000056const 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);
Benjamin Kramer10df8062011-09-14 20:52:27 +000064 if (Aranges->isEmpty()) // No aranges in file, generate them from the DIEs.
65 Aranges->generate(this);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000066 return Aranges.get();
67}
68
Benjamin Kramerb848e972011-09-15 02:12:05 +000069const 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
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000080void 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}