2022-05-30 19:35:01 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
2022-05-31 14:37:37 -04:00
|
|
|
#include <algorithm>
|
2022-05-30 19:35:01 -04:00
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include <boost/asio.hpp>
|
|
|
|
#include <boost/process/async_pipe.hpp>
|
|
|
|
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/thread.h"
|
|
|
|
#include "core/core.h"
|
|
|
|
#include "core/debugger/debugger.h"
|
|
|
|
#include "core/debugger/debugger_interface.h"
|
|
|
|
#include "core/debugger/gdbstub.h"
|
|
|
|
#include "core/hle/kernel/global_scheduler_context.h"
|
2022-07-07 20:06:46 -04:00
|
|
|
#include "core/hle/kernel/k_scheduler.h"
|
2022-05-30 19:35:01 -04:00
|
|
|
|
|
|
|
template <typename Readable, typename Buffer, typename Callback>
|
|
|
|
static void AsyncReceiveInto(Readable& r, Buffer& buffer, Callback&& c) {
|
|
|
|
static_assert(std::is_trivial_v<Buffer>);
|
|
|
|
auto boost_buffer{boost::asio::buffer(&buffer, sizeof(Buffer))};
|
2022-06-06 23:39:48 -04:00
|
|
|
r.async_read_some(
|
|
|
|
boost_buffer, [&, c](const boost::system::error_code& error, size_t bytes_read) {
|
|
|
|
if (!error.failed()) {
|
|
|
|
const u8* buffer_start = reinterpret_cast<const u8*>(&buffer);
|
|
|
|
std::span<const u8> received_data{buffer_start, buffer_start + bytes_read};
|
|
|
|
c(received_data);
|
2022-11-10 17:05:47 -05:00
|
|
|
AsyncReceiveInto(r, buffer, c);
|
2022-06-06 23:39:48 -04:00
|
|
|
}
|
|
|
|
});
|
2022-05-30 19:35:01 -04:00
|
|
|
}
|
|
|
|
|
2022-11-10 17:05:47 -05:00
|
|
|
template <typename Callback>
|
|
|
|
static void AsyncAccept(boost::asio::ip::tcp::acceptor& acceptor, Callback&& c) {
|
|
|
|
acceptor.async_accept([&, c](const boost::system::error_code& error, auto&& peer_socket) {
|
|
|
|
if (!error.failed()) {
|
|
|
|
c(peer_socket);
|
|
|
|
AsyncAccept(acceptor, c);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-30 19:35:01 -04:00
|
|
|
template <typename Readable, typename Buffer>
|
|
|
|
static std::span<const u8> ReceiveInto(Readable& r, Buffer& buffer) {
|
|
|
|
static_assert(std::is_trivial_v<Buffer>);
|
|
|
|
auto boost_buffer{boost::asio::buffer(&buffer, sizeof(Buffer))};
|
|
|
|
size_t bytes_read = r.read_some(boost_buffer);
|
|
|
|
const u8* buffer_start = reinterpret_cast<const u8*>(&buffer);
|
|
|
|
std::span<const u8> received_data{buffer_start, buffer_start + bytes_read};
|
|
|
|
return received_data;
|
|
|
|
}
|
|
|
|
|
2022-06-10 09:17:12 -04:00
|
|
|
enum class SignalType {
|
|
|
|
Stopped,
|
2022-06-06 12:56:01 -04:00
|
|
|
Watchpoint,
|
2022-06-10 09:17:12 -04:00
|
|
|
ShuttingDown,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SignalInfo {
|
|
|
|
SignalType type;
|
|
|
|
Kernel::KThread* thread;
|
2022-06-06 12:56:01 -04:00
|
|
|
const Kernel::DebugWatchpoint* watchpoint;
|
2022-06-10 09:17:12 -04:00
|
|
|
};
|
|
|
|
|
2022-05-30 19:35:01 -04:00
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
class DebuggerImpl : public DebuggerBackend {
|
|
|
|
public:
|
2022-11-10 17:05:47 -05:00
|
|
|
explicit DebuggerImpl(Core::System& system_, u16 port) : system{system_} {
|
2022-05-30 19:35:01 -04:00
|
|
|
InitializeServer(port);
|
|
|
|
}
|
|
|
|
|
2022-06-01 02:27:48 -04:00
|
|
|
~DebuggerImpl() override {
|
2022-05-30 19:35:01 -04:00
|
|
|
ShutdownServer();
|
|
|
|
}
|
|
|
|
|
2022-06-10 09:17:12 -04:00
|
|
|
bool SignalDebugger(SignalInfo signal_info) {
|
2022-11-10 17:05:47 -05:00
|
|
|
std::scoped_lock lk{connection_lock};
|
2022-05-30 19:35:01 -04:00
|
|
|
|
2022-11-10 17:05:47 -05:00
|
|
|
if (stopped || !state) {
|
|
|
|
// Do not notify the debugger about another event.
|
|
|
|
// It should be ignored.
|
|
|
|
return false;
|
2022-06-13 18:36:30 -04:00
|
|
|
}
|
2022-05-30 19:35:01 -04:00
|
|
|
|
2022-11-10 17:05:47 -05:00
|
|
|
// Set up the state.
|
|
|
|
stopped = true;
|
|
|
|
state->info = signal_info;
|
|
|
|
|
2022-06-10 09:17:12 -04:00
|
|
|
// Write a single byte into the pipe to wake up the debug interface.
|
2022-11-10 17:05:47 -05:00
|
|
|
boost::asio::write(state->signal_pipe, boost::asio::buffer(&stopped, sizeof(stopped)));
|
|
|
|
|
2022-05-30 19:35:01 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:05:47 -05:00
|
|
|
// These functions are callbacks from the frontend, and the lock will be held.
|
|
|
|
// There is no need to relock it.
|
|
|
|
|
2022-05-30 19:35:01 -04:00
|
|
|
std::span<const u8> ReadFromClient() override {
|
2022-11-10 17:05:47 -05:00
|
|
|
return ReceiveInto(state->client_socket, state->client_data);
|
2022-05-30 19:35:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void WriteToClient(std::span<const u8> data) override {
|
2022-11-10 17:05:47 -05:00
|
|
|
boost::asio::write(state->client_socket,
|
|
|
|
boost::asio::buffer(data.data(), data.size_bytes()));
|
2022-05-30 19:35:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetActiveThread(Kernel::KThread* thread) override {
|
2022-11-10 17:05:47 -05:00
|
|
|
state->active_thread = thread;
|
2022-05-30 19:35:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Kernel::KThread* GetActiveThread() override {
|
2022-11-10 17:05:47 -05:00
|
|
|
return state->active_thread;
|
2022-05-30 19:35:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void InitializeServer(u16 port) {
|
|
|
|
using boost::asio::ip::tcp;
|
|
|
|
|
|
|
|
LOG_INFO(Debug_GDBStub, "Starting server on port {}...", port);
|
|
|
|
|
|
|
|
// Run the connection thread.
|
2022-05-31 14:37:37 -04:00
|
|
|
connection_thread = std::jthread([&, port](std::stop_token stop_token) {
|
2022-11-10 17:05:47 -05:00
|
|
|
Common::SetCurrentThreadName("Debugger");
|
|
|
|
|
2022-05-30 19:35:01 -04:00
|
|
|
try {
|
2022-05-31 14:37:37 -04:00
|
|
|
// Initialize the listening socket and accept a new client.
|
2022-06-12 11:50:50 -04:00
|
|
|
tcp::endpoint endpoint{boost::asio::ip::address_v4::any(), port};
|
2022-05-31 14:37:37 -04:00
|
|
|
tcp::acceptor acceptor{io_context, endpoint};
|
|
|
|
|
2022-11-10 17:05:47 -05:00
|
|
|
AsyncAccept(acceptor, [&](auto&& peer) { AcceptConnection(std::move(peer)); });
|
2022-05-31 14:37:37 -04:00
|
|
|
|
2022-11-10 17:05:47 -05:00
|
|
|
while (!stop_token.stop_requested() && io_context.run()) {
|
2022-05-31 14:37:37 -04:00
|
|
|
}
|
2022-05-30 19:35:01 -04:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
LOG_CRITICAL(Debug_GDBStub, "Stopping server: {}", ex.what());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:05:47 -05:00
|
|
|
void AcceptConnection(boost::asio::ip::tcp::socket&& peer) {
|
|
|
|
LOG_INFO(Debug_GDBStub, "Accepting new peer connection");
|
2022-05-30 19:35:01 -04:00
|
|
|
|
2022-11-10 17:05:47 -05:00
|
|
|
std::scoped_lock lk{connection_lock};
|
|
|
|
|
|
|
|
// Ensure everything is stopped.
|
|
|
|
PauseEmulation();
|
|
|
|
|
|
|
|
// Set up the new frontend.
|
|
|
|
frontend = std::make_unique<GDBStub>(*this, system);
|
|
|
|
|
|
|
|
// Set the new state. This will tear down any existing state.
|
|
|
|
state = ConnectionState{
|
|
|
|
.client_socket{std::move(peer)},
|
|
|
|
.signal_pipe{io_context},
|
|
|
|
.info{},
|
|
|
|
.active_thread{},
|
|
|
|
.client_data{},
|
|
|
|
.pipe_data{},
|
|
|
|
};
|
2022-05-30 19:35:01 -04:00
|
|
|
|
|
|
|
// Set up the client signals for new data.
|
2022-11-10 17:05:47 -05:00
|
|
|
AsyncReceiveInto(state->signal_pipe, state->pipe_data, [&](auto d) { PipeData(d); });
|
|
|
|
AsyncReceiveInto(state->client_socket, state->client_data, [&](auto d) { ClientData(d); });
|
2022-05-30 19:35:01 -04:00
|
|
|
|
|
|
|
// Set the active thread.
|
2022-05-31 14:37:37 -04:00
|
|
|
UpdateActiveThread();
|
2022-05-30 19:35:01 -04:00
|
|
|
|
|
|
|
// Set up the frontend.
|
|
|
|
frontend->Connected();
|
2022-11-10 17:05:47 -05:00
|
|
|
}
|
2022-05-30 19:35:01 -04:00
|
|
|
|
2022-11-10 17:05:47 -05:00
|
|
|
void ShutdownServer() {
|
|
|
|
connection_thread.request_stop();
|
|
|
|
io_context.stop();
|
|
|
|
connection_thread.join();
|
2022-05-30 19:35:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void PipeData(std::span<const u8> data) {
|
2022-11-10 17:05:47 -05:00
|
|
|
std::scoped_lock lk{connection_lock};
|
|
|
|
|
|
|
|
switch (state->info.type) {
|
2022-06-10 09:17:12 -04:00
|
|
|
case SignalType::Stopped:
|
2022-06-06 12:56:01 -04:00
|
|
|
case SignalType::Watchpoint:
|
2022-06-10 09:17:12 -04:00
|
|
|
// Stop emulation.
|
2022-06-13 18:36:30 -04:00
|
|
|
PauseEmulation();
|
2022-06-10 09:17:12 -04:00
|
|
|
|
|
|
|
// Notify the client.
|
2022-11-10 17:05:47 -05:00
|
|
|
state->active_thread = state->info.thread;
|
2022-06-10 09:17:12 -04:00
|
|
|
UpdateActiveThread();
|
2022-06-06 12:56:01 -04:00
|
|
|
|
2022-11-10 17:05:47 -05:00
|
|
|
if (state->info.type == SignalType::Watchpoint) {
|
|
|
|
frontend->Watchpoint(state->active_thread, *state->info.watchpoint);
|
2022-06-06 12:56:01 -04:00
|
|
|
} else {
|
2022-11-10 17:05:47 -05:00
|
|
|
frontend->Stopped(state->active_thread);
|
2022-06-06 12:56:01 -04:00
|
|
|
}
|
2022-06-10 09:17:12 -04:00
|
|
|
|
|
|
|
break;
|
|
|
|
case SignalType::ShuttingDown:
|
|
|
|
frontend->ShuttingDown();
|
|
|
|
|
|
|
|
// Wait for emulation to shut down gracefully now.
|
2022-11-10 17:05:47 -05:00
|
|
|
state->signal_pipe.close();
|
|
|
|
state->client_socket.shutdown(boost::asio::socket_base::shutdown_both);
|
2022-06-10 09:17:12 -04:00
|
|
|
LOG_INFO(Debug_GDBStub, "Shut down server");
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2022-05-30 19:35:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClientData(std::span<const u8> data) {
|
2022-11-10 17:05:47 -05:00
|
|
|
std::scoped_lock lk{connection_lock};
|
|
|
|
|
2022-05-30 19:35:01 -04:00
|
|
|
const auto actions{frontend->ClientData(data)};
|
|
|
|
for (const auto action : actions) {
|
|
|
|
switch (action) {
|
|
|
|
case DebuggerAction::Interrupt: {
|
2022-11-10 17:05:47 -05:00
|
|
|
stopped = true;
|
2022-06-13 18:36:30 -04:00
|
|
|
PauseEmulation();
|
2022-05-31 14:37:37 -04:00
|
|
|
UpdateActiveThread();
|
2022-11-10 17:05:47 -05:00
|
|
|
frontend->Stopped(state->active_thread);
|
2022-05-30 19:35:01 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DebuggerAction::Continue:
|
2022-06-13 18:36:30 -04:00
|
|
|
MarkResumed([&] { ResumeEmulation(); });
|
2022-05-30 19:35:01 -04:00
|
|
|
break;
|
2022-05-31 14:37:37 -04:00
|
|
|
case DebuggerAction::StepThreadUnlocked:
|
2022-06-13 18:36:30 -04:00
|
|
|
MarkResumed([&] {
|
2022-11-10 17:05:47 -05:00
|
|
|
state->active_thread->SetStepState(Kernel::StepState::StepPending);
|
|
|
|
state->active_thread->Resume(Kernel::SuspendType::Debug);
|
|
|
|
ResumeEmulation(state->active_thread);
|
2022-06-13 18:36:30 -04:00
|
|
|
});
|
2022-05-31 14:37:37 -04:00
|
|
|
break;
|
2022-06-13 18:36:30 -04:00
|
|
|
case DebuggerAction::StepThreadLocked: {
|
|
|
|
MarkResumed([&] {
|
2022-11-10 17:05:47 -05:00
|
|
|
state->active_thread->SetStepState(Kernel::StepState::StepPending);
|
|
|
|
state->active_thread->Resume(Kernel::SuspendType::Debug);
|
2022-06-13 18:36:30 -04:00
|
|
|
});
|
2022-05-30 19:35:01 -04:00
|
|
|
break;
|
2022-06-13 18:36:30 -04:00
|
|
|
}
|
2022-05-30 19:35:01 -04:00
|
|
|
case DebuggerAction::ShutdownEmulation: {
|
|
|
|
// Spawn another thread that will exit after shutdown,
|
|
|
|
// to avoid a deadlock
|
|
|
|
Core::System* system_ref{&system};
|
|
|
|
std::thread t([system_ref] { system_ref->Exit(); });
|
|
|
|
t.detach();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 18:36:30 -04:00
|
|
|
void PauseEmulation() {
|
2022-07-07 20:06:46 -04:00
|
|
|
Kernel::KScopedSchedulerLock sl{system.Kernel()};
|
|
|
|
|
2022-06-13 18:36:30 -04:00
|
|
|
// Put all threads to sleep on next scheduler round.
|
|
|
|
for (auto* thread : ThreadList()) {
|
|
|
|
thread->RequestSuspend(Kernel::SuspendType::Debug);
|
2022-05-30 19:35:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 18:36:30 -04:00
|
|
|
void ResumeEmulation(Kernel::KThread* except = nullptr) {
|
|
|
|
// Wake up all threads.
|
2022-05-30 19:35:01 -04:00
|
|
|
for (auto* thread : ThreadList()) {
|
2022-06-13 18:36:30 -04:00
|
|
|
if (thread == except) {
|
|
|
|
continue;
|
2022-05-30 19:35:01 -04:00
|
|
|
}
|
2022-06-13 18:36:30 -04:00
|
|
|
|
|
|
|
thread->SetStepState(Kernel::StepState::NotStepping);
|
|
|
|
thread->Resume(Kernel::SuspendType::Debug);
|
2022-05-30 19:35:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 18:36:30 -04:00
|
|
|
template <typename Callback>
|
|
|
|
void MarkResumed(Callback&& cb) {
|
2022-07-07 20:06:46 -04:00
|
|
|
Kernel::KScopedSchedulerLock sl{system.Kernel()};
|
2022-06-13 18:36:30 -04:00
|
|
|
stopped = false;
|
|
|
|
cb();
|
2022-05-30 19:35:01 -04:00
|
|
|
}
|
|
|
|
|
2022-05-31 14:37:37 -04:00
|
|
|
void UpdateActiveThread() {
|
|
|
|
const auto& threads{ThreadList()};
|
2022-11-10 17:05:47 -05:00
|
|
|
if (std::find(threads.begin(), threads.end(), state->active_thread) == threads.end()) {
|
|
|
|
state->active_thread = threads[0];
|
2022-05-31 14:37:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 19:35:01 -04:00
|
|
|
const std::vector<Kernel::KThread*>& ThreadList() {
|
|
|
|
return system.GlobalSchedulerContext().GetThreadList();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
System& system;
|
|
|
|
std::unique_ptr<DebuggerFrontend> frontend;
|
|
|
|
|
2022-11-10 17:05:47 -05:00
|
|
|
boost::asio::io_context io_context;
|
2022-05-30 19:35:01 -04:00
|
|
|
std::jthread connection_thread;
|
|
|
|
std::mutex connection_lock;
|
|
|
|
|
2022-11-10 17:05:47 -05:00
|
|
|
struct ConnectionState {
|
|
|
|
boost::asio::ip::tcp::socket client_socket;
|
|
|
|
boost::process::async_pipe signal_pipe;
|
|
|
|
|
|
|
|
SignalInfo info;
|
|
|
|
Kernel::KThread* active_thread;
|
|
|
|
std::array<u8, 4096> client_data;
|
|
|
|
bool pipe_data;
|
|
|
|
};
|
2022-05-30 19:35:01 -04:00
|
|
|
|
2022-11-10 17:05:47 -05:00
|
|
|
std::optional<ConnectionState> state{};
|
|
|
|
bool stopped{};
|
2022-05-30 19:35:01 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Debugger::Debugger(Core::System& system, u16 port) {
|
|
|
|
try {
|
|
|
|
impl = std::make_unique<DebuggerImpl>(system, port);
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
LOG_CRITICAL(Debug_GDBStub, "Failed to initialize debugger: {}", ex.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Debugger::~Debugger() = default;
|
|
|
|
|
|
|
|
bool Debugger::NotifyThreadStopped(Kernel::KThread* thread) {
|
2022-06-06 12:56:01 -04:00
|
|
|
return impl && impl->SignalDebugger(SignalInfo{SignalType::Stopped, thread, nullptr});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Debugger::NotifyThreadWatchpoint(Kernel::KThread* thread,
|
|
|
|
const Kernel::DebugWatchpoint& watch) {
|
|
|
|
return impl && impl->SignalDebugger(SignalInfo{SignalType::Watchpoint, thread, &watch});
|
2022-06-10 09:17:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Debugger::NotifyShutdown() {
|
|
|
|
if (impl) {
|
2022-06-06 12:56:01 -04:00
|
|
|
impl->SignalDebugger(SignalInfo{SignalType::ShuttingDown, nullptr, nullptr});
|
2022-06-10 09:17:12 -04:00
|
|
|
}
|
2022-05-30 19:35:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Core
|