Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 1 | //===-- DWARFCompileUnit.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 "DWARFCompileUnit.h" |
| 11 | #include "DWARFContext.h" |
Alexey Samsonov | cd61455 | 2013-04-17 08:29:02 +0000 | [diff] [blame] | 12 | #include "llvm/DebugInfo/DWARFFormValue.h" |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Dwarf.h" |
| 14 | #include "llvm/Support/Format.h" |
| 15 | #include "llvm/Support/raw_ostream.h" |
| 16 | using namespace llvm; |
| 17 | using namespace dwarf; |
| 18 | |
| 19 | DataExtractor DWARFCompileUnit::getDebugInfoExtractor() const { |
Eric Christopher | 82de10a | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 20 | return DataExtractor(InfoSection, isLittleEndian, AddrSize); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | bool DWARFCompileUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) { |
| 24 | clear(); |
| 25 | |
| 26 | Offset = *offset_ptr; |
| 27 | |
| 28 | if (debug_info.isValidOffset(*offset_ptr)) { |
| 29 | uint64_t abbrOffset; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 30 | Length = debug_info.getU32(offset_ptr); |
| 31 | Version = debug_info.getU16(offset_ptr); |
| 32 | abbrOffset = debug_info.getU32(offset_ptr); |
| 33 | AddrSize = debug_info.getU8(offset_ptr); |
| 34 | |
| 35 | bool lengthOK = debug_info.isValidOffset(getNextCompileUnitOffset()-1); |
| 36 | bool versionOK = DWARFContext::isSupportedVersion(Version); |
Eric Christopher | 82de10a | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 37 | bool abbrOffsetOK = AbbrevSection.size() > abbrOffset; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 38 | bool addrSizeOK = AddrSize == 4 || AddrSize == 8; |
| 39 | |
Eric Christopher | 82de10a | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 40 | if (lengthOK && versionOK && addrSizeOK && abbrOffsetOK && Abbrev != NULL) { |
| 41 | Abbrevs = Abbrev->getAbbreviationDeclarationSet(abbrOffset); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 42 | return true; |
| 43 | } |
| 44 | |
| 45 | // reset the offset to where we tried to parse from if anything went wrong |
| 46 | *offset_ptr = Offset; |
| 47 | } |
| 48 | |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | uint32_t |
| 53 | DWARFCompileUnit::extract(uint32_t offset, DataExtractor debug_info_data, |
| 54 | const DWARFAbbreviationDeclarationSet *abbrevs) { |
| 55 | clear(); |
| 56 | |
| 57 | Offset = offset; |
| 58 | |
| 59 | if (debug_info_data.isValidOffset(offset)) { |
| 60 | Length = debug_info_data.getU32(&offset); |
| 61 | Version = debug_info_data.getU16(&offset); |
| 62 | bool abbrevsOK = debug_info_data.getU32(&offset) == abbrevs->getOffset(); |
| 63 | Abbrevs = abbrevs; |
Eric Christopher | 5d04a3a | 2012-08-23 23:26:57 +0000 | [diff] [blame] | 64 | AddrSize = debug_info_data.getU8(&offset); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 65 | |
| 66 | bool versionOK = DWARFContext::isSupportedVersion(Version); |
| 67 | bool addrSizeOK = AddrSize == 4 || AddrSize == 8; |
| 68 | |
| 69 | if (versionOK && addrSizeOK && abbrevsOK && |
| 70 | debug_info_data.isValidOffset(offset)) |
| 71 | return offset; |
| 72 | } |
| 73 | return 0; |
| 74 | } |
| 75 | |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 76 | bool DWARFCompileUnit::extractRangeList(uint32_t RangeListOffset, |
| 77 | DWARFDebugRangeList &RangeList) const { |
| 78 | // Require that compile unit is extracted. |
| 79 | assert(DieArray.size() > 0); |
Eric Christopher | 82de10a | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 80 | DataExtractor RangesData(RangeSection, isLittleEndian, AddrSize); |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 81 | return RangeList.extract(RangesData, &RangeListOffset); |
| 82 | } |
| 83 | |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 84 | void DWARFCompileUnit::clear() { |
| 85 | Offset = 0; |
| 86 | Length = 0; |
| 87 | Version = 0; |
| 88 | Abbrevs = 0; |
| 89 | AddrSize = 0; |
| 90 | BaseAddr = 0; |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 91 | clearDIEs(false); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void DWARFCompileUnit::dump(raw_ostream &OS) { |
| 95 | OS << format("0x%08x", Offset) << ": Compile Unit:" |
| 96 | << " length = " << format("0x%08x", Length) |
| 97 | << " version = " << format("0x%04x", Version) |
| 98 | << " abbr_offset = " << format("0x%04x", Abbrevs->getOffset()) |
| 99 | << " addr_size = " << format("0x%02x", AddrSize) |
| 100 | << " (next CU at " << format("0x%08x", getNextCompileUnitOffset()) |
| 101 | << ")\n"; |
| 102 | |
Eric Christopher | fa76f22 | 2012-08-23 23:21:11 +0000 | [diff] [blame] | 103 | const DWARFDebugInfoEntryMinimal *CU = getCompileUnitDIE(false); |
| 104 | assert(CU && "Null Compile Unit?"); |
| 105 | CU->dump(OS, this, -1U); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Alexey Samsonov | 71d94f8 | 2012-07-19 07:03:58 +0000 | [diff] [blame] | 108 | const char *DWARFCompileUnit::getCompilationDir() { |
| 109 | extractDIEsIfNeeded(true); |
| 110 | if (DieArray.empty()) |
| 111 | return 0; |
| 112 | return DieArray[0].getAttributeValueAsString(this, DW_AT_comp_dir, 0); |
| 113 | } |
| 114 | |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 115 | void DWARFCompileUnit::setDIERelations() { |
| 116 | if (DieArray.empty()) |
| 117 | return; |
| 118 | DWARFDebugInfoEntryMinimal *die_array_begin = &DieArray.front(); |
| 119 | DWARFDebugInfoEntryMinimal *die_array_end = &DieArray.back(); |
| 120 | DWARFDebugInfoEntryMinimal *curr_die; |
| 121 | // We purposely are skipping the last element in the array in the loop below |
| 122 | // so that we can always have a valid next item |
| 123 | for (curr_die = die_array_begin; curr_die < die_array_end; ++curr_die) { |
| 124 | // Since our loop doesn't include the last element, we can always |
| 125 | // safely access the next die in the array. |
| 126 | DWARFDebugInfoEntryMinimal *next_die = curr_die + 1; |
| 127 | |
| 128 | const DWARFAbbreviationDeclaration *curr_die_abbrev = |
| 129 | curr_die->getAbbreviationDeclarationPtr(); |
| 130 | |
| 131 | if (curr_die_abbrev) { |
| 132 | // Normal DIE |
| 133 | if (curr_die_abbrev->hasChildren()) |
| 134 | next_die->setParent(curr_die); |
| 135 | else |
| 136 | curr_die->setSibling(next_die); |
| 137 | } else { |
| 138 | // NULL DIE that terminates a sibling chain |
| 139 | DWARFDebugInfoEntryMinimal *parent = curr_die->getParent(); |
| 140 | if (parent) |
| 141 | parent->setSibling(next_die); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // Since we skipped the last element, we need to fix it up! |
| 146 | if (die_array_begin < die_array_end) |
| 147 | curr_die->setParent(die_array_begin); |
| 148 | } |
| 149 | |
Alexey Samsonov | 40d8c69 | 2013-07-15 08:43:35 +0000 | [diff] [blame] | 150 | void DWARFCompileUnit::extractDIEsToVector( |
| 151 | bool AppendCUDie, bool AppendNonCUDies, |
| 152 | std::vector<DWARFDebugInfoEntryMinimal> &Dies) const { |
| 153 | if (!AppendCUDie && !AppendNonCUDies) |
| 154 | return; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 155 | |
| 156 | // Set the offset to that of the first DIE and calculate the start of the |
| 157 | // next compilation unit header. |
Alexey Samsonov | 40d8c69 | 2013-07-15 08:43:35 +0000 | [diff] [blame] | 158 | uint32_t Offset = getFirstDIEOffset(); |
| 159 | uint32_t NextCUOffset = getNextCompileUnitOffset(); |
| 160 | DWARFDebugInfoEntryMinimal DIE; |
| 161 | uint32_t Depth = 0; |
| 162 | const uint8_t *FixedFormSizes = |
Alexey Samsonov | 32a3e78 | 2013-04-09 14:09:42 +0000 | [diff] [blame] | 163 | DWARFFormValue::getFixedFormSizes(getAddressByteSize(), getVersion()); |
Alexey Samsonov | 40d8c69 | 2013-07-15 08:43:35 +0000 | [diff] [blame] | 164 | bool IsCUDie = true; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 165 | |
Alexey Samsonov | 40d8c69 | 2013-07-15 08:43:35 +0000 | [diff] [blame] | 166 | while (Offset < NextCUOffset && |
| 167 | DIE.extractFast(this, FixedFormSizes, &Offset)) { |
| 168 | if (IsCUDie) { |
| 169 | if (AppendCUDie) |
| 170 | Dies.push_back(DIE); |
| 171 | if (!AppendNonCUDies) |
| 172 | break; |
| 173 | // The average bytes per DIE entry has been seen to be |
| 174 | // around 14-20 so let's pre-reserve the needed memory for |
| 175 | // our DIE entries accordingly. |
| 176 | Dies.reserve(Dies.size() + getDebugInfoSize() / 14); |
| 177 | IsCUDie = false; |
| 178 | } else { |
| 179 | Dies.push_back(DIE); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Alexey Samsonov | 40d8c69 | 2013-07-15 08:43:35 +0000 | [diff] [blame] | 182 | const DWARFAbbreviationDeclaration *AbbrDecl = |
| 183 | DIE.getAbbreviationDeclarationPtr(); |
| 184 | if (AbbrDecl) { |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 185 | // Normal DIE |
Alexey Samsonov | 40d8c69 | 2013-07-15 08:43:35 +0000 | [diff] [blame] | 186 | if (AbbrDecl->hasChildren()) |
| 187 | ++Depth; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 188 | } else { |
| 189 | // NULL DIE. |
Alexey Samsonov | 40d8c69 | 2013-07-15 08:43:35 +0000 | [diff] [blame] | 190 | if (Depth > 0) |
| 191 | --Depth; |
| 192 | if (Depth == 0) |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 193 | break; // We are done with this compile unit! |
| 194 | } |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | // Give a little bit of info if we encounter corrupt DWARF (our offset |
| 198 | // should always terminate at or before the start of the next compilation |
| 199 | // unit header). |
Alexey Samsonov | 40d8c69 | 2013-07-15 08:43:35 +0000 | [diff] [blame] | 200 | if (Offset > NextCUOffset) |
Eric Christopher | 642469f | 2013-01-04 18:30:36 +0000 | [diff] [blame] | 201 | fprintf(stderr, "warning: DWARF compile unit extends beyond its " |
Alexey Samsonov | 40d8c69 | 2013-07-15 08:43:35 +0000 | [diff] [blame] | 202 | "bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), Offset); |
| 203 | } |
| 204 | |
| 205 | size_t DWARFCompileUnit::extractDIEsIfNeeded(bool CUDieOnly) { |
| 206 | if ((CUDieOnly && DieArray.size() > 0) || |
| 207 | DieArray.size() > 1) |
| 208 | return 0; // Already parsed. |
| 209 | |
| 210 | extractDIEsToVector(DieArray.empty(), !CUDieOnly, DieArray); |
| 211 | |
| 212 | // Set the base address of current compile unit. |
| 213 | if (!DieArray.empty()) { |
| 214 | uint64_t BaseAddr = |
| 215 | DieArray[0].getAttributeValueAsUnsigned(this, DW_AT_low_pc, -1U); |
| 216 | if (BaseAddr == -1U) |
| 217 | BaseAddr = DieArray[0].getAttributeValueAsUnsigned(this, DW_AT_entry_pc, 0); |
| 218 | setBaseAddress(BaseAddr); |
| 219 | } |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 220 | |
| 221 | setDIERelations(); |
| 222 | return DieArray.size(); |
| 223 | } |
Benjamin Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 224 | |
Alexey Samsonov | 40d8c69 | 2013-07-15 08:43:35 +0000 | [diff] [blame] | 225 | void DWARFCompileUnit::clearDIEs(bool KeepCUDie) { |
| 226 | if (DieArray.size() > (unsigned)KeepCUDie) { |
Benjamin Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 227 | // std::vectors never get any smaller when resized to a smaller size, |
| 228 | // or when clear() or erase() are called, the size will report that it |
| 229 | // is smaller, but the memory allocated remains intact (call capacity() |
| 230 | // to see this). So we need to create a temporary vector and swap the |
| 231 | // contents which will cause just the internal pointers to be swapped |
Alexey Samsonov | 40d8c69 | 2013-07-15 08:43:35 +0000 | [diff] [blame] | 232 | // so that when temporary vector goes out of scope, it will destroy the |
Benjamin Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 233 | // contents. |
Alexey Samsonov | 40d8c69 | 2013-07-15 08:43:35 +0000 | [diff] [blame] | 234 | std::vector<DWARFDebugInfoEntryMinimal> TmpArray; |
| 235 | DieArray.swap(TmpArray); |
Benjamin Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 236 | // Save at least the compile unit DIE |
Alexey Samsonov | 40d8c69 | 2013-07-15 08:43:35 +0000 | [diff] [blame] | 237 | if (KeepCUDie) |
| 238 | DieArray.push_back(TmpArray.front()); |
Benjamin Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | void |
| 243 | DWARFCompileUnit::buildAddressRangeTable(DWARFDebugAranges *debug_aranges, |
| 244 | bool clear_dies_if_already_not_parsed){ |
| 245 | // This function is usually called if there in no .debug_aranges section |
| 246 | // in order to produce a compile unit level set of address ranges that |
| 247 | // is accurate. If the DIEs weren't parsed, then we don't want all dies for |
| 248 | // all compile units to stay loaded when they weren't needed. So we can end |
| 249 | // up parsing the DWARF and then throwing them all away to keep memory usage |
| 250 | // down. |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 251 | const bool clear_dies = extractDIEsIfNeeded(false) > 1 && |
| 252 | clear_dies_if_already_not_parsed; |
Benjamin Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 253 | DieArray[0].buildAddressRangeTable(this, debug_aranges); |
| 254 | |
| 255 | // Keep memory down by clearing DIEs if this generate function |
| 256 | // caused them to be parsed. |
| 257 | if (clear_dies) |
| 258 | clearDIEs(true); |
| 259 | } |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 260 | |
Alexey Samsonov | e664290 | 2013-08-06 10:49:15 +0000 | [diff] [blame^] | 261 | DWARFDebugInfoEntryInlinedChain |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 262 | DWARFCompileUnit::getInlinedChainForAddress(uint64_t Address) { |
| 263 | // First, find a subprogram that contains the given address (the root |
| 264 | // of inlined chain). |
Alexey Samsonov | a9543aa | 2012-07-04 09:42:54 +0000 | [diff] [blame] | 265 | extractDIEsIfNeeded(false); |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 266 | const DWARFDebugInfoEntryMinimal *SubprogramDIE = 0; |
Alexey Samsonov | a9543aa | 2012-07-04 09:42:54 +0000 | [diff] [blame] | 267 | for (size_t i = 0, n = DieArray.size(); i != n; i++) { |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 268 | if (DieArray[i].isSubprogramDIE() && |
| 269 | DieArray[i].addressRangeContainsAddress(this, Address)) { |
| 270 | SubprogramDIE = &DieArray[i]; |
| 271 | break; |
| 272 | } |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 273 | } |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 274 | // Get inlined chain rooted at this subprogram DIE. |
| 275 | if (!SubprogramDIE) |
Alexey Samsonov | e664290 | 2013-08-06 10:49:15 +0000 | [diff] [blame^] | 276 | return DWARFDebugInfoEntryInlinedChain(); |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 277 | return SubprogramDIE->getInlinedChainForAddress(this, Address); |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 278 | } |