1 //===-- Watchpoint.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Breakpoint/Watchpoint.h"
14 // Other libraries and framework includes
16 #include "lldb/Breakpoint/StoppointCallbackContext.h"
17 #include "lldb/Core/Stream.h"
18 #include "lldb/Core/Value.h"
19 #include "lldb/Core/ValueObject.h"
20 #include "lldb/Core/ValueObjectMemory.h"
21 #include "lldb/Symbol/ClangASTContext.h"
22 #include "lldb/Target/Process.h"
23 #include "lldb/Target/Target.h"
24 #include "lldb/Target/ThreadSpec.h"
25 #include "lldb/Expression/ClangUserExpression.h"
28 using namespace lldb_private;
30 Watchpoint::Watchpoint (Target& target, lldb::addr_t addr, uint32_t size, const ClangASTType *type, bool hardware) :
31 StoppointLocation (0, addr, size, hardware),
34 m_is_hardware(hardware),
35 m_is_watch_variable(false),
36 m_is_ephemeral(false),
41 m_watch_was_written(0),
51 if (type && type->IsValid())
55 // If we don't have a known type, then we force it to unsigned int of the right size.
56 ClangASTContext *ast_context = target.GetScratchClangASTContext();
57 m_type = ast_context->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 8 * size);
60 // Set the initial value of the watched variable:
61 if (m_target.GetProcessSP())
63 ExecutionContext exe_ctx;
64 m_target.GetProcessSP()->CalculateExecutionContext(exe_ctx);
65 CaptureWatchedValue (exe_ctx);
67 m_being_created = false;
70 Watchpoint::~Watchpoint()
74 // This function is used when "baton" doesn't need to be freed
76 Watchpoint::SetCallback (WatchpointHitCallback callback, void *baton, bool is_synchronous)
78 // The default "Baton" class will keep a copy of "baton" and won't free
79 // or delete it when it goes goes out of scope.
80 m_options.SetCallback(callback, BatonSP (new Baton(baton)), is_synchronous);
82 SendWatchpointChangedEvent (eWatchpointEventTypeCommandChanged);
85 // This function is used when a baton needs to be freed and therefore is
86 // contained in a "Baton" subclass.
88 Watchpoint::SetCallback (WatchpointHitCallback callback, const BatonSP &callback_baton_sp, bool is_synchronous)
90 m_options.SetCallback(callback, callback_baton_sp, is_synchronous);
91 SendWatchpointChangedEvent (eWatchpointEventTypeCommandChanged);
95 Watchpoint::ClearCallback ()
97 m_options.ClearCallback ();
98 SendWatchpointChangedEvent (eWatchpointEventTypeCommandChanged);
102 Watchpoint::SetDeclInfo (const std::string &str)
109 Watchpoint::GetWatchSpec()
111 return m_watch_spec_str;
115 Watchpoint::SetWatchSpec (const std::string &str)
117 m_watch_spec_str = str;
121 // Override default impl of StoppointLocation::IsHardware() since m_is_hardware
122 // member field is more accurate.
124 Watchpoint::IsHardware () const
126 return m_is_hardware;
130 Watchpoint::IsWatchVariable() const
132 return m_is_watch_variable;
136 Watchpoint::SetWatchVariable(bool val)
138 m_is_watch_variable = val;
142 Watchpoint::CaptureWatchedValue (const ExecutionContext &exe_ctx)
144 ConstString watch_name("$__lldb__watch_value");
145 m_old_value_sp = m_new_value_sp;
146 Address watch_address(GetLoadAddress());
147 if (!m_type.IsValid())
149 // Don't know how to report new & old values, since we couldn't make a scalar type for this watchpoint.
150 // This works around an assert in ValueObjectMemory::Create.
151 // FIXME: This should not happen, but if it does in some case we care about,
152 // we can go grab the value raw and print it as unsigned.
155 m_new_value_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(), watch_name.AsCString(), watch_address, m_type);
156 m_new_value_sp = m_new_value_sp->CreateConstantValue(watch_name);
157 if (m_new_value_sp && m_new_value_sp->GetError().Success())
164 Watchpoint::IncrementFalseAlarmsAndReviseHitCount()
169 if (m_hit_count >= m_false_alarms)
171 m_hit_count -= m_false_alarms;
176 m_false_alarms -= m_hit_count;
182 // RETURNS - true if we should stop at this breakpoint, false if we
186 Watchpoint::ShouldStop (StoppointCallbackContext *context)
193 if (GetHitCount() <= GetIgnoreCount())
200 Watchpoint::GetDescription (Stream *s, lldb::DescriptionLevel level)
202 DumpWithLevel(s, level);
207 Watchpoint::Dump(Stream *s) const
209 DumpWithLevel(s, lldb::eDescriptionLevelBrief);
212 // If prefix is NULL, we display the watch id and ignore the prefix altogether.
214 Watchpoint::DumpSnapshots(Stream *s, const char *prefix) const
218 s->Printf("\nWatchpoint %u hit:", GetID());
224 s->Printf("\n%sold value: %s", prefix, m_old_value_sp->GetValueAsCString());
228 s->Printf("\n%snew value: %s", prefix, m_new_value_sp->GetValueAsCString());
233 Watchpoint::DumpWithLevel(Stream *s, lldb::DescriptionLevel description_level) const
238 assert(description_level >= lldb::eDescriptionLevelBrief &&
239 description_level <= lldb::eDescriptionLevelVerbose);
241 s->Printf("Watchpoint %u: addr = 0x%8.8" PRIx64 " size = %u state = %s type = %s%s",
245 IsEnabled() ? "enabled" : "disabled",
246 m_watch_read ? "r" : "",
247 m_watch_write ? "w" : "");
249 if (description_level >= lldb::eDescriptionLevelFull) {
250 if (!m_decl_str.empty())
251 s->Printf("\n declare @ '%s'", m_decl_str.c_str());
252 if (!m_watch_spec_str.empty())
253 s->Printf("\n watchpoint spec = '%s'", m_watch_spec_str.c_str());
255 // Dump the snapshots we have taken.
256 DumpSnapshots(s, " ");
258 if (GetConditionText())
259 s->Printf("\n condition = '%s'", GetConditionText());
260 m_options.GetCallbackDescription(s, description_level);
263 if (description_level >= lldb::eDescriptionLevelVerbose)
265 s->Printf("\n hw_index = %i hit_count = %-4u ignore_count = %-4u",
273 Watchpoint::IsEnabled() const
278 // Within StopInfo.cpp, we purposely turn on the ephemeral mode right before temporarily disable the watchpoint
279 // in order to perform possible watchpoint actions without triggering further watchpoint events.
280 // After the temporary disabled watchpoint is enabled, we then turn off the ephemeral mode.
283 Watchpoint::TurnOnEphemeralMode()
285 m_is_ephemeral = true;
289 Watchpoint::TurnOffEphemeralMode()
291 m_is_ephemeral = false;
292 // Leaving ephemeral mode, reset the m_disabled_count!
293 m_disabled_count = 0;
297 Watchpoint::IsDisabledDuringEphemeralMode()
299 return m_disabled_count > 1;
303 Watchpoint::SetEnabled(bool enabled, bool notify)
308 SetHardwareIndex(LLDB_INVALID_INDEX32);
312 // Don't clear the snapshots for now.
313 // Within StopInfo.cpp, we purposely do disable/enable watchpoint while performing watchpoint actions.
315 bool changed = enabled != m_enabled;
317 if (notify && !m_is_ephemeral && changed)
318 SendWatchpointChangedEvent (enabled ? eWatchpointEventTypeEnabled : eWatchpointEventTypeDisabled);
322 Watchpoint::SetWatchpointType (uint32_t type, bool notify)
324 int old_watch_read = m_watch_read;
325 int old_watch_write = m_watch_write;
326 m_watch_read = (type & LLDB_WATCH_TYPE_READ) != 0;
327 m_watch_write = (type & LLDB_WATCH_TYPE_WRITE) != 0;
328 if (notify && (old_watch_read != m_watch_read || old_watch_write != m_watch_write))
329 SendWatchpointChangedEvent (eWatchpointEventTypeTypeChanged);
333 Watchpoint::WatchpointRead () const
335 return m_watch_read != 0;
338 Watchpoint::WatchpointWrite () const
340 return m_watch_write != 0;
343 Watchpoint::GetIgnoreCount () const
345 return m_ignore_count;
349 Watchpoint::SetIgnoreCount (uint32_t n)
351 bool changed = m_ignore_count != n;
354 SendWatchpointChangedEvent (eWatchpointEventTypeIgnoreChanged);
358 Watchpoint::InvokeCallback (StoppointCallbackContext *context)
360 return m_options.InvokeCallback (context, GetID());
364 Watchpoint::SetCondition (const char *condition)
366 if (condition == NULL || condition[0] == '\0')
368 if (m_condition_ap.get())
369 m_condition_ap.reset();
373 // Pass NULL for expr_prefix (no translation-unit level definitions).
374 m_condition_ap.reset(new ClangUserExpression (condition, NULL, lldb::eLanguageTypeUnknown, ClangUserExpression::eResultTypeAny));
376 SendWatchpointChangedEvent (eWatchpointEventTypeConditionChanged);
380 Watchpoint::GetConditionText () const
382 if (m_condition_ap.get())
383 return m_condition_ap->GetUserText();
389 Watchpoint::SendWatchpointChangedEvent (lldb::WatchpointEventType eventKind)
392 && GetTarget().EventTypeHasListeners(Target::eBroadcastBitWatchpointChanged))
394 WatchpointEventData *data = new Watchpoint::WatchpointEventData (eventKind, shared_from_this());
395 GetTarget().BroadcastEvent (Target::eBroadcastBitWatchpointChanged, data);
400 Watchpoint::SendWatchpointChangedEvent (WatchpointEventData *data)
407 && GetTarget().EventTypeHasListeners(Target::eBroadcastBitWatchpointChanged))
408 GetTarget().BroadcastEvent (Target::eBroadcastBitWatchpointChanged, data);
413 Watchpoint::WatchpointEventData::WatchpointEventData (WatchpointEventType sub_type,
414 const WatchpointSP &new_watchpoint_sp) :
416 m_watchpoint_event (sub_type),
417 m_new_watchpoint_sp (new_watchpoint_sp)
421 Watchpoint::WatchpointEventData::~WatchpointEventData ()
426 Watchpoint::WatchpointEventData::GetFlavorString ()
428 static ConstString g_flavor ("Watchpoint::WatchpointEventData");
433 Watchpoint::WatchpointEventData::GetFlavor () const
435 return WatchpointEventData::GetFlavorString ();
440 Watchpoint::WatchpointEventData::GetWatchpoint ()
442 return m_new_watchpoint_sp;
446 Watchpoint::WatchpointEventData::GetWatchpointEventType () const
448 return m_watchpoint_event;
452 Watchpoint::WatchpointEventData::Dump (Stream *s) const
456 const Watchpoint::WatchpointEventData *
457 Watchpoint::WatchpointEventData::GetEventDataFromEvent (const Event *event)
461 const EventData *event_data = event->GetData();
462 if (event_data && event_data->GetFlavor() == WatchpointEventData::GetFlavorString())
463 return static_cast <const WatchpointEventData *> (event->GetData());
469 Watchpoint::WatchpointEventData::GetWatchpointEventTypeFromEvent (const EventSP &event_sp)
471 const WatchpointEventData *data = GetEventDataFromEvent (event_sp.get());
474 return eWatchpointEventTypeInvalidType;
476 return data->GetWatchpointEventType();
480 Watchpoint::WatchpointEventData::GetWatchpointFromEvent (const EventSP &event_sp)
484 const WatchpointEventData *data = GetEventDataFromEvent (event_sp.get());
486 wp_sp = data->m_new_watchpoint_sp;