Blame SOURCES/gdb-rhbz1473411-spawn-default-signal-handlers.patch

dc7ef9
commit 35fcb4fc81e51295d14125785765e0ea3e132cd9
dc7ef9
Author: Pedro Alves <palves@redhat.com>
dc7ef9
Date:   Tue Aug 9 20:21:08 2016 +0100
dc7ef9
dc7ef9
    Fix PR gdb/18653: gdb disturbs inferior's inherited signal dispositions
dc7ef9
    
dc7ef9
    gdb's (or gdbserver's) own signal handling should not interfere with
dc7ef9
    the signal dispositions their spawned children inherit.  However, it
dc7ef9
    currently does.  For example, some paths in gdb cause SIGPIPE to be
dc7ef9
    set to SIG_IGN, and as consequence, the child starts with SIGPIPE to
dc7ef9
    set to SIG_IGN too, even though gdb was started with SIGPIPE set to
dc7ef9
    SIG_DFL.
dc7ef9
    
dc7ef9
    This is because the exec family of functions does not reset the signal
dc7ef9
    disposition of signals that are set to SIG_IGN:
dc7ef9
    
dc7ef9
      http://pubs.opengroup.org/onlinepubs/7908799/xsh/execve.html
dc7ef9
    
dc7ef9
      Signals set to the default action (SIG_DFL) in the calling process
dc7ef9
      image are set to the default action in the new process
dc7ef9
      image. Signals set to be ignored (SIG_IGN) by the calling process
dc7ef9
      image are set to be ignored by the new process image. Signals set to
dc7ef9
      be caught by the calling process image are set to the default action
dc7ef9
      in the new process image (see <signal.h>).
dc7ef9
    
dc7ef9
    And neither does it reset signal masks or flags.
dc7ef9
    
dc7ef9
    In order to be transparent, when spawning new child processes to debug
dc7ef9
    (with "run", etc.), reset signal actions and mask back to what was
dc7ef9
    originally inherited from gdb/gdbserver's parent, just before execing
dc7ef9
    the target program to debug.
dc7ef9
    
dc7ef9
    gdb/ChangeLog:
dc7ef9
    2016-08-09  Pedro Alves  <palves@redhat.com>
dc7ef9
    
dc7ef9
            PR gdb/18653
dc7ef9
            * Makefile.in (SFILES): Add
dc7ef9
            common/signals-state-save-restore.c.
dc7ef9
            (HFILES_NO_SRCDIR): Add common/signals-state-save-restore.h.
dc7ef9
            (COMMON_OBS): Add signals-state-save-restore.o.
dc7ef9
            (signals-state-save-restore.o): New rule.
dc7ef9
            * configure: Regenerate.
dc7ef9
            * fork-child.c: Include "signals-state-save-restore.h".
dc7ef9
            (fork_inferior): Call restore_original_signals_state.
dc7ef9
            * main.c: Include "signals-state-save-restore.h".
dc7ef9
            (captured_main): Call save_original_signals_state.
dc7ef9
            * common/common.m4: Add sigaction to AC_CHECK_FUNCS checks.
dc7ef9
            * common/signals-state-save-restore.c: New file.
dc7ef9
            * common/signals-state-save-restore.h: New file.
dc7ef9
    
dc7ef9
    gdb/gdbserver/ChangeLog:
dc7ef9
    2016-08-09  Pedro Alves  <palves@redhat.com>
dc7ef9
    
dc7ef9
            PR gdb/18653
dc7ef9
            * Makefile.in (OBS): Add signals-state-save-restore.o.
dc7ef9
            (signals-state-save-restore.o): New rule.
dc7ef9
            * config.in: Regenerate.
dc7ef9
            * configure: Regenerate.
dc7ef9
            * linux-low.c: Include "signals-state-save-restore.h".
dc7ef9
            (linux_create_inferior): Call
dc7ef9
            restore_original_signals_state.
dc7ef9
            * server.c: Include "dispositions-save-restore.h".
dc7ef9
            (captured_main): Call save_original_signals_state.
dc7ef9
    
dc7ef9
    gdb/testsuite/ChangeLog:
dc7ef9
    2016-08-09  Pedro Alves  <palves@redhat.com>
dc7ef9
    
dc7ef9
            PR gdb/18653
dc7ef9
            * gdb.base/signals-state-child.c: New file.
dc7ef9
            * gdb.base/signals-state-child.exp: New file.
dc7ef9
            * gdb.gdb/selftest.exp (do_steps_and_nexts): Add new pattern.
dc7ef9
dc7ef9
### a/gdb/ChangeLog
dc7ef9
### b/gdb/ChangeLog
dc7ef9
## -1,5 +1,22 @@
dc7ef9
 2016-08-09  Pedro Alves  <palves@redhat.com>
dc7ef9
 
