GDB Stub should work now.

This commit is contained in:
Jarek Syrylak
2018-06-04 16:03:01 +01:00
committed by Jarek Syrylak
parent aec90ca506
commit 341517a5f2
3 changed files with 213 additions and 152 deletions

View File

@@ -10,6 +10,7 @@
#include "core/core.h" #include "core/core.h"
#include "core/core_timing.h" #include "core/core_timing.h"
#include "core/hle/kernel/svc.h" #include "core/hle/kernel/svc.h"
#include "core/gdbstub/gdbstub.h"
// Load Unicorn DLL once on Windows using RAII // Load Unicorn DLL once on Windows using RAII
#ifdef _MSC_VER #ifdef _MSC_VER
@@ -35,13 +36,15 @@ LoadDll LoadDll::g_load_dll;
} \ } \
} while (0) } while (0)
static void CodeHook(uc_engine* uc, uint64_t address, uint32_t size, void* user_data) { static GDBStub::BreakpointAddress bkpt = {0};
GDBStub::BreakpointAddress bkpt = static bool bkptHit = false;
GDBStub::GetNextBreakpointFromAddress(address, GDBStub::BreakpointType::Execute);
if (GDBStub::IsMemoryBreak() || static void CodeHook(uc_engine *uc, uint64_t address, uint32_t size, void *user_data)
(bkpt.type != GDBStub::BreakpointType::None && address == bkpt.address)) { {
auto core = static_cast<ARM_Unicorn*>(user_data); bkpt = GDBStub::GetNextBreakpointFromAddress(address, GDBStub::BreakpointType::Execute);
core->RecordBreak(bkpt); if(GDBStub::IsMemoryBreak() || (bkpt.type != GDBStub::BreakpointType::None && address == bkpt.address))
{
bkptHit = true;
uc_emu_stop(uc); uc_emu_stop(uc);
} }
} }
@@ -78,9 +81,9 @@ ARM_Unicorn::ARM_Unicorn() {
uc_hook hook{}; uc_hook hook{};
CHECKED(uc_hook_add(uc, &hook, UC_HOOK_INTR, (void*)InterruptHook, this, 0, -1)); CHECKED(uc_hook_add(uc, &hook, UC_HOOK_INTR, (void*)InterruptHook, this, 0, -1));
CHECKED(uc_hook_add(uc, &hook, UC_HOOK_MEM_INVALID, (void*)UnmappedMemoryHook, this, 0, -1)); CHECKED(uc_hook_add(uc, &hook, UC_HOOK_MEM_INVALID, (void*)UnmappedMemoryHook, this, 0, -1));
if (GDBStub::IsServerEnabled()) { if(GDBStub::IsServerEnabled())
{
CHECKED(uc_hook_add(uc, &hook, UC_HOOK_CODE, (void*)CodeHook, this, 0, -1)); CHECKED(uc_hook_add(uc, &hook, UC_HOOK_CODE, (void*)CodeHook, this, 0, -1));
last_bkpt_hit = false;
} }
} }
@@ -170,9 +173,12 @@ void ARM_Unicorn::SetTlsAddress(VAddr base) {
} }
void ARM_Unicorn::Run() { void ARM_Unicorn::Run() {
if (GDBStub::IsServerEnabled()) { if(GDBStub::IsServerEnabled())
{
ExecuteInstructions(std::max(4000000, 0)); ExecuteInstructions(std::max(4000000, 0));
} else { }
else
{
ExecuteInstructions(std::max(CoreTiming::GetDowncount(), 0)); ExecuteInstructions(std::max(CoreTiming::GetDowncount(), 0));
} }
} }
@@ -187,17 +193,20 @@ void ARM_Unicorn::ExecuteInstructions(int num_instructions) {
MICROPROFILE_SCOPE(ARM_Jit); MICROPROFILE_SCOPE(ARM_Jit);
CHECKED(uc_emu_start(uc, GetPC(), 1ULL << 63, 0, num_instructions)); CHECKED(uc_emu_start(uc, GetPC(), 1ULL << 63, 0, num_instructions));
CoreTiming::AddTicks(num_instructions); CoreTiming::AddTicks(num_instructions);
if (GDBStub::IsServerEnabled()) { if(GDBStub::IsServerEnabled())
if (last_bkpt_hit) { {
uc_reg_write(uc, UC_ARM64_REG_PC, &last_bkpt.address); if(bkptHit)
{
uc_reg_write(uc, UC_ARM64_REG_PC, &bkpt.address);
} }
Kernel::Thread* thread = Kernel::GetCurrentThread(); Kernel::Thread *thread = Kernel::GetCurrentThread();
SaveContext(thread->context); SaveContext(thread->context);
if (last_bkpt_hit) { if(bkptHit)
last_bkpt_hit = false; {
bkptHit = false;
GDBStub::Break(); GDBStub::Break();
} }
GDBStub::SendTrap(thread, 5); GDBStub::SendSig(thread, 5);
} }
} }
@@ -264,8 +273,3 @@ void ARM_Unicorn::PrepareReschedule() {
} }
void ARM_Unicorn::ClearInstructionCache() {} void ARM_Unicorn::ClearInstructionCache() {}
void ARM_Unicorn::RecordBreak(GDBStub::BreakpointAddress bkpt) {
last_bkpt = bkpt;
last_bkpt_hit = true;
}

