Fix SIGTERM signal safety (PR gdb/15358).
authorJan Kratochvil <jan.kratochvil@redhat.com>
Tue, 18 Mar 2014 21:48:06 +0000 (22:48 +0100)
committerJan Kratochvil <jan.kratochvil@redhat.com>
Tue, 18 Mar 2014 21:48:06 +0000 (22:48 +0100)
gdb/
2014-03-18  Jan Kratochvil  <jan.kratochvil@redhat.com>

PR gdb/15358
* defs.h (sync_quit_force_run): New declaration.
(QUIT): Check also SYNC_QUIT_FORCE_RUN.
* event-top.c (async_sigterm_handler): New declaration.
(async_sigterm_token): New variable.
(async_init_signals): Create also async_sigterm_token.
(async_sigterm_handler): New function.
(sync_quit_force_run): New variable.
(handle_sigterm): Replace quit_force call by other calls.
* utils.c (quit): Call quit_force if SYNC_QUIT_FORCE_RUN.

gdb/testsuite/
2014-03-18  Jan Kratochvil  <jan.kratochvil@redhat.com>

PR gdb/15358
* gdb.base/gdb-sigterm.c: New file.
* gdb.base/gdb-sigterm.exp: New file.

Message-ID: <20140316135334.GA30698@host2.jankratochvil.net>

gdb/ChangeLog
gdb/defs.h
gdb/event-top.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.base/gdb-sigterm.c [new file with mode: 0644]
gdb/testsuite/gdb.base/gdb-sigterm.exp [new file with mode: 0644]
gdb/utils.c

index 4f486e9e74ad06cca6b0ad949db2306fea1a5df3..4370e13e9023b0b823cf426a3da8941cef29b4df 100644 (file)
@@ -1,3 +1,16 @@
+2014-03-18  Jan Kratochvil  <jan.kratochvil@redhat.com>
+
+       PR gdb/15358
+       * defs.h (sync_quit_force_run): New declaration.
+       (QUIT): Check also SYNC_QUIT_FORCE_RUN.
+       * event-top.c (async_sigterm_handler): New declaration.
+       (async_sigterm_token): New variable.
+       (async_init_signals): Create also async_sigterm_token.
+       (async_sigterm_handler): New function.
+       (sync_quit_force_run): New variable.
+       (handle_sigterm): Replace quit_force call by other calls.
+       * utils.c (quit): Call quit_force if SYNC_QUIT_FORCE_RUN.
+
 2014-03-18  Maciej W. Rozycki  <macro@codesourcery.com>
 
        * rs6000-tdep.c (rs6000_frame_cache): Correct little-endian GPR
index 480133d8c173f7c29f5009df3394105d08d9ab44..47da43a665292f1e5e0e504cc4a4cf45cb69737c 100644 (file)
@@ -171,6 +171,9 @@ extern int check_quit_flag (void);
 /* * Set the quit flag.  */
 extern void set_quit_flag (void);
 
+/* Flag that function quit should call quit_force.  */
+extern volatile int sync_quit_force_run;
+
 extern int immediate_quit;
 
 extern void quit (void);
@@ -183,7 +186,7 @@ extern void quit (void);
    needed.  */
 
 #define QUIT { \
-  if (check_quit_flag ()) quit (); \
+  if (check_quit_flag () || sync_quit_force_run) quit (); \
   if (deprecated_interactive_hook) deprecated_interactive_hook (); \
 }
 
index fbe89bdb829367a32cf31a35b77eb8e3b7906504..c353f487d2bba1dd32dbdcaf6b24cbddd1881209 100644 (file)
@@ -72,6 +72,7 @@ static void async_float_handler (gdb_client_data);
 #ifdef STOP_SIGNAL
 static void async_stop_sig (gdb_client_data);
 #endif
+static void async_sigterm_handler (gdb_client_data arg);
 
 /* Readline offers an alternate interface, via callback
    functions.  These are all included in the file callback.c in the
@@ -135,6 +136,7 @@ static struct async_signal_handler *sigfpe_token;
 #ifdef STOP_SIGNAL
 static struct async_signal_handler *sigtstp_token;
 #endif
+static struct async_signal_handler *async_sigterm_token;
 
 /* Structure to save a partially entered command.  This is used when
    the user types '\' at the end of a command line.  This is necessary
@@ -733,6 +735,8 @@ async_init_signals (void)
   sigint_token =
     create_async_signal_handler (async_request_quit, NULL);
   signal (SIGTERM, handle_sigterm);
+  async_sigterm_token
+    = create_async_signal_handler (async_sigterm_handler, NULL);
 
   /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
      to the inferior and breakpoints will be ignored.  */
@@ -769,7 +773,6 @@ async_init_signals (void)
   sigtstp_token =
     create_async_signal_handler (async_stop_sig, NULL);
 #endif