dc7ef9
+	PR gdb/18653
dc7ef9
+	* Makefile.in (SFILES): Add
dc7ef9
+	common/signals-state-save-restore.c.
dc7ef9
+	(HFILES_NO_SRCDIR): Add common/signals-state-save-restore.h.
dc7ef9
+	(COMMON_OBS): Add signals-state-save-restore.o.
dc7ef9
+	(signals-state-save-restore.o): New rule.
dc7ef9
+	* configure: Regenerate.
dc7ef9
+	* fork-child.c: Include "signals-state-save-restore.h".
dc7ef9
+	(fork_inferior): Call restore_original_signals_state.
dc7ef9
+	* main.c: Include "signals-state-save-restore.h".
dc7ef9
+	(captured_main): Call save_original_signals_state.
dc7ef9
+	* common/common.m4: Add sigaction to AC_CHECK_FUNCS checks.
dc7ef9
+	* common/signals-state-save-restore.c: New file.
dc7ef9
+	* common/signals-state-save-restore.h: New file.
dc7ef9
+
dc7ef9
+2016-08-09  Pedro Alves  <palves@redhat.com>
dc7ef9
+
dc7ef9
 	* value.c (unpack_value_bitfield): Skip unpacking if the parent
dc7ef9
 	has no contents buffer to begin with.
dc7ef9
 
dc7ef9
Index: gdb-7.6.1/gdb/Makefile.in
dc7ef9
===================================================================
dc7ef9
--- gdb-7.6.1.orig/gdb/Makefile.in	2017-11-03 19:22:10.364013263 +0100
dc7ef9
+++ gdb-7.6.1/gdb/Makefile.in	2017-11-03 19:22:11.452021206 +0100
dc7ef9
@@ -763,7 +763,8 @@
dc7ef9
 	regset.c sol-thread.c windows-termcap.c \
dc7ef9
 	common/gdb_vecs.c common/common-utils.c common/xml-utils.c \
dc7ef9
 	common/ptid.c common/buffer.c gdb-dlfcn.c common/agent.c \
dc7ef9
-	common/format.c btrace.c record-btrace.c
dc7ef9
+	common/format.c btrace.c record-btrace.c \
dc7ef9
+	common/signals-state-save-restore.c \
dc7ef9
 
dc7ef9
 LINTFILES = $(SFILES) $(YYFILES) $(CONFIG_SRCS) init.c
dc7ef9
 
dc7ef9
@@ -841,7 +842,8 @@
dc7ef9
 common/format.h common/host-defs.h utils.h common/queue.h common/gdb_string.h \
dc7ef9
 common/linux-osdata.h gdb-dlfcn.h auto-load.h probe.h stap-probe.h \
dc7ef9
 gdb_bfd.h sparc-ravenscar-thread.h ppc-ravenscar-thread.h common/linux-btrace.h \
dc7ef9
-nat/linux-namespaces.h
dc7ef9
+nat/linux-namespaces.h \
dc7ef9
+common/signals-state-save-restore.h
dc7ef9
 
dc7ef9
 # Header files that already have srcdir in them, or which are in objdir.
dc7ef9
 
dc7ef9
@@ -898,6 +900,7 @@
dc7ef9
 	memattr.o mem-break.o target.o parse.o language.o buildsym.o \
dc7ef9
 	findcmd.o \
dc7ef9
 	std-regs.o \
dc7ef9
+	signals-state-save-restore.o \
dc7ef9
 	signals.o \
dc7ef9
 	exec.o reverse.o \
dc7ef9
 	bcache.o objfiles.o observer.o minsyms.o maint.o demangle.o \
dc7ef9
@@ -2116,6 +2119,10 @@
dc7ef9
 	$(COMPILE) $(srcdir)/tui/tui-winsource.c
dc7ef9
 	$(POSTCOMPILE)
dc7ef9
 
dc7ef9
+signals-state-save-restore.o: $(srcdir)/common/signals-state-save-restore.c
dc7ef9
+	$(COMPILE) $(srcdir)/common/signals-state-save-restore.c
dc7ef9
+	$(POSTCOMPILE)
dc7ef9
+
dc7ef9
 #
dc7ef9
 # gdb/python/ dependencies
dc7ef9
 #
