summaryrefslogtreecommitdiff
path: root/libpcsxcore
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2020-05-23 01:06:07 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2020-05-23 18:05:53 +0200
commit470da05658a97a51bd2ad2db7834bcc13dd995fd (patch)
treef2c85327bfe13813098446ff91a61b725116fa86 /libpcsxcore
parentaea565d558f929729c0cb889f9c3296f2f487d0d (diff)
downloadpcsxr-470da05658a97a51bd2ad2db7834bcc13dd995fd.tar.gz
Started implementing GDB server
Diffstat (limited to 'libpcsxcore')
-rw-r--r--libpcsxcore/CMakeLists.txt1
-rw-r--r--libpcsxcore/debug.c2
-rw-r--r--libpcsxcore/debug.h2
-rw-r--r--libpcsxcore/gdb_server.c128
-rw-r--r--libpcsxcore/gdb_server.h10
-rw-r--r--libpcsxcore/psxcommon.h1
-rw-r--r--libpcsxcore/psxcounters.c1
-rw-r--r--libpcsxcore/psxinterpreter.c45
-rw-r--r--libpcsxcore/socket.c4
9 files changed, 169 insertions, 25 deletions
diff --git a/libpcsxcore/CMakeLists.txt b/libpcsxcore/CMakeLists.txt
index 827a8f0a..04633ce1 100644
--- a/libpcsxcore/CMakeLists.txt
+++ b/libpcsxcore/CMakeLists.txt
@@ -98,6 +98,7 @@ set(SRCS psxbios.c
pgxp_gte.c
pgxp_mem.c
pgxp_value.c
+ gdb_server.c
)
set(LIBS "-lm")
diff --git a/libpcsxcore/debug.c b/libpcsxcore/debug.c
index 463df73b..972343d4 100644
--- a/libpcsxcore/debug.c
+++ b/libpcsxcore/debug.c
@@ -474,7 +474,7 @@ void ProcessDebug() {
MarkMap(_Rd_, MAP_EXEC_JAL);
}
}
- while (paused) {
+ while (paused || GdbServerRunning()) {
if (client_socket < 1)
{
diff --git a/libpcsxcore/debug.h b/libpcsxcore/debug.h
index f54c4883..4ca2e5ec 100644
--- a/libpcsxcore/debug.h
+++ b/libpcsxcore/debug.h
@@ -46,7 +46,7 @@ extern char *disRNameCP0[];
char* disR3000AF(u32 code, u32 pc);
-/*
+/*
* Specficies which logs should be activated.
*/
diff --git a/libpcsxcore/gdb_server.c b/libpcsxcore/gdb_server.c
new file mode 100644
index 00000000..7f7ea2ca
--- /dev/null
+++ b/libpcsxcore/gdb_server.c
@@ -0,0 +1,128 @@
+#include "socket.h"
+#include "psxcommon.h"
+#include "misc.h"
+#include "system.h"
+#include "dynstr.h"
+#include <stdio.h>
+#include <string.h>
+#include <stddef.h>
+
+static int server_socket, client_socket;
+static int debugger_active, resetting, reset, paused, ack_expected;
+
+enum { PACKET_SIZE = 256 };
+
+void GdbStartServer(void)
+{
+ enum { PORT = 3333 };
+
+ if (server_socket > 0) {
+ GdbStopServer();
+ }
+
+ server_socket = StartServer(PORT);
+
+ if (server_socket > 0) {
+ printf("GDB server started on port %hu\n", PORT);
+ debugger_active = 1;
+ }
+ else
+ {
+ fprintf(stderr, "Could not start GDB server\n");
+ }
+}
+
+int GdbServerRunning(void)
+{
+ return server_socket > 0;
+}
+
+void GdbStopServer(void)
+{
+}
+
+static void ack(struct dynstr *const reply)
+{
+ dynstr_append(reply, "OK");
+}
+
+static void nack(struct dynstr *const reply, const int err)
+{
+ dynstr_append(reply, "E %02X", err);
+}
+
+static void HandlePacket(char *const packet, const size_t len)
+{
+ struct dynstr reply;
+ const char *c = packet;
+
+ dynstr_init(&reply);
+
+ if (strstr(packet, "qSupported")) {
+ dynstr_append(&reply, "PacketSize=%x;swbreak+;hwbreak+", PACKET_SIZE - 1);
+ }
+ else {
+ fprintf(stderr, "Unexpected packet \"%s\"\n", packet);
+ return;
+ }
+
+ printf("gdb <- \"%s\"\n", reply.str);
+ WriteSocket(client_socket, reply.str, reply.len);
+ dynstr_free(&reply);
+}
+
+static void ProcessCommands(void)
+{
+ if (HasClient(client_socket)) {
+ char packet[PACKET_SIZE];
+ size_t len = sizeof packet;
+ const enum read_socket_err err = ReadSocket(client_socket, packet, &len);
+
+ switch (err)
+ {
+ case READ_SOCKET_OK:
+ if (len && len < sizeof packet) {
+ /* gdb apparently does not send null-terminated strings. */
+ packet[len] = '\0';
+ printf("gdb -> \"%s\"\n", packet);
+ HandlePacket(packet, len);
+ }
+ break;
+
+ case READ_SOCKET_ERR_INVALID_ARG:
+ /* Fall through. */
+ case READ_SOCKET_ERR_RECV:
+ /* Fall through. */
+ case READ_SOCKET_SHUTDOWN:
+ /* Fall through. */
+ default:
+ return;
+ }
+ }
+}
+
+void GdbServerProcessDebug(void)
+{
+ ProcessCommands();
+}
+
+void GdbServerVSync(void)
+{
+ if (!debugger_active || resetting)
+ return;
+
+ if (reset) {
+ resetting = 1;
+ SysReset();
+ if (reset == 2)
+ LoadCdrom();
+ reset = resetting = 0;
+ return;
+ }
+
+ if (client_socket < 1) {
+ client_socket = GetClient(server_socket);
+ }
+
+ ProcessCommands();
+}
diff --git a/libpcsxcore/gdb_server.h b/libpcsxcore/gdb_server.h
new file mode 100644
index 00000000..d0e925a6
--- /dev/null
+++ b/libpcsxcore/gdb_server.h
@@ -0,0 +1,10 @@
+#ifndef GDB_SERVER_H
+#define GDB_SERVER_H
+
+void GdbStartServer(void);
+void GdbStopServer(void);
+void GdbServerProcessDebug(void);
+void GdbServerVSync(void);
+int GdbServerRunning(void);
+
+#endif /* GDB_SERVER_H */
diff --git a/libpcsxcore/psxcommon.h b/libpcsxcore/psxcommon.h
index c6deaa9f..babdff96 100644
--- a/libpcsxcore/psxcommon.h
+++ b/libpcsxcore/psxcommon.h
@@ -70,6 +70,7 @@ typedef uint8_t boolean;
// Local includes
#include "system.h"
#include "debug.h"
+#include "gdb_server.h"
#if defined (__linux__) || defined (__MACOSX__)
#define strnicmp strncasecmp
diff --git a/libpcsxcore/psxcounters.c b/libpcsxcore/psxcounters.c
index a6d7e10e..46f9947e 100644
--- a/libpcsxcore/psxcounters.c
+++ b/libpcsxcore/psxcounters.c
@@ -324,6 +324,7 @@ void psxRcntUpdate()
}
DebugVSync();
+ GdbServerVSync();
}
/******************************************************************************/
diff --git a/libpcsxcore/psxinterpreter.c b/libpcsxcore/psxinterpreter.c
index a4f38e6d..685c9abf 100644
--- a/libpcsxcore/psxinterpreter.c
+++ b/libpcsxcore/psxinterpreter.c
@@ -152,12 +152,12 @@ static void delayReadWrite(int reg, u32 bpc) {
psxBranchTest();
}
-// this defines shall be used with the tmp
+// this defines shall be used with the tmp
// of the next func (instead of _Funct_...)
-#define _tFunct_ ((tmp ) & 0x3F) // The funct part of the instruction register
-#define _tRd_ ((tmp >> 11) & 0x1F) // The rd part of the instruction register
-#define _tRt_ ((tmp >> 16) & 0x1F) // The rt part of the instruction register
-#define _tRs_ ((tmp >> 21) & 0x1F) // The rs part of the instruction register
+#define _tFunct_ ((tmp ) & 0x3F) // The funct part of the instruction register
+#define _tRd_ ((tmp >> 11) & 0x1F) // The rd part of the instruction register
+#define _tRt_ ((tmp >> 16) & 0x1F) // The rt part of the instruction register
+#define _tRs_ ((tmp >> 21) & 0x1F) // The rs part of the instruction register
#define _tSa_ ((tmp >> 6) & 0x1F) // The sa part of the instruction register
int psxTestLoadDelay(int reg, u32 tmp) {
@@ -184,7 +184,7 @@ int psxTestLoadDelay(int reg, u32 tmp) {
// SYSCALL/BREAK just a break;
case 0x20: case 0x21: case 0x22: case 0x23:
- case 0x24: case 0x25: case 0x26: case 0x27:
+ case 0x24: case 0x25: case 0x26: case 0x27:
case 0x2a: case 0x2b: // ADD/ADDU...
case 0x04: case 0x06: case 0x07: // SLLV...
if (_tRd_ == reg && (_tRt_ == reg || _tRs_ == reg)) return 1; else
@@ -271,7 +271,7 @@ int psxTestLoadDelay(int reg, u32 tmp) {
case 0x12: // COP2
switch (_tFunct_) {
- case 0x00:
+ case 0x00:
switch (_tRs_) {
case 0x00: // MFC2
if (_tRt_ == reg) return 3;
@@ -721,9 +721,9 @@ void psxLB() {
if (_Rt_) {
- _i32(_rRt_) = (signed char)psxMemRead8(_oB_);
+ _i32(_rRt_) = (signed char)psxMemRead8(_oB_);
} else {
- psxMemRead8(_oB_);
+ psxMemRead8(_oB_);
}
}
@@ -743,7 +743,7 @@ void psxLBU() {
if (_Rt_) {
_u32(_rRt_) = psxMemRead8(_oB_);
} else {
- psxMemRead8(_oB_);
+ psxMemRead8(_oB_);
}
}
@@ -828,7 +828,7 @@ void psxLWL() {
if (!_Rt_) return;
- _u32(_rRt_) = ( _u32(_rRt_) & LWL_MASK[shift]) |
+ _u32(_rRt_) = ( _u32(_rRt_) & LWL_MASK[shift]) |
( mem << LWL_SHIFT[shift]);
/*
@@ -850,7 +850,7 @@ void psxLWR() {
u32 mem = psxMemRead32(addr & ~3);
-
+
// load delay = 1 latency
if( branch == 0 )
{
@@ -864,7 +864,7 @@ void psxLWR() {
if (!_Rt_) return;
- _u32(_rRt_) = ( _u32(_rRt_) & LWR_MASK[shift]) |
+ _u32(_rRt_) = ( _u32(_rRt_) & LWR_MASK[shift]) |
( mem >> LWR_SHIFT[shift]);
/*
@@ -940,7 +940,7 @@ void psxMFC0()
if (!_Rt_) return;
-
+
_i32(_rRt_) = (int)_rFs_;
}
@@ -958,7 +958,7 @@ void psxCFC0()
if (!_Rt_) return;
-
+
_i32(_rRt_) = (int)_rFs_;
}
@@ -1031,7 +1031,7 @@ void psxCFC2()
* Unknow instruction (would generate an exception) *
* Format: ? *
*********************************************************/
-void psxNULL() {
+void psxNULL() {
#ifdef PSXCPU_LOG
PSXCPU_LOG("psx: Unimplemented op %x\n", psxRegs.code);
#endif
@@ -1071,9 +1071,9 @@ void (*psxBSC[64])() = {
psxCOP0 , psxNULL , psxCOP2, psxNULL , psxNULL, psxNULL, psxNULL, psxNULL,
psxNULL , psxNULL , psxNULL, psxNULL , psxNULL, psxNULL, psxNULL, psxNULL,
psxLB , psxLH , psxLWL , psxLW , psxLBU , psxLHU , psxLWR , psxNULL,
- psxSB , psxSH , psxSWL , psxSW , psxNULL, psxNULL, psxSWR , psxNULL,
+ psxSB , psxSH , psxSWL , psxSW , psxNULL, psxNULL, psxSWR , psxNULL,
psxNULL , psxNULL , gteLWC2, psxNULL , psxNULL, psxNULL, psxNULL, psxNULL,
- psxNULL , psxNULL , gteSWC2, psxHLE , psxNULL, psxNULL, psxNULL, psxNULL
+ psxNULL , psxNULL , gteSWC2, psxHLE , psxNULL, psxNULL, psxNULL, psxNULL
};
@@ -1108,7 +1108,7 @@ void (*psxCP2[64])() = {
gteDPCS , gteINTPL, gteMVMVA, gteNCDS, gteCDP , psxNULL , gteNCDT , psxNULL, // 10
psxNULL , psxNULL , psxNULL , gteNCCS, gteCC , psxNULL , gteNCS , psxNULL, // 18
gteNCT , psxNULL , psxNULL , psxNULL, psxNULL, psxNULL , psxNULL , psxNULL, // 20
- gteSQR , gteDCPL , gteDPCT , psxNULL, psxNULL, gteAVSZ3, gteAVSZ4, psxNULL, // 28
+ gteSQR , gteDCPL , gteDPCT , psxNULL, psxNULL, gteAVSZ3, gteAVSZ4, psxNULL, // 28
gteRTPT , psxNULL , psxNULL , psxNULL, psxNULL, psxNULL , psxNULL , psxNULL, // 30
psxNULL , psxNULL , psxNULL , psxNULL, psxNULL, gteGPF , gteGPL , gteNCCT // 38
};
@@ -1182,7 +1182,7 @@ static void intReset() {
}
static void intExecute() {
- for (;;)
+ for (;;)
execI();
}
@@ -1198,13 +1198,14 @@ static void intShutdown() {
}
// interpreter execution
-static inline void execI() {
+static inline void execI() {
u32 *code = Read_ICache(psxRegs.pc, FALSE);
psxRegs.code = ((code == NULL) ? 0 : SWAP32(*code));
debugI();
- if (Config.Debug) ProcessDebug();
+ if (Config.GdbServer) GdbServerProcessDebug();
+ else if (Config.Debug) ProcessDebug();
psxRegs.pc += 4;
psxRegs.cycle += BIAS;
diff --git a/libpcsxcore/socket.c b/libpcsxcore/socket.c
index 2755aaa1..1faffd3d 100644
--- a/libpcsxcore/socket.c
+++ b/libpcsxcore/socket.c
@@ -256,7 +256,9 @@ void WriteSocket(int client_socket, const char *buffer, size_t len) {
if (!client_socket)
return;
- send(client_socket, buffer, len, 0);
+ if (send(client_socket, buffer, len, 0) == -1) {
+ perror("send():");
+ }
}
void SetsBlock(int s_socket) {