1 //===-- WatchpointOptions.h -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_WatchpointOptions_h_
11 #define liblldb_WatchpointOptions_h_
18 // Other libraries and framework includes
20 #include "lldb/lldb-private.h"
21 #include "lldb/Core/Baton.h"
22 #include "lldb/Core/StringList.h"
24 namespace lldb_private {
26 //----------------------------------------------------------------------
27 /// @class WatchpointOptions WatchpointOptions.h "lldb/Breakpoint/WatchpointOptions.h"
28 /// @brief Class that manages the options on a watchpoint.
29 //----------------------------------------------------------------------
31 class WatchpointOptions
34 //------------------------------------------------------------------
35 // Constructors and Destructors
36 //------------------------------------------------------------------
37 //------------------------------------------------------------------
38 /// Default constructor. The watchpoint is enabled, and has no condition,
39 /// callback, ignore count, etc...
40 //------------------------------------------------------------------
42 WatchpointOptions(const WatchpointOptions& rhs);
44 static WatchpointOptions *
45 CopyOptionsNoCallback (WatchpointOptions &rhs);
46 //------------------------------------------------------------------
47 /// This constructor allows you to specify all the watchpoint options.
49 /// @param[in] callback
50 /// This is the plugin for some code that gets run, returns \b true if we are to stop.
53 /// Client data that will get passed to the callback.
55 /// @param[in] thread_id
56 /// Only stop if \a thread_id hits the watchpoint.
57 //------------------------------------------------------------------
58 WatchpointOptions(WatchpointHitCallback callback,
60 lldb::tid_t thread_id = LLDB_INVALID_THREAD_ID);
62 virtual ~WatchpointOptions();
64 //------------------------------------------------------------------
66 //------------------------------------------------------------------
67 const WatchpointOptions&
68 operator=(const WatchpointOptions& rhs);
70 //------------------------------------------------------------------
73 // Watchpoint callbacks come in two forms, synchronous and asynchronous. Synchronous callbacks will get
74 // run before any of the thread plans are consulted, and if they return false the target will continue
75 // "under the radar" of the thread plans. There are a couple of restrictions to synchronous callbacks:
76 // 1) They should NOT resume the target themselves. Just return false if you want the target to restart.
77 // 2) Watchpoints with synchronous callbacks can't have conditions (or rather, they can have them, but they
78 // won't do anything. Ditto with ignore counts, etc... You are supposed to control that all through the
80 // Asynchronous callbacks get run as part of the "ShouldStop" logic in the thread plan. The logic there is:
81 // a) If the watchpoint is thread specific and not for this thread, continue w/o running the callback.
82 // b) If the ignore count says we shouldn't stop, then ditto.
83 // c) If the condition says we shouldn't stop, then ditto.
84 // d) Otherwise, the callback will get run, and if it returns true we will stop, and if false we won't.
85 // The asynchronous callback can run the target itself, but at present that should be the last action the
86 // callback does. We will relax this condition at some point, but it will take a bit of plumbing to get
89 //------------------------------------------------------------------
91 //------------------------------------------------------------------
92 /// Adds a callback to the watchpoint option set.
94 /// @param[in] callback
95 /// The function to be called when the watchpoint gets hit.
97 /// @param[in] baton_sp
98 /// A baton which will get passed back to the callback when it is invoked.
100 /// @param[in] synchronous
101 /// Whether this is a synchronous or asynchronous callback. See discussion above.
102 //------------------------------------------------------------------
103 void SetCallback (WatchpointHitCallback callback, const lldb::BatonSP &baton_sp, bool synchronous = false);
105 //------------------------------------------------------------------
106 /// Remove the callback from this option set.
107 //------------------------------------------------------------------
108 void ClearCallback ();
110 // The rest of these functions are meant to be used only within the watchpoint handling mechanism.
112 //------------------------------------------------------------------
113 /// Use this function to invoke the callback for a specific stop.
115 /// @param[in] context
116 /// The context in which the callback is to be invoked. This includes the stop event, the
117 /// execution context of the stop (since you might hit the same watchpoint on multiple threads) and
118 /// whether we are currently executing synchronous or asynchronous callbacks.
120 /// @param[in] watch_id
121 /// The watchpoint ID that owns this option set.
124 /// The callback return value.
125 //------------------------------------------------------------------
126 bool InvokeCallback (StoppointCallbackContext *context, lldb::user_id_t watch_id);
128 //------------------------------------------------------------------
129 /// Used in InvokeCallback to tell whether it is the right time to run this kind of callback.
132 /// The synchronicity of our callback.
133 //------------------------------------------------------------------
134 bool IsCallbackSynchronous () {
135 return m_callback_is_synchronous;
138 //------------------------------------------------------------------
139 /// Fetch the baton from the callback.
143 //------------------------------------------------------------------
146 //------------------------------------------------------------------
147 /// Fetch a const version of the baton from the callback.
151 //------------------------------------------------------------------
152 const Baton *GetBaton () const;
154 //------------------------------------------------------------------
155 /// Return the current thread spec for this option. This will return nullptr if the no thread
156 /// specifications have been set for this Option yet.
158 /// The thread specification pointer for this option, or nullptr if none has
160 //------------------------------------------------------------------
162 GetThreadSpecNoCreate () const;
164 //------------------------------------------------------------------
165 /// Returns a pointer to the ThreadSpec for this option, creating it.
166 /// if it hasn't been created already. This API is used for setting the
167 /// ThreadSpec items for this option.
168 //------------------------------------------------------------------
173 SetThreadID(lldb::tid_t thread_id);
176 GetDescription (Stream *s, lldb::DescriptionLevel level) const;
178 //------------------------------------------------------------------
179 /// Get description for callback only.
180 //------------------------------------------------------------------
182 GetCallbackDescription (Stream *s, lldb::DescriptionLevel level) const;
184 //------------------------------------------------------------------
185 /// Returns true if the watchpoint option has a callback set.
186 //------------------------------------------------------------------
190 //------------------------------------------------------------------
191 /// This is the default empty callback.
193 /// The thread id for which the watchpoint hit will stop,
194 /// LLDB_INVALID_THREAD_ID for all threads.
195 //------------------------------------------------------------------
197 NullCallback (void *baton,
198 StoppointCallbackContext *context,
199 lldb::user_id_t watch_id);
210 ~CommandData() = default;
212 StringList user_source;
213 std::string script_source;
217 class CommandBaton : public Baton
220 CommandBaton (CommandData *data) :
225 ~CommandBaton() override
227 delete ((CommandData *)m_data);
232 GetDescription(Stream *s, lldb::DescriptionLevel level) const override;
236 //------------------------------------------------------------------
237 // Classes that inherit from WatchpointOptions can see and modify these
238 //------------------------------------------------------------------
241 //------------------------------------------------------------------
242 // For WatchpointOptions only
243 //------------------------------------------------------------------
244 WatchpointHitCallback m_callback; // This is the callback function pointer
245 lldb::BatonSP m_callback_baton_sp; // This is the client data for the callback
246 bool m_callback_is_synchronous;
247 std::unique_ptr<ThreadSpec> m_thread_spec_ap; // Thread for which this watchpoint will take
250 } // namespace lldb_private
252 #endif // liblldb_WatchpointOptions_h_