dc7ef9
Index: gdb-7.6.1/gdb/common/signals-state-save-restore.c
dc7ef9
===================================================================
dc7ef9
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
dc7ef9
+++ gdb-7.6.1/gdb/common/signals-state-save-restore.c	2017-11-03 19:33:46.346074718 +0100
dc7ef9
@@ -0,0 +1,99 @@
dc7ef9
+/* Copyright (C) 2016 Free Software Foundation, Inc.
dc7ef9
+
dc7ef9
+   This file is part of GDB.
dc7ef9
+
dc7ef9
+   This program is free software; you can redistribute it and/or modify
dc7ef9
+   it under the terms of the GNU General Public License as published by
dc7ef9
+   the Free Software Foundation; either version 3 of the License, or
dc7ef9
+   (at your option) any later version.
dc7ef9
+
dc7ef9
+   This program is distributed in the hope that it will be useful,
dc7ef9
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
dc7ef9
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dc7ef9
+   GNU General Public License for more details.
dc7ef9
+
dc7ef9
+   You should have received a copy of the GNU General Public License
dc7ef9
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
dc7ef9
+
dc7ef9
+#ifdef GDBSERVER
dc7ef9
+#include "server.h"
dc7ef9
+#else
dc7ef9
+#include "defs.h"
dc7ef9
+#endif
dc7ef9
+
dc7ef9
+#include "signals-state-save-restore.h"
dc7ef9
+
dc7ef9
+#include <signal.h>
dc7ef9
+
dc7ef9
+/* The original signal actions and mask.  */
dc7ef9
+
dc7ef9
+#ifdef HAVE_SIGACTION
dc7ef9
+static struct sigaction original_signal_actions[NSIG];
dc7ef9
+
dc7ef9
+/* Note that we use sigprocmask without worrying about threads because
dc7ef9
+   the save/restore functions are called either from main, or after a
dc7ef9
+   fork.  In both cases, we know the calling process is single
dc7ef9
+   threaded.  */
dc7ef9
+static sigset_t original_signal_mask;
dc7ef9
+#endif
dc7ef9
+
dc7ef9
+/* See signals-state-save-restore.h.   */
dc7ef9
+
dc7ef9
+void
dc7ef9
+save_original_signals_state (void)
dc7ef9
+{
dc7ef9
+#ifdef HAVE_SIGACTION
dc7ef9
+  int i;
dc7ef9
+  int res;
dc7ef9
+
dc7ef9
+  res = sigprocmask (0,  NULL, &original_signal_mask);
dc7ef9
+  if (res == -1)
dc7ef9
+    perror_with_name ("sigprocmask");
dc7ef9
+
dc7ef9
+  for (i = 1; i < NSIG; i++)
dc7ef9
+    {
dc7ef9
+      struct sigaction *oldact = &original_signal_actions[i];
dc7ef9
+
dc7ef9
+      res = sigaction (i, NULL, oldact);
dc7ef9
+      if (res == -1 && errno == EINVAL)
dc7ef9
+	{
dc7ef9
+	  /* Some signal numbers in the range are invalid.  */
dc7ef9
+	  continue;
dc7ef9
+	}
dc7ef9
+      else if (res == -1)
dc7ef9
+	perror_with_name ("sigaction");
dc7ef9
+
dc7ef9
+      /* If we find a custom signal handler already installed, then
dc7ef9
+	 this function was called too late.  */
dc7ef9
+      if (oldact->sa_handler != SIG_DFL && oldact->sa_handler != SIG_IGN)
dc7ef9
+	internal_error (__FILE__, __LINE__, _("unexpected signal handler"));
dc7ef9
+    }
dc7ef9
+#endif
dc7ef9
+}
dc7ef9
+
dc7ef9
+/* See signals-state-save-restore.h.   */
dc7ef9
+
dc7ef9
+void
dc7ef9
+restore_original_signals_state (void)
dc7ef9
+{
dc7ef9
+#ifdef HAVE_SIGACTION
dc7ef9
+  int i;
dc7ef9
+  int res;
dc7ef9
+
dc7ef9
+  for (i = 1; i < NSIG; i++)
dc7ef9
+    {
dc7ef9
+      res = sigaction (i, &original_signal_actions[i], NULL);
dc7ef9
+      if (res == -1 && errno == EINVAL)
dc7ef9
+	{
dc7ef9
+	  /* Some signal numbers in the range are invalid.  */
dc7ef9
+	  continue;
dc7ef9
+	}
dc7ef9
+      else if (res == -1)
dc7ef9
+	perror_with_name ("sigaction");
dc7ef9
+    }
dc7ef9
+
dc7ef9
+  res = sigprocmask (SIG_SETMASK,  &original_signal_mask, NULL);
dc7ef9
+  if (res == -1)
dc7ef9
+    perror_with_name ("sigprocmask");
dc7ef9
+#endif
dc7ef9
+}
dc7ef9
Index: gdb-7.6.1/gdb/common/signals-state-save-restore.h
dc7ef9
===================================================================
dc7ef9
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
dc7ef9
+++ gdb-7.6.1/gdb/common/signals-state-save-restore.h	2017-11-03 19:22:11.453021213 +0100
dc7ef9
@@ -0,0 +1,39 @@
dc7ef9
+/* Copyright (C) 2016 Free Software Foundation, Inc.
dc7ef9
+
dc7ef9
+   This file is part of GDB.
dc7ef9
+
dc7ef9
+   This program is free software; you can redistribute it and/or modify
dc7ef9
+   it under the terms of the GNU General Public License as published by
dc7ef9
+   the Free Software Foundation; either version 3 of the License, or
dc7ef9
+   (at your option) any later version.
dc7ef9
+
dc7ef9
+   This program is distributed in the hope that it will be useful,
dc7ef9
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
dc7ef9
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dc7ef9
+   GNU General Public License for more details.
dc7ef9
+
dc7ef9
+   You should have received a copy of the GNU General Public License
dc7ef9
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
dc7ef9
+
dc7ef9
+#ifndef COMMON_SIGNALS_STATE_SAVE_RESTORE_H
dc7ef9
+#define COMMON_SIGNALS_STATE_SAVE_RESTORE_H
dc7ef9
+
dc7ef9
+/* Save/restore the signal actions of all signals, and the signal
dc7ef9
+   mask.
dc7ef9
+
dc7ef9
+   Since the exec family of functions does not reset the signal
dc7ef9
+   disposition of signals set to SIG_IGN, nor does it reset the signal
dc7ef9
+   mask, in order to be transparent, when spawning new child processes
dc7ef9
+   to debug (with "run", etc.), we must reset signal actions and mask
dc7ef9
+   back to what was originally inherited from gdb/gdbserver's parent,
dc7ef9
+   just before execing the target program to debug.  */
dc7ef9
+
dc7ef9
+/* Save the signal state of all signals.  */
dc7ef9
+
dc7ef9
+extern void save_original_signals_state (void);
dc7ef9
+
dc7ef9
+/* Restore the signal state of all signals.  */
dc7ef9
+
dc7ef9
+extern void restore_original_signals_state (void);
dc7ef9
+
dc7ef9
+#endif /* COMMON_SIGNALS_STATE_SAVE_RESTORE_H */
dc7ef9
Index: gdb-7.6.1/gdb/configure
dc7ef9
===================================================================
dc7ef9
--- gdb-7.6.1.orig/gdb/configure	2017-11-03 19:22:08.181997334 +0100
dc7ef9
+++ gdb-7.6.1/gdb/configure	2017-11-03 19:22:11.455021228 +0100
dc7ef9
@@ -10667,7 +10667,7 @@
dc7ef9
 		sbrk setpgid setpgrp setsid \
