Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===-- WatchpointOptions.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_WATCHPOINTOPTIONS_H
0010 #define LLDB_BREAKPOINT_WATCHPOINTOPTIONS_H
0011 
0012 #include <memory>
0013 #include <string>
0014 
0015 #include "lldb/Utility/Baton.h"
0016 #include "lldb/Utility/StringList.h"
0017 #include "lldb/lldb-private.h"
0018 
0019 namespace lldb_private {
0020 
0021 /// \class WatchpointOptions WatchpointOptions.h
0022 /// "lldb/Breakpoint/WatchpointOptions.h" Class that manages the options on a
0023 /// watchpoint.
0024 
0025 class WatchpointOptions {
0026 public:
0027   // Constructors and Destructors
0028   /// Default constructor.  The watchpoint is enabled, and has no condition,
0029   /// callback, ignore count, etc...
0030   WatchpointOptions();
0031   WatchpointOptions(const WatchpointOptions &rhs);
0032 
0033   static WatchpointOptions *CopyOptionsNoCallback(WatchpointOptions &rhs);
0034   /// This constructor allows you to specify all the watchpoint options.
0035   ///
0036   /// \param[in] callback
0037   ///    This is the plugin for some code that gets run, returns \b true if we
0038   ///    are to stop.
0039   ///
0040   /// \param[in] baton
0041   ///    Client data that will get passed to the callback.
0042   ///
0043   /// \param[in] thread_id
0044   ///    Only stop if \a thread_id hits the watchpoint.
0045   WatchpointOptions(WatchpointHitCallback callback, void *baton,
0046                     lldb::tid_t thread_id = LLDB_INVALID_THREAD_ID);
0047 
0048   virtual ~WatchpointOptions();
0049 
0050   // Operators
0051   const WatchpointOptions &operator=(const WatchpointOptions &rhs);
0052 
0053   // Callbacks
0054   //
0055   // Watchpoint callbacks come in two forms, synchronous and asynchronous.
0056   // Synchronous callbacks will get run before any of the thread plans are
0057   // consulted, and if they return false the target will continue "under the
0058   // radar" of the thread plans.  There are a couple of restrictions to
0059   // synchronous callbacks: 1) They should NOT resume the target themselves.
0060   // Just return false if you want the target to restart. 2) Watchpoints with
0061   // synchronous callbacks can't have conditions (or rather, they can have
0062   // them, but they won't do anything.  Ditto with ignore counts, etc...
0063   // You are supposed to control that all through the callback.
0064   // Asynchronous callbacks get run as part of the "ShouldStop" logic in the
0065   // thread plan.  The logic there is:
0066   //   a) If the watchpoint is thread specific and not for this thread, continue
0067   //      w/o running the callback.
0068   //   b) If the ignore count says we shouldn't stop, then ditto.
0069   //   c) If the condition says we shouldn't stop, then ditto.
0070   //   d) Otherwise, the callback will get run, and if it returns true we will
0071   //      stop, and if false we won't.
0072   //  The asynchronous callback can run the target itself, but at present that
0073   //  should be the last action the callback does.  We will relax this
0074   //  condition at some point, but it will take a bit of plumbing to get
0075   //  that to work.
0076 
0077   /// Adds a callback to the watchpoint option set.
0078   ///
0079   /// \param[in] callback
0080   ///    The function to be called when the watchpoint gets hit.
0081   ///
0082   /// \param[in] baton_sp
0083   ///    A baton which will get passed back to the callback when it is invoked.
0084   ///
0085   /// \param[in] synchronous
0086   ///    Whether this is a synchronous or asynchronous callback.
0087   ///    See discussion above.
0088   void SetCallback(WatchpointHitCallback callback,
0089                    const lldb::BatonSP &baton_sp, bool synchronous = false);
0090 
0091   /// Remove the callback from this option set.
0092   void ClearCallback();
0093 
0094   // The rest of these functions are meant to be used only within the
0095   // watchpoint handling mechanism.
0096 
0097   /// Use this function to invoke the callback for a specific stop.
0098   ///
0099   /// \param[in] context
0100   ///    The context in which the callback is to be invoked.  This includes the
0101   ///    stop event, the execution context of the stop (since you might hit
0102   ///    the same watchpoint on multiple threads) and whether we are currently
0103   ///    executing synchronous or asynchronous callbacks.
0104   ///
0105   /// \param[in] watch_id
0106   ///    The watchpoint ID that owns this option set.
0107   ///
0108   /// \return
0109   ///     The callback return value.
0110   bool InvokeCallback(StoppointCallbackContext *context,
0111                       lldb::user_id_t watch_id);
0112 
0113   /// Used in InvokeCallback to tell whether it is the right time to run this
0114   /// kind of callback.
0115   ///
0116   /// \return
0117   ///     The synchronicity of our callback.
0118   bool IsCallbackSynchronous() { return m_callback_is_synchronous; }
0119 
0120   /// Fetch the baton from the callback.
0121   ///
0122   /// \return
0123   ///     The baton.
0124   Baton *GetBaton();
0125 
0126   /// Fetch a const version of the baton from the callback.
0127   ///
0128   /// \return
0129   ///     The baton.
0130   const Baton *GetBaton() const;
0131 
0132   /// Return the current thread spec for this option. This will return nullptr
0133   /// if the no thread specifications have been set for this WatchpointOptions
0134   /// yet.
0135   ///
0136   /// \return
0137   ///     The thread specification pointer for this option, or nullptr if none
0138   ///     has been set yet.
0139   const ThreadSpec *GetThreadSpecNoCreate() const;
0140 
0141   /// Returns a pointer to the ThreadSpec for this option, creating it if it
0142   /// hasn't been created already. This API is used for setting the
0143   /// ThreadSpec items for this WatchpointOptions.
0144   ThreadSpec *GetThreadSpec();
0145 
0146   void SetThreadID(lldb::tid_t thread_id);
0147 
0148   void GetDescription(Stream *s, lldb::DescriptionLevel level) const;
0149 
0150   /// Get description for callback only.
0151   void GetCallbackDescription(Stream *s, lldb::DescriptionLevel level) const;
0152 
0153   /// Returns true if the watchpoint option has a callback set.
0154   bool HasCallback();
0155 
0156   /// This is the default empty callback.
0157   /// \return
0158   ///     The thread id for which the watchpoint hit will stop,
0159   ///     LLDB_INVALID_THREAD_ID for all threads.
0160   static bool NullCallback(void *baton, StoppointCallbackContext *context,
0161                            lldb::user_id_t watch_id);
0162 
0163   struct CommandData {
0164     CommandData() = default;
0165 
0166     ~CommandData() = default;
0167 
0168     StringList user_source;
0169     std::string script_source;
0170     bool stop_on_error = true;
0171   };
0172 
0173   class CommandBaton : public TypedBaton<CommandData> {
0174   public:
0175     CommandBaton(std::unique_ptr<CommandData> Data)
0176         : TypedBaton(std::move(Data)) {}
0177 
0178     void GetDescription(llvm::raw_ostream &s, lldb::DescriptionLevel level,
0179                         unsigned indentation) const override;
0180   };
0181 
0182 private:
0183   WatchpointHitCallback m_callback;  // This is the callback function pointer
0184   lldb::BatonSP m_callback_baton_sp; // This is the client data for the callback
0185   bool m_callback_is_synchronous = false;
0186   std::unique_ptr<ThreadSpec>
0187       m_thread_spec_up; // Thread for which this watchpoint will take
0188 };
0189 
0190 } // namespace lldb_private
0191 
0192 #endif // LLDB_BREAKPOINT_WATCHPOINTOPTIONS_H