Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===-- BreakpointOptions.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_BREAKPOINTOPTIONS_H
0010 #define LLDB_BREAKPOINT_BREAKPOINTOPTIONS_H
0011 
0012 #include <memory>
0013 #include <string>
0014 
0015 #include "lldb/Utility/Baton.h"
0016 #include "lldb/Utility/Flags.h"
0017 #include "lldb/Utility/StringList.h"
0018 #include "lldb/Utility/StructuredData.h"
0019 #include "lldb/lldb-private.h"
0020 
0021 namespace lldb_private {
0022 
0023 /// \class BreakpointOptions BreakpointOptions.h
0024 /// "lldb/Breakpoint/BreakpointOptions.h" Class that manages the options on a
0025 /// breakpoint or breakpoint location.
0026 
0027 class BreakpointOptions {
0028 friend class BreakpointLocation;
0029 friend class BreakpointName;
0030 friend class lldb_private::BreakpointOptionGroup;
0031 friend class Breakpoint;
0032 
0033 public:
0034   enum OptionKind {
0035     eCallback     = 1 << 0,
0036     eEnabled      = 1 << 1,
0037     eOneShot      = 1 << 2,
0038     eIgnoreCount  = 1 << 3,
0039     eThreadSpec   = 1 << 4,
0040     eCondition    = 1 << 5,
0041     eAutoContinue = 1 << 6,
0042     eAllOptions   = (eCallback | eEnabled | eOneShot | eIgnoreCount | eThreadSpec
0043                      | eCondition | eAutoContinue)
0044   };
0045   struct CommandData {
0046     CommandData() = default;
0047 
0048     CommandData(const StringList &user_source, lldb::ScriptLanguage interp)
0049         : user_source(user_source), interpreter(interp), stop_on_error(true) {}
0050 
0051     virtual ~CommandData() = default;
0052 
0053     static const char *GetSerializationKey() { return "BKPTCMDData"; }
0054 
0055     StructuredData::ObjectSP SerializeToStructuredData();
0056 
0057     static std::unique_ptr<CommandData>
0058     CreateFromStructuredData(const StructuredData::Dictionary &options_dict,
0059                              Status &error);
0060 
0061     StringList user_source;
0062     std::string script_source;
0063     enum lldb::ScriptLanguage interpreter =
0064         lldb::eScriptLanguageNone; // eScriptLanguageNone means command
0065                                    // interpreter.
0066     bool stop_on_error = true;
0067 
0068   private:
0069     enum class OptionNames : uint32_t {
0070       UserSource = 0,
0071       Interpreter,
0072       StopOnError,
0073       LastOptionName
0074     };
0075 
0076     static const char
0077         *g_option_names[static_cast<uint32_t>(OptionNames::LastOptionName)];
0078 
0079     static const char *GetKey(OptionNames enum_value) {
0080       return g_option_names[static_cast<uint32_t>(enum_value)];
0081     }
0082   };
0083 
0084   class CommandBaton : public TypedBaton<CommandData> {
0085   public:
0086     explicit CommandBaton(std::unique_ptr<CommandData> Data)
0087         : TypedBaton(std::move(Data)) {}
0088 
0089     void GetDescription(llvm::raw_ostream &s, lldb::DescriptionLevel level,
0090                         unsigned indentation) const override;
0091   };
0092 
0093   typedef std::shared_ptr<CommandBaton> CommandBatonSP;
0094 
0095   // Constructors and Destructors
0096 
0097   /// This constructor allows you to specify all the breakpoint options except
0098   /// the callback.  That one is more complicated, and better to do by hand.
0099   ///
0100   /// \param[in] condition
0101   ///    The expression which if it evaluates to \b true if we are to stop
0102   ///
0103   /// \param[in] enabled
0104   ///    Is this breakpoint enabled.
0105   ///
0106   /// \param[in] ignore
0107   ///    How many breakpoint hits we should ignore before stopping.
0108   ///
0109   /// \param[in] one_shot
0110   ///    Should this breakpoint delete itself after being hit once.
0111   ///
0112   /// \param[in] auto_continue
0113   ///    Should this breakpoint auto-continue after running its commands.
0114   ///
0115   BreakpointOptions(const char *condition, bool enabled = true,
0116                     int32_t ignore = 0, bool one_shot = false,
0117                     bool auto_continue = false);
0118 
0119   /// Breakpoints make options with all flags set.  Locations and Names make
0120   /// options with no flags set.
0121   BreakpointOptions(bool all_flags_set);
0122   BreakpointOptions(const BreakpointOptions &rhs);
0123 
0124   virtual ~BreakpointOptions();
0125 
0126   static std::unique_ptr<BreakpointOptions>
0127   CreateFromStructuredData(Target &target,
0128                            const StructuredData::Dictionary &data_dict,
0129                            Status &error);
0130 
0131   virtual StructuredData::ObjectSP SerializeToStructuredData();
0132 
0133   static const char *GetSerializationKey() { return "BKPTOptions"; }
0134 
0135   // Operators
0136   const BreakpointOptions &operator=(const BreakpointOptions &rhs);
0137 
0138   /// Copy over only the options set in the incoming BreakpointOptions.
0139   void CopyOverSetOptions(const BreakpointOptions &rhs);
0140 
0141   // Callbacks
0142   //
0143   // Breakpoint callbacks come in two forms, synchronous and asynchronous.
0144   // Synchronous callbacks will get run before any of the thread plans are
0145   // consulted, and if they return false the target will continue "under the
0146   // radar" of the thread plans.  There are a couple of restrictions to
0147   // synchronous callbacks:
0148   // 1) They should NOT resume the target themselves.
0149   //     Just return false if you want the target to restart.
0150   // 2) Breakpoints with synchronous callbacks can't have conditions
0151   //    (or rather, they can have them, but they won't do anything.
0152   //    Ditto with ignore counts, etc...  You are supposed to control that all
0153   //    through the callback.
0154   // Asynchronous callbacks get run as part of the "ShouldStop" logic in the
0155   // thread plan.  The logic there is:
0156   //   a) If the breakpoint is thread specific and not for this thread, continue
0157   //   w/o running the callback.
0158   //      NB. This is actually enforced underneath the breakpoint system, the
0159   //      Process plugin is expected to
0160   //      call BreakpointSite::IsValidForThread, and set the thread's StopInfo
0161   //      to "no reason".  That way,
0162   //      thread displays won't show stops for breakpoints not for that
0163   //      thread...
0164   //   b) If the ignore count says we shouldn't stop, then ditto.
0165   //   c) If the condition says we shouldn't stop, then ditto.
0166   //   d) Otherwise, the callback will get run, and if it returns true we will
0167   //      stop, and if false we won't.
0168   //  The asynchronous callback can run the target itself, but at present that
0169   //  should be the last action the callback does.  We will relax this condition
0170   //  at some point, but it will take a bit of plumbing to get that to work.
0171   //
0172 
0173   /// Adds a callback to the breakpoint option set.
0174   ///
0175   /// \param[in] callback
0176   ///    The function to be called when the breakpoint gets hit.
0177   ///
0178   /// \param[in] baton_sp
0179   ///    A baton which will get passed back to the callback when it is invoked.
0180   ///
0181   /// \param[in] synchronous
0182   ///    Whether this is a synchronous or asynchronous callback.  See discussion
0183   ///    above.
0184   void SetCallback(BreakpointHitCallback callback,
0185                    const lldb::BatonSP &baton_sp, bool synchronous = false);
0186 
0187   void SetCallback(BreakpointHitCallback callback,
0188                    const BreakpointOptions::CommandBatonSP &command_baton_sp,
0189                    bool synchronous = false);
0190 
0191   /// Returns the command line commands for the callback on this breakpoint.
0192   ///
0193   /// \param[out] command_list
0194   ///    The commands will be appended to this list.
0195   ///
0196   /// \return
0197   ///    \b true if the command callback is a command-line callback,
0198   ///    \b false otherwise.
0199   bool GetCommandLineCallbacks(StringList &command_list);
0200 
0201   /// Remove the callback from this option set.
0202   void ClearCallback();
0203 
0204   // The rest of these functions are meant to be used only within the
0205   // breakpoint handling mechanism.
0206 
0207   /// Use this function to invoke the callback for a specific stop.
0208   ///
0209   /// \param[in] context
0210   ///    The context in which the callback is to be invoked.  This includes the
0211   ///    stop event, the
0212   ///    execution context of the stop (since you might hit the same breakpoint
0213   ///    on multiple threads) and
0214   ///    whether we are currently executing synchronous or asynchronous
0215   ///    callbacks.
0216   ///
0217   /// \param[in] break_id
0218   ///    The breakpoint ID that owns this option set.
0219   ///
0220   /// \param[in] break_loc_id
0221   ///    The breakpoint location ID that owns this option set.
0222   ///
0223   /// \return
0224   ///     The callback return value.
0225   bool InvokeCallback(StoppointCallbackContext *context,
0226                       lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
0227 
0228   /// Used in InvokeCallback to tell whether it is the right time to run this
0229   /// kind of callback.
0230   ///
0231   /// \return
0232   ///     The synchronicity of our callback.
0233   bool IsCallbackSynchronous() const { return m_callback_is_synchronous; }
0234 
0235   /// Fetch the baton from the callback.
0236   ///
0237   /// \return
0238   ///     The baton.
0239   Baton *GetBaton();
0240 
0241   /// Fetch  a const version of the baton from the callback.
0242   ///
0243   /// \return
0244   ///     The baton.
0245   const Baton *GetBaton() const;
0246 
0247   // Condition
0248   /// Set the breakpoint option's condition.
0249   ///
0250   /// \param[in] condition
0251   ///    The condition expression to evaluate when the breakpoint is hit.
0252   void SetCondition(const char *condition);
0253 
0254   /// Return a pointer to the text of the condition expression.
0255   ///
0256   /// \return
0257   ///    A pointer to the condition expression text, or nullptr if no
0258   //     condition has been set.
0259   const char *GetConditionText(size_t *hash = nullptr) const;
0260 
0261   // Enabled/Ignore Count
0262 
0263   /// Check the Enable/Disable state.
0264   /// \return
0265   ///     \b true if the breakpoint is enabled, \b false if disabled.
0266   bool IsEnabled() const { return m_enabled; }
0267 
0268   /// If \a enable is \b true, enable the breakpoint, if \b false disable it.
0269   void SetEnabled(bool enabled) {
0270     m_enabled = enabled;
0271     m_set_flags.Set(eEnabled);
0272   }
0273 
0274   /// Check the auto-continue state.
0275   /// \return
0276   ///     \b true if the breakpoint is set to auto-continue, \b false otherwise.
0277   bool IsAutoContinue() const { return m_auto_continue; }
0278 
0279   /// Set the auto-continue state.
0280   void SetAutoContinue(bool auto_continue) {
0281     m_auto_continue = auto_continue;
0282     m_set_flags.Set(eAutoContinue);
0283   }
0284 
0285   /// Check the One-shot state.
0286   /// \return
0287   ///     \b true if the breakpoint is one-shot, \b false otherwise.
0288   bool IsOneShot() const { return m_one_shot; }
0289 
0290   /// If \a enable is \b true, enable the breakpoint, if \b false disable it.
0291   void SetOneShot(bool one_shot) {
0292     m_one_shot = one_shot;
0293     m_set_flags.Set(eOneShot);
0294   }
0295 
0296   /// Set the breakpoint to ignore the next \a count breakpoint hits.
0297   /// \param[in] n
0298   ///    The number of breakpoint hits to ignore.
0299   void SetIgnoreCount(uint32_t n) {
0300     m_ignore_count = n;
0301     m_set_flags.Set(eIgnoreCount);
0302   }
0303 
0304   /// Return the current Ignore Count.
0305   /// \return
0306   ///     The number of breakpoint hits to be ignored.
0307   uint32_t GetIgnoreCount() const { return m_ignore_count; }
0308 
0309   /// Return the current thread spec for this option. This will return nullptr
0310   /// if the no thread specifications have been set for this Option yet.
0311   /// \return
0312   ///     The thread specification pointer for this option, or nullptr if none
0313   ///     has
0314   ///     been set yet.
0315   const ThreadSpec *GetThreadSpecNoCreate() const;
0316 
0317   /// Returns a pointer to the ThreadSpec for this option, creating it. if it
0318   /// hasn't been created already.   This API is used for setting the
0319   /// ThreadSpec items for this option.
0320   ThreadSpec *GetThreadSpec();
0321 
0322   void SetThreadID(lldb::tid_t thread_id);
0323 
0324   void GetDescription(Stream *s, lldb::DescriptionLevel level) const;
0325 
0326   /// Check if the breakpoint option has a callback set.
0327   ///
0328   /// \return
0329   ///    If the breakpoint option has a callback, \b true otherwise \b false.
0330   bool HasCallback() const;
0331 
0332   /// This is the default empty callback.
0333   static bool NullCallback(void *baton, StoppointCallbackContext *context,
0334                            lldb::user_id_t break_id,
0335                            lldb::user_id_t break_loc_id);
0336 
0337   /// Set a callback based on BreakpointOptions::CommandData. \param[in]
0338   /// cmd_data
0339   ///     A UP holding the new'ed CommandData object.
0340   ///     The breakpoint will take ownership of pointer held by this object.
0341   void SetCommandDataCallback(std::unique_ptr<CommandData> &cmd_data);
0342 
0343   void Clear();
0344 
0345   bool AnySet() const {
0346     return m_set_flags.AnySet(eAllOptions);
0347   }
0348 
0349 protected:
0350   // Classes that inherit from BreakpointOptions can see and modify these
0351   bool IsOptionSet(OptionKind kind)
0352   {
0353     return m_set_flags.Test(kind);
0354   }
0355 
0356   enum class OptionNames {
0357     ConditionText = 0,
0358     IgnoreCount,
0359     EnabledState,
0360     OneShotState,
0361     AutoContinue,
0362     LastOptionName
0363   };
0364   static const char *g_option_names[(size_t)OptionNames::LastOptionName];
0365 
0366   static const char *GetKey(OptionNames enum_value) {
0367     return g_option_names[(size_t)enum_value];
0368   }
0369 
0370   static bool BreakpointOptionsCallbackFunction(
0371       void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id,
0372       lldb::user_id_t break_loc_id);
0373 
0374   void SetThreadSpec(std::unique_ptr<ThreadSpec> &thread_spec_up);
0375 
0376 private:
0377   /// For BreakpointOptions only
0378 
0379   /// This is the callback function pointer
0380   BreakpointHitCallback m_callback;
0381   /// This is the client data for the callback
0382   lldb::BatonSP m_callback_baton_sp;
0383   bool m_baton_is_command_baton;
0384   bool m_callback_is_synchronous;
0385   bool m_enabled;
0386   /// If set, the breakpoint delete itself after being hit once.
0387   bool m_one_shot;
0388   /// Number of times to ignore this breakpoint.
0389   uint32_t m_ignore_count;
0390   /// Thread for which this breakpoint will stop.
0391   std::unique_ptr<ThreadSpec> m_thread_spec_up;
0392   /// The condition to test.
0393   std::string m_condition_text;
0394   /// Its hash, so that locations know when the condition is updated.
0395   size_t m_condition_text_hash;
0396   /// If set, inject breakpoint condition into process.
0397   bool m_inject_condition;
0398   /// If set, auto-continue from breakpoint.
0399   bool m_auto_continue;
0400   /// Which options are set at this level.
0401   /// Drawn from BreakpointOptions::SetOptionsFlags.
0402   Flags m_set_flags;
0403 };
0404 
0405 } // namespace lldb_private
0406 
0407 #endif // LLDB_BREAKPOINT_BREAKPOINTOPTIONS_H