View File

@@ -16,6 +16,7 @@
#include <fcntl.h> #include <fcntl.h>
#ifdef _WIN32 #ifdef _WIN32
#define NTDDI_VERSION NTDDI_WIN8
#include <winsock2.h> #include <winsock2.h>
// winsock2.h needs to be included first to prevent winsock.h being included by other includes // winsock2.h needs to be included first to prevent winsock.h being included by other includes
#include <io.h> #include <io.h>
@@ -31,16 +32,19 @@
#endif #endif
#include "common/logging/log.h" #include "common/logging/log.h"
//#undef NGLOG_INFO
//#define NGLOG_INFO NGLOG_ERROR
//#undef NGLOG_DEBUG
//#define NGLOG_DEBUG NGLOG_ERROR
#include "common/string_util.h" #include "common/string_util.h"
#include "common/swap.h"
#include "core/arm/arm_interface.h" #include "core/arm/arm_interface.h"
#include "core/core.h" #include "core/core.h"
#include "core/core_cpu.h" #include "core/core_cpu.h"
#include "core/gdbstub/gdbstub.h" #include "core/gdbstub/gdbstub.h"
#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/scheduler.h"
#include "core/loader/loader.h" #include "core/loader/loader.h"
#include "core/memory.h" #include "core/memory.h"
#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/scheduler.h"
const int GDB_BUFFER_SIZE = 10000; const int GDB_BUFFER_SIZE = 10000;
@@ -143,7 +147,8 @@ static u32 command_length;
static u32 latest_signal = 0; static u32 latest_signal = 0;
static bool memory_break = false; static bool memory_break = false;
static Kernel::Thread* current_thread = nullptr; Kernel::Thread *thread = nullptr;
int current_core = 0;
// Binding to a port within the reserved ports range (0-1023) requires root permissions, // Binding to a port within the reserved ports range (0-1023) requires root permissions,
// so default to a port outside of that range. // so default to a port outside of that range.
@@ -151,7 +156,6 @@ static u16 gdbstub_port = 24689;
static bool halt_loop = true; static bool halt_loop = true;
static bool step_loop = false; static bool step_loop = false;
static bool send_trap = false;
// If set to false, the server will never be started and no // If set to false, the server will never be started and no
// gdbstub-related functions will be executed. // gdbstub-related functions will be executed.
@@ -171,49 +175,75 @@ static std::map<u64, Breakpoint> breakpoints_execute;
static std::map<u64, Breakpoint> breakpoints_read; static std::map<u64, Breakpoint> breakpoints_read;
static std::map<u64, Breakpoint> breakpoints_write; static std::map<u64, Breakpoint> breakpoints_write;
static Kernel::Thread* FindThreadById(int id) { Kernel::Thread *FindThread(int id, int& current_core)
for (int core = 0; core < Core::NUM_CPU_CORES; core++) { {
auto threads = Core::System::GetInstance().Scheduler(core)->GetThreadList(); Kernel::Thread *thread = nullptr;
for (auto thread : threads) { for(int core = 0; core < Core::NUM_CPU_CORES; core++)
if (thread->GetThreadId() == id) { {
current_thread = thread.get(); auto list = Core::System::GetInstance().Scheduler(core)->GetThreadList();
return current_thread; for(auto it = list.begin(); it != list.end(); it++)
{
if((*it)->GetThreadId() == id)
{
thread = &(*(*it));
current_core = core;
break;
} }
} }
} }
return nullptr; return thread;
} }
static u64 RegRead(int id, Kernel::Thread* thread = nullptr) { static u64 regr(int id, Kernel::Thread *thread = nullptr)
if (!thread) { {
if(!thread)
{
return 0; return 0;
} }
if (id < SP_REGISTER) { if(id < SP_REGISTER)
{
return thread->context.cpu_registers[id]; return thread->context.cpu_registers[id];
} else if (id == SP_REGISTER) { }
else if(id == SP_REGISTER)
{
return thread->context.sp; return thread->context.sp;
} else if (id == PC_REGISTER) { }
else if(id == PC_REGISTER)
{
return thread->context.pc; return thread->context.pc;
} else if (id == CPSR_REGISTER) { }
else if(id == CPSR_REGISTER)
{
return thread->context.cpsr; return thread->context.cpsr;
} else { }
else
{
return 0; return 0;
} }
} }
static void RegWrite(int id, u64 val, Kernel::Thread* thread = nullptr) { static void regw(int id, u64 val, Kernel::Thread *thread = nullptr)
if (!thread) { {
if(!thread)
{
return; return;
} }
if (id < SP_REGISTER) { if(id < SP_REGISTER)
{
thread->context.cpu_registers[id] = val; thread->context.cpu_registers[id] = val;
} else if (id == SP_REGISTER) { }
else if(id == SP_REGISTER)
{
thread->context.sp = val; thread->context.sp = val;
} else if (id == PC_REGISTER) { }
else if(id == PC_REGISTER)
{
thread->context.pc = val; thread->context.pc = val;
} else if (id == CPSR_REGISTER) { }
else if(id == CPSR_REGISTER)
{
thread->context.cpsr = val; thread->context.cpsr = val;
} }
} }
@@ -232,7 +262,7 @@ static u8 HexCharToValue(u8 hex) {
return hex - 'A' + 0xA; return hex - 'A' + 0xA;
} }
LOG_ERROR(Debug_GDBStub, "Invalid nibble: {} ({:02X})", hex, hex); NGLOG_ERROR(Debug_GDBStub, "Invalid nibble: {} ({:02X})", hex, hex);
return 0; return 0;
} }
@@ -372,7 +402,7 @@ static u8 ReadByte() {
u8 c; u8 c;
size_t received_size = recv(gdbserver_socket, reinterpret_cast<char*>(&c), 1, MSG_WAITALL); size_t received_size = recv(gdbserver_socket, reinterpret_cast<char*>(&c), 1, MSG_WAITALL);
if (received_size != 1) { if (received_size != 1) {
LOG_ERROR(Debug_GDBStub, "recv failed: {}", received_size); NGLOG_ERROR(Debug_GDBStub, "recv failed: {}", received_size);
Shutdown(); Shutdown();
} }
@@ -413,8 +443,8 @@ static void RemoveBreakpoint(BreakpointType type, PAddr addr) {
auto bp = p.find(static_cast<u64>(addr)); auto bp = p.find(static_cast<u64>(addr));
if (bp != p.end()) { if (bp != p.end()) {
LOG_DEBUG(Debug_GDBStub, "gdb: removed a breakpoint: {:016X} bytes at {:016X} of type {}", NGLOG_DEBUG(Debug_GDBStub, "gdb: removed a breakpoint: {:016X} bytes at {:016X} of type {}",
bp->second.len, bp->second.addr, static_cast<int>(type)); bp->second.len, bp->second.addr, static_cast<int>(type));
p.erase(static_cast<u64>(addr)); p.erase(static_cast<u64>(addr));
} }
} }
@@ -459,10 +489,10 @@ bool CheckBreakpoint(PAddr addr, BreakpointType type) {
} }
if (bp->second.active && (addr >= bp->second.addr && addr < bp->second.addr + len)) { if (bp->second.active && (addr >= bp->second.addr && addr < bp->second.addr + len)) {
LOG_DEBUG(Debug_GDBStub, NGLOG_DEBUG(Debug_GDBStub,
"Found breakpoint type {} @ {:016X}, range: {:016X}" "Found breakpoint type {} @ {:016X}, range: {:016X}"
" - {:016X} ({:X} bytes)", " - {:016X} ({:X} bytes)",
static_cast<int>(type), addr, bp->second.addr, bp->second.addr + len, len); static_cast<int>(type), addr, bp->second.addr, bp->second.addr + len, len);
return true; return true;
} }
} }
@@ -478,7 +508,7 @@ bool CheckBreakpoint(PAddr addr, BreakpointType type) {
static void SendPacket(const char packet) { static void SendPacket(const char packet) {
size_t sent_size = send(gdbserver_socket, &packet, 1, 0); size_t sent_size = send(gdbserver_socket, &packet, 1, 0);
if (sent_size != 1) { if (sent_size != 1) {
LOG_ERROR(Debug_GDBStub, "send failed"); NGLOG_ERROR(Debug_GDBStub, "send failed");
} }
} }
@@ -492,13 +522,13 @@ static void SendReply(const char* reply) {
return; return;
} }
LOG_DEBUG(Debug_GDBStub, "Reply: {}", reply); NGLOG_DEBUG(Debug_GDBStub, "Reply: {}", reply);
memset(command_buffer, 0, sizeof(command_buffer)); memset(command_buffer, 0, sizeof(command_buffer));
command_length = static_cast<u32>(strlen(reply)); command_length = static_cast<u32>(strlen(reply));
if (command_length + 4 > sizeof(command_buffer)) { if (command_length + 4 > sizeof(command_buffer)) {
LOG_ERROR(Debug_GDBStub, "command_buffer overflow in SendReply"); NGLOG_ERROR(Debug_GDBStub, "command_buffer overflow in SendReply");
return; return;
} }
@@ -515,7 +545,7 @@ static void SendReply(const char* reply) {
while (left > 0) { while (left > 0) {
int sent_size = send(gdbserver_socket, reinterpret_cast<char*>(ptr), left, 0); int sent_size = send(gdbserver_socket, reinterpret_cast<char*>(ptr), left, 0);
if (sent_size < 0) { if (sent_size < 0) {
LOG_ERROR(Debug_GDBStub, "gdb: send failed"); NGLOG_ERROR(Debug_GDBStub, "gdb: send failed");
return Shutdown(); return Shutdown();
} }
@@ -525,57 +555,80 @@ static void SendReply(const char* reply) {
} }
/// Handle query command from gdb client. /// Handle query command from gdb client.
static void HandleQuery() { static void HandleQuery()
LOG_DEBUG(Debug_GDBStub, "gdb: query '{}'", command_buffer + 1); {
NGLOG_DEBUG(Debug_GDBStub, "gdb: query '{}'", command_buffer + 1);
const char* query = reinterpret_cast<const char*>(command_buffer + 1); const char* query = reinterpret_cast<const char*>(command_buffer + 1);
if (strcmp(query, "TStatus") == 0) { if(strcmp(query, "TStatus") == 0)
{
SendReply("T0"); SendReply("T0");
} else if (strncmp(query, "Supported", strlen("Supported")) == 0) { }
else if(strncmp(query, "Supported", strlen("Supported")) == 0)
{
// PacketSize needs to be large enough for target xml // PacketSize needs to be large enough for target xml
SendReply("PacketSize=2000;qXfer:features:read+"); SendReply("PacketSize=2000;qXfer:features:read+");
} else if (strncmp(query, "Xfer:features:read:target.xml:", }
strlen("Xfer:features:read:target.xml:")) == 0) { else if(strncmp(query, "Xfer:features:read:target.xml:",
strlen("Xfer:features:read:target.xml:")) == 0)
{
SendReply(target_xml); SendReply(target_xml);
} else if (strncmp(query, "Offsets", strlen("Offsets")) == 0) { }
else if(strncmp(query, "Offsets", strlen("Offsets")) == 0)
{
std::string buffer = fmt::format("TextSeg={:0x}", Memory::PROCESS_IMAGE_VADDR); std::string buffer = fmt::format("TextSeg={:0x}", Memory::PROCESS_IMAGE_VADDR);
SendReply(buffer.c_str()); SendReply(buffer.c_str());
} else if (strncmp(query, "fThreadInfo", strlen("fThreadInfo")) == 0) { }
else if(strncmp(query, "fThreadInfo", strlen("fThreadInfo")) == 0)
{
std::string val = "m"; std::string val = "m";
for (int core = 0; core < Core::NUM_CPU_CORES; core++) { for(int core = 0; core < Core::NUM_CPU_CORES; core++)
auto threads = Core::System::GetInstance().Scheduler(core)->GetThreadList(); {
for (auto thread : threads) { auto list = Core::System::GetInstance().Scheduler(core)->GetThreadList();
val += fmt::format("{:x}", thread->GetThreadId()); for(auto it = list.begin(); it != list.end(); it++)
{
char tmp[17] = {0};
memset(tmp, 0, sizeof(tmp));
sprintf(tmp, "%x", (*it)->GetThreadId());
val += (char*)tmp;
val += ","; val += ",";
} }
} }
val.pop_back(); val.pop_back();
SendReply(val.c_str()); SendReply(val.c_str());
} else if (strncmp(query, "sThreadInfo", strlen("sThreadInfo")) == 0) { }
else if(strncmp(query, "sThreadInfo", strlen("sThreadInfo")) == 0)
{
SendReply("l"); SendReply("l");
} else { }
else
{
SendReply(""); SendReply("");
} }
} }
/// Handle set thread command from gdb client. /// Handle set thread command from gdb client.
static void HandleSetThread() { static void HandleSetThread()
if (memcmp(command_buffer, "Hc", 2) == 0 || memcmp(command_buffer, "Hg", 2) == 0) { {
int thread_id = -1; if(memcmp(command_buffer, "Hc", 2) == 0 || memcmp(command_buffer, "Hg", 2) == 0)
if (command_buffer[2] != '-') { {
thread_id = static_cast<int>(HexToInt( int threadid = -1;
command_buffer + 2, if(command_buffer[2] != '-')
command_length - 2 /*strlen(reinterpret_cast<char*>(command_buffer) + 2)*/)); {
threadid = (int)HexToInt(command_buffer + 2, strlen((char*)command_buffer + 2));
} }
if (thread_id >= 1) { if(threadid >= 1)
current_thread = FindThreadById(thread_id); {
thread = FindThread(threadid, current_core);
} }
if (!current_thread) { if(!thread)
thread_id = 1; {
current_thread = FindThreadById(thread_id); threadid = 1;
thread = FindThread(threadid, current_core);
} }
if (current_thread) { if(thread)
{
SendReply("OK"); SendReply("OK");
return; return;
} }
@@ -583,15 +636,15 @@ static void HandleSetThread() {
SendReply("E01"); SendReply("E01");
} }
/// Handle thread alive command from gdb client. static void isThreadAlive()
static void HandleThreadAlive() { {
int thread_id = static_cast<int>( int threadid = (int)HexToInt(command_buffer + 1, strlen((char*)command_buffer + 1));
HexToInt(command_buffer + 1, if(threadid == 0)
command_length - 1 /*strlen(reinterpret_cast<char*>(command_buffer) + 1)*/)); {
if (thread_id == 0) { threadid = 1;
thread_id = 1;
} }
if (FindThreadById(thread_id)) { if(FindThread(threadid, current_core))
{
SendReply("OK"); SendReply("OK");
return; return;
} }
@@ -603,24 +656,29 @@ static void HandleThreadAlive() {
* *
* @param signal Signal to be sent to client. * @param signal Signal to be sent to client.
*/ */
static void SendSignal(Kernel::Thread* thread, u32 signal, bool full = true) { static void SendSignal(Kernel::Thread *thread, u32 signal, bool full = true)
if (gdbserver_socket == -1) { {
if(gdbserver_socket == -1)
{
return; return;
} }
latest_signal = signal; latest_signal = signal;
std::string buffer; std::string buffer;
if (full) { if(full)
buffer = fmt::format("T{:02x}{:02x}:{:016x};{:02x}:{:016x};", latest_signal, PC_REGISTER, {
Common::swap64(RegRead(PC_REGISTER, thread)), SP_REGISTER, buffer = fmt::format("T{:02x}{:02x}:{:016x};{:02x}:{:016x};", latest_signal, PC_REGISTER, htonll(regr(PC_REGISTER, thread)), SP_REGISTER, htonll(regr(SP_REGISTER, thread)));
Common::swap64(RegRead(SP_REGISTER, thread))); }
} else { else
{
buffer = fmt::format("T{:02x};", latest_signal); buffer = fmt::format("T{:02x};", latest_signal);
} }
buffer += fmt::format("thread:{:x};", thread->GetThreadId()); buffer += fmt::format("thread:{:x};", thread->GetThreadId());
//NGLOG_ERROR(Debug_GDBStub, "Sig: {}", buffer.c_str());
SendReply(buffer.c_str()); SendReply(buffer.c_str());
} }
@@ -634,18 +692,18 @@ static void ReadCommand() {
// ignore ack // ignore ack
return; return;
} else if (c == 0x03) { } else if (c == 0x03) {
LOG_INFO(Debug_GDBStub, "gdb: found break command"); NGLOG_INFO(Debug_GDBStub, "gdb: found break command");
halt_loop = true; halt_loop = true;
SendSignal(current_thread, SIGTRAP); SendSignal(thread, SIGTRAP);
return; return;
} else if (c != GDB_STUB_START) { } else if (c != GDB_STUB_START) {
LOG_DEBUG(Debug_GDBStub, "gdb: read invalid byte {:02X}", c); NGLOG_DEBUG(Debug_GDBStub, "gdb: read invalid byte {:02X}", c);
return; return;
} }
while ((c = ReadByte()) != GDB_STUB_END) { while ((c = ReadByte()) != GDB_STUB_END) {
if (command_length >= sizeof(command_buffer)) { if (command_length >= sizeof(command_buffer)) {
LOG_ERROR(Debug_GDBStub, "gdb: command_buffer overflow"); NGLOG_ERROR(Debug_GDBStub, "gdb: command_buffer overflow");
SendPacket(GDB_STUB_NACK); SendPacket(GDB_STUB_NACK);
return; return;
} }
@@ -658,9 +716,10 @@ static void ReadCommand() {
u8 checksum_calculated = CalculateChecksum(command_buffer, command_length); u8 checksum_calculated = CalculateChecksum(command_buffer, command_length);
if (checksum_received != checksum_calculated) { if (checksum_received != checksum_calculated) {
LOG_ERROR(Debug_GDBStub, NGLOG_ERROR(
"gdb: invalid checksum: calculated {:02X} and read {:02X} for ${}# (length: {})", Debug_GDBStub,
checksum_calculated, checksum_received, command_buffer, command_length); "gdb: invalid checksum: calculated {:02X} and read {:02X} for ${}# (length: {})",
checksum_calculated, checksum_received, command_buffer, command_length);
command_length = 0; command_length = 0;
@@ -687,7 +746,7 @@ static bool IsDataAvailable() {
t.tv_usec = 0; t.tv_usec = 0;
if (select(gdbserver_socket + 1, &fd_socket, nullptr, nullptr, &t) < 0) { if (select(gdbserver_socket + 1, &fd_socket, nullptr, nullptr, &t) < 0) {
LOG_ERROR(Debug_GDBStub, "select failed"); NGLOG_ERROR(Debug_GDBStub, "select failed");
return false; return false;
} }
@@ -706,11 +765,11 @@ static void ReadRegister() {
} }
if (id <= SP_REGISTER) { if (id <= SP_REGISTER) {
LongToGdbHex(reply, RegRead(id, current_thread)); LongToGdbHex(reply, regr(id, thread));
} else if (id == PC_REGISTER) { } else if (id == PC_REGISTER) {
LongToGdbHex(reply, RegRead(id, current_thread)); LongToGdbHex(reply, regr(id, thread));
} else if (id == CPSR_REGISTER) { } else if (id == CPSR_REGISTER) {
IntToGdbHex(reply, (u32)RegRead(id, current_thread)); IntToGdbHex(reply, (u32)regr(id, thread));
} else { } else {
return SendReply("E01"); return SendReply("E01");
} }
@@ -726,16 +785,16 @@ static void ReadRegisters() {
u8* bufptr = buffer; u8* bufptr = buffer;
for (int reg = 0; reg <= SP_REGISTER; reg++) { for (int reg = 0; reg <= SP_REGISTER; reg++) {
LongToGdbHex(bufptr + reg * 16, RegRead(reg, current_thread)); LongToGdbHex(bufptr + reg * 16, regr(reg, thread));
} }
bufptr += (32 * 16); bufptr += (32 * 16);
LongToGdbHex(bufptr, RegRead(PC_REGISTER, current_thread)); LongToGdbHex(bufptr, regr(PC_REGISTER, thread));
bufptr += 16; bufptr += 16;
IntToGdbHex(bufptr, (u32)RegRead(CPSR_REGISTER, current_thread)); IntToGdbHex(bufptr, (u32)regr(CPSR_REGISTER, thread));
bufptr += 8; bufptr += 8;
@@ -754,11 +813,11 @@ static void WriteRegister() {
} }
if (id <= SP_REGISTER) { if (id <= SP_REGISTER) {
RegWrite(id, GdbHexToLong(buffer_ptr), current_thread); regw(id, GdbHexToLong(buffer_ptr), thread);
} else if (id == PC_REGISTER) { } else if (id == PC_REGISTER) {
RegWrite(id, GdbHexToLong(buffer_ptr), current_thread); regw(id, GdbHexToLong(buffer_ptr), thread);
} else if (id == CPSR_REGISTER) { } else if (id == CPSR_REGISTER) {
RegWrite(id, GdbHexToInt(buffer_ptr), current_thread); regw(id, GdbHexToInt(buffer_ptr), thread);
} else { } else {
return SendReply("E01"); return SendReply("E01");
} }
@@ -775,11 +834,11 @@ static void WriteRegisters() {
for (int i = 0, reg = 0; reg <= CPSR_REGISTER; i++, reg++) { for (int i = 0, reg = 0; reg <= CPSR_REGISTER; i++, reg++) {
if (reg <= SP_REGISTER) { if (reg <= SP_REGISTER) {
RegWrite(reg, GdbHexToLong(buffer_ptr + i * 16), current_thread); regw(reg, GdbHexToLong(buffer_ptr + i * 16), thread);
} else if (reg == PC_REGISTER) { } else if (reg == PC_REGISTER) {
RegWrite(PC_REGISTER, GdbHexToLong(buffer_ptr + i * 16), current_thread); regw(PC_REGISTER, GdbHexToLong(buffer_ptr + i * 16), thread);
} else if (reg == CPSR_REGISTER) { } else if (reg == CPSR_REGISTER) {
RegWrite(CPSR_REGISTER, GdbHexToInt(buffer_ptr + i * 16), current_thread); regw(CPSR_REGISTER, GdbHexToInt(buffer_ptr + i * 16), thread);
} else { } else {
UNIMPLEMENTED(); UNIMPLEMENTED();
} }
@@ -800,7 +859,7 @@ static void ReadMemory() {
u64 len = u64 len =
HexToLong(start_offset, static_cast<u64>((command_buffer + command_length) - start_offset)); HexToLong(start_offset, static_cast<u64>((command_buffer + command_length) - start_offset));
LOG_DEBUG(Debug_GDBStub, "gdb: addr: {:016X} len: {:016X}", addr, len); NGLOG_DEBUG(Debug_GDBStub, "gdb: addr: {:016X} len: {:016X}", addr, len);
if (len * 2 > sizeof(reply)) { if (len * 2 > sizeof(reply)) {
SendReply("E01"); SendReply("E01");
@@ -839,9 +898,12 @@ static void WriteMemory() {
SendReply("OK"); SendReply("OK");
} }
bool send_trap = false;
void Break(bool is_memory_break) { void Break(bool is_memory_break) {
if (!halt_loop) { if (!halt_loop) {
halt_loop = true; halt_loop = true;
//SendSignal(SIGTRAP);
send_trap = true; send_trap = true;
} }
@@ -852,10 +914,10 @@ void Break(bool is_memory_break) {
static void Step() { static void Step() {
step_loop = true; step_loop = true;
halt_loop = true; halt_loop = true;
//SendSignal(SIGTRAP);
send_trap = true; send_trap = true;
} }
/// Tell the CPU if we hit a memory breakpoint.
bool IsMemoryBreak() { bool IsMemoryBreak() {
if (IsConnected()) { if (IsConnected()) {
return false; return false;
@@ -887,8 +949,8 @@ static bool CommitBreakpoint(BreakpointType type, PAddr addr, u64 len) {
breakpoint.len = len; breakpoint.len = len;
p.insert({addr, breakpoint}); p.insert({addr, breakpoint});
LOG_DEBUG(Debug_GDBStub, "gdb: added {} breakpoint: {:016X} bytes at {:016X}", NGLOG_DEBUG(Debug_GDBStub, "gdb: added {} breakpoint: {:016X} bytes at {:016X}",
static_cast<int>(type), breakpoint.len, breakpoint.addr); static_cast<int>(type), breakpoint.len, breakpoint.addr);
return true; return true;
} }
@@ -995,7 +1057,7 @@ void HandlePacket() {
return; return;
} }
LOG_DEBUG(Debug_GDBStub, "Packet: {}", command_buffer); NGLOG_DEBUG(Debug_GDBStub, "Packet: {}", command_buffer);
switch (command_buffer[0]) { switch (command_buffer[0]) {
case 'q': case 'q':
@@ -1005,11 +1067,11 @@ void HandlePacket() {
HandleSetThread(); HandleSetThread();
break; break;
case '?': case '?':
SendSignal(current_thread, latest_signal); SendSignal(thread, latest_signal);
break; break;
case 'k': case 'k':
Shutdown(); Shutdown();
LOG_INFO(Debug_GDBStub, "killed by gdb"); NGLOG_INFO(Debug_GDBStub, "killed by gdb");
return; return;
case 'g': case 'g':
ReadRegisters(); ReadRegisters();
@@ -1043,7 +1105,7 @@ void HandlePacket() {
AddBreakpoint(); AddBreakpoint();
break; break;
case 'T': case 'T':
HandleThreadAlive(); isThreadAlive();
break; break;
default: default:
SendReply(""); SendReply("");
@@ -1091,7 +1153,7 @@ static void Init(u16 port) {
breakpoints_write.clear(); breakpoints_write.clear();
// Start gdb server // Start gdb server
LOG_INFO(Debug_GDBStub, "Starting GDB server on port {}...", port); NGLOG_INFO(Debug_GDBStub, "Starting GDB server on port {}...", port);
sockaddr_in saddr_server = {}; sockaddr_in saddr_server = {};
saddr_server.sin_family = AF_INET; saddr_server.sin_family = AF_INET;
@@ -1104,28 +1166,28 @@ static void Init(u16 port) {
int tmpsock = static_cast<int>(socket(PF_INET, SOCK_STREAM, 0)); int tmpsock = static_cast<int>(socket(PF_INET, SOCK_STREAM, 0));
if (tmpsock == -1) { if (tmpsock == -1) {
LOG_ERROR(Debug_GDBStub, "Failed to create gdb socket"); NGLOG_ERROR(Debug_GDBStub, "Failed to create gdb socket");
} }
// Set socket to SO_REUSEADDR so it can always bind on the same port // Set socket to SO_REUSEADDR so it can always bind on the same port
int reuse_enabled = 1; int reuse_enabled = 1;
if (setsockopt(tmpsock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse_enabled, if (setsockopt(tmpsock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse_enabled,
sizeof(reuse_enabled)) < 0) { sizeof(reuse_enabled)) < 0) {
LOG_ERROR(Debug_GDBStub, "Failed to set gdb socket option"); NGLOG_ERROR(Debug_GDBStub, "Failed to set gdb socket option");
} }
const sockaddr* server_addr = reinterpret_cast<const sockaddr*>(&saddr_server); const sockaddr* server_addr = reinterpret_cast<const sockaddr*>(&saddr_server);
socklen_t server_addrlen = sizeof(saddr_server); socklen_t server_addrlen = sizeof(saddr_server);
if (bind(tmpsock, server_addr, server_addrlen) < 0) { if (bind(tmpsock, server_addr, server_addrlen) < 0) {
LOG_ERROR(Debug_GDBStub, "Failed to bind gdb socket"); NGLOG_ERROR(Debug_GDBStub, "Failed to bind gdb socket");
} }
if (listen(tmpsock, 1) < 0) { if (listen(tmpsock, 1) < 0) {
LOG_ERROR(Debug_GDBStub, "Failed to listen to gdb socket"); NGLOG_ERROR(Debug_GDBStub, "Failed to listen to gdb socket");
} }
// Wait for gdb to connect // Wait for gdb to connect
LOG_INFO(Debug_GDBStub, "Waiting for gdb to connect..."); NGLOG_INFO(Debug_GDBStub, "Waiting for gdb to connect...");
sockaddr_in saddr_client; sockaddr_in saddr_client;
sockaddr* client_addr = reinterpret_cast<sockaddr*>(&saddr_client); sockaddr* client_addr = reinterpret_cast<sockaddr*>(&saddr_client);
socklen_t client_addrlen = sizeof(saddr_client); socklen_t client_addrlen = sizeof(saddr_client);
@@ -1136,9 +1198,9 @@ static void Init(u16 port) {
halt_loop = false; halt_loop = false;
step_loop = false; step_loop = false;
LOG_ERROR(Debug_GDBStub, "Failed to accept gdb client"); NGLOG_ERROR(Debug_GDBStub, "Failed to accept gdb client");
} else { } else {
LOG_INFO(Debug_GDBStub, "Client connected."); NGLOG_INFO(Debug_GDBStub, "Client connected.");
saddr_client.sin_addr.s_addr = ntohl(saddr_client.sin_addr.s_addr); saddr_client.sin_addr.s_addr = ntohl(saddr_client.sin_addr.s_addr);
} }
@@ -1157,7 +1219,7 @@ void Shutdown() {
return; return;
} }
LOG_INFO(Debug_GDBStub, "Stopping GDB ..."); NGLOG_INFO(Debug_GDBStub, "Stopping GDB ...");
if (gdbserver_socket != -1) { if (gdbserver_socket != -1) {
shutdown(gdbserver_socket, SHUT_RDWR); shutdown(gdbserver_socket, SHUT_RDWR);
gdbserver_socket = -1; gdbserver_socket = -1;
@@ -1167,7 +1229,7 @@ void Shutdown() {
WSACleanup(); WSACleanup();
#endif #endif
LOG_INFO(Debug_GDBStub, "GDB stopped."); NGLOG_INFO(Debug_GDBStub, "GDB stopped.");
} }
bool IsServerEnabled() { bool IsServerEnabled() {
@@ -1190,10 +1252,12 @@ void SetCpuStepFlag(bool is_step) {
step_loop = is_step; step_loop = is_step;
} }
void SendTrap(Kernel::Thread* thread, int trap) { void SendSig(void *_thread, int sig)
if (send_trap) { {
if(send_trap)
{
send_trap = false; send_trap = false;
SendSignal(thread, trap); SendSignal((Kernel::Thread *)_thread, sig);
} }
} }
}; // namespace GDBStub }; // namespace GDBStub

View File

@@ -7,7 +7,6 @@
#pragma once #pragma once
#include "common/common_types.h" #include "common/common_types.h"
#include "core/hle/kernel/thread.h"
namespace GDBStub { namespace GDBStub {
@@ -93,11 +92,5 @@ bool GetCpuStepFlag();
*/ */
void SetCpuStepFlag(bool is_step); void SetCpuStepFlag(bool is_step);
/** void SendSig(void *thread, int sig);
* Send trap signal from thread back to the gdbstub server.
*
* @param thread Sending thread.
* @param trap Trap no.
*/
void SendTrap(Kernel::Thread* thread, int trap);
} // namespace GDBStub } // namespace GDBStub