dc7ef9
 		sigaction sigprocmask sigsetmask socketpair syscall \
dc7ef9
 		ttrace wborder wresize setlocale iconvlist libiconvlist btowc \
dc7ef9
-		setrlimit getrlimit posix_madvise waitpid lstat
dc7ef9
+		setrlimit getrlimit posix_madvise waitpid lstat sigaction
dc7ef9
 do :
dc7ef9
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
dc7ef9
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
dc7ef9
Index: gdb-7.6.1/gdb/fork-child.c
dc7ef9
===================================================================
dc7ef9
--- gdb-7.6.1.orig/gdb/fork-child.c	2013-01-01 07:32:42.000000000 +0100
dc7ef9
+++ gdb-7.6.1/gdb/fork-child.c	2017-11-03 19:22:11.455021228 +0100
dc7ef9
@@ -32,7 +32,7 @@
dc7ef9
 #include "command.h" /* for dont_repeat () */
dc7ef9
 #include "gdbcmd.h"
dc7ef9
 #include "solib.h"
dc7ef9
-
dc7ef9
+#include "signals-state-save-restore.h"
dc7ef9
 #include <signal.h>
dc7ef9
 
dc7ef9
 /* This just gets used as a default if we can't find SHELL.  */
dc7ef9
@@ -350,6 +350,8 @@
dc7ef9
         saying "not parent".  Sorry; you'll have to use print
dc7ef9
         statements!  */
dc7ef9
 
