summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOxore <oxore@protonmail.com>2022-10-30 13:07:09 +0300
committerOxore <oxore@protonmail.com>2022-10-30 13:07:09 +0300
commit24f5fa9973c13deab46317e240e1f4dfd1bb7f42 (patch)
tree0d4da547fc3febe541db39a3fcd49e9de3bc8894
parent0e06b0fe09e614937ed8d9329e336155b7f35ffb (diff)
Add sigint handler
-rw-r--r--emulator.cpp17
1 files changed, 15 insertions, 2 deletions
diff --git a/emulator.cpp b/emulator.cpp
index c6557a2..b7e884e 100644
--- a/emulator.cpp
+++ b/emulator.cpp
@@ -22,6 +22,7 @@
#include <sys/socket.h>
#include <memory>
#include <unistd.h>
+#include <signal.h>
#if !defined(DEBUG_TRACE_INSTRUCTIONS)
# define DEBUG_TRACE_INSTRUCTIONS 0
@@ -43,6 +44,8 @@ std::vector<Breakpoint> code_bkpts{}, read_bkpts{}, write_bkpts{}, access_bkpts{
uint64_t g_cycles_counter = 0;
+bool quit{};
+
template<typename T, size_t S>
struct Backtrace {
static_assert(S > 0, "Backtrace size cannot be zero");
@@ -449,7 +452,7 @@ int emulator(M68KDebuggingControl& m68k_debug, Graphics& graphics)
return EXIT_FAILURE;
}
printf("Listening TCP 0.0.0.0:%u\n", port);
- while (1) {
+ while (!quit) {
struct sockaddr client_address{};
socklen_t address_len{};
const int conn_fd = accept(socket_fd, &client_address, &address_len);
@@ -472,7 +475,7 @@ int emulator(M68KDebuggingControl& m68k_debug, Graphics& graphics)
return -1;
}
GDBRemote::ExchangeContext exchange_ctx{};
- while (1) {
+ while (!quit) {
const ssize_t read_size = recv(conn_fd, msg_buf, MESSAGE_BUFFER_SIZE, 0);
if (read_size > 0) {
ParseAndReact(conn_fd, exchange_ctx, m68k_debug, msg_buf, read_size);
@@ -512,6 +515,12 @@ int emulator(M68KDebuggingControl& m68k_debug, Graphics& graphics)
return 0;
}
+void sigint_handler(int sig)
+{
+ printf("Signal %d received, exiting\n",sig);
+ quit = true;
+}
+
int main(int argc, char* argv[])
{
if (argc != 2)
@@ -530,6 +539,10 @@ int main(int argc, char* argv[])
exit_error("Error reading %s", argv[1]);
printf("Read into ROM %zu bytes\n", fread_ret);
+ struct sigaction sa;
+ sa.sa_handler = sigint_handler;
+ sigaction(SIGINT, &sa, NULL);
+
Graphics graphics{};
if (!graphics.IsOk()) {
return EXIT_FAILURE;