From 64e8d3fb832a69689ac3753af7b9f6c0a05e648a Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Wed, 23 Apr 2014 21:42:08 -0700 Subject: [PATCH] Fix GBDStub build on Windows --- CMakeLists.txt | 1 + src/debugger/gdb-stub.c | 28 ++++++++++++++++------------ src/util/socket.h | 12 ++++++++---- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d6fa4419..53211cd7f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,7 @@ include_directories(${CMAKE_SOURCE_DIR}/src) if(WIN32) add_definitions(-D_WIN32_WINNT=0x0600) + set(OS_LIB "${OS_LIB};Ws2_32") file(GLOB OS_SRC ${CMAKE_SOURCE_DIR}/src/platform/windows/*.c) source_group("Windows-specific code" FILES ${OS_SRC}) else() diff --git a/src/debugger/gdb-stub.c b/src/debugger/gdb-stub.c index 5688086ac..76d9619c3 100644 --- a/src/debugger/gdb-stub.c +++ b/src/debugger/gdb-stub.c @@ -3,6 +3,10 @@ #include #include +#ifndef SIGTRAP +#define SIGTRAP 5 /* Win32 Signals do not include SIGTRAP */ +#endif + enum GDBError { GDB_NO_ERROR = 0x00, GDB_BAD_ARGUMENTS = 0x06, @@ -18,7 +22,7 @@ static void _sendMessage(struct GDBStub* stub); static void _gdbStubDeinit(struct ARMDebugger* debugger) { struct GDBStub* stub = (struct GDBStub*) debugger; - if (stub->socket >= 0) { + if (!SOCKET_FAILED(stub->socket)) { GDBStubShutdown(stub); } } @@ -45,7 +49,7 @@ static void _gdbStubEntered(struct ARMDebugger* debugger, enum DebuggerEntryReas static void _gdbStubPoll(struct ARMDebugger* debugger) { struct GDBStub* stub = (struct GDBStub*) debugger; while (stub->d.state == DEBUGGER_PAUSED) { - if (stub->connection >= 0) { + if (!SOCKET_FAILED(stub->connection)) { if (!SocketSetBlocking(stub->connection, 1)) { GDBStubHangup(stub); return; @@ -161,7 +165,7 @@ static void _writeHostInfo(struct GDBStub* stub) { static void _continue(struct GDBStub* stub, const char* message) { stub->d.state = DEBUGGER_RUNNING; - if (stub->connection >= 0) { + if (!SOCKET_FAILED(stub->connection)) { if (!SocketSetBlocking(stub->connection, 0)) { GDBStubHangup(stub); return; @@ -421,8 +425,8 @@ size_t _parseGDBMessage(struct GDBStub* stub, const char* message) { void GDBStubCreate(struct GDBStub* stub) { ARMDebuggerCreate(&stub->d); - stub->socket = -1; - stub->connection = -1; + stub->socket = INVALID_SOCKET; + stub->connection = INVALID_SOCKET; stub->d.init = 0; stub->d.deinit = _gdbStubDeinit; stub->d.paused = _gdbStubPoll; @@ -431,12 +435,12 @@ void GDBStubCreate(struct GDBStub* stub) { } int GDBStubListen(struct GDBStub* stub, int port, uint32_t bindAddress) { - if (stub->socket >= 0) { + if (!SOCKET_FAILED(stub->socket)) { GDBStubShutdown(stub); } // TODO: support IPv6 stub->socket = SocketOpenTCP(port, bindAddress); - if (stub->socket < 0) { + if (SOCKET_FAILED(stub->socket)) { if (stub->d.log) { stub->d.log(&stub->d, DEBUGGER_LOG_ERROR, "Couldn't open socket"); } @@ -462,7 +466,7 @@ cleanup: } void GDBStubHangup(struct GDBStub* stub) { - if (stub->connection >= 0) { + if (!SOCKET_FAILED(stub->connection)) { SocketClose(stub->connection); stub->connection = -1; } @@ -473,19 +477,19 @@ void GDBStubHangup(struct GDBStub* stub) { void GDBStubShutdown(struct GDBStub* stub) { GDBStubHangup(stub); - if (stub->socket >= 0) { + if (!SOCKET_FAILED(stub->socket)) { SocketClose(stub->socket); stub->socket = -1; } } void GDBStubUpdate(struct GDBStub* stub) { - if (stub->socket == -1) { + if (stub->socket == INVALID_SOCKET) { return; } - if (stub->connection == -1) { + if (stub->connection == INVALID_SOCKET) { stub->connection = SocketAccept(stub->socket, 0, 0); - if (stub->connection >= 0) { + if (!SOCKET_FAILED(stub->connection)) { if (!SocketSetBlocking(stub->connection, 0)) { goto connectionLost; } diff --git a/src/util/socket.h b/src/util/socket.h index 903325e2b..8e04477b0 100644 --- a/src/util/socket.h +++ b/src/util/socket.h @@ -5,7 +5,9 @@ #ifdef _WIN32 #include +#include +#define SOCKET_FAILED(s) (s) == INVALID_SOCKET typedef SOCKET Socket; #else #include @@ -13,6 +15,8 @@ typedef SOCKET Socket; #include #include +#define INVALID_SOCKET (-1) +#define SOCKET_FAILED(s) (s) < 0 typedef int Socket; #endif @@ -33,7 +37,7 @@ static inline ssize_t SocketRecv(Socket socket, void* buffer, size_t size) { static inline Socket SocketOpenTCP(int port, uint32_t bindAddress) { Socket sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); - if (sock < 0) { + if (SOCKET_FAILED(sock)) { return sock; } @@ -54,7 +58,7 @@ static inline Socket SocketOpenTCP(int port, uint32_t bindAddress) { static inline Socket SocketConnectTCP(int port, uint32_t destinationAddress) { Socket sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); - if (sock < 0) { + if (SOCKET_FAILED(sock)) { return sock; } @@ -87,8 +91,8 @@ static inline int SocketClose(Socket socket) { static inline int SocketSetBlocking(Socket socket, int blocking) { #ifdef _WIN32 - blocking = !blocking; - return ioctlsocket(socket, FIONBIO, &blocking) == NO_ERROR; + u_long unblocking = !blocking; + return ioctlsocket(socket, FIONBIO, &unblocking) == NO_ERROR; #else int flags = fcntl(socket, F_GETFL); if (flags == -1) {