15796c8dcSSimon Schubert /* Low level interface to ptrace, for GDB when running under Unix. 25796c8dcSSimon Schubert Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 35796c8dcSSimon Schubert 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 4*cf7f2e2dSJohn Marino 2009, 2010 Free Software Foundation, Inc. 55796c8dcSSimon Schubert 65796c8dcSSimon Schubert This file is part of GDB. 75796c8dcSSimon Schubert 85796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 95796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 105796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 115796c8dcSSimon Schubert (at your option) any later version. 125796c8dcSSimon Schubert 135796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 145796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 155796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 165796c8dcSSimon Schubert GNU General Public License for more details. 175796c8dcSSimon Schubert 185796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 195796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 205796c8dcSSimon Schubert 215796c8dcSSimon Schubert #include "defs.h" 225796c8dcSSimon Schubert #include "frame.h" 235796c8dcSSimon Schubert #include "inferior.h" 245796c8dcSSimon Schubert #include "command.h" 255796c8dcSSimon Schubert #include "serial.h" 265796c8dcSSimon Schubert #include "terminal.h" 275796c8dcSSimon Schubert #include "target.h" 285796c8dcSSimon Schubert #include "gdbthread.h" 295796c8dcSSimon Schubert #include "observer.h" 305796c8dcSSimon Schubert 315796c8dcSSimon Schubert #include "gdb_string.h" 325796c8dcSSimon Schubert #include <signal.h> 335796c8dcSSimon Schubert #include <fcntl.h> 345796c8dcSSimon Schubert #include "gdb_select.h" 355796c8dcSSimon Schubert 365796c8dcSSimon Schubert #include "inflow.h" 375796c8dcSSimon Schubert 385796c8dcSSimon Schubert #ifdef HAVE_SYS_IOCTL_H 395796c8dcSSimon Schubert #include <sys/ioctl.h> 405796c8dcSSimon Schubert #endif 415796c8dcSSimon Schubert 425796c8dcSSimon Schubert #ifndef O_NOCTTY 435796c8dcSSimon Schubert #define O_NOCTTY 0 445796c8dcSSimon Schubert #endif 455796c8dcSSimon Schubert 465796c8dcSSimon Schubert extern void _initialize_inflow (void); 475796c8dcSSimon Schubert 485796c8dcSSimon Schubert static void pass_signal (int); 495796c8dcSSimon Schubert 505796c8dcSSimon Schubert static void terminal_ours_1 (int); 515796c8dcSSimon Schubert 525796c8dcSSimon Schubert /* Record terminal status separately for debugger and inferior. */ 535796c8dcSSimon Schubert 545796c8dcSSimon Schubert static struct serial *stdin_serial; 555796c8dcSSimon Schubert 565796c8dcSSimon Schubert /* Terminal related info we need to keep track of. Each inferior 575796c8dcSSimon Schubert holds an instance of this structure --- we save it whenever the 585796c8dcSSimon Schubert corresponding inferior stops, and restore it to the foreground 595796c8dcSSimon Schubert inferior when it resumes. */ 605796c8dcSSimon Schubert struct terminal_info 615796c8dcSSimon Schubert { 625796c8dcSSimon Schubert /* The name of the tty (from the `tty' command) that we gave to the 635796c8dcSSimon Schubert inferior when it was started. */ 645796c8dcSSimon Schubert char *run_terminal; 655796c8dcSSimon Schubert 665796c8dcSSimon Schubert /* TTY state. We save it whenever the inferior stops, and restore 675796c8dcSSimon Schubert it when it resumes. */ 685796c8dcSSimon Schubert serial_ttystate ttystate; 695796c8dcSSimon Schubert 705796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE 715796c8dcSSimon Schubert /* Process group. Saved and restored just like ttystate. */ 725796c8dcSSimon Schubert PROCESS_GROUP_TYPE process_group; 735796c8dcSSimon Schubert #endif 745796c8dcSSimon Schubert 755796c8dcSSimon Schubert /* fcntl flags. Saved and restored just like ttystate. */ 765796c8dcSSimon Schubert int tflags; 775796c8dcSSimon Schubert }; 785796c8dcSSimon Schubert 795796c8dcSSimon Schubert /* Our own tty state, which we restore every time we need to deal with 805796c8dcSSimon Schubert the terminal. This is only set once, when GDB first starts. The 815796c8dcSSimon Schubert settings of flags which readline saves and restores and 825796c8dcSSimon Schubert unimportant. */ 835796c8dcSSimon Schubert static struct terminal_info our_terminal_info; 845796c8dcSSimon Schubert 85*cf7f2e2dSJohn Marino static struct terminal_info *get_inflow_inferior_data (struct inferior *); 86*cf7f2e2dSJohn Marino 875796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE 885796c8dcSSimon Schubert 895796c8dcSSimon Schubert /* Return the process group of the current inferior. */ 905796c8dcSSimon Schubert 915796c8dcSSimon Schubert PROCESS_GROUP_TYPE 925796c8dcSSimon Schubert inferior_process_group (void) 935796c8dcSSimon Schubert { 94*cf7f2e2dSJohn Marino return get_inflow_inferior_data (current_inferior ())->process_group; 955796c8dcSSimon Schubert } 965796c8dcSSimon Schubert #endif 975796c8dcSSimon Schubert 985796c8dcSSimon Schubert /* While the inferior is running, we want SIGINT and SIGQUIT to go to the 995796c8dcSSimon Schubert inferior only. If we have job control, that takes care of it. If not, 1005796c8dcSSimon Schubert we save our handlers in these two variables and set SIGINT and SIGQUIT 1015796c8dcSSimon Schubert to SIG_IGN. */ 1025796c8dcSSimon Schubert 1035796c8dcSSimon Schubert static void (*sigint_ours) (); 1045796c8dcSSimon Schubert static void (*sigquit_ours) (); 1055796c8dcSSimon Schubert 1065796c8dcSSimon Schubert /* The name of the tty (from the `tty' command) that we're giving to 1075796c8dcSSimon Schubert the inferior when starting it up. This is only (and should only 1085796c8dcSSimon Schubert be) used as a transient global by new_tty_prefork, 1095796c8dcSSimon Schubert create_tty_session, new_tty and new_tty_postfork, all called from 1105796c8dcSSimon Schubert fork_inferior, while forking a new child. */ 1115796c8dcSSimon Schubert static const char *inferior_thisrun_terminal; 1125796c8dcSSimon Schubert 1135796c8dcSSimon Schubert /* Nonzero if our terminal settings are in effect. Zero if the 1145796c8dcSSimon Schubert inferior's settings are in effect. Ignored if !gdb_has_a_terminal 1155796c8dcSSimon Schubert (). */ 1165796c8dcSSimon Schubert 1175796c8dcSSimon Schubert int terminal_is_ours; 1185796c8dcSSimon Schubert 1195796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE 1205796c8dcSSimon Schubert static PROCESS_GROUP_TYPE 1215796c8dcSSimon Schubert gdb_getpgrp (void) 1225796c8dcSSimon Schubert { 1235796c8dcSSimon Schubert int process_group = -1; 124*cf7f2e2dSJohn Marino 1255796c8dcSSimon Schubert #ifdef HAVE_TERMIOS 1265796c8dcSSimon Schubert process_group = tcgetpgrp (0); 1275796c8dcSSimon Schubert #endif 1285796c8dcSSimon Schubert #ifdef HAVE_TERMIO 1295796c8dcSSimon Schubert process_group = getpgrp (); 1305796c8dcSSimon Schubert #endif 1315796c8dcSSimon Schubert #ifdef HAVE_SGTTY 1325796c8dcSSimon Schubert ioctl (0, TIOCGPGRP, &process_group); 1335796c8dcSSimon Schubert #endif 1345796c8dcSSimon Schubert return process_group; 1355796c8dcSSimon Schubert } 1365796c8dcSSimon Schubert #endif 1375796c8dcSSimon Schubert 1385796c8dcSSimon Schubert enum 1395796c8dcSSimon Schubert { 1405796c8dcSSimon Schubert yes, no, have_not_checked 1415796c8dcSSimon Schubert } 1425796c8dcSSimon Schubert gdb_has_a_terminal_flag = have_not_checked; 1435796c8dcSSimon Schubert 1445796c8dcSSimon Schubert /* Does GDB have a terminal (on stdin)? */ 1455796c8dcSSimon Schubert int 1465796c8dcSSimon Schubert gdb_has_a_terminal (void) 1475796c8dcSSimon Schubert { 1485796c8dcSSimon Schubert switch (gdb_has_a_terminal_flag) 1495796c8dcSSimon Schubert { 1505796c8dcSSimon Schubert case yes: 1515796c8dcSSimon Schubert return 1; 1525796c8dcSSimon Schubert case no: 1535796c8dcSSimon Schubert return 0; 1545796c8dcSSimon Schubert case have_not_checked: 1555796c8dcSSimon Schubert /* Get all the current tty settings (including whether we have a 1565796c8dcSSimon Schubert tty at all!). Can't do this in _initialize_inflow because 1575796c8dcSSimon Schubert serial_fdopen() won't work until the serial_ops_list is 1585796c8dcSSimon Schubert initialized. */ 1595796c8dcSSimon Schubert 1605796c8dcSSimon Schubert #ifdef F_GETFL 1615796c8dcSSimon Schubert our_terminal_info.tflags = fcntl (0, F_GETFL, 0); 1625796c8dcSSimon Schubert #endif 1635796c8dcSSimon Schubert 1645796c8dcSSimon Schubert gdb_has_a_terminal_flag = no; 1655796c8dcSSimon Schubert if (stdin_serial != NULL) 1665796c8dcSSimon Schubert { 1675796c8dcSSimon Schubert our_terminal_info.ttystate = serial_get_tty_state (stdin_serial); 1685796c8dcSSimon Schubert 1695796c8dcSSimon Schubert if (our_terminal_info.ttystate != NULL) 1705796c8dcSSimon Schubert { 1715796c8dcSSimon Schubert gdb_has_a_terminal_flag = yes; 1725796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE 1735796c8dcSSimon Schubert our_terminal_info.process_group = gdb_getpgrp (); 1745796c8dcSSimon Schubert #endif 1755796c8dcSSimon Schubert } 1765796c8dcSSimon Schubert } 1775796c8dcSSimon Schubert 1785796c8dcSSimon Schubert return gdb_has_a_terminal_flag == yes; 1795796c8dcSSimon Schubert default: 1805796c8dcSSimon Schubert /* "Can't happen". */ 1815796c8dcSSimon Schubert return 0; 1825796c8dcSSimon Schubert } 1835796c8dcSSimon Schubert } 1845796c8dcSSimon Schubert 1855796c8dcSSimon Schubert /* Macro for printing errors from ioctl operations */ 1865796c8dcSSimon Schubert 1875796c8dcSSimon Schubert #define OOPSY(what) \ 1885796c8dcSSimon Schubert if (result == -1) \ 1895796c8dcSSimon Schubert fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \ 1905796c8dcSSimon Schubert what, safe_strerror (errno)) 1915796c8dcSSimon Schubert 1925796c8dcSSimon Schubert static void terminal_ours_1 (int); 1935796c8dcSSimon Schubert 1945796c8dcSSimon Schubert /* Initialize the terminal settings we record for the inferior, 1955796c8dcSSimon Schubert before we actually run the inferior. */ 1965796c8dcSSimon Schubert 1975796c8dcSSimon Schubert void 1985796c8dcSSimon Schubert terminal_init_inferior_with_pgrp (int pgrp) 1995796c8dcSSimon Schubert { 2005796c8dcSSimon Schubert if (gdb_has_a_terminal ()) 2015796c8dcSSimon Schubert { 2025796c8dcSSimon Schubert struct inferior *inf = current_inferior (); 203*cf7f2e2dSJohn Marino struct terminal_info *tinfo = get_inflow_inferior_data (inf); 2045796c8dcSSimon Schubert 2055796c8dcSSimon Schubert /* We could just as well copy our_ttystate (if we felt like 2065796c8dcSSimon Schubert adding a new function serial_copy_tty_state()). */ 207*cf7f2e2dSJohn Marino xfree (tinfo->ttystate); 208*cf7f2e2dSJohn Marino tinfo->ttystate = serial_get_tty_state (stdin_serial); 2095796c8dcSSimon Schubert 2105796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE 211*cf7f2e2dSJohn Marino tinfo->process_group = pgrp; 2125796c8dcSSimon Schubert #endif 2135796c8dcSSimon Schubert 2145796c8dcSSimon Schubert /* Make sure that next time we call terminal_inferior (which will be 2155796c8dcSSimon Schubert before the program runs, as it needs to be), we install the new 2165796c8dcSSimon Schubert process group. */ 2175796c8dcSSimon Schubert terminal_is_ours = 1; 2185796c8dcSSimon Schubert } 2195796c8dcSSimon Schubert } 2205796c8dcSSimon Schubert 2215796c8dcSSimon Schubert /* Save the terminal settings again. This is necessary for the TUI 2225796c8dcSSimon Schubert when it switches to TUI or non-TUI mode; curses changes the terminal 2235796c8dcSSimon Schubert and gdb must be able to restore it correctly. */ 2245796c8dcSSimon Schubert 2255796c8dcSSimon Schubert void 2265796c8dcSSimon Schubert terminal_save_ours (void) 2275796c8dcSSimon Schubert { 2285796c8dcSSimon Schubert if (gdb_has_a_terminal ()) 2295796c8dcSSimon Schubert { 2305796c8dcSSimon Schubert /* We could just as well copy our_ttystate (if we felt like adding 2315796c8dcSSimon Schubert a new function serial_copy_tty_state). */ 2325796c8dcSSimon Schubert xfree (our_terminal_info.ttystate); 2335796c8dcSSimon Schubert our_terminal_info.ttystate = serial_get_tty_state (stdin_serial); 2345796c8dcSSimon Schubert } 2355796c8dcSSimon Schubert } 2365796c8dcSSimon Schubert 2375796c8dcSSimon Schubert void 2385796c8dcSSimon Schubert terminal_init_inferior (void) 2395796c8dcSSimon Schubert { 2405796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE 2415796c8dcSSimon Schubert /* This is for Lynx, and should be cleaned up by having Lynx be a separate 2425796c8dcSSimon Schubert debugging target with a version of target_terminal_init_inferior which 2435796c8dcSSimon Schubert passes in the process group to a generic routine which does all the work 2445796c8dcSSimon Schubert (and the non-threaded child_terminal_init_inferior can just pass in 2455796c8dcSSimon Schubert inferior_ptid to the same routine). */ 2465796c8dcSSimon Schubert /* We assume INFERIOR_PID is also the child's process group. */ 2475796c8dcSSimon Schubert terminal_init_inferior_with_pgrp (PIDGET (inferior_ptid)); 2485796c8dcSSimon Schubert #endif /* PROCESS_GROUP_TYPE */ 2495796c8dcSSimon Schubert } 2505796c8dcSSimon Schubert 2515796c8dcSSimon Schubert /* Put the inferior's terminal settings into effect. 2525796c8dcSSimon Schubert This is preparation for starting or resuming the inferior. */ 2535796c8dcSSimon Schubert 2545796c8dcSSimon Schubert void 2555796c8dcSSimon Schubert terminal_inferior (void) 2565796c8dcSSimon Schubert { 2575796c8dcSSimon Schubert struct inferior *inf; 258*cf7f2e2dSJohn Marino struct terminal_info *tinfo; 2595796c8dcSSimon Schubert 2605796c8dcSSimon Schubert if (!terminal_is_ours) 2615796c8dcSSimon Schubert return; 2625796c8dcSSimon Schubert 2635796c8dcSSimon Schubert inf = current_inferior (); 264*cf7f2e2dSJohn Marino tinfo = get_inflow_inferior_data (inf); 2655796c8dcSSimon Schubert 2665796c8dcSSimon Schubert if (gdb_has_a_terminal () 267*cf7f2e2dSJohn Marino && tinfo->ttystate != NULL 268*cf7f2e2dSJohn Marino && tinfo->run_terminal == NULL) 2695796c8dcSSimon Schubert { 2705796c8dcSSimon Schubert int result; 2715796c8dcSSimon Schubert 2725796c8dcSSimon Schubert #ifdef F_GETFL 2735796c8dcSSimon Schubert /* Is there a reason this is being done twice? It happens both 2745796c8dcSSimon Schubert places we use F_SETFL, so I'm inclined to think perhaps there 2755796c8dcSSimon Schubert is some reason, however perverse. Perhaps not though... */ 276*cf7f2e2dSJohn Marino result = fcntl (0, F_SETFL, tinfo->tflags); 277*cf7f2e2dSJohn Marino result = fcntl (0, F_SETFL, tinfo->tflags); 2785796c8dcSSimon Schubert OOPSY ("fcntl F_SETFL"); 2795796c8dcSSimon Schubert #endif 2805796c8dcSSimon Schubert 2815796c8dcSSimon Schubert /* Because we were careful to not change in or out of raw mode in 2825796c8dcSSimon Schubert terminal_ours, we will not change in our out of raw mode with 2835796c8dcSSimon Schubert this call, so we don't flush any input. */ 2845796c8dcSSimon Schubert result = serial_set_tty_state (stdin_serial, 285*cf7f2e2dSJohn Marino tinfo->ttystate); 2865796c8dcSSimon Schubert OOPSY ("setting tty state"); 2875796c8dcSSimon Schubert 2885796c8dcSSimon Schubert if (!job_control) 2895796c8dcSSimon Schubert { 2905796c8dcSSimon Schubert sigint_ours = (void (*)()) signal (SIGINT, SIG_IGN); 2915796c8dcSSimon Schubert #ifdef SIGQUIT 2925796c8dcSSimon Schubert sigquit_ours = (void (*)()) signal (SIGQUIT, SIG_IGN); 2935796c8dcSSimon Schubert #endif 2945796c8dcSSimon Schubert } 2955796c8dcSSimon Schubert 2965796c8dcSSimon Schubert /* If attach_flag is set, we don't know whether we are sharing a 2975796c8dcSSimon Schubert terminal with the inferior or not. (attaching a process 2985796c8dcSSimon Schubert without a terminal is one case where we do not; attaching a 2995796c8dcSSimon Schubert process which we ran from the same shell as GDB via `&' is 3005796c8dcSSimon Schubert one case where we do, I think (but perhaps this is not 3015796c8dcSSimon Schubert `sharing' in the sense that we need to save and restore tty 3025796c8dcSSimon Schubert state)). I don't know if there is any way to tell whether we 3035796c8dcSSimon Schubert are sharing a terminal. So what we do is to go through all 3045796c8dcSSimon Schubert the saving and restoring of the tty state, but ignore errors 3055796c8dcSSimon Schubert setting the process group, which will happen if we are not 3065796c8dcSSimon Schubert sharing a terminal). */ 3075796c8dcSSimon Schubert 3085796c8dcSSimon Schubert if (job_control) 3095796c8dcSSimon Schubert { 3105796c8dcSSimon Schubert #ifdef HAVE_TERMIOS 311*cf7f2e2dSJohn Marino result = tcsetpgrp (0, tinfo->process_group); 3125796c8dcSSimon Schubert if (!inf->attach_flag) 3135796c8dcSSimon Schubert OOPSY ("tcsetpgrp"); 3145796c8dcSSimon Schubert #endif 3155796c8dcSSimon Schubert 3165796c8dcSSimon Schubert #ifdef HAVE_SGTTY 317*cf7f2e2dSJohn Marino result = ioctl (0, TIOCSPGRP, &tinfo->process_group); 3185796c8dcSSimon Schubert if (!inf->attach_flag) 3195796c8dcSSimon Schubert OOPSY ("TIOCSPGRP"); 3205796c8dcSSimon Schubert #endif 3215796c8dcSSimon Schubert } 3225796c8dcSSimon Schubert 3235796c8dcSSimon Schubert } 3245796c8dcSSimon Schubert terminal_is_ours = 0; 3255796c8dcSSimon Schubert } 3265796c8dcSSimon Schubert 3275796c8dcSSimon Schubert /* Put some of our terminal settings into effect, 3285796c8dcSSimon Schubert enough to get proper results from our output, 3295796c8dcSSimon Schubert but do not change into or out of RAW mode 3305796c8dcSSimon Schubert so that no input is discarded. 3315796c8dcSSimon Schubert 3325796c8dcSSimon Schubert After doing this, either terminal_ours or terminal_inferior 3335796c8dcSSimon Schubert should be called to get back to a normal state of affairs. */ 3345796c8dcSSimon Schubert 3355796c8dcSSimon Schubert void 3365796c8dcSSimon Schubert terminal_ours_for_output (void) 3375796c8dcSSimon Schubert { 3385796c8dcSSimon Schubert terminal_ours_1 (1); 3395796c8dcSSimon Schubert } 3405796c8dcSSimon Schubert 3415796c8dcSSimon Schubert /* Put our terminal settings into effect. 3425796c8dcSSimon Schubert First record the inferior's terminal settings 3435796c8dcSSimon Schubert so they can be restored properly later. */ 3445796c8dcSSimon Schubert 3455796c8dcSSimon Schubert void 3465796c8dcSSimon Schubert terminal_ours (void) 3475796c8dcSSimon Schubert { 3485796c8dcSSimon Schubert terminal_ours_1 (0); 3495796c8dcSSimon Schubert } 3505796c8dcSSimon Schubert 3515796c8dcSSimon Schubert /* output_only is not used, and should not be used unless we introduce 3525796c8dcSSimon Schubert separate terminal_is_ours and terminal_is_ours_for_output 3535796c8dcSSimon Schubert flags. */ 3545796c8dcSSimon Schubert 3555796c8dcSSimon Schubert static void 3565796c8dcSSimon Schubert terminal_ours_1 (int output_only) 3575796c8dcSSimon Schubert { 3585796c8dcSSimon Schubert struct inferior *inf; 359*cf7f2e2dSJohn Marino struct terminal_info *tinfo; 3605796c8dcSSimon Schubert 3615796c8dcSSimon Schubert if (terminal_is_ours) 3625796c8dcSSimon Schubert return; 3635796c8dcSSimon Schubert 3645796c8dcSSimon Schubert terminal_is_ours = 1; 3655796c8dcSSimon Schubert 3665796c8dcSSimon Schubert /* Checking inferior->run_terminal is necessary so that 3675796c8dcSSimon Schubert if GDB is running in the background, it won't block trying 3685796c8dcSSimon Schubert to do the ioctl()'s below. Checking gdb_has_a_terminal 3695796c8dcSSimon Schubert avoids attempting all the ioctl's when running in batch. */ 3705796c8dcSSimon Schubert 3715796c8dcSSimon Schubert inf = current_inferior (); 372*cf7f2e2dSJohn Marino tinfo = get_inflow_inferior_data (inf); 3735796c8dcSSimon Schubert 374*cf7f2e2dSJohn Marino if (tinfo->run_terminal != NULL || gdb_has_a_terminal () == 0) 3755796c8dcSSimon Schubert return; 3765796c8dcSSimon Schubert 3775796c8dcSSimon Schubert { 3785796c8dcSSimon Schubert #ifdef SIGTTOU 3795796c8dcSSimon Schubert /* Ignore this signal since it will happen when we try to set the 3805796c8dcSSimon Schubert pgrp. */ 3815796c8dcSSimon Schubert void (*osigttou) () = NULL; 3825796c8dcSSimon Schubert #endif 3835796c8dcSSimon Schubert int result; 3845796c8dcSSimon Schubert 3855796c8dcSSimon Schubert #ifdef SIGTTOU 3865796c8dcSSimon Schubert if (job_control) 3875796c8dcSSimon Schubert osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN); 3885796c8dcSSimon Schubert #endif 3895796c8dcSSimon Schubert 390*cf7f2e2dSJohn Marino xfree (tinfo->ttystate); 391*cf7f2e2dSJohn Marino tinfo->ttystate = serial_get_tty_state (stdin_serial); 3925796c8dcSSimon Schubert 3935796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE 3945796c8dcSSimon Schubert if (!inf->attach_flag) 3955796c8dcSSimon Schubert /* If setpgrp failed in terminal_inferior, this would give us 3965796c8dcSSimon Schubert our process group instead of the inferior's. See 3975796c8dcSSimon Schubert terminal_inferior for details. */ 398*cf7f2e2dSJohn Marino tinfo->process_group = gdb_getpgrp (); 3995796c8dcSSimon Schubert #endif 4005796c8dcSSimon Schubert 4015796c8dcSSimon Schubert /* Here we used to set ICANON in our ttystate, but I believe this 4025796c8dcSSimon Schubert was an artifact from before when we used readline. Readline sets 4035796c8dcSSimon Schubert the tty state when it needs to. 4045796c8dcSSimon Schubert FIXME-maybe: However, query() expects non-raw mode and doesn't 4055796c8dcSSimon Schubert use readline. Maybe query should use readline (on the other hand, 4065796c8dcSSimon Schubert this only matters for HAVE_SGTTY, not termio or termios, I think). */ 4075796c8dcSSimon Schubert 4085796c8dcSSimon Schubert /* Set tty state to our_ttystate. We don't change in our out of raw 4095796c8dcSSimon Schubert mode, to avoid flushing input. We need to do the same thing 4105796c8dcSSimon Schubert regardless of output_only, because we don't have separate 4115796c8dcSSimon Schubert terminal_is_ours and terminal_is_ours_for_output flags. It's OK, 4125796c8dcSSimon Schubert though, since readline will deal with raw mode when/if it needs to. 4135796c8dcSSimon Schubert */ 4145796c8dcSSimon Schubert 4155796c8dcSSimon Schubert serial_noflush_set_tty_state (stdin_serial, our_terminal_info.ttystate, 416*cf7f2e2dSJohn Marino tinfo->ttystate); 4175796c8dcSSimon Schubert 4185796c8dcSSimon Schubert if (job_control) 4195796c8dcSSimon Schubert { 4205796c8dcSSimon Schubert #ifdef HAVE_TERMIOS 4215796c8dcSSimon Schubert result = tcsetpgrp (0, our_terminal_info.process_group); 4225796c8dcSSimon Schubert #if 0 4235796c8dcSSimon Schubert /* This fails on Ultrix with EINVAL if you run the testsuite 4245796c8dcSSimon Schubert in the background with nohup, and then log out. GDB never 4255796c8dcSSimon Schubert used to check for an error here, so perhaps there are other 4265796c8dcSSimon Schubert such situations as well. */ 4275796c8dcSSimon Schubert if (result == -1) 4285796c8dcSSimon Schubert fprintf_unfiltered (gdb_stderr, "[tcsetpgrp failed in terminal_ours: %s]\n", 4295796c8dcSSimon Schubert safe_strerror (errno)); 4305796c8dcSSimon Schubert #endif 4315796c8dcSSimon Schubert #endif /* termios */ 4325796c8dcSSimon Schubert 4335796c8dcSSimon Schubert #ifdef HAVE_SGTTY 4345796c8dcSSimon Schubert result = ioctl (0, TIOCSPGRP, &our_terminal_info.process_group); 4355796c8dcSSimon Schubert #endif 4365796c8dcSSimon Schubert } 4375796c8dcSSimon Schubert 4385796c8dcSSimon Schubert #ifdef SIGTTOU 4395796c8dcSSimon Schubert if (job_control) 4405796c8dcSSimon Schubert signal (SIGTTOU, osigttou); 4415796c8dcSSimon Schubert #endif 4425796c8dcSSimon Schubert 4435796c8dcSSimon Schubert if (!job_control) 4445796c8dcSSimon Schubert { 4455796c8dcSSimon Schubert signal (SIGINT, sigint_ours); 4465796c8dcSSimon Schubert #ifdef SIGQUIT 4475796c8dcSSimon Schubert signal (SIGQUIT, sigquit_ours); 4485796c8dcSSimon Schubert #endif 4495796c8dcSSimon Schubert } 4505796c8dcSSimon Schubert 4515796c8dcSSimon Schubert #ifdef F_GETFL 452*cf7f2e2dSJohn Marino tinfo->tflags = fcntl (0, F_GETFL, 0); 4535796c8dcSSimon Schubert 4545796c8dcSSimon Schubert /* Is there a reason this is being done twice? It happens both 4555796c8dcSSimon Schubert places we use F_SETFL, so I'm inclined to think perhaps there 4565796c8dcSSimon Schubert is some reason, however perverse. Perhaps not though... */ 4575796c8dcSSimon Schubert result = fcntl (0, F_SETFL, our_terminal_info.tflags); 4585796c8dcSSimon Schubert result = fcntl (0, F_SETFL, our_terminal_info.tflags); 4595796c8dcSSimon Schubert #endif 4605796c8dcSSimon Schubert } 4615796c8dcSSimon Schubert } 4625796c8dcSSimon Schubert 463*cf7f2e2dSJohn Marino /* Per-inferior data key. */ 464*cf7f2e2dSJohn Marino static const struct inferior_data *inflow_inferior_data; 4655796c8dcSSimon Schubert 4665796c8dcSSimon Schubert static void 467*cf7f2e2dSJohn Marino inflow_inferior_data_cleanup (struct inferior *inf, void *arg) 4685796c8dcSSimon Schubert { 469*cf7f2e2dSJohn Marino struct terminal_info *info; 4705796c8dcSSimon Schubert 471*cf7f2e2dSJohn Marino info = inferior_data (inf, inflow_inferior_data); 472*cf7f2e2dSJohn Marino if (info != NULL) 473*cf7f2e2dSJohn Marino { 474*cf7f2e2dSJohn Marino xfree (info->run_terminal); 475*cf7f2e2dSJohn Marino xfree (info); 476*cf7f2e2dSJohn Marino } 477*cf7f2e2dSJohn Marino } 478*cf7f2e2dSJohn Marino 479*cf7f2e2dSJohn Marino /* Get the current svr4 data. If none is found yet, add it now. This 480*cf7f2e2dSJohn Marino function always returns a valid object. */ 481*cf7f2e2dSJohn Marino 482*cf7f2e2dSJohn Marino static struct terminal_info * 483*cf7f2e2dSJohn Marino get_inflow_inferior_data (struct inferior *inf) 484*cf7f2e2dSJohn Marino { 485*cf7f2e2dSJohn Marino struct terminal_info *info; 486*cf7f2e2dSJohn Marino 487*cf7f2e2dSJohn Marino info = inferior_data (inf, inflow_inferior_data); 488*cf7f2e2dSJohn Marino if (info == NULL) 489*cf7f2e2dSJohn Marino { 490*cf7f2e2dSJohn Marino info = XZALLOC (struct terminal_info); 491*cf7f2e2dSJohn Marino set_inferior_data (inf, inflow_inferior_data, info); 492*cf7f2e2dSJohn Marino } 493*cf7f2e2dSJohn Marino 494*cf7f2e2dSJohn Marino return info; 4955796c8dcSSimon Schubert } 4965796c8dcSSimon Schubert 4975796c8dcSSimon Schubert /* This is a "inferior_exit" observer. Releases the TERMINAL_INFO member 4985796c8dcSSimon Schubert of the inferior structure. This field is private to inflow.c, and 4995796c8dcSSimon Schubert its type is opaque to the rest of GDB. PID is the target pid of 5005796c8dcSSimon Schubert the inferior that is about to be removed from the inferior 5015796c8dcSSimon Schubert list. */ 5025796c8dcSSimon Schubert 5035796c8dcSSimon Schubert static void 504*cf7f2e2dSJohn Marino inflow_inferior_exit (struct inferior *inf) 5055796c8dcSSimon Schubert { 506*cf7f2e2dSJohn Marino struct terminal_info *info; 5075796c8dcSSimon Schubert 508*cf7f2e2dSJohn Marino info = inferior_data (inf, inflow_inferior_data); 509*cf7f2e2dSJohn Marino if (info != NULL) 510*cf7f2e2dSJohn Marino { 511*cf7f2e2dSJohn Marino xfree (info->run_terminal); 512*cf7f2e2dSJohn Marino xfree (info); 513*cf7f2e2dSJohn Marino set_inferior_data (inf, inflow_inferior_data, NULL); 514*cf7f2e2dSJohn Marino } 5155796c8dcSSimon Schubert } 5165796c8dcSSimon Schubert 5175796c8dcSSimon Schubert void 5185796c8dcSSimon Schubert copy_terminal_info (struct inferior *to, struct inferior *from) 5195796c8dcSSimon Schubert { 520*cf7f2e2dSJohn Marino struct terminal_info *tinfo_to, *tinfo_from; 521*cf7f2e2dSJohn Marino 522*cf7f2e2dSJohn Marino tinfo_to = get_inflow_inferior_data (to); 523*cf7f2e2dSJohn Marino tinfo_from = get_inflow_inferior_data (from); 524*cf7f2e2dSJohn Marino *tinfo_to = *tinfo_from; 525*cf7f2e2dSJohn Marino if (tinfo_from->run_terminal) 526*cf7f2e2dSJohn Marino tinfo_to->run_terminal 527*cf7f2e2dSJohn Marino = xstrdup (tinfo_from->run_terminal); 5285796c8dcSSimon Schubert } 5295796c8dcSSimon Schubert 5305796c8dcSSimon Schubert void 5315796c8dcSSimon Schubert term_info (char *arg, int from_tty) 5325796c8dcSSimon Schubert { 5335796c8dcSSimon Schubert target_terminal_info (arg, from_tty); 5345796c8dcSSimon Schubert } 5355796c8dcSSimon Schubert 5365796c8dcSSimon Schubert void 5375796c8dcSSimon Schubert child_terminal_info (char *args, int from_tty) 5385796c8dcSSimon Schubert { 5395796c8dcSSimon Schubert struct inferior *inf; 540*cf7f2e2dSJohn Marino struct terminal_info *tinfo; 5415796c8dcSSimon Schubert 5425796c8dcSSimon Schubert if (!gdb_has_a_terminal ()) 5435796c8dcSSimon Schubert { 5445796c8dcSSimon Schubert printf_filtered (_("This GDB does not control a terminal.\n")); 5455796c8dcSSimon Schubert return; 5465796c8dcSSimon Schubert } 5475796c8dcSSimon Schubert 5485796c8dcSSimon Schubert if (ptid_equal (inferior_ptid, null_ptid)) 5495796c8dcSSimon Schubert return; 5505796c8dcSSimon Schubert 5515796c8dcSSimon Schubert inf = current_inferior (); 552*cf7f2e2dSJohn Marino tinfo = get_inflow_inferior_data (inf); 5535796c8dcSSimon Schubert 5545796c8dcSSimon Schubert printf_filtered (_("Inferior's terminal status (currently saved by GDB):\n")); 5555796c8dcSSimon Schubert 5565796c8dcSSimon Schubert /* First the fcntl flags. */ 5575796c8dcSSimon Schubert { 5585796c8dcSSimon Schubert int flags; 5595796c8dcSSimon Schubert 560*cf7f2e2dSJohn Marino flags = tinfo->tflags; 5615796c8dcSSimon Schubert 5625796c8dcSSimon Schubert printf_filtered ("File descriptor flags = "); 5635796c8dcSSimon Schubert 5645796c8dcSSimon Schubert #ifndef O_ACCMODE 5655796c8dcSSimon Schubert #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR) 5665796c8dcSSimon Schubert #endif 5675796c8dcSSimon Schubert /* (O_ACCMODE) parens are to avoid Ultrix header file bug */ 5685796c8dcSSimon Schubert switch (flags & (O_ACCMODE)) 5695796c8dcSSimon Schubert { 5705796c8dcSSimon Schubert case O_RDONLY: 5715796c8dcSSimon Schubert printf_filtered ("O_RDONLY"); 5725796c8dcSSimon Schubert break; 5735796c8dcSSimon Schubert case O_WRONLY: 5745796c8dcSSimon Schubert printf_filtered ("O_WRONLY"); 5755796c8dcSSimon Schubert break; 5765796c8dcSSimon Schubert case O_RDWR: 5775796c8dcSSimon Schubert printf_filtered ("O_RDWR"); 5785796c8dcSSimon Schubert break; 5795796c8dcSSimon Schubert } 5805796c8dcSSimon Schubert flags &= ~(O_ACCMODE); 5815796c8dcSSimon Schubert 5825796c8dcSSimon Schubert #ifdef O_NONBLOCK 5835796c8dcSSimon Schubert if (flags & O_NONBLOCK) 5845796c8dcSSimon Schubert printf_filtered (" | O_NONBLOCK"); 5855796c8dcSSimon Schubert flags &= ~O_NONBLOCK; 5865796c8dcSSimon Schubert #endif 5875796c8dcSSimon Schubert 5885796c8dcSSimon Schubert #if defined (O_NDELAY) 5895796c8dcSSimon Schubert /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will 5905796c8dcSSimon Schubert print it as O_NONBLOCK, which is good cause that is what POSIX 5915796c8dcSSimon Schubert has, and the flag will already be cleared by the time we get here. */ 5925796c8dcSSimon Schubert if (flags & O_NDELAY) 5935796c8dcSSimon Schubert printf_filtered (" | O_NDELAY"); 5945796c8dcSSimon Schubert flags &= ~O_NDELAY; 5955796c8dcSSimon Schubert #endif 5965796c8dcSSimon Schubert 5975796c8dcSSimon Schubert if (flags & O_APPEND) 5985796c8dcSSimon Schubert printf_filtered (" | O_APPEND"); 5995796c8dcSSimon Schubert flags &= ~O_APPEND; 6005796c8dcSSimon Schubert 6015796c8dcSSimon Schubert #if defined (O_BINARY) 6025796c8dcSSimon Schubert if (flags & O_BINARY) 6035796c8dcSSimon Schubert printf_filtered (" | O_BINARY"); 6045796c8dcSSimon Schubert flags &= ~O_BINARY; 6055796c8dcSSimon Schubert #endif 6065796c8dcSSimon Schubert 6075796c8dcSSimon Schubert if (flags) 6085796c8dcSSimon Schubert printf_filtered (" | 0x%x", flags); 6095796c8dcSSimon Schubert printf_filtered ("\n"); 6105796c8dcSSimon Schubert } 6115796c8dcSSimon Schubert 6125796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE 613*cf7f2e2dSJohn Marino printf_filtered ("Process group = %d\n", (int) tinfo->process_group); 6145796c8dcSSimon Schubert #endif 6155796c8dcSSimon Schubert 616*cf7f2e2dSJohn Marino serial_print_tty_state (stdin_serial, tinfo->ttystate, gdb_stdout); 6175796c8dcSSimon Schubert } 6185796c8dcSSimon Schubert 6195796c8dcSSimon Schubert /* NEW_TTY_PREFORK is called before forking a new child process, 6205796c8dcSSimon Schubert so we can record the state of ttys in the child to be formed. 6215796c8dcSSimon Schubert TTYNAME is null if we are to share the terminal with gdb; 6225796c8dcSSimon Schubert or points to a string containing the name of the desired tty. 6235796c8dcSSimon Schubert 6245796c8dcSSimon Schubert NEW_TTY is called in new child processes under Unix, which will 6255796c8dcSSimon Schubert become debugger target processes. This actually switches to 6265796c8dcSSimon Schubert the terminal specified in the NEW_TTY_PREFORK call. */ 6275796c8dcSSimon Schubert 6285796c8dcSSimon Schubert void 6295796c8dcSSimon Schubert new_tty_prefork (const char *ttyname) 6305796c8dcSSimon Schubert { 6315796c8dcSSimon Schubert /* Save the name for later, for determining whether we and the child 6325796c8dcSSimon Schubert are sharing a tty. */ 6335796c8dcSSimon Schubert inferior_thisrun_terminal = ttyname; 6345796c8dcSSimon Schubert } 6355796c8dcSSimon Schubert 636*cf7f2e2dSJohn Marino #if !defined(__GO32__) && !defined(_WIN32) 6375796c8dcSSimon Schubert /* If RESULT, assumed to be the return value from a system call, is 6385796c8dcSSimon Schubert negative, print the error message indicated by errno and exit. 6395796c8dcSSimon Schubert MSG should identify the operation that failed. */ 6405796c8dcSSimon Schubert static void 6415796c8dcSSimon Schubert check_syscall (const char *msg, int result) 6425796c8dcSSimon Schubert { 6435796c8dcSSimon Schubert if (result < 0) 6445796c8dcSSimon Schubert { 6455796c8dcSSimon Schubert print_sys_errmsg (msg, errno); 6465796c8dcSSimon Schubert _exit (1); 6475796c8dcSSimon Schubert } 6485796c8dcSSimon Schubert } 649*cf7f2e2dSJohn Marino #endif 6505796c8dcSSimon Schubert 6515796c8dcSSimon Schubert void 6525796c8dcSSimon Schubert new_tty (void) 6535796c8dcSSimon Schubert { 6545796c8dcSSimon Schubert int tty; 6555796c8dcSSimon Schubert 6565796c8dcSSimon Schubert if (inferior_thisrun_terminal == 0) 6575796c8dcSSimon Schubert return; 6585796c8dcSSimon Schubert #if !defined(__GO32__) && !defined(_WIN32) 6595796c8dcSSimon Schubert #ifdef TIOCNOTTY 6605796c8dcSSimon Schubert /* Disconnect the child process from our controlling terminal. On some 6615796c8dcSSimon Schubert systems (SVR4 for example), this may cause a SIGTTOU, so temporarily 6625796c8dcSSimon Schubert ignore SIGTTOU. */ 6635796c8dcSSimon Schubert tty = open ("/dev/tty", O_RDWR); 6645796c8dcSSimon Schubert if (tty > 0) 6655796c8dcSSimon Schubert { 6665796c8dcSSimon Schubert void (*osigttou) (); 6675796c8dcSSimon Schubert 6685796c8dcSSimon Schubert osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN); 6695796c8dcSSimon Schubert ioctl (tty, TIOCNOTTY, 0); 6705796c8dcSSimon Schubert close (tty); 6715796c8dcSSimon Schubert signal (SIGTTOU, osigttou); 6725796c8dcSSimon Schubert } 6735796c8dcSSimon Schubert #endif 6745796c8dcSSimon Schubert 6755796c8dcSSimon Schubert /* Now open the specified new terminal. */ 6765796c8dcSSimon Schubert tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY); 6775796c8dcSSimon Schubert check_syscall (inferior_thisrun_terminal, tty); 6785796c8dcSSimon Schubert 6795796c8dcSSimon Schubert /* Avoid use of dup2; doesn't exist on all systems. */ 6805796c8dcSSimon Schubert if (tty != 0) 6815796c8dcSSimon Schubert { 6825796c8dcSSimon Schubert close (0); 6835796c8dcSSimon Schubert check_syscall ("dup'ing tty into fd 0", dup (tty)); 6845796c8dcSSimon Schubert } 6855796c8dcSSimon Schubert if (tty != 1) 6865796c8dcSSimon Schubert { 6875796c8dcSSimon Schubert close (1); 6885796c8dcSSimon Schubert check_syscall ("dup'ing tty into fd 1", dup (tty)); 6895796c8dcSSimon Schubert } 6905796c8dcSSimon Schubert if (tty != 2) 6915796c8dcSSimon Schubert { 6925796c8dcSSimon Schubert close (2); 6935796c8dcSSimon Schubert check_syscall ("dup'ing tty into fd 2", dup (tty)); 6945796c8dcSSimon Schubert } 6955796c8dcSSimon Schubert 6965796c8dcSSimon Schubert #ifdef TIOCSCTTY 6975796c8dcSSimon Schubert /* Make tty our new controlling terminal. */ 6985796c8dcSSimon Schubert if (ioctl (tty, TIOCSCTTY, 0) == -1) 6995796c8dcSSimon Schubert /* Mention GDB in warning because it will appear in the inferior's 7005796c8dcSSimon Schubert terminal instead of GDB's. */ 7015796c8dcSSimon Schubert warning ("GDB: Failed to set controlling terminal: %s", 7025796c8dcSSimon Schubert safe_strerror (errno)); 7035796c8dcSSimon Schubert #endif 7045796c8dcSSimon Schubert 7055796c8dcSSimon Schubert if (tty > 2) 7065796c8dcSSimon Schubert close (tty); 7075796c8dcSSimon Schubert #endif /* !go32 && !win32 */ 7085796c8dcSSimon Schubert } 7095796c8dcSSimon Schubert 7105796c8dcSSimon Schubert /* NEW_TTY_POSTFORK is called after forking a new child process, and 7115796c8dcSSimon Schubert adding it to the inferior table, to store the TTYNAME being used by 7125796c8dcSSimon Schubert the child, or null if it sharing the terminal with gdb. */ 7135796c8dcSSimon Schubert 7145796c8dcSSimon Schubert void 7155796c8dcSSimon Schubert new_tty_postfork (void) 7165796c8dcSSimon Schubert { 7175796c8dcSSimon Schubert /* Save the name for later, for determining whether we and the child 7185796c8dcSSimon Schubert are sharing a tty. */ 7195796c8dcSSimon Schubert 7205796c8dcSSimon Schubert if (inferior_thisrun_terminal) 721*cf7f2e2dSJohn Marino { 722*cf7f2e2dSJohn Marino struct inferior *inf = current_inferior (); 723*cf7f2e2dSJohn Marino struct terminal_info *tinfo = get_inflow_inferior_data (inf); 724*cf7f2e2dSJohn Marino 725*cf7f2e2dSJohn Marino tinfo->run_terminal = xstrdup (inferior_thisrun_terminal); 726*cf7f2e2dSJohn Marino } 7275796c8dcSSimon Schubert 7285796c8dcSSimon Schubert inferior_thisrun_terminal = NULL; 7295796c8dcSSimon Schubert } 7305796c8dcSSimon Schubert 7315796c8dcSSimon Schubert 7325796c8dcSSimon Schubert /* Call set_sigint_trap when you need to pass a signal on to an attached 7335796c8dcSSimon Schubert process when handling SIGINT */ 7345796c8dcSSimon Schubert 7355796c8dcSSimon Schubert static void 7365796c8dcSSimon Schubert pass_signal (int signo) 7375796c8dcSSimon Schubert { 7385796c8dcSSimon Schubert #ifndef _WIN32 7395796c8dcSSimon Schubert kill (PIDGET (inferior_ptid), SIGINT); 7405796c8dcSSimon Schubert #endif 7415796c8dcSSimon Schubert } 7425796c8dcSSimon Schubert 7435796c8dcSSimon Schubert static void (*osig) (); 7445796c8dcSSimon Schubert static int osig_set; 7455796c8dcSSimon Schubert 7465796c8dcSSimon Schubert void 7475796c8dcSSimon Schubert set_sigint_trap (void) 7485796c8dcSSimon Schubert { 7495796c8dcSSimon Schubert struct inferior *inf = current_inferior (); 750*cf7f2e2dSJohn Marino struct terminal_info *tinfo = get_inflow_inferior_data (inf); 751*cf7f2e2dSJohn Marino 752*cf7f2e2dSJohn Marino if (inf->attach_flag || tinfo->run_terminal) 7535796c8dcSSimon Schubert { 7545796c8dcSSimon Schubert osig = (void (*)()) signal (SIGINT, pass_signal); 7555796c8dcSSimon Schubert osig_set = 1; 7565796c8dcSSimon Schubert } 7575796c8dcSSimon Schubert else 7585796c8dcSSimon Schubert osig_set = 0; 7595796c8dcSSimon Schubert } 7605796c8dcSSimon Schubert 7615796c8dcSSimon Schubert void 7625796c8dcSSimon Schubert clear_sigint_trap (void) 7635796c8dcSSimon Schubert { 7645796c8dcSSimon Schubert if (osig_set) 7655796c8dcSSimon Schubert { 7665796c8dcSSimon Schubert signal (SIGINT, osig); 7675796c8dcSSimon Schubert osig_set = 0; 7685796c8dcSSimon Schubert } 7695796c8dcSSimon Schubert } 7705796c8dcSSimon Schubert 7715796c8dcSSimon Schubert 7725796c8dcSSimon Schubert /* Create a new session if the inferior will run in a different tty. 7735796c8dcSSimon Schubert A session is UNIX's way of grouping processes that share a controlling 7745796c8dcSSimon Schubert terminal, so a new one is needed if the inferior terminal will be 7755796c8dcSSimon Schubert different from GDB's. 7765796c8dcSSimon Schubert 7775796c8dcSSimon Schubert Returns the session id of the new session, 0 if no session was created 7785796c8dcSSimon Schubert or -1 if an error occurred. */ 7795796c8dcSSimon Schubert pid_t 7805796c8dcSSimon Schubert create_tty_session (void) 7815796c8dcSSimon Schubert { 7825796c8dcSSimon Schubert #ifdef HAVE_SETSID 7835796c8dcSSimon Schubert pid_t ret; 7845796c8dcSSimon Schubert 7855796c8dcSSimon Schubert if (!job_control || inferior_thisrun_terminal == 0) 7865796c8dcSSimon Schubert return 0; 7875796c8dcSSimon Schubert 7885796c8dcSSimon Schubert ret = setsid (); 7895796c8dcSSimon Schubert if (ret == -1) 7905796c8dcSSimon Schubert warning ("Failed to create new terminal session: setsid: %s", 7915796c8dcSSimon Schubert safe_strerror (errno)); 7925796c8dcSSimon Schubert 7935796c8dcSSimon Schubert return ret; 7945796c8dcSSimon Schubert #else 7955796c8dcSSimon Schubert return 0; 7965796c8dcSSimon Schubert #endif /* HAVE_SETSID */ 7975796c8dcSSimon Schubert } 7985796c8dcSSimon Schubert 7995796c8dcSSimon Schubert /* This is here because this is where we figure out whether we (probably) 8005796c8dcSSimon Schubert have job control. Just using job_control only does part of it because 8015796c8dcSSimon Schubert setpgid or setpgrp might not exist on a system without job control. 8025796c8dcSSimon Schubert It might be considered misplaced (on the other hand, process groups and 8035796c8dcSSimon Schubert job control are closely related to ttys). 8045796c8dcSSimon Schubert 8055796c8dcSSimon Schubert For a more clean implementation, in libiberty, put a setpgid which merely 8065796c8dcSSimon Schubert calls setpgrp and a setpgrp which does nothing (any system with job control 8075796c8dcSSimon Schubert will have one or the other). */ 8085796c8dcSSimon Schubert int 8095796c8dcSSimon Schubert gdb_setpgid (void) 8105796c8dcSSimon Schubert { 8115796c8dcSSimon Schubert int retval = 0; 8125796c8dcSSimon Schubert 8135796c8dcSSimon Schubert if (job_control) 8145796c8dcSSimon Schubert { 8155796c8dcSSimon Schubert #if defined (HAVE_TERMIOS) || defined (TIOCGPGRP) 8165796c8dcSSimon Schubert #ifdef HAVE_SETPGID 8175796c8dcSSimon Schubert /* The call setpgid (0, 0) is supposed to work and mean the same 8185796c8dcSSimon Schubert thing as this, but on Ultrix 4.2A it fails with EPERM (and 8195796c8dcSSimon Schubert setpgid (getpid (), getpid ()) succeeds). */ 8205796c8dcSSimon Schubert retval = setpgid (getpid (), getpid ()); 8215796c8dcSSimon Schubert #else 8225796c8dcSSimon Schubert #ifdef HAVE_SETPGRP 8235796c8dcSSimon Schubert #ifdef SETPGRP_VOID 8245796c8dcSSimon Schubert retval = setpgrp (); 8255796c8dcSSimon Schubert #else 8265796c8dcSSimon Schubert retval = setpgrp (getpid (), getpid ()); 8275796c8dcSSimon Schubert #endif 8285796c8dcSSimon Schubert #endif /* HAVE_SETPGRP */ 8295796c8dcSSimon Schubert #endif /* HAVE_SETPGID */ 8305796c8dcSSimon Schubert #endif /* defined (HAVE_TERMIOS) || defined (TIOCGPGRP) */ 8315796c8dcSSimon Schubert } 8325796c8dcSSimon Schubert 8335796c8dcSSimon Schubert return retval; 8345796c8dcSSimon Schubert } 8355796c8dcSSimon Schubert 8365796c8dcSSimon Schubert /* Get all the current tty settings (including whether we have a 8375796c8dcSSimon Schubert tty at all!). We can't do this in _initialize_inflow because 8385796c8dcSSimon Schubert serial_fdopen() won't work until the serial_ops_list is 8395796c8dcSSimon Schubert initialized, but we don't want to do it lazily either, so 8405796c8dcSSimon Schubert that we can guarantee stdin_serial is opened if there is 8415796c8dcSSimon Schubert a terminal. */ 8425796c8dcSSimon Schubert void 8435796c8dcSSimon Schubert initialize_stdin_serial (void) 8445796c8dcSSimon Schubert { 8455796c8dcSSimon Schubert stdin_serial = serial_fdopen (0); 8465796c8dcSSimon Schubert } 8475796c8dcSSimon Schubert 8485796c8dcSSimon Schubert void 8495796c8dcSSimon Schubert _initialize_inflow (void) 8505796c8dcSSimon Schubert { 8515796c8dcSSimon Schubert add_info ("terminal", term_info, 8525796c8dcSSimon Schubert _("Print inferior's saved terminal status.")); 8535796c8dcSSimon Schubert 8545796c8dcSSimon Schubert terminal_is_ours = 1; 8555796c8dcSSimon Schubert 8565796c8dcSSimon Schubert /* OK, figure out whether we have job control. If neither termios nor 8575796c8dcSSimon Schubert sgtty (i.e. termio or go32), leave job_control 0. */ 8585796c8dcSSimon Schubert 8595796c8dcSSimon Schubert #if defined (HAVE_TERMIOS) 8605796c8dcSSimon Schubert /* Do all systems with termios have the POSIX way of identifying job 8615796c8dcSSimon Schubert control? I hope so. */ 8625796c8dcSSimon Schubert #ifdef _POSIX_JOB_CONTROL 8635796c8dcSSimon Schubert job_control = 1; 8645796c8dcSSimon Schubert #else 8655796c8dcSSimon Schubert #ifdef _SC_JOB_CONTROL 8665796c8dcSSimon Schubert job_control = sysconf (_SC_JOB_CONTROL); 8675796c8dcSSimon Schubert #else 8685796c8dcSSimon Schubert job_control = 0; /* have to assume the worst */ 8695796c8dcSSimon Schubert #endif /* _SC_JOB_CONTROL */ 8705796c8dcSSimon Schubert #endif /* _POSIX_JOB_CONTROL */ 8715796c8dcSSimon Schubert #endif /* HAVE_TERMIOS */ 8725796c8dcSSimon Schubert 8735796c8dcSSimon Schubert #ifdef HAVE_SGTTY 8745796c8dcSSimon Schubert #ifdef TIOCGPGRP 8755796c8dcSSimon Schubert job_control = 1; 8765796c8dcSSimon Schubert #else 8775796c8dcSSimon Schubert job_control = 0; 8785796c8dcSSimon Schubert #endif /* TIOCGPGRP */ 8795796c8dcSSimon Schubert #endif /* sgtty */ 8805796c8dcSSimon Schubert 8815796c8dcSSimon Schubert observer_attach_inferior_exit (inflow_inferior_exit); 882*cf7f2e2dSJohn Marino 883*cf7f2e2dSJohn Marino inflow_inferior_data 884*cf7f2e2dSJohn Marino = register_inferior_data_with_cleanup (inflow_inferior_data_cleanup); 8855796c8dcSSimon Schubert } 886