Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 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" |
Benjamin Kramer | fe80f1d | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 11 | #include "llvm/Support/Dwarf.h" |
Benjamin Kramer | 34f864f | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 12 | #include "llvm/Support/Format.h" |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 13 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 101b1c5 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 14 | #include <algorithm> |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 15 | using namespace llvm; |
Benjamin Kramer | fe80f1d | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 16 | using namespace dwarf; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 17 | |
| 18 | void DWARFContext::dump(raw_ostream &OS) { |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 19 | OS << ".debug_abbrev contents:\n"; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 20 | getDebugAbbrev()->dump(OS); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 21 | |
| 22 | OS << "\n.debug_info contents:\n"; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 23 | for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) |
| 24 | getCompileUnitAtIndex(i)->dump(OS); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 25 | |
| 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 Kramer | b848e97 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 32 | |
| 33 | OS << "\n.debug_lines contents:\n"; |
Benjamin Kramer | fe80f1d | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 34 | 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 Kramer | 34f864f | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 46 | |
| 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 Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | const 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 Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 68 | const 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 Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 76 | if (Aranges->isEmpty()) // No aranges in file, generate them from the DIEs. |
| 77 | Aranges->generate(this); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 78 | return Aranges.get(); |
| 79 | } |
| 80 | |
Benjamin Kramer | c26ed9b | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 81 | const DWARFDebugLine::LineTable * |
| 82 | DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) { |
| 83 | if (!Line) |
| 84 | Line.reset(new DWARFDebugLine()); |
Benjamin Kramer | b848e97 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 85 | |
Benjamin Kramer | c26ed9b | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 86 | 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 Kramer | b848e97 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 91 | |
Benjamin Kramer | c26ed9b | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 92 | // 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 Kramer | b848e97 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 102 | void 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 Kramer | 101b1c5 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 116 | |
| 117 | namespace { |
| 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 | |
| 132 | DWARFCompileUnit *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 | |
| 143 | DILineInfo DWARFContext::getLineInfoForAddress(uint64_t address) { |
Benjamin Kramer | 9013db3 | 2011-09-15 21:59:13 +0000 | [diff] [blame^] | 144 | // First, get the offset of the compile unit. |
| 145 | uint32_t cuOffset = getDebugAranges()->findAddress(address); |
Benjamin Kramer | 101b1c5 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 146 | // Retrieve the compile unit. |
| 147 | DWARFCompileUnit *cu = getCompileUnitForOffset(cuOffset); |
Benjamin Kramer | f5b0acc | 2011-09-15 21:08:54 +0000 | [diff] [blame] | 148 | if (!cu) |
| 149 | return DILineInfo("<invalid>", 0, 0); |
Benjamin Kramer | 101b1c5 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 150 | // Get the line table for this compile unit. |
| 151 | const DWARFDebugLine::LineTable *lineTable = getLineTableForCompileUnit(cu); |
Benjamin Kramer | f5b0acc | 2011-09-15 21:08:54 +0000 | [diff] [blame] | 152 | if (!lineTable) |
| 153 | return DILineInfo("<invalid>", 0, 0); |
Benjamin Kramer | 101b1c5 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 154 | // 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 Kramer | f5b0acc | 2011-09-15 21:08:54 +0000 | [diff] [blame] | 159 | if (rowIndex == -1U) |
| 160 | return DILineInfo("<invalid>", 0, 0); |
Benjamin Kramer | 101b1c5 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 161 | |
| 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 | } |