dc7ef9
+      restore_original_signals_state ();
dc7ef9
+
dc7ef9
       /* There is no execlpe call, so we have to set the environment
dc7ef9
          for our child in the global variable.  If we've vforked, this
dc7ef9
          clobbers the parent, but environ is restored a few lines down
dc7ef9
Index: gdb-7.6.1/gdb/gdbserver/Makefile.in
dc7ef9
===================================================================
dc7ef9
--- gdb-7.6.1.orig/gdb/gdbserver/Makefile.in	2017-11-03 19:22:10.364013263 +0100
dc7ef9
+++ gdb-7.6.1/gdb/gdbserver/Makefile.in	2017-11-03 19:22:11.455021228 +0100
dc7ef9
@@ -170,6 +170,7 @@
dc7ef9
 	mem-break.o hostio.o event-loop.o tracepoint.o \
dc7ef9
 	xml-utils.o common-utils.o ptid.o buffer.o format.o \
dc7ef9
 	dll.o notif.o \
dc7ef9
+	signals-state-save-restore.o \
dc7ef9
 	$(XML_BUILTIN) \
dc7ef9
 	$(DEPFILES) $(LIBOBJS)
dc7ef9
 GDBREPLAY_OBS = gdbreplay.o version.o
dc7ef9
@@ -572,6 +573,9 @@
dc7ef9
 linux-namespaces.o: ../nat/linux-namespaces.c
dc7ef9
 	$(COMPILE) $<
dc7ef9
 	$(POSTCOMPILE)
dc7ef9
+signals-state-save-restore.o: ../common/signals-state-save-restore.c
dc7ef9
+	$(COMPILE) $<
dc7ef9
+	$(POSTCOMPILE)
dc7ef9
 
dc7ef9
 win32_low_h = $(srcdir)/win32-low.h
dc7ef9
 
dc7ef9
Index: gdb-7.6.1/gdb/gdbserver/config.in
dc7ef9
===================================================================
dc7ef9
--- gdb-7.6.1.orig/gdb/gdbserver/config.in	2017-11-03 19:22:10.364013263 +0100
dc7ef9
+++ gdb-7.6.1/gdb/gdbserver/config.in	2017-11-03 19:22:11.455021228 +0100
dc7ef9
@@ -152,6 +152,9 @@
dc7ef9
 /* Define to 1 if you have the <sgtty.h> header file. */
dc7ef9
 #undef HAVE_SGTTY_H
dc7ef9
 
dc7ef9
+/* Define to 1 if you have the `sigaction' function. */
dc7ef9
+#undef HAVE_SIGACTION
dc7ef9
+
dc7ef9
 /* Define to 1 if you have the <signal.h> header file. */
dc7ef9
 #undef HAVE_SIGNAL_H
dc7ef9
 
dc7ef9
Index: gdb-7.6.1/gdb/gdbserver/configure
dc7ef9
===================================================================
dc7ef9
--- gdb-7.6.1.orig/gdb/gdbserver/configure	2017-11-03 19:22:10.366013278 +0100
dc7ef9
+++ gdb-7.6.1/gdb/gdbserver/configure	2017-11-03 19:22:11.456021235 +0100
dc7ef9
@@ -4796,7 +4796,7 @@
dc7ef9
 
dc7ef9
 done
dc7ef9
 
dc7ef9
-for ac_func in pread pwrite pread64 readlink setns
dc7ef9
+for ac_func in pread pwrite pread64 readlink setns sigaction
dc7ef9
 do :
dc7ef9
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
dc7ef9
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
dc7ef9
Index: gdb-7.6.1/gdb/gdbserver/linux-low.c
dc7ef9
===================================================================
dc7ef9
--- gdb-7.6.1.orig/gdb/gdbserver/linux-low.c	2017-11-03 19:22:10.380013380 +0100
dc7ef9
+++ gdb-7.6.1/gdb/gdbserver/linux-low.c	2017-11-03 19:22:11.457021243 +0100
dc7ef9
@@ -20,7 +20,7 @@
dc7ef9
 #include "linux-low.h"
dc7ef9
 #include "linux-osdata.h"
dc7ef9
 #include "agent.h"
dc7ef9
-
dc7ef9
+#include "signals-state-save-restore.h"
dc7ef9
 #include "gdb_wait.h"
dc7ef9
 #include <stdio.h>
dc7ef9
 #include <sys/param.h>
dc7ef9
@@ -699,6 +699,8 @@
dc7ef9
 	    close (remote_desc);
dc7ef9
 	}
dc7ef9
 
dc7ef9
+      restore_original_signals_state ();
dc7ef9
+
dc7ef9
       execv (program, allargs);
dc7ef9
       if (errno == ENOENT)
dc7ef9
 	execvp (program, allargs);
dc7ef9
Index: gdb-7.6.1/gdb/gdbserver/server.c
dc7ef9
===================================================================
dc7ef9
--- gdb-7.6.1.orig/gdb/gdbserver/server.c	2017-11-03 19:22:10.380013380 +0100
dc7ef9
+++ gdb-7.6.1/gdb/gdbserver/server.c	2017-11-03 19:22:11.457021243 +0100
dc7ef9
@@ -20,7 +20,7 @@
dc7ef9
 #include "gdbthread.h"
dc7ef9
 #include "agent.h"
dc7ef9
 #include "notif.h"
dc7ef9
-
dc7ef9
+#include "signals-state-save-restore.h"
dc7ef9
 #if HAVE_UNISTD_H
dc7ef9
 #include <unistd.h>
dc7ef9
 #endif
dc7ef9
@@ -2896,6 +2896,8 @@
dc7ef9
       exit (1);
dc7ef9
     }
dc7ef9
 
