From 5ea485de396ce004604a0adc4bf1278c6a3a845f Mon Sep 17 00:00:00 2001 From: Oleksiy Slyshyk Date: Fri, 28 Aug 2020 23:35:45 +0300 Subject: [PATCH] use SetConsoleCtrlHandler for windows --- .gitignore | 1 + src/st-util/gdb-server.c | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 229242a53..9fbe0139c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ build-mingw .project obj-* *.user* +.cmake/ diff --git a/src/st-util/gdb-server.c b/src/st-util/gdb-server.c index 94cd6fda7..60c9c16d3 100644 --- a/src/st-util/gdb-server.c +++ b/src/st-util/gdb-server.c @@ -71,19 +71,30 @@ int serve(stlink_t *sl, st_state_t *st); char* make_memory_map(stlink_t *sl); static void init_cache(stlink_t *sl); -static void cleanup(int signum) { - (void)signum; - +static void _cleanup() { if (connected_stlink) { // Switch back to mass storage mode before closing stlink_run(connected_stlink); stlink_exit_debug_mode(connected_stlink); stlink_close(connected_stlink); } +} +static void cleanup(int signum) { + printf("Receive signal %i. Exiting...\n", signum); + _cleanup(); exit(1); + (void)signum; } +#if defined(_WIN32) +BOOL WINAPI CtrlHandler(DWORD fdwCtrlType) { + printf("Receive signal %i. Exiting...\r\n", (int)fdwCtrlType); + _cleanup(); + return FALSE; +} +#endif + static stlink_t* do_connect(st_state_t *st) { stlink_t *sl = NULL; @@ -234,9 +245,13 @@ int main(int argc, char** argv) { } connected_stlink = sl; +#if defined(_WIN32) + SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE); +#else signal(SIGINT, &cleanup); signal(SIGTERM, &cleanup); signal(SIGSEGV, &cleanup); +#endif if (state.reset) { stlink_reset(sl); }