2011-10-14 08:42:25 +08:00
|
|
|
//===-- Watchpoint.cpp ------------------------------------------*- C++ -*-===//
|
2010-06-09 00:52:24 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-10-14 08:42:25 +08:00
|
|
|
#include "lldb/Breakpoint/Watchpoint.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
// C Includes
|
|
|
|
// C++ Includes
|
|
|
|
// Other libraries and framework includes
|
|
|
|
// Project includes
|
2011-10-18 02:58:00 +08:00
|
|
|
#include "lldb/Breakpoint/StoppointCallbackContext.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
#include "lldb/Core/Stream.h"
|
2012-10-23 15:20:06 +08:00
|
|
|
#include "lldb/Core/Value.h"
|
|
|
|
#include "lldb/Core/ValueObject.h"
|
|
|
|
#include "lldb/Core/ValueObjectMemory.h"
|
|
|
|
#include "lldb/Symbol/ClangASTContext.h"
|
2011-10-18 02:58:00 +08:00
|
|
|
#include "lldb/Target/Process.h"
|
|
|
|
#include "lldb/Target/Target.h"
|
|
|
|
#include "lldb/Target/ThreadSpec.h"
|
2012-05-02 08:30:53 +08:00
|
|
|
#include "lldb/Expression/ClangUserExpression.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2012-10-23 15:20:06 +08:00
|
|
|
Watchpoint::Watchpoint (Target& target, lldb::addr_t addr, size_t size, const ClangASTType *type, bool hardware) :
|
2012-02-25 14:44:30 +08:00
|
|
|
StoppointLocation (0, addr, size, hardware),
|
2012-10-23 15:20:06 +08:00
|
|
|
m_target(target),
|
2012-02-25 14:44:30 +08:00
|
|
|
m_enabled(false),
|
2011-09-23 02:04:58 +08:00
|
|
|
m_is_hardware(hardware),
|
2012-08-14 05:09:54 +08:00
|
|
|
m_is_watch_variable(false),
|
2012-08-22 07:17:04 +08:00
|
|
|
m_is_ephemeral(false),
|
2012-08-24 06:28:26 +08:00
|
|
|
m_disabled_count(0),
|
2010-06-09 00:52:24 +08:00
|
|
|
m_watch_read(0),
|
|
|
|
m_watch_write(0),
|
|
|
|
m_watch_was_read(0),
|
|
|
|
m_watch_was_written(0),
|
|
|
|
m_ignore_count(0),
|
2012-08-15 04:56:37 +08:00
|
|
|
m_false_alarms(0),
|
2011-10-15 03:15:48 +08:00
|
|
|
m_decl_str(),
|
2012-02-25 14:44:30 +08:00
|
|
|
m_watch_spec_str(),
|
2012-10-23 15:20:06 +08:00
|
|
|
m_type(),
|
2012-08-10 07:09:42 +08:00
|
|
|
m_error(),
|
|
|
|
m_options ()
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2012-10-23 15:20:06 +08:00
|
|
|
if (type && type->IsValid())
|
|
|
|
m_type = *type;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If we don't have a known type, then we force it to unsigned int of the right size.
|
|
|
|
ClangASTContext *ast_context = target.GetScratchClangASTContext();
|
2012-10-24 05:09:09 +08:00
|
|
|
clang_type_t clang_type = ast_context->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 8 * size);
|
2012-10-23 15:20:06 +08:00
|
|
|
m_type.SetClangType(ast_context->getASTContext(), clang_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the initial value of the watched variable:
|
|
|
|
if (m_target.GetProcessSP())
|
|
|
|
{
|
|
|
|
ExecutionContext exe_ctx;
|
|
|
|
m_target.GetProcessSP()->CalculateExecutionContext(exe_ctx);
|
|
|
|
CaptureWatchedValue (exe_ctx);
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2011-10-14 08:42:25 +08:00
|
|
|
Watchpoint::~Watchpoint()
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-10 07:09:42 +08:00
|
|
|
// This function is used when "baton" doesn't need to be freed
|
|
|
|
void
|
|
|
|
Watchpoint::SetCallback (WatchpointHitCallback callback, void *baton, bool is_synchronous)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2012-08-10 07:09:42 +08:00
|
|
|
// The default "Baton" class will keep a copy of "baton" and won't free
|
|
|
|
// or delete it when it goes goes out of scope.
|
|
|
|
m_options.SetCallback(callback, BatonSP (new Baton(baton)), is_synchronous);
|
|
|
|
|
|
|
|
//SendWatchpointChangedEvent (eWatchpointEventTypeCommandChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function is used when a baton needs to be freed and therefore is
|
|
|
|
// contained in a "Baton" subclass.
|
|
|
|
void
|
|
|
|
Watchpoint::SetCallback (WatchpointHitCallback callback, const BatonSP &callback_baton_sp, bool is_synchronous)
|
|
|
|
{
|
|
|
|
m_options.SetCallback(callback, callback_baton_sp, is_synchronous);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Watchpoint::ClearCallback ()
|
|
|
|
{
|
|
|
|
m_options.ClearCallback ();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2011-09-17 05:41:42 +08:00
|
|
|
void
|
2012-08-14 05:09:54 +08:00
|
|
|
Watchpoint::SetDeclInfo (const std::string &str)
|
2011-09-17 05:41:42 +08:00
|
|
|
{
|
|
|
|
m_decl_str = str;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-14 05:09:54 +08:00
|
|
|
std::string
|
|
|
|
Watchpoint::GetWatchSpec()
|
|
|
|
{
|
|
|
|
return m_watch_spec_str;
|
|
|
|
}
|
|
|
|
|
2012-02-25 14:44:30 +08:00
|
|
|
void
|
2012-08-14 05:09:54 +08:00
|
|
|
Watchpoint::SetWatchSpec (const std::string &str)
|
2012-02-25 14:44:30 +08:00
|
|
|
{
|
|
|
|
m_watch_spec_str = str;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-24 07:03:59 +08:00
|
|
|
// Override default impl of StoppointLocation::IsHardware() since m_is_hardware
|
|
|
|
// member field is more accurate.
|
2011-09-23 02:04:58 +08:00
|
|
|
bool
|
2011-10-14 08:42:25 +08:00
|
|
|
Watchpoint::IsHardware () const
|
2011-09-23 02:04:58 +08:00
|
|
|
{
|
|
|
|
return m_is_hardware;
|
|
|
|
}
|
|
|
|
|
2012-08-14 05:09:54 +08:00
|
|
|
bool
|
|
|
|
Watchpoint::IsWatchVariable() const
|
|
|
|
{
|
|
|
|
return m_is_watch_variable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Watchpoint::SetWatchVariable(bool val)
|
|
|
|
{
|
|
|
|
m_is_watch_variable = val;
|
|
|
|
}
|
|
|
|
|
2012-10-23 15:20:06 +08:00
|
|
|
bool
|
|
|
|
Watchpoint::CaptureWatchedValue (const ExecutionContext &exe_ctx)
|
|
|
|
{
|
|
|
|
ConstString watch_name("$__lldb__watch_value");
|
|
|
|
m_old_value_sp = m_new_value_sp;
|
|
|
|
Address watch_address(GetLoadAddress());
|
2012-10-24 05:09:09 +08:00
|
|
|
if (!m_type.IsValid())
|
|
|
|
{
|
|
|
|
// Don't know how to report new & old values, since we couldn't make a scalar type for this watchpoint.
|
|
|
|
// This works around an assert in ValueObjectMemory::Create.
|
|
|
|
// FIXME: This should not happen, but if it does in some case we care about,
|
|
|
|
// we can go grab the value raw and print it as unsigned.
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-23 15:20:06 +08:00
|
|
|
m_new_value_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(), watch_name.AsCString(), watch_address, m_type);
|
|
|
|
m_new_value_sp = m_new_value_sp->CreateConstantValue(watch_name);
|
|
|
|
if (m_new_value_sp && m_new_value_sp->GetError().Success())
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-15 04:56:37 +08:00
|
|
|
void
|
|
|
|
Watchpoint::IncrementFalseAlarmsAndReviseHitCount()
|
|
|
|
{
|
|
|
|
++m_false_alarms;
|
|
|
|
if (m_false_alarms)
|
|
|
|
{
|
|
|
|
if (m_hit_count >= m_false_alarms)
|
|
|
|
{
|
|
|
|
m_hit_count -= m_false_alarms;
|
|
|
|
m_false_alarms = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_false_alarms -= m_hit_count;
|
|
|
|
m_hit_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
// RETURNS - true if we should stop at this breakpoint, false if we
|
|
|
|
// should continue.
|
|
|
|
|
|
|
|
bool
|
2011-10-14 08:42:25 +08:00
|
|
|
Watchpoint::ShouldStop (StoppointCallbackContext *context)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2012-01-24 07:03:59 +08:00
|
|
|
IncrementHitCount();
|
2011-09-23 02:04:58 +08:00
|
|
|
|
|
|
|
if (!IsEnabled())
|
|
|
|
return false;
|
|
|
|
|
2012-01-24 07:03:59 +08:00
|
|
|
if (GetHitCount() <= GetIgnoreCount())
|
2011-09-23 02:04:58 +08:00
|
|
|
return false;
|
|
|
|
|
2011-10-18 02:58:00 +08:00
|
|
|
return true;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2011-09-13 07:38:44 +08:00
|
|
|
void
|
2011-10-14 08:42:25 +08:00
|
|
|
Watchpoint::GetDescription (Stream *s, lldb::DescriptionLevel level)
|
2011-09-13 07:38:44 +08:00
|
|
|
{
|
2011-09-17 05:41:42 +08:00
|
|
|
DumpWithLevel(s, level);
|
2011-09-13 07:38:44 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
void
|
2011-10-14 08:42:25 +08:00
|
|
|
Watchpoint::Dump(Stream *s) const
|
2011-09-17 05:41:42 +08:00
|
|
|
{
|
|
|
|
DumpWithLevel(s, lldb::eDescriptionLevelBrief);
|
|
|
|
}
|
|
|
|
|
2012-08-14 07:27:50 +08:00
|
|
|
// If prefix is NULL, we display the watch id and ignore the prefix altogether.
|
2012-08-14 05:09:54 +08:00
|
|
|
void
|
2012-08-14 07:27:50 +08:00
|
|
|
Watchpoint::DumpSnapshots(Stream *s, const char *prefix) const
|
2012-08-14 05:09:54 +08:00
|
|
|
{
|
2012-08-14 07:27:50 +08:00
|
|
|
if (!prefix)
|
|
|
|
{
|
|
|
|
s->Printf("\nWatchpoint %u hit:", GetID());
|
|
|
|
prefix = "";
|
|
|
|
}
|
2012-10-23 15:20:06 +08:00
|
|
|
|
|
|
|
if (m_old_value_sp)
|
2012-08-14 05:09:54 +08:00
|
|
|
{
|
2012-10-23 15:20:06 +08:00
|
|
|
s->Printf("\n%sold value: %s", prefix, m_old_value_sp->GetValueAsCString());
|
2012-08-14 05:09:54 +08:00
|
|
|
}
|
2012-10-23 15:20:06 +08:00
|
|
|
if (m_new_value_sp)
|
2012-08-14 05:09:54 +08:00
|
|
|
{
|
2012-10-23 15:20:06 +08:00
|
|
|
s->Printf("\n%snew value: %s", prefix, m_new_value_sp->GetValueAsCString());
|
2012-08-14 05:09:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-17 05:41:42 +08:00
|
|
|
void
|
2011-10-14 08:42:25 +08:00
|
|
|
Watchpoint::DumpWithLevel(Stream *s, lldb::DescriptionLevel description_level) const
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
if (s == NULL)
|
|
|
|
return;
|
|
|
|
|
2011-09-17 05:41:42 +08:00
|
|
|
assert(description_level >= lldb::eDescriptionLevelBrief &&
|
|
|
|
description_level <= lldb::eDescriptionLevelVerbose);
|
|
|
|
|
2012-11-30 05:49:15 +08:00
|
|
|
s->Printf("Watchpoint %u: addr = 0x%8.8" PRIx64 " size = %u state = %s type = %s%s",
|
2011-09-17 05:41:42 +08:00
|
|
|
GetID(),
|
2012-01-24 08:11:02 +08:00
|
|
|
GetLoadAddress(),
|
2011-09-17 05:41:42 +08:00
|
|
|
m_byte_size,
|
2012-02-25 14:44:30 +08:00
|
|
|
IsEnabled() ? "enabled" : "disabled",
|
2011-09-17 05:41:42 +08:00
|
|
|
m_watch_read ? "r" : "",
|
|
|
|
m_watch_write ? "w" : "");
|
|
|
|
|
2011-10-18 02:58:00 +08:00
|
|
|
if (description_level >= lldb::eDescriptionLevelFull) {
|
2012-01-31 05:46:17 +08:00
|
|
|
if (!m_decl_str.empty())
|
2011-10-18 02:58:00 +08:00
|
|
|
s->Printf("\n declare @ '%s'", m_decl_str.c_str());
|
2012-02-25 14:44:30 +08:00
|
|
|
if (!m_watch_spec_str.empty())
|
2012-08-14 05:09:54 +08:00
|
|
|
s->Printf("\n watchpoint spec = '%s'", m_watch_spec_str.c_str());
|
|
|
|
|
|
|
|
// Dump the snapshots we have taken.
|
2012-08-14 07:27:50 +08:00
|
|
|
DumpSnapshots(s, " ");
|
2012-08-14 05:09:54 +08:00
|
|
|
|
2011-10-18 02:58:00 +08:00
|
|
|
if (GetConditionText())
|
|
|
|
s->Printf("\n condition = '%s'", GetConditionText());
|
2012-08-10 07:09:42 +08:00
|
|
|
m_options.GetCallbackDescription(s, description_level);
|
2011-10-18 02:58:00 +08:00
|
|
|
}
|
2011-09-17 05:41:42 +08:00
|
|
|
|
|
|
|
if (description_level >= lldb::eDescriptionLevelVerbose)
|
2012-02-24 06:32:13 +08:00
|
|
|
{
|
2012-08-10 07:09:42 +08:00
|
|
|
s->Printf("\n hw_index = %i hit_count = %-4u ignore_count = %-4u",
|
|
|
|
GetHardwareIndex(),
|
|
|
|
GetHitCount(),
|
|
|
|
GetIgnoreCount());
|
2012-02-24 06:32:13 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2011-10-14 08:42:25 +08:00
|
|
|
Watchpoint::IsEnabled() const
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
return m_enabled;
|
|
|
|
}
|
|
|
|
|
2012-08-22 07:17:04 +08:00
|
|
|
// Within StopInfo.cpp, we purposely turn on the ephemeral mode right before temporarily disable the watchpoint
|
|
|
|
// in order to perform possible watchpoint actions without triggering further watchpoint events.
|
|
|
|
// After the temporary disabled watchpoint is enabled, we then turn off the ephemeral mode.
|
|
|
|
|
|
|
|
void
|
|
|
|
Watchpoint::TurnOnEphemeralMode()
|
|
|
|
{
|
|
|
|
m_is_ephemeral = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Watchpoint::TurnOffEphemeralMode()
|
|
|
|
{
|
|
|
|
m_is_ephemeral = false;
|
2012-08-24 06:28:26 +08:00
|
|
|
// Leaving ephemeral mode, reset the m_disabled_count!
|
|
|
|
m_disabled_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Watchpoint::IsDisabledDuringEphemeralMode()
|
|
|
|
{
|
|
|
|
return m_disabled_count > 1;
|
2012-08-22 07:17:04 +08:00
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
void
|
2011-10-14 08:42:25 +08:00
|
|
|
Watchpoint::SetEnabled(bool enabled)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
if (!enabled)
|
2012-08-14 07:27:50 +08:00
|
|
|
{
|
2012-08-22 07:17:04 +08:00
|
|
|
if (!m_is_ephemeral)
|
|
|
|
SetHardwareIndex(LLDB_INVALID_INDEX32);
|
2012-08-24 06:28:26 +08:00
|
|
|
else
|
|
|
|
++m_disabled_count;
|
|
|
|
|
2012-08-22 06:06:34 +08:00
|
|
|
// Don't clear the snapshots for now.
|
|
|
|
// Within StopInfo.cpp, we purposely do disable/enable watchpoint while performing watchpoint actions.
|
2012-08-14 07:27:50 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
m_enabled = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-10-14 08:42:25 +08:00
|
|
|
Watchpoint::SetWatchpointType (uint32_t type)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
m_watch_read = (type & LLDB_WATCH_TYPE_READ) != 0;
|
|
|
|
m_watch_write = (type & LLDB_WATCH_TYPE_WRITE) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2011-10-14 08:42:25 +08:00
|
|
|
Watchpoint::WatchpointRead () const
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
return m_watch_read != 0;
|
|
|
|
}
|
|
|
|
bool
|
2011-10-14 08:42:25 +08:00
|
|
|
Watchpoint::WatchpointWrite () const
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
return m_watch_write != 0;
|
|
|
|
}
|
2010-07-10 04:39:50 +08:00
|
|
|
uint32_t
|
2011-10-14 08:42:25 +08:00
|
|
|
Watchpoint::GetIgnoreCount () const
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
return m_ignore_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-10-14 08:42:25 +08:00
|
|
|
Watchpoint::SetIgnoreCount (uint32_t n)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
m_ignore_count = n;
|
|
|
|
}
|
2011-10-18 02:58:00 +08:00
|
|
|
|
|
|
|
bool
|
|
|
|
Watchpoint::InvokeCallback (StoppointCallbackContext *context)
|
|
|
|
{
|
2012-08-10 07:09:42 +08:00
|
|
|
return m_options.InvokeCallback (context, GetID());
|
2011-10-18 02:58:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Watchpoint::SetCondition (const char *condition)
|
|
|
|
{
|
|
|
|
if (condition == NULL || condition[0] == '\0')
|
|
|
|
{
|
|
|
|
if (m_condition_ap.get())
|
|
|
|
m_condition_ap.reset();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Pass NULL for expr_prefix (no translation-unit level definitions).
|
2011-12-22 06:22:58 +08:00
|
|
|
m_condition_ap.reset(new ClangUserExpression (condition, NULL, lldb::eLanguageTypeUnknown, ClangUserExpression::eResultTypeAny));
|
2011-10-18 02:58:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
Watchpoint::GetConditionText () const
|
|
|
|
{
|
|
|
|
if (m_condition_ap.get())
|
|
|
|
return m_condition_ap->GetUserText();
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|