LLDB mainline
Watchpoint.h
Go to the documentation of this file.
1//===-- Watchpoint.h --------------------------------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_BREAKPOINT_WATCHPOINT_H
10#define LLDB_BREAKPOINT_WATCHPOINT_H
11
12#include <memory>
13#include <string>
14
18#include "lldb/Target/Target.h"
19#include "lldb/Utility/UserID.h"
20#include "lldb/lldb-private.h"
21
22namespace lldb_private {
23
24class Watchpoint : public std::enable_shared_from_this<Watchpoint>,
25 public StoppointSite {
26public:
28 public:
29 WatchpointEventData(lldb::WatchpointEventType sub_type,
30 const lldb::WatchpointSP &new_watchpoint_sp);
31
33
34 static llvm::StringRef GetFlavorString();
35
36 llvm::StringRef GetFlavor() const override;
37
38 lldb::WatchpointEventType GetWatchpointEventType() const;
39
41
42 void Dump(Stream *s) const override;
43
44 static lldb::WatchpointEventType
46
49
50 static const WatchpointEventData *
51 GetEventDataFromEvent(const Event *event_sp);
52
53 private:
54 lldb::WatchpointEventType m_watchpoint_event;
56
59 };
60
61 Watchpoint(Target &target, lldb::addr_t addr, uint32_t size,
62 const CompilerType *type, bool hardware = true);
63
64 ~Watchpoint() override;
65
66 bool IsEnabled() const;
67
68 // This doesn't really enable/disable the watchpoint. It is currently just
69 // for use in the Process plugin's {Enable,Disable}Watchpoint, which should
70 // be used instead.
71 void SetEnabled(bool enabled, bool notify = true);
72
73 bool IsHardware() const override;
74
75 bool ShouldStop(StoppointCallbackContext *context) override;
76
77 bool WatchpointRead() const;
78 bool WatchpointWrite() const;
79 uint32_t GetIgnoreCount() const;
80 void SetIgnoreCount(uint32_t n);
81 void SetWatchpointType(uint32_t type, bool notify = true);
82 void SetDeclInfo(const std::string &str);
83 std::string GetWatchSpec();
84 void SetWatchSpec(const std::string &str);
85
86 // Snapshot management interface.
87 bool IsWatchVariable() const;
88 void SetWatchVariable(bool val);
89 bool CaptureWatchedValue(const ExecutionContext &exe_ctx);
90
91 /// \struct WatchpointVariableContext
92 /// \brief Represents the context of a watchpoint variable.
93 ///
94 /// This struct encapsulates the information related to a watchpoint variable,
95 /// including the watch ID and the execution context in which it is being
96 /// used. This struct is passed as a Baton to the \b
97 /// VariableWatchpointDisabler breakpoint callback.
99 /// \brief Constructor for WatchpointVariableContext.
100 /// \param watch_id The ID of the watchpoint.
101 /// \param exe_ctx The execution context associated with the watchpoint.
105
106 lldb::watch_id_t watch_id; ///< The ID of the watchpoint.
108 exe_ctx; ///< The execution context associated with the watchpoint.
109 };
110
111 class WatchpointVariableBaton : public TypedBaton<WatchpointVariableContext> {
112 public:
113 WatchpointVariableBaton(std::unique_ptr<WatchpointVariableContext> Data)
114 : TypedBaton(std::move(Data)) {}
115 };
116
118
119 /// Callback routine to disable the watchpoint set on a local variable when
120 /// it goes out of scope.
121 static bool VariableWatchpointDisabler(
122 void *baton, lldb_private::StoppointCallbackContext *context,
123 lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
124
126 void Dump(Stream *s) const override;
127 void DumpSnapshots(Stream *s, const char *prefix = nullptr) const;
128 void DumpWithLevel(Stream *s, lldb::DescriptionLevel description_level) const;
129 Target &GetTarget() { return m_target; }
130 const Status &GetError() { return m_error; }
131
132 /// Returns the WatchpointOptions structure set for this watchpoint.
133 ///
134 /// \return
135 /// A pointer to this watchpoint's WatchpointOptions.
137
138 /// Set the callback action invoked when the watchpoint is hit.
139 ///
140 /// \param[in] callback
141 /// The method that will get called when the watchpoint is hit.
142 /// \param[in] callback_baton
143 /// A void * pointer that will get passed back to the callback function.
144 /// \param[in] is_synchronous
145 /// If \b true the callback will be run on the private event thread
146 /// before the stop event gets reported. If false, the callback will get
147 /// handled on the public event thread after the stop has been posted.
148 void SetCallback(WatchpointHitCallback callback, void *callback_baton,
149 bool is_synchronous = false);
150
152 const lldb::BatonSP &callback_baton_sp,
153 bool is_synchronous = false);
154
155 void ClearCallback();
156
157 /// Invoke the callback action when the watchpoint is hit.
158 ///
159 /// \param[in] context
160 /// Described the watchpoint event.
161 ///
162 /// \return
163 /// \b true if the target should stop at this watchpoint and \b false not.
165
166 // Condition
167 /// Set the watchpoint's condition.
168 ///
169 /// \param[in] condition
170 /// The condition expression to evaluate when the watchpoint is hit.
171 /// Pass in nullptr to clear the condition.
172 void SetCondition(const char *condition);
173
174 /// Return a pointer to the text of the condition expression.
175 ///
176 /// \return
177 /// A pointer to the condition expression text, or nullptr if no
178 // condition has been set.
179 const char *GetConditionText() const;
180
181 void TurnOnEphemeralMode();
182
184
186
188
189private:
190 friend class Target;
191 friend class WatchpointList;
192 friend class StopInfoWatchpoint; // This needs to call UndoHitCount()
193
195 m_old_value_sp.reset();
196 m_new_value_sp.reset();
197 }
198
200
202 bool m_enabled; // Is this watchpoint enabled
203 bool m_is_hardware; // Is this a hardware watchpoint
204 bool m_is_watch_variable; // True if set via 'watchpoint set variable'.
205 bool m_is_ephemeral; // True if the watchpoint is in the ephemeral mode,
206 // meaning that it is
207 // undergoing a pair of temporary disable/enable actions to avoid recursively
208 // triggering further watchpoint events.
209 uint32_t m_disabled_count; // Keep track of the count that the watchpoint is
210 // disabled while in ephemeral mode.
211 // At the end of the ephemeral mode when the watchpoint is to be enabled
212 // again, we check the count, if it is more than 1, it means the user-
213 // supplied actions actually want the watchpoint to be disabled!
214 uint32_t m_watch_read : 1, // 1 if we stop when the watched data is read from
215 m_watch_write : 1; // 1 if we stop when the watched data is written to
216 uint32_t m_ignore_count; // Number of times to ignore this watchpoint
217 std::string m_decl_str; // Declaration information, if any.
218 std::string m_watch_spec_str; // Spec for the watchpoint.
222 Status m_error; // An error object describing errors associated with this
223 // watchpoint.
225 m_options; // Settable watchpoint options, which is a delegate to handle
226 // the callback machinery.
228
229 std::unique_ptr<UserExpression> m_condition_up; // The condition to test.
230
232
233 void SendWatchpointChangedEvent(lldb::WatchpointEventType eventKind);
234
235 void SendWatchpointChangedEvent(WatchpointEventData *data);
236
237 Watchpoint(const Watchpoint &) = delete;
238 const Watchpoint &operator=(const Watchpoint &) = delete;
239};
240
241} // namespace lldb_private
242
243#endif // LLDB_BREAKPOINT_WATCHPOINT_H
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
An error handling class.
Definition: Status.h:44
General Outline: When we hit a breakpoint we need to package up whatever information is needed to eva...
void Decrement(uint32_t difference=1)
lldb::break_id_t m_id
Stoppoint site ID.
Definition: StoppointSite.h:53
StoppointHitCounter m_hit_counter
Number of times this breakpoint/watchpoint has been hit.
Definition: StoppointSite.h:71
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
This class is used by Watchpoint to manage a list of watchpoints,.
"lldb/Breakpoint/WatchpointOptions.h" Class that manages the options on a watchpoint.
static lldb::WatchpointSP GetWatchpointFromEvent(const lldb::EventSP &event_sp)
Definition: Watchpoint.cpp:455
lldb::WatchpointEventType m_watchpoint_event
Definition: Watchpoint.h:54
lldb::WatchpointEventType GetWatchpointEventType() const
Definition: Watchpoint.cpp:427
const WatchpointEventData & operator=(const WatchpointEventData &)=delete
llvm::StringRef GetFlavor() const override
Definition: Watchpoint.cpp:418
static lldb::WatchpointEventType GetWatchpointEventTypeFromEvent(const lldb::EventSP &event_sp)
Definition: Watchpoint.cpp:445
void Dump(Stream *s) const override
Definition: Watchpoint.cpp:431
WatchpointEventData(const WatchpointEventData &)=delete
static const WatchpointEventData * GetEventDataFromEvent(const Event *event_sp)
Definition: Watchpoint.cpp:434
WatchpointEventData(lldb::WatchpointEventType sub_type, const lldb::WatchpointSP &new_watchpoint_sp)
WatchpointVariableBaton(std::unique_ptr< WatchpointVariableContext > Data)
Definition: Watchpoint.h:113
bool IsWatchVariable() const
Definition: Watchpoint.cpp:191
void SetCallback(WatchpointHitCallback callback, void *callback_baton, bool is_synchronous=false)
Set the callback action invoked when the watchpoint is hit.
Definition: Watchpoint.cpp:66
bool CaptureWatchedValue(const ExecutionContext &exe_ctx)
Definition: Watchpoint.cpp:195
void DumpSnapshots(Stream *s, const char *prefix=nullptr) const
Definition: Watchpoint.cpp:233
uint32_t GetIgnoreCount() const
Definition: Watchpoint.cpp:347
bool InvokeCallback(StoppointCallbackContext *context)
Invoke the callback action when the watchpoint is hit.
Definition: Watchpoint.cpp:356
void SetIgnoreCount(uint32_t n)
Definition: Watchpoint.cpp:349
std::unique_ptr< UserExpression > m_condition_up
Definition: Watchpoint.h:229
void SetEnabled(bool enabled, bool notify=true)
Definition: Watchpoint.cpp:315
bool IsHardware() const override
Definition: Watchpoint.cpp:186
void Dump(Stream *s) const override
Definition: Watchpoint.cpp:227
Watchpoint(const Watchpoint &)=delete
void SendWatchpointChangedEvent(lldb::WatchpointEventType eventKind)
Definition: Watchpoint.cpp:386
std::string GetWatchSpec()
Definition: Watchpoint.cpp:180
lldb::ValueObjectSP m_new_value_sp
Definition: Watchpoint.h:220
static bool VariableWatchpointDisabler(void *baton, lldb_private::StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id)
Callback routine to disable the watchpoint set on a local variable when it goes out of scope.
Definition: Watchpoint.cpp:126
lldb::ValueObjectSP m_old_value_sp
Definition: Watchpoint.h:219
bool ShouldStop(StoppointCallbackContext *context) override
Definition: Watchpoint.cpp:217
void SetWatchSpec(const std::string &str)
Definition: Watchpoint.cpp:182
void SetWatchVariable(bool val)
Definition: Watchpoint.cpp:193
void SetID(lldb::watch_id_t id)
Definition: Watchpoint.h:231
const char * GetConditionText() const
Return a pointer to the text of the condition expression.
Definition: Watchpoint.cpp:379
void GetDescription(Stream *s, lldb::DescriptionLevel level)
Definition: Watchpoint.cpp:223
const Watchpoint & operator=(const Watchpoint &)=delete
WatchpointOptions m_options
Definition: Watchpoint.h:225
bool WatchpointRead() const
Definition: Watchpoint.cpp:343
std::string m_watch_spec_str
Definition: Watchpoint.h:218
const CompilerType & GetCompilerType()
Definition: Watchpoint.h:187
bool SetupVariableWatchpointDisabler(lldb::StackFrameSP frame_sp) const
Definition: Watchpoint.cpp:85
void SetCondition(const char *condition)
Set the watchpoint's condition.
Definition: Watchpoint.cpp:360
WatchpointOptions * GetOptions()
Returns the WatchpointOptions structure set for this watchpoint.
Definition: Watchpoint.h:136
void SetWatchpointType(uint32_t type, bool notify=true)
Definition: Watchpoint.cpp:333
const Status & GetError()
Definition: Watchpoint.h:130
void SetDeclInfo(const std::string &str)
Definition: Watchpoint.cpp:178
bool WatchpointWrite() const
Definition: Watchpoint.cpp:345
void DumpWithLevel(Stream *s, lldb::DescriptionLevel description_level) const
Definition: Watchpoint.cpp:262
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
bool(* WatchpointHitCallback)(void *baton, StoppointCallbackContext *context, lldb::user_id_t watch_id)
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
Definition: lldb-forward.h:398
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:457
std::shared_ptr< lldb_private::Baton > BatonSP
Definition: lldb-forward.h:300
std::shared_ptr< lldb_private::Event > EventSP
Definition: lldb-forward.h:326
std::shared_ptr< lldb_private::Watchpoint > WatchpointSP
Definition: lldb-forward.h:462
int32_t watch_id_t
Definition: lldb-types.h:85
uint64_t user_id_t
Definition: lldb-types.h:80
uint64_t addr_t
Definition: lldb-types.h:79
Represents the context of a watchpoint variable.
Definition: Watchpoint.h:98
ExecutionContext exe_ctx
The execution context associated with the watchpoint.
Definition: Watchpoint.h:108
lldb::watch_id_t watch_id
The ID of the watchpoint.
Definition: Watchpoint.h:106
WatchpointVariableContext(lldb::watch_id_t watch_id, ExecutionContext exe_ctx)
Constructor for WatchpointVariableContext.
Definition: Watchpoint.h:102