-
 }
 
 /* Tell the event loop what to do if SIGINT is received.
@@ -797,13 +800,33 @@ handle_sigint (int sig)
   gdb_call_async_signal_handler (sigint_token, immediate_quit);
 }
 
+/* Handle GDB exit upon receiving SIGTERM if target_can_async_p ().  */
+
+static void
+async_sigterm_handler (gdb_client_data arg)
+{
+  quit_force (NULL, stdin == instream);
+}
+
+/* See defs.h.  */
+volatile int sync_quit_force_run;
+
 /* Quit GDB if SIGTERM is received.
    GDB would quit anyway, but this way it will clean up properly.  */
 void
 handle_sigterm (int sig)
 {
   signal (sig, handle_sigterm);
-  quit_force ((char *) 0, stdin == instream);
+
+  /* Call quit_force in a signal safe way.
+     quit_force itself is not signal safe.  */
+  if (target_can_async_p ())
+    mark_async_signal_handler (async_sigterm_token);
+  else
+    {
+      sync_quit_force_run = 1;
+      set_quit_flag ();
+    }
 }
 
 /* Do the quit.  All the checks have been done by the caller.  */
index 458e7954923d1bd27bcbd3317ff4b7c5eee00715..91f1cf39e2f61fe424700e05d936b22e483ae03f 100644 (file)
@@ -1,3 +1,9 @@
+2014-03-18  Jan Kratochvil  <jan.kratochvil@redhat.com>
+
+       PR gdb/15358
+       * gdb.base/gdb-sigterm.c: New file.
+       * gdb.base/gdb-sigterm.exp: New file.
+
 2014-03-18  Pedro Alves  <palves@redhat.com>
 
        PR gdb/13860
diff --git a/gdb/testsuite/gdb.base/gdb-sigterm.c b/gdb/testsuite/gdb.base/gdb-sigterm.c
new file mode 100644 (file)
index 0000000..ffa09e4
--- /dev/null
@@ -0,0 +1,26 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2013 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <unistd.h>
+
+int
+main (void)
+{
+  alarm (60);
+
+  for (;;); /* loop-line */
+}
diff --git a/gdb/testsuite/gdb.base/gdb-sigterm.exp b/gdb/testsuite/gdb.base/gdb-sigterm.exp
new file mode 100644 (file)
index 0000000..b57680d
--- /dev/null
@@ -0,0 +1,96 @@
+# This testcase is part of GDB, the GNU debugger.
+#
+# Copyright 2013 Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+standard_testfile
+
+if { [build_executable ${testfile}.exp ${testfile}] == -1 } {
+    return -1
+}
+
+proc do_test { pass } {
+    global testfile gdb_prompt binfile pf_prefix
+
+    if ![runto_main] {
+       return -1
+    }
+
+    gdb_breakpoint "${testfile}.c:[gdb_get_line_number "loop-line" ${testfile}.c]" \
+                  temporary
+
+    # gdb_continue_to_breakpoint would print a pass message.
+    gdb_test "continue" "Temporary breakpoint .* loop-line .*" ""
+
+    gdb_test_no_output "set range-stepping off" ""
+    gdb_test_no_output "set debug infrun 1" ""
+    gdb_test_no_output "set debug lin-lwp 1" ""
+
+    set test "run a bit #$pass"
+    set abort 1
+    gdb_test_multiple "step" $test {
+       -re "infrun: stepping inside range" {
+           # Suppress pass $test
+           verbose -log "$pf_prefix $test"
+           set abort 0
+       }
+    }
+    if $abort {
+       return
+    }
+
+    set gdb_pid [exp_pid -i [board_info host fileid]]
+    remote_exec host "kill -TERM ${gdb_pid}"
+
+    set test "expect eof #$pass"
+    set abort 1
+    set stepping 0
+    gdb_test_multiple "" $test {
+       eof {
+           verbose -log "$pf_prefix $test"
+           set abort 0
+       }
+       -re "infrun: stepping inside range" {
+           incr stepping
+           if { $stepping > 200 } {
+               fail "$test (stepping inside range $stepping times)"
+           } else {
+               exp_continue
+           }
+       }
+    }
+    if $abort {
+       return
+    }
+}
+
+# Testcase was FAILing approx. on 10th pass with unpatched GDB.
+# 50 runs should be approx. a safe number to be sure it is fixed now.
+
+for {set pass 0} {$pass < 50} {incr pass} {
+
+    clean_restart ${testfile}
+    gdb_test_no_output "set target-async off" ""
+    with_test_prefix "sync" {
+        do_test $pass
+    }
+
+    clean_restart ${testfile}
+    gdb_test_no_output "set target-async on" ""
+    with_test_prefix "async" {
+        do_test $pass
+    }
+}
+pass "$pass SIGTERM passes"
index 9d068c563c8b88cf04df921edfe7878c2a4d929a..364470ca6fe6e541b332213c89d3cb3a27c5960f 100644 (file)
@@ -999,6 +999,12 @@ print_sys_errmsg (const char *string, int errcode)
 void
 quit (void)
 {
+  if (sync_quit_force_run)
+    {
+      sync_quit_force_run = 0;
+      quit_force (NULL, stdin == instream);
+    }
+
 #ifdef __MSDOS__
   /* No steenking SIGINT will ever be coming our way when the
      program is resumed.  Don't lie.  */
This page took 0.06037 seconds and 4 git commands to generate.