1 //===-- BreakpointOptions.h -------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_BreakpointOptions_h_
10 #define liblldb_BreakpointOptions_h_
15 #include "lldb/Utility/Baton.h"
16 #include "lldb/Utility/Flags.h"
17 #include "lldb/Utility/StringList.h"
18 #include "lldb/Utility/StructuredData.h"
19 #include "lldb/lldb-private.h"
21 namespace lldb_private {
23 /// \class BreakpointOptions BreakpointOptions.h
24 /// "lldb/Breakpoint/BreakpointOptions.h" Class that manages the options on a
25 /// breakpoint or breakpoint location.
27 class BreakpointOptions {
28 friend class BreakpointLocation;
29 friend class BreakpointName;
30 friend class lldb_private::BreakpointOptionGroup;
31 friend class Breakpoint;
38 eIgnoreCount = 1 << 3,
41 eAutoContinue = 1 << 6,
42 eAllOptions = (eCallback | eEnabled | eOneShot | eIgnoreCount | eThreadSpec
43 | eCondition | eAutoContinue)
47 : user_source(), script_source(),
48 interpreter(lldb::eScriptLanguageNone), stop_on_error(true) {}
50 CommandData(const StringList &user_source, lldb::ScriptLanguage interp)
51 : user_source(user_source), script_source(), interpreter(interp),
52 stop_on_error(true) {}
54 ~CommandData() = default;
56 static const char *GetSerializationKey() { return "BKPTCMDData"; }
58 StructuredData::ObjectSP SerializeToStructuredData();
60 static std::unique_ptr<CommandData>
61 CreateFromStructuredData(const StructuredData::Dictionary &options_dict,
64 StringList user_source;
65 std::string script_source;
66 enum lldb::ScriptLanguage
67 interpreter; // eScriptLanguageNone means command interpreter.
71 enum class OptionNames : uint32_t {
79 *g_option_names[static_cast<uint32_t>(OptionNames::LastOptionName)];
81 static const char *GetKey(OptionNames enum_value) {
82 return g_option_names[static_cast<uint32_t>(enum_value)];
86 class CommandBaton : public TypedBaton<CommandData> {
88 explicit CommandBaton(std::unique_ptr<CommandData> Data)
89 : TypedBaton(std::move(Data)) {}
91 void GetDescription(Stream *s, lldb::DescriptionLevel level) const override;
94 typedef std::shared_ptr<CommandBaton> CommandBatonSP;
96 // Constructors and Destructors
98 /// This constructor allows you to specify all the breakpoint options except
99 /// the callback. That one is more complicated, and better to do by hand.
101 /// \param[in] condition
102 /// The expression which if it evaluates to \b true if we are to stop
104 /// \param[in] enabled
105 /// Is this breakpoint enabled.
107 /// \param[in] ignore
108 /// How many breakpoint hits we should ignore before stopping.
110 BreakpointOptions(const char *condition, bool enabled = true,
111 int32_t ignore = 0, bool one_shot = false,
112 bool auto_continue = false);
114 /// Breakpoints make options with all flags set. Locations and Names make
115 /// options with no flags set.
116 BreakpointOptions(bool all_flags_set);
117 BreakpointOptions(const BreakpointOptions &rhs);
119 virtual ~BreakpointOptions();
121 static std::unique_ptr<BreakpointOptions>
122 CreateFromStructuredData(Target &target,
123 const StructuredData::Dictionary &data_dict,
126 virtual StructuredData::ObjectSP SerializeToStructuredData();
128 static const char *GetSerializationKey() { return "BKPTOptions"; }
131 const BreakpointOptions &operator=(const BreakpointOptions &rhs);
133 /// Copy over only the options set in the incoming BreakpointOptions.
134 void CopyOverSetOptions(const BreakpointOptions &rhs);
138 // Breakpoint callbacks come in two forms, synchronous and asynchronous.
139 // Synchronous callbacks will get run before any of the thread plans are
140 // consulted, and if they return false the target will continue "under the
141 // radar" of the thread plans. There are a couple of restrictions to
142 // synchronous callbacks:
143 // 1) They should NOT resume the target themselves.
144 // Just return false if you want the target to restart.
145 // 2) Breakpoints with synchronous callbacks can't have conditions
146 // (or rather, they can have them, but they won't do anything.
147 // Ditto with ignore counts, etc... You are supposed to control that all
148 // through the callback.
149 // Asynchronous callbacks get run as part of the "ShouldStop" logic in the
150 // thread plan. The logic there is:
151 // a) If the breakpoint is thread specific and not for this thread, continue
152 // w/o running the callback.
153 // NB. This is actually enforced underneath the breakpoint system, the
154 // Process plugin is expected to
155 // call BreakpointSite::IsValidForThread, and set the thread's StopInfo
156 // to "no reason". That way,
157 // thread displays won't show stops for breakpoints not for that
159 // b) If the ignore count says we shouldn't stop, then ditto.
160 // c) If the condition says we shouldn't stop, then ditto.
161 // d) Otherwise, the callback will get run, and if it returns true we will
162 // stop, and if false we won't.
163 // The asynchronous callback can run the target itself, but at present that
164 // should be the last action the callback does. We will relax this condition
165 // at some point, but it will take a bit of plumbing to get that to work.
168 /// Adds a callback to the breakpoint option set.
170 /// \param[in] callback
171 /// The function to be called when the breakpoint gets hit.
173 /// \param[in] baton_sp
174 /// A baton which will get passed back to the callback when it is invoked.
176 /// \param[in] synchronous
177 /// Whether this is a synchronous or asynchronous callback. See discussion
179 void SetCallback(BreakpointHitCallback callback,
180 const lldb::BatonSP &baton_sp, bool synchronous = false);
182 void SetCallback(BreakpointHitCallback callback,
183 const BreakpointOptions::CommandBatonSP &command_baton_sp,
184 bool synchronous = false);
186 /// Returns the command line commands for the callback on this breakpoint.
188 /// \param[out] command_list
189 /// The commands will be appended to this list.
192 /// \btrue if the command callback is a command-line callback,
193 /// \bfalse otherwise.
194 bool GetCommandLineCallbacks(StringList &command_list);
196 /// Remove the callback from this option set.
197 void ClearCallback();
199 // The rest of these functions are meant to be used only within the
200 // breakpoint handling mechanism.
202 /// Use this function to invoke the callback for a specific stop.
204 /// \param[in] context
205 /// The context in which the callback is to be invoked. This includes the
207 /// execution context of the stop (since you might hit the same breakpoint
208 /// on multiple threads) and
209 /// whether we are currently executing synchronous or asynchronous
212 /// \param[in] break_id
213 /// The breakpoint ID that owns this option set.
215 /// \param[in] break_loc_id
216 /// The breakpoint location ID that owns this option set.
219 /// The callback return value.
220 bool InvokeCallback(StoppointCallbackContext *context,
221 lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
223 /// Used in InvokeCallback to tell whether it is the right time to run this
224 /// kind of callback.
227 /// The synchronicity of our callback.
228 bool IsCallbackSynchronous() const { return m_callback_is_synchronous; }
230 /// Fetch the baton from the callback.
236 /// Fetch a const version of the baton from the callback.
240 const Baton *GetBaton() const;
243 /// Set the breakpoint option's condition.
245 /// \param[in] condition
246 /// The condition expression to evaluate when the breakpoint is hit.
247 void SetCondition(const char *condition);
249 /// Return a pointer to the text of the condition expression.
252 /// A pointer to the condition expression text, or nullptr if no
253 // condition has been set.
254 const char *GetConditionText(size_t *hash = nullptr) const;
256 // Enabled/Ignore Count
258 /// Check the Enable/Disable state.
260 /// \b true if the breakpoint is enabled, \b false if disabled.
261 bool IsEnabled() const { return m_enabled; }
263 /// If \a enable is \b true, enable the breakpoint, if \b false disable it.
264 void SetEnabled(bool enabled) {
266 m_set_flags.Set(eEnabled);
269 /// Check the auto-continue state.
271 /// \b true if the breakpoint is set to auto-continue, \b false otherwise.
272 bool IsAutoContinue() const { return m_auto_continue; }
274 /// Set the auto-continue state.
275 void SetAutoContinue(bool auto_continue) {
276 m_auto_continue = auto_continue;
277 m_set_flags.Set(eAutoContinue);
280 /// Check the One-shot state.
282 /// \b true if the breakpoint is one-shot, \b false otherwise.
283 bool IsOneShot() const { return m_one_shot; }
285 /// If \a enable is \b true, enable the breakpoint, if \b false disable it.
286 void SetOneShot(bool one_shot) {
287 m_one_shot = one_shot;
288 m_set_flags.Set(eOneShot);
291 /// Set the breakpoint to ignore the next \a count breakpoint hits.
293 /// The number of breakpoint hits to ignore.
295 void SetIgnoreCount(uint32_t n) {
297 m_set_flags.Set(eIgnoreCount);
300 /// Return the current Ignore Count.
302 /// The number of breakpoint hits to be ignored.
303 uint32_t GetIgnoreCount() const { return m_ignore_count; }
305 /// Return the current thread spec for this option. This will return nullptr
306 /// if the no thread specifications have been set for this Option yet.
308 /// The thread specification pointer for this option, or nullptr if none
311 const ThreadSpec *GetThreadSpecNoCreate() const;
313 /// Returns a pointer to the ThreadSpec for this option, creating it. if it
314 /// hasn't been created already. This API is used for setting the
315 /// ThreadSpec items for this option.
316 ThreadSpec *GetThreadSpec();
318 void SetThreadID(lldb::tid_t thread_id);
320 void GetDescription(Stream *s, lldb::DescriptionLevel level) const;
322 /// Returns true if the breakpoint option has a callback set.
323 bool HasCallback() const;
325 /// This is the default empty callback.
326 static bool NullCallback(void *baton, StoppointCallbackContext *context,
327 lldb::user_id_t break_id,
328 lldb::user_id_t break_loc_id);
330 /// Set a callback based on BreakpointOptions::CommandData. \param[in]
332 /// A UP holding the new'ed CommandData object.
333 /// The breakpoint will take ownership of pointer held by this object.
334 void SetCommandDataCallback(std::unique_ptr<CommandData> &cmd_data);
338 bool AnySet() const {
339 return m_set_flags.AnySet(eAllOptions);
343 // Classes that inherit from BreakpointOptions can see and modify these
344 bool IsOptionSet(OptionKind kind)
346 return m_set_flags.Test(kind);
349 enum class OptionNames {
357 static const char *g_option_names[(size_t)OptionNames::LastOptionName];
359 static const char *GetKey(OptionNames enum_value) {
360 return g_option_names[(size_t)enum_value];
363 static bool BreakpointOptionsCallbackFunction(
364 void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id,
365 lldb::user_id_t break_loc_id);
367 void SetThreadSpec(std::unique_ptr<ThreadSpec> &thread_spec_up);
370 // For BreakpointOptions only
371 BreakpointHitCallback m_callback; // This is the callback function pointer
372 lldb::BatonSP m_callback_baton_sp; // This is the client data for the callback
373 bool m_baton_is_command_baton;
374 bool m_callback_is_synchronous;
377 uint32_t m_ignore_count; // Number of times to ignore this breakpoint
378 std::unique_ptr<ThreadSpec>
379 m_thread_spec_up; // Thread for which this breakpoint will take
380 std::string m_condition_text; // The condition to test.
381 size_t m_condition_text_hash; // Its hash, so that locations know when the
382 // condition is updated.
383 bool m_auto_continue; // If set, auto-continue from breakpoint.
384 Flags m_set_flags; // Which options are set at this level. Drawn
385 // from BreakpointOptions::SetOptionsFlags.
388 } // namespace lldb_private
390 #endif // liblldb_BreakpointOptions_h_