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" |
Alexey Samsonov | 71d94f8 | 2012-07-19 07:03:58 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | fe80f1d | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 12 | #include "llvm/Support/Dwarf.h" |
Benjamin Kramer | 34f864f | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Format.h" |
Alexey Samsonov | 71d94f8 | 2012-07-19 07:03:58 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Path.h" |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 15 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 101b1c5 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 16 | #include <algorithm> |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 17 | using namespace llvm; |
Benjamin Kramer | fe80f1d | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 18 | using namespace dwarf; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 19 | |
| 20 | void DWARFContext::dump(raw_ostream &OS) { |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 21 | OS << ".debug_abbrev contents:\n"; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 22 | getDebugAbbrev()->dump(OS); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 23 | |
| 24 | OS << "\n.debug_info contents:\n"; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 25 | for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) |
| 26 | getCompileUnitAtIndex(i)->dump(OS); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 27 | |
| 28 | OS << "\n.debug_aranges contents:\n"; |
| 29 | DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0); |
| 30 | uint32_t offset = 0; |
| 31 | DWARFDebugArangeSet set; |
| 32 | while (set.extract(arangesData, &offset)) |
| 33 | set.dump(OS); |
Benjamin Kramer | b848e97 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 34 | |
Alexey Samsonov | eceb5b9 | 2012-08-27 07:17:47 +0000 | [diff] [blame^] | 35 | uint8_t savedAddressByteSize = 0; |
Benjamin Kramer | b848e97 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 36 | OS << "\n.debug_lines contents:\n"; |
Benjamin Kramer | fe80f1d | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 37 | for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) { |
| 38 | DWARFCompileUnit *cu = getCompileUnitAtIndex(i); |
Alexey Samsonov | eceb5b9 | 2012-08-27 07:17:47 +0000 | [diff] [blame^] | 39 | savedAddressByteSize = cu->getAddressByteSize(); |
Benjamin Kramer | fe80f1d | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 40 | unsigned stmtOffset = |
| 41 | cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list, |
| 42 | -1U); |
| 43 | if (stmtOffset != -1U) { |
| 44 | DataExtractor lineData(getLineSection(), isLittleEndian(), |
Alexey Samsonov | eceb5b9 | 2012-08-27 07:17:47 +0000 | [diff] [blame^] | 45 | savedAddressByteSize); |
Benjamin Kramer | fe80f1d | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 46 | DWARFDebugLine::DumpingState state(OS); |
| 47 | DWARFDebugLine::parseStatementTable(lineData, &stmtOffset, state); |
| 48 | } |
| 49 | } |
Benjamin Kramer | 34f864f | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 50 | |
| 51 | OS << "\n.debug_str contents:\n"; |
| 52 | DataExtractor strData(getStringSection(), isLittleEndian(), 0); |
| 53 | offset = 0; |
| 54 | uint32_t lastOffset = 0; |
| 55 | while (const char *s = strData.getCStr(&offset)) { |
| 56 | OS << format("0x%8.8x: \"%s\"\n", lastOffset, s); |
| 57 | lastOffset = offset; |
| 58 | } |
Alexey Samsonov | eceb5b9 | 2012-08-27 07:17:47 +0000 | [diff] [blame^] | 59 | |
| 60 | OS << "\n.debug_ranges contents:\n"; |
| 61 | // In fact, different compile units may have different address byte |
| 62 | // sizes, but for simplicity we just use the address byte size of the last |
| 63 | // compile unit (there is no easy and fast way to associate address range |
| 64 | // list and the compile unit it describes). |
| 65 | DataExtractor rangesData(getRangeSection(), isLittleEndian(), |
| 66 | savedAddressByteSize); |
| 67 | offset = 0; |
| 68 | DWARFDebugRangeList rangeList; |
| 69 | while (rangeList.extract(rangesData, &offset)) |
| 70 | rangeList.dump(OS); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() { |
| 74 | if (Abbrev) |
| 75 | return Abbrev.get(); |
| 76 | |
| 77 | DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0); |
| 78 | |
| 79 | Abbrev.reset(new DWARFDebugAbbrev()); |
| 80 | Abbrev->parse(abbrData); |
| 81 | return Abbrev.get(); |
| 82 | } |
| 83 | |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 84 | const DWARFDebugAranges *DWARFContext::getDebugAranges() { |
| 85 | if (Aranges) |
| 86 | return Aranges.get(); |
| 87 | |
| 88 | DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0); |
| 89 | |
| 90 | Aranges.reset(new DWARFDebugAranges()); |
| 91 | Aranges->extract(arangesData); |
Benjamin Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 92 | if (Aranges->isEmpty()) // No aranges in file, generate them from the DIEs. |
| 93 | Aranges->generate(this); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 94 | return Aranges.get(); |
| 95 | } |
| 96 | |
Benjamin Kramer | c26ed9b | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 97 | const DWARFDebugLine::LineTable * |
| 98 | DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) { |
| 99 | if (!Line) |
| 100 | Line.reset(new DWARFDebugLine()); |
Benjamin Kramer | b848e97 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 101 | |
Benjamin Kramer | c26ed9b | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 102 | unsigned stmtOffset = |
| 103 | cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list, |
| 104 | -1U); |
| 105 | if (stmtOffset == -1U) |
| 106 | return 0; // No line table for this compile unit. |
Benjamin Kramer | b848e97 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 107 | |
Benjamin Kramer | c26ed9b | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 108 | // See if the line table is cached. |
| 109 | if (const DWARFDebugLine::LineTable *lt = Line->getLineTable(stmtOffset)) |
| 110 | return lt; |
| 111 | |
| 112 | // We have to parse it first. |
| 113 | DataExtractor lineData(getLineSection(), isLittleEndian(), |
| 114 | cu->getAddressByteSize()); |
| 115 | return Line->getOrParseLineTable(lineData, stmtOffset); |
Benjamin Kramer | b848e97 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 118 | void DWARFContext::parseCompileUnits() { |
| 119 | uint32_t offset = 0; |
| 120 | const DataExtractor &debug_info_data = DataExtractor(getInfoSection(), |
| 121 | isLittleEndian(), 0); |
| 122 | while (debug_info_data.isValidOffset(offset)) { |
| 123 | CUs.push_back(DWARFCompileUnit(*this)); |
| 124 | if (!CUs.back().extract(debug_info_data, &offset)) { |
| 125 | CUs.pop_back(); |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | offset = CUs.back().getNextCompileUnitOffset(); |
| 130 | } |
| 131 | } |
Benjamin Kramer | 101b1c5 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 132 | |
| 133 | namespace { |
| 134 | struct OffsetComparator { |
| 135 | bool operator()(const DWARFCompileUnit &LHS, |
| 136 | const DWARFCompileUnit &RHS) const { |
| 137 | return LHS.getOffset() < RHS.getOffset(); |
| 138 | } |
| 139 | bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const { |
| 140 | return LHS.getOffset() < RHS; |
| 141 | } |
| 142 | bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const { |
| 143 | return LHS < RHS.getOffset(); |
| 144 | } |
| 145 | }; |
| 146 | } |
| 147 | |
| 148 | DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t offset) { |
| 149 | if (CUs.empty()) |
| 150 | parseCompileUnits(); |
| 151 | |
| 152 | DWARFCompileUnit *i = std::lower_bound(CUs.begin(), CUs.end(), offset, |
| 153 | OffsetComparator()); |
| 154 | if (i != CUs.end()) |
| 155 | return &*i; |
| 156 | return 0; |
| 157 | } |
| 158 | |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 159 | DILineInfo DWARFContext::getLineInfoForAddress(uint64_t address, |
| 160 | DILineInfoSpecifier specifier) { |
Benjamin Kramer | 9013db3 | 2011-09-15 21:59:13 +0000 | [diff] [blame] | 161 | // First, get the offset of the compile unit. |
| 162 | uint32_t cuOffset = getDebugAranges()->findAddress(address); |
Benjamin Kramer | 101b1c5 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 163 | // Retrieve the compile unit. |
| 164 | DWARFCompileUnit *cu = getCompileUnitForOffset(cuOffset); |
Benjamin Kramer | f5b0acc | 2011-09-15 21:08:54 +0000 | [diff] [blame] | 165 | if (!cu) |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 166 | return DILineInfo(); |
Alexey Samsonov | 71d94f8 | 2012-07-19 07:03:58 +0000 | [diff] [blame] | 167 | SmallString<16> fileName("<invalid>"); |
| 168 | SmallString<16> functionName("<invalid>"); |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 169 | uint32_t line = 0; |
| 170 | uint32_t column = 0; |
| 171 | if (specifier.needs(DILineInfoSpecifier::FunctionName)) { |
| 172 | const DWARFDebugInfoEntryMinimal *function_die = |
| 173 | cu->getFunctionDIEForAddress(address); |
Alexey Samsonov | 9d26b0b | 2012-07-17 15:28:35 +0000 | [diff] [blame] | 174 | if (function_die) { |
| 175 | if (const char *name = function_die->getSubprogramName(cu)) |
| 176 | functionName = name; |
| 177 | } |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 178 | } |
| 179 | if (specifier.needs(DILineInfoSpecifier::FileLineInfo)) { |
| 180 | // Get the line table for this compile unit. |
| 181 | const DWARFDebugLine::LineTable *lineTable = getLineTableForCompileUnit(cu); |
| 182 | if (lineTable) { |
| 183 | // Get the index of the row we're looking for in the line table. |
Alexey Samsonov | 351f83b | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 184 | uint32_t rowIndex = lineTable->lookupAddress(address); |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 185 | if (rowIndex != -1U) { |
| 186 | const DWARFDebugLine::Row &row = lineTable->Rows[rowIndex]; |
| 187 | // Take file/line info from the line table. |
Alexey Samsonov | 71d94f8 | 2012-07-19 07:03:58 +0000 | [diff] [blame] | 188 | const DWARFDebugLine::FileNameEntry &fileNameEntry = |
| 189 | lineTable->Prologue.FileNames[row.File - 1]; |
| 190 | fileName = fileNameEntry.Name; |
| 191 | if (specifier.needs(DILineInfoSpecifier::AbsoluteFilePath) && |
| 192 | sys::path::is_relative(fileName.str())) { |
| 193 | // Append include directory of file (if it is present in line table) |
| 194 | // and compilation directory of compile unit to make path absolute. |
| 195 | const char *includeDir = 0; |
| 196 | if (uint64_t includeDirIndex = fileNameEntry.DirIdx) { |
| 197 | includeDir = lineTable->Prologue |
| 198 | .IncludeDirectories[includeDirIndex - 1]; |
| 199 | } |
| 200 | SmallString<16> absFileName; |
| 201 | if (includeDir == 0 || sys::path::is_relative(includeDir)) { |
| 202 | if (const char *compilationDir = cu->getCompilationDir()) |
| 203 | sys::path::append(absFileName, compilationDir); |
| 204 | } |
| 205 | if (includeDir) { |
| 206 | sys::path::append(absFileName, includeDir); |
| 207 | } |
| 208 | sys::path::append(absFileName, fileName.str()); |
| 209 | fileName = absFileName; |
| 210 | } |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 211 | line = row.Line; |
| 212 | column = row.Column; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | return DILineInfo(fileName, functionName, line, column); |
Benjamin Kramer | 101b1c5 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 217 | } |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 218 | |
| 219 | void DWARFContextInMemory::anchor() { } |