gdb/inflow.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Low level interface to ptrace, for GDB when running under Unix.
  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "frame.h"
  16. #include "inferior.h"
  17. #include "command.h"
  18. #include "serial.h"
  19. #include "terminal.h"
  20. #include "target.h"
  21. #include "gdbthread.h"
  22. #include "observer.h"
  23. #include <signal.h>
  24. #include <fcntl.h>
  25. #include "gdb_select.h"

  26. #include "inflow.h"
  27. #include "gdbcmd.h"

  28. #ifdef HAVE_SYS_IOCTL_H
  29. #include <sys/ioctl.h>
  30. #endif

  31. #ifndef O_NOCTTY
  32. #define O_NOCTTY 0
  33. #endif

  34. extern void _initialize_inflow (void);

  35. static void pass_signal (int);

  36. static void child_terminal_ours_1 (int);

  37. /* Record terminal status separately for debugger and inferior.  */

  38. static struct serial *stdin_serial;

  39. /* Terminal related info we need to keep track of.  Each inferior
  40.    holds an instance of this structure --- we save it whenever the
  41.    corresponding inferior stops, and restore it to the foreground
  42.    inferior when it resumes.  */
  43. struct terminal_info
  44. {
  45.   /* The name of the tty (from the `tty' command) that we gave to the
  46.      inferior when it was started.  */
  47.   char *run_terminal;

  48.   /* TTY state.  We save it whenever the inferior stops, and restore
  49.      it when it resumes.  */
  50.   serial_ttystate ttystate;

  51. #ifdef PROCESS_GROUP_TYPE
  52.   /* Process group.  Saved and restored just like ttystate.  */
  53.   PROCESS_GROUP_TYPE process_group;
  54. #endif

  55.   /* fcntl flags.  Saved and restored just like ttystate.  */
  56.   int tflags;
  57. };

  58. /* Our own tty state, which we restore every time we need to deal with
  59.    the terminal.  This is only set once, when GDB first starts.  The
  60.    settings of flags which readline saves and restores and
  61.    unimportant.  */
  62. static struct terminal_info our_terminal_info;

  63. /* Snapshot of our own tty state taken during initialization of GDB.
  64.    This is used as the initial tty state given to each new inferior.  */
  65. static serial_ttystate initial_gdb_ttystate;

  66. static struct terminal_info *get_inflow_inferior_data (struct inferior *);

  67. #ifdef PROCESS_GROUP_TYPE

  68. /* Return the process group of the current inferior.  */

  69. PROCESS_GROUP_TYPE
  70. inferior_process_group (void)
  71. {
  72.   return get_inflow_inferior_data (current_inferior ())->process_group;
  73. }
  74. #endif

  75. /* While the inferior is running, we want SIGINT and SIGQUIT to go to the
  76.    inferior only.  If we have job control, that takes care of it.  If not,
  77.    we save our handlers in these two variables and set SIGINT and SIGQUIT
  78.    to SIG_IGN.  */

  79. static void (*sigint_ours) ();
  80. static void (*sigquit_ours) ();

  81. /* The name of the tty (from the `tty' command) that we're giving to
  82.    the inferior when starting it up.  This is only (and should only
  83.    be) used as a transient global by new_tty_prefork,
  84.    create_tty_session, new_tty and new_tty_postfork, all called from
  85.    fork_inferior, while forking a new child.  */
  86. static const char *inferior_thisrun_terminal;

  87. /* Nonzero if our terminal settings are in effect.  Zero if the
  88.    inferior's settings are in effect.  Ignored if !gdb_has_a_terminal
  89.    ().  */

  90. int terminal_is_ours;

  91. #ifdef PROCESS_GROUP_TYPE
  92. static PROCESS_GROUP_TYPE
  93. gdb_getpgrp (void)
  94. {
  95.   int process_group = -1;

  96. #ifdef HAVE_TERMIOS
  97.   process_group = tcgetpgrp (0);
  98. #endif
  99. #ifdef HAVE_TERMIO
  100.   process_group = getpgrp ();
  101. #endif
  102. #ifdef HAVE_SGTTY
  103.   ioctl (0, TIOCGPGRP, &process_group);
  104. #endif
  105.   return process_group;
  106. }
  107. #endif

  108. enum
  109.   {
  110.     yes, no, have_not_checked
  111.   }
  112. gdb_has_a_terminal_flag = have_not_checked;

  113. /* The value of the "interactive-mode" setting.  */
  114. static enum auto_boolean interactive_mode = AUTO_BOOLEAN_AUTO;

  115. /* Implement the "show interactive-mode" option.  */

  116. static void
  117. show_interactive_mode (struct ui_file *file, int from_tty,
  118.                        struct cmd_list_element *c,
  119.                        const char *value)
  120. {
  121.   if (interactive_mode == AUTO_BOOLEAN_AUTO)
  122.     fprintf_filtered (file, "Debugger's interactive mode "
  123.                             "is %s (currently %s).\n",
  124.                       value, gdb_has_a_terminal () ? "on" : "off");
  125.   else
  126.     fprintf_filtered (file, "Debugger's interactive mode is %s.\n", value);
  127. }

  128. /* Set the initial tty state that is to be inherited by new inferiors.  */

  129. void
  130. set_initial_gdb_ttystate (void)
  131. {
  132.   initial_gdb_ttystate = serial_get_tty_state (stdin_serial);
  133. }

  134. /* Does GDB have a terminal (on stdin)?  */
  135. int
  136. gdb_has_a_terminal (void)
  137. {
  138.   if (interactive_mode != AUTO_BOOLEAN_AUTO)
  139.     return interactive_mode == AUTO_BOOLEAN_TRUE;

  140.   switch (gdb_has_a_terminal_flag)
  141.     {
  142.     case yes:
  143.       return 1;
  144.     case no:
  145.       return 0;
  146.     case have_not_checked:
  147.       /* Get all the current tty settings (including whether we have a
  148.          tty at all!).  Can't do this in _initialize_inflow because
  149.          serial_fdopen() won't work until the serial_ops_list is
  150.          initialized.  */

  151. #ifdef F_GETFL
  152.       our_terminal_info.tflags = fcntl (0, F_GETFL, 0);
  153. #endif

  154.       gdb_has_a_terminal_flag = no;
  155.       if (stdin_serial != NULL)
  156.         {
  157.           our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);

  158.           if (our_terminal_info.ttystate != NULL)
  159.             {
  160.               gdb_has_a_terminal_flag = yes;
  161. #ifdef PROCESS_GROUP_TYPE
  162.               our_terminal_info.process_group = gdb_getpgrp ();
  163. #endif
  164.             }
  165.         }

  166.       return gdb_has_a_terminal_flag == yes;
  167.     default:
  168.       /* "Can't happen".  */
  169.       return 0;
  170.     }
  171. }

  172. /* Macro for printing errors from ioctl operations */

  173. #define        OOPSY(what)        \
  174.   if (result == -1)        \
  175.     fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
  176.             what, safe_strerror (errno))

  177. /* Initialize the terminal settings we record for the inferior,
  178.    before we actually run the inferior.  */

  179. void
  180. child_terminal_init_with_pgrp (int pgrp)
  181. {
  182.   struct inferior *inf = current_inferior ();
  183.   struct terminal_info *tinfo = get_inflow_inferior_data (inf);

  184. #ifdef PROCESS_GROUP_TYPE
  185.   /* Store the process group even without a terminal as it is used not
  186.      only to reset the tty foreground process group, but also to
  187.      interrupt the inferior.  */
  188.   tinfo->process_group = pgrp;
  189. #endif

  190.   if (gdb_has_a_terminal ())
  191.     {
  192.       xfree (tinfo->ttystate);
  193.       tinfo->ttystate = serial_copy_tty_state (stdin_serial,
  194.                                                initial_gdb_ttystate);

  195.       /* Make sure that next time we call terminal_inferior (which will be
  196.          before the program runs, as it needs to be), we install the new
  197.          process group.  */
  198.       terminal_is_ours = 1;
  199.     }
  200. }

  201. /* Save the terminal settings again.  This is necessary for the TUI
  202.    when it switches to TUI or non-TUI mode;  curses changes the terminal
  203.    and gdb must be able to restore it correctly.  */

  204. void
  205. gdb_save_tty_state (void)
  206. {
  207.   if (gdb_has_a_terminal ())
  208.     {
  209.       xfree (our_terminal_info.ttystate);
  210.       our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);
  211.     }
  212. }

  213. void
  214. child_terminal_init (struct target_ops *self)
  215. {
  216. #ifdef PROCESS_GROUP_TYPE
  217.   /* This is for Lynx, and should be cleaned up by having Lynx be a
  218.      separate debugging target with a version of target_terminal_init
  219.      which passes in the process group to a generic routine which does
  220.      all the work (and the non-threaded child_terminal_init can just
  221.      pass in inferior_ptid to the same routine).  */
  222.   /* We assume INFERIOR_PID is also the child's process group.  */
  223.   child_terminal_init_with_pgrp (ptid_get_pid (inferior_ptid));
  224. #endif /* PROCESS_GROUP_TYPE */
  225. }

  226. /* Put the inferior's terminal settings into effect.
  227.    This is preparation for starting or resuming the inferior.

  228.    N.B. Targets that want to use this with async support must build that
  229.    support on top of this (e.g., the caller still needs to remove stdin
  230.    from the event loop).  E.g., see linux_nat_terminal_inferior.  */

  231. void
  232. child_terminal_inferior (struct target_ops *self)
  233. {
  234.   struct inferior *inf;
  235.   struct terminal_info *tinfo;

  236.   if (!terminal_is_ours)
  237.     return;

  238.   inf = current_inferior ();
  239.   tinfo = get_inflow_inferior_data (inf);

  240.   if (gdb_has_a_terminal ()
  241.       && tinfo->ttystate != NULL
  242.       && tinfo->run_terminal == NULL)
  243.     {
  244.       int result;

  245. #ifdef F_GETFL
  246.       /* Is there a reason this is being done twice?  It happens both
  247.          places we use F_SETFL, so I'm inclined to think perhaps there
  248.          is some reason, however perverse.  Perhaps not though...  */
  249.       result = fcntl (0, F_SETFL, tinfo->tflags);
  250.       result = fcntl (0, F_SETFL, tinfo->tflags);
  251.       OOPSY ("fcntl F_SETFL");
  252. #endif

  253.       /* Because we were careful to not change in or out of raw mode in
  254.          terminal_ours, we will not change in our out of raw mode with
  255.          this call, so we don't flush any input.  */
  256.       result = serial_set_tty_state (stdin_serial,
  257.                                      tinfo->ttystate);
  258.       OOPSY ("setting tty state");

  259.       if (!job_control)
  260.         {
  261.           sigint_ours = (void (*)()) signal (SIGINT, SIG_IGN);
  262. #ifdef SIGQUIT
  263.           sigquit_ours = (void (*)()) signal (SIGQUIT, SIG_IGN);
  264. #endif
  265.         }

  266.       /* If attach_flag is set, we don't know whether we are sharing a
  267.          terminal with the inferior or not.  (attaching a process
  268.          without a terminal is one case where we do not; attaching a
  269.          process which we ran from the same shell as GDB via `&' is
  270.          one case where we do, I think (but perhaps this is not
  271.          `sharing' in the sense that we need to save and restore tty
  272.          state)).  I don't know if there is any way to tell whether we
  273.          are sharing a terminal.  So what we do is to go through all
  274.          the saving and restoring of the tty state, but ignore errors
  275.          setting the process group, which will happen if we are not
  276.          sharing a terminal).  */

  277.       if (job_control)
  278.         {
  279. #ifdef HAVE_TERMIOS
  280.           result = tcsetpgrp (0, tinfo->process_group);
  281.           if (!inf->attach_flag)
  282.             OOPSY ("tcsetpgrp");
  283. #endif

  284. #ifdef HAVE_SGTTY
  285.           result = ioctl (0, TIOCSPGRP, &tinfo->process_group);
  286.           if (!inf->attach_flag)
  287.             OOPSY ("TIOCSPGRP");
  288. #endif
  289.         }

  290.     }
  291.   terminal_is_ours = 0;
  292. }

  293. /* Put some of our terminal settings into effect,
  294.    enough to get proper results from our output,
  295.    but do not change into or out of RAW mode
  296.    so that no input is discarded.

  297.    After doing this, either terminal_ours or terminal_inferior
  298.    should be called to get back to a normal state of affairs.

  299.    N.B. The implementation is (currently) no different than
  300.    child_terminal_ours.  See child_terminal_ours_1.  */

  301. void
  302. child_terminal_ours_for_output (struct target_ops *self)
  303. {
  304.   child_terminal_ours_1 (1);
  305. }

  306. /* Put our terminal settings into effect.
  307.    First record the inferior's terminal settings
  308.    so they can be restored properly later.

  309.    N.B. Targets that want to use this with async support must build that
  310.    support on top of this (e.g., the caller still needs to add stdin to the
  311.    event loop).  E.g., see linux_nat_terminal_ours.  */

  312. void
  313. child_terminal_ours (struct target_ops *self)
  314. {
  315.   child_terminal_ours_1 (0);
  316. }

  317. /* output_only is not used, and should not be used unless we introduce
  318.    separate terminal_is_ours and terminal_is_ours_for_output
  319.    flags.  */

  320. static void
  321. child_terminal_ours_1 (int output_only)
  322. {
  323.   struct inferior *inf;
  324.   struct terminal_info *tinfo;

  325.   if (terminal_is_ours)
  326.     return;

  327.   terminal_is_ours = 1;

  328.   /* Checking inferior->run_terminal is necessary so that
  329.      if GDB is running in the background, it won't block trying
  330.      to do the ioctl()'s below.  Checking gdb_has_a_terminal
  331.      avoids attempting all the ioctl's when running in batch.  */

  332.   inf = current_inferior ();
  333.   tinfo = get_inflow_inferior_data (inf);

  334.   if (tinfo->run_terminal != NULL || gdb_has_a_terminal () == 0)
  335.     return;

  336.     {
  337. #ifdef SIGTTOU
  338.       /* Ignore this signal since it will happen when we try to set the
  339.          pgrp.  */
  340.       void (*osigttou) () = NULL;
  341. #endif
  342.       int result;

  343. #ifdef SIGTTOU
  344.       if (job_control)
  345.         osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN);
  346. #endif

  347.       xfree (tinfo->ttystate);
  348.       tinfo->ttystate = serial_get_tty_state (stdin_serial);

  349. #ifdef PROCESS_GROUP_TYPE
  350.       if (!inf->attach_flag)
  351.         /* If setpgrp failed in terminal_inferior, this would give us
  352.            our process group instead of the inferior's.  See
  353.            terminal_inferior for details.  */
  354.         tinfo->process_group = gdb_getpgrp ();
  355. #endif

  356.       /* Here we used to set ICANON in our ttystate, but I believe this
  357.          was an artifact from before when we used readline.  Readline sets
  358.          the tty state when it needs to.
  359.          FIXME-maybe: However, query() expects non-raw mode and doesn't
  360.          use readline.  Maybe query should use readline (on the other hand,
  361.          this only matters for HAVE_SGTTY, not termio or termios, I think).  */

  362.       /* Set tty state to our_ttystate.  We don't change in our out of raw
  363.          mode, to avoid flushing input.  We need to do the same thing
  364.          regardless of output_only, because we don't have separate
  365.          terminal_is_ours and terminal_is_ours_for_output flags.  It's OK,
  366.          though, since readline will deal with raw mode when/if it needs
  367.          to.  */

  368.       serial_noflush_set_tty_state (stdin_serial, our_terminal_info.ttystate,
  369.                                     tinfo->ttystate);

  370.       if (job_control)
  371.         {
  372. #ifdef HAVE_TERMIOS
  373.           result = tcsetpgrp (0, our_terminal_info.process_group);
  374. #if 0
  375.           /* This fails on Ultrix with EINVAL if you run the testsuite
  376.              in the background with nohup, and then log out.  GDB never
  377.              used to check for an error here, so perhaps there are other
  378.              such situations as well.  */
  379.           if (result == -1)
  380.             fprintf_unfiltered (gdb_stderr,
  381.                                 "[tcsetpgrp failed in child_terminal_ours: %s]\n",
  382.                                 safe_strerror (errno));
  383. #endif
  384. #endif /* termios */

  385. #ifdef HAVE_SGTTY
  386.           result = ioctl (0, TIOCSPGRP, &our_terminal_info.process_group);
  387. #endif
  388.         }

  389. #ifdef SIGTTOU
  390.       if (job_control)
  391.         signal (SIGTTOU, osigttou);
  392. #endif

  393.       if (!job_control)
  394.         {
  395.           signal (SIGINT, sigint_ours);
  396. #ifdef SIGQUIT
  397.           signal (SIGQUIT, sigquit_ours);
  398. #endif
  399.         }

  400. #ifdef F_GETFL
  401.       tinfo->tflags = fcntl (0, F_GETFL, 0);

  402.       /* Is there a reason this is being done twice?  It happens both
  403.          places we use F_SETFL, so I'm inclined to think perhaps there
  404.          is some reason, however perverse.  Perhaps not though...  */
  405.       result = fcntl (0, F_SETFL, our_terminal_info.tflags);
  406.       result = fcntl (0, F_SETFL, our_terminal_info.tflags);
  407. #endif
  408.     }
  409. }

  410. /* Per-inferior data key.  */
  411. static const struct inferior_data *inflow_inferior_data;

  412. static void
  413. inflow_inferior_data_cleanup (struct inferior *inf, void *arg)
  414. {
  415.   struct terminal_info *info = arg;

  416.   xfree (info->run_terminal);
  417.   xfree (info->ttystate);
  418.   xfree (info);
  419. }

  420. /* Get the current svr4 data.  If none is found yet, add it now.  This
  421.    function always returns a valid object.  */

  422. static struct terminal_info *
  423. get_inflow_inferior_data (struct inferior *inf)
  424. {
  425.   struct terminal_info *info;

  426.   info = inferior_data (inf, inflow_inferior_data);
  427.   if (info == NULL)
  428.     {
  429.       info = XCNEW (struct terminal_info);
  430.       set_inferior_data (inf, inflow_inferior_data, info);
  431.     }

  432.   return info;
  433. }

  434. /* This is a "inferior_exit" observer.  Releases the TERMINAL_INFO member
  435.    of the inferior structure.  This field is private to inflow.c, and
  436.    its type is opaque to the rest of GDB.  PID is the target pid of
  437.    the inferior that is about to be removed from the inferior
  438.    list.  */

  439. static void
  440. inflow_inferior_exit (struct inferior *inf)
  441. {
  442.   struct terminal_info *info;

  443.   info = inferior_data (inf, inflow_inferior_data);
  444.   if (info != NULL)
  445.     {
  446.       xfree (info->run_terminal);
  447.       xfree (info->ttystate);
  448.       xfree (info);
  449.       set_inferior_data (inf, inflow_inferior_data, NULL);
  450.     }
  451. }

  452. void
  453. copy_terminal_info (struct inferior *to, struct inferior *from)
  454. {
  455.   struct terminal_info *tinfo_to, *tinfo_from;

  456.   tinfo_to = get_inflow_inferior_data (to);
  457.   tinfo_from = get_inflow_inferior_data (from);

  458.   xfree (tinfo_to->run_terminal);
  459.   xfree (tinfo_to->ttystate);

  460.   *tinfo_to = *tinfo_from;

  461.   if (tinfo_from->run_terminal)
  462.     tinfo_to->run_terminal
  463.       = xstrdup (tinfo_from->run_terminal);

  464.   if (tinfo_from->ttystate)
  465.     tinfo_to->ttystate
  466.       = serial_copy_tty_state (stdin_serial, tinfo_from->ttystate);
  467. }

  468. void
  469. term_info (char *arg, int from_tty)
  470. {
  471.   target_terminal_info (arg, from_tty);
  472. }

  473. void
  474. child_terminal_info (struct target_ops *self, const char *args, int from_tty)
  475. {
  476.   struct inferior *inf;
  477.   struct terminal_info *tinfo;

  478.   if (!gdb_has_a_terminal ())
  479.     {
  480.       printf_filtered (_("This GDB does not control a terminal.\n"));
  481.       return;
  482.     }

  483.   if (ptid_equal (inferior_ptid, null_ptid))
  484.     return;

  485.   inf = current_inferior ();
  486.   tinfo = get_inflow_inferior_data (inf);

  487.   printf_filtered (_("Inferior's terminal status "
  488.                      "(currently saved by GDB):\n"));

  489.   /* First the fcntl flags.  */
  490.   {
  491.     int flags;

  492.     flags = tinfo->tflags;

  493.     printf_filtered ("File descriptor flags = ");

  494. #ifndef O_ACCMODE
  495. #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
  496. #endif
  497.     /* (O_ACCMODE) parens are to avoid Ultrix header file bug.  */
  498.     switch (flags & (O_ACCMODE))
  499.       {
  500.       case O_RDONLY:
  501.         printf_filtered ("O_RDONLY");
  502.         break;
  503.       case O_WRONLY:
  504.         printf_filtered ("O_WRONLY");
  505.         break;
  506.       case O_RDWR:
  507.         printf_filtered ("O_RDWR");
  508.         break;
  509.       }
  510.     flags &= ~(O_ACCMODE);

  511. #ifdef O_NONBLOCK
  512.     if (flags & O_NONBLOCK)
  513.       printf_filtered (" | O_NONBLOCK");
  514.     flags &= ~O_NONBLOCK;
  515. #endif

  516. #if defined (O_NDELAY)
  517.     /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
  518.        print it as O_NONBLOCK, which is good cause that is what POSIX
  519.        has, and the flag will already be cleared by the time we get here.  */
  520.     if (flags & O_NDELAY)
  521.       printf_filtered (" | O_NDELAY");
  522.     flags &= ~O_NDELAY;
  523. #endif

  524.     if (flags & O_APPEND)
  525.       printf_filtered (" | O_APPEND");
  526.     flags &= ~O_APPEND;

  527. #if defined (O_BINARY)
  528.     if (flags & O_BINARY)
  529.       printf_filtered (" | O_BINARY");
  530.     flags &= ~O_BINARY;
  531. #endif

  532.     if (flags)
  533.       printf_filtered (" | 0x%x", flags);
  534.     printf_filtered ("\n");
  535.   }

  536. #ifdef PROCESS_GROUP_TYPE
  537.   printf_filtered ("Process group = %d\n", (int) tinfo->process_group);
  538. #endif

  539.   serial_print_tty_state (stdin_serial, tinfo->ttystate, gdb_stdout);
  540. }

  541. /* NEW_TTY_PREFORK is called before forking a new child process,
  542.    so we can record the state of ttys in the child to be formed.
  543.    TTYNAME is null if we are to share the terminal with gdb;
  544.    or points to a string containing the name of the desired tty.

  545.    NEW_TTY is called in new child processes under Unix, which will
  546.    become debugger target processes.  This actually switches to
  547.    the terminal specified in the NEW_TTY_PREFORK call.  */

  548. void
  549. new_tty_prefork (const char *ttyname)
  550. {
  551.   /* Save the name for later, for determining whether we and the child
  552.      are sharing a tty.  */
  553.   inferior_thisrun_terminal = ttyname;
  554. }

  555. #if !defined(__GO32__) && !defined(_WIN32)
  556. /* If RESULT, assumed to be the return value from a system call, is
  557.    negative, print the error message indicated by errno and exit.
  558.    MSG should identify the operation that failed.  */
  559. static void
  560. check_syscall (const char *msg, int result)
  561. {
  562.   if (result < 0)
  563.     {
  564.       print_sys_errmsg (msg, errno);
  565.       _exit (1);
  566.     }
  567. }
  568. #endif

  569. void
  570. new_tty (void)
  571. {
  572.   int tty;

  573.   if (inferior_thisrun_terminal == 0)
  574.     return;
  575. #if !defined(__GO32__) && !defined(_WIN32)
  576. #ifdef TIOCNOTTY
  577.   /* Disconnect the child process from our controlling terminal.  On some
  578.      systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
  579.      ignore SIGTTOU.  */
  580.   tty = open ("/dev/tty", O_RDWR);
  581.   if (tty > 0)
  582.     {
  583.       void (*osigttou) ();

  584.       osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN);
  585.       ioctl (tty, TIOCNOTTY, 0);
  586.       close (tty);
  587.       signal (SIGTTOU, osigttou);
  588.     }
  589. #endif

  590.   /* Now open the specified new terminal.  */
  591.   tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
  592.   check_syscall (inferior_thisrun_terminal, tty);

  593.   /* Avoid use of dup2; doesn't exist on all systems.  */
  594.   if (tty != 0)
  595.     {
  596.       close (0);
  597.       check_syscall ("dup'ing tty into fd 0", dup (tty));
  598.     }
  599.   if (tty != 1)
  600.     {
  601.       close (1);
  602.       check_syscall ("dup'ing tty into fd 1", dup (tty));
  603.     }
  604.   if (tty != 2)
  605.     {
  606.       close (2);
  607.       check_syscall ("dup'ing tty into fd 2", dup (tty));
  608.     }

  609. #ifdef TIOCSCTTY
  610.   /* Make tty our new controlling terminal.  */
  611.   if (ioctl (tty, TIOCSCTTY, 0) == -1)
  612.     /* Mention GDB in warning because it will appear in the inferior's
  613.        terminal instead of GDB's.  */
  614.     warning (_("GDB: Failed to set controlling terminal: %s"),
  615.              safe_strerror (errno));
  616. #endif

  617.   if (tty > 2)
  618.     close (tty);
  619. #endif /* !go32 && !win32 */
  620. }

  621. /* NEW_TTY_POSTFORK is called after forking a new child process, and
  622.    adding it to the inferior table, to store the TTYNAME being used by
  623.    the child, or null if it sharing the terminal with gdb.  */

  624. void
  625. new_tty_postfork (void)
  626. {
  627.   /* Save the name for later, for determining whether we and the child
  628.      are sharing a tty.  */

  629.   if (inferior_thisrun_terminal)
  630.     {
  631.       struct inferior *inf = current_inferior ();
  632.       struct terminal_info *tinfo = get_inflow_inferior_data (inf);

  633.       tinfo->run_terminal = xstrdup (inferior_thisrun_terminal);
  634.     }

  635.   inferior_thisrun_terminal = NULL;
  636. }


  637. /* Call set_sigint_trap when you need to pass a signal on to an attached
  638.    process when handling SIGINT.  */

  639. static void
  640. pass_signal (int signo)
  641. {
  642. #ifndef _WIN32
  643.   kill (ptid_get_pid (inferior_ptid), SIGINT);
  644. #endif
  645. }

  646. static void (*osig) ();
  647. static int osig_set;

  648. void
  649. set_sigint_trap (void)
  650. {
  651.   struct inferior *inf = current_inferior ();
  652.   struct terminal_info *tinfo = get_inflow_inferior_data (inf);

  653.   if (inf->attach_flag || tinfo->run_terminal)
  654.     {
  655.       osig = (void (*)()) signal (SIGINT, pass_signal);
  656.       osig_set = 1;
  657.     }
  658.   else
  659.     osig_set = 0;
  660. }

  661. void
  662. clear_sigint_trap (void)
  663. {
  664.   if (osig_set)
  665.     {
  666.       signal (SIGINT, osig);
  667.       osig_set = 0;
  668.     }
  669. }


  670. /* Create a new session if the inferior will run in a different tty.
  671.    A session is UNIX's way of grouping processes that share a controlling
  672.    terminal, so a new one is needed if the inferior terminal will be
  673.    different from GDB's.

  674.    Returns the session id of the new session, 0 if no session was created
  675.    or -1 if an error occurred.  */
  676. pid_t
  677. create_tty_session (void)
  678. {
  679. #ifdef HAVE_SETSID
  680.   pid_t ret;

  681.   if (!job_control || inferior_thisrun_terminal == 0)
  682.     return 0;

  683.   ret = setsid ();
  684.   if (ret == -1)
  685.     warning (_("Failed to create new terminal session: setsid: %s"),
  686.              safe_strerror (errno));

  687.   return ret;
  688. #else
  689.   return 0;
  690. #endif /* HAVE_SETSID */
  691. }

  692. /* This is here because this is where we figure out whether we (probably)
  693.    have job control.  Just using job_control only does part of it because
  694.    setpgid or setpgrp might not exist on a system without job control.
  695.    It might be considered misplaced (on the other hand, process groups and
  696.    job control are closely related to ttys).

  697.    For a more clean implementation, in libiberty, put a setpgid which merely
  698.    calls setpgrp and a setpgrp which does nothing (any system with job control
  699.    will have one or the other).  */
  700. int
  701. gdb_setpgid (void)
  702. {
  703.   int retval = 0;

  704.   if (job_control)
  705.     {
  706. #if defined (HAVE_TERMIOS) || defined (TIOCGPGRP)
  707. #ifdef HAVE_SETPGID
  708.       /* The call setpgid (0, 0) is supposed to work and mean the same
  709.          thing as this, but on Ultrix 4.2A it fails with EPERM (and
  710.          setpgid (getpid (), getpid ()) succeeds).  */
  711.       retval = setpgid (getpid (), getpid ());
  712. #else
  713. #ifdef HAVE_SETPGRP
  714. #ifdef SETPGRP_VOID
  715.       retval = setpgrp ();
  716. #else
  717.       retval = setpgrp (getpid (), getpid ());
  718. #endif
  719. #endif /* HAVE_SETPGRP */
  720. #endif /* HAVE_SETPGID */
  721. #endif /* defined (HAVE_TERMIOS) || defined (TIOCGPGRP) */
  722.     }

  723.   return retval;
  724. }

  725. /* Get all the current tty settings (including whether we have a
  726.    tty at all!).  We can't do this in _initialize_inflow because
  727.    serial_fdopen() won't work until the serial_ops_list is
  728.    initialized, but we don't want to do it lazily either, so
  729.    that we can guarantee stdin_serial is opened if there is
  730.    a terminal.  */
  731. void
  732. initialize_stdin_serial (void)
  733. {
  734.   stdin_serial = serial_fdopen (0);
  735. }

  736. void
  737. _initialize_inflow (void)
  738. {
  739.   add_info ("terminal", term_info,
  740.             _("Print inferior's saved terminal status."));

  741.   add_setshow_auto_boolean_cmd ("interactive-mode", class_support,
  742.                                 &interactive_mode, _("\
  743. Set whether GDB's standard input is a terminal."), _("\
  744. Show whether GDB's standard input is a terminal."), _("\
  745. If on, GDB assumes that standard input is a terminal.  In practice, it\n\
  746. means that GDB should wait for the user to answer queries associated to\n\
  747. commands entered at the command prompt.  If off, GDB assumes that standard\n\
  748. input is not a terminal, and uses the default answer to all queries.\n\
  749. If auto (the default), determine which mode to use based on the standard\n\
  750. input settings."),
  751.                         NULL,
  752.                         show_interactive_mode,
  753.                         &setlist, &showlist);

  754.   terminal_is_ours = 1;

  755.   /* OK, figure out whether we have job control.  If neither termios nor
  756.      sgtty (i.e. termio or go32), leave job_control 0.  */

  757. #if defined (HAVE_TERMIOS)
  758.   /* Do all systems with termios have the POSIX way of identifying job
  759.      control?  I hope so.  */
  760. #ifdef _POSIX_JOB_CONTROL
  761.   job_control = 1;
  762. #else
  763. #ifdef _SC_JOB_CONTROL
  764.   job_control = sysconf (_SC_JOB_CONTROL);
  765. #else
  766.   job_control = 0;                /* Have to assume the worst.  */
  767. #endif /* _SC_JOB_CONTROL */
  768. #endif /* _POSIX_JOB_CONTROL */
  769. #endif /* HAVE_TERMIOS */

  770. #ifdef HAVE_SGTTY
  771. #ifdef TIOCGPGRP
  772.   job_control = 1;
  773. #else
  774.   job_control = 0;
  775. #endif /* TIOCGPGRP */
  776. #endif /* sgtty */

  777.   observer_attach_inferior_exit (inflow_inferior_exit);

  778.   inflow_inferior_data
  779.     = register_inferior_data_with_cleanup (NULL, inflow_inferior_data_cleanup);
  780. }