Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 1 | //===-- DWARFDebugAbbrev.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 "DWARFDebugAbbrev.h" |
| 11 | #include "llvm/Support/Format.h" |
| 12 | #include "llvm/Support/raw_ostream.h" |
| 13 | using namespace llvm; |
| 14 | |
| 15 | bool DWARFAbbreviationDeclarationSet::extract(DataExtractor data, |
| 16 | uint32_t* offset_ptr) { |
| 17 | const uint32_t beginOffset = *offset_ptr; |
| 18 | Offset = beginOffset; |
| 19 | clear(); |
| 20 | DWARFAbbreviationDeclaration abbrevDeclaration; |
| 21 | uint32_t prevAbbrAode = 0; |
| 22 | while (abbrevDeclaration.extract(data, offset_ptr)) { |
| 23 | Decls.push_back(abbrevDeclaration); |
| 24 | if (IdxOffset == 0) { |
| 25 | IdxOffset = abbrevDeclaration.getCode(); |
| 26 | } else { |
| 27 | if (prevAbbrAode + 1 != abbrevDeclaration.getCode()) |
| 28 | IdxOffset = UINT32_MAX;// Out of order indexes, we can't do O(1) lookups |
| 29 | } |
| 30 | prevAbbrAode = abbrevDeclaration.getCode(); |
| 31 | } |
| 32 | return beginOffset != *offset_ptr; |
| 33 | } |
| 34 | |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 35 | void DWARFAbbreviationDeclarationSet::dump(raw_ostream &OS) const { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 36 | for (const auto &Decl : Decls) |
| 37 | Decl.dump(OS); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 40 | const DWARFAbbreviationDeclaration* |
| 41 | DWARFAbbreviationDeclarationSet::getAbbreviationDeclaration(uint32_t abbrCode) |
| 42 | const { |
| 43 | if (IdxOffset == UINT32_MAX) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 44 | for (const auto &Decl : Decls) { |
| 45 | if (Decl.getCode() == abbrCode) |
| 46 | return &Decl; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 47 | } |
| 48 | } else { |
| 49 | uint32_t idx = abbrCode - IdxOffset; |
| 50 | if (idx < Decls.size()) |
| 51 | return &Decls[idx]; |
| 52 | } |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | DWARFDebugAbbrev::DWARFDebugAbbrev() : |
Benjamin Kramer | 4aa3fea | 2011-09-13 21:47:32 +0000 | [diff] [blame] | 57 | AbbrevCollMap(), |
| 58 | PrevAbbrOffsetPos(AbbrevCollMap.end()) {} |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 59 | |
| 60 | |
| 61 | void DWARFDebugAbbrev::parse(DataExtractor data) { |
| 62 | uint32_t offset = 0; |
| 63 | |
| 64 | while (data.isValidOffset(offset)) { |
| 65 | uint32_t initial_cu_offset = offset; |
| 66 | DWARFAbbreviationDeclarationSet abbrevDeclSet; |
| 67 | |
| 68 | if (abbrevDeclSet.extract(data, &offset)) |
Benjamin Kramer | 4aa3fea | 2011-09-13 21:47:32 +0000 | [diff] [blame] | 69 | AbbrevCollMap[initial_cu_offset] = abbrevDeclSet; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 70 | else |
| 71 | break; |
| 72 | } |
Benjamin Kramer | 4aa3fea | 2011-09-13 21:47:32 +0000 | [diff] [blame] | 73 | PrevAbbrOffsetPos = AbbrevCollMap.end(); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void DWARFDebugAbbrev::dump(raw_ostream &OS) const { |
Benjamin Kramer | 4aa3fea | 2011-09-13 21:47:32 +0000 | [diff] [blame] | 77 | if (AbbrevCollMap.empty()) { |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 78 | OS << "< EMPTY >\n"; |
| 79 | return; |
| 80 | } |
| 81 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 82 | for (const auto &I : AbbrevCollMap) { |
| 83 | OS << format("Abbrev table for offset: 0x%8.8" PRIx64 "\n", I.first); |
| 84 | I.second.dump(OS); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | const DWARFAbbreviationDeclarationSet* |
| 89 | DWARFDebugAbbrev::getAbbreviationDeclarationSet(uint64_t cu_abbr_offset) const { |
Benjamin Kramer | 4aa3fea | 2011-09-13 21:47:32 +0000 | [diff] [blame] | 90 | DWARFAbbreviationDeclarationCollMapConstIter end = AbbrevCollMap.end(); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 91 | DWARFAbbreviationDeclarationCollMapConstIter pos; |
Benjamin Kramer | 4aa3fea | 2011-09-13 21:47:32 +0000 | [diff] [blame] | 92 | if (PrevAbbrOffsetPos != end && |
| 93 | PrevAbbrOffsetPos->first == cu_abbr_offset) { |
| 94 | return &(PrevAbbrOffsetPos->second); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 95 | } else { |
Benjamin Kramer | 4aa3fea | 2011-09-13 21:47:32 +0000 | [diff] [blame] | 96 | pos = AbbrevCollMap.find(cu_abbr_offset); |
| 97 | PrevAbbrOffsetPos = pos; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Benjamin Kramer | 4aa3fea | 2011-09-13 21:47:32 +0000 | [diff] [blame] | 100 | if (pos != AbbrevCollMap.end()) |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 101 | return &(pos->second); |
| 102 | return NULL; |
| 103 | } |