make st-flash clean up on sigint/term/segv, make gdb-server clean up on sigsegv.
authorGeorge Talusan <george.talusan@gmail.com>
Tue, 23 Feb 2016 01:07:43 +0000 (20:07 -0500)
committerGeorge Talusan <george.talusan@gmail.com>
Tue, 23 Feb 2016 01:07:43 +0000 (20:07 -0500)
flash/main.c
gdbserver/gdb-server.c

index d0d303a0dc8c0e545c1b11571f0b5a15a7420c75..284068cb57d57b2fe3ec390906779a6497778d04 100644 (file)
@@ -3,6 +3,7 @@
 // TODO - this should be done as just a simple flag to the st-util command line...
 
 
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #define DEBUG_LOG_LEVEL 100
 #define STND_LOG_LEVEL  50
 
+stlink_t *connected_stlink = NULL;
+
+static void cleanup(int signal __attribute__((unused))) {
+    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);
+    }
+
+    exit(1);
+}
+
 enum st_cmds {DO_WRITE = 0, DO_READ = 1, DO_ERASE = 2};
 struct opts
 {
@@ -174,6 +188,11 @@ int main(int ac, char** av)
         sl->verbose = o.log_level;
     }
 
+    connected_stlink = sl;
+    signal(SIGINT, &cleanup);
+    signal(SIGTERM, &cleanup);
+    signal(SIGSEGV, &cleanup);
+
     if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
         stlink_exit_dfu_mode(sl);
 
index b272f4caa751bd418aad96448440134bf27ef79f..0c31cff18aca66e521e5d1bd1dda79c736d3f913 100644 (file)
@@ -9,7 +9,6 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include <signal.h>
 #include <unistd.h>
 #include <sys/types.h>
 #ifdef __MINGW32__
@@ -186,6 +185,7 @@ int main(int argc, char** argv) {
     connected_stlink = sl;
     signal(SIGINT, &cleanup);
     signal(SIGTERM, &cleanup);
+    signal(SIGSEGV, &cleanup);
 
     if (state.reset) {
         stlink_reset(sl);