This is an automated email from Gerrit.

Antonio Borneo ([email protected]) just uploaded a new patch set to 
Gerrit, which you can find at http://openocd.zylin.com/4551

-- gerrit

commit c1415812936139994d1a98114e20cab57bb23ccf
Author: Antonio Borneo <[email protected]>
Date:   Fri Jun 1 18:31:49 2018 +0200

    server: explicitly call "shutdown" when catch CTRL-C or a signal
    
    Every TCL command can be renamed (or deleted) and then replaced by
    a TCL proc that has the same name of the original TCL command.
    This can be used either to completely replace an existing command
    or to wrap the original command to extend its functionality.
    This applies also to the OpenOCD command "shutdown".
    E.g. (TCL code):
        rename shutdown original_shutdown
        proc shutdown {} {
                puts "This is my implementation of shutdown"
                # my own stuff before exit OpenOCD
                original_shutdown
        }
    This allows, for example, to set back some default value to the
    target before quitting OpenOCD.
    
    Unfortunately, sending a signal (or pressing CTRL-C) to terminate
    OpenOCD doesn't trigger calling the original "shutdown" command
    nor its (eventual) replacement.
    
    Detect if the main loop is terminated by an external signal and
    in such case execute explicitly the command "shutdown".
    
    Please notice that it's possible to write a custom "shutdown" TCL
    proc that does not call the original "shutdown" command. This is
    useful, for example, to prevent the user to quit OpenOCD by typing
    "shutdown" in the telnet session.
    Such case will not prevent OpenOCD to terminate when receiving a
    signal; OpenOCD will quit after executing the custom "shutdown"
    command.
    
    Change-Id: I86b8f9eab8dbd7a28dad58b8cafd97caa7a82f43
    Signed-off-by: Antonio Borneo <[email protected]>

diff --git a/doc/openocd.texi b/doc/openocd.texi
index 78d42eb..2160baa 100644
--- a/doc/openocd.texi
+++ b/doc/openocd.texi
@@ -7280,6 +7280,19 @@ Useful in connection with script files
 Close the OpenOCD server, disconnecting all clients (GDB, telnet,
 other). If option @option{error} is used, OpenOCD will return a
 non-zero exit code to the parent process.
+
+Like any TCL commands, also @command{shutdown} can be redefined, e.g.:
+@example
+# redefine shutdown
+rename shutdown original_shutdown
+proc shutdown @{@} @{
+    puts "This is my implementation of shutdown"
+    # my own stuff before exit OpenOCD
+    original_shutdown
+@}
+@end example
+If user types CTRL-C or kills OpenOCD, either the command @command{shutdown}
+or its replacement will be automatically executed before OpenOCD exits.
 @end deffn
 
 @anchor{debuglevel}
diff --git a/src/server/server.c b/src/server/server.c
index 4e80656..4ac7895 100644
--- a/src/server/server.c
+++ b/src/server/server.c
@@ -46,8 +46,13 @@
 
 static struct service *services;
 
-/* shutdown_openocd == 1: exit the main event loop, and quit the
- * debugger; 2: quit with non-zero return code */
+/*
+ * shutdown_openocd values:
+ * 0: stay in main event loop
+ * 1: set by shutdown command; exit the event loop and quit the debugger
+ * 2: set by shutdown command; quit with non-zero return code
+ * 3: set by sig_handler; exec shutdown then exit with signal as return code
+ */
 static int shutdown_openocd;
 
 /* store received signal to exit application by killing ourselves */
@@ -555,18 +560,22 @@ int server_loop(struct command_context *command_context)
                MSG msg;
                while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
                        if (msg.message == WM_QUIT)
-                               shutdown_openocd = 1;
+                               shutdown_openocd = 3;
                }
 #endif
        }
 
+       /* when quit for signal or CTRL-C, run (eventually user implemented) 
"shutdown" */
+       if (shutdown_openocd == 3)
+               command_run_line(command_context, "shutdown");
+
        return shutdown_openocd != 2 ? ERROR_OK : ERROR_FAIL;
 }
 
 #ifdef _WIN32
 BOOL WINAPI ControlHandler(DWORD dwCtrlType)
 {
-       shutdown_openocd = 1;
+       shutdown_openocd = 3;
        return TRUE;
 }
 #endif
@@ -576,7 +585,7 @@ void sig_handler(int sig)
        /* store only first signal that hits us */
        if (!last_signal)
                last_signal = sig;
-       shutdown_openocd = 1;
+       shutdown_openocd = 3;
 }
 
 int server_preinit(void)

-- 

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
OpenOCD-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openocd-devel

Reply via email to