LLDB mainline
Watchpoint.cpp
Go to the documentation of this file.
1//===-- Watchpoint.cpp ----------------------------------------------------===//
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
10
12#include "lldb/Core/Value.h"
17#include "lldb/Target/Process.h"
18#include "lldb/Target/Target.h"
21#include "lldb/Utility/Log.h"
22#include "lldb/Utility/Stream.h"
23
24using namespace lldb;
25using namespace lldb_private;
26
28 const CompilerType *type, bool hardware)
29 : StoppointSite(0, addr, size, hardware), m_target(target),
30 m_enabled(false), m_is_hardware(hardware), m_is_watch_variable(false),
31 m_is_ephemeral(false), m_disabled_count(0), m_watch_read(0),
32 m_watch_write(0), m_watch_was_read(0), m_watch_was_written(0),
33 m_ignore_count(0), m_false_alarms(0), m_being_created(true) {
34
35 if (type && type->IsValid())
36 m_type = *type;
37 else {
38 // If we don't have a known type, then we force it to unsigned int of the
39 // right size.
40 auto type_system_or_err =
42 if (auto err = type_system_or_err.takeError()) {
44 "Failed to set type.");
45 } else {
46 if (auto ts = *type_system_or_err)
47 m_type =
48 ts->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 8 * size);
49 else
51 "Failed to set type. Typesystem is no longer live.");
52 }
53 }
54
55 // Set the initial value of the watched variable:
56 if (m_target.GetProcessSP()) {
57 ExecutionContext exe_ctx;
58 m_target.GetProcessSP()->CalculateExecutionContext(exe_ctx);
59 CaptureWatchedValue(exe_ctx);
60 }
61 m_being_created = false;
62}
63
64Watchpoint::~Watchpoint() = default;
65
66// This function is used when "baton" doesn't need to be freed
67void Watchpoint::SetCallback(WatchpointHitCallback callback, void *baton,
68 bool is_synchronous) {
69 // The default "Baton" class will keep a copy of "baton" and won't free or
70 // delete it when it goes goes out of scope.
71 m_options.SetCallback(callback, std::make_shared<UntypedBaton>(baton),
72 is_synchronous);
73
74 SendWatchpointChangedEvent(eWatchpointEventTypeCommandChanged);
75}
76
77// This function is used when a baton needs to be freed and therefore is
78// contained in a "Baton" subclass.
79void Watchpoint::SetCallback(WatchpointHitCallback callback,
80 const BatonSP &callback_baton_sp,
81 bool is_synchronous) {
82 m_options.SetCallback(callback, callback_baton_sp, is_synchronous);
83 SendWatchpointChangedEvent(eWatchpointEventTypeCommandChanged);
84}
85
88 SendWatchpointChangedEvent(eWatchpointEventTypeCommandChanged);
89}
90
91void Watchpoint::SetDeclInfo(const std::string &str) { m_decl_str = str; }
92
94
95void Watchpoint::SetWatchSpec(const std::string &str) {
96 m_watch_spec_str = str;
97}
98
101 return m_is_hardware;
102}
103
105
107
109 ConstString watch_name("$__lldb__watch_value");
111 Address watch_address(GetLoadAddress());
112 if (!m_type.IsValid()) {
113 // Don't know how to report new & old values, since we couldn't make a
114 // scalar type for this watchpoint. This works around an assert in
115 // ValueObjectMemory::Create.
116 // FIXME: This should not happen, but if it does in some case we care about,
117 // we can go grab the value raw and print it as unsigned.
118 return false;
119 }
121 exe_ctx.GetBestExecutionContextScope(), watch_name.GetStringRef(),
122 watch_address, m_type);
123 m_new_value_sp = m_new_value_sp->CreateConstantValue(watch_name);
124 return (m_new_value_sp && m_new_value_sp->GetError().Success());
125}
126
129 if (m_false_alarms) {
132 m_false_alarms = 0;
133 } else {
136 }
137 }
138}
139
140// RETURNS - true if we should stop at this breakpoint, false if we
141// should continue.
142
145
146 return IsEnabled();
147}
148
150 DumpWithLevel(s, level);
151}
152
153void Watchpoint::Dump(Stream *s) const {
155}
156
157// If prefix is nullptr, we display the watch id and ignore the prefix
158// altogether.
159void Watchpoint::DumpSnapshots(Stream *s, const char *prefix) const {
160 if (!prefix) {
161 s->Printf("\nWatchpoint %u hit:", GetID());
162 prefix = "";
163 }
164
165 if (m_old_value_sp) {
166 const char *old_value_cstr = m_old_value_sp->GetValueAsCString();
167 if (old_value_cstr && old_value_cstr[0])
168 s->Printf("\n%sold value: %s", prefix, old_value_cstr);
169 else {
170 const char *old_summary_cstr = m_old_value_sp->GetSummaryAsCString();
171 if (old_summary_cstr && old_summary_cstr[0])
172 s->Printf("\n%sold value: %s", prefix, old_summary_cstr);
173 }
174 }
175
176 if (m_new_value_sp) {
177 const char *new_value_cstr = m_new_value_sp->GetValueAsCString();
178 if (new_value_cstr && new_value_cstr[0])
179 s->Printf("\n%snew value: %s", prefix, new_value_cstr);
180 else {
181 const char *new_summary_cstr = m_new_value_sp->GetSummaryAsCString();
182 if (new_summary_cstr && new_summary_cstr[0])
183 s->Printf("\n%snew value: %s", prefix, new_summary_cstr);
184 }
185 }
186}
187
189 lldb::DescriptionLevel description_level) const {
190 if (s == nullptr)
191 return;
192
193 assert(description_level >= lldb::eDescriptionLevelBrief &&
194 description_level <= lldb::eDescriptionLevelVerbose);
195
196 s->Printf("Watchpoint %u: addr = 0x%8.8" PRIx64
197 " size = %u state = %s type = %s%s",
199 IsEnabled() ? "enabled" : "disabled", m_watch_read ? "r" : "",
200 m_watch_write ? "w" : "");
201
202 if (description_level >= lldb::eDescriptionLevelFull) {
203 if (!m_decl_str.empty())
204 s->Printf("\n declare @ '%s'", m_decl_str.c_str());
205 if (!m_watch_spec_str.empty())
206 s->Printf("\n watchpoint spec = '%s'", m_watch_spec_str.c_str());
207
208 // Dump the snapshots we have taken.
209 DumpSnapshots(s, " ");
210
211 if (GetConditionText())
212 s->Printf("\n condition = '%s'", GetConditionText());
213 m_options.GetCallbackDescription(s, description_level);
214 }
215
216 if (description_level >= lldb::eDescriptionLevelVerbose) {
217 s->Printf("\n hw_index = %i hit_count = %-4u ignore_count = %-4u",
219 }
220}
221
222bool Watchpoint::IsEnabled() const { return m_enabled; }
223
224// Within StopInfo.cpp, we purposely turn on the ephemeral mode right before
225// temporarily disable the watchpoint in order to perform possible watchpoint
226// actions without triggering further watchpoint events. After the temporary
227// disabled watchpoint is enabled, we then turn off the ephemeral mode.
228
230
232 m_is_ephemeral = false;
233 // Leaving ephemeral mode, reset the m_disabled_count!
235}
236
238 return m_disabled_count > 1 && m_is_ephemeral;
239}
240
241void Watchpoint::SetEnabled(bool enabled, bool notify) {
242 if (!enabled) {
243 if (!m_is_ephemeral)
245 else
247
248 // Don't clear the snapshots for now.
249 // Within StopInfo.cpp, we purposely do disable/enable watchpoint while
250 // performing watchpoint actions.
251 }
252 bool changed = enabled != m_enabled;
253 m_enabled = enabled;
254 if (notify && !m_is_ephemeral && changed)
255 SendWatchpointChangedEvent(enabled ? eWatchpointEventTypeEnabled
256 : eWatchpointEventTypeDisabled);
257}
258
260 int old_watch_read = m_watch_read;
261 int old_watch_write = m_watch_write;
262 m_watch_read = (type & LLDB_WATCH_TYPE_READ) != 0;
263 m_watch_write = (type & LLDB_WATCH_TYPE_WRITE) != 0;
264 if (notify &&
265 (old_watch_read != m_watch_read || old_watch_write != m_watch_write))
266 SendWatchpointChangedEvent(eWatchpointEventTypeTypeChanged);
267}
268
269bool Watchpoint::WatchpointRead() const { return m_watch_read != 0; }
270
271bool Watchpoint::WatchpointWrite() const { return m_watch_write != 0; }
272
274
276 bool changed = m_ignore_count != n;
277 m_ignore_count = n;
278 if (changed)
279 SendWatchpointChangedEvent(eWatchpointEventTypeIgnoreChanged);
280}
281
283 return m_options.InvokeCallback(context, GetID());
284}
285
286void Watchpoint::SetCondition(const char *condition) {
287 if (condition == nullptr || condition[0] == '\0') {
288 if (m_condition_up)
289 m_condition_up.reset();
290 } else {
291 // Pass nullptr for expr_prefix (no translation-unit level definitions).
294 condition, llvm::StringRef(), lldb::eLanguageTypeUnknown,
296 error));
297 if (error.Fail()) {
298 // FIXME: Log something...
299 m_condition_up.reset();
300 }
301 }
302 SendWatchpointChangedEvent(eWatchpointEventTypeConditionChanged);
303}
304
305const char *Watchpoint::GetConditionText() const {
306 if (m_condition_up)
307 return m_condition_up->GetUserText();
308 else
309 return nullptr;
310}
311
313 lldb::WatchpointEventType eventKind) {
314 if (!m_being_created &&
315 GetTarget().EventTypeHasListeners(
317 WatchpointEventData *data =
318 new Watchpoint::WatchpointEventData(eventKind, shared_from_this());
320 }
321}
322
324 if (data == nullptr)
325 return;
326
327 if (!m_being_created &&
328 GetTarget().EventTypeHasListeners(Target::eBroadcastBitWatchpointChanged))
330 else
331 delete data;
332}
333
335 WatchpointEventType sub_type, const WatchpointSP &new_watchpoint_sp)
336 : m_watchpoint_event(sub_type), m_new_watchpoint_sp(new_watchpoint_sp) {}
337
339
341 static ConstString g_flavor("Watchpoint::WatchpointEventData");
342 return g_flavor;
343}
344
347}
348
350 return m_new_watchpoint_sp;
351}
352
353WatchpointEventType
355 return m_watchpoint_event;
356}
357
359
362 if (event) {
363 const EventData *event_data = event->GetData();
364 if (event_data &&
366 return static_cast<const WatchpointEventData *>(event->GetData());
367 }
368 return nullptr;
369}
370
371WatchpointEventType
373 const EventSP &event_sp) {
374 const WatchpointEventData *data = GetEventDataFromEvent(event_sp.get());
375
376 if (data == nullptr)
377 return eWatchpointEventTypeInvalidType;
378 else
379 return data->GetWatchpointEventType();
380}
381
383 const EventSP &event_sp) {
384 WatchpointSP wp_sp;
385
386 const WatchpointEventData *data = GetEventDataFromEvent(event_sp.get());
387 if (data)
388 wp_sp = data->m_new_watchpoint_sp;
389
390 return wp_sp;
391}
static llvm::raw_ostream & error(Stream &strm)
#define lldbassert(x)
Definition: LLDBAssert.h:15
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:360
A section + offset based address class.
Definition: Address.h:59
void BroadcastEvent(lldb::EventSP &event_sp)
Broadcast an event which has no associated data.
Definition: Broadcaster.h:262
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
A uniqued constant string class.
Definition: ConstString.h:39
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:201
virtual ConstString GetFlavor() const =0
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
ExecutionContextScope * GetBestExecutionContextScope() const
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)
void Increment(uint32_t difference=1)
uint32_t GetHardwareIndex() const
Definition: StoppointSite.h:41
lldb::break_id_t GetID() const
Definition: StoppointSite.h:49
uint32_t GetHitCount() const
Definition: StoppointSite.h:33
void SetHardwareIndex(uint32_t index)
Definition: StoppointSite.h:43
StoppointHitCounter m_hit_counter
Number of times this breakpoint/watchpoint has been hit.
Definition: StoppointSite.h:71
virtual lldb::addr_t GetLoadAddress() const
Definition: StoppointSite.h:27
uint32_t m_byte_size
The size in bytes of stoppoint, e.g.
Definition: StoppointSite.h:68
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
UserExpression * GetUserExpressionForLanguage(llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language, Expression::ResultType desired_type, const EvaluateExpressionOptions &options, ValueObject *ctx_obj, Status &error)
Definition: Target.cpp:2415
const lldb::ProcessSP & GetProcessSP() const
Definition: Target.cpp:218
@ eBroadcastBitWatchpointChanged
Definition: Target.h:485
llvm::Expected< lldb::TypeSystemSP > GetScratchTypeSystemForLanguage(lldb::LanguageType language, bool create_on_demand=true)
Definition: Target.cpp:2333
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, llvm::StringRef name, const Address &address, lldb::TypeSP &type_sp)
void ClearCallback()
Remove the callback from this option set.
bool InvokeCallback(StoppointCallbackContext *context, lldb::user_id_t watch_id)
Use this function to invoke the callback for a specific stop.
void SetCallback(WatchpointHitCallback callback, const lldb::BatonSP &baton_sp, bool synchronous=false)
Adds a callback to the watchpoint option set.
void GetCallbackDescription(Stream *s, lldb::DescriptionLevel level) const
Get description for callback only.
static lldb::WatchpointSP GetWatchpointFromEvent(const lldb::EventSP &event_sp)
Definition: Watchpoint.cpp:382
lldb::WatchpointEventType GetWatchpointEventType() const
Definition: Watchpoint.cpp:354
ConstString GetFlavor() const override
Definition: Watchpoint.cpp:345
static lldb::WatchpointEventType GetWatchpointEventTypeFromEvent(const lldb::EventSP &event_sp)
Definition: Watchpoint.cpp:372
void Dump(Stream *s) const override
Definition: Watchpoint.cpp:358
static const WatchpointEventData * GetEventDataFromEvent(const Event *event_sp)
Definition: Watchpoint.cpp:361
WatchpointEventData(lldb::WatchpointEventType sub_type, const lldb::WatchpointSP &new_watchpoint_sp)
bool IsWatchVariable() const
Definition: Watchpoint.cpp:104
void SetCallback(WatchpointHitCallback callback, void *callback_baton, bool is_synchronous=false)
Set the callback action invoked when the watchpoint is hit.
Definition: Watchpoint.cpp:67
Watchpoint(Target &target, lldb::addr_t addr, uint32_t size, const CompilerType *type, bool hardware=true)
Definition: Watchpoint.cpp:27
bool CaptureWatchedValue(const ExecutionContext &exe_ctx)
Definition: Watchpoint.cpp:108
void DumpSnapshots(Stream *s, const char *prefix=nullptr) const
Definition: Watchpoint.cpp:159
uint32_t GetIgnoreCount() const
Definition: Watchpoint.cpp:273
bool InvokeCallback(StoppointCallbackContext *context)
Invoke the callback action when the watchpoint is hit.
Definition: Watchpoint.cpp:282
void SetIgnoreCount(uint32_t n)
Definition: Watchpoint.cpp:275
std::unique_ptr< UserExpression > m_condition_up
Definition: Watchpoint.h:201
void SetEnabled(bool enabled, bool notify=true)
Definition: Watchpoint.cpp:241
bool IsHardware() const override
Definition: Watchpoint.cpp:99
void Dump(Stream *s) const override
Definition: Watchpoint.cpp:153
void SendWatchpointChangedEvent(lldb::WatchpointEventType eventKind)
Definition: Watchpoint.cpp:312
std::string GetWatchSpec()
Definition: Watchpoint.cpp:93
lldb::ValueObjectSP m_new_value_sp
Definition: Watchpoint.h:192
lldb::ValueObjectSP m_old_value_sp
Definition: Watchpoint.h:191
bool ShouldStop(StoppointCallbackContext *context) override
Definition: Watchpoint.cpp:143
void SetWatchSpec(const std::string &str)
Definition: Watchpoint.cpp:95
void SetWatchVariable(bool val)
Definition: Watchpoint.cpp:106
const char * GetConditionText() const
Return a pointer to the text of the condition expression.
Definition: Watchpoint.cpp:305
void GetDescription(Stream *s, lldb::DescriptionLevel level)
Definition: Watchpoint.cpp:149
WatchpointOptions m_options
Definition: Watchpoint.h:197
bool WatchpointRead() const
Definition: Watchpoint.cpp:269
std::string m_watch_spec_str
Definition: Watchpoint.h:190
void SetCondition(const char *condition)
Set the watchpoint's condition.
Definition: Watchpoint.cpp:286
void SetWatchpointType(uint32_t type, bool notify=true)
Definition: Watchpoint.cpp:259
void SetDeclInfo(const std::string &str)
Definition: Watchpoint.cpp:91
bool WatchpointWrite() const
Definition: Watchpoint.cpp:271
void DumpWithLevel(Stream *s, lldb::DescriptionLevel description_level) const
Definition: Watchpoint.cpp:188
void IncrementFalseAlarmsAndReviseHitCount()
Definition: Watchpoint.cpp:127
#define LLDB_WATCH_TYPE_WRITE
Definition: lldb-defines.h:46
#define LLDB_INVALID_INDEX32
Definition: lldb-defines.h:75
#define LLDB_WATCH_TYPE_READ
Definition: lldb-defines.h:45
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:309
Definition: SBAddress.h:15
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
@ eDescriptionLevelBrief
@ eDescriptionLevelFull
@ eDescriptionLevelVerbose
@ eLanguageTypeUnknown
Unknown or invalid language value.
@ eLanguageTypeC
Non-standardized C, such as K&R.
@ eEncodingUint
unsigned integer
uint64_t addr_t
Definition: lldb-types.h:83