1 /* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1986-2018 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 "gdb_select.h"
37 #include "job-control.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 /* The name of the tty (from the `tty' command) that we gave to the
62 inferior when it was started. */
65 /* TTY state. We save it whenever the inferior stops, and restore
66 it when it resumes in the foreground. */
67 serial_ttystate ttystate;
70 /* The terminal's foreground process group. Saved whenever the
71 inferior stops. This is the pgrp displayed by "info terminal".
72 Note that this may be not the inferior's actual process group,
73 since each inferior that we spawn has its own process group, and
74 only one can be in the foreground at a time. When the inferior
75 resumes, if we can determine the inferior's actual pgrp, then we
76 make that the foreground pgrp instead of what was saved here.
77 While it's a bit arbitrary which inferior's pgrp ends up in the
78 foreground when we resume several inferiors, this at least makes
79 'resume inf1+inf2' + 'stop all' + 'resume inf2' end up with
80 inf2's pgrp in the foreground instead of inf1's (which would be
81 problematic since it would be left stopped: Ctrl-C wouldn't work,
86 /* fcntl flags. Saved and restored just like ttystate. */
90 /* Our own tty state, which we restore every time we need to deal with
91 the terminal. This is set once, when GDB first starts, and then
92 whenever we enter/leave TUI mode (gdb_save_tty_state). The
93 settings of flags which readline saves and restores are
95 static struct terminal_info our_terminal_info;
97 /* Snapshot of the initial tty state taken during initialization of
98 GDB, before readline/ncurses have had a chance to change it. This
99 is used as the initial tty state given to each new spawned
100 inferior. Unlike our_terminal_info, this is only ever set
102 static serial_ttystate initial_gdb_ttystate;
104 static struct terminal_info *get_inflow_inferior_data (struct inferior *);
106 /* RAII class used to ignore SIGTTOU in a scope. */
108 class scoped_ignore_sigttou
111 scoped_ignore_sigttou ()
115 m_osigttou = signal (SIGTTOU, SIG_IGN);
119 ~scoped_ignore_sigttou ()
123 signal (SIGTTOU, m_osigttou);
127 DISABLE_COPY_AND_ASSIGN (scoped_ignore_sigttou);
131 sighandler_t m_osigttou = NULL;
135 /* While the inferior is running, we want SIGINT and SIGQUIT to go to the
136 inferior only. If we have job control, that takes care of it. If not,
137 we save our handlers in these two variables and set SIGINT and SIGQUIT
140 static sighandler_t sigint_ours;
142 static sighandler_t sigquit_ours;
145 /* The name of the tty (from the `tty' command) that we're giving to
146 the inferior when starting it up. This is only (and should only
147 be) used as a transient global by new_tty_prefork,
148 create_tty_session, new_tty and new_tty_postfork, all called from
149 fork_inferior, while forking a new child. */
150 static const char *inferior_thisrun_terminal;
152 /* Track who owns GDB's terminal (is it GDB or some inferior?). While
153 target_terminal::is_ours() etc. tracks the core's intention and is
154 independent of the target backend, this tracks the actual state of
155 GDB's own tty. So for example,
157 (target_terminal::is_inferior () && gdb_tty_state == terminal_is_ours)
159 is true when the (native) inferior is not sharing a terminal with
160 GDB (e.g., because we attached to an inferior that is running on a
161 different terminal). */
162 static target_terminal_state gdb_tty_state = target_terminal_state::is_ours;
164 /* See terminal.h. */
167 set_initial_gdb_ttystate (void)
169 /* Note we can't do any of this in _initialize_inflow because at
170 that point stdin_serial has not been created yet. */
172 initial_gdb_ttystate = serial_get_tty_state (stdin_serial);
174 if (initial_gdb_ttystate != NULL)
176 our_terminal_info.ttystate
177 = serial_copy_tty_state (stdin_serial, initial_gdb_ttystate);
179 our_terminal_info.tflags = fcntl (0, F_GETFL, 0);
181 #ifdef HAVE_TERMIOS_H
182 our_terminal_info.process_group = tcgetpgrp (0);
187 /* Does GDB have a terminal (on stdin)? */
190 gdb_has_a_terminal (void)
192 return initial_gdb_ttystate != NULL;
195 /* Macro for printing errors from ioctl operations */
197 #define OOPSY(what) \
199 fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
200 what, safe_strerror (errno))
202 /* Initialize the terminal settings we record for the inferior,
203 before we actually run the inferior. */
206 child_terminal_init (struct target_ops *self)
208 if (!gdb_has_a_terminal ())
211 inferior *inf = current_inferior ();
212 terminal_info *tinfo = get_inflow_inferior_data (inf);
214 #ifdef HAVE_TERMIOS_H
215 /* A child we spawn should be a process group leader (PGID==PID) at
216 this point, though that may not be true if we're attaching to an
218 tinfo->process_group = inf->pid;
221 xfree (tinfo->ttystate);
222 tinfo->ttystate = serial_copy_tty_state (stdin_serial, initial_gdb_ttystate);
225 /* Save the terminal settings again. This is necessary for the TUI
226 when it switches to TUI or non-TUI mode; curses changes the terminal
227 and gdb must be able to restore it correctly. */
230 gdb_save_tty_state (void)
232 if (gdb_has_a_terminal ())
234 xfree (our_terminal_info.ttystate);
235 our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);
239 /* Try to determine whether TTY is GDB's input terminal. Returns
240 TRIBOOL_UNKNOWN if we can't tell. */
243 is_gdb_terminal (const char *tty)
246 struct stat other_tty;
249 res = stat (tty, &other_tty);
251 return TRIBOOL_UNKNOWN;
253 res = fstat (STDIN_FILENO, &gdb_tty);
255 return TRIBOOL_UNKNOWN;
257 return ((gdb_tty.st_dev == other_tty.st_dev
258 && gdb_tty.st_ino == other_tty.st_ino)
263 /* Helper for sharing_input_terminal. Try to determine whether
264 inferior INF is using the same TTY for input as GDB is. Returns
265 TRIBOOL_UNKNOWN if we can't tell. */
268 sharing_input_terminal_1 (inferior *inf)
270 /* Using host-dependent code here is fine, because the
271 child_terminal_foo functions are meant to be used by child/native
273 #if defined (__linux__) || defined (__sun__)
276 xsnprintf (buf, sizeof (buf), "/proc/%d/fd/0", inf->pid);
277 return is_gdb_terminal (buf);
279 return TRIBOOL_UNKNOWN;
283 /* Return true if the inferior is using the same TTY for input as GDB
284 is. If this is true, then we save/restore terminal flags/state.
286 This is necessary because if inf->attach_flag is set, we don't
287 offhand know whether we are sharing a terminal with the inferior or
288 not. Attaching a process without a terminal is one case where we
289 do not; attaching a process which we ran from the same shell as GDB
290 via `&' is one case where we do.
292 If we can't determine, we assume the TTY is being shared. This
293 works OK if you're only debugging one inferior. However, if you're
294 debugging more than one inferior, and e.g., one is spawned by GDB
295 with "run" (sharing terminal with GDB), and another is attached to
296 (and running on a different terminal, as is most common), then it
297 matters, because we can only restore the terminal settings of one
298 of the inferiors, and in that scenario, we want to restore the
299 settings of the "run"'ed inferior.
301 Note, this is not the same as determining whether GDB and the
302 inferior are in the same session / connected to the same
303 controlling tty. An inferior (fork child) may call setsid,
304 disconnecting itself from the ctty, while still leaving
305 stdin/stdout/stderr associated with the original terminal. If
306 we're debugging that process, we should also save/restore terminal
310 sharing_input_terminal (inferior *inf)
312 terminal_info *tinfo = get_inflow_inferior_data (inf);
314 tribool res = sharing_input_terminal_1 (inf);
316 if (res == TRIBOOL_UNKNOWN)
318 /* As fallback, if we can't determine by stat'ing the inferior's
319 tty directly (because it's not supported on this host) and
320 the child was spawned, check whether run_terminal is our tty.
321 This isn't ideal, since this is checking the child's
322 controlling terminal, not the input terminal (which may have
323 been redirected), but is still better than nothing. A false
324 positive ("set inferior-tty" points to our terminal, but I/O
325 was redirected) is much more likely than a false negative
326 ("set inferior-tty" points to some other terminal, and then
327 output was redirected to our terminal), and with a false
328 positive we just end up trying to save/restore terminal
329 settings when we didn't need to or we actually can't. */
330 if (tinfo->run_terminal != NULL)
331 res = is_gdb_terminal (tinfo->run_terminal);
333 /* If we still can't determine, assume yes. */
334 if (res == TRIBOOL_UNKNOWN)
338 return res == TRIBOOL_TRUE;
341 /* Put the inferior's terminal settings into effect. This is
342 preparation for starting or resuming the inferior. */
345 child_terminal_inferior (struct target_ops *self)
347 /* If we resume more than one inferior in the foreground on GDB's
348 terminal, then the first inferior's terminal settings "win".
349 Note that every child process is put in its own process group, so
350 the first process that ends up resumed ends up determining which
351 process group the kernel forwards Ctrl-C/Ctrl-Z (SIGINT/SIGTTOU)
353 if (gdb_tty_state == target_terminal_state::is_inferior)
356 inferior *inf = current_inferior ();
357 terminal_info *tinfo = get_inflow_inferior_data (inf);
359 if (gdb_has_a_terminal ()
360 && tinfo->ttystate != NULL
361 && sharing_input_terminal (inf))
365 /* Ignore SIGTTOU since it will happen when we try to set the
366 terminal's state (if gdb_tty_state is currently
368 scoped_ignore_sigttou ignore_sigttou;
371 result = fcntl (0, F_SETFL, tinfo->tflags);
372 OOPSY ("fcntl F_SETFL");
375 result = serial_set_tty_state (stdin_serial, tinfo->ttystate);
376 OOPSY ("setting tty state");
380 sigint_ours = signal (SIGINT, SIG_IGN);
382 sigquit_ours = signal (SIGQUIT, SIG_IGN);
388 #ifdef HAVE_TERMIOS_H
389 /* If we can't tell the inferior's actual process group,
390 then restore whatever was the foreground pgrp the last
391 time the inferior was running. See also comments
392 describing terminal_state::process_group. */
394 result = tcsetpgrp (0, getpgid (inf->pid));
396 result = tcsetpgrp (0, tinfo->process_group);
401 /* This fails if either GDB has no controlling terminal,
402 e.g., running under 'setsid(1)', or if the inferior
403 is not attached to GDB's controlling terminal. E.g.,
404 if it called setsid to create a new session or used
405 the TIOCNOTTY ioctl, or simply if we've attached to a
406 process running on another terminal and we couldn't
407 tell whether it was sharing GDB's terminal (and so
411 "[tcsetpgrp failed in child_terminal_inferior: %s]\n",
412 safe_strerror (errno));
418 gdb_tty_state = target_terminal_state::is_inferior;
422 /* Put some of our terminal settings into effect,
423 enough to get proper results from our output,
424 but do not change into or out of RAW mode
425 so that no input is discarded.
427 After doing this, either terminal_ours or terminal_inferior
428 should be called to get back to a normal state of affairs.
430 N.B. The implementation is (currently) no different than
431 child_terminal_ours. See child_terminal_ours_1. */
434 child_terminal_ours_for_output (struct target_ops *self)
436 child_terminal_ours_1 (target_terminal_state::is_ours_for_output);
439 /* Put our terminal settings into effect.
440 First record the inferior's terminal settings
441 so they can be restored properly later.
443 N.B. Targets that want to use this with async support must build that
444 support on top of this (e.g., the caller still needs to add stdin to the
445 event loop). E.g., see linux_nat_terminal_ours. */
448 child_terminal_ours (struct target_ops *self)
450 child_terminal_ours_1 (target_terminal_state::is_ours);
453 /* Save the current terminal settings in the inferior's terminal_info
457 child_terminal_save_inferior (struct target_ops *self)
459 /* Avoid attempting all the ioctl's when running in batch. */
460 if (!gdb_has_a_terminal ())
463 inferior *inf = current_inferior ();
464 terminal_info *tinfo = get_inflow_inferior_data (inf);
466 /* No need to save/restore if the inferior is not sharing GDB's
468 if (!sharing_input_terminal (inf))
471 xfree (tinfo->ttystate);
472 tinfo->ttystate = serial_get_tty_state (stdin_serial);
474 #ifdef HAVE_TERMIOS_H
475 tinfo->process_group = tcgetpgrp (0);
479 tinfo->tflags = fcntl (0, F_GETFL, 0);
483 /* Switch terminal state to DESIRED_STATE, either is_ours, or
484 is_ours_for_output. */
487 child_terminal_ours_1 (target_terminal_state desired_state)
489 gdb_assert (desired_state != target_terminal_state::is_inferior);
491 /* Avoid attempting all the ioctl's when running in batch. */
492 if (!gdb_has_a_terminal ())
495 if (gdb_tty_state != desired_state)
497 int result ATTRIBUTE_UNUSED;
499 /* Ignore SIGTTOU since it will happen when we try to set the
501 scoped_ignore_sigttou ignore_sigttou;
503 /* Set tty state to our_ttystate. */
504 serial_set_tty_state (stdin_serial, our_terminal_info.ttystate);
506 /* If we only want output, then leave the inferior's pgrp in the
507 foreground, so that Ctrl-C/Ctrl-Z reach the inferior
509 if (job_control && desired_state == target_terminal_state::is_ours)
511 #ifdef HAVE_TERMIOS_H
512 result = tcsetpgrp (0, our_terminal_info.process_group);
514 /* This fails on Ultrix with EINVAL if you run the testsuite
515 in the background with nohup, and then log out. GDB never
516 used to check for an error here, so perhaps there are other
517 such situations as well. */
519 fprintf_unfiltered (gdb_stderr,
520 "[tcsetpgrp failed in child_terminal_ours: %s]\n",
521 safe_strerror (errno));
526 if (!job_control && desired_state == target_terminal_state::is_ours)
528 signal (SIGINT, sigint_ours);
530 signal (SIGQUIT, sigquit_ours);
535 result = fcntl (0, F_SETFL, our_terminal_info.tflags);
538 gdb_tty_state = desired_state;
542 /* Interrupt the inferior. Implementation of target_interrupt for
543 child/native targets. */
546 child_interrupt (struct target_ops *self)
548 /* Interrupt the first inferior that has a resumed thread. */
550 thread_info *resumed = NULL;
551 ALL_NON_EXITED_THREADS (thr)
558 if (thr->suspend.waitstatus_pending_p)
564 /* Note that unlike pressing Ctrl-C on the controlling terminal,
565 here we only interrupt one process, not the whole process
566 group. This is because interrupting a process group (with
567 either Ctrl-C or with kill(3) with negative PID) sends a
568 SIGINT to each process in the process group, and we may not
569 be debugging all processes in the process group. */
571 kill (resumed->inf->pid, SIGINT);
576 /* Pass a Ctrl-C to the inferior as-if a Ctrl-C was pressed while the
577 inferior was in the foreground. Implementation of
578 target_pass_ctrlc for child/native targets. */
581 child_pass_ctrlc (struct target_ops *self)
583 gdb_assert (!target_terminal::is_ours ());
585 #ifdef HAVE_TERMIOS_H
588 pid_t term_pgrp = tcgetpgrp (0);
590 /* If there's any inferior sharing our terminal, pass the SIGINT
591 to the terminal's foreground process group. This acts just
592 like the user typed a ^C on the terminal while the inferior
593 was in the foreground. Note that using a negative process
594 number in kill() is a System V-ism. The proper BSD interface
595 is killpg(). However, all modern BSDs support the System V
598 if (term_pgrp != -1 && term_pgrp != our_terminal_info.process_group)
600 kill (-term_pgrp, SIGINT);
606 /* Otherwise, pass the Ctrl-C to the first inferior that was resumed
607 in the foreground. */
611 if (inf->terminal_state != target_terminal_state::is_ours)
613 gdb_assert (inf->pid != 0);
616 kill (inf->pid, SIGINT);
622 /* If no inferior was resumed in the foreground, then how did the
623 !is_ours assert above pass? */
624 gdb_assert_not_reached ("no inferior resumed in the fg found");
627 /* Per-inferior data key. */
628 static const struct inferior_data *inflow_inferior_data;
631 inflow_inferior_data_cleanup (struct inferior *inf, void *arg)
633 struct terminal_info *info = (struct terminal_info *) arg;
635 xfree (info->run_terminal);
636 xfree (info->ttystate);
640 /* Get the current svr4 data. If none is found yet, add it now. This
641 function always returns a valid object. */
643 static struct terminal_info *
644 get_inflow_inferior_data (struct inferior *inf)
646 struct terminal_info *info;
648 info = (struct terminal_info *) inferior_data (inf, inflow_inferior_data);
651 info = XCNEW (struct terminal_info);
652 set_inferior_data (inf, inflow_inferior_data, info);
658 /* This is a "inferior_exit" observer. Releases the TERMINAL_INFO member
659 of the inferior structure. This field is private to inflow.c, and
660 its type is opaque to the rest of GDB. PID is the target pid of
661 the inferior that is about to be removed from the inferior
665 inflow_inferior_exit (struct inferior *inf)
667 struct terminal_info *info;
669 inf->terminal_state = target_terminal_state::is_ours;
671 info = (struct terminal_info *) inferior_data (inf, inflow_inferior_data);
674 xfree (info->run_terminal);
675 xfree (info->ttystate);
677 set_inferior_data (inf, inflow_inferior_data, NULL);
682 copy_terminal_info (struct inferior *to, struct inferior *from)
684 struct terminal_info *tinfo_to, *tinfo_from;
686 tinfo_to = get_inflow_inferior_data (to);
687 tinfo_from = get_inflow_inferior_data (from);
689 xfree (tinfo_to->run_terminal);
690 xfree (tinfo_to->ttystate);
692 *tinfo_to = *tinfo_from;
694 if (tinfo_from->run_terminal)
695 tinfo_to->run_terminal
696 = xstrdup (tinfo_from->run_terminal);
698 if (tinfo_from->ttystate)
700 = serial_copy_tty_state (stdin_serial, tinfo_from->ttystate);
702 to->terminal_state = from->terminal_state;
705 /* See terminal.h. */
708 swap_terminal_info (inferior *a, inferior *b)
710 terminal_info *info_a
711 = (terminal_info *) inferior_data (a, inflow_inferior_data);
712 terminal_info *info_b
713 = (terminal_info *) inferior_data (a, inflow_inferior_data);
715 set_inferior_data (a, inflow_inferior_data, info_b);
716 set_inferior_data (b, inflow_inferior_data, info_a);
718 std::swap (a->terminal_state, b->terminal_state);
722 info_terminal_command (const char *arg, int from_tty)
724 target_terminal::info (arg, from_tty);
728 child_terminal_info (struct target_ops *self, const char *args, int from_tty)
730 struct inferior *inf;
731 struct terminal_info *tinfo;
733 if (!gdb_has_a_terminal ())
735 printf_filtered (_("This GDB does not control a terminal.\n"));
739 if (inferior_ptid == null_ptid)
742 inf = current_inferior ();
743 tinfo = get_inflow_inferior_data (inf);
745 printf_filtered (_("Inferior's terminal status "
746 "(currently saved by GDB):\n"));
748 /* First the fcntl flags. */
752 flags = tinfo->tflags;
754 printf_filtered ("File descriptor flags = ");
757 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
759 /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */
760 switch (flags & (O_ACCMODE))
763 printf_filtered ("O_RDONLY");
766 printf_filtered ("O_WRONLY");
769 printf_filtered ("O_RDWR");
772 flags &= ~(O_ACCMODE);
775 if (flags & O_NONBLOCK)
776 printf_filtered (" | O_NONBLOCK");
777 flags &= ~O_NONBLOCK;
780 #if defined (O_NDELAY)
781 /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
782 print it as O_NONBLOCK, which is good cause that is what POSIX
783 has, and the flag will already be cleared by the time we get here. */
784 if (flags & O_NDELAY)
785 printf_filtered (" | O_NDELAY");
789 if (flags & O_APPEND)
790 printf_filtered (" | O_APPEND");
793 #if defined (O_BINARY)
794 if (flags & O_BINARY)
795 printf_filtered (" | O_BINARY");
800 printf_filtered (" | 0x%x", flags);
801 printf_filtered ("\n");
804 #ifdef HAVE_TERMIOS_H
805 printf_filtered ("Process group = %d\n", (int) tinfo->process_group);
808 serial_print_tty_state (stdin_serial, tinfo->ttystate, gdb_stdout);
811 /* NEW_TTY_PREFORK is called before forking a new child process,
812 so we can record the state of ttys in the child to be formed.
813 TTYNAME is null if we are to share the terminal with gdb;
814 or points to a string containing the name of the desired tty.
816 NEW_TTY is called in new child processes under Unix, which will
817 become debugger target processes. This actually switches to
818 the terminal specified in the NEW_TTY_PREFORK call. */
821 new_tty_prefork (const char *ttyname)
823 /* Save the name for later, for determining whether we and the child
824 are sharing a tty. */
825 inferior_thisrun_terminal = ttyname;
828 #if !defined(__GO32__) && !defined(_WIN32)
829 /* If RESULT, assumed to be the return value from a system call, is
830 negative, print the error message indicated by errno and exit.
831 MSG should identify the operation that failed. */
833 check_syscall (const char *msg, int result)
837 print_sys_errmsg (msg, errno);
846 if (inferior_thisrun_terminal == 0)
848 #if !defined(__GO32__) && !defined(_WIN32)
852 /* Disconnect the child process from our controlling terminal. On some
853 systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
855 tty = open ("/dev/tty", O_RDWR);
858 scoped_ignore_sigttou ignore_sigttou;
860 ioctl (tty, TIOCNOTTY, 0);
865 /* Now open the specified new terminal. */
866 tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
867 check_syscall (inferior_thisrun_terminal, tty);
869 /* Avoid use of dup2; doesn't exist on all systems. */
873 check_syscall ("dup'ing tty into fd 0", dup (tty));
878 check_syscall ("dup'ing tty into fd 1", dup (tty));
883 check_syscall ("dup'ing tty into fd 2", dup (tty));
887 /* Make tty our new controlling terminal. */
888 if (ioctl (tty, TIOCSCTTY, 0) == -1)
889 /* Mention GDB in warning because it will appear in the inferior's
890 terminal instead of GDB's. */
891 warning (_("GDB: Failed to set controlling terminal: %s"),
892 safe_strerror (errno));
897 #endif /* !go32 && !win32 */
900 /* NEW_TTY_POSTFORK is called after forking a new child process, and
901 adding it to the inferior table, to store the TTYNAME being used by
902 the child, or null if it sharing the terminal with gdb. */
905 new_tty_postfork (void)
907 /* Save the name for later, for determining whether we and the child
908 are sharing a tty. */
910 if (inferior_thisrun_terminal)
912 struct inferior *inf = current_inferior ();
913 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
915 tinfo->run_terminal = xstrdup (inferior_thisrun_terminal);
918 inferior_thisrun_terminal = NULL;
922 /* Call set_sigint_trap when you need to pass a signal on to an attached
923 process when handling SIGINT. */
926 pass_signal (int signo)
929 kill (inferior_ptid.pid (), SIGINT);
933 static sighandler_t osig;
937 set_sigint_trap (void)
939 struct inferior *inf = current_inferior ();
940 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
942 if (inf->attach_flag || tinfo->run_terminal)
944 osig = signal (SIGINT, pass_signal);
952 clear_sigint_trap (void)
956 signal (SIGINT, osig);
962 /* Create a new session if the inferior will run in a different tty.
963 A session is UNIX's way of grouping processes that share a controlling
964 terminal, so a new one is needed if the inferior terminal will be
965 different from GDB's.
967 Returns the session id of the new session, 0 if no session was created
968 or -1 if an error occurred. */
970 create_tty_session (void)
975 if (!job_control || inferior_thisrun_terminal == 0)
980 warning (_("Failed to create new terminal session: setsid: %s"),
981 safe_strerror (errno));
986 #endif /* HAVE_SETSID */
989 /* Get all the current tty settings (including whether we have a
990 tty at all!). We can't do this in _initialize_inflow because
991 serial_fdopen() won't work until the serial_ops_list is
992 initialized, but we don't want to do it lazily either, so
993 that we can guarantee stdin_serial is opened if there is
996 initialize_stdin_serial (void)
998 stdin_serial = serial_fdopen (0);
1002 _initialize_inflow (void)
1004 add_info ("terminal", info_terminal_command,
1005 _("Print inferior's saved terminal status."));
1007 /* OK, figure out whether we have job control. */
1008 have_job_control ();
1010 gdb::observers::inferior_exit.attach (inflow_inferior_exit);
1012 inflow_inferior_data
1013 = register_inferior_data_with_cleanup (NULL, inflow_inferior_data_cleanup);