Home | History | Annotate | Line # | Download | only in gdb
inflow.c revision 1.1.1.6
      1      1.1  christos /* Low level interface to ptrace, for GDB when running under Unix.
      2  1.1.1.6  christos    Copyright (C) 1986-2020 Free Software Foundation, Inc.
      3      1.1  christos 
      4      1.1  christos    This file is part of GDB.
      5      1.1  christos 
      6      1.1  christos    This program is free software; you can redistribute it and/or modify
      7      1.1  christos    it under the terms of the GNU General Public License as published by
      8      1.1  christos    the Free Software Foundation; either version 3 of the License, or
      9      1.1  christos    (at your option) any later version.
     10      1.1  christos 
     11      1.1  christos    This program is distributed in the hope that it will be useful,
     12      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14      1.1  christos    GNU General Public License for more details.
     15      1.1  christos 
     16      1.1  christos    You should have received a copy of the GNU General Public License
     17      1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     18      1.1  christos 
     19      1.1  christos #include "defs.h"
     20      1.1  christos #include "frame.h"
     21      1.1  christos #include "inferior.h"
     22      1.1  christos #include "command.h"
     23      1.1  christos #include "serial.h"
     24      1.1  christos #include "terminal.h"
     25      1.1  christos #include "target.h"
     26      1.1  christos #include "gdbthread.h"
     27  1.1.1.5  christos #include "observable.h"
     28      1.1  christos #include <signal.h>
     29      1.1  christos #include <fcntl.h>
     30  1.1.1.6  christos #include "gdbsupport/gdb_select.h"
     31      1.1  christos 
     32      1.1  christos #include "inflow.h"
     33      1.1  christos #include "gdbcmd.h"
     34  1.1.1.5  christos #ifdef HAVE_TERMIOS_H
     35  1.1.1.5  christos #include <termios.h>
     36  1.1.1.5  christos #endif
     37  1.1.1.6  christos #include "gdbsupport/job-control.h"
     38      1.1  christos 
     39      1.1  christos #ifdef HAVE_SYS_IOCTL_H
     40      1.1  christos #include <sys/ioctl.h>
     41      1.1  christos #endif
     42      1.1  christos 
     43      1.1  christos #ifndef O_NOCTTY
     44      1.1  christos #define O_NOCTTY 0
     45      1.1  christos #endif
     46      1.1  christos 
     47      1.1  christos static void pass_signal (int);
     48      1.1  christos 
     49  1.1.1.5  christos static void child_terminal_ours_1 (target_terminal_state);
     50      1.1  christos 
     51      1.1  christos /* Record terminal status separately for debugger and inferior.  */
     53      1.1  christos 
     54      1.1  christos static struct serial *stdin_serial;
     55      1.1  christos 
     56      1.1  christos /* Terminal related info we need to keep track of.  Each inferior
     57  1.1.1.5  christos    holds an instance of this structure --- we save it whenever the
     58  1.1.1.5  christos    corresponding inferior stops, and restore it to the terminal when
     59      1.1  christos    the inferior is resumed in the foreground.  */
     60      1.1  christos struct terminal_info
     61  1.1.1.6  christos {
     62  1.1.1.6  christos   terminal_info () = default;
     63  1.1.1.6  christos   ~terminal_info ();
     64  1.1.1.6  christos 
     65  1.1.1.6  christos   terminal_info &operator= (const terminal_info &) = default;
     66      1.1  christos 
     67      1.1  christos   /* The name of the tty (from the `tty' command) that we gave to the
     68  1.1.1.6  christos      inferior when it was started.  */
     69      1.1  christos   char *run_terminal = nullptr;
     70      1.1  christos 
     71  1.1.1.5  christos   /* TTY state.  We save it whenever the inferior stops, and restore
     72  1.1.1.6  christos      it when it resumes in the foreground.  */
     73      1.1  christos   serial_ttystate ttystate {};
     74  1.1.1.5  christos 
     75  1.1.1.5  christos #ifdef HAVE_TERMIOS_H
     76  1.1.1.5  christos   /* The terminal's foreground process group.  Saved whenever the
     77  1.1.1.5  christos      inferior stops.  This is the pgrp displayed by "info terminal".
     78  1.1.1.5  christos      Note that this may be not the inferior's actual process group,
     79  1.1.1.5  christos      since each inferior that we spawn has its own process group, and
     80  1.1.1.5  christos      only one can be in the foreground at a time.  When the inferior
     81  1.1.1.5  christos      resumes, if we can determine the inferior's actual pgrp, then we
     82  1.1.1.5  christos      make that the foreground pgrp instead of what was saved here.
     83  1.1.1.5  christos      While it's a bit arbitrary which inferior's pgrp ends up in the
     84  1.1.1.5  christos      foreground when we resume several inferiors, this at least makes
     85  1.1.1.5  christos      'resume inf1+inf2' + 'stop all' + 'resume inf2' end up with
     86  1.1.1.5  christos      inf2's pgrp in the foreground instead of inf1's (which would be
     87  1.1.1.5  christos      problematic since it would be left stopped: Ctrl-C wouldn't work,
     88  1.1.1.6  christos      for example).  */
     89      1.1  christos   pid_t process_group = 0;
     90      1.1  christos #endif
     91      1.1  christos 
     92  1.1.1.6  christos   /* fcntl flags.  Saved and restored just like ttystate.  */
     93      1.1  christos   int tflags = 0;
     94      1.1  christos };
     95      1.1  christos 
     96  1.1.1.3  christos /* Our own tty state, which we restore every time we need to deal with
     97  1.1.1.3  christos    the terminal.  This is set once, when GDB first starts, and then
     98  1.1.1.3  christos    whenever we enter/leave TUI mode (gdb_save_tty_state).  The
     99      1.1  christos    settings of flags which readline saves and restores are
    100      1.1  christos    unimportant.  */
    101      1.1  christos static struct terminal_info our_terminal_info;
    102  1.1.1.3  christos 
    103  1.1.1.3  christos /* Snapshot of the initial tty state taken during initialization of
    104  1.1.1.3  christos    GDB, before readline/ncurses have had a chance to change it.  This
    105  1.1.1.3  christos    is used as the initial tty state given to each new spawned
    106  1.1.1.3  christos    inferior.  Unlike our_terminal_info, this is only ever set
    107  1.1.1.2  christos    once.  */
    108  1.1.1.2  christos static serial_ttystate initial_gdb_ttystate;
    109      1.1  christos 
    110      1.1  christos static struct terminal_info *get_inflow_inferior_data (struct inferior *);
    111      1.1  christos 
    112      1.1  christos /* While the inferior is running, we want SIGINT and SIGQUIT to go to the
    113      1.1  christos    inferior only.  If we have job control, that takes care of it.  If not,
    114      1.1  christos    we save our handlers in these two variables and set SIGINT and SIGQUIT
    115      1.1  christos    to SIG_IGN.  */
    116  1.1.1.3  christos 
    117  1.1.1.5  christos static sighandler_t sigint_ours;
    118  1.1.1.3  christos #ifdef SIGQUIT
    119  1.1.1.5  christos static sighandler_t sigquit_ours;
    120      1.1  christos #endif
    121      1.1  christos 
    122      1.1  christos /* The name of the tty (from the `tty' command) that we're giving to
    123      1.1  christos    the inferior when starting it up.  This is only (and should only
    124      1.1  christos    be) used as a transient global by new_tty_prefork,
    125      1.1  christos    create_tty_session, new_tty and new_tty_postfork, all called from
    126      1.1  christos    fork_inferior, while forking a new child.  */
    127      1.1  christos static const char *inferior_thisrun_terminal;
    128  1.1.1.5  christos 
    129  1.1.1.5  christos /* Track who owns GDB's terminal (is it GDB or some inferior?).  While
    130  1.1.1.5  christos    target_terminal::is_ours() etc. tracks the core's intention and is
    131  1.1.1.5  christos    independent of the target backend, this tracks the actual state of
    132  1.1.1.5  christos    GDB's own tty.  So for example,
    133  1.1.1.5  christos 
    134  1.1.1.5  christos      (target_terminal::is_inferior () && gdb_tty_state == terminal_is_ours)
    135  1.1.1.5  christos 
    136  1.1.1.5  christos    is true when the (native) inferior is not sharing a terminal with
    137  1.1.1.5  christos    GDB (e.g., because we attached to an inferior that is running on a
    138  1.1.1.5  christos    different terminal).  */
    139      1.1  christos static target_terminal_state gdb_tty_state = target_terminal_state::is_ours;
    140  1.1.1.3  christos 
    141  1.1.1.2  christos /* See terminal.h.  */
    142  1.1.1.2  christos 
    143  1.1.1.2  christos void
    144  1.1.1.2  christos set_initial_gdb_ttystate (void)
    145  1.1.1.3  christos {
    146  1.1.1.3  christos   /* Note we can't do any of this in _initialize_inflow because at
    147  1.1.1.2  christos      that point stdin_serial has not been created yet.  */
    148  1.1.1.3  christos 
    149      1.1  christos   initial_gdb_ttystate = serial_get_tty_state (stdin_serial);
    150  1.1.1.3  christos 
    151      1.1  christos   if (initial_gdb_ttystate != NULL)
    152  1.1.1.3  christos     {
    153  1.1.1.3  christos       our_terminal_info.ttystate
    154      1.1  christos 	= serial_copy_tty_state (stdin_serial, initial_gdb_ttystate);
    155      1.1  christos #ifdef F_GETFL
    156      1.1  christos       our_terminal_info.tflags = fcntl (0, F_GETFL, 0);
    157  1.1.1.5  christos #endif
    158  1.1.1.5  christos #ifdef HAVE_TERMIOS_H
    159      1.1  christos       our_terminal_info.process_group = tcgetpgrp (0);
    160      1.1  christos #endif
    161      1.1  christos     }
    162      1.1  christos }
    163  1.1.1.3  christos 
    164  1.1.1.3  christos /* Does GDB have a terminal (on stdin)?  */
    165  1.1.1.3  christos 
    166  1.1.1.3  christos static int
    167  1.1.1.3  christos gdb_has_a_terminal (void)
    168  1.1.1.3  christos {
    169  1.1.1.3  christos   return initial_gdb_ttystate != NULL;
    170  1.1.1.3  christos }
    171      1.1  christos 
    172      1.1  christos /* Macro for printing errors from ioctl operations */
    173      1.1  christos 
    174      1.1  christos #define	OOPSY(what)	\
    175      1.1  christos   if (result == -1)	\
    176      1.1  christos     fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
    177      1.1  christos 	    what, safe_strerror (errno))
    178      1.1  christos 
    179      1.1  christos /* Initialize the terminal settings we record for the inferior,
    180      1.1  christos    before we actually run the inferior.  */
    181      1.1  christos 
    182  1.1.1.5  christos void
    183      1.1  christos child_terminal_init (struct target_ops *self)
    184  1.1.1.5  christos {
    185  1.1.1.5  christos   if (!gdb_has_a_terminal ())
    186  1.1.1.5  christos     return;
    187  1.1.1.5  christos 
    188  1.1.1.5  christos   inferior *inf = current_inferior ();
    189      1.1  christos   terminal_info *tinfo = get_inflow_inferior_data (inf);
    190  1.1.1.5  christos 
    191  1.1.1.5  christos #ifdef HAVE_TERMIOS_H
    192  1.1.1.5  christos   /* A child we spawn should be a process group leader (PGID==PID) at
    193  1.1.1.5  christos      this point, though that may not be true if we're attaching to an
    194  1.1.1.5  christos      existing process.  */
    195      1.1  christos   tinfo->process_group = inf->pid;
    196      1.1  christos #endif
    197  1.1.1.5  christos 
    198  1.1.1.5  christos   xfree (tinfo->ttystate);
    199      1.1  christos   tinfo->ttystate = serial_copy_tty_state (stdin_serial, initial_gdb_ttystate);
    200      1.1  christos }
    201      1.1  christos 
    202      1.1  christos /* Save the terminal settings again.  This is necessary for the TUI
    203      1.1  christos    when it switches to TUI or non-TUI mode;  curses changes the terminal
    204      1.1  christos    and gdb must be able to restore it correctly.  */
    205      1.1  christos 
    206  1.1.1.2  christos void
    207      1.1  christos gdb_save_tty_state (void)
    208      1.1  christos {
    209      1.1  christos   if (gdb_has_a_terminal ())
    210      1.1  christos     {
    211      1.1  christos       xfree (our_terminal_info.ttystate);
    212      1.1  christos       our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);
    213      1.1  christos     }
    214      1.1  christos }
    215  1.1.1.5  christos 
    216  1.1.1.5  christos /* Try to determine whether TTY is GDB's input terminal.  Returns
    217  1.1.1.5  christos    TRIBOOL_UNKNOWN if we can't tell.  */
    218  1.1.1.5  christos 
    219  1.1.1.5  christos static tribool
    220      1.1  christos is_gdb_terminal (const char *tty)
    221  1.1.1.5  christos {
    222  1.1.1.5  christos   struct stat gdb_tty;
    223  1.1.1.5  christos   struct stat other_tty;
    224  1.1.1.5  christos   int res;
    225  1.1.1.5  christos 
    226  1.1.1.5  christos   res = stat (tty, &other_tty);
    227  1.1.1.5  christos   if (res == -1)
    228  1.1.1.5  christos     return TRIBOOL_UNKNOWN;
    229  1.1.1.5  christos 
    230  1.1.1.5  christos   res = fstat (STDIN_FILENO, &gdb_tty);
    231  1.1.1.5  christos   if (res == -1)
    232  1.1.1.5  christos     return TRIBOOL_UNKNOWN;
    233  1.1.1.5  christos 
    234  1.1.1.5  christos   return ((gdb_tty.st_dev == other_tty.st_dev
    235  1.1.1.5  christos 	   && gdb_tty.st_ino == other_tty.st_ino)
    236  1.1.1.5  christos 	  ? TRIBOOL_TRUE
    237      1.1  christos 	  : TRIBOOL_FALSE);
    238      1.1  christos }
    239  1.1.1.5  christos 
    240  1.1.1.5  christos /* Helper for sharing_input_terminal.  Try to determine whether
    241  1.1.1.5  christos    inferior INF is using the same TTY for input as GDB is.  Returns
    242  1.1.1.2  christos    TRIBOOL_UNKNOWN if we can't tell.  */
    243  1.1.1.5  christos 
    244  1.1.1.5  christos static tribool
    245  1.1.1.5  christos sharing_input_terminal_1 (inferior *inf)
    246  1.1.1.5  christos {
    247  1.1.1.5  christos   /* Using host-dependent code here is fine, because the
    248  1.1.1.5  christos      child_terminal_foo functions are meant to be used by child/native
    249  1.1.1.5  christos      targets.  */
    250  1.1.1.5  christos #if defined (__linux__) || defined (__sun__)
    251  1.1.1.5  christos   char buf[100];
    252  1.1.1.5  christos 
    253  1.1.1.5  christos   xsnprintf (buf, sizeof (buf), "/proc/%d/fd/0", inf->pid);
    254  1.1.1.5  christos   return is_gdb_terminal (buf);
    255  1.1.1.5  christos #else
    256  1.1.1.5  christos   return TRIBOOL_UNKNOWN;
    257  1.1.1.5  christos #endif
    258  1.1.1.5  christos }
    259  1.1.1.5  christos 
    260  1.1.1.5  christos /* Return true if the inferior is using the same TTY for input as GDB
    261  1.1.1.5  christos    is.  If this is true, then we save/restore terminal flags/state.
    262  1.1.1.5  christos 
    263  1.1.1.5  christos    This is necessary because if inf->attach_flag is set, we don't
    264  1.1.1.5  christos    offhand know whether we are sharing a terminal with the inferior or
    265  1.1.1.5  christos    not.  Attaching a process without a terminal is one case where we
    266  1.1.1.5  christos    do not; attaching a process which we ran from the same shell as GDB
    267  1.1.1.5  christos    via `&' is one case where we do.
    268  1.1.1.5  christos 
    269  1.1.1.5  christos    If we can't determine, we assume the TTY is being shared.  This
    270  1.1.1.5  christos    works OK if you're only debugging one inferior.  However, if you're
    271  1.1.1.5  christos    debugging more than one inferior, and e.g., one is spawned by GDB
    272  1.1.1.5  christos    with "run" (sharing terminal with GDB), and another is attached to
    273  1.1.1.5  christos    (and running on a different terminal, as is most common), then it
    274  1.1.1.5  christos    matters, because we can only restore the terminal settings of one
    275  1.1.1.5  christos    of the inferiors, and in that scenario, we want to restore the
    276  1.1.1.5  christos    settings of the "run"'ed inferior.
    277  1.1.1.5  christos 
    278  1.1.1.5  christos    Note, this is not the same as determining whether GDB and the
    279  1.1.1.5  christos    inferior are in the same session / connected to the same
    280  1.1.1.5  christos    controlling tty.  An inferior (fork child) may call setsid,
    281  1.1.1.5  christos    disconnecting itself from the ctty, while still leaving
    282  1.1.1.5  christos    stdin/stdout/stderr associated with the original terminal.  If
    283  1.1.1.5  christos    we're debugging that process, we should also save/restore terminal
    284  1.1.1.5  christos    settings.  */
    285  1.1.1.5  christos 
    286  1.1.1.5  christos static bool
    287  1.1.1.5  christos sharing_input_terminal (inferior *inf)
    288  1.1.1.5  christos {
    289  1.1.1.5  christos   terminal_info *tinfo = get_inflow_inferior_data (inf);
    290  1.1.1.5  christos 
    291  1.1.1.5  christos   tribool res = sharing_input_terminal_1 (inf);
    292  1.1.1.5  christos 
    293  1.1.1.5  christos   if (res == TRIBOOL_UNKNOWN)
    294  1.1.1.5  christos     {
    295  1.1.1.5  christos       /* As fallback, if we can't determine by stat'ing the inferior's
    296  1.1.1.5  christos 	 tty directly (because it's not supported on this host) and
    297  1.1.1.5  christos 	 the child was spawned, check whether run_terminal is our tty.
    298  1.1.1.5  christos 	 This isn't ideal, since this is checking the child's
    299  1.1.1.5  christos 	 controlling terminal, not the input terminal (which may have
    300  1.1.1.5  christos 	 been redirected), but is still better than nothing.  A false
    301  1.1.1.5  christos 	 positive ("set inferior-tty" points to our terminal, but I/O
    302  1.1.1.5  christos 	 was redirected) is much more likely than a false negative
    303  1.1.1.5  christos 	 ("set inferior-tty" points to some other terminal, and then
    304  1.1.1.5  christos 	 output was redirected to our terminal), and with a false
    305  1.1.1.5  christos 	 positive we just end up trying to save/restore terminal
    306  1.1.1.5  christos 	 settings when we didn't need to or we actually can't.  */
    307  1.1.1.5  christos       if (tinfo->run_terminal != NULL)
    308  1.1.1.5  christos 	res = is_gdb_terminal (tinfo->run_terminal);
    309  1.1.1.5  christos 
    310  1.1.1.5  christos       /* If we still can't determine, assume yes.  */
    311  1.1.1.5  christos       if (res == TRIBOOL_UNKNOWN)
    312  1.1.1.5  christos 	return true;
    313  1.1.1.5  christos     }
    314  1.1.1.5  christos 
    315  1.1.1.5  christos   return res == TRIBOOL_TRUE;
    316  1.1.1.5  christos }
    317  1.1.1.5  christos 
    318  1.1.1.5  christos /* Put the inferior's terminal settings into effect.  This is
    319      1.1  christos    preparation for starting or resuming the inferior.  */
    320      1.1  christos 
    321  1.1.1.2  christos void
    322      1.1  christos child_terminal_inferior (struct target_ops *self)
    323  1.1.1.5  christos {
    324  1.1.1.5  christos   /* If we resume more than one inferior in the foreground on GDB's
    325  1.1.1.5  christos      terminal, then the first inferior's terminal settings "win".
    326  1.1.1.5  christos      Note that every child process is put in its own process group, so
    327  1.1.1.5  christos      the first process that ends up resumed ends up determining which
    328  1.1.1.5  christos      process group the kernel forwards Ctrl-C/Ctrl-Z (SIGINT/SIGTTOU)
    329  1.1.1.5  christos      to.  */
    330      1.1  christos   if (gdb_tty_state == target_terminal_state::is_inferior)
    331      1.1  christos     return;
    332  1.1.1.5  christos 
    333  1.1.1.5  christos   inferior *inf = current_inferior ();
    334      1.1  christos   terminal_info *tinfo = get_inflow_inferior_data (inf);
    335      1.1  christos 
    336      1.1  christos   if (gdb_has_a_terminal ()
    337  1.1.1.5  christos       && tinfo->ttystate != NULL
    338      1.1  christos       && sharing_input_terminal (inf))
    339      1.1  christos     {
    340      1.1  christos       int result;
    341  1.1.1.5  christos 
    342  1.1.1.5  christos       /* Ignore SIGTTOU since it will happen when we try to set the
    343  1.1.1.5  christos 	 terminal's state (if gdb_tty_state is currently
    344  1.1.1.5  christos 	 ours_for_output).  */
    345  1.1.1.5  christos       scoped_ignore_sigttou ignore_sigttou;
    346      1.1  christos 
    347      1.1  christos #ifdef F_GETFL
    348      1.1  christos       result = fcntl (0, F_SETFL, tinfo->tflags);
    349      1.1  christos       OOPSY ("fcntl F_SETFL");
    350      1.1  christos #endif
    351  1.1.1.5  christos 
    352      1.1  christos       result = serial_set_tty_state (stdin_serial, tinfo->ttystate);
    353      1.1  christos       OOPSY ("setting tty state");
    354      1.1  christos 
    355      1.1  christos       if (!job_control)
    356  1.1.1.3  christos 	{
    357      1.1  christos 	  sigint_ours = signal (SIGINT, SIG_IGN);
    358  1.1.1.3  christos #ifdef SIGQUIT
    359      1.1  christos 	  sigquit_ours = signal (SIGQUIT, SIG_IGN);
    360      1.1  christos #endif
    361      1.1  christos 	}
    362      1.1  christos 
    363      1.1  christos       if (job_control)
    364  1.1.1.5  christos 	{
    365  1.1.1.5  christos #ifdef HAVE_TERMIOS_H
    366  1.1.1.5  christos 	  /* If we can't tell the inferior's actual process group,
    367  1.1.1.5  christos 	     then restore whatever was the foreground pgrp the last
    368  1.1.1.5  christos 	     time the inferior was running.  See also comments
    369  1.1.1.5  christos 	     describing terminal_state::process_group.  */
    370  1.1.1.5  christos #ifdef HAVE_GETPGID
    371  1.1.1.5  christos 	  result = tcsetpgrp (0, getpgid (inf->pid));
    372      1.1  christos #else
    373      1.1  christos 	  result = tcsetpgrp (0, tinfo->process_group);
    374  1.1.1.5  christos #endif
    375  1.1.1.5  christos 	  if (result == -1)
    376  1.1.1.5  christos 	    {
    377  1.1.1.5  christos #if 0
    378  1.1.1.5  christos 	      /* This fails if either GDB has no controlling terminal,
    379  1.1.1.5  christos 		 e.g., running under 'setsid(1)', or if the inferior
    380  1.1.1.5  christos 		 is not attached to GDB's controlling terminal.  E.g.,
    381  1.1.1.5  christos 		 if it called setsid to create a new session or used
    382  1.1.1.5  christos 		 the TIOCNOTTY ioctl, or simply if we've attached to a
    383  1.1.1.5  christos 		 process running on another terminal and we couldn't
    384  1.1.1.5  christos 		 tell whether it was sharing GDB's terminal (and so
    385  1.1.1.5  christos 		 assumed yes).  */
    386  1.1.1.5  christos 	      fprintf_unfiltered
    387  1.1.1.5  christos 		(gdb_stderr,
    388  1.1.1.5  christos 		 "[tcsetpgrp failed in child_terminal_inferior: %s]\n",
    389  1.1.1.5  christos 		 safe_strerror (errno));
    390  1.1.1.5  christos #endif
    391      1.1  christos 	    }
    392      1.1  christos #endif
    393      1.1  christos 	}
    394  1.1.1.5  christos 
    395      1.1  christos       gdb_tty_state = target_terminal_state::is_inferior;
    396      1.1  christos     }
    397      1.1  christos }
    398      1.1  christos 
    399      1.1  christos /* Put some of our terminal settings into effect,
    400      1.1  christos    enough to get proper results from our output,
    401      1.1  christos    but do not change into or out of RAW mode
    402      1.1  christos    so that no input is discarded.
    403      1.1  christos 
    404  1.1.1.2  christos    After doing this, either terminal_ours or terminal_inferior
    405  1.1.1.2  christos    should be called to get back to a normal state of affairs.
    406  1.1.1.2  christos 
    407  1.1.1.2  christos    N.B. The implementation is (currently) no different than
    408      1.1  christos    child_terminal_ours.  See child_terminal_ours_1.  */
    409      1.1  christos 
    410  1.1.1.2  christos void
    411      1.1  christos child_terminal_ours_for_output (struct target_ops *self)
    412  1.1.1.5  christos {
    413      1.1  christos   child_terminal_ours_1 (target_terminal_state::is_ours_for_output);
    414      1.1  christos }
    415      1.1  christos 
    416      1.1  christos /* Put our terminal settings into effect.
    417  1.1.1.2  christos    First record the inferior's terminal settings
    418  1.1.1.2  christos    so they can be restored properly later.
    419  1.1.1.2  christos 
    420  1.1.1.2  christos    N.B. Targets that want to use this with async support must build that
    421  1.1.1.2  christos    support on top of this (e.g., the caller still needs to add stdin to the
    422      1.1  christos    event loop).  E.g., see linux_nat_terminal_ours.  */
    423      1.1  christos 
    424  1.1.1.2  christos void
    425      1.1  christos child_terminal_ours (struct target_ops *self)
    426  1.1.1.5  christos {
    427      1.1  christos   child_terminal_ours_1 (target_terminal_state::is_ours);
    428      1.1  christos }
    429  1.1.1.5  christos 
    430  1.1.1.5  christos /* Save the current terminal settings in the inferior's terminal_info
    431      1.1  christos    cache.  */
    432  1.1.1.5  christos 
    433  1.1.1.5  christos void
    434      1.1  christos child_terminal_save_inferior (struct target_ops *self)
    435  1.1.1.5  christos {
    436  1.1.1.5  christos   /* Avoid attempting all the ioctl's when running in batch.  */
    437      1.1  christos   if (!gdb_has_a_terminal ())
    438      1.1  christos     return;
    439  1.1.1.5  christos 
    440  1.1.1.5  christos   inferior *inf = current_inferior ();
    441      1.1  christos   terminal_info *tinfo = get_inflow_inferior_data (inf);
    442  1.1.1.5  christos 
    443  1.1.1.5  christos   /* No need to save/restore if the inferior is not sharing GDB's
    444  1.1.1.5  christos      tty.  */
    445  1.1.1.5  christos   if (!sharing_input_terminal (inf))
    446      1.1  christos     return;
    447  1.1.1.5  christos 
    448  1.1.1.5  christos   xfree (tinfo->ttystate);
    449      1.1  christos   tinfo->ttystate = serial_get_tty_state (stdin_serial);
    450  1.1.1.5  christos 
    451  1.1.1.5  christos #ifdef HAVE_TERMIOS_H
    452      1.1  christos   tinfo->process_group = tcgetpgrp (0);
    453      1.1  christos #endif
    454  1.1.1.5  christos 
    455  1.1.1.5  christos #ifdef F_GETFL
    456      1.1  christos   tinfo->tflags = fcntl (0, F_GETFL, 0);
    457  1.1.1.5  christos #endif
    458      1.1  christos }
    459  1.1.1.5  christos 
    460  1.1.1.5  christos /* Switch terminal state to DESIRED_STATE, either is_ours, or
    461      1.1  christos    is_ours_for_output.  */
    462  1.1.1.5  christos 
    463  1.1.1.5  christos static void
    464  1.1.1.5  christos child_terminal_ours_1 (target_terminal_state desired_state)
    465  1.1.1.5  christos {
    466      1.1  christos   gdb_assert (desired_state != target_terminal_state::is_inferior);
    467  1.1.1.5  christos 
    468  1.1.1.5  christos   /* Avoid attempting all the ioctl's when running in batch.  */
    469  1.1.1.5  christos   if (!gdb_has_a_terminal ())
    470      1.1  christos     return;
    471  1.1.1.5  christos 
    472  1.1.1.5  christos   if (gdb_tty_state != desired_state)
    473  1.1.1.5  christos     {
    474  1.1.1.5  christos       int result ATTRIBUTE_UNUSED;
    475  1.1.1.5  christos 
    476  1.1.1.5  christos       /* Ignore SIGTTOU since it will happen when we try to set the
    477  1.1.1.5  christos 	 terminal's pgrp.  */
    478  1.1.1.5  christos       scoped_ignore_sigttou ignore_sigttou;
    479  1.1.1.5  christos 
    480  1.1.1.5  christos       /* Set tty state to our_ttystate.  */
    481  1.1.1.5  christos       serial_set_tty_state (stdin_serial, our_terminal_info.ttystate);
    482  1.1.1.5  christos 
    483  1.1.1.5  christos       /* If we only want output, then leave the inferior's pgrp in the
    484  1.1.1.5  christos 	 foreground, so that Ctrl-C/Ctrl-Z reach the inferior
    485  1.1.1.5  christos 	 directly.  */
    486      1.1  christos       if (job_control && desired_state == target_terminal_state::is_ours)
    487  1.1.1.5  christos 	{
    488      1.1  christos #ifdef HAVE_TERMIOS_H
    489      1.1  christos 	  result = tcsetpgrp (0, our_terminal_info.process_group);
    490      1.1  christos #if 0
    491      1.1  christos 	  /* This fails on Ultrix with EINVAL if you run the testsuite
    492      1.1  christos 	     in the background with nohup, and then log out.  GDB never
    493      1.1  christos 	     used to check for an error here, so perhaps there are other
    494      1.1  christos 	     such situations as well.  */
    495      1.1  christos 	  if (result == -1)
    496  1.1.1.2  christos 	    fprintf_unfiltered (gdb_stderr,
    497      1.1  christos 				"[tcsetpgrp failed in child_terminal_ours: %s]\n",
    498      1.1  christos 				safe_strerror (errno));
    499      1.1  christos #endif
    500      1.1  christos #endif /* termios */
    501      1.1  christos 	}
    502  1.1.1.5  christos 
    503      1.1  christos       if (!job_control && desired_state == target_terminal_state::is_ours)
    504      1.1  christos 	{
    505      1.1  christos 	  signal (SIGINT, sigint_ours);
    506      1.1  christos #ifdef SIGQUIT
    507      1.1  christos 	  signal (SIGQUIT, sigquit_ours);
    508      1.1  christos #endif
    509      1.1  christos 	}
    510      1.1  christos 
    511      1.1  christos #ifdef F_GETFL
    512      1.1  christos       result = fcntl (0, F_SETFL, our_terminal_info.tflags);
    513  1.1.1.5  christos #endif
    514  1.1.1.5  christos 
    515  1.1.1.5  christos       gdb_tty_state = desired_state;
    516  1.1.1.5  christos     }
    517  1.1.1.5  christos }
    518  1.1.1.5  christos 
    519  1.1.1.5  christos /* Interrupt the inferior.  Implementation of target_interrupt for
    520  1.1.1.5  christos    child/native targets.  */
    521  1.1.1.5  christos 
    522  1.1.1.5  christos void
    523  1.1.1.5  christos child_interrupt (struct target_ops *self)
    524  1.1.1.5  christos {
    525  1.1.1.5  christos   /* Interrupt the first inferior that has a resumed thread.  */
    526  1.1.1.5  christos   thread_info *resumed = NULL;
    527  1.1.1.5  christos   for (thread_info *thr : all_non_exited_threads ())
    528  1.1.1.5  christos     {
    529  1.1.1.5  christos       if (thr->executing)
    530  1.1.1.5  christos 	{
    531  1.1.1.5  christos 	  resumed = thr;
    532  1.1.1.5  christos 	  break;
    533  1.1.1.5  christos 	}
    534  1.1.1.5  christos       if (thr->suspend.waitstatus_pending_p)
    535  1.1.1.5  christos 	resumed = thr;
    536  1.1.1.5  christos     }
    537  1.1.1.5  christos 
    538  1.1.1.5  christos   if (resumed != NULL)
    539  1.1.1.5  christos     {
    540  1.1.1.5  christos       /* Note that unlike pressing Ctrl-C on the controlling terminal,
    541  1.1.1.5  christos 	 here we only interrupt one process, not the whole process
    542  1.1.1.5  christos 	 group.  This is because interrupting a process group (with
    543  1.1.1.5  christos 	 either Ctrl-C or with kill(3) with negative PID) sends a
    544  1.1.1.5  christos 	 SIGINT to each process in the process group, and we may not
    545  1.1.1.5  christos 	 be debugging all processes in the process group.  */
    546  1.1.1.5  christos #ifndef _WIN32
    547  1.1.1.5  christos       kill (resumed->inf->pid, SIGINT);
    548      1.1  christos #endif
    549      1.1  christos     }
    550      1.1  christos }
    551  1.1.1.5  christos 
    552  1.1.1.5  christos /* Pass a Ctrl-C to the inferior as-if a Ctrl-C was pressed while the
    553  1.1.1.5  christos    inferior was in the foreground.  Implementation of
    554  1.1.1.5  christos    target_pass_ctrlc for child/native targets.  */
    555  1.1.1.5  christos 
    556  1.1.1.5  christos void
    557  1.1.1.5  christos child_pass_ctrlc (struct target_ops *self)
    558  1.1.1.5  christos {
    559  1.1.1.5  christos   gdb_assert (!target_terminal::is_ours ());
    560  1.1.1.5  christos 
    561  1.1.1.5  christos #ifdef HAVE_TERMIOS_H
    562  1.1.1.5  christos   if (job_control)
    563  1.1.1.5  christos     {
    564  1.1.1.5  christos       pid_t term_pgrp = tcgetpgrp (0);
    565  1.1.1.5  christos 
    566  1.1.1.5  christos       /* If there's any inferior sharing our terminal, pass the SIGINT
    567  1.1.1.5  christos 	 to the terminal's foreground process group.  This acts just
    568  1.1.1.5  christos 	 like the user typed a ^C on the terminal while the inferior
    569  1.1.1.5  christos 	 was in the foreground.  Note that using a negative process
    570  1.1.1.5  christos 	 number in kill() is a System V-ism.  The proper BSD interface
    571  1.1.1.5  christos 	 is killpg().  However, all modern BSDs support the System V
    572  1.1.1.5  christos 	 interface too.  */
    573  1.1.1.5  christos 
    574  1.1.1.5  christos       if (term_pgrp != -1 && term_pgrp != our_terminal_info.process_group)
    575  1.1.1.5  christos 	{
    576  1.1.1.5  christos 	  kill (-term_pgrp, SIGINT);
    577  1.1.1.5  christos 	  return;
    578  1.1.1.5  christos 	}
    579  1.1.1.5  christos     }
    580  1.1.1.5  christos #endif
    581  1.1.1.5  christos 
    582  1.1.1.5  christos   /* Otherwise, pass the Ctrl-C to the first inferior that was resumed
    583  1.1.1.5  christos      in the foreground.  */
    584  1.1.1.5  christos   for (inferior *inf : all_inferiors ())
    585  1.1.1.5  christos     {
    586  1.1.1.5  christos       if (inf->terminal_state != target_terminal_state::is_ours)
    587  1.1.1.5  christos 	{
    588  1.1.1.5  christos 	  gdb_assert (inf->pid != 0);
    589  1.1.1.5  christos 
    590  1.1.1.5  christos #ifndef _WIN32
    591  1.1.1.5  christos 	  kill (inf->pid, SIGINT);
    592  1.1.1.5  christos #endif
    593  1.1.1.5  christos 	  return;
    594  1.1.1.5  christos 	}
    595  1.1.1.5  christos     }
    596  1.1.1.5  christos 
    597  1.1.1.5  christos   /* If no inferior was resumed in the foreground, then how did the
    598  1.1.1.5  christos      !is_ours assert above pass?  */
    599  1.1.1.5  christos   gdb_assert_not_reached ("no inferior resumed in the fg found");
    600  1.1.1.5  christos }
    601      1.1  christos 
    602  1.1.1.6  christos /* Per-inferior data key.  */
    603      1.1  christos static const struct inferior_key<terminal_info> inflow_inferior_data;
    604  1.1.1.6  christos 
    605      1.1  christos terminal_info::~terminal_info ()
    606  1.1.1.6  christos {
    607  1.1.1.6  christos   xfree (run_terminal);
    608      1.1  christos   xfree (ttystate);
    609      1.1  christos }
    610      1.1  christos 
    611      1.1  christos /* Get the current svr4 data.  If none is found yet, add it now.  This
    612      1.1  christos    function always returns a valid object.  */
    613      1.1  christos 
    614      1.1  christos static struct terminal_info *
    615      1.1  christos get_inflow_inferior_data (struct inferior *inf)
    616      1.1  christos {
    617      1.1  christos   struct terminal_info *info;
    618  1.1.1.6  christos 
    619      1.1  christos   info = inflow_inferior_data.get (inf);
    620  1.1.1.6  christos   if (info == NULL)
    621      1.1  christos     info = inflow_inferior_data.emplace (inf);
    622      1.1  christos 
    623      1.1  christos   return info;
    624      1.1  christos }
    625      1.1  christos 
    626      1.1  christos /* This is a "inferior_exit" observer.  Releases the TERMINAL_INFO member
    627      1.1  christos    of the inferior structure.  This field is private to inflow.c, and
    628      1.1  christos    its type is opaque to the rest of GDB.  PID is the target pid of
    629      1.1  christos    the inferior that is about to be removed from the inferior
    630      1.1  christos    list.  */
    631      1.1  christos 
    632      1.1  christos static void
    633      1.1  christos inflow_inferior_exit (struct inferior *inf)
    634  1.1.1.5  christos {
    635  1.1.1.6  christos   inf->terminal_state = target_terminal_state::is_ours;
    636      1.1  christos   inflow_inferior_data.clear (inf);
    637      1.1  christos }
    638      1.1  christos 
    639      1.1  christos void
    640      1.1  christos copy_terminal_info (struct inferior *to, struct inferior *from)
    641      1.1  christos {
    642      1.1  christos   struct terminal_info *tinfo_to, *tinfo_from;
    643      1.1  christos 
    644      1.1  christos   tinfo_to = get_inflow_inferior_data (to);
    645      1.1  christos   tinfo_from = get_inflow_inferior_data (from);
    646      1.1  christos 
    647      1.1  christos   xfree (tinfo_to->run_terminal);
    648      1.1  christos   xfree (tinfo_to->ttystate);
    649      1.1  christos 
    650      1.1  christos   *tinfo_to = *tinfo_from;
    651      1.1  christos 
    652      1.1  christos   if (tinfo_from->run_terminal)
    653      1.1  christos     tinfo_to->run_terminal
    654      1.1  christos       = xstrdup (tinfo_from->run_terminal);
    655      1.1  christos 
    656      1.1  christos   if (tinfo_from->ttystate)
    657      1.1  christos     tinfo_to->ttystate
    658  1.1.1.5  christos       = serial_copy_tty_state (stdin_serial, tinfo_from->ttystate);
    659  1.1.1.5  christos 
    660  1.1.1.5  christos   to->terminal_state = from->terminal_state;
    661  1.1.1.5  christos }
    662  1.1.1.5  christos 
    663  1.1.1.5  christos /* See terminal.h.  */
    664  1.1.1.5  christos 
    665  1.1.1.5  christos void
    666  1.1.1.5  christos swap_terminal_info (inferior *a, inferior *b)
    667  1.1.1.6  christos {
    668  1.1.1.6  christos   terminal_info *info_a = inflow_inferior_data.get (a);
    669  1.1.1.5  christos   terminal_info *info_b = inflow_inferior_data.get (b);
    670  1.1.1.6  christos 
    671  1.1.1.6  christos   inflow_inferior_data.set (a, info_b);
    672  1.1.1.5  christos   inflow_inferior_data.set (b, info_a);
    673  1.1.1.5  christos 
    674      1.1  christos   std::swap (a->terminal_state, b->terminal_state);
    675      1.1  christos }
    676  1.1.1.6  christos 
    677  1.1.1.5  christos static void
    678      1.1  christos info_terminal_command (const char *arg, int from_tty)
    679  1.1.1.5  christos {
    680      1.1  christos   target_terminal::info (arg, from_tty);
    681      1.1  christos }
    682      1.1  christos 
    683  1.1.1.2  christos void
    684      1.1  christos child_terminal_info (struct target_ops *self, const char *args, int from_tty)
    685      1.1  christos {
    686      1.1  christos   struct inferior *inf;
    687      1.1  christos   struct terminal_info *tinfo;
    688      1.1  christos 
    689      1.1  christos   if (!gdb_has_a_terminal ())
    690      1.1  christos     {
    691      1.1  christos       printf_filtered (_("This GDB does not control a terminal.\n"));
    692      1.1  christos       return;
    693      1.1  christos     }
    694  1.1.1.5  christos 
    695      1.1  christos   if (inferior_ptid == null_ptid)
    696      1.1  christos     return;
    697      1.1  christos 
    698      1.1  christos   inf = current_inferior ();
    699      1.1  christos   tinfo = get_inflow_inferior_data (inf);
    700      1.1  christos 
    701      1.1  christos   printf_filtered (_("Inferior's terminal status "
    702      1.1  christos 		     "(currently saved by GDB):\n"));
    703      1.1  christos 
    704      1.1  christos   /* First the fcntl flags.  */
    705      1.1  christos   {
    706      1.1  christos     int flags;
    707      1.1  christos 
    708      1.1  christos     flags = tinfo->tflags;
    709      1.1  christos 
    710      1.1  christos     printf_filtered ("File descriptor flags = ");
    711      1.1  christos 
    712      1.1  christos #ifndef O_ACCMODE
    713      1.1  christos #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
    714      1.1  christos #endif
    715      1.1  christos     /* (O_ACCMODE) parens are to avoid Ultrix header file bug.  */
    716      1.1  christos     switch (flags & (O_ACCMODE))
    717      1.1  christos       {
    718      1.1  christos       case O_RDONLY:
    719      1.1  christos 	printf_filtered ("O_RDONLY");
    720      1.1  christos 	break;
    721      1.1  christos       case O_WRONLY:
    722      1.1  christos 	printf_filtered ("O_WRONLY");
    723      1.1  christos 	break;
    724      1.1  christos       case O_RDWR:
    725      1.1  christos 	printf_filtered ("O_RDWR");
    726      1.1  christos 	break;
    727      1.1  christos       }
    728      1.1  christos     flags &= ~(O_ACCMODE);
    729      1.1  christos 
    730      1.1  christos #ifdef O_NONBLOCK
    731      1.1  christos     if (flags & O_NONBLOCK)
    732      1.1  christos       printf_filtered (" | O_NONBLOCK");
    733      1.1  christos     flags &= ~O_NONBLOCK;
    734      1.1  christos #endif
    735      1.1  christos 
    736      1.1  christos #if defined (O_NDELAY)
    737      1.1  christos     /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
    738      1.1  christos        print it as O_NONBLOCK, which is good cause that is what POSIX
    739      1.1  christos        has, and the flag will already be cleared by the time we get here.  */
    740      1.1  christos     if (flags & O_NDELAY)
    741      1.1  christos       printf_filtered (" | O_NDELAY");
    742      1.1  christos     flags &= ~O_NDELAY;
    743      1.1  christos #endif
    744      1.1  christos 
    745      1.1  christos     if (flags & O_APPEND)
    746      1.1  christos       printf_filtered (" | O_APPEND");
    747      1.1  christos     flags &= ~O_APPEND;
    748      1.1  christos 
    749      1.1  christos #if defined (O_BINARY)
    750      1.1  christos     if (flags & O_BINARY)
    751      1.1  christos       printf_filtered (" | O_BINARY");
    752      1.1  christos     flags &= ~O_BINARY;
    753      1.1  christos #endif
    754      1.1  christos 
    755      1.1  christos     if (flags)
    756      1.1  christos       printf_filtered (" | 0x%x", flags);
    757      1.1  christos     printf_filtered ("\n");
    758      1.1  christos   }
    759  1.1.1.5  christos 
    760      1.1  christos #ifdef HAVE_TERMIOS_H
    761      1.1  christos   printf_filtered ("Process group = %d\n", (int) tinfo->process_group);
    762      1.1  christos #endif
    763      1.1  christos 
    764      1.1  christos   serial_print_tty_state (stdin_serial, tinfo->ttystate, gdb_stdout);
    765      1.1  christos }
    766      1.1  christos 
    767      1.1  christos /* NEW_TTY_PREFORK is called before forking a new child process,
    769      1.1  christos    so we can record the state of ttys in the child to be formed.
    770      1.1  christos    TTYNAME is null if we are to share the terminal with gdb;
    771      1.1  christos    or points to a string containing the name of the desired tty.
    772      1.1  christos 
    773      1.1  christos    NEW_TTY is called in new child processes under Unix, which will
    774      1.1  christos    become debugger target processes.  This actually switches to
    775      1.1  christos    the terminal specified in the NEW_TTY_PREFORK call.  */
    776      1.1  christos 
    777      1.1  christos void
    778      1.1  christos new_tty_prefork (const char *ttyname)
    779      1.1  christos {
    780      1.1  christos   /* Save the name for later, for determining whether we and the child
    781      1.1  christos      are sharing a tty.  */
    782      1.1  christos   inferior_thisrun_terminal = ttyname;
    783      1.1  christos }
    784      1.1  christos 
    785      1.1  christos #if !defined(__GO32__) && !defined(_WIN32)
    786      1.1  christos /* If RESULT, assumed to be the return value from a system call, is
    787      1.1  christos    negative, print the error message indicated by errno and exit.
    788      1.1  christos    MSG should identify the operation that failed.  */
    789      1.1  christos static void
    790      1.1  christos check_syscall (const char *msg, int result)
    791      1.1  christos {
    792      1.1  christos   if (result < 0)
    793      1.1  christos     {
    794      1.1  christos       print_sys_errmsg (msg, errno);
    795      1.1  christos       _exit (1);
    796      1.1  christos     }
    797      1.1  christos }
    798      1.1  christos #endif
    799      1.1  christos 
    800      1.1  christos void
    801      1.1  christos new_tty (void)
    802      1.1  christos {
    803      1.1  christos   if (inferior_thisrun_terminal == 0)
    804  1.1.1.5  christos     return;
    805  1.1.1.5  christos #if !defined(__GO32__) && !defined(_WIN32)
    806      1.1  christos   int tty;
    807      1.1  christos 
    808      1.1  christos #ifdef TIOCNOTTY
    809      1.1  christos   /* Disconnect the child process from our controlling terminal.  On some
    810      1.1  christos      systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
    811      1.1  christos      ignore SIGTTOU.  */
    812      1.1  christos   tty = open ("/dev/tty", O_RDWR);
    813  1.1.1.5  christos   if (tty > 0)
    814      1.1  christos     {
    815      1.1  christos       scoped_ignore_sigttou ignore_sigttou;
    816      1.1  christos 
    817      1.1  christos       ioctl (tty, TIOCNOTTY, 0);
    818      1.1  christos       close (tty);
    819      1.1  christos     }
    820      1.1  christos #endif
    821      1.1  christos 
    822      1.1  christos   /* Now open the specified new terminal.  */
    823      1.1  christos   tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
    824      1.1  christos   check_syscall (inferior_thisrun_terminal, tty);
    825      1.1  christos 
    826      1.1  christos   /* Avoid use of dup2; doesn't exist on all systems.  */
    827      1.1  christos   if (tty != 0)
    828      1.1  christos     {
    829      1.1  christos       close (0);
    830      1.1  christos       check_syscall ("dup'ing tty into fd 0", dup (tty));
    831      1.1  christos     }
    832      1.1  christos   if (tty != 1)
    833      1.1  christos     {
    834      1.1  christos       close (1);
    835      1.1  christos       check_syscall ("dup'ing tty into fd 1", dup (tty));
    836      1.1  christos     }
    837      1.1  christos   if (tty != 2)
    838      1.1  christos     {
    839      1.1  christos       close (2);
    840      1.1  christos       check_syscall ("dup'ing tty into fd 2", dup (tty));
    841      1.1  christos     }
    842      1.1  christos 
    843      1.1  christos #ifdef TIOCSCTTY
    844      1.1  christos   /* Make tty our new controlling terminal.  */
    845      1.1  christos   if (ioctl (tty, TIOCSCTTY, 0) == -1)
    846      1.1  christos     /* Mention GDB in warning because it will appear in the inferior's
    847      1.1  christos        terminal instead of GDB's.  */
    848      1.1  christos     warning (_("GDB: Failed to set controlling terminal: %s"),
    849      1.1  christos 	     safe_strerror (errno));
    850      1.1  christos #endif
    851      1.1  christos 
    852      1.1  christos   if (tty > 2)
    853      1.1  christos     close (tty);
    854      1.1  christos #endif /* !go32 && !win32 */
    855      1.1  christos }
    856      1.1  christos 
    857      1.1  christos /* NEW_TTY_POSTFORK is called after forking a new child process, and
    858      1.1  christos    adding it to the inferior table, to store the TTYNAME being used by
    859      1.1  christos    the child, or null if it sharing the terminal with gdb.  */
    860      1.1  christos 
    861      1.1  christos void
    862      1.1  christos new_tty_postfork (void)
    863      1.1  christos {
    864      1.1  christos   /* Save the name for later, for determining whether we and the child
    865      1.1  christos      are sharing a tty.  */
    866      1.1  christos 
    867      1.1  christos   if (inferior_thisrun_terminal)
    868      1.1  christos     {
    869      1.1  christos       struct inferior *inf = current_inferior ();
    870      1.1  christos       struct terminal_info *tinfo = get_inflow_inferior_data (inf);
    871      1.1  christos 
    872      1.1  christos       tinfo->run_terminal = xstrdup (inferior_thisrun_terminal);
    873      1.1  christos     }
    874      1.1  christos 
    875      1.1  christos   inferior_thisrun_terminal = NULL;
    876      1.1  christos }
    877      1.1  christos 
    878      1.1  christos 
    879      1.1  christos /* Call set_sigint_trap when you need to pass a signal on to an attached
    881      1.1  christos    process when handling SIGINT.  */
    882      1.1  christos 
    883      1.1  christos static void
    884  1.1.1.5  christos pass_signal (int signo)
    885      1.1  christos {
    886      1.1  christos #ifndef _WIN32
    887      1.1  christos   kill (inferior_ptid.pid (), SIGINT);
    888  1.1.1.3  christos #endif
    889      1.1  christos }
    890      1.1  christos 
    891      1.1  christos static sighandler_t osig;
    892      1.1  christos static int osig_set;
    893      1.1  christos 
    894      1.1  christos void
    895      1.1  christos set_sigint_trap (void)
    896      1.1  christos {
    897      1.1  christos   struct inferior *inf = current_inferior ();
    898      1.1  christos   struct terminal_info *tinfo = get_inflow_inferior_data (inf);
    899  1.1.1.3  christos 
    900      1.1  christos   if (inf->attach_flag || tinfo->run_terminal)
    901      1.1  christos     {
    902      1.1  christos       osig = signal (SIGINT, pass_signal);
    903      1.1  christos       osig_set = 1;
    904      1.1  christos     }
    905      1.1  christos   else
    906      1.1  christos     osig_set = 0;
    907      1.1  christos }
    908      1.1  christos 
    909      1.1  christos void
    910      1.1  christos clear_sigint_trap (void)
    911      1.1  christos {
    912      1.1  christos   if (osig_set)
    913      1.1  christos     {
    914      1.1  christos       signal (SIGINT, osig);
    915      1.1  christos       osig_set = 0;
    916      1.1  christos     }
    917      1.1  christos }
    918      1.1  christos 
    919      1.1  christos 
    921      1.1  christos /* Create a new session if the inferior will run in a different tty.
    922      1.1  christos    A session is UNIX's way of grouping processes that share a controlling
    923      1.1  christos    terminal, so a new one is needed if the inferior terminal will be
    924      1.1  christos    different from GDB's.
    925      1.1  christos 
    926      1.1  christos    Returns the session id of the new session, 0 if no session was created
    927      1.1  christos    or -1 if an error occurred.  */
    928      1.1  christos pid_t
    929      1.1  christos create_tty_session (void)
    930      1.1  christos {
    931      1.1  christos #ifdef HAVE_SETSID
    932      1.1  christos   pid_t ret;
    933      1.1  christos 
    934      1.1  christos   if (!job_control || inferior_thisrun_terminal == 0)
    935      1.1  christos     return 0;
    936      1.1  christos 
    937      1.1  christos   ret = setsid ();
    938      1.1  christos   if (ret == -1)
    939      1.1  christos     warning (_("Failed to create new terminal session: setsid: %s"),
    940      1.1  christos 	     safe_strerror (errno));
    941      1.1  christos 
    942      1.1  christos   return ret;
    943      1.1  christos #else
    944      1.1  christos   return 0;
    945      1.1  christos #endif /* HAVE_SETSID */
    946      1.1  christos }
    947      1.1  christos 
    948      1.1  christos /* Get all the current tty settings (including whether we have a
    949      1.1  christos    tty at all!).  We can't do this in _initialize_inflow because
    950      1.1  christos    serial_fdopen() won't work until the serial_ops_list is
    951      1.1  christos    initialized, but we don't want to do it lazily either, so
    952      1.1  christos    that we can guarantee stdin_serial is opened if there is
    953      1.1  christos    a terminal.  */
    954      1.1  christos void
    955      1.1  christos initialize_stdin_serial (void)
    956  1.1.1.6  christos {
    957      1.1  christos   stdin_serial = serial_fdopen (0);
    958  1.1.1.6  christos }
    959      1.1  christos 
    960  1.1.1.5  christos void _initialize_inflow ();
    961      1.1  christos void
    962      1.1  christos _initialize_inflow ()
    963  1.1.1.5  christos {
    964  1.1.1.5  christos   add_info ("terminal", info_terminal_command,
    965      1.1  christos 	    _("Print inferior's saved terminal status."));
    966  1.1.1.5  christos 
    967      1.1  christos   /* OK, figure out whether we have job control.  */
    968                      have_job_control ();
    969                    
    970                      gdb::observers::inferior_exit.attach (inflow_inferior_exit);
    971                    }
    972