Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Use SetConsoleCtrlHandler for Windows #1021

Merged
merged 1 commit into from
Aug 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ build-mingw
.project
obj-*
*.user*
.cmake/
21 changes: 18 additions & 3 deletions src/st-util/gdb-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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); }

Expand Down