Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===-- Watchpoint.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_BREAKPOINT_WATCHPOINT_H
0010 #define LLDB_BREAKPOINT_WATCHPOINT_H
0011 
0012 #include <memory>
0013 #include <string>
0014 
0015 #include "lldb/Breakpoint/StoppointSite.h"
0016 #include "lldb/Breakpoint/WatchpointOptions.h"
0017 #include "lldb/Symbol/CompilerType.h"
0018 #include "lldb/Target/Target.h"
0019 #include "lldb/Utility/UserID.h"
0020 #include "lldb/lldb-private.h"
0021 
0022 namespace lldb_private {
0023 
0024 class Watchpoint : public std::enable_shared_from_this<Watchpoint>,
0025                    public StoppointSite {
0026 public:
0027   class WatchpointEventData : public EventData {
0028   public:
0029     WatchpointEventData(lldb::WatchpointEventType sub_type,
0030                         const lldb::WatchpointSP &new_watchpoint_sp);
0031 
0032     ~WatchpointEventData() override;
0033 
0034     static llvm::StringRef GetFlavorString();
0035 
0036     llvm::StringRef GetFlavor() const override;
0037 
0038     lldb::WatchpointEventType GetWatchpointEventType() const;
0039 
0040     lldb::WatchpointSP &GetWatchpoint();
0041 
0042     void Dump(Stream *s) const override;
0043 
0044     static lldb::WatchpointEventType
0045     GetWatchpointEventTypeFromEvent(const lldb::EventSP &event_sp);
0046 
0047     static lldb::WatchpointSP
0048     GetWatchpointFromEvent(const lldb::EventSP &event_sp);
0049 
0050     static const WatchpointEventData *
0051     GetEventDataFromEvent(const Event *event_sp);
0052 
0053   private:
0054     lldb::WatchpointEventType m_watchpoint_event;
0055     lldb::WatchpointSP m_new_watchpoint_sp;
0056 
0057     WatchpointEventData(const WatchpointEventData &) = delete;
0058     const WatchpointEventData &operator=(const WatchpointEventData &) = delete;
0059   };
0060 
0061   Watchpoint(Target &target, lldb::addr_t addr, uint32_t size,
0062              const CompilerType *type, bool hardware = true);
0063 
0064   ~Watchpoint() override;
0065 
0066   bool IsEnabled() const;
0067 
0068   // This doesn't really enable/disable the watchpoint.   It is currently just
0069   // for use in the Process plugin's {Enable,Disable}Watchpoint, which should
0070   // be used instead.
0071   void SetEnabled(bool enabled, bool notify = true);
0072 
0073   bool IsHardware() const override;
0074 
0075   bool ShouldStop(StoppointCallbackContext *context) override;
0076   
0077   bool WatchpointRead() const;
0078   bool WatchpointWrite() const;
0079   bool WatchpointModify() const;
0080   uint32_t GetIgnoreCount() const;
0081   void SetIgnoreCount(uint32_t n);
0082   void SetWatchpointType(uint32_t type, bool notify = true);
0083   void SetDeclInfo(const std::string &str);
0084   std::string GetWatchSpec();
0085   void SetWatchSpec(const std::string &str);
0086   bool WatchedValueReportable(const ExecutionContext &exe_ctx);
0087 
0088   // Snapshot management interface.
0089   bool IsWatchVariable() const;
0090   void SetWatchVariable(bool val);
0091   bool CaptureWatchedValue(const ExecutionContext &exe_ctx);
0092 
0093   /// \struct WatchpointVariableContext
0094   /// \brief Represents the context of a watchpoint variable.
0095   ///
0096   /// This struct encapsulates the information related to a watchpoint variable,
0097   /// including the watch ID and the execution context in which it is being
0098   /// used. This struct is passed as a Baton to the \b
0099   /// VariableWatchpointDisabler breakpoint callback.
0100   struct WatchpointVariableContext {
0101     /// \brief Constructor for WatchpointVariableContext.
0102     /// \param watch_id The ID of the watchpoint.
0103     /// \param exe_ctx The execution context associated with the watchpoint.
0104     WatchpointVariableContext(lldb::watch_id_t watch_id,
0105                               ExecutionContext exe_ctx)
0106         : watch_id(watch_id), exe_ctx(exe_ctx) {}
0107 
0108     lldb::watch_id_t watch_id; ///< The ID of the watchpoint.
0109     ExecutionContext
0110         exe_ctx; ///< The execution context associated with the watchpoint.
0111   };
0112 
0113   class WatchpointVariableBaton : public TypedBaton<WatchpointVariableContext> {
0114   public:
0115     WatchpointVariableBaton(std::unique_ptr<WatchpointVariableContext> Data)
0116         : TypedBaton(std::move(Data)) {}
0117   };
0118 
0119   bool SetupVariableWatchpointDisabler(lldb::StackFrameSP frame_sp) const;
0120 
0121   /// Callback routine to disable the watchpoint set on a local variable when
0122   ///  it goes out of scope.
0123   static bool VariableWatchpointDisabler(
0124       void *baton, lldb_private::StoppointCallbackContext *context,
0125       lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
0126 
0127   void GetDescription(Stream *s, lldb::DescriptionLevel level);
0128   void Dump(Stream *s) const override;
0129   bool DumpSnapshots(Stream *s, const char *prefix = nullptr) const;
0130   void DumpWithLevel(Stream *s, lldb::DescriptionLevel description_level) const;
0131   Target &GetTarget() { return m_target; }
0132   const Status &GetError() { return m_error; }
0133 
0134   /// Returns the WatchpointOptions structure set for this watchpoint.
0135   ///
0136   /// \return
0137   ///     A pointer to this watchpoint's WatchpointOptions.
0138   WatchpointOptions *GetOptions() { return &m_options; }
0139 
0140   /// Set the callback action invoked when the watchpoint is hit.
0141   ///
0142   /// \param[in] callback
0143   ///    The method that will get called when the watchpoint is hit.
0144   /// \param[in] callback_baton
0145   ///    A void * pointer that will get passed back to the callback function.
0146   /// \param[in] is_synchronous
0147   ///    If \b true the callback will be run on the private event thread
0148   ///    before the stop event gets reported.  If false, the callback will get
0149   ///    handled on the public event thread after the stop has been posted.
0150   void SetCallback(WatchpointHitCallback callback, void *callback_baton,
0151                    bool is_synchronous = false);
0152 
0153   void SetCallback(WatchpointHitCallback callback,
0154                    const lldb::BatonSP &callback_baton_sp,
0155                    bool is_synchronous = false);
0156 
0157   void ClearCallback();
0158 
0159   /// Invoke the callback action when the watchpoint is hit.
0160   ///
0161   /// \param[in] context
0162   ///     Described the watchpoint event.
0163   ///
0164   /// \return
0165   ///     \b true if the target should stop at this watchpoint and \b false not.
0166   bool InvokeCallback(StoppointCallbackContext *context);
0167 
0168   // Condition
0169   /// Set the watchpoint's condition.
0170   ///
0171   /// \param[in] condition
0172   ///    The condition expression to evaluate when the watchpoint is hit.
0173   ///    Pass in nullptr to clear the condition.
0174   void SetCondition(const char *condition);
0175 
0176   /// Return a pointer to the text of the condition expression.
0177   ///
0178   /// \return
0179   ///    A pointer to the condition expression text, or nullptr if no
0180   //     condition has been set.
0181   const char *GetConditionText() const;
0182 
0183   void TurnOnEphemeralMode();
0184 
0185   void TurnOffEphemeralMode();
0186 
0187   bool IsDisabledDuringEphemeralMode();
0188 
0189   const CompilerType &GetCompilerType() { return m_type; }
0190 
0191 private:
0192   friend class Target;
0193   friend class WatchpointList;
0194   friend class StopInfoWatchpoint; // This needs to call UndoHitCount()
0195 
0196   void ResetHistoricValues() {
0197     m_old_value_sp.reset();
0198     m_new_value_sp.reset();
0199   }
0200 
0201   void UndoHitCount() { m_hit_counter.Decrement(); }
0202 
0203   Target &m_target;
0204   bool m_enabled;           // Is this watchpoint enabled
0205   bool m_is_hardware;       // Is this a hardware watchpoint
0206   bool m_is_watch_variable; // True if set via 'watchpoint set variable'.
0207   bool m_is_ephemeral;      // True if the watchpoint is in the ephemeral mode,
0208                             // meaning that it is
0209   // undergoing a pair of temporary disable/enable actions to avoid recursively
0210   // triggering further watchpoint events.
0211   uint32_t m_disabled_count; // Keep track of the count that the watchpoint is
0212                              // disabled while in ephemeral mode.
0213   // At the end of the ephemeral mode when the watchpoint is to be enabled
0214   // again, we check the count, if it is more than 1, it means the user-
0215   // supplied actions actually want the watchpoint to be disabled!
0216   uint32_t m_watch_read : 1, // 1 if we stop when the watched data is read from
0217       m_watch_write : 1,     // 1 if we stop when the watched data is written to
0218       m_watch_modify : 1;    // 1 if we stop when the watched data is changed
0219   uint32_t m_ignore_count;      // Number of times to ignore this watchpoint
0220   std::string m_decl_str;       // Declaration information, if any.
0221   std::string m_watch_spec_str; // Spec for the watchpoint.
0222   lldb::ValueObjectSP m_old_value_sp;
0223   lldb::ValueObjectSP m_new_value_sp;
0224   CompilerType m_type;
0225   Status m_error; // An error object describing errors associated with this
0226                   // watchpoint.
0227   WatchpointOptions m_options; // Settable watchpoint options, which is a
0228                                // delegate to handle the callback machinery.
0229   std::unique_ptr<UserExpression> m_condition_up; // The condition to test.
0230 
0231   void SetID(lldb::watch_id_t id) { m_id = id; }
0232 
0233   void SendWatchpointChangedEvent(lldb::WatchpointEventType eventKind);
0234 
0235   Watchpoint(const Watchpoint &) = delete;
0236   const Watchpoint &operator=(const Watchpoint &) = delete;
0237 };
0238 
0239 } // namespace lldb_private
0240 
0241 #endif // LLDB_BREAKPOINT_WATCHPOINT_H