Jason Molenda c73a3f16f8 [lldb] [mostly NFC] Large WP foundation: WatchpointResources (#68845)
This patch is rearranging code a bit to add WatchpointResources to
Process. A WatchpointResource is meant to represent a hardware
watchpoint register in the inferior process. It has an address, a size,
a type, and a list of Watchpoints that are using this
WatchpointResource.

This current patch doesn't add any of the features of
WatchpointResources that make them interesting -- a user asking to watch
a 24 byte object could watch this with three 8 byte WatchpointResources.
Or a Watchpoint on 1 byte at 0x1002 and a second watchpoint on 1 byte at
0x1003, these must both be served by a single WatchpointResource on that
doubleword at 0x1000 on a 64-bit target, if two hardware watchpoint
registers were used to track these separately, one of them may not be
hit. Or if you have one Watchpoint on a variable with a condition set,
and another Watchpoint on that same variable with a command defined or
different condition, or ignorecount, both of those Watchpoints need to
evaluate their criteria/commands when their WatchpointResource has been
hit.

There's a bit of code movement to rearrange things in the direction I'll
need for implementing this feature, so I want to start with reviewing &
landing this mostly NFC patch and we can focus on the algorithmic
choices about how WatchpointResources are shared and handled as they're
triggeed, separately.

This patch also stops printing "Watchpoint <n> hit: old value: <x>, new
vlaue: <y>" for Read watchpoints. I could make an argument for print
"Watchpoint <n> hit: current value <x>" but the current output doesn't
make any sense, and the user can print the value if they are
particularly interested. Read watchpoints are used primarily to
understand what code is reading a variable.

This patch adds more fallbacks for how to print the objects being
watched if we have types, instead of assuming they are all integral
values, so a struct will print its elements. As large watchpoints are
added, we'll be doing a lot more of those.

To track the WatchpointSP in the WatchpointResources, I changed the
internal API which took a WatchpointSP and devolved it to a Watchpoint*,
which meant touching several different Process files. I removed the
watchpoint code in ProcessKDP which only reported that watchpoints
aren't supported, the base class does that already.

I haven't yet changed how we receive a watchpoint to identify the
WatchpointResource responsible for the trigger, and identify all
Watchpoints that are using this Resource to evaluate their conditions
etc. This is the same work that a BreakpointSite needs to do when it has
been tiggered, where multiple Breakpoints may be at the same address.

There is not yet any printing of the Resources that a Watchpoint is
implemented in terms of ("watchpoint list", or
SBWatchpoint::GetDescription).

"watchpoint set var" and "watchpoint set expression" take a size
argument which was previously 1, 2, 4, or 8 (an enum). I've changed this
to an unsigned int. Most hardware implementations can only watch 1, 2,
4, 8 byte ranges, but with Resources we'll allow a user to ask for
different sized watchpoints and set them in hardware-expressble terms
soon.

I've annotated areas where I know there is work still needed with
LWP_TODO that I'll be working on once this is landed.

I've tested this on aarch64 macOS, aarch64 Linux, and Intel macOS.

https://discourse.llvm.org/t/rfc-large-watchpoint-support-in-lldb/72116
(cherry picked from commit fc6b72523f3d73b921690a713e97a433c96066c6)
2023-11-30 14:59:10 -08:00

185 lines
5.5 KiB
C++

//===-- ProcessKDP.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_SOURCE_PLUGINS_PROCESS_MACOSX_KERNEL_PROCESSKDP_H
#define LLDB_SOURCE_PLUGINS_PROCESS_MACOSX_KERNEL_PROCESSKDP_H
#include <list>
#include <vector>
#include "lldb/Core/ThreadSafeValue.h"
#include "lldb/Host/HostThread.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Thread.h"
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/Broadcaster.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/Utility/Status.h"
#include "lldb/Utility/StreamString.h"
#include "lldb/Utility/StringList.h"
#include "CommunicationKDP.h"
class ThreadKDP;
class ProcessKDP : public lldb_private::Process {
public:
// Constructors and Destructors
static lldb::ProcessSP
CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
const lldb_private::FileSpec *crash_file_path,
bool can_connect);
static void Initialize();
static void DebuggerInitialize(lldb_private::Debugger &debugger);
static void Terminate();
static llvm::StringRef GetPluginNameStatic() { return "kdp-remote"; }
static llvm::StringRef GetPluginDescriptionStatic();
// Constructors and Destructors
ProcessKDP(lldb::TargetSP target_sp, lldb::ListenerSP listener);
~ProcessKDP() override;
// Check if a given Process
bool CanDebug(lldb::TargetSP target_sp,
bool plugin_specified_by_name) override;
lldb_private::CommandObject *GetPluginCommandObject() override;
// Creating a new process, or attaching to an existing one
lldb_private::Status DoWillLaunch(lldb_private::Module *module) override;
lldb_private::Status
DoLaunch(lldb_private::Module *exe_module,
lldb_private::ProcessLaunchInfo &launch_info) override;
lldb_private::Status DoWillAttachToProcessWithID(lldb::pid_t pid) override;
lldb_private::Status
DoWillAttachToProcessWithName(const char *process_name,
bool wait_for_launch) override;
lldb_private::Status DoConnectRemote(llvm::StringRef remote_url) override;
lldb_private::Status DoAttachToProcessWithID(
lldb::pid_t pid,
const lldb_private::ProcessAttachInfo &attach_info) override;
lldb_private::Status DoAttachToProcessWithName(
const char *process_name,
const lldb_private::ProcessAttachInfo &attach_info) override;
void DidAttach(lldb_private::ArchSpec &process_arch) override;
lldb::addr_t GetImageInfoAddress() override;
lldb_private::DynamicLoader *GetDynamicLoader() override;
// PluginInterface protocol
llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
// Process Control
lldb_private::Status WillResume() override;
lldb_private::Status DoResume() override;
lldb_private::Status DoHalt(bool &caused_stop) override;
lldb_private::Status DoDetach(bool keep_stopped) override;
lldb_private::Status DoSignal(int signal) override;
lldb_private::Status DoDestroy() override;
void RefreshStateAfterStop() override;
// Process Queries
bool IsAlive() override;
// Process Memory
size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
lldb_private::Status &error) override;
size_t DoWriteMemory(lldb::addr_t addr, const void *buf, size_t size,
lldb_private::Status &error) override;
lldb::addr_t DoAllocateMemory(size_t size, uint32_t permissions,
lldb_private::Status &error) override;
lldb_private::Status DoDeallocateMemory(lldb::addr_t ptr) override;
// Process Breakpoints
lldb_private::Status
EnableBreakpointSite(lldb_private::BreakpointSite *bp_site) override;
lldb_private::Status
DisableBreakpointSite(lldb_private::BreakpointSite *bp_site) override;
CommunicationKDP &GetCommunication() { return m_comm; }
protected:
friend class ThreadKDP;
friend class CommunicationKDP;
// Accessors
bool IsRunning(lldb::StateType state) {
return state == lldb::eStateRunning || IsStepping(state);
}
bool IsStepping(lldb::StateType state) {
return state == lldb::eStateStepping;
}
bool CanResume(lldb::StateType state) { return state == lldb::eStateStopped; }
bool HasExited(lldb::StateType state) { return state == lldb::eStateExited; }
bool GetHostArchitecture(lldb_private::ArchSpec &arch);
bool ProcessIDIsValid() const;
void Clear();
bool DoUpdateThreadList(lldb_private::ThreadList &old_thread_list,
lldb_private::ThreadList &new_thread_list) override;
enum {
eBroadcastBitAsyncContinue = (1 << 0),
eBroadcastBitAsyncThreadShouldExit = (1 << 1)
};
lldb::ThreadSP GetKernelThread();
/// Broadcaster event bits definitions.
CommunicationKDP m_comm;
lldb_private::Broadcaster m_async_broadcaster;
lldb_private::HostThread m_async_thread;
llvm::StringRef m_dyld_plugin_name;
lldb::addr_t m_kernel_load_addr;
lldb::CommandObjectSP m_command_sp;
lldb::ThreadWP m_kernel_thread_wp;
bool StartAsyncThread();
void StopAsyncThread();
void *AsyncThread();
private:
// For ProcessKDP only
ProcessKDP(const ProcessKDP &) = delete;
const ProcessKDP &operator=(const ProcessKDP &) = delete;
};
#endif // LLDB_SOURCE_PLUGINS_PROCESS_MACOSX_KERNEL_PROCESSKDP_H