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 bool WatchpointModify() const;
80 uint32_t GetIgnoreCount() const;
81 void SetIgnoreCount(uint32_t n);
82 void SetWatchpointType(uint32_t type, bool notify = true);
83 void SetDeclInfo(const std::string &str);
84 std::string GetWatchSpec();
85 void SetWatchSpec(const std::string &str);
86 bool WatchedValueReportable(const ExecutionContext &exe_ctx);
87
88 // Snapshot management interface.
89 bool IsWatchVariable() const;
90 void SetWatchVariable(bool val);
91 bool CaptureWatchedValue(const ExecutionContext &exe_ctx);
92
93 /// \struct WatchpointVariableContext
94 /// \brief Represents the context of a watchpoint variable.
95 ///
96 /// This struct encapsulates the information related to a watchpoint variable,
97 /// including the watch ID and the execution context in which it is being
98 /// used. This struct is passed as a Baton to the \b
99 /// VariableWatchpointDisabler breakpoint callback.
101 /// \brief Constructor for WatchpointVariableContext.
102 /// \param watch_id The ID of the watchpoint.
103 /// \param exe_ctx The execution context associated with the watchpoint.
107
108 lldb::watch_id_t watch_id; ///< The ID of the watchpoint.
110 exe_ctx; ///< The execution context associated with the watchpoint.
111 };
112
113 class WatchpointVariableBaton : public TypedBaton<WatchpointVariableContext> {
114 public:
115 WatchpointVariableBaton(std::unique_ptr<WatchpointVariableContext> Data)
116 : TypedBaton(std::move(Data)) {}
117 };
118
120
121 /// Callback routine to disable the watchpoint set on a local variable when
122 /// it goes out of scope.
123 static bool VariableWatchpointDisabler(
124 void *baton, lldb_private::StoppointCallbackContext *context,
125 lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
126
128 void Dump(Stream *s) const override;
129 bool DumpSnapshots(Stream *s, const char *prefix = nullptr) const;
130 void DumpWithLevel(Stream *s, lldb::DescriptionLevel description_level) const;
131 Target &GetTarget() { return m_target; }
132 const Status &GetError() { return m_error; }
133
134 /// Returns the WatchpointOptions structure set for this watchpoint.
135 ///
136 /// \return
137 /// A pointer to this watchpoint's WatchpointOptions.
139
140 /// Set the callback action invoked when the watchpoint is hit.
141 ///
142 /// \param[in] callback
143 /// The method that will get called when the watchpoint is hit.
144 /// \param[in] callback_baton
145 /// A void * pointer that will get passed back to the callback function.
146 /// \param[in] is_synchronous
147 /// If \b true the callback will be run on the private event thread
148 /// before the stop event gets reported. If false, the callback will get
149 /// handled on the public event thread after the stop has been posted.
150 void SetCallback(WatchpointHitCallback callback, void *callback_baton,
151 bool is_synchronous = false);
152
154 const lldb::BatonSP &callback_baton_sp,
155 bool is_synchronous = false);
156
157 void ClearCallback();
158
159 /// Invoke the callback action when the watchpoint is hit.
160 ///
161 /// \param[in] context
162 /// Described the watchpoint event.
163 ///
164 /// \return
165 /// \b true if the target should stop at this watchpoint and \b false not.
167
168 // Condition
169 /// Set the watchpoint's condition.
170 ///
171 /// \param[in] condition
172 /// The condition expression to evaluate when the watchpoint is hit.
173 /// Pass in nullptr to clear the condition.
174 void SetCondition(const char *condition);
175
176 /// Return a pointer to the text of the condition expression.
177 ///
178 /// \return
179 /// A pointer to the condition expression text, or nullptr if no
180 // condition has been set.
181 const char *GetConditionText() const;
182
183 void TurnOnEphemeralMode();
184
186
188
190
191private:
192 friend class Target;
193 friend class WatchpointList;
194 friend class StopInfoWatchpoint; // This needs to call UndoHitCount()
195
197 m_old_value_sp.reset();
198 m_new_value_sp.reset();
199 }
200
202
204 bool m_enabled; // Is this watchpoint enabled
205 bool m_is_hardware; // Is this a hardware watchpoint
206 bool m_is_watch_variable; // True if set via 'watchpoint set variable'.
207 bool m_is_ephemeral; // True if the watchpoint is in the ephemeral mode,
208 // meaning that it is
209 // undergoing a pair of temporary disable/enable actions to avoid recursively
210 // triggering further watchpoint events.
211 uint32_t m_disabled_count; // Keep track of the count that the watchpoint is
212 // disabled while in ephemeral mode.
213 // At the end of the ephemeral mode when the watchpoint is to be enabled
214 // again, we check the count, if it is more than 1, it means the user-
215 // supplied actions actually want the watchpoint to be disabled!
216 uint32_t m_watch_read : 1, // 1 if we stop when the watched data is read from
217 m_watch_write : 1, // 1 if we stop when the watched data is written to
218 m_watch_modify : 1; // 1 if we stop when the watched data is changed
219 uint32_t m_ignore_count; // Number of times to ignore this watchpoint
220 std::string m_decl_str; // Declaration information, if any.
221 std::string m_watch_spec_str; // Spec for the watchpoint.
225 Status m_error; // An error object describing errors associated with this
226 // watchpoint.
227 WatchpointOptions m_options; // Settable watchpoint options, which is a
228 // delegate to handle the callback machinery.
229 std::unique_ptr<UserExpression> m_condition_up; // The condition to test.
230
232
233 void SendWatchpointChangedEvent(lldb::WatchpointEventType eventKind);
234
235 Watchpoint(const Watchpoint &) = delete;
236 const Watchpoint &operator=(const Watchpoint &) = delete;
237};
238
239} // namespace lldb_private
240
241#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:49
StoppointHitCounter m_hit_counter
Number of times this breakpoint/watchpoint has been hit.
Definition: StoppointSite.h:64
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:538
lldb::WatchpointEventType m_watchpoint_event
Definition: Watchpoint.h:54
lldb::WatchpointEventType GetWatchpointEventType() const
Definition: Watchpoint.cpp:510
const WatchpointEventData & operator=(const WatchpointEventData &)=delete
llvm::StringRef GetFlavor() const override
Definition: Watchpoint.cpp:501
static lldb::WatchpointEventType GetWatchpointEventTypeFromEvent(const lldb::EventSP &event_sp)
Definition: Watchpoint.cpp:528
void Dump(Stream *s) const override
Definition: Watchpoint.cpp:514
WatchpointEventData(const WatchpointEventData &)=delete
static const WatchpointEventData * GetEventDataFromEvent(const Event *event_sp)
Definition: Watchpoint.cpp:517
WatchpointEventData(lldb::WatchpointEventType sub_type, const lldb::WatchpointSP &new_watchpoint_sp)
WatchpointVariableBaton(std::unique_ptr< WatchpointVariableContext > Data)
Definition: Watchpoint.h:115
bool IsWatchVariable() const
Definition: Watchpoint.cpp:198
void SetCallback(WatchpointHitCallback callback, void *callback_baton, bool is_synchronous=false)
Set the callback action invoked when the watchpoint is hit.
Definition: Watchpoint.cpp:73
bool CaptureWatchedValue(const ExecutionContext &exe_ctx)
Definition: Watchpoint.cpp:202
uint32_t GetIgnoreCount() const
Definition: Watchpoint.cpp:442
bool InvokeCallback(StoppointCallbackContext *context)
Invoke the callback action when the watchpoint is hit.
Definition: Watchpoint.cpp:451
void SetIgnoreCount(uint32_t n)
Definition: Watchpoint.cpp:444
std::unique_ptr< UserExpression > m_condition_up
Definition: Watchpoint.h:229
void SetEnabled(bool enabled, bool notify=true)
Definition: Watchpoint.cpp:407
bool IsHardware() const override
Definition: Watchpoint.cpp:193
void Dump(Stream *s) const override
Definition: Watchpoint.cpp:269
bool WatchedValueReportable(const ExecutionContext &exe_ctx)
Definition: Watchpoint.cpp:221
Watchpoint(const Watchpoint &)=delete
void SendWatchpointChangedEvent(lldb::WatchpointEventType eventKind)
Definition: Watchpoint.cpp:481
std::string GetWatchSpec()
Definition: Watchpoint.cpp:187
lldb::ValueObjectSP m_new_value_sp
Definition: Watchpoint.h:223
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:133
bool DumpSnapshots(Stream *s, const char *prefix=nullptr) const
Definition: Watchpoint.cpp:275
lldb::ValueObjectSP m_old_value_sp
Definition: Watchpoint.h:222
bool ShouldStop(StoppointCallbackContext *context) override
Definition: Watchpoint.cpp:259
void SetWatchSpec(const std::string &str)
Definition: Watchpoint.cpp:189
void SetWatchVariable(bool val)
Definition: Watchpoint.cpp:200
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:474
void GetDescription(Stream *s, lldb::DescriptionLevel level)
Definition: Watchpoint.cpp:265
const Watchpoint & operator=(const Watchpoint &)=delete
WatchpointOptions m_options
Definition: Watchpoint.h:227
bool WatchpointRead() const
Definition: Watchpoint.cpp:436
std::string m_watch_spec_str
Definition: Watchpoint.h:221
const CompilerType & GetCompilerType()
Definition: Watchpoint.h:189
bool SetupVariableWatchpointDisabler(lldb::StackFrameSP frame_sp) const
Definition: Watchpoint.cpp:92
bool WatchpointModify() const
Definition: Watchpoint.cpp:440
void SetCondition(const char *condition)
Set the watchpoint's condition.
Definition: Watchpoint.cpp:455
WatchpointOptions * GetOptions()
Returns the WatchpointOptions structure set for this watchpoint.
Definition: Watchpoint.h:138
void SetWatchpointType(uint32_t type, bool notify=true)
Definition: Watchpoint.cpp:423
const Status & GetError()
Definition: Watchpoint.h:132
void SetDeclInfo(const std::string &str)
Definition: Watchpoint.cpp:185
bool WatchpointWrite() const
Definition: Watchpoint.cpp:438
void DumpWithLevel(Stream *s, lldb::DescriptionLevel description_level) const
Definition: Watchpoint.cpp:340
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:412
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:472
std::shared_ptr< lldb_private::Baton > BatonSP
Definition: lldb-forward.h:311
std::shared_ptr< lldb_private::Event > EventSP
Definition: lldb-forward.h:337
std::shared_ptr< lldb_private::Watchpoint > WatchpointSP
Definition: lldb-forward.h:477
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:100
ExecutionContext exe_ctx
The execution context associated with the watchpoint.
Definition: Watchpoint.h:110
lldb::watch_id_t watch_id
The ID of the watchpoint.
Definition: Watchpoint.h:108
WatchpointVariableContext(lldb::watch_id_t watch_id, ExecutionContext exe_ctx)
Constructor for WatchpointVariableContext.
Definition: Watchpoint.h:104