Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:55

0001 //===-- StackFrameRecognizer.h ----------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_TARGET_STACKFRAMERECOGNIZER_H
0010 #define LLDB_TARGET_STACKFRAMERECOGNIZER_H
0011 
0012 #include "lldb/Symbol/VariableList.h"
0013 #include "lldb/Target/StopInfo.h"
0014 #include "lldb/Utility/StructuredData.h"
0015 #include "lldb/ValueObject/ValueObject.h"
0016 #include "lldb/ValueObject/ValueObjectList.h"
0017 #include "lldb/lldb-private-forward.h"
0018 #include "lldb/lldb-public.h"
0019 
0020 #include <cstdint>
0021 #include <deque>
0022 #include <optional>
0023 #include <vector>
0024 
0025 namespace lldb_private {
0026 
0027 /// \class RecognizedStackFrame
0028 ///
0029 /// This class provides extra information about a stack frame that was
0030 /// provided by a specific stack frame recognizer. Right now, this class only
0031 /// holds recognized arguments (via GetRecognizedArguments).
0032 class RecognizedStackFrame
0033     : public std::enable_shared_from_this<RecognizedStackFrame> {
0034 public:
0035   virtual ~RecognizedStackFrame() = default;
0036 
0037   virtual lldb::ValueObjectListSP GetRecognizedArguments() {
0038     return m_arguments;
0039   }
0040   virtual lldb::ValueObjectSP GetExceptionObject() {
0041     return lldb::ValueObjectSP();
0042   }
0043   virtual lldb::StackFrameSP GetMostRelevantFrame() { return nullptr; }
0044 
0045   std::string GetStopDescription() { return m_stop_desc; }
0046   /// Controls whether this frame should be filtered out when
0047   /// displaying backtraces, for example.
0048   virtual bool ShouldHide() { return false; }
0049 
0050 protected:
0051   lldb::ValueObjectListSP m_arguments;
0052   std::string m_stop_desc;
0053 };
0054 
0055 /// \class StackFrameRecognizer
0056 ///
0057 /// A base class for frame recognizers. Subclasses (actual frame recognizers)
0058 /// should implement RecognizeFrame to provide a RecognizedStackFrame for a
0059 /// given stack frame.
0060 class StackFrameRecognizer
0061     : public std::enable_shared_from_this<StackFrameRecognizer> {
0062 public:
0063   virtual lldb::RecognizedStackFrameSP RecognizeFrame(
0064       lldb::StackFrameSP frame) {
0065     return lldb::RecognizedStackFrameSP();
0066   };
0067   virtual std::string GetName() {
0068     return "";
0069   }
0070 
0071   virtual ~StackFrameRecognizer() = default;
0072 };
0073 
0074 /// \class ScriptedStackFrameRecognizer
0075 ///
0076 /// Python implementation for frame recognizers. An instance of this class
0077 /// tracks a particular Python classobject, which will be asked to recognize
0078 /// stack frames.
0079 class ScriptedStackFrameRecognizer : public StackFrameRecognizer {
0080   lldb_private::ScriptInterpreter *m_interpreter;
0081   lldb_private::StructuredData::ObjectSP m_python_object_sp;
0082 
0083   std::string m_python_class;
0084 
0085 public:
0086   ScriptedStackFrameRecognizer(lldb_private::ScriptInterpreter *interpreter,
0087                                const char *pclass);
0088   ~ScriptedStackFrameRecognizer() override = default;
0089 
0090   std::string GetName() override {
0091     return GetPythonClassName();
0092   }
0093 
0094   const char *GetPythonClassName() { return m_python_class.c_str(); }
0095 
0096   lldb::RecognizedStackFrameSP RecognizeFrame(
0097       lldb::StackFrameSP frame) override;
0098 
0099 private:
0100   ScriptedStackFrameRecognizer(const ScriptedStackFrameRecognizer &) = delete;
0101   const ScriptedStackFrameRecognizer &
0102   operator=(const ScriptedStackFrameRecognizer &) = delete;
0103 };
0104 
0105 /// Class that provides a registry of known stack frame recognizers.
0106 class StackFrameRecognizerManager {
0107 public:
0108   /// Add a new recognizer that triggers on a given symbol name.
0109   ///
0110   /// \param symbol_mangling controls whether the symbol name should be
0111   /// compared to the mangled or demangled name.
0112   void AddRecognizer(lldb::StackFrameRecognizerSP recognizer,
0113                      ConstString module, llvm::ArrayRef<ConstString> symbols,
0114                      Mangled::NamePreference symbol_mangling,
0115                      bool first_instruction_only = true);
0116 
0117   /// Add a new recognizer that triggers on a symbol regex.
0118   ///
0119   /// \param symbol_mangling controls whether the regex should apply
0120   /// to the mangled or demangled name.
0121   void AddRecognizer(lldb::StackFrameRecognizerSP recognizer,
0122                      lldb::RegularExpressionSP module,
0123                      lldb::RegularExpressionSP symbol,
0124                      Mangled::NamePreference symbol_mangling,
0125                      bool first_instruction_only = true);
0126 
0127   void
0128   ForEach(std::function<void(uint32_t recognizer_id, bool enabled,
0129                              std::string recognizer_name, std::string module,
0130                              llvm::ArrayRef<ConstString> symbols,
0131                              Mangled::NamePreference name_preference,
0132                              bool regexp)> const &callback);
0133 
0134   bool SetEnabledForID(uint32_t recognizer_id, bool enabled);
0135   bool RemoveRecognizerWithID(uint32_t recognizer_id);
0136 
0137   void RemoveAllRecognizers();
0138 
0139   lldb::StackFrameRecognizerSP GetRecognizerForFrame(lldb::StackFrameSP frame);
0140 
0141   lldb::RecognizedStackFrameSP RecognizeFrame(lldb::StackFrameSP frame);
0142   /// Returns a number that changes whenever the list of recognizers
0143   /// has been modified.
0144   uint16_t GetGeneration() const { return m_generation; }
0145 
0146 private:
0147   /// Increase the generation counter.
0148   void BumpGeneration();
0149 
0150   struct RegisteredEntry {
0151     uint32_t recognizer_id;
0152     lldb::StackFrameRecognizerSP recognizer;
0153     bool is_regexp;
0154     ConstString module;
0155     lldb::RegularExpressionSP module_regexp;
0156     std::vector<ConstString> symbols;
0157     lldb::RegularExpressionSP symbol_regexp;
0158     Mangled::NamePreference symbol_mangling;
0159     bool first_instruction_only;
0160     bool enabled;
0161   };
0162 
0163   std::deque<RegisteredEntry> m_recognizers;
0164   uint16_t m_generation = 0;
0165 };
0166 
0167 /// \class ValueObjectRecognizerSynthesizedValue
0168 ///
0169 /// ValueObject subclass that presents the passed ValueObject as a recognized
0170 /// value with the specified ValueType. Frame recognizers should return
0171 /// instances of this class as the returned objects in GetRecognizedArguments().
0172 class ValueObjectRecognizerSynthesizedValue : public ValueObject {
0173  public:
0174   static lldb::ValueObjectSP Create(ValueObject &parent, lldb::ValueType type) {
0175     return (new ValueObjectRecognizerSynthesizedValue(parent, type))->GetSP();
0176   }
0177   ValueObjectRecognizerSynthesizedValue(ValueObject &parent,
0178                                         lldb::ValueType type)
0179       : ValueObject(parent), m_type(type) {
0180     SetName(parent.GetName());
0181   }
0182 
0183   std::optional<uint64_t> GetByteSize() override {
0184     return m_parent->GetByteSize();
0185   }
0186   lldb::ValueType GetValueType() const override { return m_type; }
0187   bool UpdateValue() override {
0188     if (!m_parent->UpdateValueIfNeeded()) return false;
0189     m_value = m_parent->GetValue();
0190     return true;
0191   }
0192   llvm::Expected<uint32_t>
0193   CalculateNumChildren(uint32_t max = UINT32_MAX) override {
0194     return m_parent->GetNumChildren(max);
0195   }
0196   CompilerType GetCompilerTypeImpl() override {
0197     return m_parent->GetCompilerType();
0198   }
0199   bool IsSynthetic() override { return true; }
0200 
0201  private:
0202   lldb::ValueType m_type;
0203 };
0204 
0205 } // namespace lldb_private
0206 
0207 #endif // LLDB_TARGET_STACKFRAMERECOGNIZER_H