blob: e1ac398b10116cf5eb11ded835a87f403bf4a417 [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 Kramerfe80f1d2011-09-15 18:02:20 +000011#include "llvm/Support/Dwarf.h"
Benjamin Kramer34f864f2011-09-15 16:57:13 +000012#include "llvm/Support/Format.h"
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000013#include "llvm/Support/raw_ostream.h"
Benjamin Kramer101b1c52011-09-15 20:43:22 +000014#include <algorithm>
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000015using namespace llvm;
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000016using namespace dwarf;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000017
18void DWARFContext::dump(raw_ostream &OS) {
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000019 OS << ".debug_abbrev contents:\n";
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000020 getDebugAbbrev()->dump(OS);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000021
22 OS << "\n.debug_info contents:\n";
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000023 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
24 getCompileUnitAtIndex(i)->dump(OS);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000025
26 OS << "\n.debug_aranges contents:\n";
27 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
28 uint32_t offset = 0;
29 DWARFDebugArangeSet set;
30 while (set.extract(arangesData, &offset))
31 set.dump(OS);
Benjamin Kramerb848e972011-09-15 02:12:05 +000032
33 OS << "\n.debug_lines contents:\n";
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000034 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
35 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
36 unsigned stmtOffset =
37 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
38 -1U);
39 if (stmtOffset != -1U) {
40 DataExtractor lineData(getLineSection(), isLittleEndian(),
41 cu->getAddressByteSize());
42 DWARFDebugLine::DumpingState state(OS);
43 DWARFDebugLine::parseStatementTable(lineData, &stmtOffset, state);
44 }
45 }
Benjamin Kramer34f864f2011-09-15 16:57:13 +000046
47 OS << "\n.debug_str contents:\n";
48 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
49 offset = 0;
50 uint32_t lastOffset = 0;
51 while (const char *s = strData.getCStr(&offset)) {
52 OS << format("0x%8.8x: \"%s\"\n", lastOffset, s);
53 lastOffset = offset;
54 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000055}
56
57const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
58 if (Abbrev)
59 return Abbrev.get();
60
61 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
62
63 Abbrev.reset(new DWARFDebugAbbrev());
64 Abbrev->parse(abbrData);
65 return Abbrev.get();
66}
67
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000068const DWARFDebugAranges *DWARFContext::getDebugAranges() {
69 if (Aranges)
70 return Aranges.get();
71
72 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
73
74 Aranges.reset(new DWARFDebugAranges());
75 Aranges->extract(arangesData);
Benjamin Kramer10df8062011-09-14 20:52:27 +000076 if (Aranges->isEmpty()) // No aranges in file, generate them from the DIEs.
77 Aranges->generate(this);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000078 return Aranges.get();
79}
80
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +000081const DWARFDebugLine::LineTable *
82DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
83 if (!Line)
84 Line.reset(new DWARFDebugLine());
Benjamin Kramerb848e972011-09-15 02:12:05 +000085
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +000086 unsigned stmtOffset =
87 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
88 -1U);
89 if (stmtOffset == -1U)
90 return 0; // No line table for this compile unit.
Benjamin Kramerb848e972011-09-15 02:12:05 +000091
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +000092 // See if the line table is cached.
93 if (const DWARFDebugLine::LineTable *lt = Line->getLineTable(stmtOffset))
94 return lt;
95
96 // We have to parse it first.
97 DataExtractor lineData(getLineSection(), isLittleEndian(),
98 cu->getAddressByteSize());
99 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramerb848e972011-09-15 02:12:05 +0000100}
101
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000102void DWARFContext::parseCompileUnits() {
103 uint32_t offset = 0;
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)) {
109 CUs.pop_back();
110 break;
111 }
112
113 offset = CUs.back().getNextCompileUnitOffset();
114 }
115}
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000116
117namespace {
118 struct OffsetComparator {
119 bool operator()(const DWARFCompileUnit &LHS,
120 const DWARFCompileUnit &RHS) const {
121 return LHS.getOffset() < RHS.getOffset();
122 }
123 bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const {
124 return LHS.getOffset() < RHS;
125 }
126 bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const {
127 return LHS < RHS.getOffset();
128 }
129 };
130}
131
132DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t offset) {
133 if (CUs.empty())
134 parseCompileUnits();
135
136 DWARFCompileUnit *i = std::lower_bound(CUs.begin(), CUs.end(), offset,
137 OffsetComparator());
138 if (i != CUs.end())
139 return &*i;
140 return 0;
141}
142
143DILineInfo DWARFContext::getLineInfoForAddress(uint64_t address) {
Benjamin Kramer9013db32011-09-15 21:59:13 +0000144 // First, get the offset of the compile unit.
145 uint32_t cuOffset = getDebugAranges()->findAddress(address);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000146 // Retrieve the compile unit.
147 DWARFCompileUnit *cu = getCompileUnitForOffset(cuOffset);
Benjamin Kramerf5b0acc2011-09-15 21:08:54 +0000148 if (!cu)
149 return DILineInfo("<invalid>", 0, 0);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000150 // Get the line table for this compile unit.
151 const DWARFDebugLine::LineTable *lineTable = getLineTableForCompileUnit(cu);
Benjamin Kramerf5b0acc2011-09-15 21:08:54 +0000152 if (!lineTable)
153 return DILineInfo("<invalid>", 0, 0);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000154 // Get the index of the row we're looking for in the line table.
155 uint64_t hiPC =
156 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_high_pc,
157 -1ULL);
158 uint32_t rowIndex = lineTable->lookupAddress(address, hiPC);
Benjamin Kramerf5b0acc2011-09-15 21:08:54 +0000159 if (rowIndex == -1U)
160 return DILineInfo("<invalid>", 0, 0);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000161
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;
165
166 return DILineInfo(fileName.c_str(), row.Line, row.Column);
167}