Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lldb] Add 'modify' type watchpoints, make it default #66308

Merged
merged 4 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lldb/bindings/headers.swig
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@
#include "lldb/API/SBValueList.h"
#include "lldb/API/SBVariablesOptions.h"
#include "lldb/API/SBWatchpoint.h"
#include "lldb/API/SBWatchpointOptions.h"
%}
12 changes: 12 additions & 0 deletions lldb/bindings/interface/SBWatchpointOptionsDocstrings.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
%feature("docstring",
"A container for options to use when creating watchpoints."
) lldb::SBWatchpointOptions;

%feature("docstring", "Sets whether the watchpoint should stop on read accesses."
) lldb::SBWatchpointOptions::SetWatchpointTypeRead;
%feature("docstring", "Gets whether the watchpoint should stop on read accesses."
) lldb::SBWatchpointOptions::GetWatchpointTypeRead;
%feature("docstring", "Sets whether the watchpoint should stop on write accesses. eWatchpointWriteTypeOnModify is the most commonly useful mode, where lldb will stop when the watched value has changed. eWatchpointWriteTypeAlways will stop on any write to the watched region, and on some targets there can false watchpoint stops where memory near the watched region was written, and lldb cannot detect that it is a spurious stop."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to state "and on some targets there can false watchpoint stops where memory near the watched region was written, and lldb cannot detect that it is a spurious stop."? Can we remove that? Seems like information that most users wouldn't need to know?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'll remove that text. It's going to be part of my large watchpoints enhancement -- on stubs that support it on AArch64, we'll use a masking watchpoint, so you can watch a 48 byte object but we'll do it by watching 64 bytes of memory. for a straight-up "write" watchpoint, we can't tell whether the 48 bytes were written to with the same value, or the 16 bytes we aren't intending to watch were written to. Also with AArch64 in SVE Streaming Mode, the address matching is at lower accuracy, so a write near a watchpoint can match the watchpoint, even if it didn't actually access the memory range.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(and the "modify" watchpoint type solves both of these, because we can only report watchpoint hits where the watched memory region changes)

) lldb::SBWatchpointOptions::SetWatchpointTypeWrite;
%feature("docstring", "Gets whether the watchpoint should stop on write accesses, returning WatchpointWriteType to indicate the type of write watching that is enabled, or eWatchpointWriteTypeDisabled."
) lldb::SBWatchpointOptions::GetWatchpointTypeWrite;
2 changes: 2 additions & 0 deletions lldb/bindings/interfaces.swig
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
%include "./interface/SBValueListDocstrings.i"
%include "./interface/SBVariablesOptionsDocstrings.i"
%include "./interface/SBWatchpointDocstrings.i"
%include "./interface/SBWatchpointOptionsDocstrings.i"

/* API headers */
%include "lldb/API/SBAddress.h"
Expand Down Expand Up @@ -152,6 +153,7 @@
%include "lldb/API/SBValueList.h"
%include "lldb/API/SBVariablesOptions.h"
%include "lldb/API/SBWatchpoint.h"
%include "lldb/API/SBWatchpointOptions.h"

/* Extensions for SB classes */
%include "./interface/SBAddressExtensions.i"
Expand Down
8 changes: 7 additions & 1 deletion lldb/include/lldb/API/SBTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "lldb/API/SBType.h"
#include "lldb/API/SBValue.h"
#include "lldb/API/SBWatchpoint.h"
#include "lldb/API/SBWatchpointOptions.h"

