blob: ddd2d92e66aafa6edee6333e2171a0138418c46c [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.
#include "src/developer/debug/debug_agent/mock_thread_handle.h"
#include <lib/syslog/cpp/macros.h>
#include <utility>
#include "src/developer/debug/debug_agent/mock_suspend_handle.h"
namespace debug_agent {
namespace {
bool CanSuspendState(const ThreadHandle::State& state) {
return !state.is_blocked_on_exception() &&
state.state != debug_ipc::ThreadRecord::State::kDying &&
state.state != debug_ipc::ThreadRecord::State::kDead;
}
} // namespace
NativeThreadHandle MockThreadHandle::null_handle_;
MockThreadHandle::MockThreadHandle(zx_koid_t thread_koid, std::string name)
: thread_koid_(thread_koid), name_(std::move(name)), suspend_count_(std::make_shared<int>(0)) {
// Tests could accidentally write to this handle since it's returned as a mutable value in some
// cases. Catch accidents like that.
FX_DCHECK(!null_handle_);
}
void MockThreadHandle::SetRegisterCategory(debug::RegisterCategory cat,
std::vector<debug::RegisterValue> values) {
FX_CHECK(std::to_underlying(cat) < std::size(registers_));
registers_[std::to_underlying(cat)] = std::move(values);
}
size_t MockThreadHandle::BreakpointInstallCount(uint64_t address) const {
auto it = bp_installs_.find(address);
if (it == bp_installs_.end())
return 0;
return it->second;
}
size_t MockThreadHandle::TotalBreakpointInstallCalls() const {
size_t total = 0;
for (auto it : bp_installs_)
total += it.second;
return total;
}
size_t MockThreadHandle::BreakpointUninstallCount(uint64_t address) const {
auto it = bp_uninstalls_.find(address);
if (it == bp_uninstalls_.end())
return 0;
return it->second;
}
size_t MockThreadHandle::TotalBreakpointUninstallCalls() const {
size_t total = 0;
for (auto it : bp_uninstalls_)
total += it.second;
return total;
}
size_t MockThreadHandle::WatchpointInstallCount(const debug::AddressRange& range) const {
auto it = wp_installs_.find(range);
if (it == wp_installs_.end())
return 0;
return it->second;
}
size_t MockThreadHandle::TotalWatchpointInstallCalls() const {
size_t total = 0;
for (auto it : wp_installs_)
total += it.second;
return total;
}
size_t MockThreadHandle::WatchpointUninstallCount(const debug::AddressRange& range) const {
auto it = wp_uninstalls_.find(range);
if (it == wp_uninstalls_.end())
return 0;
return it->second;
}
size_t MockThreadHandle::TotalWatchpointUninstallCalls() const {
size_t total = 0;
for (auto it : wp_uninstalls_)
total += it.second;
return total;
}
bool MockThreadHandle::is_process_suspended() const {
if (!process_suspend_count_) {
return false;
}
const int count = *process_suspend_count_;
FX_DCHECK(count >= 0);
return count > 0 && CanSuspendState(*state_);
}
ThreadHandle::State MockThreadHandle::GetState() const {
const int thread_count = suspend_count();
FX_DCHECK(thread_count >= 0);
if ((thread_count > 0 && CanSuspendState(*state_)) || is_process_suspended()) {
return State(debug_ipc::ThreadRecord::State::kSuspended);
}
return *state_;
}
debug_ipc::ThreadRecord MockThreadHandle::GetThreadRecord(zx_koid_t process_koid) const {
State state = GetState();
debug_ipc::ThreadRecord record;
record.id = {.process = process_koid, .thread = thread_koid_};
record.name = name_.empty() ? "test thread" : name_;
record.state = state.state;
record.blocked_reason = state.blocked_reason;
return record;
}
std::unique_ptr<SuspendHandle> MockThreadHandle::Suspend() {
++(*suspend_calls_);
if (*suspend_fails_) {
return nullptr;
}
return std::make_unique<MockSuspendHandle>(suspend_count_);
}
bool MockThreadHandle::WaitForSuspension(TickTimePoint deadline) const {
++(*wait_for_suspension_calls_);
if (*on_wait_for_suspension_) {
return (*on_wait_for_suspension_)();
}
// Existing mock callers assume WaitForSuspension succeeds immediately without explicitly
// requiring an active suspend handle or exception state, whereas WaitForSuspensionSignal checks
// GetState() == kSuspended so tests exercising exception-release transitions (e.g.,
// OnThreadStarting) observe false until the thread actually leaves the exception.
return true;
}
bool MockThreadHandle::WaitForSuspensionSignal(TickTimePoint) const {
return GetState().state == debug_ipc::ThreadRecord::State::kSuspended;
}
std::optional<GeneralRegisters> MockThreadHandle::GetGeneralRegisters() const {
return general_registers_;
}
void MockThreadHandle::SetGeneralRegisters(const GeneralRegisters& regs) {
general_registers_ = regs;
}
std::optional<DebugRegisters> MockThreadHandle::GetDebugRegisters() const {
return debug_registers_;
}
bool MockThreadHandle::SetDebugRegisters(const DebugRegisters& regs) {
debug_registers_ = regs;
return true;
}
void MockThreadHandle::SetSingleStep(bool single_step) { single_step_ = single_step; }
std::vector<debug::RegisterValue> MockThreadHandle::ReadRegisters(
const std::vector<debug::RegisterCategory>& cats_to_get) const {
std::vector<debug::RegisterValue> result;
for (const auto cat : cats_to_get) {
FX_CHECK(std::to_underlying(cat) < std::size(registers_));
const auto& source = registers_[std::to_underlying(cat)];
result.insert(result.end(), source.begin(), source.end());
}
return result;
}
std::vector<debug::RegisterValue> MockThreadHandle::WriteRegisters(
const std::vector<debug::RegisterValue>& regs) {
// Return the same values as the input to pretend the write succeeded.
return regs;
}
bool MockThreadHandle::InstallHWBreakpoint(uint64_t address) {
bp_installs_[address]++;
return true;
}
bool MockThreadHandle::UninstallHWBreakpoint(uint64_t address) {
bp_uninstalls_[address]++;
return true;
}
std::optional<WatchpointInfo> MockThreadHandle::InstallWatchpoint(
debug_ipc::BreakpointType type, const debug::AddressRange& range) {
watchpoint_installs_.push_back(WatchpointInstallation{.type = type, .address_range = range});
wp_installs_[range]++;
return WatchpointInfo(watchpoint_range_to_return_, watchpoint_slot_to_return_);
}
bool MockThreadHandle::UninstallWatchpoint(const debug::AddressRange& range) {
wp_uninstalls_[range]++;
return true;
}
} // namespace debug_agent