From 24f5fa9973c13deab46317e240e1f4dfd1bb7f42 Mon Sep 17 00:00:00 2001 From: Oxore Date: Sun, 30 Oct 2022 13:07:09 +0300 Subject: Add sigint handler --- emulator.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'emulator.cpp') diff --git a/emulator.cpp b/emulator.cpp index c6557a2..b7e884e 100644 --- a/emulator.cpp +++ b/emulator.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #if !defined(DEBUG_TRACE_INSTRUCTIONS) # define DEBUG_TRACE_INSTRUCTIONS 0 @@ -43,6 +44,8 @@ std::vector code_bkpts{}, read_bkpts{}, write_bkpts{}, access_bkpts{ uint64_t g_cycles_counter = 0; +bool quit{}; + template 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; -- cgit v1.2.3