dc7ef9
+  save_original_signals_state ();
dc7ef9
+
dc7ef9
   /* We need to know whether the remote connection is stdio before
dc7ef9
      starting the inferior.  Inferiors created in this scenario have
dc7ef9
      stdin,stdout redirected.  So do this here before we call
dc7ef9
Index: gdb-7.6.1/gdb/main.c
dc7ef9
===================================================================
dc7ef9
--- gdb-7.6.1.orig/gdb/main.c	2017-11-03 19:22:10.319012935 +0100
dc7ef9
+++ gdb-7.6.1/gdb/main.c	2017-11-03 19:22:11.458021250 +0100
dc7ef9
@@ -45,6 +45,7 @@
dc7ef9
 #include "auto-load.h"
dc7ef9
 
dc7ef9
 #include "filenames.h"
dc7ef9
+#include "signals-state-save-restore.h"
dc7ef9
 
dc7ef9
 /* The selected interpreter.  This will be used as a set command
dc7ef9
    variable, so it should always be malloc'ed - since
dc7ef9
@@ -393,6 +394,7 @@
dc7ef9
   textdomain (PACKAGE);
dc7ef9
 
dc7ef9
   bfd_init ();
dc7ef9
+  save_original_signals_state ();
dc7ef9
 
dc7ef9
   make_cleanup (VEC_cleanup (cmdarg_s), &cmdarg_vec);
dc7ef9
   dirsize = 1;
dc7ef9
Index: gdb-7.6.1/gdb/testsuite/gdb.base/signals-state-child.c
dc7ef9
===================================================================
dc7ef9
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
dc7ef9
+++ gdb-7.6.1/gdb/testsuite/gdb.base/signals-state-child.c	2017-11-03 19:22:11.458021250 +0100
dc7ef9
@@ -0,0 +1,101 @@
dc7ef9
+/* Copyright 2016 Free Software Foundation, Inc.
dc7ef9
+
dc7ef9
+   This file is part of GDB.
dc7ef9
+
dc7ef9
+   This program is free software; you can redistribute it and/or modify
dc7ef9
+   it under the terms of the GNU General Public License as published by
dc7ef9
+   the Free Software Foundation; either version 3 of the License, or
dc7ef9
+   (at your option) any later version.
dc7ef9
+
dc7ef9
+   This program is distributed in the hope that it will be useful,
dc7ef9
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
dc7ef9
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dc7ef9
+   GNU General Public License for more details.
dc7ef9
+
dc7ef9
+   You should have received a copy of the GNU General Public License
dc7ef9
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
dc7ef9
+
dc7ef9
+#include <stdlib.h>
dc7ef9
+#include <stdio.h>
dc7ef9
+#include <signal.h>
dc7ef9
+#include <assert.h>
dc7ef9
+#include <errno.h>
dc7ef9
+
dc7ef9
+#ifndef OUTPUT_TXT
dc7ef9
+# define OUTPUT_TXT "output.txt"
dc7ef9
+#endif
dc7ef9
+
dc7ef9
+static void
dc7ef9
+perror_and_exit (const char *s)
dc7ef9
+{
dc7ef9
+  perror (s);
dc7ef9
+  exit (1);
dc7ef9
+}
dc7ef9
+
dc7ef9
+int
dc7ef9
+main (int argc, char **argv)
dc7ef9
+{
dc7ef9
+  int i;
dc7ef9
+  FILE *out;
dc7ef9
+  sigset_t sigset;
dc7ef9
+  int res;
dc7ef9
+
dc7ef9
+  res = sigprocmask (0,  NULL, &sigset);
dc7ef9
+  if (res != 0)
dc7ef9
+    perror_and_exit ("sigprocmask");
dc7ef9
+
dc7ef9
+  if (argc > 1)
dc7ef9
+    out = stdout;
dc7ef9
+  else
dc7ef9
+    {
dc7ef9
+      out = fopen (OUTPUT_TXT, "w");
dc7ef9
+      if (out == NULL)
dc7ef9
+	perror_and_exit ("fopen");
dc7ef9
+    }
dc7ef9
+
dc7ef9
+  for (i = 1; i < NSIG; i++)
dc7ef9
+    {
dc7ef9
+      struct sigaction oldact;
dc7ef9
+
dc7ef9
+      fprintf (out, "signal %d: ", i);
dc7ef9
+
dc7ef9
+      res = sigaction (i, NULL, &oldact);
dc7ef9
+      if (res == -1 && errno == EINVAL)
dc7ef9
+	{
dc7ef9
+	  /* Some signal numbers in the range are invalid.  E.g.,
dc7ef9
+	     signals 32 and 33 on GNU/Linux.  */
dc7ef9
+	  fprintf (out, "invalid");
dc7ef9
+	}
dc7ef9
+      else if (res == -1)
dc7ef9
+	{
dc7ef9
+	  perror_and_exit ("sigaction");
dc7ef9
+	}
dc7ef9
+      else
dc7ef9
+	{
dc7ef9
+	  int m;
dc7ef9
+
dc7ef9
+	  fprintf (out, "sigaction={sa_handler=", i);
dc7ef9
+
dc7ef9
+	  if (oldact.sa_handler == SIG_DFL)
dc7ef9
+	    fprintf (out, "SIG_DFL");
dc7ef9
+	  else if (oldact.sa_handler == SIG_IGN)
dc7ef9
+	    fprintf (out, "SIG_IGN");
dc7ef9
+	  else
dc7ef9
+	    abort ();
dc7ef9
+
dc7ef9
+	  fprintf (out, ", sa_mask=");
dc7ef9
+	  for (m = 1; m < NSIG; m++)
dc7ef9
+	    fprintf (out, "%c", sigismember (&oldact.sa_mask, m) ? '1' : '0');
dc7ef9
+
dc7ef9
+	  fprintf (out, ", sa_flags=%d", oldact.sa_flags);
dc7ef9
+
dc7ef9
+	  fprintf (out, "}, masked=%d", sigismember (&sigset, i));
dc7ef9
+	}
dc7ef9
+      fprintf (out, "\n");
dc7ef9
+    }
dc7ef9
+
dc7ef9
+  if (out != stdout)
dc7ef9
+    fclose (out);
dc7ef9
+
dc7ef9
+  return 0;
dc7ef9
+}
dc7ef9
Index: gdb-7.6.1/gdb/testsuite/gdb.base/signals-state-child.exp
dc7ef9
===================================================================
dc7ef9
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
dc7ef9
+++ gdb-7.6.1/gdb/testsuite/gdb.base/signals-state-child.exp	2017-11-03 19:22:11.458021250 +0100
dc7ef9
@@ -0,0 +1,82 @@
dc7ef9
+# Copyright 2016 Free Software Foundation, Inc.
dc7ef9
+
dc7ef9
+# This program is free software; you can redistribute it and/or modify
dc7ef9
+# it under the terms of the GNU General Public License as published by
dc7ef9
+# the Free Software Foundation; either version 3 of the License, or
dc7ef9
+# (at your option) any later version.
dc7ef9
+#
dc7ef9
+# This program is distributed in the hope that it will be useful,
dc7ef9
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
dc7ef9
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dc7ef9
+# GNU General Public License for more details.
dc7ef9
+#
dc7ef9
+# You should have received a copy of the GNU General Public License
dc7ef9
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
dc7ef9
+
dc7ef9
+# Test that gdb's (or gdbserver's) own signal handling does not
dc7ef9
+# interfere with the signal actions (dispositions, etc.) and mask
dc7ef9
+# their spawned children inherit.
dc7ef9
+#
dc7ef9
+# - If gdb inherits some signal set to SIG_IGN, so should the
dc7ef9
+#   inferior, even if gdb itself chooses not to ignore the signal.
dc7ef9
+#
dc7ef9
+# - If gdb inherits some signal set to SIG_DFL, so should the inferior
dc7ef9
+#   even if gdb itself ignores that signal.
dc7ef9
+#
dc7ef9
+# This requires special support in gdb/gdbserver because the exec
dc7ef9
+# family of functions does not reset the signal disposition of signals
dc7ef9
+# that are set to SIG_IGN, nor signal masks and flags.
dc7ef9
+
dc7ef9
+standard_testfile
dc7ef9
+
dc7ef9
+set gdb_txt [standard_output_file gdb.txt]
dc7ef9
+set standalone_txt [standard_output_file standalone.txt]
dc7ef9
+remote_exec host "rm -f $gdb_txt"
dc7ef9
+remote_exec host "rm -f $standalone_txt"
dc7ef9
+
dc7ef9
+set options [list debug "additional_flags=-DOUTPUT_TXT=\"$gdb_txt\""]
dc7ef9
+if {[build_executable $testfile.exp $testfile $srcfile $options]} {
dc7ef9
+    untested $testfile.exp
dc7ef9
+    return -1
dc7ef9
+}
dc7ef9
+
dc7ef9
+set options [list debug "additional_flags=-DOUTPUT_TXT=\"$standalone_txt\""]
dc7ef9
+if {[build_executable $testfile.exp $testfile-standalone $srcfile $options]} {
dc7ef9
+    untested $testfile.exp
dc7ef9
+    return -1
dc7ef9
+}
dc7ef9
+
dc7ef9
+# Run the program directly, and dump its initial signal actions and
dc7ef9
+# mask in "standalone.txt".
dc7ef9
+
dc7ef9
+# Use remote_spawn instead of remote_exec, like how we spawn gdb.
dc7ef9
+# This is in order to take the same code code paths in dejagnu
dc7ef9
+# compared to when running the program through gdb.  E.g., because
dc7ef9
+# local_exec uses -ignore SIGHUP, while remote_spawn does not, if we
dc7ef9
+# used remote_exec, the test program would start with SIGHUP ignored
dc7ef9
+# when run standalone, but not when run through gdb.
dc7ef9
+set res [remote_spawn host "$binfile-standalone"]
dc7ef9
+if { $res < 0 || $res == "" } {
dc7ef9
+    untested "spawning $binfile-standalone failed"
dc7ef9
+    return 1
dc7ef9
+} else {
dc7ef9
+    pass "collect standalone signals state"
dc7ef9
+}
dc7ef9
+remote_close host
dc7ef9
+
dc7ef9
+# Now run the program through gdb, and dump its initial signal actions
dc7ef9
+# and mask in "gdb.txt".
dc7ef9
+
dc7ef9
+clean_restart $binfile
dc7ef9
+
dc7ef9
+if { ! [ runto_main ] } then {
dc7ef9
+    untested $testfile.exp
dc7ef9
+    return -1
dc7ef9
+}
dc7ef9
+
dc7ef9
+gdb_continue_to_end "collect signals state under gdb"
dc7ef9
+
dc7ef9
+# Diff the .txt files.  They should be identical.
dc7ef9
+gdb_test "shell diff -s $standalone_txt $gdb_txt" \
dc7ef9
+    "Files .* are identical.*" \
dc7ef9
+    "signals states are identical"
dc7ef9
Index: gdb-7.6.1/gdb/testsuite/gdb.gdb/selftest.exp
dc7ef9
===================================================================
dc7ef9
--- gdb-7.6.1.orig/gdb/testsuite/gdb.gdb/selftest.exp	2017-11-03 19:22:06.904988012 +0100
dc7ef9
+++ gdb-7.6.1/gdb/testsuite/gdb.gdb/selftest.exp	2017-11-03 19:22:11.458021250 +0100
dc7ef9
@@ -158,6 +158,10 @@
dc7ef9
 		set description "next over bfd_init"
