GDB (API)
/home/stan/gdb/src/gdb/inflow.c
Go to the documentation of this file.
00001 /* Low level interface to ptrace, for GDB when running under Unix.
00002    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00003 
00004    This file is part of GDB.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 3 of the License, or
00009    (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00018 
00019 #include "defs.h"
00020 #include "frame.h"
00021 #include "inferior.h"
00022 #include "command.h"
00023 #include "serial.h"
00024 #include "terminal.h"
00025 #include "target.h"
00026 #include "gdbthread.h"
00027 #include "observer.h"
00028 
00029 #include "gdb_string.h"
00030 #include <signal.h>
00031 #include <fcntl.h>
00032 #include "gdb_select.h"
00033 
00034 #include "inflow.h"
00035 #include "gdbcmd.h"
00036 
00037 #ifdef HAVE_SYS_IOCTL_H
00038 #include <sys/ioctl.h>
00039 #endif
00040 
00041 #ifndef O_NOCTTY
00042 #define O_NOCTTY 0
00043 #endif
00044 
00045 extern void _initialize_inflow (void);
00046 
00047 static void pass_signal (int);
00048 
00049 static void terminal_ours_1 (int);
00050 
00051 /* Record terminal status separately for debugger and inferior.  */
00052 
00053 static struct serial *stdin_serial;
00054 
00055 /* Terminal related info we need to keep track of.  Each inferior
00056    holds an instance of this structure --- we save it whenever the
00057    corresponding inferior stops, and restore it to the foreground
00058    inferior when it resumes.  */
00059 struct terminal_info
00060 {
00061   /* The name of the tty (from the `tty' command) that we gave to the
00062      inferior when it was started.  */
00063   char *run_terminal;
00064 
00065   /* TTY state.  We save it whenever the inferior stops, and restore
00066      it when it resumes.  */
00067   serial_ttystate ttystate;
00068 
00069 #ifdef PROCESS_GROUP_TYPE
00070   /* Process group.  Saved and restored just like ttystate.  */
00071   PROCESS_GROUP_TYPE process_group;
00072 #endif
00073 
00074   /* fcntl flags.  Saved and restored just like ttystate.  */
00075   int tflags;
00076 };
00077 
00078 /* Our own tty state, which we restore every time we need to deal with
00079    the terminal.  This is only set once, when GDB first starts.  The
00080    settings of flags which readline saves and restores and
00081    unimportant.  */
00082 static struct terminal_info our_terminal_info;
00083 
00084 static struct terminal_info *get_inflow_inferior_data (struct inferior *);
00085 
00086 #ifdef PROCESS_GROUP_TYPE
00087 
00088 /* Return the process group of the current inferior.  */
00089 
00090 PROCESS_GROUP_TYPE
00091 inferior_process_group (void)
00092 {
00093   return get_inflow_inferior_data (current_inferior ())->process_group;
00094 }
00095 #endif
00096 
00097 /* While the inferior is running, we want SIGINT and SIGQUIT to go to the
00098    inferior only.  If we have job control, that takes care of it.  If not,
00099    we save our handlers in these two variables and set SIGINT and SIGQUIT
00100    to SIG_IGN.  */
00101 
00102 static void (*sigint_ours) ();
00103 static void (*sigquit_ours) ();
00104 
00105 /* The name of the tty (from the `tty' command) that we're giving to
00106    the inferior when starting it up.  This is only (and should only
00107    be) used as a transient global by new_tty_prefork,
00108    create_tty_session, new_tty and new_tty_postfork, all called from
00109    fork_inferior, while forking a new child.  */
00110 static const char *inferior_thisrun_terminal;
00111 
00112 /* Nonzero if our terminal settings are in effect.  Zero if the
00113    inferior's settings are in effect.  Ignored if !gdb_has_a_terminal
00114    ().  */
00115 
00116 int terminal_is_ours;
00117 
00118 #ifdef PROCESS_GROUP_TYPE
00119 static PROCESS_GROUP_TYPE
00120 gdb_getpgrp (void)
00121 {
00122   int process_group = -1;
00123 
00124 #ifdef HAVE_TERMIOS
00125   process_group = tcgetpgrp (0);
00126 #endif
00127 #ifdef HAVE_TERMIO
00128   process_group = getpgrp ();
00129 #endif
00130 #ifdef HAVE_SGTTY
00131   ioctl (0, TIOCGPGRP, &process_group);
00132 #endif
00133   return process_group;
00134 }
00135 #endif
00136 
00137 enum
00138   {
00139     yes, no, have_not_checked
00140   }
00141 gdb_has_a_terminal_flag = have_not_checked;
00142 
00143 /* The value of the "interactive-mode" setting.  */
00144 static enum auto_boolean interactive_mode = AUTO_BOOLEAN_AUTO;
00145 
00146 /* Implement the "show interactive-mode" option.  */
00147 
00148 static void
00149 show_interactive_mode (struct ui_file *file, int from_tty,
00150                        struct cmd_list_element *c,
00151                        const char *value)
00152 {
00153   if (interactive_mode == AUTO_BOOLEAN_AUTO)
00154     fprintf_filtered (file, "Debugger's interactive mode "
00155                             "is %s (currently %s).\n",
00156                       value, gdb_has_a_terminal () ? "on" : "off");
00157   else
00158     fprintf_filtered (file, "Debugger's interactive mode is %s.\n", value);
00159 }
00160 
00161 /* Does GDB have a terminal (on stdin)?  */
00162 int
00163 gdb_has_a_terminal (void)
00164 {
00165   if (interactive_mode != AUTO_BOOLEAN_AUTO)
00166     return interactive_mode == AUTO_BOOLEAN_TRUE;
00167 
00168   switch (gdb_has_a_terminal_flag)
00169     {
00170     case yes:
00171       return 1;
00172     case no:
00173       return 0;
00174     case have_not_checked:
00175       /* Get all the current tty settings (including whether we have a
00176          tty at all!).  Can't do this in _initialize_inflow because
00177          serial_fdopen() won't work until the serial_ops_list is
00178          initialized.  */
00179 
00180 #ifdef F_GETFL
00181       our_terminal_info.tflags = fcntl (0, F_GETFL, 0);
00182 #endif
00183 
00184       gdb_has_a_terminal_flag = no;
00185       if (stdin_serial != NULL)
00186         {
00187           our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);
00188 
00189           if (our_terminal_info.ttystate != NULL)
00190             {
00191               gdb_has_a_terminal_flag = yes;
00192 #ifdef PROCESS_GROUP_TYPE
00193               our_terminal_info.process_group = gdb_getpgrp ();
00194 #endif
00195             }
00196         }
00197 
00198       return gdb_has_a_terminal_flag == yes;
00199     default:
00200       /* "Can't happen".  */
00201       return 0;
00202     }
00203 }
00204 
00205 /* Macro for printing errors from ioctl operations */
00206 
00207 #define OOPSY(what)     \
00208   if (result == -1)     \
00209     fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
00210             what, safe_strerror (errno))
00211 
00212 static void terminal_ours_1 (int);
00213 
00214 /* Initialize the terminal settings we record for the inferior,
00215    before we actually run the inferior.  */
00216 
00217 void
00218 terminal_init_inferior_with_pgrp (int pgrp)
00219 {
00220   struct inferior *inf = current_inferior ();
00221   struct terminal_info *tinfo = get_inflow_inferior_data (inf);
00222 
00223 #ifdef PROCESS_GROUP_TYPE
00224   /* Store the process group even without a terminal as it is used not
00225      only to reset the tty foreground process group, but also to
00226      interrupt the inferior.  */
00227   tinfo->process_group = pgrp;
00228 #endif
00229 
00230   if (gdb_has_a_terminal ())
00231     {
00232       xfree (tinfo->ttystate);
00233       tinfo->ttystate = serial_copy_tty_state (stdin_serial,
00234                                                our_terminal_info.ttystate);
00235 
00236       /* Make sure that next time we call terminal_inferior (which will be
00237          before the program runs, as it needs to be), we install the new
00238          process group.  */
00239       terminal_is_ours = 1;
00240     }
00241 }
00242 
00243 /* Save the terminal settings again.  This is necessary for the TUI
00244    when it switches to TUI or non-TUI mode;  curses changes the terminal
00245    and gdb must be able to restore it correctly.  */
00246 
00247 void
00248 terminal_save_ours (void)
00249 {
00250   if (gdb_has_a_terminal ())
00251     {
00252       xfree (our_terminal_info.ttystate);
00253       our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);
00254     }
00255 }
00256 
00257 void
00258 terminal_init_inferior (void)
00259 {
00260 #ifdef PROCESS_GROUP_TYPE
00261   /* This is for Lynx, and should be cleaned up by having Lynx be a separate
00262      debugging target with a version of target_terminal_init_inferior which
00263      passes in the process group to a generic routine which does all the work
00264      (and the non-threaded child_terminal_init_inferior can just pass in
00265      inferior_ptid to the same routine).  */
00266   /* We assume INFERIOR_PID is also the child's process group.  */
00267   terminal_init_inferior_with_pgrp (ptid_get_pid (inferior_ptid));
00268 #endif /* PROCESS_GROUP_TYPE */
00269 }
00270 
00271 /* Put the inferior's terminal settings into effect.
00272    This is preparation for starting or resuming the inferior.  */
00273 
00274 void
00275 terminal_inferior (void)
00276 {
00277   struct inferior *inf;
00278   struct terminal_info *tinfo;
00279 
00280   if (!terminal_is_ours)
00281     return;
00282 
00283   inf = current_inferior ();
00284   tinfo = get_inflow_inferior_data (inf);
00285 
00286   if (gdb_has_a_terminal ()
00287       && tinfo->ttystate != NULL
00288       && tinfo->run_terminal == NULL)
00289     {
00290       int result;
00291 
00292 #ifdef F_GETFL
00293       /* Is there a reason this is being done twice?  It happens both
00294          places we use F_SETFL, so I'm inclined to think perhaps there
00295          is some reason, however perverse.  Perhaps not though...  */
00296       result = fcntl (0, F_SETFL, tinfo->tflags);
00297       result = fcntl (0, F_SETFL, tinfo->tflags);
00298       OOPSY ("fcntl F_SETFL");
00299 #endif
00300 
00301       /* Because we were careful to not change in or out of raw mode in
00302          terminal_ours, we will not change in our out of raw mode with
00303          this call, so we don't flush any input.  */
00304       result = serial_set_tty_state (stdin_serial,
00305                                      tinfo->ttystate);
00306       OOPSY ("setting tty state");
00307 
00308       if (!job_control)
00309         {
00310           sigint_ours = (void (*)()) signal (SIGINT, SIG_IGN);
00311 #ifdef SIGQUIT
00312           sigquit_ours = (void (*)()) signal (SIGQUIT, SIG_IGN);
00313 #endif
00314         }
00315 
00316       /* If attach_flag is set, we don't know whether we are sharing a
00317          terminal with the inferior or not.  (attaching a process
00318          without a terminal is one case where we do not; attaching a
00319          process which we ran from the same shell as GDB via `&' is
00320          one case where we do, I think (but perhaps this is not
00321          `sharing' in the sense that we need to save and restore tty
00322          state)).  I don't know if there is any way to tell whether we
00323          are sharing a terminal.  So what we do is to go through all
00324          the saving and restoring of the tty state, but ignore errors
00325          setting the process group, which will happen if we are not
00326          sharing a terminal).  */
00327 
00328       if (job_control)
00329         {
00330 #ifdef HAVE_TERMIOS
00331           result = tcsetpgrp (0, tinfo->process_group);
00332           if (!inf->attach_flag)
00333             OOPSY ("tcsetpgrp");
00334 #endif
00335 
00336 #ifdef HAVE_SGTTY
00337           result = ioctl (0, TIOCSPGRP, &tinfo->process_group);
00338           if (!inf->attach_flag)
00339             OOPSY ("TIOCSPGRP");
00340 #endif
00341         }
00342 
00343     }
00344   terminal_is_ours = 0;
00345 }
00346 
00347 /* Put some of our terminal settings into effect,
00348    enough to get proper results from our output,
00349    but do not change into or out of RAW mode
00350    so that no input is discarded.
00351 
00352    After doing this, either terminal_ours or terminal_inferior
00353    should be called to get back to a normal state of affairs.  */
00354 
00355 void
00356 terminal_ours_for_output (void)
00357 {
00358   terminal_ours_1 (1);
00359 }
00360 
00361 /* Put our terminal settings into effect.
00362    First record the inferior's terminal settings
00363    so they can be restored properly later.  */
00364 
00365 void
00366 terminal_ours (void)
00367 {
00368   terminal_ours_1 (0);
00369 }
00370 
00371 /* output_only is not used, and should not be used unless we introduce
00372    separate terminal_is_ours and terminal_is_ours_for_output
00373    flags.  */
00374 
00375 static void
00376 terminal_ours_1 (int output_only)
00377 {
00378   struct inferior *inf;
00379   struct terminal_info *tinfo;
00380 
00381   if (terminal_is_ours)
00382     return;
00383 
00384   terminal_is_ours = 1;
00385 
00386   /* Checking inferior->run_terminal is necessary so that
00387      if GDB is running in the background, it won't block trying
00388      to do the ioctl()'s below.  Checking gdb_has_a_terminal
00389      avoids attempting all the ioctl's when running in batch.  */
00390 
00391   inf = current_inferior ();
00392   tinfo = get_inflow_inferior_data (inf);
00393 
00394   if (tinfo->run_terminal != NULL || gdb_has_a_terminal () == 0)
00395     return;
00396 
00397     {
00398 #ifdef SIGTTOU
00399       /* Ignore this signal since it will happen when we try to set the
00400          pgrp.  */
00401       void (*osigttou) () = NULL;
00402 #endif
00403       int result;
00404 
00405 #ifdef SIGTTOU
00406       if (job_control)
00407         osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN);
00408 #endif
00409 
00410       xfree (tinfo->ttystate);
00411       tinfo->ttystate = serial_get_tty_state (stdin_serial);
00412 
00413 #ifdef PROCESS_GROUP_TYPE
00414       if (!inf->attach_flag)
00415         /* If setpgrp failed in terminal_inferior, this would give us
00416            our process group instead of the inferior's.  See
00417            terminal_inferior for details.  */
00418         tinfo->process_group = gdb_getpgrp ();
00419 #endif
00420 
00421       /* Here we used to set ICANON in our ttystate, but I believe this
00422          was an artifact from before when we used readline.  Readline sets
00423          the tty state when it needs to.
00424          FIXME-maybe: However, query() expects non-raw mode and doesn't
00425          use readline.  Maybe query should use readline (on the other hand,
00426          this only matters for HAVE_SGTTY, not termio or termios, I think).  */
00427 
00428       /* Set tty state to our_ttystate.  We don't change in our out of raw
00429          mode, to avoid flushing input.  We need to do the same thing
00430          regardless of output_only, because we don't have separate
00431          terminal_is_ours and terminal_is_ours_for_output flags.  It's OK,
00432          though, since readline will deal with raw mode when/if it needs
00433          to.  */
00434 
00435       serial_noflush_set_tty_state (stdin_serial, our_terminal_info.ttystate,
00436                                     tinfo->ttystate);
00437 
00438       if (job_control)
00439         {
00440 #ifdef HAVE_TERMIOS
00441           result = tcsetpgrp (0, our_terminal_info.process_group);
00442 #if 0
00443           /* This fails on Ultrix with EINVAL if you run the testsuite
00444              in the background with nohup, and then log out.  GDB never
00445              used to check for an error here, so perhaps there are other
00446              such situations as well.  */
00447           if (result == -1)
00448             fprintf_unfiltered (gdb_stderr,
00449                                 "[tcsetpgrp failed in terminal_ours: %s]\n",
00450                                 safe_strerror (errno));
00451 #endif
00452 #endif /* termios */
00453 
00454 #ifdef HAVE_SGTTY
00455           result = ioctl (0, TIOCSPGRP, &our_terminal_info.process_group);
00456 #endif
00457         }
00458 
00459 #ifdef SIGTTOU
00460       if (job_control)
00461         signal (SIGTTOU, osigttou);
00462 #endif
00463 
00464       if (!job_control)
00465         {
00466           signal (SIGINT, sigint_ours);
00467 #ifdef SIGQUIT
00468           signal (SIGQUIT, sigquit_ours);
00469 #endif
00470         }
00471 
00472 #ifdef F_GETFL
00473       tinfo->tflags = fcntl (0, F_GETFL, 0);
00474 
00475       /* Is there a reason this is being done twice?  It happens both
00476          places we use F_SETFL, so I'm inclined to think perhaps there
00477          is some reason, however perverse.  Perhaps not though...  */
00478       result = fcntl (0, F_SETFL, our_terminal_info.tflags);
00479       result = fcntl (0, F_SETFL, our_terminal_info.tflags);
00480 #endif
00481     }
00482 }
00483 
00484 /* Per-inferior data key.  */
00485 static const struct inferior_data *inflow_inferior_data;
00486 
00487 static void
00488 inflow_inferior_data_cleanup (struct inferior *inf, void *arg)
00489 {
00490   struct terminal_info *info;
00491 
00492   info = inferior_data (inf, inflow_inferior_data);
00493   if (info != NULL)
00494     {
00495       xfree (info->run_terminal);
00496       xfree (info->ttystate);
00497       xfree (info);
00498     }
00499 }
00500 
00501 /* Get the current svr4 data.  If none is found yet, add it now.  This
00502    function always returns a valid object.  */
00503 
00504 static struct terminal_info *
00505 get_inflow_inferior_data (struct inferior *inf)
00506 {
00507   struct terminal_info *info;
00508 
00509   info = inferior_data (inf, inflow_inferior_data);
00510   if (info == NULL)
00511     {
00512       info = XZALLOC (struct terminal_info);
00513       set_inferior_data (inf, inflow_inferior_data, info);
00514     }
00515 
00516   return info;
00517 }
00518 
00519 /* This is a "inferior_exit" observer.  Releases the TERMINAL_INFO member
00520    of the inferior structure.  This field is private to inflow.c, and
00521    its type is opaque to the rest of GDB.  PID is the target pid of
00522    the inferior that is about to be removed from the inferior
00523    list.  */
00524 
00525 static void
00526 inflow_inferior_exit (struct inferior *inf)
00527 {
00528   struct terminal_info *info;
00529 
00530   info = inferior_data (inf, inflow_inferior_data);
00531   if (info != NULL)
00532     {
00533       xfree (info->run_terminal);
00534       xfree (info->ttystate);
00535       xfree (info);
00536       set_inferior_data (inf, inflow_inferior_data, NULL);
00537     }
00538 }
00539 
00540 void
00541 copy_terminal_info (struct inferior *to, struct inferior *from)
00542 {
00543   struct terminal_info *tinfo_to, *tinfo_from;
00544 
00545   tinfo_to = get_inflow_inferior_data (to);
00546   tinfo_from = get_inflow_inferior_data (from);
00547 
00548   xfree (tinfo_to->run_terminal);
00549   xfree (tinfo_to->ttystate);
00550 
00551   *tinfo_to = *tinfo_from;
00552 
00553   if (tinfo_from->run_terminal)
00554     tinfo_to->run_terminal
00555       = xstrdup (tinfo_from->run_terminal);
00556 
00557   if (tinfo_from->ttystate)
00558     tinfo_to->ttystate
00559       = serial_copy_tty_state (stdin_serial, tinfo_from->ttystate);
00560 }
00561 
00562 void
00563 term_info (char *arg, int from_tty)
00564 {
00565   target_terminal_info (arg, from_tty);
00566 }
00567 
00568 void
00569 child_terminal_info (const char *args, int from_tty)
00570 {
00571   struct inferior *inf;
00572   struct terminal_info *tinfo;
00573 
00574   if (!gdb_has_a_terminal ())
00575     {
00576       printf_filtered (_("This GDB does not control a terminal.\n"));
00577       return;
00578     }
00579 
00580   if (ptid_equal (inferior_ptid, null_ptid))
00581     return;
00582 
00583   inf = current_inferior ();
00584   tinfo = get_inflow_inferior_data (inf);
00585 
00586   printf_filtered (_("Inferior's terminal status "
00587                      "(currently saved by GDB):\n"));
00588 
00589   /* First the fcntl flags.  */
00590   {
00591     int flags;
00592 
00593     flags = tinfo->tflags;
00594 
00595     printf_filtered ("File descriptor flags = ");
00596 
00597 #ifndef O_ACCMODE
00598 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
00599 #endif
00600     /* (O_ACCMODE) parens are to avoid Ultrix header file bug.  */
00601     switch (flags & (O_ACCMODE))
00602       {
00603       case O_RDONLY:
00604         printf_filtered ("O_RDONLY");
00605         break;
00606       case O_WRONLY:
00607         printf_filtered ("O_WRONLY");
00608         break;
00609       case O_RDWR:
00610         printf_filtered ("O_RDWR");
00611         break;
00612       }
00613     flags &= ~(O_ACCMODE);
00614 
00615 #ifdef O_NONBLOCK
00616     if (flags & O_NONBLOCK)
00617       printf_filtered (" | O_NONBLOCK");
00618     flags &= ~O_NONBLOCK;
00619 #endif
00620 
00621 #if defined (O_NDELAY)
00622     /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
00623        print it as O_NONBLOCK, which is good cause that is what POSIX
00624        has, and the flag will already be cleared by the time we get here.  */
00625     if (flags & O_NDELAY)
00626       printf_filtered (" | O_NDELAY");
00627     flags &= ~O_NDELAY;
00628 #endif
00629 
00630     if (flags & O_APPEND)
00631       printf_filtered (" | O_APPEND");
00632     flags &= ~O_APPEND;
00633 
00634 #if defined (O_BINARY)
00635     if (flags & O_BINARY)
00636       printf_filtered (" | O_BINARY");
00637     flags &= ~O_BINARY;
00638 #endif
00639 
00640     if (flags)
00641       printf_filtered (" | 0x%x", flags);
00642     printf_filtered ("\n");
00643   }
00644 
00645 #ifdef PROCESS_GROUP_TYPE
00646   printf_filtered ("Process group = %d\n", (int) tinfo->process_group);
00647 #endif
00648 
00649   serial_print_tty_state (stdin_serial, tinfo->ttystate, gdb_stdout);
00650 }
00651 
00652 /* NEW_TTY_PREFORK is called before forking a new child process,
00653    so we can record the state of ttys in the child to be formed.
00654    TTYNAME is null if we are to share the terminal with gdb;
00655    or points to a string containing the name of the desired tty.
00656 
00657    NEW_TTY is called in new child processes under Unix, which will
00658    become debugger target processes.  This actually switches to
00659    the terminal specified in the NEW_TTY_PREFORK call.  */
00660 
00661 void
00662 new_tty_prefork (const char *ttyname)
00663 {
00664   /* Save the name for later, for determining whether we and the child
00665      are sharing a tty.  */
00666   inferior_thisrun_terminal = ttyname;
00667 }
00668 
00669 #if !defined(__GO32__) && !defined(_WIN32)
00670 /* If RESULT, assumed to be the return value from a system call, is
00671    negative, print the error message indicated by errno and exit.
00672    MSG should identify the operation that failed.  */
00673 static void
00674 check_syscall (const char *msg, int result)
00675 {
00676   if (result < 0)
00677     {
00678       print_sys_errmsg (msg, errno);
00679       _exit (1);
00680     }
00681 }
00682 #endif
00683 
00684 void
00685 new_tty (void)
00686 {
00687   int tty;
00688 
00689   if (inferior_thisrun_terminal == 0)
00690     return;
00691 #if !defined(__GO32__) && !defined(_WIN32)
00692 #ifdef TIOCNOTTY
00693   /* Disconnect the child process from our controlling terminal.  On some
00694      systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
00695      ignore SIGTTOU.  */
00696   tty = open ("/dev/tty", O_RDWR);
00697   if (tty > 0)
00698     {
00699       void (*osigttou) ();
00700 
00701       osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN);
00702       ioctl (tty, TIOCNOTTY, 0);
00703       close (tty);
00704       signal (SIGTTOU, osigttou);
00705     }
00706 #endif
00707 
00708   /* Now open the specified new terminal.  */
00709   tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
00710   check_syscall (inferior_thisrun_terminal, tty);
00711 
00712   /* Avoid use of dup2; doesn't exist on all systems.  */
00713   if (tty != 0)
00714     {
00715       close (0);
00716       check_syscall ("dup'ing tty into fd 0", dup (tty));
00717     }
00718   if (tty != 1)
00719     {
00720       close (1);
00721       check_syscall ("dup'ing tty into fd 1", dup (tty));
00722     }
00723   if (tty != 2)
00724     {
00725       close (2);
00726       check_syscall ("dup'ing tty into fd 2", dup (tty));
00727     }
00728 
00729 #ifdef TIOCSCTTY
00730   /* Make tty our new controlling terminal.  */
00731   if (ioctl (tty, TIOCSCTTY, 0) == -1)
00732     /* Mention GDB in warning because it will appear in the inferior's
00733        terminal instead of GDB's.  */
00734     warning (_("GDB: Failed to set controlling terminal: %s"),
00735              safe_strerror (errno));
00736 #endif
00737 
00738   if (tty > 2)
00739     close (tty);
00740 #endif /* !go32 && !win32 */
00741 }
00742 
00743 /* NEW_TTY_POSTFORK is called after forking a new child process, and
00744    adding it to the inferior table, to store the TTYNAME being used by
00745    the child, or null if it sharing the terminal with gdb.  */
00746 
00747 void
00748 new_tty_postfork (void)
00749 {
00750   /* Save the name for later, for determining whether we and the child
00751      are sharing a tty.  */
00752 
00753   if (inferior_thisrun_terminal)
00754     {
00755       struct inferior *inf = current_inferior ();
00756       struct terminal_info *tinfo = get_inflow_inferior_data (inf);
00757 
00758       tinfo->run_terminal = xstrdup (inferior_thisrun_terminal);
00759     }
00760 
00761   inferior_thisrun_terminal = NULL;
00762 }
00763 
00764 
00765 /* Call set_sigint_trap when you need to pass a signal on to an attached
00766    process when handling SIGINT.  */
00767 
00768 static void
00769 pass_signal (int signo)
00770 {
00771 #ifndef _WIN32
00772   kill (ptid_get_pid (inferior_ptid), SIGINT);
00773 #endif
00774 }
00775 
00776 static void (*osig) ();
00777 static int osig_set;
00778 
00779 void
00780 set_sigint_trap (void)
00781 {
00782   struct inferior *inf = current_inferior ();
00783   struct terminal_info *tinfo = get_inflow_inferior_data (inf);
00784 
00785   if (inf->attach_flag || tinfo->run_terminal)
00786     {
00787       osig = (void (*)()) signal (SIGINT, pass_signal);
00788       osig_set = 1;
00789     }
00790   else
00791     osig_set = 0;
00792 }
00793 
00794 void
00795 clear_sigint_trap (void)
00796 {
00797   if (osig_set)
00798     {
00799       signal (SIGINT, osig);
00800       osig_set = 0;
00801     }
00802 }
00803 
00804 
00805 /* Create a new session if the inferior will run in a different tty.
00806    A session is UNIX's way of grouping processes that share a controlling
00807    terminal, so a new one is needed if the inferior terminal will be
00808    different from GDB's.
00809 
00810    Returns the session id of the new session, 0 if no session was created
00811    or -1 if an error occurred.  */
00812 pid_t
00813 create_tty_session (void)
00814 {
00815 #ifdef HAVE_SETSID
00816   pid_t ret;
00817 
00818   if (!job_control || inferior_thisrun_terminal == 0)
00819     return 0;
00820 
00821   ret = setsid ();
00822   if (ret == -1)
00823     warning (_("Failed to create new terminal session: setsid: %s"),
00824              safe_strerror (errno));
00825 
00826   return ret;
00827 #else
00828   return 0;
00829 #endif /* HAVE_SETSID */
00830 }
00831 
00832 /* This is here because this is where we figure out whether we (probably)
00833    have job control.  Just using job_control only does part of it because
00834    setpgid or setpgrp might not exist on a system without job control.
00835    It might be considered misplaced (on the other hand, process groups and
00836    job control are closely related to ttys).
00837 
00838    For a more clean implementation, in libiberty, put a setpgid which merely
00839    calls setpgrp and a setpgrp which does nothing (any system with job control
00840    will have one or the other).  */
00841 int
00842 gdb_setpgid (void)
00843 {
00844   int retval = 0;
00845 
00846   if (job_control)
00847     {
00848 #if defined (HAVE_TERMIOS) || defined (TIOCGPGRP)
00849 #ifdef HAVE_SETPGID
00850       /* The call setpgid (0, 0) is supposed to work and mean the same
00851          thing as this, but on Ultrix 4.2A it fails with EPERM (and
00852          setpgid (getpid (), getpid ()) succeeds).  */
00853       retval = setpgid (getpid (), getpid ());
00854 #else
00855 #ifdef HAVE_SETPGRP
00856 #ifdef SETPGRP_VOID 
00857       retval = setpgrp ();
00858 #else
00859       retval = setpgrp (getpid (), getpid ());
00860 #endif
00861 #endif /* HAVE_SETPGRP */
00862 #endif /* HAVE_SETPGID */
00863 #endif /* defined (HAVE_TERMIOS) || defined (TIOCGPGRP) */
00864     }
00865 
00866   return retval;
00867 }
00868 
00869 /* Get all the current tty settings (including whether we have a
00870    tty at all!).  We can't do this in _initialize_inflow because
00871    serial_fdopen() won't work until the serial_ops_list is
00872    initialized, but we don't want to do it lazily either, so
00873    that we can guarantee stdin_serial is opened if there is
00874    a terminal.  */
00875 void
00876 initialize_stdin_serial (void)
00877 {
00878   stdin_serial = serial_fdopen (0);
00879 }
00880 
00881 void
00882 _initialize_inflow (void)
00883 {
00884   add_info ("terminal", term_info,
00885             _("Print inferior's saved terminal status."));
00886 
00887   add_setshow_auto_boolean_cmd ("interactive-mode", class_support,
00888                                 &interactive_mode, _("\
00889 Set whether GDB's standard input is a terminal."), _("\
00890 Show whether GDB's standard input is a terminal."), _("\
00891 If on, GDB assumes that standard input is a terminal.  In practice, it\n\
00892 means that GDB should wait for the user to answer queries associated to\n\
00893 commands entered at the command prompt.  If off, GDB assumes that standard\n\
00894 input is not a terminal, and uses the default answer to all queries.\n\
00895 If auto (the default), determine which mode to use based on the standard\n\
00896 input settings."),
00897                         NULL,
00898                         show_interactive_mode,
00899                         &setlist, &showlist);
00900 
00901   terminal_is_ours = 1;
00902 
00903   /* OK, figure out whether we have job control.  If neither termios nor
00904      sgtty (i.e. termio or go32), leave job_control 0.  */
00905 
00906 #if defined (HAVE_TERMIOS)
00907   /* Do all systems with termios have the POSIX way of identifying job
00908      control?  I hope so.  */
00909 #ifdef _POSIX_JOB_CONTROL
00910   job_control = 1;
00911 #else
00912 #ifdef _SC_JOB_CONTROL
00913   job_control = sysconf (_SC_JOB_CONTROL);
00914 #else
00915   job_control = 0;              /* Have to assume the worst.  */
00916 #endif /* _SC_JOB_CONTROL */
00917 #endif /* _POSIX_JOB_CONTROL */
00918 #endif /* HAVE_TERMIOS */
00919 
00920 #ifdef HAVE_SGTTY
00921 #ifdef TIOCGPGRP
00922   job_control = 1;
00923 #else
00924   job_control = 0;
00925 #endif /* TIOCGPGRP */
00926 #endif /* sgtty */
00927 
00928   observer_attach_inferior_exit (inflow_inferior_exit);
00929 
00930   inflow_inferior_data
00931     = register_inferior_data_with_cleanup (NULL, inflow_inferior_data_cleanup);
00932 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines