180814287SRaphael Isemann //===-- Watchpoint.cpp ----------------------------------------------------===//
201a67860SJohnny Chen //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
601a67860SJohnny Chen //
701a67860SJohnny Chen //===----------------------------------------------------------------------===//
801a67860SJohnny Chen 
916fd7511SEugene Zelenko #include "lldb/Breakpoint/Watchpoint.h"
1016fd7511SEugene Zelenko 
1116dcf718SJohnny Chen #include "lldb/Breakpoint/StoppointCallbackContext.h"
12a7dfb665SJim Ingham #include "lldb/Core/Value.h"
13a7dfb665SJim Ingham #include "lldb/Core/ValueObject.h"
14a7dfb665SJim Ingham #include "lldb/Core/ValueObjectMemory.h"
15b9c1b51eSKate Stone #include "lldb/Expression/UserExpression.h"
1679976b37SAlex Langford #include "lldb/Symbol/TypeSystem.h"
1716dcf718SJohnny Chen #include "lldb/Target/Process.h"
1816dcf718SJohnny Chen #include "lldb/Target/Target.h"
1916dcf718SJohnny Chen #include "lldb/Target/ThreadSpec.h"
20c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
210e252e38SAlex Langford #include "lldb/Utility/Log.h"
22bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
2301a67860SJohnny Chen 
2401a67860SJohnny Chen using namespace lldb;
2501a67860SJohnny Chen using namespace lldb_private;
2601a67860SJohnny Chen 
Watchpoint(Target & target,lldb::addr_t addr,uint32_t size,const CompilerType * type,bool hardware)27b9c1b51eSKate Stone Watchpoint::Watchpoint(Target &target, lldb::addr_t addr, uint32_t size,
28b9c1b51eSKate Stone                        const CompilerType *type, bool hardware)
29da0bba5cSTatyana Krasnukha     : StoppointSite(0, addr, size, hardware), m_target(target),
30b9c1b51eSKate Stone       m_enabled(false), m_is_hardware(hardware), m_is_watch_variable(false),
31b9c1b51eSKate Stone       m_is_ephemeral(false), m_disabled_count(0), m_watch_read(0),
32b9c1b51eSKate Stone       m_watch_write(0), m_watch_was_read(0), m_watch_was_written(0),
33*ee4b6cf5SKazu Hirata       m_ignore_count(0), m_false_alarms(0), m_being_created(true) {
340e252e38SAlex Langford 
35a7dfb665SJim Ingham   if (type && type->IsValid())
36a7dfb665SJim Ingham     m_type = *type;
37b9c1b51eSKate Stone   else {
38b9c1b51eSKate Stone     // If we don't have a known type, then we force it to unsigned int of the
39b9c1b51eSKate Stone     // right size.
400e252e38SAlex Langford     auto type_system_or_err =
410e252e38SAlex Langford         target.GetScratchTypeSystemForLanguage(eLanguageTypeC);
420e252e38SAlex Langford     if (auto err = type_system_or_err.takeError()) {
43a007a6d8SPavel Labath       LLDB_LOG_ERROR(GetLog(LLDBLog::Watchpoints), std::move(err),
44a007a6d8SPavel Labath                      "Failed to set type.");
450e252e38SAlex Langford     } else {
460e252e38SAlex Langford       m_type = type_system_or_err->GetBuiltinTypeForEncodingAndBitSize(
470e252e38SAlex Langford           eEncodingUint, 8 * size);
480e252e38SAlex Langford     }
49a7dfb665SJim Ingham   }
50a7dfb665SJim Ingham 
51a7dfb665SJim Ingham   // Set the initial value of the watched variable:
52b9c1b51eSKate Stone   if (m_target.GetProcessSP()) {
53a7dfb665SJim Ingham     ExecutionContext exe_ctx;
54a7dfb665SJim Ingham     m_target.GetProcessSP()->CalculateExecutionContext(exe_ctx);
55a7dfb665SJim Ingham     CaptureWatchedValue(exe_ctx);
56a7dfb665SJim Ingham   }
571b5792e5SJim Ingham   m_being_created = false;
5801a67860SJohnny Chen }
5901a67860SJohnny Chen 
6016fd7511SEugene Zelenko Watchpoint::~Watchpoint() = default;
6101a67860SJohnny Chen 
62e9a5627eSJohnny Chen // This function is used when "baton" doesn't need to be freed
SetCallback(WatchpointHitCallback callback,void * baton,bool is_synchronous)63b9c1b51eSKate Stone void Watchpoint::SetCallback(WatchpointHitCallback callback, void *baton,
64b9c1b51eSKate Stone                              bool is_synchronous) {
6505097246SAdrian Prantl   // The default "Baton" class will keep a copy of "baton" and won't free or
6605097246SAdrian Prantl   // delete it when it goes goes out of scope.
674e4fbe82SZachary Turner   m_options.SetCallback(callback, std::make_shared<UntypedBaton>(baton),
684e4fbe82SZachary Turner                         is_synchronous);
69e9a5627eSJohnny Chen 
701b5792e5SJim Ingham   SendWatchpointChangedEvent(eWatchpointEventTypeCommandChanged);
71e9a5627eSJohnny Chen }
72e9a5627eSJohnny Chen 
73e9a5627eSJohnny Chen // This function is used when a baton needs to be freed and therefore is
74e9a5627eSJohnny Chen // contained in a "Baton" subclass.
SetCallback(WatchpointHitCallback callback,const BatonSP & callback_baton_sp,bool is_synchronous)75b9c1b51eSKate Stone void Watchpoint::SetCallback(WatchpointHitCallback callback,
76b9c1b51eSKate Stone                              const BatonSP &callback_baton_sp,
77b9c1b51eSKate Stone                              bool is_synchronous) {
78e9a5627eSJohnny Chen   m_options.SetCallback(callback, callback_baton_sp, is_synchronous);
791b5792e5SJim Ingham   SendWatchpointChangedEvent(eWatchpointEventTypeCommandChanged);
80e9a5627eSJohnny Chen }
81e9a5627eSJohnny Chen 
ClearCallback()82b9c1b51eSKate Stone void Watchpoint::ClearCallback() {
83e9a5627eSJohnny Chen   m_options.ClearCallback();
841b5792e5SJim Ingham   SendWatchpointChangedEvent(eWatchpointEventTypeCommandChanged);
8501a67860SJohnny Chen }
8601a67860SJohnny Chen 
SetDeclInfo(const std::string & str)87b9c1b51eSKate Stone void Watchpoint::SetDeclInfo(const std::string &str) { m_decl_str = str; }
8801a67860SJohnny Chen 
GetWatchSpec()89b9c1b51eSKate Stone std::string Watchpoint::GetWatchSpec() { return m_watch_spec_str; }
90209bd65eSJohnny Chen 
SetWatchSpec(const std::string & str)91b9c1b51eSKate Stone void Watchpoint::SetWatchSpec(const std::string &str) {
92a4d6bc9fSJohnny Chen   m_watch_spec_str = str;
93a4d6bc9fSJohnny Chen }
94a4d6bc9fSJohnny Chen 
IsHardware() const95ebaa8b1cSTatyana Krasnukha bool Watchpoint::IsHardware() const {
96ebaa8b1cSTatyana Krasnukha   lldbassert(m_is_hardware || !HardwareRequired());
97ebaa8b1cSTatyana Krasnukha   return m_is_hardware;
98ebaa8b1cSTatyana Krasnukha }
9901a67860SJohnny Chen 
IsWatchVariable() const100b9c1b51eSKate Stone bool Watchpoint::IsWatchVariable() const { return m_is_watch_variable; }
101209bd65eSJohnny Chen 
SetWatchVariable(bool val)102b9c1b51eSKate Stone void Watchpoint::SetWatchVariable(bool val) { m_is_watch_variable = val; }
103209bd65eSJohnny Chen 
CaptureWatchedValue(const ExecutionContext & exe_ctx)104b9c1b51eSKate Stone bool Watchpoint::CaptureWatchedValue(const ExecutionContext &exe_ctx) {
105a7dfb665SJim Ingham   ConstString watch_name("$__lldb__watch_value");
106a7dfb665SJim Ingham   m_old_value_sp = m_new_value_sp;
107a7dfb665SJim Ingham   Address watch_address(GetLoadAddress());
108b9c1b51eSKate Stone   if (!m_type.IsValid()) {
109b9c1b51eSKate Stone     // Don't know how to report new & old values, since we couldn't make a
11005097246SAdrian Prantl     // scalar type for this watchpoint. This works around an assert in
11105097246SAdrian Prantl     // ValueObjectMemory::Create.
112ce0740d8SJim Ingham     // FIXME: This should not happen, but if it does in some case we care about,
113ce0740d8SJim Ingham     // we can go grab the value raw and print it as unsigned.
114ce0740d8SJim Ingham     return false;
115ce0740d8SJim Ingham   }
11622a2628fSZachary Turner   m_new_value_sp = ValueObjectMemory::Create(
11722a2628fSZachary Turner       exe_ctx.GetBestExecutionContextScope(), watch_name.GetStringRef(),
11822a2628fSZachary Turner       watch_address, m_type);
119a7dfb665SJim Ingham   m_new_value_sp = m_new_value_sp->CreateConstantValue(watch_name);
12016fd7511SEugene Zelenko   return (m_new_value_sp && m_new_value_sp->GetError().Success());
121a7dfb665SJim Ingham }
122a7dfb665SJim Ingham 
IncrementFalseAlarmsAndReviseHitCount()123b9c1b51eSKate Stone void Watchpoint::IncrementFalseAlarmsAndReviseHitCount() {
12425c0eb4aSJohnny Chen   ++m_false_alarms;
125b9c1b51eSKate Stone   if (m_false_alarms) {
126da0bba5cSTatyana Krasnukha     if (m_hit_counter.GetValue() >= m_false_alarms) {
127da0bba5cSTatyana Krasnukha       m_hit_counter.Decrement(m_false_alarms);
12825c0eb4aSJohnny Chen       m_false_alarms = 0;
129b9c1b51eSKate Stone     } else {
130da0bba5cSTatyana Krasnukha       m_false_alarms -= m_hit_counter.GetValue();
131da0bba5cSTatyana Krasnukha       m_hit_counter.Reset();
13225c0eb4aSJohnny Chen     }
13325c0eb4aSJohnny Chen   }
13425c0eb4aSJohnny Chen }
13525c0eb4aSJohnny Chen 
13601a67860SJohnny Chen // RETURNS - true if we should stop at this breakpoint, false if we
13701a67860SJohnny Chen // should continue.
13801a67860SJohnny Chen 
ShouldStop(StoppointCallbackContext * context)139b9c1b51eSKate Stone bool Watchpoint::ShouldStop(StoppointCallbackContext *context) {
140da0bba5cSTatyana Krasnukha   m_hit_counter.Increment();
14101a67860SJohnny Chen 
142a6682a41SJonas Devlieghere   return IsEnabled();
14301a67860SJohnny Chen }
14401a67860SJohnny Chen 
GetDescription(Stream * s,lldb::DescriptionLevel level)145b9c1b51eSKate Stone void Watchpoint::GetDescription(Stream *s, lldb::DescriptionLevel level) {
14601a67860SJohnny Chen   DumpWithLevel(s, level);
14701a67860SJohnny Chen }
14801a67860SJohnny Chen 
Dump(Stream * s) const149b9c1b51eSKate Stone void Watchpoint::Dump(Stream *s) const {
15001a67860SJohnny Chen   DumpWithLevel(s, lldb::eDescriptionLevelBrief);
15101a67860SJohnny Chen }
15201a67860SJohnny Chen 
153b9c1b51eSKate Stone // If prefix is nullptr, we display the watch id and ignore the prefix
154b9c1b51eSKate Stone // altogether.
DumpSnapshots(Stream * s,const char * prefix) const155b9c1b51eSKate Stone void Watchpoint::DumpSnapshots(Stream *s, const char *prefix) const {
156b9c1b51eSKate Stone   if (!prefix) {
15788fc73b8SJohnny Chen     s->Printf("\nWatchpoint %u hit:", GetID());
15888fc73b8SJohnny Chen     prefix = "";
15988fc73b8SJohnny Chen   }
16088fc73b8SJohnny Chen 
161b9c1b51eSKate Stone   if (m_old_value_sp) {
16218af8a20SMohit K. Bhakkad     const char *old_value_cstr = m_old_value_sp->GetValueAsCString();
16318af8a20SMohit K. Bhakkad     if (old_value_cstr && old_value_cstr[0])
16418af8a20SMohit K. Bhakkad       s->Printf("\n%sold value: %s", prefix, old_value_cstr);
165b9c1b51eSKate Stone     else {
16618af8a20SMohit K. Bhakkad       const char *old_summary_cstr = m_old_value_sp->GetSummaryAsCString();
16718af8a20SMohit K. Bhakkad       if (old_summary_cstr && old_summary_cstr[0])
16818af8a20SMohit K. Bhakkad         s->Printf("\n%sold value: %s", prefix, old_summary_cstr);
169209bd65eSJohnny Chen     }
17018af8a20SMohit K. Bhakkad   }
17118af8a20SMohit K. Bhakkad 
172b9c1b51eSKate Stone   if (m_new_value_sp) {
17318af8a20SMohit K. Bhakkad     const char *new_value_cstr = m_new_value_sp->GetValueAsCString();
17418af8a20SMohit K. Bhakkad     if (new_value_cstr && new_value_cstr[0])
17518af8a20SMohit K. Bhakkad       s->Printf("\n%snew value: %s", prefix, new_value_cstr);
176b9c1b51eSKate Stone     else {
17718af8a20SMohit K. Bhakkad       const char *new_summary_cstr = m_new_value_sp->GetSummaryAsCString();
17818af8a20SMohit K. Bhakkad       if (new_summary_cstr && new_summary_cstr[0])
17918af8a20SMohit K. Bhakkad         s->Printf("\n%snew value: %s", prefix, new_summary_cstr);
18018af8a20SMohit K. Bhakkad     }
181209bd65eSJohnny Chen   }
182209bd65eSJohnny Chen }
183209bd65eSJohnny Chen 
DumpWithLevel(Stream * s,lldb::DescriptionLevel description_level) const184b9c1b51eSKate Stone void Watchpoint::DumpWithLevel(Stream *s,
185b9c1b51eSKate Stone                                lldb::DescriptionLevel description_level) const {
18616fd7511SEugene Zelenko   if (s == nullptr)
18701a67860SJohnny Chen     return;
18801a67860SJohnny Chen 
18901a67860SJohnny Chen   assert(description_level >= lldb::eDescriptionLevelBrief &&
19001a67860SJohnny Chen          description_level <= lldb::eDescriptionLevelVerbose);
19101a67860SJohnny Chen 
192b9c1b51eSKate Stone   s->Printf("Watchpoint %u: addr = 0x%8.8" PRIx64
193b9c1b51eSKate Stone             " size = %u state = %s type = %s%s",
194b9c1b51eSKate Stone             GetID(), GetLoadAddress(), m_byte_size,
195b9c1b51eSKate Stone             IsEnabled() ? "enabled" : "disabled", m_watch_read ? "r" : "",
19601a67860SJohnny Chen             m_watch_write ? "w" : "");
19701a67860SJohnny Chen 
19816dcf718SJohnny Chen   if (description_level >= lldb::eDescriptionLevelFull) {
199dedb67abSJohnny Chen     if (!m_decl_str.empty())
20001a67860SJohnny Chen       s->Printf("\n    declare @ '%s'", m_decl_str.c_str());
201a4d6bc9fSJohnny Chen     if (!m_watch_spec_str.empty())
202209bd65eSJohnny Chen       s->Printf("\n    watchpoint spec = '%s'", m_watch_spec_str.c_str());
203209bd65eSJohnny Chen 
204209bd65eSJohnny Chen     // Dump the snapshots we have taken.
20588fc73b8SJohnny Chen     DumpSnapshots(s, "    ");
206209bd65eSJohnny Chen 
20716dcf718SJohnny Chen     if (GetConditionText())
20816dcf718SJohnny Chen       s->Printf("\n    condition = '%s'", GetConditionText());
209e9a5627eSJohnny Chen     m_options.GetCallbackDescription(s, description_level);
21016dcf718SJohnny Chen   }
21101a67860SJohnny Chen 
212b9c1b51eSKate Stone   if (description_level >= lldb::eDescriptionLevelVerbose) {
21301a67860SJohnny Chen     s->Printf("\n    hw_index = %i  hit_count = %-4u  ignore_count = %-4u",
214b9c1b51eSKate Stone               GetHardwareIndex(), GetHitCount(), GetIgnoreCount());
21501a67860SJohnny Chen   }
2167e1f45f9SJason Molenda }
21701a67860SJohnny Chen 
IsEnabled() const218b9c1b51eSKate Stone bool Watchpoint::IsEnabled() const { return m_enabled; }
21901a67860SJohnny Chen 
220b9c1b51eSKate Stone // Within StopInfo.cpp, we purposely turn on the ephemeral mode right before
22105097246SAdrian Prantl // temporarily disable the watchpoint in order to perform possible watchpoint
22205097246SAdrian Prantl // actions without triggering further watchpoint events. After the temporary
22305097246SAdrian Prantl // disabled watchpoint is enabled, we then turn off the ephemeral mode.
2244fe2302aSJohnny Chen 
TurnOnEphemeralMode()225b9c1b51eSKate Stone void Watchpoint::TurnOnEphemeralMode() { m_is_ephemeral = true; }
2264fe2302aSJohnny Chen 
TurnOffEphemeralMode()227b9c1b51eSKate Stone void Watchpoint::TurnOffEphemeralMode() {
2284fe2302aSJohnny Chen   m_is_ephemeral = false;
229892943f9SJohnny Chen   // Leaving ephemeral mode, reset the m_disabled_count!
230892943f9SJohnny Chen   m_disabled_count = 0;
231892943f9SJohnny Chen }
232892943f9SJohnny Chen 
IsDisabledDuringEphemeralMode()233b9c1b51eSKate Stone bool Watchpoint::IsDisabledDuringEphemeralMode() {
234209a77d8SJim Ingham   return m_disabled_count > 1 && m_is_ephemeral;
2354fe2302aSJohnny Chen }
2364fe2302aSJohnny Chen 
SetEnabled(bool enabled,bool notify)237b9c1b51eSKate Stone void Watchpoint::SetEnabled(bool enabled, bool notify) {
238b9c1b51eSKate Stone   if (!enabled) {
2394fe2302aSJohnny Chen     if (!m_is_ephemeral)
24001a67860SJohnny Chen       SetHardwareIndex(LLDB_INVALID_INDEX32);
241892943f9SJohnny Chen     else
242892943f9SJohnny Chen       ++m_disabled_count;
243892943f9SJohnny Chen 
2440f7ad8d9SJohnny Chen     // Don't clear the snapshots for now.
245b9c1b51eSKate Stone     // Within StopInfo.cpp, we purposely do disable/enable watchpoint while
246b9c1b51eSKate Stone     // performing watchpoint actions.
24788fc73b8SJohnny Chen   }
2481b5792e5SJim Ingham   bool changed = enabled != m_enabled;
24901a67860SJohnny Chen   m_enabled = enabled;
2501b5792e5SJim Ingham   if (notify && !m_is_ephemeral && changed)
251b9c1b51eSKate Stone     SendWatchpointChangedEvent(enabled ? eWatchpointEventTypeEnabled
252b9c1b51eSKate Stone                                        : eWatchpointEventTypeDisabled);
25301a67860SJohnny Chen }
25401a67860SJohnny Chen 
SetWatchpointType(uint32_t type,bool notify)255b9c1b51eSKate Stone void Watchpoint::SetWatchpointType(uint32_t type, bool notify) {
2561b5792e5SJim Ingham   int old_watch_read = m_watch_read;
2571b5792e5SJim Ingham   int old_watch_write = m_watch_write;
25801a67860SJohnny Chen   m_watch_read = (type & LLDB_WATCH_TYPE_READ) != 0;
25901a67860SJohnny Chen   m_watch_write = (type & LLDB_WATCH_TYPE_WRITE) != 0;
260b9c1b51eSKate Stone   if (notify &&
261b9c1b51eSKate Stone       (old_watch_read != m_watch_read || old_watch_write != m_watch_write))
2621b5792e5SJim Ingham     SendWatchpointChangedEvent(eWatchpointEventTypeTypeChanged);
26301a67860SJohnny Chen }
26401a67860SJohnny Chen 
WatchpointRead() const265b9c1b51eSKate Stone bool Watchpoint::WatchpointRead() const { return m_watch_read != 0; }
26616fd7511SEugene Zelenko 
WatchpointWrite() const267b9c1b51eSKate Stone bool Watchpoint::WatchpointWrite() const { return m_watch_write != 0; }
26816fd7511SEugene Zelenko 
GetIgnoreCount() const269b9c1b51eSKate Stone uint32_t Watchpoint::GetIgnoreCount() const { return m_ignore_count; }
27001a67860SJohnny Chen 
SetIgnoreCount(uint32_t n)271b9c1b51eSKate Stone void Watchpoint::SetIgnoreCount(uint32_t n) {
2721b5792e5SJim Ingham   bool changed = m_ignore_count != n;
27301a67860SJohnny Chen   m_ignore_count = n;
2741b5792e5SJim Ingham   if (changed)
2751b5792e5SJim Ingham     SendWatchpointChangedEvent(eWatchpointEventTypeIgnoreChanged);
27601a67860SJohnny Chen }
27716dcf718SJohnny Chen 
InvokeCallback(StoppointCallbackContext * context)278b9c1b51eSKate Stone bool Watchpoint::InvokeCallback(StoppointCallbackContext *context) {
279e9a5627eSJohnny Chen   return m_options.InvokeCallback(context, GetID());
28016dcf718SJohnny Chen }
28116dcf718SJohnny Chen 
SetCondition(const char * condition)282b9c1b51eSKate Stone void Watchpoint::SetCondition(const char *condition) {
283b9c1b51eSKate Stone   if (condition == nullptr || condition[0] == '\0') {
284d5b44036SJonas Devlieghere     if (m_condition_up)
285d5b44036SJonas Devlieghere       m_condition_up.reset();
286b9c1b51eSKate Stone   } else {
28716fd7511SEugene Zelenko     // Pass nullptr for expr_prefix (no translation-unit level definitions).
28897206d57SZachary Turner     Status error;
289d5b44036SJonas Devlieghere     m_condition_up.reset(m_target.GetUserExpressionForLanguage(
290c5d7df90SZachary Turner         condition, llvm::StringRef(), lldb::eLanguageTypeUnknown,
291d5b44036SJonas Devlieghere         UserExpression::eResultTypeAny, EvaluateExpressionOptions(), nullptr,
292d5b44036SJonas Devlieghere         error));
293b9c1b51eSKate Stone     if (error.Fail()) {
294151c032cSJim Ingham       // FIXME: Log something...
295d5b44036SJonas Devlieghere       m_condition_up.reset();
296151c032cSJim Ingham     }
29716dcf718SJohnny Chen   }
2981b5792e5SJim Ingham   SendWatchpointChangedEvent(eWatchpointEventTypeConditionChanged);
29916dcf718SJohnny Chen }
30016dcf718SJohnny Chen 
GetConditionText() const301b9c1b51eSKate Stone const char *Watchpoint::GetConditionText() const {
302d5b44036SJonas Devlieghere   if (m_condition_up)
303d5b44036SJonas Devlieghere     return m_condition_up->GetUserText();
30416dcf718SJohnny Chen   else
30516fd7511SEugene Zelenko     return nullptr;
30616dcf718SJohnny Chen }
30716dcf718SJohnny Chen 
SendWatchpointChangedEvent(lldb::WatchpointEventType eventKind)308b9c1b51eSKate Stone void Watchpoint::SendWatchpointChangedEvent(
309b9c1b51eSKate Stone     lldb::WatchpointEventType eventKind) {
310b9c1b51eSKate Stone   if (!m_being_created &&
311b9c1b51eSKate Stone       GetTarget().EventTypeHasListeners(
312b9c1b51eSKate Stone           Target::eBroadcastBitWatchpointChanged)) {
313b9c1b51eSKate Stone     WatchpointEventData *data =
314b9c1b51eSKate Stone         new Watchpoint::WatchpointEventData(eventKind, shared_from_this());
3151b5792e5SJim Ingham     GetTarget().BroadcastEvent(Target::eBroadcastBitWatchpointChanged, data);
3161b5792e5SJim Ingham   }
3171b5792e5SJim Ingham }
3181b5792e5SJim Ingham 
SendWatchpointChangedEvent(WatchpointEventData * data)319b9c1b51eSKate Stone void Watchpoint::SendWatchpointChangedEvent(WatchpointEventData *data) {
32016fd7511SEugene Zelenko   if (data == nullptr)
3211b5792e5SJim Ingham     return;
3221b5792e5SJim Ingham 
323b9c1b51eSKate Stone   if (!m_being_created &&
324b9c1b51eSKate Stone       GetTarget().EventTypeHasListeners(Target::eBroadcastBitWatchpointChanged))
3251b5792e5SJim Ingham     GetTarget().BroadcastEvent(Target::eBroadcastBitWatchpointChanged, data);
3261b5792e5SJim Ingham   else
3271b5792e5SJim Ingham     delete data;
3281b5792e5SJim Ingham }
3291b5792e5SJim Ingham 
WatchpointEventData(WatchpointEventType sub_type,const WatchpointSP & new_watchpoint_sp)330b9c1b51eSKate Stone Watchpoint::WatchpointEventData::WatchpointEventData(
331b9c1b51eSKate Stone     WatchpointEventType sub_type, const WatchpointSP &new_watchpoint_sp)
332*ee4b6cf5SKazu Hirata     : m_watchpoint_event(sub_type), m_new_watchpoint_sp(new_watchpoint_sp) {}
3331b5792e5SJim Ingham 
33416fd7511SEugene Zelenko Watchpoint::WatchpointEventData::~WatchpointEventData() = default;
3351b5792e5SJim Ingham 
GetFlavorString()3360e4c4821SAdrian Prantl ConstString Watchpoint::WatchpointEventData::GetFlavorString() {
3371b5792e5SJim Ingham   static ConstString g_flavor("Watchpoint::WatchpointEventData");
3381b5792e5SJim Ingham   return g_flavor;
3391b5792e5SJim Ingham }
3401b5792e5SJim Ingham 
GetFlavor() const3410e4c4821SAdrian Prantl ConstString Watchpoint::WatchpointEventData::GetFlavor() const {
3421b5792e5SJim Ingham   return WatchpointEventData::GetFlavorString();
3431b5792e5SJim Ingham }
3441b5792e5SJim Ingham 
GetWatchpoint()345b9c1b51eSKate Stone WatchpointSP &Watchpoint::WatchpointEventData::GetWatchpoint() {
3461b5792e5SJim Ingham   return m_new_watchpoint_sp;
3471b5792e5SJim Ingham }
3481b5792e5SJim Ingham 
3491b5792e5SJim Ingham WatchpointEventType
GetWatchpointEventType() const350b9c1b51eSKate Stone Watchpoint::WatchpointEventData::GetWatchpointEventType() const {
3511b5792e5SJim Ingham   return m_watchpoint_event;
3521b5792e5SJim Ingham }
3531b5792e5SJim Ingham 
Dump(Stream * s) const354b9c1b51eSKate Stone void Watchpoint::WatchpointEventData::Dump(Stream *s) const {}
3551b5792e5SJim Ingham 
3561b5792e5SJim Ingham const Watchpoint::WatchpointEventData *
GetEventDataFromEvent(const Event * event)357b9c1b51eSKate Stone Watchpoint::WatchpointEventData::GetEventDataFromEvent(const Event *event) {
358b9c1b51eSKate Stone   if (event) {
3591b5792e5SJim Ingham     const EventData *event_data = event->GetData();
360b9c1b51eSKate Stone     if (event_data &&
361b9c1b51eSKate Stone         event_data->GetFlavor() == WatchpointEventData::GetFlavorString())
3621b5792e5SJim Ingham       return static_cast<const WatchpointEventData *>(event->GetData());
3631b5792e5SJim Ingham   }
36416fd7511SEugene Zelenko   return nullptr;
3651b5792e5SJim Ingham }
3661b5792e5SJim Ingham 
3671b5792e5SJim Ingham WatchpointEventType
GetWatchpointEventTypeFromEvent(const EventSP & event_sp)368b9c1b51eSKate Stone Watchpoint::WatchpointEventData::GetWatchpointEventTypeFromEvent(
369b9c1b51eSKate Stone     const EventSP &event_sp) {
3701b5792e5SJim Ingham   const WatchpointEventData *data = GetEventDataFromEvent(event_sp.get());
3711b5792e5SJim Ingham 
37216fd7511SEugene Zelenko   if (data == nullptr)
3731b5792e5SJim Ingham     return eWatchpointEventTypeInvalidType;
3741b5792e5SJim Ingham   else
3751b5792e5SJim Ingham     return data->GetWatchpointEventType();
3761b5792e5SJim Ingham }
3771b5792e5SJim Ingham 
GetWatchpointFromEvent(const EventSP & event_sp)378b9c1b51eSKate Stone WatchpointSP Watchpoint::WatchpointEventData::GetWatchpointFromEvent(
379b9c1b51eSKate Stone     const EventSP &event_sp) {
3801b5792e5SJim Ingham   WatchpointSP wp_sp;
3811b5792e5SJim Ingham 
3821b5792e5SJim Ingham   const WatchpointEventData *data = GetEventDataFromEvent(event_sp.get());
3831b5792e5SJim Ingham   if (data)
3841b5792e5SJim Ingham     wp_sp = data->m_new_watchpoint_sp;
3851b5792e5SJim Ingham 
3861b5792e5SJim Ingham   return wp_sp;
3871b5792e5SJim Ingham }
388