diff options
| author | 2022-06-01 00:19:49 -0400 | |
|---|---|---|
| committer | 2022-06-01 00:19:49 -0400 | |
| commit | de2f2e5140eb85311e0fd844c580d6726adf7e03 (patch) | |
| tree | 10f0e96a0689b2edb823f5833ff388ac9c645229 /src/core/debugger | |
| parent | Merge pull request #8401 from zhaobot/tx-update-20220601034505 (diff) | |
| parent | core/debugger: Implement new GDB stub debugger (diff) | |
| download | yuzu-de2f2e5140eb85311e0fd844c580d6726adf7e03.tar.gz yuzu-de2f2e5140eb85311e0fd844c580d6726adf7e03.tar.xz yuzu-de2f2e5140eb85311e0fd844c580d6726adf7e03.zip | |
Merge pull request #8394 from liamwhite/debugger
core/debugger: Implement new GDB stub debugger
Diffstat (limited to 'src/core/debugger')
| -rw-r--r-- | src/core/debugger/debugger.cpp | 259 | ||||
| -rw-r--r-- | src/core/debugger/debugger.h | 46 | ||||
| -rw-r--r-- | src/core/debugger/debugger_interface.h | 74 | ||||
| -rw-r--r-- | src/core/debugger/gdbstub.cpp | 382 | ||||
| -rw-r--r-- | src/core/debugger/gdbstub.h | 47 | ||||
| -rw-r--r-- | src/core/debugger/gdbstub_arch.cpp | 406 | ||||
| -rw-r--r-- | src/core/debugger/gdbstub_arch.h | 67 |
7 files changed, 1281 insertions, 0 deletions
diff --git a/src/core/debugger/debugger.cpp b/src/core/debugger/debugger.cpp new file mode 100644 index 000000000..7a2012d3c --- /dev/null +++ b/src/core/debugger/debugger.cpp | |||
| @@ -0,0 +1,259 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #include <mutex> | ||
| 5 | #include <thread> | ||
| 6 | |||
| 7 | #include <boost/asio.hpp> | ||
| 8 | #include <boost/process/async_pipe.hpp> | ||
| 9 | |||
| 10 | #include "common/logging/log.h" | ||
| 11 | #include "common/thread.h" | ||
| 12 | #include "core/core.h" | ||
| 13 | #include "core/debugger/debugger.h" | ||
| 14 | #include "core/debugger/debugger_interface.h" | ||
| 15 | #include "core/debugger/gdbstub.h" | ||
| 16 | #include "core/hle/kernel/global_scheduler_context.h" | ||
| 17 | |||
| 18 | template <typename Readable, typename Buffer, typename Callback> | ||
| 19 | static void AsyncReceiveInto(Readable& r, Buffer& buffer, Callback&& c) { | ||
| 20 | static_assert(std::is_trivial_v<Buffer>); | ||
| 21 | auto boost_buffer{boost::asio::buffer(&buffer, sizeof(Buffer))}; | ||
| 22 | r.async_read_some(boost_buffer, [&](const boost::system::error_code& error, size_t bytes_read) { | ||
| 23 | if (!error.failed()) { | ||
| 24 | const u8* buffer_start = reinterpret_cast<const u8*>(&buffer); | ||
| 25 | std::span<const u8> received_data{buffer_start, buffer_start + bytes_read}; | ||
| 26 | c(received_data); | ||
| 27 | } | ||
| 28 | |||
| 29 | AsyncReceiveInto(r, buffer, c); | ||
| 30 | }); | ||
| 31 | } | ||
| 32 | |||
| 33 | template <typename Readable, typename Buffer> | ||
| 34 | static std::span<const u8> ReceiveInto(Readable& r, Buffer& buffer) { | ||
| 35 | static_assert(std::is_trivial_v<Buffer>); | ||
| 36 | auto boost_buffer{boost::asio::buffer(&buffer, sizeof(Buffer))}; | ||
| 37 | size_t bytes_read = r.read_some(boost_buffer); | ||
| 38 | const u8* buffer_start = reinterpret_cast<const u8*>(&buffer); | ||
| 39 | std::span<const u8> received_data{buffer_start, buffer_start + bytes_read}; | ||
| 40 | return received_data; | ||
| 41 | } | ||
| 42 | |||
| 43 | namespace Core { | ||
| 44 | |||
| 45 | class DebuggerImpl : public DebuggerBackend { | ||
| 46 | public: | ||
| 47 | explicit DebuggerImpl(Core::System& system_, u16 port) | ||
| 48 | : system{system_}, signal_pipe{io_context}, client_socket{io_context} { | ||
| 49 | frontend = std::make_unique<GDBStub>(*this, system); | ||
| 50 | InitializeServer(port); | ||
| 51 | } | ||
| 52 | |||
| 53 | ~DebuggerImpl() { | ||
| 54 | ShutdownServer(); | ||
| 55 | } | ||
| 56 | |||
| 57 | bool NotifyThreadStopped(Kernel::KThread* thread) { | ||
| 58 | std::scoped_lock lk{connection_lock}; | ||
| 59 | |||
| 60 | if (stopped) { | ||
| 61 | // Do not notify the debugger about another event. | ||
| 62 | // It should be ignored. | ||
| 63 | return false; | ||
| 64 | } | ||
| 65 | stopped = true; | ||
| 66 | |||
| 67 | signal_pipe.write_some(boost::asio::buffer(&thread, sizeof(thread))); | ||
| 68 | return true; | ||
| 69 | } | ||
| 70 | |||
| 71 | std::span<const u8> ReadFromClient() override { | ||
| 72 | return ReceiveInto(client_socket, client_data); | ||
| 73 | } | ||
| 74 | |||
| 75 | void WriteToClient(std::span<const u8> data) override { | ||
| 76 | client_socket.write_some(boost::asio::buffer(data.data(), data.size_bytes())); | ||
| 77 | } | ||
| 78 | |||
| 79 | void SetActiveThread(Kernel::KThread* thread) override { | ||
| 80 | active_thread = thread; | ||
| 81 | } | ||
| 82 | |||
| 83 | Kernel::KThread* GetActiveThread() override { | ||
| 84 | return active_thread; | ||
| 85 | } | ||
| 86 | |||
| 87 | bool IsStepping() const { | ||
| 88 | return stepping; | ||
| 89 | } | ||
| 90 | |||
| 91 | private: | ||
| 92 | void InitializeServer(u16 port) { | ||
| 93 | using boost::asio::ip::tcp; | ||
| 94 | |||
| 95 | LOG_INFO(Debug_GDBStub, "Starting server on port {}...", port); | ||
| 96 | |||
| 97 | // Initialize the listening socket and accept a new client. | ||
| 98 | tcp::endpoint endpoint{boost::asio::ip::address_v4::loopback(), port}; | ||
| 99 | tcp::acceptor acceptor{io_context, endpoint}; | ||
| 100 | client_socket = acceptor.accept(); | ||
| 101 | |||
| 102 | // Run the connection thread. | ||
| 103 | connection_thread = std::jthread([&](std::stop_token stop_token) { | ||
| 104 | try { | ||
| 105 | ThreadLoop(stop_token); | ||
| 106 | } catch (const std::exception& ex) { | ||
| 107 | LOG_CRITICAL(Debug_GDBStub, "Stopping server: {}", ex.what()); | ||
| 108 | } | ||
| 109 | |||
| 110 | client_socket.shutdown(client_socket.shutdown_both); | ||
| 111 | client_socket.close(); | ||
| 112 | }); | ||
| 113 | } | ||
| 114 | |||
| 115 | void ShutdownServer() { | ||
| 116 | connection_thread.request_stop(); | ||
| 117 | io_context.stop(); | ||
| 118 | connection_thread.join(); | ||
| 119 | } | ||
| 120 | |||
| 121 | void ThreadLoop(std::stop_token stop_token) { | ||
| 122 | Common::SetCurrentThreadName("yuzu:Debugger"); | ||
| 123 | |||
| 124 | // Set up the client signals for new data. | ||
| 125 | AsyncReceiveInto(signal_pipe, active_thread, [&](auto d) { PipeData(d); }); | ||
| 126 | AsyncReceiveInto(client_socket, client_data, [&](auto d) { ClientData(d); }); | ||
| 127 | |||
| 128 | // Stop the emulated CPU. | ||
| 129 | AllCoreStop(); | ||
| 130 | |||
| 131 | // Set the active thread. | ||
| 132 | active_thread = ThreadList()[0]; | ||
| 133 | active_thread->Resume(Kernel::SuspendType::Debug); | ||
| 134 | |||
| 135 | // Set up the frontend. | ||
| 136 | frontend->Connected(); | ||
| 137 | |||
| 138 | // Main event loop. | ||
| 139 | while (!stop_token.stop_requested() && io_context.run()) { | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | void PipeData(std::span<const u8> data) { | ||
| 144 | AllCoreStop(); | ||
| 145 | active_thread->Resume(Kernel::SuspendType::Debug); | ||
| 146 | frontend->Stopped(active_thread); | ||
| 147 | } | ||
| 148 | |||
| 149 | void ClientData(std::span<const u8> data) { | ||
| 150 | const auto actions{frontend->ClientData(data)}; | ||
| 151 | for (const auto action : actions) { | ||
| 152 | switch (action) { | ||
| 153 | case DebuggerAction::Interrupt: { | ||
| 154 | { | ||
| 155 | std::scoped_lock lk{connection_lock}; | ||
| 156 | stopped = true; | ||
| 157 | } | ||
| 158 | AllCoreStop(); | ||
| 159 | active_thread = ThreadList()[0]; | ||
| 160 | active_thread->Resume(Kernel::SuspendType::Debug); | ||
| 161 | frontend->Stopped(active_thread); | ||
| 162 | break; | ||
| 163 | } | ||
| 164 | case DebuggerAction::Continue: | ||
| 165 | stepping = false; | ||
| 166 | ResumeInactiveThreads(); | ||
| 167 | AllCoreResume(); | ||
| 168 | break; | ||
| 169 | case DebuggerAction::StepThread: | ||
| 170 | stepping = true; | ||
| 171 | SuspendInactiveThreads(); | ||
| 172 | AllCoreResume(); | ||
| 173 | break; | ||
| 174 | case DebuggerAction::ShutdownEmulation: { | ||
| 175 | // Suspend all threads and release any locks held | ||
| 176 | active_thread->RequestSuspend(Kernel::SuspendType::Debug); | ||
| 177 | SuspendInactiveThreads(); | ||
| 178 | AllCoreResume(); | ||
| 179 | |||
| 180 | // Spawn another thread that will exit after shutdown, | ||
| 181 | // to avoid a deadlock | ||
| 182 | Core::System* system_ref{&system}; | ||
| 183 | std::thread t([system_ref] { system_ref->Exit(); }); | ||
| 184 | t.detach(); | ||
| 185 | break; | ||
| 186 | } | ||
| 187 | } | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | void AllCoreStop() { | ||
| 192 | if (!suspend) { | ||
| 193 | suspend = system.StallCPU(); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | void AllCoreResume() { | ||
| 198 | stopped = false; | ||
| 199 | system.UnstallCPU(); | ||
| 200 | suspend.reset(); | ||
| 201 | } | ||
| 202 | |||
| 203 | void SuspendInactiveThreads() { | ||
| 204 | for (auto* thread : ThreadList()) { | ||
| 205 | if (thread != active_thread) { | ||
| 206 | thread->RequestSuspend(Kernel::SuspendType::Debug); | ||
| 207 | } | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | void ResumeInactiveThreads() { | ||
| 212 | for (auto* thread : ThreadList()) { | ||
| 213 | if (thread != active_thread) { | ||
| 214 | thread->Resume(Kernel::SuspendType::Debug); | ||
| 215 | } | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | const std::vector<Kernel::KThread*>& ThreadList() { | ||
| 220 | return system.GlobalSchedulerContext().GetThreadList(); | ||
| 221 | } | ||
| 222 | |||
| 223 | private: | ||
| 224 | System& system; | ||
| 225 | std::unique_ptr<DebuggerFrontend> frontend; | ||
| 226 | |||
| 227 | std::jthread connection_thread; | ||
| 228 | std::mutex connection_lock; | ||
| 229 | boost::asio::io_context io_context; | ||
| 230 | boost::process::async_pipe signal_pipe; | ||
| 231 | boost::asio::ip::tcp::socket client_socket; | ||
| 232 | std::optional<std::unique_lock<std::mutex>> suspend; | ||
| 233 | |||
| 234 | Kernel::KThread* active_thread; | ||
| 235 | bool stopped; | ||
| 236 | bool stepping; | ||
| 237 | |||
| 238 | std::array<u8, 4096> client_data; | ||
| 239 | }; | ||
| 240 | |||
| 241 | Debugger::Debugger(Core::System& system, u16 port) { | ||
| 242 | try { | ||
| 243 | impl = std::make_unique<DebuggerImpl>(system, port); | ||
| 244 | } catch (const std::exception& ex) { | ||
| 245 | LOG_CRITICAL(Debug_GDBStub, "Failed to initialize debugger: {}", ex.what()); | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 249 | Debugger::~Debugger() = default; | ||
| 250 | |||
| 251 | bool Debugger::NotifyThreadStopped(Kernel::KThread* thread) { | ||
| 252 | return impl && impl->NotifyThreadStopped(thread); | ||
| 253 | } | ||
| 254 | |||
| 255 | bool Debugger::IsStepping() const { | ||
| 256 | return impl && impl->IsStepping(); | ||
| 257 | } | ||
| 258 | |||
| 259 | } // namespace Core | ||
diff --git a/src/core/debugger/debugger.h b/src/core/debugger/debugger.h new file mode 100644 index 000000000..7acd11815 --- /dev/null +++ b/src/core/debugger/debugger.h | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #include <memory> | ||
| 7 | |||
| 8 | #include "common/common_types.h" | ||
| 9 | |||
| 10 | namespace Kernel { | ||
| 11 | class KThread; | ||
| 12 | } | ||
| 13 | |||
| 14 | namespace Core { | ||
| 15 | class System; | ||
| 16 | |||
| 17 | class DebuggerImpl; | ||
| 18 | |||
| 19 | class Debugger { | ||
| 20 | public: | ||
| 21 | /** | ||
| 22 | * Blocks and waits for a connection on localhost, port `server_port`. | ||
| 23 | * Does not create the debugger if the port is already in use. | ||
| 24 | */ | ||
| 25 | explicit Debugger(Core::System& system, u16 server_port); | ||
| 26 | ~Debugger(); | ||
| 27 | |||
| 28 | /** | ||
| 29 | * Notify the debugger that the given thread is stopped | ||
| 30 | * (due to a breakpoint, or due to stopping after a successful step). | ||
| 31 | * | ||
| 32 | * The debugger will asynchronously halt emulation after the notification has | ||
| 33 | * occurred. If another thread attempts to notify before emulation has stopped, | ||
| 34 | * it is ignored and this method will return false. Otherwise it will return true. | ||
| 35 | */ | ||
| 36 | bool NotifyThreadStopped(Kernel::KThread* thread); | ||
| 37 | |||
| 38 | /** | ||
| 39 | * Returns whether a step is in progress. | ||
| 40 | */ | ||
| 41 | bool IsStepping() const; | ||
| 42 | |||
| 43 | private: | ||
| 44 | std::unique_ptr<DebuggerImpl> impl; | ||
| 45 | }; | ||
| 46 | } // namespace Core | ||
diff --git a/src/core/debugger/debugger_interface.h b/src/core/debugger/debugger_interface.h new file mode 100644 index 000000000..0b357fcb5 --- /dev/null +++ b/src/core/debugger/debugger_interface.h | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #include <functional> | ||
| 7 | #include <span> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "common/common_types.h" | ||
| 11 | |||
| 12 | namespace Kernel { | ||
| 13 | class KThread; | ||
| 14 | } | ||
| 15 | |||
| 16 | namespace Core { | ||
| 17 | |||
| 18 | enum class DebuggerAction { | ||
| 19 | Interrupt, // Stop emulation as soon as possible. | ||
| 20 | Continue, // Resume emulation. | ||
| 21 | StepThread, // Step the currently-active thread. | ||
| 22 | ShutdownEmulation, // Shut down the emulator. | ||
| 23 | }; | ||
| 24 | |||
| 25 | class DebuggerBackend { | ||
| 26 | public: | ||
| 27 | /** | ||
| 28 | * Can be invoked from a callback to synchronously wait for more data. | ||
| 29 | * Will return as soon as least one byte is received. Reads up to 4096 bytes. | ||
| 30 | */ | ||
| 31 | virtual std::span<const u8> ReadFromClient() = 0; | ||
| 32 | |||
| 33 | /** | ||
| 34 | * Can be invoked from a callback to write data to the client. | ||
| 35 | * Returns immediately after the data is sent. | ||
| 36 | */ | ||
| 37 | virtual void WriteToClient(std::span<const u8> data) = 0; | ||
| 38 | |||
| 39 | /** | ||
| 40 | * Gets the currently active thread when the debugger is stopped. | ||
| 41 | */ | ||
| 42 | virtual Kernel::KThread* GetActiveThread() = 0; | ||
| 43 | |||
| 44 | /** | ||
| 45 | * Sets the currently active thread when the debugger is stopped. | ||
| 46 | */ | ||
| 47 | virtual void SetActiveThread(Kernel::KThread* thread) = 0; | ||
| 48 | }; | ||
| 49 | |||
| 50 | class DebuggerFrontend { | ||
| 51 | public: | ||
| 52 | explicit DebuggerFrontend(DebuggerBackend& backend_) : backend{backend_} {} | ||
| 53 | |||
| 54 | /** | ||
| 55 | * Called after the client has successfully connected to the port. | ||
| 56 | */ | ||
| 57 | virtual void Connected() = 0; | ||
| 58 | |||
| 59 | /** | ||
| 60 | * Called when emulation has stopped. | ||
| 61 | */ | ||
| 62 | virtual void Stopped(Kernel::KThread* thread) = 0; | ||
| 63 | |||
| 64 | /** | ||
| 65 | * Called when new data is asynchronously received on the client socket. | ||
| 66 | * A list of actions to perform is returned. | ||
| 67 | */ | ||
| 68 | [[nodiscard]] virtual std::vector<DebuggerAction> ClientData(std::span<const u8> data) = 0; | ||
| 69 | |||
| 70 | protected: | ||
| 71 | DebuggerBackend& backend; | ||
| 72 | }; | ||
| 73 | |||
| 74 | } // namespace Core | ||
diff --git a/src/core/debugger/gdbstub.cpp b/src/core/debugger/gdbstub.cpp new file mode 100644 index 000000000..718c45952 --- /dev/null +++ b/src/core/debugger/gdbstub.cpp | |||
| @@ -0,0 +1,382 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #include <atomic> | ||
| 5 | #include <numeric> | ||
| 6 | #include <optional> | ||
| 7 | #include <thread> | ||
| 8 | |||
| 9 | #include <boost/asio.hpp> | ||
| 10 | #include <boost/process/async_pipe.hpp> | ||
| 11 | |||
| 12 | #include "common/hex_util.h" | ||
| 13 | #include "common/logging/log.h" | ||
| 14 | #include "common/scope_exit.h" | ||
| 15 | #include "core/arm/arm_interface.h" | ||
| 16 | #include "core/core.h" | ||
| 17 | #include "core/debugger/gdbstub.h" | ||
| 18 | #include "core/debugger/gdbstub_arch.h" | ||
| 19 | #include "core/hle/kernel/k_page_table.h" | ||
| 20 | #include "core/hle/kernel/k_process.h" | ||
| 21 | #include "core/hle/kernel/k_thread.h" | ||
| 22 | #include "core/loader/loader.h" | ||
| 23 | #include "core/memory.h" | ||
| 24 | |||
| 25 | namespace Core { | ||
| 26 | |||
| 27 | constexpr char GDB_STUB_START = '$'; | ||
| 28 | constexpr char GDB_STUB_END = '#'; | ||
| 29 | constexpr char GDB_STUB_ACK = '+'; | ||
| 30 | constexpr char GDB_STUB_NACK = '-'; | ||
| 31 | constexpr char GDB_STUB_INT3 = 0x03; | ||
| 32 | constexpr int GDB_STUB_SIGTRAP = 5; | ||
| 33 | |||
| 34 | constexpr char GDB_STUB_REPLY_ERR[] = "E01"; | ||
| 35 | constexpr char GDB_STUB_REPLY_OK[] = "OK"; | ||
| 36 | constexpr char GDB_STUB_REPLY_EMPTY[] = ""; | ||
| 37 | |||
| 38 | GDBStub::GDBStub(DebuggerBackend& backend_, Core::System& system_) | ||
| 39 | : DebuggerFrontend(backend_), system{system_} { | ||
| 40 | if (system.CurrentProcess()->Is64BitProcess()) { | ||
| 41 | arch = std::make_unique<GDBStubA64>(); | ||
| 42 | } else { | ||
| 43 | arch = std::make_unique<GDBStubA32>(); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | GDBStub::~GDBStub() = default; | ||
| 48 | |||
| 49 | void GDBStub::Connected() {} | ||
| 50 | |||
| 51 | void GDBStub::Stopped(Kernel::KThread* thread) { | ||
| 52 | SendReply(arch->ThreadStatus(thread, GDB_STUB_SIGTRAP)); | ||
| 53 | } | ||
| 54 | |||
| 55 | std::vector<DebuggerAction> GDBStub::ClientData(std::span<const u8> data) { | ||
| 56 | std::vector<DebuggerAction> actions; | ||
| 57 | current_command.insert(current_command.end(), data.begin(), data.end()); | ||
| 58 | |||
| 59 | while (current_command.size() != 0) { | ||
| 60 | ProcessData(actions); | ||
| 61 | } | ||
| 62 | |||
| 63 | return actions; | ||
| 64 | } | ||
| 65 | |||
| 66 | void GDBStub::ProcessData(std::vector<DebuggerAction>& actions) { | ||
| 67 | const char c{current_command[0]}; | ||
| 68 | |||
| 69 | // Acknowledgement | ||
| 70 | if (c == GDB_STUB_ACK || c == GDB_STUB_NACK) { | ||
| 71 | current_command.erase(current_command.begin()); | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | |||
| 75 | // Interrupt | ||
| 76 | if (c == GDB_STUB_INT3) { | ||
| 77 | LOG_INFO(Debug_GDBStub, "Received interrupt"); | ||
| 78 | current_command.erase(current_command.begin()); | ||
| 79 | actions.push_back(DebuggerAction::Interrupt); | ||
| 80 | SendStatus(GDB_STUB_ACK); | ||
| 81 | return; | ||
| 82 | } | ||
| 83 | |||
| 84 | // Otherwise, require the data to be the start of a command | ||
| 85 | if (c != GDB_STUB_START) { | ||
| 86 | LOG_ERROR(Debug_GDBStub, "Invalid command buffer contents: {}", current_command.data()); | ||
| 87 | current_command.clear(); | ||
| 88 | SendStatus(GDB_STUB_NACK); | ||
| 89 | return; | ||
| 90 | } | ||
| 91 | |||
| 92 | // Continue reading until command is complete | ||
| 93 | while (CommandEnd() == current_command.end()) { | ||
| 94 | const auto new_data{backend.ReadFromClient()}; | ||
| 95 | current_command.insert(current_command.end(), new_data.begin(), new_data.end()); | ||
| 96 | } | ||
| 97 | |||
| 98 | // Execute and respond to GDB | ||
| 99 | const auto command{DetachCommand()}; | ||
| 100 | |||
| 101 | if (command) { | ||
| 102 | SendStatus(GDB_STUB_ACK); | ||
| 103 | ExecuteCommand(*command, actions); | ||
| 104 | } else { | ||
| 105 | SendStatus(GDB_STUB_NACK); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | void GDBStub::ExecuteCommand(std::string_view packet, std::vector<DebuggerAction>& actions) { | ||
| 110 | LOG_TRACE(Debug_GDBStub, "Executing command: {}", packet); | ||
| 111 | |||
| 112 | if (packet.length() == 0) { | ||
| 113 | SendReply(GDB_STUB_REPLY_ERR); | ||
| 114 | return; | ||
| 115 | } | ||
| 116 | |||
| 117 | std::string_view command{packet.substr(1, packet.size())}; | ||
| 118 | |||
| 119 | switch (packet[0]) { | ||
| 120 | case 'H': { | ||
| 121 | Kernel::KThread* thread{nullptr}; | ||
| 122 | s64 thread_id{strtoll(command.data() + 1, nullptr, 16)}; | ||
| 123 | if (thread_id >= 1) { | ||
| 124 | thread = GetThreadByID(thread_id); | ||
| 125 | } | ||
| 126 | |||
| 127 | if (thread) { | ||
| 128 | SendReply(GDB_STUB_REPLY_OK); | ||
| 129 | backend.SetActiveThread(thread); | ||
| 130 | } else { | ||
| 131 | SendReply(GDB_STUB_REPLY_ERR); | ||
| 132 | } | ||
| 133 | break; | ||
| 134 | } | ||
| 135 | case 'T': { | ||
| 136 | s64 thread_id{strtoll(command.data(), nullptr, 16)}; | ||
| 137 | if (GetThreadByID(thread_id)) { | ||
| 138 | SendReply(GDB_STUB_REPLY_OK); | ||
| 139 | } else { | ||
| 140 | SendReply(GDB_STUB_REPLY_ERR); | ||
| 141 | } | ||
| 142 | break; | ||
| 143 | } | ||
| 144 | case 'q': | ||
| 145 | HandleQuery(command); | ||
| 146 | break; | ||
| 147 | case '?': | ||
| 148 | SendReply(arch->ThreadStatus(backend.GetActiveThread(), GDB_STUB_SIGTRAP)); | ||
| 149 | break; | ||
| 150 | case 'k': | ||
| 151 | LOG_INFO(Debug_GDBStub, "Shutting down emulation"); | ||
| 152 | actions.push_back(DebuggerAction::ShutdownEmulation); | ||
| 153 | break; | ||
| 154 | case 'g': | ||
| 155 | SendReply(arch->ReadRegisters(backend.GetActiveThread())); | ||
| 156 | break; | ||
| 157 | case 'G': | ||
| 158 | arch->WriteRegisters(backend.GetActiveThread(), command); | ||
| 159 | SendReply(GDB_STUB_REPLY_OK); | ||
| 160 | break; | ||
| 161 | case 'p': { | ||
| 162 | const size_t reg{static_cast<size_t>(strtoll(command.data(), nullptr, 16))}; | ||
| 163 | SendReply(arch->RegRead(backend.GetActiveThread(), reg)); | ||
| 164 | break; | ||
| 165 | } | ||
| 166 | case 'P': { | ||
| 167 | const auto sep{std::find(command.begin(), command.end(), '=') - command.begin() + 1}; | ||
| 168 | const size_t reg{static_cast<size_t>(strtoll(command.data(), nullptr, 16))}; | ||
| 169 | arch->RegWrite(backend.GetActiveThread(), reg, std::string_view(command).substr(sep)); | ||
| 170 | break; | ||
| 171 | } | ||
| 172 | case 'm': { | ||
| 173 | const auto sep{std::find(command.begin(), command.end(), ',') - command.begin() + 1}; | ||
| 174 | const size_t addr{static_cast<size_t>(strtoll(command.data(), nullptr, 16))}; | ||
| 175 | const size_t size{static_cast<size_t>(strtoll(command.data() + sep, nullptr, 16))}; | ||
| 176 | |||
| 177 | if (system.Memory().IsValidVirtualAddressRange(addr, size)) { | ||
| 178 | std::vector<u8> mem(size); | ||
| 179 | system.Memory().ReadBlock(addr, mem.data(), size); | ||
| 180 | |||
| 181 | SendReply(Common::HexToString(mem)); | ||
| 182 | } else { | ||
| 183 | SendReply(GDB_STUB_REPLY_ERR); | ||
| 184 | } | ||
| 185 | break; | ||
| 186 | } | ||
| 187 | case 'M': { | ||
| 188 | const auto size_sep{std::find(command.begin(), command.end(), ',') - command.begin() + 1}; | ||
| 189 | const auto mem_sep{std::find(command.begin(), command.end(), ':') - command.begin() + 1}; | ||
| 190 | |||
| 191 | const size_t addr{static_cast<size_t>(strtoll(command.data(), nullptr, 16))}; | ||
| 192 | const size_t size{static_cast<size_t>(strtoll(command.data() + size_sep, nullptr, 16))}; | ||
| 193 | |||
| 194 | const auto mem_substr{std::string_view(command).substr(mem_sep)}; | ||
| 195 | const auto mem{Common::HexStringToVector(mem_substr, false)}; | ||
| 196 | |||
| 197 | if (system.Memory().IsValidVirtualAddressRange(addr, size)) { | ||
| 198 | system.Memory().WriteBlock(addr, mem.data(), size); | ||
| 199 | system.InvalidateCpuInstructionCacheRange(addr, size); | ||
| 200 | SendReply(GDB_STUB_REPLY_OK); | ||
| 201 | } else { | ||
| 202 | SendReply(GDB_STUB_REPLY_ERR); | ||
| 203 | } | ||
| 204 | break; | ||
| 205 | } | ||
| 206 | case 's': | ||
| 207 | actions.push_back(DebuggerAction::StepThread); | ||
| 208 | break; | ||
| 209 | case 'C': | ||
| 210 | case 'c': | ||
| 211 | actions.push_back(DebuggerAction::Continue); | ||
| 212 | break; | ||
| 213 | case 'Z': { | ||
| 214 | const auto addr_sep{std::find(command.begin(), command.end(), ',') - command.begin() + 1}; | ||
| 215 | const size_t addr{static_cast<size_t>(strtoll(command.data() + addr_sep, nullptr, 16))}; | ||
| 216 | |||
| 217 | if (system.Memory().IsValidVirtualAddress(addr)) { | ||
| 218 | replaced_instructions[addr] = system.Memory().Read32(addr); | ||
| 219 | system.Memory().Write32(addr, arch->BreakpointInstruction()); | ||
| 220 | system.InvalidateCpuInstructionCacheRange(addr, sizeof(u32)); | ||
| 221 | |||
| 222 | SendReply(GDB_STUB_REPLY_OK); | ||
| 223 | } else { | ||
| 224 | SendReply(GDB_STUB_REPLY_ERR); | ||
| 225 | } | ||
| 226 | break; | ||
| 227 | } | ||
| 228 | case 'z': { | ||
| 229 | const auto addr_sep{std::find(command.begin(), command.end(), ',') - command.begin() + 1}; | ||
| 230 | const size_t addr{static_cast<size_t>(strtoll(command.data() + addr_sep, nullptr, 16))}; | ||
| 231 | |||
| 232 | const auto orig_insn{replaced_instructions.find(addr)}; | ||
| 233 | if (system.Memory().IsValidVirtualAddress(addr) && | ||
| 234 | orig_insn != replaced_instructions.end()) { | ||
| 235 | system.Memory().Write32(addr, orig_insn->second); | ||
| 236 | system.InvalidateCpuInstructionCacheRange(addr, sizeof(u32)); | ||
| 237 | replaced_instructions.erase(addr); | ||
| 238 | |||
| 239 | SendReply(GDB_STUB_REPLY_OK); | ||
| 240 | } else { | ||
| 241 | SendReply(GDB_STUB_REPLY_ERR); | ||
| 242 | } | ||
| 243 | break; | ||
| 244 | } | ||
| 245 | default: | ||
| 246 | SendReply(GDB_STUB_REPLY_EMPTY); | ||
| 247 | break; | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | void GDBStub::HandleQuery(std::string_view command) { | ||
| 252 | if (command.starts_with("TStatus")) { | ||
| 253 | // no tracepoint support | ||
| 254 | SendReply("T0"); | ||
| 255 | } else if (command.starts_with("Supported")) { | ||
| 256 | SendReply("PacketSize=4000;qXfer:features:read+;qXfer:threads:read+;qXfer:libraries:read+"); | ||
| 257 | } else if (command.starts_with("Xfer:features:read:target.xml:")) { | ||
| 258 | const auto offset{command.substr(30)}; | ||
| 259 | const auto amount{command.substr(command.find(',') + 1)}; | ||
| 260 | |||
| 261 | const auto offset_val{static_cast<u64>(strtoll(offset.data(), nullptr, 16))}; | ||
| 262 | const auto amount_val{static_cast<u64>(strtoll(amount.data(), nullptr, 16))}; | ||
| 263 | const auto target_xml{arch->GetTargetXML()}; | ||
| 264 | |||
| 265 | if (offset_val + amount_val > target_xml.size()) { | ||
| 266 | SendReply("l" + target_xml.substr(offset_val)); | ||
| 267 | } else { | ||
| 268 | SendReply("m" + target_xml.substr(offset_val, amount_val)); | ||
| 269 | } | ||
| 270 | } else if (command.starts_with("Offsets")) { | ||
| 271 | Loader::AppLoader::Modules modules; | ||
| 272 | system.GetAppLoader().ReadNSOModules(modules); | ||
| 273 | |||
| 274 | const auto main = std::find_if(modules.begin(), modules.end(), | ||
| 275 | [](const auto& key) { return key.second == "main"; }); | ||
| 276 | if (main != modules.end()) { | ||
| 277 | SendReply(fmt::format("TextSeg={:x}", main->first)); | ||
| 278 | } else { | ||
| 279 | SendReply(fmt::format("TextSeg={:x}", | ||
| 280 | system.CurrentProcess()->PageTable().GetCodeRegionStart())); | ||
| 281 | } | ||
| 282 | } else if (command.starts_with("fThreadInfo")) { | ||
| 283 | // beginning of list | ||
| 284 | const auto& threads = system.GlobalSchedulerContext().GetThreadList(); | ||
| 285 | std::vector<std::string> thread_ids; | ||
| 286 | for (const auto& thread : threads) { | ||
| 287 | thread_ids.push_back(fmt::format("{:x}", thread->GetThreadID())); | ||
| 288 | } | ||
| 289 | SendReply(fmt::format("m{}", fmt::join(thread_ids, ","))); | ||
| 290 | } else if (command.starts_with("sThreadInfo")) { | ||
| 291 | // end of list | ||
| 292 | SendReply("l"); | ||
| 293 | } else if (command.starts_with("Xfer:threads:read")) { | ||
| 294 | std::string buffer; | ||
| 295 | buffer += R"(l<?xml version="1.0"?>)"; | ||
| 296 | buffer += "<threads>"; | ||
| 297 | |||
| 298 | const auto& threads = system.GlobalSchedulerContext().GetThreadList(); | ||
| 299 | for (const auto& thread : threads) { | ||
| 300 | buffer += | ||
| 301 | fmt::format(R"(<thread id="{:x}" core="{:d}" name="Thread {:d}"/>)", | ||
| 302 | thread->GetThreadID(), thread->GetActiveCore(), thread->GetThreadID()); | ||
| 303 | } | ||
| 304 | |||
| 305 | buffer += "</threads>"; | ||
| 306 | SendReply(buffer); | ||
| 307 | } else { | ||
| 308 | SendReply(GDB_STUB_REPLY_EMPTY); | ||
| 309 | } | ||
| 310 | } | ||
| 311 | |||
| 312 | Kernel::KThread* GDBStub::GetThreadByID(u64 thread_id) { | ||
| 313 | const auto& threads{system.GlobalSchedulerContext().GetThreadList()}; | ||
| 314 | for (auto* thread : threads) { | ||
| 315 | if (thread->GetThreadID() == thread_id) { | ||
| 316 | return thread; | ||
| 317 | } | ||
| 318 | } | ||
| 319 | |||
| 320 | return nullptr; | ||
| 321 | } | ||
| 322 | |||
| 323 | std::vector<char>::const_iterator GDBStub::CommandEnd() const { | ||
| 324 | // Find the end marker | ||
| 325 | const auto end{std::find(current_command.begin(), current_command.end(), GDB_STUB_END)}; | ||
| 326 | |||
| 327 | // Require the checksum to be present | ||
| 328 | return std::min(end + 2, current_command.end()); | ||
| 329 | } | ||
| 330 | |||
| 331 | std::optional<std::string> GDBStub::DetachCommand() { | ||
| 332 | // Slice the string part from the beginning to the end marker | ||
| 333 | const auto end{CommandEnd()}; | ||
| 334 | |||
| 335 | // Extract possible command data | ||
| 336 | std::string data(current_command.data(), end - current_command.begin() + 1); | ||
| 337 | |||
| 338 | // Shift over the remaining contents | ||
| 339 | current_command.erase(current_command.begin(), end + 1); | ||
| 340 | |||
| 341 | // Validate received command | ||
| 342 | if (data[0] != GDB_STUB_START) { | ||
| 343 | LOG_ERROR(Debug_GDBStub, "Invalid start data: {}", data[0]); | ||
| 344 | return std::nullopt; | ||
| 345 | } | ||
| 346 | |||
| 347 | u8 calculated = CalculateChecksum(std::string_view(data).substr(1, data.size() - 4)); | ||
| 348 | u8 received = static_cast<u8>(strtoll(data.data() + data.size() - 2, nullptr, 16)); | ||
| 349 | |||
| 350 | // Verify checksum | ||
| 351 | if (calculated != received) { | ||
| 352 | LOG_ERROR(Debug_GDBStub, "Checksum mismatch: calculated {:02x}, received {:02x}", | ||
| 353 | calculated, received); | ||
| 354 | return std::nullopt; | ||
| 355 | } | ||
| 356 | |||
| 357 | return data.substr(1, data.size() - 4); | ||
| 358 | } | ||
| 359 | |||
| 360 | u8 GDBStub::CalculateChecksum(std::string_view data) { | ||
| 361 | return static_cast<u8>( | ||
| 362 | std::accumulate(data.begin(), data.end(), u8{0}, [](u8 lhs, u8 rhs) { return lhs + rhs; })); | ||
| 363 | } | ||
| 364 | |||
| 365 | void GDBStub::SendReply(std::string_view data) { | ||
| 366 | const auto output{ | ||
| 367 | fmt::format("{}{}{}{:02x}", GDB_STUB_START, data, GDB_STUB_END, CalculateChecksum(data))}; | ||
| 368 | LOG_TRACE(Debug_GDBStub, "Writing reply: {}", output); | ||
| 369 | |||
| 370 | // C++ string support is complete rubbish | ||
| 371 | const u8* output_begin = reinterpret_cast<const u8*>(output.data()); | ||
| 372 | const u8* output_end = output_begin + output.size(); | ||
| 373 | backend.WriteToClient(std::span<const u8>(output_begin, output_end)); | ||
| 374 | } | ||
| 375 | |||
| 376 | void GDBStub::SendStatus(char status) { | ||
| 377 | std::array<u8, 1> buf = {static_cast<u8>(status)}; | ||
| 378 | LOG_TRACE(Debug_GDBStub, "Writing status: {}", status); | ||
| 379 | backend.WriteToClient(buf); | ||
| 380 | } | ||
| 381 | |||
| 382 | } // namespace Core | ||
diff --git a/src/core/debugger/gdbstub.h b/src/core/debugger/gdbstub.h new file mode 100644 index 000000000..b93a3a511 --- /dev/null +++ b/src/core/debugger/gdbstub.h | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #include <map> | ||
| 7 | #include <memory> | ||
| 8 | #include <optional> | ||
| 9 | #include <string_view> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | #include "core/debugger/debugger_interface.h" | ||
| 13 | #include "core/debugger/gdbstub_arch.h" | ||
| 14 | |||
| 15 | namespace Core { | ||
| 16 | |||
| 17 | class System; | ||
| 18 | |||
| 19 | class GDBStub : public DebuggerFrontend { | ||
| 20 | public: | ||
| 21 | explicit GDBStub(DebuggerBackend& backend, Core::System& system); | ||
| 22 | ~GDBStub(); | ||
| 23 | |||
| 24 | void Connected() override; | ||
| 25 | void Stopped(Kernel::KThread* thread) override; | ||
| 26 | std::vector<DebuggerAction> ClientData(std::span<const u8> data) override; | ||
| 27 | |||
| 28 | private: | ||
| 29 | void ProcessData(std::vector<DebuggerAction>& actions); | ||
| 30 | void ExecuteCommand(std::string_view packet, std::vector<DebuggerAction>& actions); | ||
| 31 | void HandleQuery(std::string_view command); | ||
| 32 | std::vector<char>::const_iterator CommandEnd() const; | ||
| 33 | std::optional<std::string> DetachCommand(); | ||
| 34 | Kernel::KThread* GetThreadByID(u64 thread_id); | ||
| 35 | |||
| 36 | static u8 CalculateChecksum(std::string_view data); | ||
| 37 | void SendReply(std::string_view data); | ||
| 38 | void SendStatus(char status); | ||
| 39 | |||
| 40 | private: | ||
| 41 | Core::System& system; | ||
| 42 | std::unique_ptr<GDBStubArch> arch; | ||
| 43 | std::vector<char> current_command; | ||
| 44 | std::map<VAddr, u32> replaced_instructions; | ||
| 45 | }; | ||
| 46 | |||
| 47 | } // namespace Core | ||
diff --git a/src/core/debugger/gdbstub_arch.cpp b/src/core/debugger/gdbstub_arch.cpp new file mode 100644 index 000000000..99e3893a9 --- /dev/null +++ b/src/core/debugger/gdbstub_arch.cpp | |||
| @@ -0,0 +1,406 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #include "common/hex_util.h" | ||
| 5 | #include "core/debugger/gdbstub_arch.h" | ||
| 6 | #include "core/hle/kernel/k_thread.h" | ||
| 7 | |||
| 8 | namespace Core { | ||
| 9 | |||
| 10 | template <typename T> | ||
| 11 | static T HexToValue(std::string_view hex) { | ||
| 12 | static_assert(std::is_trivially_copyable_v<T>); | ||
| 13 | T value{}; | ||
| 14 | const auto mem{Common::HexStringToVector(hex, false)}; | ||
| 15 | std::memcpy(&value, mem.data(), std::min(mem.size(), sizeof(T))); | ||
| 16 | return value; | ||
| 17 | } | ||
| 18 | |||
| 19 | template <typename T> | ||
| 20 | static std::string ValueToHex(const T value) { | ||
| 21 | static_assert(std::is_trivially_copyable_v<T>); | ||
| 22 | std::array<u8, sizeof(T)> mem{}; | ||
| 23 | std::memcpy(mem.data(), &value, sizeof(T)); | ||
| 24 | return Common::HexToString(mem); | ||
| 25 | } | ||
| 26 | |||
| 27 | template <typename T> | ||
| 28 | static T GetSIMDRegister(const std::array<u32, 64>& simd_regs, size_t offset) { | ||
| 29 | static_assert(std::is_trivially_copyable_v<T>); | ||
| 30 | T value{}; | ||
| 31 | std::memcpy(&value, reinterpret_cast<const u8*>(simd_regs.data()) + sizeof(T) * offset, | ||
| 32 | sizeof(T)); | ||
| 33 | return value; | ||
| 34 | } | ||
| 35 | |||
| 36 | template <typename T> | ||
| 37 | static void PutSIMDRegister(std::array<u32, 64>& simd_regs, size_t offset, const T value) { | ||
| 38 | static_assert(std::is_trivially_copyable_v<T>); | ||
| 39 | std::memcpy(reinterpret_cast<u8*>(simd_regs.data()) + sizeof(T) * offset, &value, sizeof(T)); | ||
| 40 | } | ||
| 41 | |||
| 42 | // For sample XML files see the GDB source /gdb/features | ||
| 43 | // This XML defines what the registers are for this specific ARM device | ||
| 44 | std::string GDBStubA64::GetTargetXML() const { | ||
| 45 | constexpr const char* target_xml = | ||
| 46 | R"(<?xml version="1.0"?> | ||
| 47 | <!DOCTYPE target SYSTEM "gdb-target.dtd"> | ||
| 48 | <target version="1.0"> | ||
| 49 | <feature name="org.gnu.gdb.aarch64.core"> | ||
| 50 | <reg name="x0" bitsize="64"/> | ||
| 51 | <reg name="x1" bitsize="64"/> | ||
| 52 | <reg name="x2" bitsize="64"/> | ||
| 53 | <reg name="x3" bitsize="64"/> | ||
| 54 | <reg name="x4" bitsize="64"/> | ||
| 55 | <reg name="x5" bitsize="64"/> | ||
| 56 | <reg name="x6" bitsize="64"/> | ||
| 57 | <reg name="x7" bitsize="64"/> | ||
| 58 | <reg name="x8" bitsize="64"/> | ||
| 59 | <reg name="x9" bitsize="64"/> | ||
| 60 | <reg name="x10" bitsize="64"/> | ||
| 61 | <reg name="x11" bitsize="64"/> | ||
| 62 | <reg name="x12" bitsize="64"/> | ||
| 63 | <reg name="x13" bitsize="64"/> | ||
| 64 | <reg name="x14" bitsize="64"/> | ||
| 65 | <reg name="x15" bitsize="64"/> | ||
| 66 | <reg name="x16" bitsize="64"/> | ||
| 67 | <reg name="x17" bitsize="64"/> | ||
| 68 | <reg name="x18" bitsize="64"/> | ||
| 69 | <reg name="x19" bitsize="64"/> | ||
| 70 | <reg name="x20" bitsize="64"/> | ||
| 71 | <reg name="x21" bitsize="64"/> | ||
| 72 | <reg name="x22" bitsize="64"/> | ||
| 73 | <reg name="x23" bitsize="64"/> | ||
| 74 | <reg name="x24" bitsize="64"/> | ||
| 75 | <reg name="x25" bitsize="64"/> | ||
| 76 | <reg name="x26" bitsize="64"/> | ||
| 77 | <reg name="x27" bitsize="64"/> | ||
| 78 | <reg name="x28" bitsize="64"/> | ||
| 79 | <reg name="x29" bitsize="64"/> | ||
| 80 | <reg name="x30" bitsize="64"/> | ||
| 81 | <reg name="sp" bitsize="64" type="data_ptr"/> | ||
| 82 | <reg name="pc" bitsize="64" type="code_ptr"/> | ||
| 83 | <flags id="pstate_flags" size="4"> | ||
| 84 | <field name="SP" start="0" end="0"/> | ||
| 85 | <field name="" start="1" end="1"/> | ||
| 86 | <field name="EL" start="2" end="3"/> | ||
| 87 | <field name="nRW" start="4" end="4"/> | ||
| 88 | <field name="" start="5" end="5"/> | ||
| 89 | <field name="F" start="6" end="6"/> | ||
| 90 | <field name="I" start="7" end="7"/> | ||
| 91 | <field name="A" start="8" end="8"/> | ||
| 92 | <field name="D" start="9" end="9"/> | ||
| 93 | <field name="IL" start="20" end="20"/> | ||
| 94 | <field name="SS" start="21" end="21"/> | ||
| 95 | <field name="V" start="28" end="28"/> | ||
| 96 | <field name="C" start="29" end="29"/> | ||
| 97 | <field name="Z" start="30" end="30"/> | ||
| 98 | <field name="N" start="31" end="31"/> | ||
| 99 | </flags> | ||
| 100 | <reg name="pstate" bitsize="32" type="pstate_flags"/> | ||
| 101 | </feature> | ||
| 102 | <feature name="org.gnu.gdb.aarch64.fpu"> | ||
| 103 | </feature> | ||
| 104 | </target>)"; | ||
| 105 | |||
| 106 | return target_xml; | ||
| 107 | } | ||
| 108 | |||
| 109 | std::string GDBStubA64::RegRead(const Kernel::KThread* thread, size_t id) const { | ||
| 110 | if (!thread) { | ||
| 111 | return ""; | ||
| 112 | } | ||
| 113 | |||
| 114 | const auto& context{thread->GetContext64()}; | ||
| 115 | const auto& gprs{context.cpu_registers}; | ||
| 116 | const auto& fprs{context.vector_registers}; | ||
| 117 | |||
| 118 | if (id <= SP_REGISTER) { | ||
| 119 | return ValueToHex(gprs[id]); | ||
| 120 | } else if (id == PC_REGISTER) { | ||
| 121 | return ValueToHex(context.pc); | ||
| 122 | } else if (id == PSTATE_REGISTER) { | ||
| 123 | return ValueToHex(context.pstate); | ||
| 124 | } else if (id >= Q0_REGISTER && id < FPCR_REGISTER) { | ||
| 125 | return ValueToHex(fprs[id - Q0_REGISTER]); | ||
| 126 | } else if (id == FPCR_REGISTER) { | ||
| 127 | return ValueToHex(context.fpcr); | ||
| 128 | } else if (id == FPSR_REGISTER) { | ||
| 129 | return ValueToHex(context.fpsr); | ||
| 130 | } else { | ||
| 131 | return ""; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | void GDBStubA64::RegWrite(Kernel::KThread* thread, size_t id, std::string_view value) const { | ||
| 136 | if (!thread) { | ||
| 137 | return; | ||
| 138 | } | ||
| 139 | |||
| 140 | auto& context{thread->GetContext64()}; | ||
| 141 | |||
| 142 | if (id <= SP_REGISTER) { | ||
| 143 | context.cpu_registers[id] = HexToValue<u64>(value); | ||
| 144 | } else if (id == PC_REGISTER) { | ||
| 145 | context.pc = HexToValue<u64>(value); | ||
| 146 | } else if (id == PSTATE_REGISTER) { | ||
| 147 | context.pstate = HexToValue<u32>(value); | ||
| 148 | } else if (id >= Q0_REGISTER && id < FPCR_REGISTER) { | ||
| 149 | context.vector_registers[id - Q0_REGISTER] = HexToValue<u128>(value); | ||
| 150 | } else if (id == FPCR_REGISTER) { | ||
| 151 | context.fpcr = HexToValue<u32>(value); | ||
| 152 | } else if (id == FPSR_REGISTER) { | ||
| 153 | context.fpsr = HexToValue<u32>(value); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | std::string GDBStubA64::ReadRegisters(const Kernel::KThread* thread) const { | ||
| 158 | std::string output; | ||
| 159 | |||
| 160 | for (size_t reg = 0; reg <= FPCR_REGISTER; reg++) { | ||
| 161 | output += RegRead(thread, reg); | ||
| 162 | } | ||
| 163 | |||
| 164 | return output; | ||
| 165 | } | ||
| 166 | |||
| 167 | void GDBStubA64::WriteRegisters(Kernel::KThread* thread, std::string_view register_data) const { | ||
| 168 | for (size_t i = 0, reg = 0; reg <= FPCR_REGISTER; reg++) { | ||
| 169 | if (reg <= SP_REGISTER || reg == PC_REGISTER) { | ||
| 170 | RegWrite(thread, reg, register_data.substr(i, 16)); | ||
| 171 | i += 16; | ||
| 172 | } else if (reg == PSTATE_REGISTER || reg == FPCR_REGISTER || reg == FPSR_REGISTER) { | ||
| 173 | RegWrite(thread, reg, register_data.substr(i, 8)); | ||
| 174 | i += 8; | ||
| 175 | } else if (reg >= Q0_REGISTER && reg < FPCR_REGISTER) { | ||
| 176 | RegWrite(thread, reg, register_data.substr(i, 32)); | ||
| 177 | i += 32; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | std::string GDBStubA64::ThreadStatus(const Kernel::KThread* thread, u8 signal) const { | ||
| 183 | return fmt::format("T{:02x}{:02x}:{};{:02x}:{};{:02x}:{};thread:{:x};", signal, PC_REGISTER, | ||
| 184 | RegRead(thread, PC_REGISTER), SP_REGISTER, RegRead(thread, SP_REGISTER), | ||
| 185 | LR_REGISTER, RegRead(thread, LR_REGISTER), thread->GetThreadID()); | ||
| 186 | } | ||
| 187 | |||
| 188 | u32 GDBStubA64::BreakpointInstruction() const { | ||
| 189 | // A64: brk #0 | ||
| 190 | return 0xd4200000; | ||
| 191 | } | ||
| 192 | |||
| 193 | std::string GDBStubA32::GetTargetXML() const { | ||
| 194 | constexpr const char* target_xml = | ||
| 195 | R"(<?xml version="1.0"?> | ||
| 196 | <!DOCTYPE target SYSTEM "gdb-target.dtd"> | ||
| 197 | <target version="1.0"> | ||
| 198 | <feature name="org.gnu.gdb.arm.core"> | ||
| 199 | <reg name="r0" bitsize="32" type="uint32"/> | ||
| 200 | <reg name="r1" bitsize="32" type="uint32"/> | ||
| 201 | <reg name="r2" bitsize="32" type="uint32"/> | ||
| 202 | <reg name="r3" bitsize="32" type="uint32"/> | ||
| 203 | <reg name="r4" bitsize="32" type="uint32"/> | ||
| 204 | <reg name="r5" bitsize="32" type="uint32"/> | ||
| 205 | <reg name="r6" bitsize="32" type="uint32"/> | ||
| 206 | <reg name="r7" bitsize="32" type="uint32"/> | ||
| 207 | <reg name="r8" bitsize="32" type="uint32"/> | ||
| 208 | <reg name="r9" bitsize="32" type="uint32"/> | ||
| 209 | <reg name="r10" bitsize="32" type="uint32"/> | ||
| 210 | <reg name="r11" bitsize="32" type="uint32"/> | ||
| 211 | <reg name="r12" bitsize="32" type="uint32"/> | ||
| 212 | <reg name="sp" bitsize="32" type="data_ptr"/> | ||
| 213 | <reg name="lr" bitsize="32" type="code_ptr"/> | ||
| 214 | <reg name="pc" bitsize="32" type="code_ptr"/> | ||
| 215 | <!-- The CPSR is register 25, rather than register 16, because | ||
| 216 | the FPA registers historically were placed between the PC | ||
| 217 | and the CPSR in the "g" packet. --> | ||
| 218 | <reg name="cpsr" bitsize="32" regnum="25"/> | ||
| 219 | </feature> | ||
| 220 | <feature name="org.gnu.gdb.arm.vfp"> | ||
| 221 | <vector id="neon_uint8x8" type="uint8" count="8"/> | ||
| 222 | <vector id="neon_uint16x4" type="uint16" count="4"/> | ||
| 223 | <vector id="neon_uint32x2" type="uint32" count="2"/> | ||
| 224 | <vector id="neon_float32x2" type="ieee_single" count="2"/> | ||
| 225 | <union id="neon_d"> | ||
| 226 | <field name="u8" type="neon_uint8x8"/> | ||
| 227 | <field name="u16" type="neon_uint16x4"/> | ||
| 228 | <field name="u32" type="neon_uint32x2"/> | ||
| 229 | <field name="u64" type="uint64"/> | ||
| 230 | <field name="f32" type="neon_float32x2"/> | ||
| 231 | <field name="f64" type="ieee_double"/> | ||
| 232 | </union> | ||
| 233 | <vector id="neon_uint8x16" type="uint8" count="16"/> | ||
| 234 | <vector id="neon_uint16x8" type="uint16" count="8"/> | ||
| 235 | <vector id="neon_uint32x4" type="uint32" count="4"/> | ||
| 236 | <vector id="neon_uint64x2" type="uint64" count="2"/> | ||
| 237 | <vector id="neon_float32x4" type="ieee_single" count="4"/> | ||
| 238 | <vector id="neon_float64x2" type="ieee_double" count="2"/> | ||
| 239 | <union id="neon_q"> | ||
| 240 | <field name="u8" type="neon_uint8x16"/> | ||
| 241 | <field name="u16" type="neon_uint16x8"/> | ||
| 242 | <field name="u32" type="neon_uint32x4"/> | ||
| 243 | <field name="u64" type="neon_uint64x2"/> | ||
| 244 | <field name="f32" type="neon_float32x4"/> | ||
| 245 | <field name="f64" type="neon_float64x2"/> | ||
| 246 | </union> | ||
| 247 | <reg name="d0" bitsize="64" type="neon_d" regnum="32"/> | ||
| 248 | <reg name="d1" bitsize="64" type="neon_d"/> | ||
| 249 | <reg name="d2" bitsize="64" type="neon_d"/> | ||
| 250 | <reg name="d3" bitsize="64" type="neon_d"/> | ||
| 251 | <reg name="d4" bitsize="64" type="neon_d"/> | ||
| 252 | <reg name="d5" bitsize="64" type="neon_d"/> | ||
| 253 | <reg name="d6" bitsize="64" type="neon_d"/> | ||
| 254 | <reg name="d7" bitsize="64" type="neon_d"/> | ||
| 255 | <reg name="d8" bitsize="64" type="neon_d"/> | ||
| 256 | <reg name="d9" bitsize="64" type="neon_d"/> | ||
| 257 | <reg name="d10" bitsize="64" type="neon_d"/> | ||
| 258 | <reg name="d11" bitsize="64" type="neon_d"/> | ||
| 259 | <reg name="d12" bitsize="64" type="neon_d"/> | ||
| 260 | <reg name="d13" bitsize="64" type="neon_d"/> | ||
| 261 | <reg name="d14" bitsize="64" type="neon_d"/> | ||
| 262 | <reg name="d15" bitsize="64" type="neon_d"/> | ||
| 263 | <reg name="d16" bitsize="64" type="neon_d"/> | ||
| 264 | <reg name="d17" bitsize="64" type="neon_d"/> | ||
| 265 | <reg name="d18" bitsize="64" type="neon_d"/> | ||
| 266 | <reg name="d19" bitsize="64" type="neon_d"/> | ||
| 267 | <reg name="d20" bitsize="64" type="neon_d"/> | ||
| 268 | <reg name="d21" bitsize="64" type="neon_d"/> | ||
| 269 | <reg name="d22" bitsize="64" type="neon_d"/> | ||
| 270 | <reg name="d23" bitsize="64" type="neon_d"/> | ||
| 271 | <reg name="d24" bitsize="64" type="neon_d"/> | ||
| 272 | <reg name="d25" bitsize="64" type="neon_d"/> | ||
| 273 | <reg name="d26" bitsize="64" type="neon_d"/> | ||
| 274 | <reg name="d27" bitsize="64" type="neon_d"/> | ||
| 275 | <reg name="d28" bitsize="64" type="neon_d"/> | ||
| 276 | <reg name="d29" bitsize="64" type="neon_d"/> | ||
| 277 | <reg name="d30" bitsize="64" type="neon_d"/> | ||
| 278 | <reg name="d31" bitsize="64" type="neon_d"/> | ||
| 279 | |||
| 280 | <reg name="q0" bitsize="128" type="neon_q" regnum="64"/> | ||
| 281 | <reg name="q1" bitsize="128" type="neon_q"/> | ||
| 282 | <reg name="q2" bitsize="128" type="neon_q"/> | ||
| 283 | <reg name="q3" bitsize="128" type="neon_q"/> | ||
| 284 | <reg name="q4" bitsize="128" type="neon_q"/> | ||
| 285 | <reg name="q5" bitsize="128" type="neon_q"/> | ||
| 286 | <reg name="q6" bitsize="128" type="neon_q"/> | ||
| 287 | <reg name="q7" bitsize="128" type="neon_q"/> | ||
| 288 | <reg name="q8" bitsize="128" type="neon_q"/> | ||
| 289 | <reg name="q9" bitsize="128" type="neon_q"/> | ||
| 290 | <reg name="q10" bitsize="128" type="neon_q"/> | ||
| 291 | <reg name="q10" bitsize="128" type="neon_q"/> | ||
| 292 | <reg name="q12" bitsize="128" type="neon_q"/> | ||
| 293 | <reg name="q13" bitsize="128" type="neon_q"/> | ||
| 294 | <reg name="q14" bitsize="128" type="neon_q"/> | ||
| 295 | <reg name="q15" bitsize="128" type="neon_q"/> | ||
| 296 | |||
| 297 | <reg name="fpscr" bitsize="32" type="int" group="float" regnum="80"/> | ||
| 298 | </feature> | ||
| 299 | </target>)"; | ||
| 300 | |||
| 301 | return target_xml; | ||
| 302 | } | ||
| 303 | |||
| 304 | std::string GDBStubA32::RegRead(const Kernel::KThread* thread, size_t id) const { | ||
| 305 | if (!thread) { | ||
| 306 | return ""; | ||
| 307 | } | ||
| 308 | |||
| 309 | const auto& context{thread->GetContext32()}; | ||
| 310 | const auto& gprs{context.cpu_registers}; | ||
| 311 | const auto& fprs{context.extension_registers}; | ||
| 312 | |||
| 313 | if (id <= PC_REGISTER) { | ||
| 314 | return ValueToHex(gprs[id]); | ||
| 315 | } else if (id == CPSR_REGISTER) { | ||
| 316 | return ValueToHex(context.cpsr); | ||
| 317 | } else if (id >= D0_REGISTER && id < Q0_REGISTER) { | ||
| 318 | const u64 dN{GetSIMDRegister<u64>(fprs, id - D0_REGISTER)}; | ||
| 319 | return ValueToHex(dN); | ||
| 320 | } else if (id >= Q0_REGISTER && id < FPSCR_REGISTER) { | ||
| 321 | const u128 qN{GetSIMDRegister<u128>(fprs, id - Q0_REGISTER)}; | ||
| 322 | return ValueToHex(qN); | ||
| 323 | } else if (id == FPSCR_REGISTER) { | ||
| 324 | return ValueToHex(context.fpscr); | ||
| 325 | } else { | ||
| 326 | return ""; | ||
| 327 | } | ||
| 328 | } | ||
| 329 | |||
| 330 | void GDBStubA32::RegWrite(Kernel::KThread* thread, size_t id, std::string_view value) const { | ||
| 331 | if (!thread) { | ||
| 332 | return; | ||
| 333 | } | ||
| 334 | |||
| 335 | auto& context{thread->GetContext32()}; | ||
| 336 | auto& fprs{context.extension_registers}; | ||
| 337 | |||
| 338 | if (id <= PC_REGISTER) { | ||
| 339 | context.cpu_registers[id] = HexToValue<u32>(value); | ||
| 340 | } else if (id == CPSR_REGISTER) { | ||
| 341 | context.cpsr = HexToValue<u32>(value); | ||
| 342 | } else if (id >= D0_REGISTER && id < Q0_REGISTER) { | ||
| 343 | PutSIMDRegister(fprs, id - D0_REGISTER, HexToValue<u64>(value)); | ||
| 344 | } else if (id >= Q0_REGISTER && id < FPSCR_REGISTER) { | ||
| 345 | PutSIMDRegister(fprs, id - Q0_REGISTER, HexToValue<u128>(value)); | ||
| 346 | } else if (id == FPSCR_REGISTER) { | ||
| 347 | context.fpscr = HexToValue<u32>(value); | ||
| 348 | } | ||
| 349 | } | ||
| 350 | |||
| 351 | std::string GDBStubA32::ReadRegisters(const Kernel::KThread* thread) const { | ||
| 352 | std::string output; | ||
| 353 | |||
| 354 | for (size_t reg = 0; reg <= FPSCR_REGISTER; reg++) { | ||
| 355 | const bool gpr{reg <= PC_REGISTER}; | ||
| 356 | const bool dfpr{reg >= D0_REGISTER && reg < Q0_REGISTER}; | ||
| 357 | const bool qfpr{reg >= Q0_REGISTER && reg < FPSCR_REGISTER}; | ||
| 358 | |||
| 359 | if (!(gpr || dfpr || qfpr || reg == CPSR_REGISTER || reg == FPSCR_REGISTER)) { | ||
| 360 | continue; | ||
| 361 | } | ||
| 362 | |||
| 363 | output += RegRead(thread, reg); | ||
| 364 | } | ||
| 365 | |||
| 366 | return output; | ||
| 367 | } | ||
| 368 | |||
| 369 | void GDBStubA32::WriteRegisters(Kernel::KThread* thread, std::string_view register_data) const { | ||
| 370 | for (size_t i = 0, reg = 0; reg <= FPSCR_REGISTER; reg++) { | ||
| 371 | const bool gpr{reg <= PC_REGISTER}; | ||
| 372 | const bool dfpr{reg >= D0_REGISTER && reg < Q0_REGISTER}; | ||
| 373 | const bool qfpr{reg >= Q0_REGISTER && reg < FPSCR_REGISTER}; | ||
| 374 | |||
| 375 | if (gpr || reg == CPSR_REGISTER || reg == FPSCR_REGISTER) { | ||
| 376 | RegWrite(thread, reg, register_data.substr(i, 8)); | ||
| 377 | i += 8; | ||
| 378 | } else if (dfpr) { | ||
| 379 | RegWrite(thread, reg, register_data.substr(i, 16)); | ||
| 380 | i += 16; | ||
| 381 | } else if (qfpr) { | ||
| 382 | RegWrite(thread, reg, register_data.substr(i, 32)); | ||
| 383 | i += 32; | ||
| 384 | } | ||
| 385 | |||
| 386 | if (reg == PC_REGISTER) { | ||
| 387 | reg = CPSR_REGISTER - 1; | ||
| 388 | } else if (reg == CPSR_REGISTER) { | ||
| 389 | reg = D0_REGISTER - 1; | ||
| 390 | } | ||
| 391 | } | ||
| 392 | } | ||
| 393 | |||
| 394 | std::string GDBStubA32::ThreadStatus(const Kernel::KThread* thread, u8 signal) const { | ||
| 395 | return fmt::format("T{:02x}{:02x}:{};{:02x}:{};{:02x}:{};thread:{:x};", signal, PC_REGISTER, | ||
| 396 | RegRead(thread, PC_REGISTER), SP_REGISTER, RegRead(thread, SP_REGISTER), | ||
| 397 | LR_REGISTER, RegRead(thread, LR_REGISTER), thread->GetThreadID()); | ||
| 398 | } | ||
| 399 | |||
| 400 | u32 GDBStubA32::BreakpointInstruction() const { | ||
| 401 | // A32: trap | ||
| 402 | // T32: trap + b #4 | ||
| 403 | return 0xe7ffdefe; | ||
| 404 | } | ||
| 405 | |||
| 406 | } // namespace Core | ||
diff --git a/src/core/debugger/gdbstub_arch.h b/src/core/debugger/gdbstub_arch.h new file mode 100644 index 000000000..e943848e5 --- /dev/null +++ b/src/core/debugger/gdbstub_arch.h | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #include <string> | ||
| 7 | |||
| 8 | #include "common/common_types.h" | ||
| 9 | |||
| 10 | namespace Kernel { | ||
| 11 | class KThread; | ||
| 12 | } | ||
| 13 | |||
| 14 | namespace Core { | ||
| 15 | |||
| 16 | class GDBStubArch { | ||
| 17 | public: | ||
| 18 | virtual std::string GetTargetXML() const = 0; | ||
| 19 | virtual std::string RegRead(const Kernel::KThread* thread, size_t id) const = 0; | ||
| 20 | virtual void RegWrite(Kernel::KThread* thread, size_t id, std::string_view value) const = 0; | ||
| 21 | virtual std::string ReadRegisters(const Kernel::KThread* thread) const = 0; | ||
| 22 | virtual void WriteRegisters(Kernel::KThread* thread, std::string_view register_data) const = 0; | ||
| 23 | virtual std::string ThreadStatus(const Kernel::KThread* thread, u8 signal) const = 0; | ||
| 24 | virtual u32 BreakpointInstruction() const = 0; | ||
| 25 | }; | ||
| 26 | |||
| 27 | class GDBStubA64 final : public GDBStubArch { | ||
| 28 | public: | ||
| 29 | std::string GetTargetXML() const override; | ||
| 30 | std::string RegRead(const Kernel::KThread* thread, size_t id) const override; | ||
| 31 | void RegWrite(Kernel::KThread* thread, size_t id, std::string_view value) const override; | ||
| 32 | std::string ReadRegisters(const Kernel::KThread* thread) const override; | ||
| 33 | void WriteRegisters(Kernel::KThread* thread, std::string_view register_data) const override; | ||
| 34 | std::string ThreadStatus(const Kernel::KThread* thread, u8 signal) const override; | ||
| 35 | u32 BreakpointInstruction() const override; | ||
| 36 | |||
| 37 | private: | ||
| 38 | static constexpr u32 LR_REGISTER = 30; | ||
| 39 | static constexpr u32 SP_REGISTER = 31; | ||
| 40 | static constexpr u32 PC_REGISTER = 32; | ||
| 41 | static constexpr u32 PSTATE_REGISTER = 33; | ||
| 42 | static constexpr u32 Q0_REGISTER = 34; | ||
| 43 | static constexpr u32 FPCR_REGISTER = 66; | ||
| 44 | static constexpr u32 FPSR_REGISTER = 67; | ||
| 45 | }; | ||
| 46 | |||
| 47 | class GDBStubA32 final : public GDBStubArch { | ||
| 48 | public: | ||
| 49 | std::string GetTargetXML() const override; | ||
| 50 | std::string RegRead(const Kernel::KThread* thread, size_t id) const override; | ||
| 51 | void RegWrite(Kernel::KThread* thread, size_t id, std::string_view value) const override; | ||
| 52 | std::string ReadRegisters(const Kernel::KThread* thread) const override; | ||
| 53 | void WriteRegisters(Kernel::KThread* thread, std::string_view register_data) const override; | ||
| 54 | std::string ThreadStatus(const Kernel::KThread* thread, u8 signal) const override; | ||
| 55 | u32 BreakpointInstruction() const override; | ||
| 56 | |||
| 57 | private: | ||
| 58 | static constexpr u32 SP_REGISTER = 13; | ||
| 59 | static constexpr u32 LR_REGISTER = 14; | ||
| 60 | static constexpr u32 PC_REGISTER = 15; | ||
| 61 | static constexpr u32 CPSR_REGISTER = 25; | ||
| 62 | static constexpr u32 D0_REGISTER = 32; | ||
| 63 | static constexpr u32 Q0_REGISTER = 64; | ||
| 64 | static constexpr u32 FPSCR_REGISTER = 80; | ||
| 65 | }; | ||
| 66 | |||
| 67 | } // namespace Core | ||