StackFrameRecognizer.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //===-- StackFrameRecognizer.h ----------------------------------*- 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. #ifndef LLDB_TARGET_STACKFRAMERECOGNIZER_H
  9. #define LLDB_TARGET_STACKFRAMERECOGNIZER_H
  10. #include "lldb/Core/ValueObject.h"
  11. #include "lldb/Core/ValueObjectList.h"
  12. #include "lldb/Symbol/VariableList.h"
  13. #include "lldb/Target/StopInfo.h"
  14. #include "lldb/Utility/StructuredData.h"
  15. #include "lldb/lldb-private-forward.h"
  16. #include "lldb/lldb-public.h"
  17. #include <vector>
  18. namespace lldb_private {
  19. /// \class RecognizedStackFrame
  20. ///
  21. /// This class provides extra information about a stack frame that was
  22. /// provided by a specific stack frame recognizer. Right now, this class only
  23. /// holds recognized arguments (via GetRecognizedArguments).
  24. class RecognizedStackFrame
  25. : public std::enable_shared_from_this<RecognizedStackFrame> {
  26. public:
  27. virtual lldb::ValueObjectListSP GetRecognizedArguments() {
  28. return m_arguments;
  29. }
  30. virtual lldb::ValueObjectSP GetExceptionObject() {
  31. return lldb::ValueObjectSP();
  32. }
  33. virtual lldb::StackFrameSP GetMostRelevantFrame() { return nullptr; };
  34. virtual ~RecognizedStackFrame(){};
  35. std::string GetStopDescription() { return m_stop_desc; }
  36. protected:
  37. lldb::ValueObjectListSP m_arguments;
  38. std::string m_stop_desc;
  39. };
  40. /// \class StackFrameRecognizer
  41. ///
  42. /// A base class for frame recognizers. Subclasses (actual frame recognizers)
  43. /// should implement RecognizeFrame to provide a RecognizedStackFrame for a
  44. /// given stack frame.
  45. class StackFrameRecognizer
  46. : public std::enable_shared_from_this<StackFrameRecognizer> {
  47. public:
  48. virtual lldb::RecognizedStackFrameSP RecognizeFrame(
  49. lldb::StackFrameSP frame) {
  50. return lldb::RecognizedStackFrameSP();
  51. };
  52. virtual std::string GetName() {
  53. return "";
  54. }
  55. virtual ~StackFrameRecognizer(){};
  56. };
  57. /// \class ScriptedStackFrameRecognizer
  58. ///
  59. /// Python implementation for frame recognizers. An instance of this class
  60. /// tracks a particular Python classobject, which will be asked to recognize
  61. /// stack frames.
  62. class ScriptedStackFrameRecognizer : public StackFrameRecognizer {
  63. lldb_private::ScriptInterpreter *m_interpreter;
  64. lldb_private::StructuredData::ObjectSP m_python_object_sp;
  65. std::string m_python_class;
  66. public:
  67. ScriptedStackFrameRecognizer(lldb_private::ScriptInterpreter *interpreter,
  68. const char *pclass);
  69. ~ScriptedStackFrameRecognizer() override {}
  70. std::string GetName() override {
  71. return GetPythonClassName();
  72. }
  73. const char *GetPythonClassName() { return m_python_class.c_str(); }
  74. lldb::RecognizedStackFrameSP RecognizeFrame(
  75. lldb::StackFrameSP frame) override;
  76. private:
  77. ScriptedStackFrameRecognizer(const ScriptedStackFrameRecognizer &) = delete;
  78. const ScriptedStackFrameRecognizer &
  79. operator=(const ScriptedStackFrameRecognizer &) = delete;
  80. };
  81. /// Class that provides a registry of known stack frame recognizers.
  82. class StackFrameRecognizerManager {
  83. public:
  84. void AddRecognizer(lldb::StackFrameRecognizerSP recognizer,
  85. ConstString module, llvm::ArrayRef<ConstString> symbols,
  86. bool first_instruction_only = true);
  87. void AddRecognizer(lldb::StackFrameRecognizerSP recognizer,
  88. lldb::RegularExpressionSP module,
  89. lldb::RegularExpressionSP symbol,
  90. bool first_instruction_only = true);
  91. void ForEach(std::function<
  92. void(uint32_t recognizer_id, std::string recognizer_name,
  93. std::string module, llvm::ArrayRef<ConstString> symbols,
  94. bool regexp)> const &callback);
  95. bool RemoveRecognizerWithID(uint32_t recognizer_id);
  96. void RemoveAllRecognizers();
  97. lldb::StackFrameRecognizerSP GetRecognizerForFrame(lldb::StackFrameSP frame);
  98. lldb::RecognizedStackFrameSP RecognizeFrame(lldb::StackFrameSP frame);
  99. private:
  100. struct RegisteredEntry {
  101. uint32_t recognizer_id;
  102. lldb::StackFrameRecognizerSP recognizer;
  103. bool is_regexp;
  104. ConstString module;
  105. lldb::RegularExpressionSP module_regexp;
  106. std::vector<ConstString> symbols;
  107. lldb::RegularExpressionSP symbol_regexp;
  108. bool first_instruction_only;
  109. };
  110. std::deque<RegisteredEntry> m_recognizers;
  111. };
  112. /// \class ValueObjectRecognizerSynthesizedValue
  113. ///
  114. /// ValueObject subclass that presents the passed ValueObject as a recognized
  115. /// value with the specified ValueType. Frame recognizers should return
  116. /// instances of this class as the returned objects in GetRecognizedArguments().
  117. class ValueObjectRecognizerSynthesizedValue : public ValueObject {
  118. public:
  119. static lldb::ValueObjectSP Create(ValueObject &parent, lldb::ValueType type) {
  120. return (new ValueObjectRecognizerSynthesizedValue(parent, type))->GetSP();
  121. }
  122. ValueObjectRecognizerSynthesizedValue(ValueObject &parent,
  123. lldb::ValueType type)
  124. : ValueObject(parent), m_type(type) {
  125. SetName(parent.GetName());
  126. }
  127. llvm::Optional<uint64_t> GetByteSize() override {
  128. return m_parent->GetByteSize();
  129. }
  130. lldb::ValueType GetValueType() const override { return m_type; }
  131. bool UpdateValue() override {
  132. if (!m_parent->UpdateValueIfNeeded()) return false;
  133. m_value = m_parent->GetValue();
  134. return true;
  135. }
  136. size_t CalculateNumChildren(uint32_t max = UINT32_MAX) override {
  137. return m_parent->GetNumChildren(max);
  138. }
  139. CompilerType GetCompilerTypeImpl() override {
  140. return m_parent->GetCompilerType();
  141. }
  142. bool IsSynthetic() override { return true; }
  143. private:
  144. lldb::ValueType m_type;
  145. };
  146. } // namespace lldb_private
  147. #endif // LLDB_TARGET_STACKFRAMERECOGNIZER_H