summaryrefslogtreecommitdiff
path: root/src/core/debugger
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/debugger')
-rw-r--r--src/core/debugger/debugger.cpp39
-rw-r--r--src/core/debugger/gdbstub.cpp66
-rw-r--r--src/core/debugger/gdbstub.h15
3 files changed, 78 insertions, 42 deletions
diff --git a/src/core/debugger/debugger.cpp b/src/core/debugger/debugger.cpp
index 0e270eb50..e86aae846 100644
--- a/src/core/debugger/debugger.cpp
+++ b/src/core/debugger/debugger.cpp
@@ -114,7 +114,7 @@ public:
114 } 114 }
115 115
116 Kernel::KThread* GetActiveThread() override { 116 Kernel::KThread* GetActiveThread() override {
117 return state->active_thread; 117 return state->active_thread.GetPointerUnsafe();
118 } 118 }
119 119
120private: 120private:
@@ -147,11 +147,14 @@ private:
147 147
148 std::scoped_lock lk{connection_lock}; 148 std::scoped_lock lk{connection_lock};
149 149
150 // Find the process we are going to debug.
151 SetDebugProcess();
152
150 // Ensure everything is stopped. 153 // Ensure everything is stopped.
151 PauseEmulation(); 154 PauseEmulation();
152 155
153 // Set up the new frontend. 156 // Set up the new frontend.
154 frontend = std::make_unique<GDBStub>(*this, system); 157 frontend = std::make_unique<GDBStub>(*this, system, debug_process.GetPointerUnsafe());
155 158
156 // Set the new state. This will tear down any existing state. 159 // Set the new state. This will tear down any existing state.
157 state = ConnectionState{ 160 state = ConnectionState{
@@ -194,15 +197,20 @@ private:
194 UpdateActiveThread(); 197 UpdateActiveThread();
195 198
196 if (state->info.type == SignalType::Watchpoint) { 199 if (state->info.type == SignalType::Watchpoint) {
197 frontend->Watchpoint(state->active_thread, *state->info.watchpoint); 200 frontend->Watchpoint(std::addressof(*state->active_thread),
201 *state->info.watchpoint);
198 } else { 202 } else {
199 frontend->Stopped(state->active_thread); 203 frontend->Stopped(std::addressof(*state->active_thread));
200 } 204 }
201 205
202 break; 206 break;
203 case SignalType::ShuttingDown: 207 case SignalType::ShuttingDown:
204 frontend->ShuttingDown(); 208 frontend->ShuttingDown();
205 209
210 // Release members.
211 state->active_thread.Reset(nullptr);
212 debug_process.Reset(nullptr);
213
206 // Wait for emulation to shut down gracefully now. 214 // Wait for emulation to shut down gracefully now.
207 state->signal_pipe.close(); 215 state->signal_pipe.close();
208 state->client_socket.shutdown(boost::asio::socket_base::shutdown_both); 216 state->client_socket.shutdown(boost::asio::socket_base::shutdown_both);
@@ -222,7 +230,7 @@ private:
222 stopped = true; 230 stopped = true;
223 PauseEmulation(); 231 PauseEmulation();
224 UpdateActiveThread(); 232 UpdateActiveThread();
225 frontend->Stopped(state->active_thread); 233 frontend->Stopped(state->active_thread.GetPointerUnsafe());
226 break; 234 break;
227 } 235 }
228 case DebuggerAction::Continue: 236 case DebuggerAction::Continue:
@@ -232,7 +240,7 @@ private:
232 MarkResumed([&] { 240 MarkResumed([&] {
233 state->active_thread->SetStepState(Kernel::StepState::StepPending); 241 state->active_thread->SetStepState(Kernel::StepState::StepPending);
234 state->active_thread->Resume(Kernel::SuspendType::Debug); 242 state->active_thread->Resume(Kernel::SuspendType::Debug);
235 ResumeEmulation(state->active_thread); 243 ResumeEmulation(state->active_thread.GetPointerUnsafe());
236 }); 244 });
237 break; 245 break;
238 case DebuggerAction::StepThreadLocked: { 246 case DebuggerAction::StepThreadLocked: {
@@ -255,6 +263,7 @@ private:
255 } 263 }
256 264
257 void PauseEmulation() { 265 void PauseEmulation() {
266 Kernel::KScopedLightLock ll{debug_process->GetListLock()};
258 Kernel::KScopedSchedulerLock sl{system.Kernel()}; 267 Kernel::KScopedSchedulerLock sl{system.Kernel()};
259 268
260 // Put all threads to sleep on next scheduler round. 269 // Put all threads to sleep on next scheduler round.
@@ -264,6 +273,9 @@ private:
264 } 273 }
265 274
266 void ResumeEmulation(Kernel::KThread* except = nullptr) { 275 void ResumeEmulation(Kernel::KThread* except = nullptr) {
276 Kernel::KScopedLightLock ll{debug_process->GetListLock()};
277 Kernel::KScopedSchedulerLock sl{system.Kernel()};
278
267 // Wake up all threads. 279 // Wake up all threads.
268 for (auto& thread : ThreadList()) { 280 for (auto& thread : ThreadList()) {
269 if (std::addressof(thread) == except) { 281 if (std::addressof(thread) == except) {
@@ -277,15 +289,16 @@ private:
277 289
278 template <typename Callback> 290 template <typename Callback>
279 void MarkResumed(Callback&& cb) { 291 void MarkResumed(Callback&& cb) {
280 Kernel::KScopedSchedulerLock sl{system.Kernel()};
281 stopped = false; 292 stopped = false;
282 cb(); 293 cb();
283 } 294 }
284 295
285 void UpdateActiveThread() { 296 void UpdateActiveThread() {
297 Kernel::KScopedLightLock ll{debug_process->GetListLock()};
298
286 auto& threads{ThreadList()}; 299 auto& threads{ThreadList()};
287 for (auto& thread : threads) { 300 for (auto& thread : threads) {
288 if (std::addressof(thread) == state->active_thread) { 301 if (std::addressof(thread) == state->active_thread.GetPointerUnsafe()) {
289 // Thread is still alive, no need to update. 302 // Thread is still alive, no need to update.
290 return; 303 return;
291 } 304 }
@@ -293,12 +306,18 @@ private:
293 state->active_thread = std::addressof(threads.front()); 306 state->active_thread = std::addressof(threads.front());
294 } 307 }
295 308
309private:
310 void SetDebugProcess() {
311 debug_process = std::move(system.Kernel().GetProcessList().back());
312 }
313
296 Kernel::KProcess::ThreadList& ThreadList() { 314 Kernel::KProcess::ThreadList& ThreadList() {
297 return system.ApplicationProcess()->GetThreadList(); 315 return debug_process->GetThreadList();
298 } 316 }
299 317
300private: 318private:
301 System& system; 319 System& system;
320 Kernel::KScopedAutoObject<Kernel::KProcess> debug_process;
302 std::unique_ptr<DebuggerFrontend> frontend; 321 std::unique_ptr<DebuggerFrontend> frontend;
303 322
304 boost::asio::io_context io_context; 323 boost::asio::io_context io_context;
@@ -310,7 +329,7 @@ private:
310 boost::process::async_pipe signal_pipe; 329 boost::process::async_pipe signal_pipe;
311 330
312 SignalInfo info; 331 SignalInfo info;
313 Kernel::KThread* active_thread; 332 Kernel::KScopedAutoObject<Kernel::KThread> active_thread;
314 std::array<u8, 4096> client_data; 333 std::array<u8, 4096> client_data;
315 bool pipe_data; 334 bool pipe_data;
316 }; 335 };
diff --git a/src/core/debugger/gdbstub.cpp b/src/core/debugger/gdbstub.cpp
index 4051ed4af..80091cc7e 100644
--- a/src/core/debugger/gdbstub.cpp
+++ b/src/core/debugger/gdbstub.cpp
@@ -108,9 +108,9 @@ static std::string EscapeXML(std::string_view data) {
108 return escaped; 108 return escaped;
109} 109}
110 110
111GDBStub::GDBStub(DebuggerBackend& backend_, Core::System& system_) 111GDBStub::GDBStub(DebuggerBackend& backend_, Core::System& system_, Kernel::KProcess* debug_process_)
112 : DebuggerFrontend(backend_), system{system_} { 112 : DebuggerFrontend(backend_), system{system_}, debug_process{debug_process_} {
113 if (system.ApplicationProcess()->Is64Bit()) { 113 if (GetProcess()->Is64Bit()) {
114 arch = std::make_unique<GDBStubA64>(); 114 arch = std::make_unique<GDBStubA64>();
115 } else { 115 } else {
116 arch = std::make_unique<GDBStubA32>(); 116 arch = std::make_unique<GDBStubA32>();
@@ -276,7 +276,7 @@ void GDBStub::ExecuteCommand(std::string_view packet, std::vector<DebuggerAction
276 const size_t size{static_cast<size_t>(strtoll(command.data() + sep, nullptr, 16))}; 276 const size_t size{static_cast<size_t>(strtoll(command.data() + sep, nullptr, 16))};
277 277
278 std::vector<u8> mem(size); 278 std::vector<u8> mem(size);
279 if (system.ApplicationMemory().ReadBlock(addr, mem.data(), size)) { 279 if (GetMemory().ReadBlock(addr, mem.data(), size)) {
280 // Restore any bytes belonging to replaced instructions. 280 // Restore any bytes belonging to replaced instructions.
281 auto it = replaced_instructions.lower_bound(addr); 281 auto it = replaced_instructions.lower_bound(addr);
282 for (; it != replaced_instructions.end() && it->first < addr + size; it++) { 282 for (; it != replaced_instructions.end() && it->first < addr + size; it++) {
@@ -310,8 +310,8 @@ void GDBStub::ExecuteCommand(std::string_view packet, std::vector<DebuggerAction
310 const auto mem_substr{std::string_view(command).substr(mem_sep)}; 310 const auto mem_substr{std::string_view(command).substr(mem_sep)};
311 const auto mem{Common::HexStringToVector(mem_substr, false)}; 311 const auto mem{Common::HexStringToVector(mem_substr, false)};
312 312
313 if (system.ApplicationMemory().WriteBlock(addr, mem.data(), size)) { 313 if (GetMemory().WriteBlock(addr, mem.data(), size)) {
314 Core::InvalidateInstructionCacheRange(system.ApplicationProcess(), addr, size); 314 Core::InvalidateInstructionCacheRange(GetProcess(), addr, size);
315 SendReply(GDB_STUB_REPLY_OK); 315 SendReply(GDB_STUB_REPLY_OK);
316 } else { 316 } else {
317 SendReply(GDB_STUB_REPLY_ERR); 317 SendReply(GDB_STUB_REPLY_ERR);
@@ -353,7 +353,7 @@ void GDBStub::HandleBreakpointInsert(std::string_view command) {
353 const size_t addr{static_cast<size_t>(strtoll(command.data() + addr_sep, nullptr, 16))}; 353 const size_t addr{static_cast<size_t>(strtoll(command.data() + addr_sep, nullptr, 16))};
354 const size_t size{static_cast<size_t>(strtoll(command.data() + size_sep, nullptr, 16))}; 354 const size_t size{static_cast<size_t>(strtoll(command.data() + size_sep, nullptr, 16))};
355 355
356 if (!system.ApplicationMemory().IsValidVirtualAddressRange(addr, size)) { 356 if (!GetMemory().IsValidVirtualAddressRange(addr, size)) {
357 SendReply(GDB_STUB_REPLY_ERR); 357 SendReply(GDB_STUB_REPLY_ERR);
358 return; 358 return;
359 } 359 }
@@ -362,22 +362,20 @@ void GDBStub::HandleBreakpointInsert(std::string_view command) {
362 362
363 switch (type) { 363 switch (type) {
364 case BreakpointType::Software: 364 case BreakpointType::Software:
365 replaced_instructions[addr] = system.ApplicationMemory().Read32(addr); 365 replaced_instructions[addr] = GetMemory().Read32(addr);
366 system.ApplicationMemory().Write32(addr, arch->BreakpointInstruction()); 366 GetMemory().Write32(addr, arch->BreakpointInstruction());
367 Core::InvalidateInstructionCacheRange(system.ApplicationProcess(), addr, sizeof(u32)); 367 Core::InvalidateInstructionCacheRange(GetProcess(), addr, sizeof(u32));
368 success = true; 368 success = true;
369 break; 369 break;
370 case BreakpointType::WriteWatch: 370 case BreakpointType::WriteWatch:
371 success = system.ApplicationProcess()->InsertWatchpoint(addr, size, 371 success = GetProcess()->InsertWatchpoint(addr, size, Kernel::DebugWatchpointType::Write);
372 Kernel::DebugWatchpointType::Write);
373 break; 372 break;
374 case BreakpointType::ReadWatch: 373 case BreakpointType::ReadWatch:
375 success = system.ApplicationProcess()->InsertWatchpoint(addr, size, 374 success = GetProcess()->InsertWatchpoint(addr, size, Kernel::DebugWatchpointType::Read);
376 Kernel::DebugWatchpointType::Read);
377 break; 375 break;
378 case BreakpointType::AccessWatch: 376 case BreakpointType::AccessWatch:
379 success = system.ApplicationProcess()->InsertWatchpoint( 377 success =
380 addr, size, Kernel::DebugWatchpointType::ReadOrWrite); 378 GetProcess()->InsertWatchpoint(addr, size, Kernel::DebugWatchpointType::ReadOrWrite);
381 break; 379 break;
382 case BreakpointType::Hardware: 380 case BreakpointType::Hardware:
383 default: 381 default:
@@ -400,7 +398,7 @@ void GDBStub::HandleBreakpointRemove(std::string_view command) {
400 const size_t addr{static_cast<size_t>(strtoll(command.data() + addr_sep, nullptr, 16))}; 398 const size_t addr{static_cast<size_t>(strtoll(command.data() + addr_sep, nullptr, 16))};
401 const size_t size{static_cast<size_t>(strtoll(command.data() + size_sep, nullptr, 16))}; 399 const size_t size{static_cast<size_t>(strtoll(command.data() + size_sep, nullptr, 16))};
402 400
403 if (!system.ApplicationMemory().IsValidVirtualAddressRange(addr, size)) { 401 if (!GetMemory().IsValidVirtualAddressRange(addr, size)) {
404 SendReply(GDB_STUB_REPLY_ERR); 402 SendReply(GDB_STUB_REPLY_ERR);
405 return; 403 return;
406 } 404 }
@@ -411,24 +409,22 @@ void GDBStub::HandleBreakpointRemove(std::string_view command) {
411 case BreakpointType::Software: { 409 case BreakpointType::Software: {
412 const auto orig_insn{replaced_instructions.find(addr)}; 410 const auto orig_insn{replaced_instructions.find(addr)};
413 if (orig_insn != replaced_instructions.end()) { 411 if (orig_insn != replaced_instructions.end()) {
414 system.ApplicationMemory().Write32(addr, orig_insn->second); 412 GetMemory().Write32(addr, orig_insn->second);
415 Core::InvalidateInstructionCacheRange(system.ApplicationProcess(), addr, sizeof(u32)); 413 Core::InvalidateInstructionCacheRange(GetProcess(), addr, sizeof(u32));
416 replaced_instructions.erase(addr); 414 replaced_instructions.erase(addr);
417 success = true; 415 success = true;
418 } 416 }
419 break; 417 break;
420 } 418 }
421 case BreakpointType::WriteWatch: 419 case BreakpointType::WriteWatch:
422 success = system.ApplicationProcess()->RemoveWatchpoint(addr, size, 420 success = GetProcess()->RemoveWatchpoint(addr, size, Kernel::DebugWatchpointType::Write);
423 Kernel::DebugWatchpointType::Write);
424 break; 421 break;
425 case BreakpointType::ReadWatch: 422 case BreakpointType::ReadWatch:
426 success = system.ApplicationProcess()->RemoveWatchpoint(addr, size, 423 success = GetProcess()->RemoveWatchpoint(addr, size, Kernel::DebugWatchpointType::Read);
427 Kernel::DebugWatchpointType::Read);
428 break; 424 break;
429 case BreakpointType::AccessWatch: 425 case BreakpointType::AccessWatch:
430 success = system.ApplicationProcess()->RemoveWatchpoint( 426 success =
431 addr, size, Kernel::DebugWatchpointType::ReadOrWrite); 427 GetProcess()->RemoveWatchpoint(addr, size, Kernel::DebugWatchpointType::ReadOrWrite);
432 break; 428 break;
433 case BreakpointType::Hardware: 429 case BreakpointType::Hardware:
434 default: 430 default:
@@ -466,10 +462,10 @@ void GDBStub::HandleQuery(std::string_view command) {
466 const auto target_xml{arch->GetTargetXML()}; 462 const auto target_xml{arch->GetTargetXML()};
467 SendReply(PaginateBuffer(target_xml, command.substr(30))); 463 SendReply(PaginateBuffer(target_xml, command.substr(30)));
468 } else if (command.starts_with("Offsets")) { 464 } else if (command.starts_with("Offsets")) {
469 const auto main_offset = Core::FindMainModuleEntrypoint(system.ApplicationProcess()); 465 const auto main_offset = Core::FindMainModuleEntrypoint(GetProcess());
470 SendReply(fmt::format("TextSeg={:x}", GetInteger(main_offset))); 466 SendReply(fmt::format("TextSeg={:x}", GetInteger(main_offset)));
471 } else if (command.starts_with("Xfer:libraries:read::")) { 467 } else if (command.starts_with("Xfer:libraries:read::")) {
472 auto modules = Core::FindModules(system.ApplicationProcess()); 468 auto modules = Core::FindModules(GetProcess());
473 469
474 std::string buffer; 470 std::string buffer;
475 buffer += R"(<?xml version="1.0"?>)"; 471 buffer += R"(<?xml version="1.0"?>)";
@@ -483,7 +479,7 @@ void GDBStub::HandleQuery(std::string_view command) {
483 SendReply(PaginateBuffer(buffer, command.substr(21))); 479 SendReply(PaginateBuffer(buffer, command.substr(21)));
484 } else if (command.starts_with("fThreadInfo")) { 480 } else if (command.starts_with("fThreadInfo")) {
485 // beginning of list 481 // beginning of list
486 const auto& threads = system.ApplicationProcess()->GetThreadList(); 482 const auto& threads = GetProcess()->GetThreadList();
487 std::vector<std::string> thread_ids; 483 std::vector<std::string> thread_ids;
488 for (const auto& thread : threads) { 484 for (const auto& thread : threads) {
489 thread_ids.push_back(fmt::format("{:x}", thread.GetThreadId())); 485 thread_ids.push_back(fmt::format("{:x}", thread.GetThreadId()));
@@ -497,7 +493,7 @@ void GDBStub::HandleQuery(std::string_view command) {
497 buffer += R"(<?xml version="1.0"?>)"; 493 buffer += R"(<?xml version="1.0"?>)";
498 buffer += "<threads>"; 494 buffer += "<threads>";
499 495
500 const auto& threads = system.ApplicationProcess()->GetThreadList(); 496 const auto& threads = GetProcess()->GetThreadList();
501 for (const auto& thread : threads) { 497 for (const auto& thread : threads) {
502 auto thread_name{Core::GetThreadName(&thread)}; 498 auto thread_name{Core::GetThreadName(&thread)};
503 if (!thread_name) { 499 if (!thread_name) {
@@ -613,7 +609,7 @@ void GDBStub::HandleRcmd(const std::vector<u8>& command) {
613 std::string_view command_str{reinterpret_cast<const char*>(&command[0]), command.size()}; 609 std::string_view command_str{reinterpret_cast<const char*>(&command[0]), command.size()};
614 std::string reply; 610 std::string reply;
615 611
616 auto* process = system.ApplicationProcess(); 612 auto* process = GetProcess();
617 auto& page_table = process->GetPageTable(); 613 auto& page_table = process->GetPageTable();
618 614
619 const char* commands = "Commands:\n" 615 const char* commands = "Commands:\n"
@@ -714,7 +710,7 @@ void GDBStub::HandleRcmd(const std::vector<u8>& command) {
714} 710}
715 711
716Kernel::KThread* GDBStub::GetThreadByID(u64 thread_id) { 712Kernel::KThread* GDBStub::GetThreadByID(u64 thread_id) {
717 auto& threads{system.ApplicationProcess()->GetThreadList()}; 713 auto& threads{GetProcess()->GetThreadList()};
718 for (auto& thread : threads) { 714 for (auto& thread : threads) {
719 if (thread.GetThreadId() == thread_id) { 715 if (thread.GetThreadId() == thread_id) {
720 return std::addressof(thread); 716 return std::addressof(thread);
@@ -783,4 +779,12 @@ void GDBStub::SendStatus(char status) {
783 backend.WriteToClient(buf); 779 backend.WriteToClient(buf);
784} 780}
785 781
782Kernel::KProcess* GDBStub::GetProcess() {
783 return debug_process;
784}
785
786Core::Memory::Memory& GDBStub::GetMemory() {
787 return GetProcess()->GetMemory();
788}
789
786} // namespace Core 790} // namespace Core
diff --git a/src/core/debugger/gdbstub.h b/src/core/debugger/gdbstub.h
index 368197920..232dcf49f 100644
--- a/src/core/debugger/gdbstub.h
+++ b/src/core/debugger/gdbstub.h
@@ -12,13 +12,22 @@
12#include "core/debugger/debugger_interface.h" 12#include "core/debugger/debugger_interface.h"
13#include "core/debugger/gdbstub_arch.h" 13#include "core/debugger/gdbstub_arch.h"
14 14
15namespace Kernel {
16class KProcess;
17}
18
19namespace Core::Memory {
20class Memory;
21}
22
15namespace Core { 23namespace Core {
16 24
17class System; 25class System;
18 26
19class GDBStub : public DebuggerFrontend { 27class GDBStub : public DebuggerFrontend {
20public: 28public:
21 explicit GDBStub(DebuggerBackend& backend, Core::System& system); 29 explicit GDBStub(DebuggerBackend& backend, Core::System& system,
30 Kernel::KProcess* debug_process);
22 ~GDBStub() override; 31 ~GDBStub() override;
23 32
24 void Connected() override; 33 void Connected() override;
@@ -42,8 +51,12 @@ private:
42 void SendReply(std::string_view data); 51 void SendReply(std::string_view data);
43 void SendStatus(char status); 52 void SendStatus(char status);
44 53
54 Kernel::KProcess* GetProcess();
55 Core::Memory::Memory& GetMemory();
56
45private: 57private:
46 Core::System& system; 58 Core::System& system;
59 Kernel::KProcess* debug_process;
47 std::unique_ptr<GDBStubArch> arch; 60 std::unique_ptr<GDBStubArch> arch;
48 std::vector<char> current_command; 61 std::vector<char> current_command;
49 std::map<VAddr, u32> replaced_instructions; 62 std::map<VAddr, u32> replaced_instructions;