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