blob: 794369db6e8a96a168ad948824a01e7212e5f300 [file]
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SRC_DEVELOPER_DEBUG_DEBUG_AGENT_MOCK_THREAD_HANDLE_H_
#define SRC_DEVELOPER_DEBUG_DEBUG_AGENT_MOCK_THREAD_HANDLE_H_
#include <lib/fit/function.h>
#include <map>
#include <memory>
#include <utility>
#include "src/developer/debug/debug_agent/thread_handle.h"
namespace debug_agent {
class MockThreadHandle final : public ThreadHandle {
public:
struct WatchpointInstallation {
debug_ipc::BreakpointType type;
debug::AddressRange address_range;
};
explicit MockThreadHandle(zx_koid_t thread_koid, std::string name = std::string());
// Sets the underlying thread state. Active thread-level or process-level suspension overrides
// GetState to kSuspended unless the thread is blocked on an exception, dying, or dead.
void set_state(State s) { *state_ = s; }
void set_in_thread_starting_exception(bool value) { in_thread_starting_exception_ = value; }
// Sets the values to be returned for the given register category query.
void SetRegisterCategory(debug::RegisterCategory cat, std::vector<debug::RegisterValue> values);
// Sets the information to return for the next watchpoint set.
void set_watchpoint_range_to_return(debug::AddressRange r) { watchpoint_range_to_return_ = r; }
void set_watchpoint_slot_to_return(int slot) { watchpoint_slot_to_return_ = slot; }
// Returns the number of breakpoint installs/uninstalls for the given address / total.
size_t BreakpointInstallCount(uint64_t address) const;
size_t TotalBreakpointInstallCalls() const;
size_t BreakpointUninstallCount(uint64_t address) const;
size_t TotalBreakpointUninstallCalls() const;
// Log of all watchpoint additions.
const std::vector<WatchpointInstallation>& watchpoint_installs() const {
return watchpoint_installs_;
}
// Returns the number of watchpoint installs/uninstalls for the given address / total.
size_t WatchpointInstallCount(const debug::AddressRange&) const;
size_t TotalWatchpointInstallCalls() const;
size_t WatchpointUninstallCount(const debug::AddressRange&) const;
size_t TotalWatchpointUninstallCalls() const;
// Returns the suspend count for implementing reference-counted suspension via MockSuspendHandle.
int suspend_count() const { return *suspend_count_; }
bool is_suspended() const { return suspend_count() > 0 || is_process_suspended(); }
// Attaches a shared process-level suspend count from MockProcessHandle so process suspension
// is reflected live in GetState(), is_suspended(), and WaitForSuspensionSignal().
void set_process_suspend_count(std::shared_ptr<int> count) {
process_suspend_count_ = std::move(count);
}
void set_suspend_fails(bool fails) { *suspend_fails_ = fails; }
void set_on_wait_for_suspension(fit::function<bool()> cb) {
*on_wait_for_suspension_ = std::move(cb);
}
int suspend_calls() const { return *suspend_calls_; }
int wait_for_suspension_calls() const { return *wait_for_suspension_calls_; }
bool single_step() const { return single_step_; }
// ThreadHandle implementation.
const NativeThreadHandle& GetNativeHandle() const override { return null_handle_; }
NativeThreadHandle& GetNativeHandle() override { return null_handle_; }
zx_koid_t GetKoid() const override { return thread_koid_; }
std::string GetName() const override { return name_; }
State GetState() const override;
bool IsInThreadStartingException() const override { return in_thread_starting_exception_; }
debug_ipc::ThreadRecord GetThreadRecord(zx_koid_t process_koid) const override;
std::unique_ptr<SuspendHandle> Suspend() override;
bool WaitForSuspension(TickTimePoint deadline) const override;
bool WaitForSuspensionSignal(TickTimePoint deadline) const override;
std::optional<GeneralRegisters> GetGeneralRegisters() const override;
void SetGeneralRegisters(const GeneralRegisters& regs) override;
std::optional<DebugRegisters> GetDebugRegisters() const override;
bool SetDebugRegisters(const DebugRegisters& regs) override;
void SetSingleStep(bool single_step) override;
std::vector<debug::RegisterValue> ReadRegisters(
const std::vector<debug::RegisterCategory>& cats_to_get) const override;
std::vector<debug::RegisterValue> WriteRegisters(
const std::vector<debug::RegisterValue>& regs) override;
bool InstallHWBreakpoint(uint64_t address) override;
bool UninstallHWBreakpoint(uint64_t address) override;
std::optional<WatchpointInfo> InstallWatchpoint(debug_ipc::BreakpointType type,
const debug::AddressRange& range) override;
bool UninstallWatchpoint(const debug::AddressRange& range) override;
private:
// Always null, for returning only from the getters above.
// TODO(brettw) Remove this when the ThreadHandle no longer exposes a zx::thread getter.
static NativeThreadHandle null_handle_;
zx_koid_t thread_koid_;
std::string name_;
std::vector<debug::RegisterValue> registers_[std::to_underlying(debug::RegisterCategory::kLast)];
std::shared_ptr<State> state_ = std::make_shared<State>();
bool in_thread_starting_exception_ = false;
bool single_step_ = false;
GeneralRegisters general_registers_;
DebugRegisters debug_registers_;
debug::AddressRange watchpoint_range_to_return_;
int watchpoint_slot_to_return_ = 0;
std::map<uint64_t, size_t> bp_installs_;
std::map<uint64_t, size_t> bp_uninstalls_;
std::vector<WatchpointInstallation> watchpoint_installs_;
std::map<debug::AddressRange, size_t, debug::AddressRangeBeginCmp> wp_installs_;
std::map<debug::AddressRange, size_t, debug::AddressRangeBeginCmp> wp_uninstalls_;
bool is_process_suspended() const;
// Shared count modified by MockSuspendHandles. Positive indicates this thread is suspended.
std::shared_ptr<int> suspend_count_;
// Optional shared count owned by MockProcessHandle.
std::shared_ptr<int> process_suspend_count_;
std::shared_ptr<bool> suspend_fails_ = std::make_shared<bool>(false);
std::shared_ptr<int> suspend_calls_ = std::make_shared<int>(0);
std::shared_ptr<int> wait_for_suspension_calls_ = std::make_shared<int>(0);
std::shared_ptr<fit::function<bool()>> on_wait_for_suspension_ =
std::make_shared<fit::function<bool()>>();
};
} // namespace debug_agent
#endif // SRC_DEVELOPER_DEBUG_DEBUG_AGENT_MOCK_THREAD_HANDLE_H_