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

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