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*c50c785cSJohn Marino 2009, 2010, 2011 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" 37*c50c785cSJohn Marino #include "gdbcmd.h" 385796c8dcSSimon Schubert 395796c8dcSSimon Schubert #ifdef HAVE_SYS_IOCTL_H 405796c8dcSSimon Schubert #include <sys/ioctl.h> 415796c8dcSSimon Schubert #endif 425796c8dcSSimon Schubert 435796c8dcSSimon Schubert #ifndef O_NOCTTY 445796c8dcSSimon Schubert #define O_NOCTTY 0 455796c8dcSSimon Schubert #endif 465796c8dcSSimon Schubert 475796c8dcSSimon Schubert extern void _initialize_inflow (void); 485796c8dcSSimon Schubert 495796c8dcSSimon Schubert static void pass_signal (int); 505796c8dcSSimon Schubert 515796c8dcSSimon Schubert static void terminal_ours_1 (int); 525796c8dcSSimon Schubert 535796c8dcSSimon Schubert /* Record terminal status separately for debugger and inferior. */ 545796c8dcSSimon Schubert 555796c8dcSSimon Schubert static struct serial *stdin_serial; 565796c8dcSSimon Schubert 575796c8dcSSimon Schubert /* Terminal related info we need to keep track of. Each inferior 585796c8dcSSimon Schubert holds an instance of this structure --- we save it whenever the 595796c8dcSSimon Schubert corresponding inferior stops, and restore it to the foreground 605796c8dcSSimon Schubert inferior when it resumes. */ 615796c8dcSSimon Schubert struct terminal_info 625796c8dcSSimon Schubert { 635796c8dcSSimon Schubert /* The name of the tty (from the `tty' command) that we gave to the 645796c8dcSSimon Schubert inferior when it was started. */ 655796c8dcSSimon Schubert char *run_terminal; 665796c8dcSSimon Schubert 675796c8dcSSimon Schubert /* TTY state. We save it whenever the inferior stops, and restore 685796c8dcSSimon Schubert it when it resumes. */ 695796c8dcSSimon Schubert serial_ttystate ttystate; 705796c8dcSSimon Schubert 715796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE 725796c8dcSSimon Schubert /* Process group. Saved and restored just like ttystate. */ 735796c8dcSSimon Schubert PROCESS_GROUP_TYPE process_group; 745796c8dcSSimon Schubert #endif 755796c8dcSSimon Schubert 765796c8dcSSimon Schubert /* fcntl flags. Saved and restored just like ttystate. */ 775796c8dcSSimon Schubert int tflags; 785796c8dcSSimon Schubert }; 795796c8dcSSimon Schubert 805796c8dcSSimon Schubert /* Our own tty state, which we restore every time we need to deal with 815796c8dcSSimon Schubert the terminal. This is only set once, when GDB first starts. The 825796c8dcSSimon Schubert settings of flags which readline saves and restores and 835796c8dcSSimon Schubert unimportant. */ 845796c8dcSSimon Schubert static struct terminal_info our_terminal_info; 855796c8dcSSimon Schubert 86cf7f2e2dSJohn Marino static struct terminal_info *get_inflow_inferior_data (struct inferior *); 87cf7f2e2dSJohn Marino 885796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE 895796c8dcSSimon Schubert 905796c8dcSSimon Schubert /* Return the process group of the current inferior. */ 915796c8dcSSimon Schubert 925796c8dcSSimon Schubert PROCESS_GROUP_TYPE 935796c8dcSSimon Schubert inferior_process_group (void) 945796c8dcSSimon Schubert { 95cf7f2e2dSJohn Marino return get_inflow_inferior_data (current_inferior ())->process_group; 965796c8dcSSimon Schubert } 975796c8dcSSimon Schubert #endif 985796c8dcSSimon Schubert 995796c8dcSSimon Schubert /* While the inferior is running, we want SIGINT and SIGQUIT to go to the 1005796c8dcSSimon Schubert inferior only. If we have job control, that takes care of it. If not, 1015796c8dcSSimon Schubert we save our handlers in these two variables and set SIGINT and SIGQUIT 1025796c8dcSSimon Schubert to SIG_IGN. */ 1035796c8dcSSimon Schubert 1045796c8dcSSimon Schubert static void (*sigint_ours) (); 1055796c8dcSSimon Schubert static void (*sigquit_ours) (); 1065796c8dcSSimon Schubert 1075796c8dcSSimon Schubert /* The name of the tty (from the `tty' command) that we're giving to 1085796c8dcSSimon Schubert the inferior when starting it up. This is only (and should only 1095796c8dcSSimon Schubert be) used as a transient global by new_tty_prefork, 1105796c8dcSSimon Schubert create_tty_session, new_tty and new_tty_postfork, all called from 1115796c8dcSSimon Schubert fork_inferior, while forking a new child. */ 1125796c8dcSSimon Schubert static const char *inferior_thisrun_terminal; 1135796c8dcSSimon Schubert 1145796c8dcSSimon Schubert /* Nonzero if our terminal settings are in effect. Zero if the 1155796c8dcSSimon Schubert inferior's settings are in effect. Ignored if !gdb_has_a_terminal 1165796c8dcSSimon Schubert (). */ 1175796c8dcSSimon Schubert 1185796c8dcSSimon Schubert int terminal_is_ours; 1195796c8dcSSimon Schubert 1205796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE 1215796c8dcSSimon Schubert static PROCESS_GROUP_TYPE 1225796c8dcSSimon Schubert gdb_getpgrp (void) 1235796c8dcSSimon Schubert { 1245796c8dcSSimon Schubert int process_group = -1; 125cf7f2e2dSJohn Marino 1265796c8dcSSimon Schubert #ifdef HAVE_TERMIOS 1275796c8dcSSimon Schubert process_group = tcgetpgrp (0); 1285796c8dcSSimon Schubert #endif 1295796c8dcSSimon Schubert #ifdef HAVE_TERMIO 1305796c8dcSSimon Schubert process_group = getpgrp (); 1315796c8dcSSimon Schubert #endif 1325796c8dcSSimon Schubert #ifdef HAVE_SGTTY 1335796c8dcSSimon Schubert ioctl (0, TIOCGPGRP, &process_group); 1345796c8dcSSimon Schubert #endif 1355796c8dcSSimon Schubert return process_group; 1365796c8dcSSimon Schubert } 1375796c8dcSSimon Schubert #endif 1385796c8dcSSimon Schubert 1395796c8dcSSimon Schubert enum 1405796c8dcSSimon Schubert { 1415796c8dcSSimon Schubert yes, no, have_not_checked 1425796c8dcSSimon Schubert } 1435796c8dcSSimon Schubert gdb_has_a_terminal_flag = have_not_checked; 1445796c8dcSSimon Schubert 145*c50c785cSJohn Marino /* The value of the "interactive-mode" setting. */ 146*c50c785cSJohn Marino static enum auto_boolean interactive_mode = AUTO_BOOLEAN_AUTO; 147*c50c785cSJohn Marino 148*c50c785cSJohn Marino /* Implement the "show interactive-mode" option. */ 149*c50c785cSJohn Marino 150*c50c785cSJohn Marino static void 151*c50c785cSJohn Marino show_interactive_mode (struct ui_file *file, int from_tty, 152*c50c785cSJohn Marino struct cmd_list_element *c, 153*c50c785cSJohn Marino const char *value) 154*c50c785cSJohn Marino { 155*c50c785cSJohn Marino if (interactive_mode == AUTO_BOOLEAN_AUTO) 156*c50c785cSJohn Marino fprintf_filtered (file, "Debugger's interactive mode " 157*c50c785cSJohn Marino "is %s (currently %s).\n", 158*c50c785cSJohn Marino value, gdb_has_a_terminal () ? "on" : "off"); 159*c50c785cSJohn Marino else 160*c50c785cSJohn Marino fprintf_filtered (file, "Debugger's interactive mode is %s.\n", value); 161*c50c785cSJohn Marino } 162*c50c785cSJohn Marino 1635796c8dcSSimon Schubert /* Does GDB have a terminal (on stdin)? */ 1645796c8dcSSimon Schubert int 1655796c8dcSSimon Schubert gdb_has_a_terminal (void) 1665796c8dcSSimon Schubert { 167*c50c785cSJohn Marino if (interactive_mode != AUTO_BOOLEAN_AUTO) 168*c50c785cSJohn Marino return interactive_mode == AUTO_BOOLEAN_TRUE; 169*c50c785cSJohn Marino 1705796c8dcSSimon Schubert switch (gdb_has_a_terminal_flag) 1715796c8dcSSimon Schubert { 1725796c8dcSSimon Schubert case yes: 1735796c8dcSSimon Schubert return 1; 1745796c8dcSSimon Schubert case no: 1755796c8dcSSimon Schubert return 0; 1765796c8dcSSimon Schubert case have_not_checked: 1775796c8dcSSimon Schubert /* Get all the current tty settings (including whether we have a 1785796c8dcSSimon Schubert tty at all!). Can't do this in _initialize_inflow because 1795796c8dcSSimon Schubert serial_fdopen() won't work until the serial_ops_list is 1805796c8dcSSimon Schubert initialized. */ 1815796c8dcSSimon Schubert 1825796c8dcSSimon Schubert #ifdef F_GETFL 1835796c8dcSSimon Schubert our_terminal_info.tflags = fcntl (0, F_GETFL, 0); 1845796c8dcSSimon Schubert #endif 1855796c8dcSSimon Schubert 1865796c8dcSSimon Schubert gdb_has_a_terminal_flag = no; 1875796c8dcSSimon Schubert if (stdin_serial != NULL) 1885796c8dcSSimon Schubert { 1895796c8dcSSimon Schubert our_terminal_info.ttystate = serial_get_tty_state (stdin_serial); 1905796c8dcSSimon Schubert 1915796c8dcSSimon Schubert if (our_terminal_info.ttystate != NULL) 1925796c8dcSSimon Schubert { 1935796c8dcSSimon Schubert gdb_has_a_terminal_flag = yes; 1945796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE 1955796c8dcSSimon Schubert our_terminal_info.process_group = gdb_getpgrp (); 1965796c8dcSSimon Schubert #endif 1975796c8dcSSimon Schubert } 1985796c8dcSSimon Schubert } 1995796c8dcSSimon Schubert 2005796c8dcSSimon Schubert return gdb_has_a_terminal_flag == yes; 2015796c8dcSSimon Schubert default: 2025796c8dcSSimon Schubert /* "Can't happen". */ 2035796c8dcSSimon Schubert return 0; 2045796c8dcSSimon Schubert } 2055796c8dcSSimon Schubert } 2065796c8dcSSimon Schubert 2075796c8dcSSimon Schubert /* Macro for printing errors from ioctl operations */ 2085796c8dcSSimon Schubert 2095796c8dcSSimon Schubert #define OOPSY(what) \ 2105796c8dcSSimon Schubert if (result == -1) \ 2115796c8dcSSimon Schubert fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \ 2125796c8dcSSimon Schubert what, safe_strerror (errno)) 2135796c8dcSSimon Schubert 2145796c8dcSSimon Schubert static void terminal_ours_1 (int); 2155796c8dcSSimon Schubert 2165796c8dcSSimon Schubert /* Initialize the terminal settings we record for the inferior, 2175796c8dcSSimon Schubert before we actually run the inferior. */ 2185796c8dcSSimon Schubert 2195796c8dcSSimon Schubert void 2205796c8dcSSimon Schubert terminal_init_inferior_with_pgrp (int pgrp) 2215796c8dcSSimon Schubert { 2225796c8dcSSimon Schubert if (gdb_has_a_terminal ()) 2235796c8dcSSimon Schubert { 2245796c8dcSSimon Schubert struct inferior *inf = current_inferior (); 225cf7f2e2dSJohn Marino struct terminal_info *tinfo = get_inflow_inferior_data (inf); 2265796c8dcSSimon Schubert 227cf7f2e2dSJohn Marino xfree (tinfo->ttystate); 228*c50c785cSJohn Marino tinfo->ttystate = serial_copy_tty_state (stdin_serial, 229*c50c785cSJohn Marino our_terminal_info.ttystate); 2305796c8dcSSimon Schubert 2315796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE 232cf7f2e2dSJohn Marino tinfo->process_group = pgrp; 2335796c8dcSSimon Schubert #endif 2345796c8dcSSimon Schubert 2355796c8dcSSimon Schubert /* Make sure that next time we call terminal_inferior (which will be 2365796c8dcSSimon Schubert before the program runs, as it needs to be), we install the new 2375796c8dcSSimon Schubert process group. */ 2385796c8dcSSimon Schubert terminal_is_ours = 1; 2395796c8dcSSimon Schubert } 2405796c8dcSSimon Schubert } 2415796c8dcSSimon Schubert 2425796c8dcSSimon Schubert /* Save the terminal settings again. This is necessary for the TUI 2435796c8dcSSimon Schubert when it switches to TUI or non-TUI mode; curses changes the terminal 2445796c8dcSSimon Schubert and gdb must be able to restore it correctly. */ 2455796c8dcSSimon Schubert 2465796c8dcSSimon Schubert void 2475796c8dcSSimon Schubert terminal_save_ours (void) 2485796c8dcSSimon Schubert { 2495796c8dcSSimon Schubert if (gdb_has_a_terminal ()) 2505796c8dcSSimon Schubert { 2515796c8dcSSimon Schubert xfree (our_terminal_info.ttystate); 2525796c8dcSSimon Schubert our_terminal_info.ttystate = serial_get_tty_state (stdin_serial); 2535796c8dcSSimon Schubert } 2545796c8dcSSimon Schubert } 2555796c8dcSSimon Schubert 2565796c8dcSSimon Schubert void 2575796c8dcSSimon Schubert terminal_init_inferior (void) 2585796c8dcSSimon Schubert { 2595796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE 2605796c8dcSSimon Schubert /* This is for Lynx, and should be cleaned up by having Lynx be a separate 2615796c8dcSSimon Schubert debugging target with a version of target_terminal_init_inferior which 2625796c8dcSSimon Schubert passes in the process group to a generic routine which does all the work 2635796c8dcSSimon Schubert (and the non-threaded child_terminal_init_inferior can just pass in 2645796c8dcSSimon Schubert inferior_ptid to the same routine). */ 2655796c8dcSSimon Schubert /* We assume INFERIOR_PID is also the child's process group. */ 2665796c8dcSSimon Schubert terminal_init_inferior_with_pgrp (PIDGET (inferior_ptid)); 2675796c8dcSSimon Schubert #endif /* PROCESS_GROUP_TYPE */ 2685796c8dcSSimon Schubert } 2695796c8dcSSimon Schubert 2705796c8dcSSimon Schubert /* Put the inferior's terminal settings into effect. 2715796c8dcSSimon Schubert This is preparation for starting or resuming the inferior. */ 2725796c8dcSSimon Schubert 2735796c8dcSSimon Schubert void 2745796c8dcSSimon Schubert terminal_inferior (void) 2755796c8dcSSimon Schubert { 2765796c8dcSSimon Schubert struct inferior *inf; 277cf7f2e2dSJohn Marino struct terminal_info *tinfo; 2785796c8dcSSimon Schubert 2795796c8dcSSimon Schubert if (!terminal_is_ours) 2805796c8dcSSimon Schubert return; 2815796c8dcSSimon Schubert 2825796c8dcSSimon Schubert inf = current_inferior (); 283cf7f2e2dSJohn Marino tinfo = get_inflow_inferior_data (inf); 2845796c8dcSSimon Schubert 2855796c8dcSSimon Schubert if (gdb_has_a_terminal () 286cf7f2e2dSJohn Marino && tinfo->ttystate != NULL 287cf7f2e2dSJohn Marino && tinfo->run_terminal == NULL) 2885796c8dcSSimon Schubert { 2895796c8dcSSimon Schubert int result; 2905796c8dcSSimon Schubert 2915796c8dcSSimon Schubert #ifdef F_GETFL 2925796c8dcSSimon Schubert /* Is there a reason this is being done twice? It happens both 2935796c8dcSSimon Schubert places we use F_SETFL, so I'm inclined to think perhaps there 2945796c8dcSSimon Schubert is some reason, however perverse. Perhaps not though... */ 295cf7f2e2dSJohn Marino result = fcntl (0, F_SETFL, tinfo->tflags); 296cf7f2e2dSJohn Marino result = fcntl (0, F_SETFL, tinfo->tflags); 2975796c8dcSSimon Schubert OOPSY ("fcntl F_SETFL"); 2985796c8dcSSimon Schubert #endif 2995796c8dcSSimon Schubert 3005796c8dcSSimon Schubert /* Because we were careful to not change in or out of raw mode in 3015796c8dcSSimon Schubert terminal_ours, we will not change in our out of raw mode with 3025796c8dcSSimon Schubert this call, so we don't flush any input. */ 3035796c8dcSSimon Schubert result = serial_set_tty_state (stdin_serial, 304cf7f2e2dSJohn Marino tinfo->ttystate); 3055796c8dcSSimon Schubert OOPSY ("setting tty state"); 3065796c8dcSSimon Schubert 3075796c8dcSSimon Schubert if (!job_control) 3085796c8dcSSimon Schubert { 3095796c8dcSSimon Schubert sigint_ours = (void (*)()) signal (SIGINT, SIG_IGN); 3105796c8dcSSimon Schubert #ifdef SIGQUIT 3115796c8dcSSimon Schubert sigquit_ours = (void (*)()) signal (SIGQUIT, SIG_IGN); 3125796c8dcSSimon Schubert #endif 3135796c8dcSSimon Schubert } 3145796c8dcSSimon Schubert 3155796c8dcSSimon Schubert /* If attach_flag is set, we don't know whether we are sharing a 3165796c8dcSSimon Schubert terminal with the inferior or not. (attaching a process 3175796c8dcSSimon Schubert without a terminal is one case where we do not; attaching a 3185796c8dcSSimon Schubert process which we ran from the same shell as GDB via `&' is 3195796c8dcSSimon Schubert one case where we do, I think (but perhaps this is not 3205796c8dcSSimon Schubert `sharing' in the sense that we need to save and restore tty 3215796c8dcSSimon Schubert state)). I don't know if there is any way to tell whether we 3225796c8dcSSimon Schubert are sharing a terminal. So what we do is to go through all 3235796c8dcSSimon Schubert the saving and restoring of the tty state, but ignore errors 3245796c8dcSSimon Schubert setting the process group, which will happen if we are not 3255796c8dcSSimon Schubert sharing a terminal). */ 3265796c8dcSSimon Schubert 3275796c8dcSSimon Schubert if (job_control) 3285796c8dcSSimon Schubert { 3295796c8dcSSimon Schubert #ifdef HAVE_TERMIOS 330cf7f2e2dSJohn Marino result = tcsetpgrp (0, tinfo->process_group); 3315796c8dcSSimon Schubert if (!inf->attach_flag) 3325796c8dcSSimon Schubert OOPSY ("tcsetpgrp"); 3335796c8dcSSimon Schubert #endif 3345796c8dcSSimon Schubert 3355796c8dcSSimon Schubert #ifdef HAVE_SGTTY 336cf7f2e2dSJohn Marino result = ioctl (0, TIOCSPGRP, &tinfo->process_group); 3375796c8dcSSimon Schubert if (!inf->attach_flag) 3385796c8dcSSimon Schubert OOPSY ("TIOCSPGRP"); 3395796c8dcSSimon Schubert #endif 3405796c8dcSSimon Schubert } 3415796c8dcSSimon Schubert 3425796c8dcSSimon Schubert } 3435796c8dcSSimon Schubert terminal_is_ours = 0; 3445796c8dcSSimon Schubert } 3455796c8dcSSimon Schubert 3465796c8dcSSimon Schubert /* Put some of our terminal settings into effect, 3475796c8dcSSimon Schubert enough to get proper results from our output, 3485796c8dcSSimon Schubert but do not change into or out of RAW mode 3495796c8dcSSimon Schubert so that no input is discarded. 3505796c8dcSSimon Schubert 3515796c8dcSSimon Schubert After doing this, either terminal_ours or terminal_inferior 3525796c8dcSSimon Schubert should be called to get back to a normal state of affairs. */ 3535796c8dcSSimon Schubert 3545796c8dcSSimon Schubert void 3555796c8dcSSimon Schubert terminal_ours_for_output (void) 3565796c8dcSSimon Schubert { 3575796c8dcSSimon Schubert terminal_ours_1 (1); 3585796c8dcSSimon Schubert } 3595796c8dcSSimon Schubert 3605796c8dcSSimon Schubert /* Put our terminal settings into effect. 3615796c8dcSSimon Schubert First record the inferior's terminal settings 3625796c8dcSSimon Schubert so they can be restored properly later. */ 3635796c8dcSSimon Schubert 3645796c8dcSSimon Schubert void 3655796c8dcSSimon Schubert terminal_ours (void) 3665796c8dcSSimon Schubert { 3675796c8dcSSimon Schubert terminal_ours_1 (0); 3685796c8dcSSimon Schubert } 3695796c8dcSSimon Schubert 3705796c8dcSSimon Schubert /* output_only is not used, and should not be used unless we introduce 3715796c8dcSSimon Schubert separate terminal_is_ours and terminal_is_ours_for_output 3725796c8dcSSimon Schubert flags. */ 3735796c8dcSSimon Schubert 3745796c8dcSSimon Schubert static void 3755796c8dcSSimon Schubert terminal_ours_1 (int output_only) 3765796c8dcSSimon Schubert { 3775796c8dcSSimon Schubert struct inferior *inf; 378cf7f2e2dSJohn Marino struct terminal_info *tinfo; 3795796c8dcSSimon Schubert 3805796c8dcSSimon Schubert if (terminal_is_ours) 3815796c8dcSSimon Schubert return; 3825796c8dcSSimon Schubert 3835796c8dcSSimon Schubert terminal_is_ours = 1; 3845796c8dcSSimon Schubert 3855796c8dcSSimon Schubert /* Checking inferior->run_terminal is necessary so that 3865796c8dcSSimon Schubert if GDB is running in the background, it won't block trying 3875796c8dcSSimon Schubert to do the ioctl()'s below. Checking gdb_has_a_terminal 3885796c8dcSSimon Schubert avoids attempting all the ioctl's when running in batch. */ 3895796c8dcSSimon Schubert 3905796c8dcSSimon Schubert inf = current_inferior (); 391cf7f2e2dSJohn Marino tinfo = get_inflow_inferior_data (inf); 3925796c8dcSSimon Schubert 393cf7f2e2dSJohn Marino if (tinfo->run_terminal != NULL || gdb_has_a_terminal () == 0) 3945796c8dcSSimon Schubert return; 3955796c8dcSSimon Schubert 3965796c8dcSSimon Schubert { 3975796c8dcSSimon Schubert #ifdef SIGTTOU 3985796c8dcSSimon Schubert /* Ignore this signal since it will happen when we try to set the 3995796c8dcSSimon Schubert pgrp. */ 4005796c8dcSSimon Schubert void (*osigttou) () = NULL; 4015796c8dcSSimon Schubert #endif 4025796c8dcSSimon Schubert int result; 4035796c8dcSSimon Schubert 4045796c8dcSSimon Schubert #ifdef SIGTTOU 4055796c8dcSSimon Schubert if (job_control) 4065796c8dcSSimon Schubert osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN); 4075796c8dcSSimon Schubert #endif 4085796c8dcSSimon Schubert 409cf7f2e2dSJohn Marino xfree (tinfo->ttystate); 410cf7f2e2dSJohn Marino tinfo->ttystate = serial_get_tty_state (stdin_serial); 4115796c8dcSSimon Schubert 4125796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE 4135796c8dcSSimon Schubert if (!inf->attach_flag) 4145796c8dcSSimon Schubert /* If setpgrp failed in terminal_inferior, this would give us 4155796c8dcSSimon Schubert our process group instead of the inferior's. See 4165796c8dcSSimon Schubert terminal_inferior for details. */ 417cf7f2e2dSJohn Marino tinfo->process_group = gdb_getpgrp (); 4185796c8dcSSimon Schubert #endif 4195796c8dcSSimon Schubert 4205796c8dcSSimon Schubert /* Here we used to set ICANON in our ttystate, but I believe this 4215796c8dcSSimon Schubert was an artifact from before when we used readline. Readline sets 4225796c8dcSSimon Schubert the tty state when it needs to. 4235796c8dcSSimon Schubert FIXME-maybe: However, query() expects non-raw mode and doesn't 4245796c8dcSSimon Schubert use readline. Maybe query should use readline (on the other hand, 4255796c8dcSSimon Schubert this only matters for HAVE_SGTTY, not termio or termios, I think). */ 4265796c8dcSSimon Schubert 4275796c8dcSSimon Schubert /* Set tty state to our_ttystate. We don't change in our out of raw 4285796c8dcSSimon Schubert mode, to avoid flushing input. We need to do the same thing 4295796c8dcSSimon Schubert regardless of output_only, because we don't have separate 4305796c8dcSSimon Schubert terminal_is_ours and terminal_is_ours_for_output flags. It's OK, 431*c50c785cSJohn Marino though, since readline will deal with raw mode when/if it needs 432*c50c785cSJohn Marino to. */ 4335796c8dcSSimon Schubert 4345796c8dcSSimon Schubert serial_noflush_set_tty_state (stdin_serial, our_terminal_info.ttystate, 435cf7f2e2dSJohn Marino tinfo->ttystate); 4365796c8dcSSimon Schubert 4375796c8dcSSimon Schubert if (job_control) 4385796c8dcSSimon Schubert { 4395796c8dcSSimon Schubert #ifdef HAVE_TERMIOS 4405796c8dcSSimon Schubert result = tcsetpgrp (0, our_terminal_info.process_group); 4415796c8dcSSimon Schubert #if 0 4425796c8dcSSimon Schubert /* This fails on Ultrix with EINVAL if you run the testsuite 4435796c8dcSSimon Schubert in the background with nohup, and then log out. GDB never 4445796c8dcSSimon Schubert used to check for an error here, so perhaps there are other 4455796c8dcSSimon Schubert such situations as well. */ 4465796c8dcSSimon Schubert if (result == -1) 447*c50c785cSJohn Marino fprintf_unfiltered (gdb_stderr, 448*c50c785cSJohn Marino "[tcsetpgrp failed in terminal_ours: %s]\n", 4495796c8dcSSimon Schubert safe_strerror (errno)); 4505796c8dcSSimon Schubert #endif 4515796c8dcSSimon Schubert #endif /* termios */ 4525796c8dcSSimon Schubert 4535796c8dcSSimon Schubert #ifdef HAVE_SGTTY 4545796c8dcSSimon Schubert result = ioctl (0, TIOCSPGRP, &our_terminal_info.process_group); 4555796c8dcSSimon Schubert #endif 4565796c8dcSSimon Schubert } 4575796c8dcSSimon Schubert 4585796c8dcSSimon Schubert #ifdef SIGTTOU 4595796c8dcSSimon Schubert if (job_control) 4605796c8dcSSimon Schubert signal (SIGTTOU, osigttou); 4615796c8dcSSimon Schubert #endif 4625796c8dcSSimon Schubert 4635796c8dcSSimon Schubert if (!job_control) 4645796c8dcSSimon Schubert { 4655796c8dcSSimon Schubert signal (SIGINT, sigint_ours); 4665796c8dcSSimon Schubert #ifdef SIGQUIT 4675796c8dcSSimon Schubert signal (SIGQUIT, sigquit_ours); 4685796c8dcSSimon Schubert #endif 4695796c8dcSSimon Schubert } 4705796c8dcSSimon Schubert 4715796c8dcSSimon Schubert #ifdef F_GETFL 472cf7f2e2dSJohn Marino tinfo->tflags = fcntl (0, F_GETFL, 0); 4735796c8dcSSimon Schubert 4745796c8dcSSimon Schubert /* Is there a reason this is being done twice? It happens both 4755796c8dcSSimon Schubert places we use F_SETFL, so I'm inclined to think perhaps there 4765796c8dcSSimon Schubert is some reason, however perverse. Perhaps not though... */ 4775796c8dcSSimon Schubert result = fcntl (0, F_SETFL, our_terminal_info.tflags); 4785796c8dcSSimon Schubert result = fcntl (0, F_SETFL, our_terminal_info.tflags); 4795796c8dcSSimon Schubert #endif 4805796c8dcSSimon Schubert } 4815796c8dcSSimon Schubert } 4825796c8dcSSimon Schubert 483cf7f2e2dSJohn Marino /* Per-inferior data key. */ 484cf7f2e2dSJohn Marino static const struct inferior_data *inflow_inferior_data; 4855796c8dcSSimon Schubert 4865796c8dcSSimon Schubert static void 487cf7f2e2dSJohn Marino inflow_inferior_data_cleanup (struct inferior *inf, void *arg) 4885796c8dcSSimon Schubert { 489cf7f2e2dSJohn Marino struct terminal_info *info; 4905796c8dcSSimon Schubert 491cf7f2e2dSJohn Marino info = inferior_data (inf, inflow_inferior_data); 492cf7f2e2dSJohn Marino if (info != NULL) 493cf7f2e2dSJohn Marino { 494cf7f2e2dSJohn Marino xfree (info->run_terminal); 495*c50c785cSJohn Marino xfree (info->ttystate); 496cf7f2e2dSJohn Marino xfree (info); 497cf7f2e2dSJohn Marino } 498cf7f2e2dSJohn Marino } 499cf7f2e2dSJohn Marino 500cf7f2e2dSJohn Marino /* Get the current svr4 data. If none is found yet, add it now. This 501cf7f2e2dSJohn Marino function always returns a valid object. */ 502cf7f2e2dSJohn Marino 503cf7f2e2dSJohn Marino static struct terminal_info * 504cf7f2e2dSJohn Marino get_inflow_inferior_data (struct inferior *inf) 505cf7f2e2dSJohn Marino { 506cf7f2e2dSJohn Marino struct terminal_info *info; 507cf7f2e2dSJohn Marino 508cf7f2e2dSJohn Marino info = inferior_data (inf, inflow_inferior_data); 509cf7f2e2dSJohn Marino if (info == NULL) 510cf7f2e2dSJohn Marino { 511cf7f2e2dSJohn Marino info = XZALLOC (struct terminal_info); 512cf7f2e2dSJohn Marino set_inferior_data (inf, inflow_inferior_data, info); 513cf7f2e2dSJohn Marino } 514cf7f2e2dSJohn Marino 515cf7f2e2dSJohn Marino return info; 5165796c8dcSSimon Schubert } 5175796c8dcSSimon Schubert 5185796c8dcSSimon Schubert /* This is a "inferior_exit" observer. Releases the TERMINAL_INFO member 5195796c8dcSSimon Schubert of the inferior structure. This field is private to inflow.c, and 5205796c8dcSSimon Schubert its type is opaque to the rest of GDB. PID is the target pid of 5215796c8dcSSimon Schubert the inferior that is about to be removed from the inferior 5225796c8dcSSimon Schubert list. */ 5235796c8dcSSimon Schubert 5245796c8dcSSimon Schubert static void 525cf7f2e2dSJohn Marino inflow_inferior_exit (struct inferior *inf) 5265796c8dcSSimon Schubert { 527cf7f2e2dSJohn Marino struct terminal_info *info; 5285796c8dcSSimon Schubert 529cf7f2e2dSJohn Marino info = inferior_data (inf, inflow_inferior_data); 530cf7f2e2dSJohn Marino if (info != NULL) 531cf7f2e2dSJohn Marino { 532cf7f2e2dSJohn Marino xfree (info->run_terminal); 533*c50c785cSJohn Marino xfree (info->ttystate); 534cf7f2e2dSJohn Marino xfree (info); 535cf7f2e2dSJohn Marino set_inferior_data (inf, inflow_inferior_data, NULL); 536cf7f2e2dSJohn Marino } 5375796c8dcSSimon Schubert } 5385796c8dcSSimon Schubert 5395796c8dcSSimon Schubert void 5405796c8dcSSimon Schubert copy_terminal_info (struct inferior *to, struct inferior *from) 5415796c8dcSSimon Schubert { 542cf7f2e2dSJohn Marino struct terminal_info *tinfo_to, *tinfo_from; 543cf7f2e2dSJohn Marino 544cf7f2e2dSJohn Marino tinfo_to = get_inflow_inferior_data (to); 545cf7f2e2dSJohn Marino tinfo_from = get_inflow_inferior_data (from); 546*c50c785cSJohn Marino 547*c50c785cSJohn Marino xfree (tinfo_to->run_terminal); 548*c50c785cSJohn Marino xfree (tinfo_to->ttystate); 549*c50c785cSJohn Marino 550cf7f2e2dSJohn Marino *tinfo_to = *tinfo_from; 551*c50c785cSJohn Marino 552cf7f2e2dSJohn Marino if (tinfo_from->run_terminal) 553cf7f2e2dSJohn Marino tinfo_to->run_terminal 554cf7f2e2dSJohn Marino = xstrdup (tinfo_from->run_terminal); 555*c50c785cSJohn Marino 556*c50c785cSJohn Marino if (tinfo_from->ttystate) 557*c50c785cSJohn Marino tinfo_to->ttystate 558*c50c785cSJohn Marino = serial_copy_tty_state (stdin_serial, tinfo_from->ttystate); 5595796c8dcSSimon Schubert } 5605796c8dcSSimon Schubert 5615796c8dcSSimon Schubert void 5625796c8dcSSimon Schubert term_info (char *arg, int from_tty) 5635796c8dcSSimon Schubert { 5645796c8dcSSimon Schubert target_terminal_info (arg, from_tty); 5655796c8dcSSimon Schubert } 5665796c8dcSSimon Schubert 5675796c8dcSSimon Schubert void 5685796c8dcSSimon Schubert child_terminal_info (char *args, int from_tty) 5695796c8dcSSimon Schubert { 5705796c8dcSSimon Schubert struct inferior *inf; 571cf7f2e2dSJohn Marino struct terminal_info *tinfo; 5725796c8dcSSimon Schubert 5735796c8dcSSimon Schubert if (!gdb_has_a_terminal ()) 5745796c8dcSSimon Schubert { 5755796c8dcSSimon Schubert printf_filtered (_("This GDB does not control a terminal.\n")); 5765796c8dcSSimon Schubert return; 5775796c8dcSSimon Schubert } 5785796c8dcSSimon Schubert 5795796c8dcSSimon Schubert if (ptid_equal (inferior_ptid, null_ptid)) 5805796c8dcSSimon Schubert return; 5815796c8dcSSimon Schubert 5825796c8dcSSimon Schubert inf = current_inferior (); 583cf7f2e2dSJohn Marino tinfo = get_inflow_inferior_data (inf); 5845796c8dcSSimon Schubert 585*c50c785cSJohn Marino printf_filtered (_("Inferior's terminal status " 586*c50c785cSJohn Marino "(currently saved by GDB):\n")); 5875796c8dcSSimon Schubert 5885796c8dcSSimon Schubert /* First the fcntl flags. */ 5895796c8dcSSimon Schubert { 5905796c8dcSSimon Schubert int flags; 5915796c8dcSSimon Schubert 592cf7f2e2dSJohn Marino flags = tinfo->tflags; 5935796c8dcSSimon Schubert 5945796c8dcSSimon Schubert printf_filtered ("File descriptor flags = "); 5955796c8dcSSimon Schubert 5965796c8dcSSimon Schubert #ifndef O_ACCMODE 5975796c8dcSSimon Schubert #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR) 5985796c8dcSSimon Schubert #endif 599*c50c785cSJohn Marino /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */ 6005796c8dcSSimon Schubert switch (flags & (O_ACCMODE)) 6015796c8dcSSimon Schubert { 6025796c8dcSSimon Schubert case O_RDONLY: 6035796c8dcSSimon Schubert printf_filtered ("O_RDONLY"); 6045796c8dcSSimon Schubert break; 6055796c8dcSSimon Schubert case O_WRONLY: 6065796c8dcSSimon Schubert printf_filtered ("O_WRONLY"); 6075796c8dcSSimon Schubert break; 6085796c8dcSSimon Schubert case O_RDWR: 6095796c8dcSSimon Schubert printf_filtered ("O_RDWR"); 6105796c8dcSSimon Schubert break; 6115796c8dcSSimon Schubert } 6125796c8dcSSimon Schubert flags &= ~(O_ACCMODE); 6135796c8dcSSimon Schubert 6145796c8dcSSimon Schubert #ifdef O_NONBLOCK 6155796c8dcSSimon Schubert if (flags & O_NONBLOCK) 6165796c8dcSSimon Schubert printf_filtered (" | O_NONBLOCK"); 6175796c8dcSSimon Schubert flags &= ~O_NONBLOCK; 6185796c8dcSSimon Schubert #endif 6195796c8dcSSimon Schubert 6205796c8dcSSimon Schubert #if defined (O_NDELAY) 6215796c8dcSSimon Schubert /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will 6225796c8dcSSimon Schubert print it as O_NONBLOCK, which is good cause that is what POSIX 6235796c8dcSSimon Schubert has, and the flag will already be cleared by the time we get here. */ 6245796c8dcSSimon Schubert if (flags & O_NDELAY) 6255796c8dcSSimon Schubert printf_filtered (" | O_NDELAY"); 6265796c8dcSSimon Schubert flags &= ~O_NDELAY; 6275796c8dcSSimon Schubert #endif 6285796c8dcSSimon Schubert 6295796c8dcSSimon Schubert if (flags & O_APPEND) 6305796c8dcSSimon Schubert printf_filtered (" | O_APPEND"); 6315796c8dcSSimon Schubert flags &= ~O_APPEND; 6325796c8dcSSimon Schubert 6335796c8dcSSimon Schubert #if defined (O_BINARY) 6345796c8dcSSimon Schubert if (flags & O_BINARY) 6355796c8dcSSimon Schubert printf_filtered (" | O_BINARY"); 6365796c8dcSSimon Schubert flags &= ~O_BINARY; 6375796c8dcSSimon Schubert #endif 6385796c8dcSSimon Schubert 6395796c8dcSSimon Schubert if (flags) 6405796c8dcSSimon Schubert printf_filtered (" | 0x%x", flags); 6415796c8dcSSimon Schubert printf_filtered ("\n"); 6425796c8dcSSimon Schubert } 6435796c8dcSSimon Schubert 6445796c8dcSSimon Schubert #ifdef PROCESS_GROUP_TYPE 645cf7f2e2dSJohn Marino printf_filtered ("Process group = %d\n", (int) tinfo->process_group); 6465796c8dcSSimon Schubert #endif 6475796c8dcSSimon Schubert 648cf7f2e2dSJohn Marino serial_print_tty_state (stdin_serial, tinfo->ttystate, gdb_stdout); 6495796c8dcSSimon Schubert } 6505796c8dcSSimon Schubert 6515796c8dcSSimon Schubert /* NEW_TTY_PREFORK is called before forking a new child process, 6525796c8dcSSimon Schubert so we can record the state of ttys in the child to be formed. 6535796c8dcSSimon Schubert TTYNAME is null if we are to share the terminal with gdb; 6545796c8dcSSimon Schubert or points to a string containing the name of the desired tty. 6555796c8dcSSimon Schubert 6565796c8dcSSimon Schubert NEW_TTY is called in new child processes under Unix, which will 6575796c8dcSSimon Schubert become debugger target processes. This actually switches to 6585796c8dcSSimon Schubert the terminal specified in the NEW_TTY_PREFORK call. */ 6595796c8dcSSimon Schubert 6605796c8dcSSimon Schubert void 6615796c8dcSSimon Schubert new_tty_prefork (const char *ttyname) 6625796c8dcSSimon Schubert { 6635796c8dcSSimon Schubert /* Save the name for later, for determining whether we and the child 6645796c8dcSSimon Schubert are sharing a tty. */ 6655796c8dcSSimon Schubert inferior_thisrun_terminal = ttyname; 6665796c8dcSSimon Schubert } 6675796c8dcSSimon Schubert 668cf7f2e2dSJohn Marino #if !defined(__GO32__) && !defined(_WIN32) 6695796c8dcSSimon Schubert /* If RESULT, assumed to be the return value from a system call, is 6705796c8dcSSimon Schubert negative, print the error message indicated by errno and exit. 6715796c8dcSSimon Schubert MSG should identify the operation that failed. */ 6725796c8dcSSimon Schubert static void 6735796c8dcSSimon Schubert check_syscall (const char *msg, int result) 6745796c8dcSSimon Schubert { 6755796c8dcSSimon Schubert if (result < 0) 6765796c8dcSSimon Schubert { 6775796c8dcSSimon Schubert print_sys_errmsg (msg, errno); 6785796c8dcSSimon Schubert _exit (1); 6795796c8dcSSimon Schubert } 6805796c8dcSSimon Schubert } 681cf7f2e2dSJohn Marino #endif 6825796c8dcSSimon Schubert 6835796c8dcSSimon Schubert void 6845796c8dcSSimon Schubert new_tty (void) 6855796c8dcSSimon Schubert { 6865796c8dcSSimon Schubert int tty; 6875796c8dcSSimon Schubert 6885796c8dcSSimon Schubert if (inferior_thisrun_terminal == 0) 6895796c8dcSSimon Schubert return; 6905796c8dcSSimon Schubert #if !defined(__GO32__) && !defined(_WIN32) 6915796c8dcSSimon Schubert #ifdef TIOCNOTTY 6925796c8dcSSimon Schubert /* Disconnect the child process from our controlling terminal. On some 6935796c8dcSSimon Schubert systems (SVR4 for example), this may cause a SIGTTOU, so temporarily 6945796c8dcSSimon Schubert ignore SIGTTOU. */ 6955796c8dcSSimon Schubert tty = open ("/dev/tty", O_RDWR); 6965796c8dcSSimon Schubert if (tty > 0) 6975796c8dcSSimon Schubert { 6985796c8dcSSimon Schubert void (*osigttou) (); 6995796c8dcSSimon Schubert 7005796c8dcSSimon Schubert osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN); 7015796c8dcSSimon Schubert ioctl (tty, TIOCNOTTY, 0); 7025796c8dcSSimon Schubert close (tty); 7035796c8dcSSimon Schubert signal (SIGTTOU, osigttou); 7045796c8dcSSimon Schubert } 7055796c8dcSSimon Schubert #endif 7065796c8dcSSimon Schubert 7075796c8dcSSimon Schubert /* Now open the specified new terminal. */ 7085796c8dcSSimon Schubert tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY); 7095796c8dcSSimon Schubert check_syscall (inferior_thisrun_terminal, tty); 7105796c8dcSSimon Schubert 7115796c8dcSSimon Schubert /* Avoid use of dup2; doesn't exist on all systems. */ 7125796c8dcSSimon Schubert if (tty != 0) 7135796c8dcSSimon Schubert { 7145796c8dcSSimon Schubert close (0); 7155796c8dcSSimon Schubert check_syscall ("dup'ing tty into fd 0", dup (tty)); 7165796c8dcSSimon Schubert } 7175796c8dcSSimon Schubert if (tty != 1) 7185796c8dcSSimon Schubert { 7195796c8dcSSimon Schubert close (1); 7205796c8dcSSimon Schubert check_syscall ("dup'ing tty into fd 1", dup (tty)); 7215796c8dcSSimon Schubert } 7225796c8dcSSimon Schubert if (tty != 2) 7235796c8dcSSimon Schubert { 7245796c8dcSSimon Schubert close (2); 7255796c8dcSSimon Schubert check_syscall ("dup'ing tty into fd 2", dup (tty)); 7265796c8dcSSimon Schubert } 7275796c8dcSSimon Schubert 7285796c8dcSSimon Schubert #ifdef TIOCSCTTY 7295796c8dcSSimon Schubert /* Make tty our new controlling terminal. */ 7305796c8dcSSimon Schubert if (ioctl (tty, TIOCSCTTY, 0) == -1) 7315796c8dcSSimon Schubert /* Mention GDB in warning because it will appear in the inferior's 7325796c8dcSSimon Schubert terminal instead of GDB's. */ 733*c50c785cSJohn Marino warning (_("GDB: Failed to set controlling terminal: %s"), 7345796c8dcSSimon Schubert safe_strerror (errno)); 7355796c8dcSSimon Schubert #endif 7365796c8dcSSimon Schubert 7375796c8dcSSimon Schubert if (tty > 2) 7385796c8dcSSimon Schubert close (tty); 7395796c8dcSSimon Schubert #endif /* !go32 && !win32 */ 7405796c8dcSSimon Schubert } 7415796c8dcSSimon Schubert 7425796c8dcSSimon Schubert /* NEW_TTY_POSTFORK is called after forking a new child process, and 7435796c8dcSSimon Schubert adding it to the inferior table, to store the TTYNAME being used by 7445796c8dcSSimon Schubert the child, or null if it sharing the terminal with gdb. */ 7455796c8dcSSimon Schubert 7465796c8dcSSimon Schubert void 7475796c8dcSSimon Schubert new_tty_postfork (void) 7485796c8dcSSimon Schubert { 7495796c8dcSSimon Schubert /* Save the name for later, for determining whether we and the child 7505796c8dcSSimon Schubert are sharing a tty. */ 7515796c8dcSSimon Schubert 7525796c8dcSSimon Schubert if (inferior_thisrun_terminal) 753cf7f2e2dSJohn Marino { 754cf7f2e2dSJohn Marino struct inferior *inf = current_inferior (); 755cf7f2e2dSJohn Marino struct terminal_info *tinfo = get_inflow_inferior_data (inf); 756cf7f2e2dSJohn Marino 757cf7f2e2dSJohn Marino tinfo->run_terminal = xstrdup (inferior_thisrun_terminal); 758cf7f2e2dSJohn Marino } 7595796c8dcSSimon Schubert 7605796c8dcSSimon Schubert inferior_thisrun_terminal = NULL; 7615796c8dcSSimon Schubert } 7625796c8dcSSimon Schubert 7635796c8dcSSimon Schubert 7645796c8dcSSimon Schubert /* Call set_sigint_trap when you need to pass a signal on to an attached 765*c50c785cSJohn Marino process when handling SIGINT. */ 7665796c8dcSSimon Schubert 7675796c8dcSSimon Schubert static void 7685796c8dcSSimon Schubert pass_signal (int signo) 7695796c8dcSSimon Schubert { 7705796c8dcSSimon Schubert #ifndef _WIN32 7715796c8dcSSimon Schubert kill (PIDGET (inferior_ptid), SIGINT); 7725796c8dcSSimon Schubert #endif 7735796c8dcSSimon Schubert } 7745796c8dcSSimon Schubert 7755796c8dcSSimon Schubert static void (*osig) (); 7765796c8dcSSimon Schubert static int osig_set; 7775796c8dcSSimon Schubert 7785796c8dcSSimon Schubert void 7795796c8dcSSimon Schubert set_sigint_trap (void) 7805796c8dcSSimon Schubert { 7815796c8dcSSimon Schubert struct inferior *inf = current_inferior (); 782cf7f2e2dSJohn Marino struct terminal_info *tinfo = get_inflow_inferior_data (inf); 783cf7f2e2dSJohn Marino 784cf7f2e2dSJohn Marino if (inf->attach_flag || tinfo->run_terminal) 7855796c8dcSSimon Schubert { 7865796c8dcSSimon Schubert osig = (void (*)()) signal (SIGINT, pass_signal); 7875796c8dcSSimon Schubert osig_set = 1; 7885796c8dcSSimon Schubert } 7895796c8dcSSimon Schubert else 7905796c8dcSSimon Schubert osig_set = 0; 7915796c8dcSSimon Schubert } 7925796c8dcSSimon Schubert 7935796c8dcSSimon Schubert void 7945796c8dcSSimon Schubert clear_sigint_trap (void) 7955796c8dcSSimon Schubert { 7965796c8dcSSimon Schubert if (osig_set) 7975796c8dcSSimon Schubert { 7985796c8dcSSimon Schubert signal (SIGINT, osig); 7995796c8dcSSimon Schubert osig_set = 0; 8005796c8dcSSimon Schubert } 8015796c8dcSSimon Schubert } 8025796c8dcSSimon Schubert 8035796c8dcSSimon Schubert 8045796c8dcSSimon Schubert /* Create a new session if the inferior will run in a different tty. 8055796c8dcSSimon Schubert A session is UNIX's way of grouping processes that share a controlling 8065796c8dcSSimon Schubert terminal, so a new one is needed if the inferior terminal will be 8075796c8dcSSimon Schubert different from GDB's. 8085796c8dcSSimon Schubert 8095796c8dcSSimon Schubert Returns the session id of the new session, 0 if no session was created 8105796c8dcSSimon Schubert or -1 if an error occurred. */ 8115796c8dcSSimon Schubert pid_t 8125796c8dcSSimon Schubert create_tty_session (void) 8135796c8dcSSimon Schubert { 8145796c8dcSSimon Schubert #ifdef HAVE_SETSID 8155796c8dcSSimon Schubert pid_t ret; 8165796c8dcSSimon Schubert 8175796c8dcSSimon Schubert if (!job_control || inferior_thisrun_terminal == 0) 8185796c8dcSSimon Schubert return 0; 8195796c8dcSSimon Schubert 8205796c8dcSSimon Schubert ret = setsid (); 8215796c8dcSSimon Schubert if (ret == -1) 822*c50c785cSJohn Marino warning (_("Failed to create new terminal session: setsid: %s"), 8235796c8dcSSimon Schubert safe_strerror (errno)); 8245796c8dcSSimon Schubert 8255796c8dcSSimon Schubert return ret; 8265796c8dcSSimon Schubert #else 8275796c8dcSSimon Schubert return 0; 8285796c8dcSSimon Schubert #endif /* HAVE_SETSID */ 8295796c8dcSSimon Schubert } 8305796c8dcSSimon Schubert 8315796c8dcSSimon Schubert /* This is here because this is where we figure out whether we (probably) 8325796c8dcSSimon Schubert have job control. Just using job_control only does part of it because 8335796c8dcSSimon Schubert setpgid or setpgrp might not exist on a system without job control. 8345796c8dcSSimon Schubert It might be considered misplaced (on the other hand, process groups and 8355796c8dcSSimon Schubert job control are closely related to ttys). 8365796c8dcSSimon Schubert 8375796c8dcSSimon Schubert For a more clean implementation, in libiberty, put a setpgid which merely 8385796c8dcSSimon Schubert calls setpgrp and a setpgrp which does nothing (any system with job control 8395796c8dcSSimon Schubert will have one or the other). */ 8405796c8dcSSimon Schubert int 8415796c8dcSSimon Schubert gdb_setpgid (void) 8425796c8dcSSimon Schubert { 8435796c8dcSSimon Schubert int retval = 0; 8445796c8dcSSimon Schubert 8455796c8dcSSimon Schubert if (job_control) 8465796c8dcSSimon Schubert { 8475796c8dcSSimon Schubert #if defined (HAVE_TERMIOS) || defined (TIOCGPGRP) 8485796c8dcSSimon Schubert #ifdef HAVE_SETPGID 8495796c8dcSSimon Schubert /* The call setpgid (0, 0) is supposed to work and mean the same 8505796c8dcSSimon Schubert thing as this, but on Ultrix 4.2A it fails with EPERM (and 8515796c8dcSSimon Schubert setpgid (getpid (), getpid ()) succeeds). */ 8525796c8dcSSimon Schubert retval = setpgid (getpid (), getpid ()); 8535796c8dcSSimon Schubert #else 8545796c8dcSSimon Schubert #ifdef HAVE_SETPGRP 8555796c8dcSSimon Schubert #ifdef SETPGRP_VOID 8565796c8dcSSimon Schubert retval = setpgrp (); 8575796c8dcSSimon Schubert #else 8585796c8dcSSimon Schubert retval = setpgrp (getpid (), getpid ()); 8595796c8dcSSimon Schubert #endif 8605796c8dcSSimon Schubert #endif /* HAVE_SETPGRP */ 8615796c8dcSSimon Schubert #endif /* HAVE_SETPGID */ 8625796c8dcSSimon Schubert #endif /* defined (HAVE_TERMIOS) || defined (TIOCGPGRP) */ 8635796c8dcSSimon Schubert } 8645796c8dcSSimon Schubert 8655796c8dcSSimon Schubert return retval; 8665796c8dcSSimon Schubert } 8675796c8dcSSimon Schubert 8685796c8dcSSimon Schubert /* Get all the current tty settings (including whether we have a 8695796c8dcSSimon Schubert tty at all!). We can't do this in _initialize_inflow because 8705796c8dcSSimon Schubert serial_fdopen() won't work until the serial_ops_list is 8715796c8dcSSimon Schubert initialized, but we don't want to do it lazily either, so 8725796c8dcSSimon Schubert that we can guarantee stdin_serial is opened if there is 8735796c8dcSSimon Schubert a terminal. */ 8745796c8dcSSimon Schubert void 8755796c8dcSSimon Schubert initialize_stdin_serial (void) 8765796c8dcSSimon Schubert { 8775796c8dcSSimon Schubert stdin_serial = serial_fdopen (0); 8785796c8dcSSimon Schubert } 8795796c8dcSSimon Schubert 8805796c8dcSSimon Schubert void 8815796c8dcSSimon Schubert _initialize_inflow (void) 8825796c8dcSSimon Schubert { 8835796c8dcSSimon Schubert add_info ("terminal", term_info, 8845796c8dcSSimon Schubert _("Print inferior's saved terminal status.")); 8855796c8dcSSimon Schubert 886*c50c785cSJohn Marino add_setshow_auto_boolean_cmd ("interactive-mode", class_support, 887*c50c785cSJohn Marino &interactive_mode, _("\ 888*c50c785cSJohn Marino Set whether GDB's standard input is a terminal."), _("\ 889*c50c785cSJohn Marino Show whether GDB's standard input is a terminal."), _("\ 890*c50c785cSJohn Marino If on, GDB assumes that standard input is a terminal. In practice, it\n\ 891*c50c785cSJohn Marino means that GDB should wait for the user to answer queries associated to\n\ 892*c50c785cSJohn Marino commands entered at the command prompt. If off, GDB assumes that standard\n\ 893*c50c785cSJohn Marino input is not a terminal, and uses the default answer to all queries.\n\ 894*c50c785cSJohn Marino If auto (the default), determine which mode to use based on the standard\n\ 895*c50c785cSJohn Marino input settings."), 896*c50c785cSJohn Marino NULL, 897*c50c785cSJohn Marino show_interactive_mode, 898*c50c785cSJohn Marino &setlist, &showlist); 899*c50c785cSJohn Marino 9005796c8dcSSimon Schubert terminal_is_ours = 1; 9015796c8dcSSimon Schubert 9025796c8dcSSimon Schubert /* OK, figure out whether we have job control. If neither termios nor 9035796c8dcSSimon Schubert sgtty (i.e. termio or go32), leave job_control 0. */ 9045796c8dcSSimon Schubert 9055796c8dcSSimon Schubert #if defined (HAVE_TERMIOS) 9065796c8dcSSimon Schubert /* Do all systems with termios have the POSIX way of identifying job 9075796c8dcSSimon Schubert control? I hope so. */ 9085796c8dcSSimon Schubert #ifdef _POSIX_JOB_CONTROL 9095796c8dcSSimon Schubert job_control = 1; 9105796c8dcSSimon Schubert #else 9115796c8dcSSimon Schubert #ifdef _SC_JOB_CONTROL 9125796c8dcSSimon Schubert job_control = sysconf (_SC_JOB_CONTROL); 9135796c8dcSSimon Schubert #else 914*c50c785cSJohn Marino job_control = 0; /* Have to assume the worst. */ 9155796c8dcSSimon Schubert #endif /* _SC_JOB_CONTROL */ 9165796c8dcSSimon Schubert #endif /* _POSIX_JOB_CONTROL */ 9175796c8dcSSimon Schubert #endif /* HAVE_TERMIOS */ 9185796c8dcSSimon Schubert 9195796c8dcSSimon Schubert #ifdef HAVE_SGTTY 9205796c8dcSSimon Schubert #ifdef TIOCGPGRP 9215796c8dcSSimon Schubert job_control = 1; 9225796c8dcSSimon Schubert #else 9235796c8dcSSimon Schubert job_control = 0; 9245796c8dcSSimon Schubert #endif /* TIOCGPGRP */ 9255796c8dcSSimon Schubert #endif /* sgtty */ 9265796c8dcSSimon Schubert 9275796c8dcSSimon Schubert observer_attach_inferior_exit (inflow_inferior_exit); 928cf7f2e2dSJohn Marino 929cf7f2e2dSJohn Marino inflow_inferior_data 930cf7f2e2dSJohn Marino = register_inferior_data_with_cleanup (inflow_inferior_data_cleanup); 9315796c8dcSSimon Schubert } 932