dc7ef9
 		set command "next"
dc7ef9
 	    }
dc7ef9
+	    -re ".*save_original_signals_state ..;.*$gdb_prompt $" {
dc7ef9
+		set description "next over save_original_signals_state"
dc7ef9
+		set command "next"
dc7ef9
+	    }
dc7ef9
 	    -re ".*VEC_cleanup .cmdarg_s.*$gdb_prompt $" {
dc7ef9
 		set description "next over cmdarg_s VEC_cleanup"
dc7ef9
 		set command "next"
dc7ef9
Index: gdb-7.6.1/gdb/configure.ac
dc7ef9
===================================================================
dc7ef9
--- gdb-7.6.1.orig/gdb/configure.ac	2017-11-03 19:22:08.164997210 +0100
dc7ef9
+++ gdb-7.6.1/gdb/configure.ac	2017-11-03 19:22:11.459021257 +0100
dc7ef9
@@ -1365,7 +1365,7 @@
dc7ef9
 		sbrk setpgid setpgrp setsid \
dc7ef9
 		sigaction sigprocmask sigsetmask socketpair syscall \
dc7ef9
 		ttrace wborder wresize setlocale iconvlist libiconvlist btowc \
dc7ef9
-		setrlimit getrlimit posix_madvise waitpid lstat])
dc7ef9
+		setrlimit getrlimit posix_madvise waitpid lstat sigaction])
dc7ef9
 AM_LANGINFO_CODESET
dc7ef9
 
dc7ef9
 # Check the return and argument types of ptrace.  No canned test for
dc7ef9
Index: gdb-7.6.1/gdb/gdbserver/configure.ac
dc7ef9
===================================================================
dc7ef9
--- gdb-7.6.1.orig/gdb/gdbserver/configure.ac	2017-11-03 19:22:10.366013278 +0100
dc7ef9
+++ gdb-7.6.1/gdb/gdbserver/configure.ac	2017-11-03 19:22:11.459021257 +0100
dc7ef9
@@ -70,7 +70,7 @@
dc7ef9
 		 sys/ioctl.h netinet/in.h sys/socket.h netdb.h dnl
dc7ef9
 		 netinet/tcp.h arpa/inet.h sys/wait.h wait.h sys/un.h dnl
dc7ef9
 		 linux/perf_event.h)
dc7ef9
-AC_CHECK_FUNCS(pread pwrite pread64 readlink setns)
dc7ef9
+AC_CHECK_FUNCS(pread pwrite pread64 readlink setns sigaction)
dc7ef9
 AC_REPLACE_FUNCS(vasprintf vsnprintf)
dc7ef9
 
dc7ef9
 # Check for UST