namespace lldb_private {
namespace python {
Expand Down Expand Up @@ -828,8 +829,13 @@ class LLDB_API SBTarget {

lldb::SBWatchpoint FindWatchpointByID(lldb::watch_id_t watch_id);

LLDB_DEPRECATED("WatchAddress deprecated, use WatchpointCreateByAddress")
lldb::SBWatchpoint WatchAddress(lldb::addr_t addr, size_t size, bool read,
bool write, SBError &error);
bool modify, SBError &error);

lldb::SBWatchpoint
WatchpointCreateByAddress(lldb::addr_t addr, size_t size,
lldb::SBWatchpointOptions options, SBError &error);

bool EnableAllWatchpoints();

Expand Down
43 changes: 43 additions & 0 deletions lldb/include/lldb/API/SBWatchpointOptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//===-- SBWatchpointOptions.h -----------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_API_SBWATCHPOINTOPTIONS_H
#define LLDB_API_SBWATCHPOINTOPTIONS_H

#include "lldb/API/SBDefines.h"

class WatchpointOptionsImpl;

namespace lldb {

class LLDB_API SBWatchpointOptions {
public:
SBWatchpointOptions();

SBWatchpointOptions(const lldb::SBWatchpointOptions &rhs);

~SBWatchpointOptions();

const SBWatchpointOptions &operator=(const lldb::SBWatchpointOptions &rhs);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a header doc explaining this will stop any time memory is read from. Might want to mention if this works along with "modify" at all to only stop when reading a value and that value has changed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can do read+modify, actually. I fixed a subtle bug in this latest commit in Watchpoint::WatchedValueReportable() where we know that a watched memory region may have been accessed. If it's read+modify, we don't know if it's been read from, or written to (value mutating or not). Unless we emulate the instruction that was executed, it could be a read, or a write with the same value, or it could be an access near the watched region which triggered the stop. (this latter happening with large watchpoint support where watching a 24 byte region may actually need to watch 32 bytes, or on an AArch64 processor in streaming SVE mode when a write is near the watched region)

Effectively, if a watchpoint is read+modify, it will behave the same as read+write -- we will have to stop on every access to that memory region (or near that memory region, for the above reasons).

I only came to understand this behavior this afternoon while debugging a testsuite failure, I need to do a fuller once-over of my changes to see if there are other updates I need to make.

/// Stop when the watched memory region is read.
void SetWatchpointTypeRead(bool read);
bool GetWatchpointTypeRead() const;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add header doc here to explain that write mode will stop on any write even if the value doesn't change

/// Stop when the watched memory region is written to/modified
void SetWatchpointTypeWrite(lldb::WatchpointWriteType write_type);
lldb::WatchpointWriteType GetWatchpointTypeWrite() const;

private:
// This auto_pointer is made in the constructor and is always valid.
mutable std::unique_ptr<WatchpointOptionsImpl> m_opaque_up;
};

} // namespace lldb

#endif // LLDB_API_SBWATCHPOINTOPTIONS_H
5 changes: 4 additions & 1 deletion lldb/include/lldb/Breakpoint/Watchpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ class Watchpoint : public std::enable_shared_from_this<Watchpoint>,

bool WatchpointRead() const;
bool WatchpointWrite() const;
bool WatchpointModify() const;
uint32_t GetIgnoreCount() const;
void SetIgnoreCount(uint32_t n);
void SetWatchpointType(uint32_t type, bool notify = true);
void SetDeclInfo(const std::string &str);
std::string GetWatchSpec();
void SetWatchSpec(const std::string &str);
bool WatchedValueReportable(const ExecutionContext &exe_ctx);

// Snapshot management interface.
bool IsWatchVariable() const;
Expand Down Expand Up @@ -212,7 +214,8 @@ class Watchpoint : public std::enable_shared_from_this<Watchpoint>,
// again, we check the count, if it is more than 1, it means the user-
// supplied actions actually want the watchpoint to be disabled!
uint32_t m_watch_read : 1, // 1 if we stop when the watched data is read from
m_watch_write : 1; // 1 if we stop when the watched data is written to
m_watch_write : 1, // 1 if we stop when the watched data is written to
m_watch_modify : 1; // 1 if we stop when the watched data is changed
uint32_t m_ignore_count; // Number of times to ignore this watchpoint
std::string m_decl_str; // Declaration information, if any.
std::string m_watch_spec_str; // Spec for the watchpoint.
Expand Down
8 changes: 5 additions & 3 deletions lldb/include/lldb/Interpreter/OptionGroupWatchpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ class OptionGroupWatchpoint : public OptionGroup {

void OptionParsingStarting(ExecutionContext *execution_context) override;

// Note:
// eWatchRead == LLDB_WATCH_TYPE_READ; and
// eWatchWrite == LLDB_WATCH_TYPE_WRITE
/// eWatchRead == LLDB_WATCH_TYPE_READ
/// eWatchWrite == LLDB_WATCH_TYPE_WRITE
/// eWatchModify == LLDB_WATCH_TYPE_MODIFY
/// eWatchReadWrite == LLDB_WATCH_TYPE_READ | LLDB_WATCH_TYPE_WRITE
enum WatchType {
eWatchInvalid = 0,
eWatchRead,
eWatchWrite,
eWatchModify,
eWatchReadWrite
};

Expand Down
4 changes: 3 additions & 1 deletion lldb/include/lldb/lldb-defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@
#define LLDB_WATCH_ID_IS_VALID(uid) ((uid) != (LLDB_INVALID_WATCH_ID))
#define LLDB_WATCH_TYPE_READ (1u << 0)
#define LLDB_WATCH_TYPE_WRITE (1u << 1)
#define LLDB_WATCH_TYPE_MODIFY (1u << 2)
#define LLDB_WATCH_TYPE_IS_VALID(type) \
((type & LLDB_WATCH_TYPE_READ) || (type & LLDB_WATCH_TYPE_WRITE))
((type & LLDB_WATCH_TYPE_READ) || (type & LLDB_WATCH_TYPE_WRITE) || \
(type & LLDB_WATCH_TYPE_MODIFY))

// Generic Register Numbers
#define LLDB_REGNUM_GENERIC_PC 0 // Program Counter
Expand Down
15 changes: 15 additions & 0 deletions lldb/include/lldb/lldb-enumerations.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,21 @@ FLAGS_ENUM(WatchpointEventType){
eWatchpointEventTypeThreadChanged = (1u << 11),
eWatchpointEventTypeTypeChanged = (1u << 12)};

enum WatchpointWriteType {
/// Don't stop when the watched memory region is written to.
eWatchpointWriteTypeDisabled,
/// Stop on any write access to the memory region, even if
/// the value doesn't change. On some architectures, a write
/// near the memory region may be falsely reported as a match,
/// and notify this spurious stop as a watchpoint trap.
eWatchpointWriteTypeAlways,
/// Stop on a write to the memory region that changes its value.
/// This is most likely the behavior a user expects, and is the
/// behavior in gdb. lldb can silently ignore writes near the
/// watched memory region that are reported as accesses to lldb.
eWatchpointWriteTypeOnModify
};

/// Programming language type.
///
/// These enumerations use the same language enumerations as the DWARF
Expand Down
1 change: 1 addition & 0 deletions lldb/include/lldb/lldb-forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ class VariableList;
class Watchpoint;
class WatchpointList;
class WatchpointOptions;
class WatchpointSetOptions;
struct CompilerContext;
struct LineEntry;
struct PropertyDefinition;
Expand Down
1 change: 1 addition & 0 deletions lldb/source/API/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ add_lldb_library(liblldb SHARED ${option_framework}
SBValueList.cpp
SBVariablesOptions.cpp
SBWatchpoint.cpp
SBWatchpointOptions.cpp
SBUnixSignals.cpp
SystemInitializerFull.cpp
${lldb_python_wrapper}
Expand Down
40 changes: 26 additions & 14 deletions lldb/source/API/SBTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1322,27 +1322,39 @@ SBWatchpoint SBTarget::FindWatchpointByID(lldb::watch_id_t wp_id) {
}

lldb::SBWatchpoint SBTarget::WatchAddress(lldb::addr_t addr, size_t size,
bool read, bool write,
bool read, bool modify,
SBError &error) {
LLDB_INSTRUMENT_VA(this, addr, size, read, write, error);

SBWatchpointOptions options;
options.SetWatchpointTypeRead(read);
options.SetWatchpointTypeWrite(eWatchpointWriteTypeOnModify);
return WatchpointCreateByAddress(addr, size, options, error);
}

lldb::SBWatchpoint
SBTarget::WatchpointCreateByAddress(lldb::addr_t addr, size_t size,
SBWatchpointOptions options,
SBError &error) {
LLDB_INSTRUMENT_VA(this, addr, size, options, error);

SBWatchpoint sb_watchpoint;
lldb::WatchpointSP watchpoint_sp;
TargetSP target_sp(GetSP());
if (target_sp && (read || write) && addr != LLDB_INVALID_ADDRESS &&
size > 0) {
uint32_t watch_type = 0;
if (options.GetWatchpointTypeRead())
watch_type |= LLDB_WATCH_TYPE_READ;
if (options.GetWatchpointTypeWrite() == eWatchpointWriteTypeAlways)
watch_type |= LLDB_WATCH_TYPE_WRITE;
if (options.GetWatchpointTypeWrite() == eWatchpointWriteTypeOnModify)
watch_type |= LLDB_WATCH_TYPE_MODIFY;
if (watch_type == 0) {
error.SetErrorString("Can't create a watchpoint that is neither read nor "
"write nor modify.");
return sb_watchpoint;
}
if (target_sp && addr != LLDB_INVALID_ADDRESS && size > 0) {
std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
uint32_t watch_type = 0;
if (read)
watch_type |= LLDB_WATCH_TYPE_READ;
if (write)
watch_type |= LLDB_WATCH_TYPE_WRITE;
if (watch_type == 0) {
error.SetErrorString(
"Can't create a watchpoint that is neither read nor write.");
return sb_watchpoint;
}

// Target::CreateWatchpoint() is thread safe.
Status cw_error;
// This API doesn't take in a type, so we can't figure out what it is.
Expand Down
13 changes: 10 additions & 3 deletions lldb/source/API/SBValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1433,10 +1433,17 @@ lldb::SBWatchpoint SBValue::Watch(bool resolve_location, bool read, bool write,
return sb_watchpoint;

uint32_t watch_type = 0;
if (read)
if (read) {
watch_type |= LLDB_WATCH_TYPE_READ;
if (write)
watch_type |= LLDB_WATCH_TYPE_WRITE;
// read + write, the most likely intention
// is to catch all writes to this, not just
// value modifications.
if (write)
watch_type |= LLDB_WATCH_TYPE_WRITE;
} else {
if (write)
watch_type |= LLDB_WATCH_TYPE_MODIFY;
}

Status rc;
CompilerType type(value_sp->GetCompilerType());
Expand Down
3 changes: 2 additions & 1 deletion lldb/source/API/SBWatchpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ bool SBWatchpoint::IsWatchingWrites() {
std::lock_guard<std::recursive_mutex> guard(
watchpoint_sp->GetTarget().GetAPIMutex());

return watchpoint_sp->WatchpointWrite();
return watchpoint_sp->WatchpointWrite() ||
watchpoint_sp->WatchpointModify();
}

return false;
Expand Down
73 changes: 73 additions & 0 deletions lldb/source/API/SBWatchpointOptions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//===-- SBWatchpointOptions.cpp -------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "lldb/API/SBWatchpointOptions.h"
#include "lldb/Breakpoint/Watchpoint.h"
#include "lldb/Utility/Instrumentation.h"

#include "Utils.h"

using namespace lldb;
using namespace lldb_private;

class WatchpointOptionsImpl {
public:
bool m_read = false;
bool m_write = false;
bool m_modify = false;
};
Comment on lines +18 to +23
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be moved to Watchpoint.h and then used in our internal API as well if you like that idea.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I originally wrote that little class in Watchpoint.h and then I moved it into the SBWatchpointOptions because I wasn't using it anywhere else. tbh I thought the class for the three bools was overkill right now but I agree that there's long-term value in making the SB API cleanly extensible. But for the lldb_private API, I'm less worried about using three bools for now. If it does become more than that, an Options class would be a thing to adopt.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we can do anything internally, so it doesn't really matter what we do inside as we can always change it at any point as long as the public API is solid.



SBWatchpointOptions::SBWatchpointOptions()
: m_opaque_up(new WatchpointOptionsImpl()) {
LLDB_INSTRUMENT_VA(this);
}

SBWatchpointOptions::SBWatchpointOptions(const SBWatchpointOptions &rhs) {
LLDB_INSTRUMENT_VA(this, rhs);

m_opaque_up = clone(rhs.m_opaque_up);
}

const SBWatchpointOptions &
SBWatchpointOptions::operator=(const SBWatchpointOptions &rhs) {
LLDB_INSTRUMENT_VA(this, rhs);

if (this != &rhs)
m_opaque_up = clone(rhs.m_opaque_up);
return *this;
}

SBWatchpointOptions::~SBWatchpointOptions() = default;

void SBWatchpointOptions::SetWatchpointTypeRead(bool read) {
m_opaque_up->m_read = read;
}
bool SBWatchpointOptions::GetWatchpointTypeRead() const {
return m_opaque_up->m_read;
}

void SBWatchpointOptions::SetWatchpointTypeWrite(
WatchpointWriteType write_type) {
if (write_type == eWatchpointWriteTypeOnModify) {
m_opaque_up->m_write = false;
m_opaque_up->m_modify = true;
} else if (write_type == eWatchpointWriteTypeAlways) {
m_opaque_up->m_write = true;
m_opaque_up->m_modify = false;
} else
m_opaque_up->m_write = m_opaque_up->m_modify = false;
}

WatchpointWriteType SBWatchpointOptions::GetWatchpointTypeWrite() const {
if (m_opaque_up->m_modify)
return eWatchpointWriteTypeOnModify;
if (m_opaque_up->m_write)
return eWatchpointWriteTypeAlways;
return eWatchpointWriteTypeDisabled;
}