blob: 3dbd6d23821c087e195525fdbf215f2af8fee544 [file] [log] [blame]
Johnny Chen01a67862011-10-14 00:42:25 +00001//===-- Watchpoint.cpp ------------------------------------------*- C++ -*-===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Eugene Zelenko16fd7512015-10-30 18:50:12 +000014#include "lldb/Breakpoint/Watchpoint.h"
15
Johnny Chen16dcf712011-10-17 18:58:00 +000016#include "lldb/Breakpoint/StoppointCallbackContext.h"
Jim Inghama7dfb662012-10-23 07:20:06 +000017#include "lldb/Core/Value.h"
18#include "lldb/Core/ValueObject.h"
19#include "lldb/Core/ValueObjectMemory.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000020#include "lldb/Expression/UserExpression.h"
Jim Inghama7dfb662012-10-23 07:20:06 +000021#include "lldb/Symbol/ClangASTContext.h"
Johnny Chen16dcf712011-10-17 18:58:00 +000022#include "lldb/Target/Process.h"
23#include "lldb/Target/Target.h"
24#include "lldb/Target/ThreadSpec.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000025#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
27using namespace lldb;
28using namespace lldb_private;
29
Kate Stoneb9c1b512016-09-06 20:57:50 +000030Watchpoint::Watchpoint(Target &target, lldb::addr_t addr, uint32_t size,
31 const CompilerType *type, bool hardware)
32 : StoppointLocation(0, addr, size, hardware), m_target(target),
33 m_enabled(false), m_is_hardware(hardware), m_is_watch_variable(false),
34 m_is_ephemeral(false), m_disabled_count(0), m_watch_read(0),
35 m_watch_write(0), m_watch_was_read(0), m_watch_was_written(0),
36 m_ignore_count(0), m_false_alarms(0), m_decl_str(), m_watch_spec_str(),
37 m_type(), m_error(), m_options(), m_being_created(true) {
38 if (type && type->IsValid())
39 m_type = *type;
40 else {
41 // If we don't have a known type, then we force it to unsigned int of the
42 // right size.
43 ClangASTContext *ast_context = target.GetScratchClangASTContext();
44 m_type = ast_context->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint,
45 8 * size);
46 }
47
48 // Set the initial value of the watched variable:
49 if (m_target.GetProcessSP()) {
50 ExecutionContext exe_ctx;
51 m_target.GetProcessSP()->CalculateExecutionContext(exe_ctx);
52 CaptureWatchedValue(exe_ctx);
53 }
54 m_being_created = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055}
56
Eugene Zelenko16fd7512015-10-30 18:50:12 +000057Watchpoint::~Watchpoint() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058
Johnny Chene9a56272012-08-09 23:09:42 +000059// This function is used when "baton" doesn't need to be freed
Kate Stoneb9c1b512016-09-06 20:57:50 +000060void Watchpoint::SetCallback(WatchpointHitCallback callback, void *baton,
61 bool is_synchronous) {
62 // The default "Baton" class will keep a copy of "baton" and won't free
63 // or delete it when it goes goes out of scope.
Zachary Turner4e4fbe82016-09-13 17:53:38 +000064 m_options.SetCallback(callback, std::make_shared<UntypedBaton>(baton),
65 is_synchronous);
Kate Stoneb9c1b512016-09-06 20:57:50 +000066
67 SendWatchpointChangedEvent(eWatchpointEventTypeCommandChanged);
Johnny Chene9a56272012-08-09 23:09:42 +000068}
69
Kate Stoneb9c1b512016-09-06 20:57:50 +000070// This function is used when a baton needs to be freed and therefore is
Johnny Chene9a56272012-08-09 23:09:42 +000071// contained in a "Baton" subclass.
Kate Stoneb9c1b512016-09-06 20:57:50 +000072void Watchpoint::SetCallback(WatchpointHitCallback callback,
73 const BatonSP &callback_baton_sp,
74 bool is_synchronous) {
75 m_options.SetCallback(callback, callback_baton_sp, is_synchronous);
76 SendWatchpointChangedEvent(eWatchpointEventTypeCommandChanged);
Johnny Chene9a56272012-08-09 23:09:42 +000077}
78
Kate Stoneb9c1b512016-09-06 20:57:50 +000079void Watchpoint::ClearCallback() {
80 m_options.ClearCallback();
81 SendWatchpointChangedEvent(eWatchpointEventTypeCommandChanged);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082}
83
Kate Stoneb9c1b512016-09-06 20:57:50 +000084void Watchpoint::SetDeclInfo(const std::string &str) { m_decl_str = str; }
Johnny Chende6bd242011-09-16 21:41:42 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086std::string Watchpoint::GetWatchSpec() { return m_watch_spec_str; }
Johnny Chen209bd652012-08-13 21:09:54 +000087
Kate Stoneb9c1b512016-09-06 20:57:50 +000088void Watchpoint::SetWatchSpec(const std::string &str) {
89 m_watch_spec_str = str;
Johnny Chena4d6bc92012-02-25 06:44:30 +000090}
91
Johnny Chenfab7a912012-01-23 23:03:59 +000092// Override default impl of StoppointLocation::IsHardware() since m_is_hardware
93// member field is more accurate.
Kate Stoneb9c1b512016-09-06 20:57:50 +000094bool Watchpoint::IsHardware() const { return m_is_hardware; }
95
96bool Watchpoint::IsWatchVariable() const { return m_is_watch_variable; }
97
98void Watchpoint::SetWatchVariable(bool val) { m_is_watch_variable = val; }
99
100bool Watchpoint::CaptureWatchedValue(const ExecutionContext &exe_ctx) {
101 ConstString watch_name("$__lldb__watch_value");
102 m_old_value_sp = m_new_value_sp;
103 Address watch_address(GetLoadAddress());
104 if (!m_type.IsValid()) {
105 // Don't know how to report new & old values, since we couldn't make a
106 // scalar type for this watchpoint.
107 // This works around an assert in ValueObjectMemory::Create.
108 // FIXME: This should not happen, but if it does in some case we care about,
109 // we can go grab the value raw and print it as unsigned.
110 return false;
111 }
Zachary Turner22a26282016-11-12 18:17:36 +0000112 m_new_value_sp = ValueObjectMemory::Create(
113 exe_ctx.GetBestExecutionContextScope(), watch_name.GetStringRef(),
114 watch_address, m_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 m_new_value_sp = m_new_value_sp->CreateConstantValue(watch_name);
116 return (m_new_value_sp && m_new_value_sp->GetError().Success());
Johnny Chenf04ee932011-09-22 18:04:58 +0000117}
118
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119void Watchpoint::IncrementFalseAlarmsAndReviseHitCount() {
120 ++m_false_alarms;
121 if (m_false_alarms) {
122 if (m_hit_count >= m_false_alarms) {
123 m_hit_count -= m_false_alarms;
124 m_false_alarms = 0;
125 } else {
126 m_false_alarms -= m_hit_count;
127 m_hit_count = 0;
Jim Inghamce0740d2012-10-23 21:09:09 +0000128 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 }
Johnny Chen25c0eb42012-08-14 20:56:37 +0000130}
131
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132// RETURNS - true if we should stop at this breakpoint, false if we
133// should continue.
134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135bool Watchpoint::ShouldStop(StoppointCallbackContext *context) {
136 IncrementHitCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 if (!IsEnabled())
139 return false;
Johnny Chenfd158f42011-09-21 22:47:15 +0000140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142}
143
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144void Watchpoint::GetDescription(Stream *s, lldb::DescriptionLevel level) {
145 DumpWithLevel(s, level);
Johnny Chen887062a2011-09-12 23:38:44 +0000146}
147
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148void Watchpoint::Dump(Stream *s) const {
149 DumpWithLevel(s, lldb::eDescriptionLevelBrief);
Johnny Chende6bd242011-09-16 21:41:42 +0000150}
151
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152// If prefix is nullptr, we display the watch id and ignore the prefix
153// altogether.
154void Watchpoint::DumpSnapshots(Stream *s, const char *prefix) const {
155 if (!prefix) {
156 s->Printf("\nWatchpoint %u hit:", GetID());
157 prefix = "";
158 }
159
160 if (m_old_value_sp) {
161 const char *old_value_cstr = m_old_value_sp->GetValueAsCString();
162 if (old_value_cstr && old_value_cstr[0])
163 s->Printf("\n%sold value: %s", prefix, old_value_cstr);
164 else {
165 const char *old_summary_cstr = m_old_value_sp->GetSummaryAsCString();
166 if (old_summary_cstr && old_summary_cstr[0])
167 s->Printf("\n%sold value: %s", prefix, old_summary_cstr);
Johnny Chen88fc73b2012-08-13 23:27:50 +0000168 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 }
Mohit K. Bhakkad18af8a22015-10-09 15:13:20 +0000170
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 if (m_new_value_sp) {
172 const char *new_value_cstr = m_new_value_sp->GetValueAsCString();
173 if (new_value_cstr && new_value_cstr[0])
174 s->Printf("\n%snew value: %s", prefix, new_value_cstr);
175 else {
176 const char *new_summary_cstr = m_new_value_sp->GetSummaryAsCString();
177 if (new_summary_cstr && new_summary_cstr[0])
178 s->Printf("\n%snew value: %s", prefix, new_summary_cstr);
Johnny Chen209bd652012-08-13 21:09:54 +0000179 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 }
Johnny Chen209bd652012-08-13 21:09:54 +0000181}
182
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183void Watchpoint::DumpWithLevel(Stream *s,
184 lldb::DescriptionLevel description_level) const {
185 if (s == nullptr)
186 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188 assert(description_level >= lldb::eDescriptionLevelBrief &&
189 description_level <= lldb::eDescriptionLevelVerbose);
Johnny Chende6bd242011-09-16 21:41:42 +0000190
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 s->Printf("Watchpoint %u: addr = 0x%8.8" PRIx64
192 " size = %u state = %s type = %s%s",
193 GetID(), GetLoadAddress(), m_byte_size,
194 IsEnabled() ? "enabled" : "disabled", m_watch_read ? "r" : "",
195 m_watch_write ? "w" : "");
Johnny Chende6bd242011-09-16 21:41:42 +0000196
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 if (description_level >= lldb::eDescriptionLevelFull) {
198 if (!m_decl_str.empty())
199 s->Printf("\n declare @ '%s'", m_decl_str.c_str());
200 if (!m_watch_spec_str.empty())
201 s->Printf("\n watchpoint spec = '%s'", m_watch_spec_str.c_str());
Johnny Chen209bd652012-08-13 21:09:54 +0000202
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 // Dump the snapshots we have taken.
204 DumpSnapshots(s, " ");
Johnny Chen209bd652012-08-13 21:09:54 +0000205
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 if (GetConditionText())
207 s->Printf("\n condition = '%s'", GetConditionText());
208 m_options.GetCallbackDescription(s, description_level);
209 }
Johnny Chende6bd242011-09-16 21:41:42 +0000210
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 if (description_level >= lldb::eDescriptionLevelVerbose) {
212 s->Printf("\n hw_index = %i hit_count = %-4u ignore_count = %-4u",
213 GetHardwareIndex(), GetHitCount(), GetIgnoreCount());
214 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215}
216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217bool Watchpoint::IsEnabled() const { return m_enabled; }
218
219// Within StopInfo.cpp, we purposely turn on the ephemeral mode right before
220// temporarily disable the watchpoint
221// in order to perform possible watchpoint actions without triggering further
222// watchpoint events.
223// After the temporary disabled watchpoint is enabled, we then turn off the
224// ephemeral mode.
225
226void Watchpoint::TurnOnEphemeralMode() { m_is_ephemeral = true; }
227
228void Watchpoint::TurnOffEphemeralMode() {
229 m_is_ephemeral = false;
230 // Leaving ephemeral mode, reset the m_disabled_count!
231 m_disabled_count = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232}
233
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234bool Watchpoint::IsDisabledDuringEphemeralMode() {
Jim Ingham209a77d2016-10-25 20:34:32 +0000235 return m_disabled_count > 1 && m_is_ephemeral;
Johnny Chen4fe23022012-08-21 23:17:04 +0000236}
237
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238void Watchpoint::SetEnabled(bool enabled, bool notify) {
239 if (!enabled) {
240 if (!m_is_ephemeral)
241 SetHardwareIndex(LLDB_INVALID_INDEX32);
Johnny Chen16dcf712011-10-17 18:58:00 +0000242 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 ++m_disabled_count;
244
245 // Don't clear the snapshots for now.
246 // Within StopInfo.cpp, we purposely do disable/enable watchpoint while
247 // performing watchpoint actions.
248 }
249 bool changed = enabled != m_enabled;
250 m_enabled = enabled;
251 if (notify && !m_is_ephemeral && changed)
252 SendWatchpointChangedEvent(enabled ? eWatchpointEventTypeEnabled
253 : eWatchpointEventTypeDisabled);
Johnny Chen16dcf712011-10-17 18:58:00 +0000254}
255
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256void Watchpoint::SetWatchpointType(uint32_t type, bool notify) {
257 int old_watch_read = m_watch_read;
258 int old_watch_write = m_watch_write;
259 m_watch_read = (type & LLDB_WATCH_TYPE_READ) != 0;
260 m_watch_write = (type & LLDB_WATCH_TYPE_WRITE) != 0;
261 if (notify &&
262 (old_watch_read != m_watch_read || old_watch_write != m_watch_write))
263 SendWatchpointChangedEvent(eWatchpointEventTypeTypeChanged);
264}
265
266bool Watchpoint::WatchpointRead() const { return m_watch_read != 0; }
267
268bool Watchpoint::WatchpointWrite() const { return m_watch_write != 0; }
269
270uint32_t Watchpoint::GetIgnoreCount() const { return m_ignore_count; }
271
272void Watchpoint::SetIgnoreCount(uint32_t n) {
273 bool changed = m_ignore_count != n;
274 m_ignore_count = n;
275 if (changed)
276 SendWatchpointChangedEvent(eWatchpointEventTypeIgnoreChanged);
277}
278
279bool Watchpoint::InvokeCallback(StoppointCallbackContext *context) {
280 return m_options.InvokeCallback(context, GetID());
281}
282
283void Watchpoint::SetCondition(const char *condition) {
284 if (condition == nullptr || condition[0] == '\0') {
Johnny Chen16dcf712011-10-17 18:58:00 +0000285 if (m_condition_ap.get())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 m_condition_ap.reset();
287 } else {
288 // Pass nullptr for expr_prefix (no translation-unit level definitions).
289 Error error;
290 m_condition_ap.reset(m_target.GetUserExpressionForLanguage(
Zachary Turnerc5d7df92016-11-08 04:52:16 +0000291 condition, llvm::StringRef(), lldb::eLanguageTypeUnknown,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292 UserExpression::eResultTypeAny, EvaluateExpressionOptions(), error));
293 if (error.Fail()) {
294 // FIXME: Log something...
295 m_condition_ap.reset();
Jim Ingham1b5792e2012-12-18 02:03:49 +0000296 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297 }
298 SendWatchpointChangedEvent(eWatchpointEventTypeConditionChanged);
Jim Ingham1b5792e2012-12-18 02:03:49 +0000299}
300
Kate Stoneb9c1b512016-09-06 20:57:50 +0000301const char *Watchpoint::GetConditionText() const {
302 if (m_condition_ap.get())
303 return m_condition_ap->GetUserText();
304 else
Eugene Zelenko16fd7512015-10-30 18:50:12 +0000305 return nullptr;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000306}
307
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308void Watchpoint::SendWatchpointChangedEvent(
309 lldb::WatchpointEventType eventKind) {
310 if (!m_being_created &&
311 GetTarget().EventTypeHasListeners(
312 Target::eBroadcastBitWatchpointChanged)) {
313 WatchpointEventData *data =
314 new Watchpoint::WatchpointEventData(eventKind, shared_from_this());
315 GetTarget().BroadcastEvent(Target::eBroadcastBitWatchpointChanged, data);
316 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000317}
318
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319void Watchpoint::SendWatchpointChangedEvent(WatchpointEventData *data) {
320 if (data == nullptr)
321 return;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000322
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323 if (!m_being_created &&
324 GetTarget().EventTypeHasListeners(Target::eBroadcastBitWatchpointChanged))
325 GetTarget().BroadcastEvent(Target::eBroadcastBitWatchpointChanged, data);
326 else
327 delete data;
328}
Jim Ingham1b5792e2012-12-18 02:03:49 +0000329
Kate Stoneb9c1b512016-09-06 20:57:50 +0000330Watchpoint::WatchpointEventData::WatchpointEventData(
331 WatchpointEventType sub_type, const WatchpointSP &new_watchpoint_sp)
332 : EventData(), m_watchpoint_event(sub_type),
333 m_new_watchpoint_sp(new_watchpoint_sp) {}
334
335Watchpoint::WatchpointEventData::~WatchpointEventData() = default;
336
337const ConstString &Watchpoint::WatchpointEventData::GetFlavorString() {
338 static ConstString g_flavor("Watchpoint::WatchpointEventData");
339 return g_flavor;
340}
341
342const ConstString &Watchpoint::WatchpointEventData::GetFlavor() const {
343 return WatchpointEventData::GetFlavorString();
344}
345
346WatchpointSP &Watchpoint::WatchpointEventData::GetWatchpoint() {
347 return m_new_watchpoint_sp;
348}
349
350WatchpointEventType
351Watchpoint::WatchpointEventData::GetWatchpointEventType() const {
352 return m_watchpoint_event;
353}
354
355void Watchpoint::WatchpointEventData::Dump(Stream *s) const {}
356
357const Watchpoint::WatchpointEventData *
358Watchpoint::WatchpointEventData::GetEventDataFromEvent(const Event *event) {
359 if (event) {
360 const EventData *event_data = event->GetData();
361 if (event_data &&
362 event_data->GetFlavor() == WatchpointEventData::GetFlavorString())
363 return static_cast<const WatchpointEventData *>(event->GetData());
364 }
365 return nullptr;
366}
367
368WatchpointEventType
369Watchpoint::WatchpointEventData::GetWatchpointEventTypeFromEvent(
370 const EventSP &event_sp) {
371 const WatchpointEventData *data = GetEventDataFromEvent(event_sp.get());
372
373 if (data == nullptr)
374 return eWatchpointEventTypeInvalidType;
375 else
376 return data->GetWatchpointEventType();
377}
378
379WatchpointSP Watchpoint::WatchpointEventData::GetWatchpointFromEvent(
380 const EventSP &event_sp) {
381 WatchpointSP wp_sp;
382
383 const WatchpointEventData *data = GetEventDataFromEvent(event_sp.get());
384 if (data)
385 wp_sp = data->m_new_watchpoint_sp;
386
387 return wp_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000388}