CVSymbolVisitor.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //===- CVSymbolVisitor.cpp --------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/DebugInfo/CodeView/CVSymbolVisitor.h"
  9. #include "llvm/DebugInfo/CodeView/CodeView.h"
  10. #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
  11. #include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h"
  12. #include "llvm/DebugInfo/CodeView/SymbolVisitorCallbacks.h"
  13. #include "llvm/Support/BinaryStreamArray.h"
  14. #include "llvm/Support/ErrorHandling.h"
  15. using namespace llvm;
  16. using namespace llvm::codeview;
  17. CVSymbolVisitor::CVSymbolVisitor(SymbolVisitorCallbacks &Callbacks)
  18. : Callbacks(Callbacks) {}
  19. template <typename T>
  20. static Error visitKnownRecord(CVSymbol &Record,
  21. SymbolVisitorCallbacks &Callbacks) {
  22. SymbolRecordKind RK = static_cast<SymbolRecordKind>(Record.kind());
  23. T KnownRecord(RK);
  24. if (auto EC = Callbacks.visitKnownRecord(Record, KnownRecord))
  25. return EC;
  26. return Error::success();
  27. }
  28. static Error finishVisitation(CVSymbol &Record,
  29. SymbolVisitorCallbacks &Callbacks) {
  30. switch (Record.kind()) {
  31. default:
  32. if (auto EC = Callbacks.visitUnknownSymbol(Record))
  33. return EC;
  34. break;
  35. #define SYMBOL_RECORD(EnumName, EnumVal, Name) \
  36. case EnumName: { \
  37. if (auto EC = visitKnownRecord<Name>(Record, Callbacks)) \
  38. return EC; \
  39. break; \
  40. }
  41. #define SYMBOL_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) \
  42. SYMBOL_RECORD(EnumVal, EnumVal, AliasName)
  43. #include "llvm/DebugInfo/CodeView/CodeViewSymbols.def"
  44. }
  45. if (auto EC = Callbacks.visitSymbolEnd(Record))
  46. return EC;
  47. return Error::success();
  48. }
  49. Error CVSymbolVisitor::visitSymbolRecord(CVSymbol &Record) {
  50. if (auto EC = Callbacks.visitSymbolBegin(Record))
  51. return EC;
  52. return finishVisitation(Record, Callbacks);
  53. }
  54. Error CVSymbolVisitor::visitSymbolRecord(CVSymbol &Record, uint32_t Offset) {
  55. if (auto EC = Callbacks.visitSymbolBegin(Record, Offset))
  56. return EC;
  57. return finishVisitation(Record, Callbacks);
  58. }
  59. Error CVSymbolVisitor::visitSymbolStream(const CVSymbolArray &Symbols) {
  60. for (auto I : Symbols) {
  61. if (auto EC = visitSymbolRecord(I))
  62. return EC;
  63. }
  64. return Error::success();
  65. }
  66. Error CVSymbolVisitor::visitSymbolStream(const CVSymbolArray &Symbols,
  67. uint32_t InitialOffset) {
  68. for (auto I : Symbols) {
  69. if (auto EC = visitSymbolRecord(I, InitialOffset + Symbols.skew()))
  70. return EC;
  71. InitialOffset += I.length();
  72. }
  73. return Error::success();
  74. }
  75. Error CVSymbolVisitor::visitSymbolStreamFiltered(const CVSymbolArray &Symbols,
  76. const FilterOptions &Filter) {
  77. if (!Filter.SymbolOffset)
  78. return visitSymbolStream(Symbols);
  79. uint32_t SymbolOffset = *Filter.SymbolOffset;
  80. uint32_t ParentRecurseDepth = Filter.ParentRecursiveDepth.value_or(0);
  81. uint32_t ChildrenRecurseDepth = Filter.ChildRecursiveDepth.value_or(0);
  82. if (!Symbols.isOffsetValid(SymbolOffset))
  83. return createStringError(inconvertibleErrorCode(), "Invalid symbol offset");
  84. CVSymbol Sym = *Symbols.at(SymbolOffset);
  85. uint32_t SymEndOffset =
  86. symbolOpensScope(Sym.kind()) ? getScopeEndOffset(Sym) : 0;
  87. std::vector<uint32_t> ParentOffsets;
  88. std::vector<uint32_t> ParentEndOffsets;
  89. uint32_t ChildrenDepth = 0;
  90. for (auto Begin = Symbols.begin(), End = Symbols.end(); Begin != End;
  91. ++Begin) {
  92. uint32_t BeginOffset = Begin.offset();
  93. CVSymbol BeginSym = *Begin;
  94. if (BeginOffset < SymbolOffset) {
  95. if (symbolOpensScope(Begin->kind())) {
  96. uint32_t EndOffset = getScopeEndOffset(BeginSym);
  97. if (SymbolOffset < EndOffset) {
  98. ParentOffsets.push_back(BeginOffset);
  99. ParentEndOffsets.push_back(EndOffset);
  100. }
  101. }
  102. } else if (BeginOffset == SymbolOffset) {
  103. // Found symbol at offset. Visit its parent up to ParentRecurseDepth.
  104. if (ParentRecurseDepth >= ParentOffsets.size())
  105. ParentRecurseDepth = ParentOffsets.size();
  106. uint32_t StartIndex = ParentOffsets.size() - ParentRecurseDepth;
  107. while (StartIndex < ParentOffsets.size()) {
  108. if (!Symbols.isOffsetValid(ParentOffsets[StartIndex]))
  109. break;
  110. CVSymbol Parent = *Symbols.at(ParentOffsets[StartIndex]);
  111. if (auto EC = visitSymbolRecord(Parent, ParentOffsets[StartIndex]))
  112. return EC;
  113. ++StartIndex;
  114. }
  115. if (auto EC = visitSymbolRecord(Sym, SymbolOffset))
  116. return EC;
  117. } else if (BeginOffset <= SymEndOffset) {
  118. if (ChildrenRecurseDepth) {
  119. // Visit children.
  120. if (symbolEndsScope(Begin->kind()))
  121. --ChildrenDepth;
  122. if (ChildrenDepth < ChildrenRecurseDepth ||
  123. BeginOffset == SymEndOffset) {
  124. if (auto EC = visitSymbolRecord(BeginSym, BeginOffset))
  125. return EC;
  126. }
  127. if (symbolOpensScope(Begin->kind()))
  128. ++ChildrenDepth;
  129. }
  130. } else {
  131. // Visit parents' ends.
  132. if (ParentRecurseDepth && BeginOffset == ParentEndOffsets.back()) {
  133. if (auto EC = visitSymbolRecord(BeginSym, BeginOffset))
  134. return EC;
  135. ParentEndOffsets.pop_back();
  136. --ParentRecurseDepth;
  137. }
  138. }
  139. }
  140. return Error::success();
  141. }