1 /* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1986-2022 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "gdbthread.h"
27 #include "observable.h"
30 #include "gdbsupport/gdb_select.h"
36 #include "gdbsupport/job-control.h"
37 #include "gdbsupport/scoped_ignore_sigttou.h"
39 #ifdef HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
47 static void pass_signal (int);
49 static void child_terminal_ours_1 (target_terminal_state
);
51 /* Record terminal status separately for debugger and inferior. */
53 static struct serial
*stdin_serial
;
55 /* Terminal related info we need to keep track of. Each inferior
56 holds an instance of this structure --- we save it whenever the
57 corresponding inferior stops, and restore it to the terminal when
58 the inferior is resumed in the foreground. */
61 terminal_info () = default;
64 terminal_info
&operator= (const terminal_info
&) = default;
66 /* The name of the tty (from the `tty' command) that we gave to the
67 inferior when it was started. */
68 std::string run_terminal
;
70 /* TTY state. We save it whenever the inferior stops, and restore
71 it when it resumes in the foreground. */
72 serial_ttystate ttystate
{};
75 /* The terminal's foreground process group. Saved whenever the
76 inferior stops. This is the pgrp displayed by "info terminal".
77 Note that this may be not the inferior's actual process group,
78 since each inferior that we spawn has its own process group, and
79 only one can be in the foreground at a time. When the inferior
80 resumes, if we can determine the inferior's actual pgrp, then we
81 make that the foreground pgrp instead of what was saved here.
82 While it's a bit arbitrary which inferior's pgrp ends up in the
83 foreground when we resume several inferiors, this at least makes
84 'resume inf1+inf2' + 'stop all' + 'resume inf2' end up with
85 inf2's pgrp in the foreground instead of inf1's (which would be
86 problematic since it would be left stopped: Ctrl-C wouldn't work,
88 pid_t process_group
= 0;
91 /* fcntl flags. Saved and restored just like ttystate. */
95 /* Our own tty state, which we restore every time we need to deal with
96 the terminal. This is set once, when GDB first starts, and then
97 whenever we enter/leave TUI mode (gdb_save_tty_state). The
98 settings of flags which readline saves and restores are
100 static struct terminal_info our_terminal_info
;
102 /* Snapshot of the initial tty state taken during initialization of
103 GDB, before readline/ncurses have had a chance to change it. This
104 is used as the initial tty state given to each new spawned
105 inferior. Unlike our_terminal_info, this is only ever set
107 static serial_ttystate initial_gdb_ttystate
;
109 static struct terminal_info
*get_inflow_inferior_data (struct inferior
*);
111 /* While the inferior is running, we want SIGINT and SIGQUIT to go to the
112 inferior only. If we have job control, that takes care of it. If not,
113 we save our handlers in these two variables and set SIGINT and SIGQUIT
116 static sighandler_t sigint_ours
;
118 static sighandler_t sigquit_ours
;
121 /* The name of the tty (from the `tty' command) that we're giving to
122 the inferior when starting it up. This is only (and should only
123 be) used as a transient global by new_tty_prefork,
124 create_tty_session, new_tty and new_tty_postfork, all called from
125 fork_inferior, while forking a new child. */
126 static std::string inferior_thisrun_terminal
;
128 /* Track who owns GDB's terminal (is it GDB or some inferior?). While
129 target_terminal::is_ours() etc. tracks the core's intention and is
130 independent of the target backend, this tracks the actual state of
131 GDB's own tty. So for example,
133 (target_terminal::is_inferior () && gdb_tty_state == terminal_is_ours)
135 is true when the (native) inferior is not sharing a terminal with
136 GDB (e.g., because we attached to an inferior that is running on a
137 different terminal). */
138 static target_terminal_state gdb_tty_state
= target_terminal_state::is_ours
;
140 /* See terminal.h. */
143 set_initial_gdb_ttystate (void)
145 /* Note we can't do any of this in _initialize_inflow because at
146 that point stdin_serial has not been created yet. */
148 initial_gdb_ttystate
= serial_get_tty_state (stdin_serial
);
150 if (initial_gdb_ttystate
!= NULL
)
152 our_terminal_info
.ttystate
153 = serial_copy_tty_state (stdin_serial
, initial_gdb_ttystate
);
155 our_terminal_info
.tflags
= fcntl (0, F_GETFL
, 0);
157 #ifdef HAVE_TERMIOS_H
158 our_terminal_info
.process_group
= tcgetpgrp (0);
163 /* Does GDB have a terminal (on stdin)? */
166 gdb_has_a_terminal (void)
168 return initial_gdb_ttystate
!= NULL
;
171 /* Macro for printing errors from ioctl operations */
173 #define OOPSY(what) \
175 gdb_printf(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
176 what, safe_strerror (errno))
178 /* Initialize the terminal settings we record for the inferior,
179 before we actually run the inferior. */
182 child_terminal_init (struct target_ops
*self
)
184 if (!gdb_has_a_terminal ())
187 inferior
*inf
= current_inferior ();
188 terminal_info
*tinfo
= get_inflow_inferior_data (inf
);
190 #ifdef HAVE_TERMIOS_H
191 /* A child we spawn should be a process group leader (PGID==PID) at
192 this point, though that may not be true if we're attaching to an
194 tinfo
->process_group
= inf
->pid
;
197 xfree (tinfo
->ttystate
);
198 tinfo
->ttystate
= serial_copy_tty_state (stdin_serial
, initial_gdb_ttystate
);
201 /* Save the terminal settings again. This is necessary for the TUI
202 when it switches to TUI or non-TUI mode; curses changes the terminal
203 and gdb must be able to restore it correctly. */
206 gdb_save_tty_state (void)
208 if (gdb_has_a_terminal ())
210 xfree (our_terminal_info
.ttystate
);
211 our_terminal_info
.ttystate
= serial_get_tty_state (stdin_serial
);
215 /* Try to determine whether TTY is GDB's input terminal. Returns
216 TRIBOOL_UNKNOWN if we can't tell. */
219 is_gdb_terminal (const char *tty
)
222 struct stat other_tty
;
225 res
= stat (tty
, &other_tty
);
227 return TRIBOOL_UNKNOWN
;
229 res
= fstat (STDIN_FILENO
, &gdb_tty
);
231 return TRIBOOL_UNKNOWN
;
233 return ((gdb_tty
.st_dev
== other_tty
.st_dev
234 && gdb_tty
.st_ino
== other_tty
.st_ino
)
239 /* Helper for sharing_input_terminal. Try to determine whether
240 inferior INF is using the same TTY for input as GDB is. Returns
241 TRIBOOL_UNKNOWN if we can't tell. */
244 sharing_input_terminal_1 (inferior
*inf
)
246 /* Using host-dependent code here is fine, because the
247 child_terminal_foo functions are meant to be used by child/native
249 #if defined (__linux__) || defined (__sun__)
252 xsnprintf (buf
, sizeof (buf
), "/proc/%d/fd/0", inf
->pid
);
253 return is_gdb_terminal (buf
);
255 return TRIBOOL_UNKNOWN
;
259 /* Return true if the inferior is using the same TTY for input as GDB
260 is. If this is true, then we save/restore terminal flags/state.
262 This is necessary because if inf->attach_flag is set, we don't
263 offhand know whether we are sharing a terminal with the inferior or
264 not. Attaching a process without a terminal is one case where we
265 do not; attaching a process which we ran from the same shell as GDB
266 via `&' is one case where we do.
268 If we can't determine, we assume the TTY is being shared. This
269 works OK if you're only debugging one inferior. However, if you're
270 debugging more than one inferior, and e.g., one is spawned by GDB
271 with "run" (sharing terminal with GDB), and another is attached to
272 (and running on a different terminal, as is most common), then it
273 matters, because we can only restore the terminal settings of one
274 of the inferiors, and in that scenario, we want to restore the
275 settings of the "run"'ed inferior.
277 Note, this is not the same as determining whether GDB and the
278 inferior are in the same session / connected to the same
279 controlling tty. An inferior (fork child) may call setsid,
280 disconnecting itself from the ctty, while still leaving
281 stdin/stdout/stderr associated with the original terminal. If
282 we're debugging that process, we should also save/restore terminal
286 sharing_input_terminal (inferior
*inf
)
288 terminal_info
*tinfo
= get_inflow_inferior_data (inf
);
290 tribool res
= sharing_input_terminal_1 (inf
);
292 if (res
== TRIBOOL_UNKNOWN
)
294 /* As fallback, if we can't determine by stat'ing the inferior's
295 tty directly (because it's not supported on this host) and
296 the child was spawned, check whether run_terminal is our tty.
297 This isn't ideal, since this is checking the child's
298 controlling terminal, not the input terminal (which may have
299 been redirected), but is still better than nothing. A false
300 positive ("set inferior-tty" points to our terminal, but I/O
301 was redirected) is much more likely than a false negative
302 ("set inferior-tty" points to some other terminal, and then
303 output was redirected to our terminal), and with a false
304 positive we just end up trying to save/restore terminal
305 settings when we didn't need to or we actually can't. */
306 if (!tinfo
->run_terminal
.empty ())
307 res
= is_gdb_terminal (tinfo
->run_terminal
.c_str ());
309 /* If we still can't determine, assume yes. */
310 if (res
== TRIBOOL_UNKNOWN
)
314 return res
== TRIBOOL_TRUE
;
317 /* Put the inferior's terminal settings into effect. This is
318 preparation for starting or resuming the inferior. */
321 child_terminal_inferior (struct target_ops
*self
)
323 /* If we resume more than one inferior in the foreground on GDB's
324 terminal, then the first inferior's terminal settings "win".
325 Note that every child process is put in its own process group, so
326 the first process that ends up resumed ends up determining which
327 process group the kernel forwards Ctrl-C/Ctrl-Z (SIGINT/SIGTTOU)
329 if (gdb_tty_state
== target_terminal_state::is_inferior
)
332 inferior
*inf
= current_inferior ();
333 terminal_info
*tinfo
= get_inflow_inferior_data (inf
);
335 if (gdb_has_a_terminal ()
336 && tinfo
->ttystate
!= NULL
337 && sharing_input_terminal (inf
))
341 /* Ignore SIGTTOU since it will happen when we try to set the
342 terminal's state (if gdb_tty_state is currently
344 scoped_ignore_sigttou ignore_sigttou
;
347 result
= fcntl (0, F_SETFL
, tinfo
->tflags
);
348 OOPSY ("fcntl F_SETFL");
351 result
= serial_set_tty_state (stdin_serial
, tinfo
->ttystate
);
352 OOPSY ("setting tty state");
356 sigint_ours
= signal (SIGINT
, SIG_IGN
);
358 sigquit_ours
= signal (SIGQUIT
, SIG_IGN
);
364 #ifdef HAVE_TERMIOS_H
365 /* If we can't tell the inferior's actual process group,
366 then restore whatever was the foreground pgrp the last
367 time the inferior was running. See also comments
368 describing terminal_state::process_group. */
370 result
= tcsetpgrp (0, getpgid (inf
->pid
));
372 result
= tcsetpgrp (0, tinfo
->process_group
);
377 /* This fails if either GDB has no controlling terminal,
378 e.g., running under 'setsid(1)', or if the inferior
379 is not attached to GDB's controlling terminal. E.g.,
380 if it called setsid to create a new session or used
381 the TIOCNOTTY ioctl, or simply if we've attached to a
382 process running on another terminal and we couldn't
383 tell whether it was sharing GDB's terminal (and so
387 "[tcsetpgrp failed in child_terminal_inferior: %s]\n",
388 safe_strerror (errno
));
394 gdb_tty_state
= target_terminal_state::is_inferior
;
398 /* Put some of our terminal settings into effect,
399 enough to get proper results from our output,
400 but do not change into or out of RAW mode
401 so that no input is discarded.
403 After doing this, either terminal_ours or terminal_inferior
404 should be called to get back to a normal state of affairs.
406 N.B. The implementation is (currently) no different than
407 child_terminal_ours. See child_terminal_ours_1. */
410 child_terminal_ours_for_output (struct target_ops
*self
)
412 child_terminal_ours_1 (target_terminal_state::is_ours_for_output
);
415 /* Put our terminal settings into effect.
416 First record the inferior's terminal settings
417 so they can be restored properly later.
419 N.B. Targets that want to use this with async support must build that
420 support on top of this (e.g., the caller still needs to add stdin to the
421 event loop). E.g., see linux_nat_terminal_ours. */
424 child_terminal_ours (struct target_ops
*self
)
426 child_terminal_ours_1 (target_terminal_state::is_ours
);
429 /* Save the current terminal settings in the inferior's terminal_info
433 child_terminal_save_inferior (struct target_ops
*self
)
435 /* Avoid attempting all the ioctl's when running in batch. */
436 if (!gdb_has_a_terminal ())
439 inferior
*inf
= current_inferior ();
440 terminal_info
*tinfo
= get_inflow_inferior_data (inf
);
442 /* No need to save/restore if the inferior is not sharing GDB's
444 if (!sharing_input_terminal (inf
))
447 xfree (tinfo
->ttystate
);
448 tinfo
->ttystate
= serial_get_tty_state (stdin_serial
);
450 #ifdef HAVE_TERMIOS_H
451 tinfo
->process_group
= tcgetpgrp (0);
455 tinfo
->tflags
= fcntl (0, F_GETFL
, 0);
459 /* Switch terminal state to DESIRED_STATE, either is_ours, or
460 is_ours_for_output. */
463 child_terminal_ours_1 (target_terminal_state desired_state
)
465 gdb_assert (desired_state
!= target_terminal_state::is_inferior
);
467 /* Avoid attempting all the ioctl's when running in batch. */
468 if (!gdb_has_a_terminal ())
471 if (gdb_tty_state
!= desired_state
)
473 int result ATTRIBUTE_UNUSED
;
475 /* Ignore SIGTTOU since it will happen when we try to set the
477 scoped_ignore_sigttou ignore_sigttou
;
479 /* Set tty state to our_ttystate. */
480 serial_set_tty_state (stdin_serial
, our_terminal_info
.ttystate
);
482 /* If we only want output, then leave the inferior's pgrp in the
483 foreground, so that Ctrl-C/Ctrl-Z reach the inferior
485 if (job_control
&& desired_state
== target_terminal_state::is_ours
)
487 #ifdef HAVE_TERMIOS_H
488 result
= tcsetpgrp (0, our_terminal_info
.process_group
);
490 /* This fails on Ultrix with EINVAL if you run the testsuite
491 in the background with nohup, and then log out. GDB never
492 used to check for an error here, so perhaps there are other
493 such situations as well. */
495 gdb_printf (gdb_stderr
,
496 "[tcsetpgrp failed in child_terminal_ours: %s]\n",
497 safe_strerror (errno
));
502 if (!job_control
&& desired_state
== target_terminal_state::is_ours
)
504 signal (SIGINT
, sigint_ours
);
506 signal (SIGQUIT
, sigquit_ours
);
511 result
= fcntl (0, F_SETFL
, our_terminal_info
.tflags
);
514 gdb_tty_state
= desired_state
;
518 /* Interrupt the inferior. Implementation of target_interrupt for
519 child/native targets. */
522 child_interrupt (struct target_ops
*self
)
524 /* Interrupt the first inferior that has a resumed thread. */
525 thread_info
*resumed
= NULL
;
526 for (thread_info
*thr
: all_non_exited_threads ())
528 if (thr
->executing ())
533 if (thr
->has_pending_waitstatus ())
539 /* Note that unlike pressing Ctrl-C on the controlling terminal,
540 here we only interrupt one process, not the whole process
541 group. This is because interrupting a process group (with
542 either Ctrl-C or with kill(3) with negative PID) sends a
543 SIGINT to each process in the process group, and we may not
544 be debugging all processes in the process group. */
546 kill (resumed
->inf
->pid
, SIGINT
);
551 /* Pass a Ctrl-C to the inferior as-if a Ctrl-C was pressed while the
552 inferior was in the foreground. Implementation of
553 target_pass_ctrlc for child/native targets. */
556 child_pass_ctrlc (struct target_ops
*self
)
558 gdb_assert (!target_terminal::is_ours ());
560 #ifdef HAVE_TERMIOS_H
563 pid_t term_pgrp
= tcgetpgrp (0);
565 /* If there's any inferior sharing our terminal, pass the SIGINT
566 to the terminal's foreground process group. This acts just
567 like the user typed a ^C on the terminal while the inferior
568 was in the foreground. Note that using a negative process
569 number in kill() is a System V-ism. The proper BSD interface
570 is killpg(). However, all modern BSDs support the System V
573 if (term_pgrp
!= -1 && term_pgrp
!= our_terminal_info
.process_group
)
575 kill (-term_pgrp
, SIGINT
);
581 /* Otherwise, pass the Ctrl-C to the first inferior that was resumed
582 in the foreground. */
583 for (inferior
*inf
: all_inferiors ())
585 if (inf
->terminal_state
!= target_terminal_state::is_ours
)
587 gdb_assert (inf
->pid
!= 0);
590 kill (inf
->pid
, SIGINT
);
596 /* If no inferior was resumed in the foreground, then how did the
597 !is_ours assert above pass? */
598 gdb_assert_not_reached ("no inferior resumed in the fg found");
601 /* Per-inferior data key. */
602 static const struct inferior_key
<terminal_info
> inflow_inferior_data
;
604 terminal_info::~terminal_info ()
609 /* Get the current svr4 data. If none is found yet, add it now. This
610 function always returns a valid object. */
612 static struct terminal_info
*
613 get_inflow_inferior_data (struct inferior
*inf
)
615 struct terminal_info
*info
;
617 info
= inflow_inferior_data
.get (inf
);
619 info
= inflow_inferior_data
.emplace (inf
);
624 /* This is a "inferior_exit" observer. Releases the TERMINAL_INFO member
625 of the inferior structure. This field is private to inflow.c, and
626 its type is opaque to the rest of GDB. PID is the target pid of
627 the inferior that is about to be removed from the inferior
631 inflow_inferior_exit (struct inferior
*inf
)
633 inf
->terminal_state
= target_terminal_state::is_ours
;
634 inflow_inferior_data
.clear (inf
);
638 copy_terminal_info (struct inferior
*to
, struct inferior
*from
)
640 struct terminal_info
*tinfo_to
, *tinfo_from
;
642 tinfo_to
= get_inflow_inferior_data (to
);
643 tinfo_from
= get_inflow_inferior_data (from
);
645 xfree (tinfo_to
->ttystate
);
647 *tinfo_to
= *tinfo_from
;
649 if (tinfo_from
->ttystate
)
651 = serial_copy_tty_state (stdin_serial
, tinfo_from
->ttystate
);
653 to
->terminal_state
= from
->terminal_state
;
656 /* See terminal.h. */
659 swap_terminal_info (inferior
*a
, inferior
*b
)
661 terminal_info
*info_a
= inflow_inferior_data
.get (a
);
662 terminal_info
*info_b
= inflow_inferior_data
.get (b
);
664 inflow_inferior_data
.set (a
, info_b
);
665 inflow_inferior_data
.set (b
, info_a
);
667 std::swap (a
->terminal_state
, b
->terminal_state
);
671 info_terminal_command (const char *arg
, int from_tty
)
673 target_terminal::info (arg
, from_tty
);
677 child_terminal_info (struct target_ops
*self
, const char *args
, int from_tty
)
679 struct inferior
*inf
;
680 struct terminal_info
*tinfo
;
682 if (!gdb_has_a_terminal ())
684 gdb_printf (_("This GDB does not control a terminal.\n"));
688 if (inferior_ptid
== null_ptid
)
691 inf
= current_inferior ();
692 tinfo
= get_inflow_inferior_data (inf
);
694 gdb_printf (_("Inferior's terminal status "
695 "(currently saved by GDB):\n"));
697 /* First the fcntl flags. */
701 flags
= tinfo
->tflags
;
703 gdb_printf ("File descriptor flags = ");
706 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
708 /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */
709 switch (flags
& (O_ACCMODE
))
712 gdb_printf ("O_RDONLY");
715 gdb_printf ("O_WRONLY");
718 gdb_printf ("O_RDWR");
721 flags
&= ~(O_ACCMODE
);
724 if (flags
& O_NONBLOCK
)
725 gdb_printf (" | O_NONBLOCK");
726 flags
&= ~O_NONBLOCK
;
729 #if defined (O_NDELAY)
730 /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
731 print it as O_NONBLOCK, which is good cause that is what POSIX
732 has, and the flag will already be cleared by the time we get here. */
733 if (flags
& O_NDELAY
)
734 gdb_printf (" | O_NDELAY");
738 if (flags
& O_APPEND
)
739 gdb_printf (" | O_APPEND");
742 #if defined (O_BINARY)
743 if (flags
& O_BINARY
)
744 gdb_printf (" | O_BINARY");
749 gdb_printf (" | 0x%x", flags
);
753 #ifdef HAVE_TERMIOS_H
754 gdb_printf ("Process group = %d\n", (int) tinfo
->process_group
);
757 serial_print_tty_state (stdin_serial
, tinfo
->ttystate
, gdb_stdout
);
760 /* NEW_TTY_PREFORK is called before forking a new child process,
761 so we can record the state of ttys in the child to be formed.
762 TTYNAME is empty if we are to share the terminal with gdb;
763 otherwise it contains the name of the desired tty.
765 NEW_TTY is called in new child processes under Unix, which will
766 become debugger target processes. This actually switches to
767 the terminal specified in the NEW_TTY_PREFORK call. */
770 new_tty_prefork (std::string ttyname
)
772 /* Save the name for later, for determining whether we and the child
773 are sharing a tty. */
774 inferior_thisrun_terminal
= std::move (ttyname
);
777 #if !defined(__GO32__) && !defined(_WIN32)
778 /* If RESULT, assumed to be the return value from a system call, is
779 negative, print the error message indicated by errno and exit.
780 MSG should identify the operation that failed. */
782 check_syscall (const char *msg
, int result
)
786 print_sys_errmsg (msg
, errno
);
795 if (inferior_thisrun_terminal
.empty ())
797 #if !defined(__GO32__) && !defined(_WIN32)
801 /* Disconnect the child process from our controlling terminal. On some
802 systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
804 tty
= open ("/dev/tty", O_RDWR
);
807 scoped_ignore_sigttou ignore_sigttou
;
809 ioctl (tty
, TIOCNOTTY
, 0);
814 /* Now open the specified new terminal. */
815 tty
= open (inferior_thisrun_terminal
.c_str (), O_RDWR
| O_NOCTTY
);
816 check_syscall (inferior_thisrun_terminal
.c_str (), tty
);
818 /* Avoid use of dup2; doesn't exist on all systems. */
822 check_syscall ("dup'ing tty into fd 0", dup (tty
));
827 check_syscall ("dup'ing tty into fd 1", dup (tty
));
832 check_syscall ("dup'ing tty into fd 2", dup (tty
));
836 /* Make tty our new controlling terminal. */
837 if (ioctl (tty
, TIOCSCTTY
, 0) == -1)
838 /* Mention GDB in warning because it will appear in the inferior's
839 terminal instead of GDB's. */
840 warning (_("GDB: Failed to set controlling terminal: %s"),
841 safe_strerror (errno
));
846 #endif /* !go32 && !win32 */
849 /* NEW_TTY_POSTFORK is called after forking a new child process, and
850 adding it to the inferior table, to store the TTYNAME being used by
851 the child, or empty if it sharing the terminal with gdb. */
854 new_tty_postfork (void)
856 /* Save the name for later, for determining whether we and the child
857 are sharing a tty. */
859 struct inferior
*inf
= current_inferior ();
860 struct terminal_info
*tinfo
= get_inflow_inferior_data (inf
);
862 tinfo
->run_terminal
= std::move (inferior_thisrun_terminal
);
863 inferior_thisrun_terminal
.clear ();
867 /* Call set_sigint_trap when you need to pass a signal on to an attached
868 process when handling SIGINT. */
871 pass_signal (int signo
)
874 kill (inferior_ptid
.pid (), SIGINT
);
878 static sighandler_t osig
;
882 set_sigint_trap (void)
884 struct inferior
*inf
= current_inferior ();
885 struct terminal_info
*tinfo
= get_inflow_inferior_data (inf
);
887 if (inf
->attach_flag
|| !tinfo
->run_terminal
.empty ())
889 osig
= signal (SIGINT
, pass_signal
);
897 clear_sigint_trap (void)
901 signal (SIGINT
, osig
);
907 /* Create a new session if the inferior will run in a different tty.
908 A session is UNIX's way of grouping processes that share a controlling
909 terminal, so a new one is needed if the inferior terminal will be
910 different from GDB's.
912 Returns the session id of the new session, 0 if no session was created
913 or -1 if an error occurred. */
915 create_tty_session (void)
920 if (!job_control
|| inferior_thisrun_terminal
.empty ())
925 warning (_("Failed to create new terminal session: setsid: %s"),
926 safe_strerror (errno
));
931 #endif /* HAVE_SETSID */
934 /* Get all the current tty settings (including whether we have a
935 tty at all!). We can't do this in _initialize_inflow because
936 serial_fdopen() won't work until the serial_ops_list is
937 initialized, but we don't want to do it lazily either, so
938 that we can guarantee stdin_serial is opened if there is
941 initialize_stdin_serial (void)
943 stdin_serial
= serial_fdopen (0);
946 void _initialize_inflow ();
948 _initialize_inflow ()
950 add_info ("terminal", info_terminal_command
,
951 _("Print inferior's saved terminal status."));
953 /* OK, figure out whether we have job control. */
956 gdb::observers::inferior_exit
.attach (inflow_inferior_exit
, "inflow");