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"
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 (int);
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 foreground
58 inferior when it resumes. */
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. */
67 serial_ttystate ttystate;
70 /* Process group. Saved and restored just like ttystate. */
74 /* fcntl flags. Saved and restored just like ttystate. */
78 /* Our own tty state, which we restore every time we need to deal with
79 the terminal. This is set once, when GDB first starts, and then
80 whenever we enter/leave TUI mode (gdb_save_tty_state). The
81 settings of flags which readline saves and restores are
83 static struct terminal_info our_terminal_info;
85 /* Snapshot of the initial tty state taken during initialization of
86 GDB, before readline/ncurses have had a chance to change it. This
87 is used as the initial tty state given to each new spawned
88 inferior. Unlike our_terminal_info, this is only ever set
90 static serial_ttystate initial_gdb_ttystate;
92 static struct terminal_info *get_inflow_inferior_data (struct inferior *);
94 /* RAII class used to ignore SIGTTOU in a scope. */
96 class scoped_ignore_sigttou
99 scoped_ignore_sigttou ()
103 m_osigttou = signal (SIGTTOU, SIG_IGN);
107 ~scoped_ignore_sigttou ()
111 signal (SIGTTOU, m_osigttou);
115 DISABLE_COPY_AND_ASSIGN (scoped_ignore_sigttou);
119 sighandler_t m_osigttou = NULL;
123 #ifdef HAVE_TERMIOS_H
125 /* Return the process group of the current inferior. */
128 inferior_process_group (void)
130 return get_inflow_inferior_data (current_inferior ())->process_group;
134 /* While the inferior is running, we want SIGINT and SIGQUIT to go to the
135 inferior only. If we have job control, that takes care of it. If not,
136 we save our handlers in these two variables and set SIGINT and SIGQUIT
139 static sighandler_t sigint_ours;
140 static sighandler_t sigquit_ours;
142 /* The name of the tty (from the `tty' command) that we're giving to
143 the inferior when starting it up. This is only (and should only
144 be) used as a transient global by new_tty_prefork,
145 create_tty_session, new_tty and new_tty_postfork, all called from
146 fork_inferior, while forking a new child. */
147 static const char *inferior_thisrun_terminal;
149 /* Nonzero if our terminal settings are in effect. Zero if the
150 inferior's settings are in effect. Ignored if !gdb_has_a_terminal
153 int terminal_is_ours;
155 /* See terminal.h. */
158 set_initial_gdb_ttystate (void)
160 /* Note we can't do any of this in _initialize_inflow because at
161 that point stdin_serial has not been created yet. */
163 initial_gdb_ttystate = serial_get_tty_state (stdin_serial);
165 if (initial_gdb_ttystate != NULL)
167 our_terminal_info.ttystate
168 = serial_copy_tty_state (stdin_serial, initial_gdb_ttystate);
170 our_terminal_info.tflags = fcntl (0, F_GETFL, 0);
172 #ifdef HAVE_TERMIOS_H
173 our_terminal_info.process_group = tcgetpgrp (0);
178 /* Does GDB have a terminal (on stdin)? */
181 gdb_has_a_terminal (void)
183 return initial_gdb_ttystate != NULL;
186 /* Macro for printing errors from ioctl operations */
188 #define OOPSY(what) \
190 fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
191 what, safe_strerror (errno))
193 /* Initialize the terminal settings we record for the inferior,
194 before we actually run the inferior. */
197 child_terminal_init (struct target_ops *self)
199 struct inferior *inf = current_inferior ();
200 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
202 #ifdef HAVE_TERMIOS_H
203 /* Store the process group even without a terminal as it is used not
204 only to reset the tty foreground process group, but also to
205 interrupt the inferior. A child we spawn should be a process
206 group leader (PGID==PID) at this point, though that may not be
207 true if we're attaching to an existing process. */
208 tinfo->process_group = inf->pid;
211 if (gdb_has_a_terminal ())
213 xfree (tinfo->ttystate);
214 tinfo->ttystate = serial_copy_tty_state (stdin_serial,
215 initial_gdb_ttystate);
217 /* Make sure that next time we call terminal_inferior (which will be
218 before the program runs, as it needs to be), we install the new
220 terminal_is_ours = 1;
224 /* Save the terminal settings again. This is necessary for the TUI
225 when it switches to TUI or non-TUI mode; curses changes the terminal
226 and gdb must be able to restore it correctly. */
229 gdb_save_tty_state (void)
231 if (gdb_has_a_terminal ())
233 xfree (our_terminal_info.ttystate);
234 our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);
238 /* Put the inferior's terminal settings into effect.
239 This is preparation for starting or resuming the inferior.
241 N.B. Targets that want to use this with async support must build that
242 support on top of this (e.g., the caller still needs to remove stdin
243 from the event loop). E.g., see linux_nat_terminal_inferior. */
246 child_terminal_inferior (struct target_ops *self)
248 struct inferior *inf;
249 struct terminal_info *tinfo;
251 if (!terminal_is_ours)
254 inf = current_inferior ();
255 tinfo = get_inflow_inferior_data (inf);
257 if (gdb_has_a_terminal ()
258 && tinfo->ttystate != NULL
259 && tinfo->run_terminal == NULL)
264 result = fcntl (0, F_SETFL, tinfo->tflags);
265 OOPSY ("fcntl F_SETFL");
268 result = serial_set_tty_state (stdin_serial, tinfo->ttystate);
269 OOPSY ("setting tty state");
273 sigint_ours = signal (SIGINT, SIG_IGN);
275 sigquit_ours = signal (SIGQUIT, SIG_IGN);
279 /* If attach_flag is set, we don't know whether we are sharing a
280 terminal with the inferior or not. (attaching a process
281 without a terminal is one case where we do not; attaching a
282 process which we ran from the same shell as GDB via `&' is
283 one case where we do, I think (but perhaps this is not
284 `sharing' in the sense that we need to save and restore tty
285 state)). I don't know if there is any way to tell whether we
286 are sharing a terminal. So what we do is to go through all
287 the saving and restoring of the tty state, but ignore errors
288 setting the process group, which will happen if we are not
289 sharing a terminal). */
293 #ifdef HAVE_TERMIOS_H
294 result = tcsetpgrp (0, tinfo->process_group);
295 if (!inf->attach_flag)
300 terminal_is_ours = 0;
303 /* Put some of our terminal settings into effect,
304 enough to get proper results from our output,
305 but do not change into or out of RAW mode
306 so that no input is discarded.
308 After doing this, either terminal_ours or terminal_inferior
309 should be called to get back to a normal state of affairs.
311 N.B. The implementation is (currently) no different than
312 child_terminal_ours. See child_terminal_ours_1. */
315 child_terminal_ours_for_output (struct target_ops *self)
317 child_terminal_ours_1 (1);
320 /* Put our terminal settings into effect.
321 First record the inferior's terminal settings
322 so they can be restored properly later.
324 N.B. Targets that want to use this with async support must build that
325 support on top of this (e.g., the caller still needs to add stdin to the
326 event loop). E.g., see linux_nat_terminal_ours. */
329 child_terminal_ours (struct target_ops *self)
331 child_terminal_ours_1 (0);
334 /* output_only is not used, and should not be used unless we introduce
335 separate terminal_is_ours and terminal_is_ours_for_output
339 child_terminal_ours_1 (int output_only)
341 struct inferior *inf;
342 struct terminal_info *tinfo;
344 if (terminal_is_ours)
347 terminal_is_ours = 1;
349 /* Checking inferior->run_terminal is necessary so that
350 if GDB is running in the background, it won't block trying
351 to do the ioctl()'s below. Checking gdb_has_a_terminal
352 avoids attempting all the ioctl's when running in batch. */
354 inf = current_inferior ();
355 tinfo = get_inflow_inferior_data (inf);
357 if (tinfo->run_terminal != NULL || gdb_has_a_terminal () == 0)
361 int result ATTRIBUTE_UNUSED;
363 /* Ignore SIGTTOU since it will happen when we try to set the
365 scoped_ignore_sigttou ignore_sigttou;
367 xfree (tinfo->ttystate);
368 tinfo->ttystate = serial_get_tty_state (stdin_serial);
370 #ifdef HAVE_TERMIOS_H
371 if (!inf->attach_flag)
372 /* If tcsetpgrp failed in terminal_inferior, this would give us
373 our process group instead of the inferior's. See
374 terminal_inferior for details. */
375 tinfo->process_group = tcgetpgrp (0);
378 /* Set tty state to our_ttystate. */
379 serial_set_tty_state (stdin_serial, our_terminal_info.ttystate);
383 #ifdef HAVE_TERMIOS_H
384 result = tcsetpgrp (0, our_terminal_info.process_group);
386 /* This fails on Ultrix with EINVAL if you run the testsuite
387 in the background with nohup, and then log out. GDB never
388 used to check for an error here, so perhaps there are other
389 such situations as well. */
391 fprintf_unfiltered (gdb_stderr,
392 "[tcsetpgrp failed in child_terminal_ours: %s]\n",
393 safe_strerror (errno));
400 signal (SIGINT, sigint_ours);
402 signal (SIGQUIT, sigquit_ours);
407 tinfo->tflags = fcntl (0, F_GETFL, 0);
408 result = fcntl (0, F_SETFL, our_terminal_info.tflags);
413 /* Per-inferior data key. */
414 static const struct inferior_data *inflow_inferior_data;
417 inflow_inferior_data_cleanup (struct inferior *inf, void *arg)
419 struct terminal_info *info = (struct terminal_info *) arg;
421 xfree (info->run_terminal);
422 xfree (info->ttystate);
426 /* Get the current svr4 data. If none is found yet, add it now. This
427 function always returns a valid object. */
429 static struct terminal_info *
430 get_inflow_inferior_data (struct inferior *inf)
432 struct terminal_info *info;
434 info = (struct terminal_info *) inferior_data (inf, inflow_inferior_data);
437 info = XCNEW (struct terminal_info);
438 set_inferior_data (inf, inflow_inferior_data, info);
444 /* This is a "inferior_exit" observer. Releases the TERMINAL_INFO member
445 of the inferior structure. This field is private to inflow.c, and
446 its type is opaque to the rest of GDB. PID is the target pid of
447 the inferior that is about to be removed from the inferior
451 inflow_inferior_exit (struct inferior *inf)
453 struct terminal_info *info;
455 info = (struct terminal_info *) inferior_data (inf, inflow_inferior_data);
458 xfree (info->run_terminal);
459 xfree (info->ttystate);
461 set_inferior_data (inf, inflow_inferior_data, NULL);
466 copy_terminal_info (struct inferior *to, struct inferior *from)
468 struct terminal_info *tinfo_to, *tinfo_from;
470 tinfo_to = get_inflow_inferior_data (to);
471 tinfo_from = get_inflow_inferior_data (from);
473 xfree (tinfo_to->run_terminal);
474 xfree (tinfo_to->ttystate);
476 *tinfo_to = *tinfo_from;
478 if (tinfo_from->run_terminal)
479 tinfo_to->run_terminal
480 = xstrdup (tinfo_from->run_terminal);
482 if (tinfo_from->ttystate)
484 = serial_copy_tty_state (stdin_serial, tinfo_from->ttystate);
488 info_terminal_command (const char *arg, int from_tty)
490 target_terminal::info (arg, from_tty);
494 child_terminal_info (struct target_ops *self, const char *args, int from_tty)
496 struct inferior *inf;
497 struct terminal_info *tinfo;
499 if (!gdb_has_a_terminal ())
501 printf_filtered (_("This GDB does not control a terminal.\n"));
505 if (ptid_equal (inferior_ptid, null_ptid))
508 inf = current_inferior ();
509 tinfo = get_inflow_inferior_data (inf);
511 printf_filtered (_("Inferior's terminal status "
512 "(currently saved by GDB):\n"));
514 /* First the fcntl flags. */
518 flags = tinfo->tflags;
520 printf_filtered ("File descriptor flags = ");
523 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
525 /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */
526 switch (flags & (O_ACCMODE))
529 printf_filtered ("O_RDONLY");
532 printf_filtered ("O_WRONLY");
535 printf_filtered ("O_RDWR");
538 flags &= ~(O_ACCMODE);
541 if (flags & O_NONBLOCK)
542 printf_filtered (" | O_NONBLOCK");
543 flags &= ~O_NONBLOCK;
546 #if defined (O_NDELAY)
547 /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
548 print it as O_NONBLOCK, which is good cause that is what POSIX
549 has, and the flag will already be cleared by the time we get here. */
550 if (flags & O_NDELAY)
551 printf_filtered (" | O_NDELAY");
555 if (flags & O_APPEND)
556 printf_filtered (" | O_APPEND");
559 #if defined (O_BINARY)
560 if (flags & O_BINARY)
561 printf_filtered (" | O_BINARY");
566 printf_filtered (" | 0x%x", flags);
567 printf_filtered ("\n");
570 #ifdef HAVE_TERMIOS_H
571 printf_filtered ("Process group = %d\n", (int) tinfo->process_group);
574 serial_print_tty_state (stdin_serial, tinfo->ttystate, gdb_stdout);
577 /* NEW_TTY_PREFORK is called before forking a new child process,
578 so we can record the state of ttys in the child to be formed.
579 TTYNAME is null if we are to share the terminal with gdb;
580 or points to a string containing the name of the desired tty.
582 NEW_TTY is called in new child processes under Unix, which will
583 become debugger target processes. This actually switches to
584 the terminal specified in the NEW_TTY_PREFORK call. */
587 new_tty_prefork (const char *ttyname)
589 /* Save the name for later, for determining whether we and the child
590 are sharing a tty. */
591 inferior_thisrun_terminal = ttyname;
594 #if !defined(__GO32__) && !defined(_WIN32)
595 /* If RESULT, assumed to be the return value from a system call, is
596 negative, print the error message indicated by errno and exit.
597 MSG should identify the operation that failed. */
599 check_syscall (const char *msg, int result)
603 print_sys_errmsg (msg, errno);
614 if (inferior_thisrun_terminal == 0)
616 #if !defined(__GO32__) && !defined(_WIN32)
618 /* Disconnect the child process from our controlling terminal. On some
619 systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
621 tty = open ("/dev/tty", O_RDWR);
624 scoped_ignore_sigttou ignore_sigttou;
626 ioctl (tty, TIOCNOTTY, 0);
631 /* Now open the specified new terminal. */
632 tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
633 check_syscall (inferior_thisrun_terminal, tty);
635 /* Avoid use of dup2; doesn't exist on all systems. */
639 check_syscall ("dup'ing tty into fd 0", dup (tty));
644 check_syscall ("dup'ing tty into fd 1", dup (tty));
649 check_syscall ("dup'ing tty into fd 2", dup (tty));
653 /* Make tty our new controlling terminal. */
654 if (ioctl (tty, TIOCSCTTY, 0) == -1)
655 /* Mention GDB in warning because it will appear in the inferior's
656 terminal instead of GDB's. */
657 warning (_("GDB: Failed to set controlling terminal: %s"),
658 safe_strerror (errno));
663 #endif /* !go32 && !win32 */
666 /* NEW_TTY_POSTFORK is called after forking a new child process, and
667 adding it to the inferior table, to store the TTYNAME being used by
668 the child, or null if it sharing the terminal with gdb. */
671 new_tty_postfork (void)
673 /* Save the name for later, for determining whether we and the child
674 are sharing a tty. */
676 if (inferior_thisrun_terminal)
678 struct inferior *inf = current_inferior ();
679 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
681 tinfo->run_terminal = xstrdup (inferior_thisrun_terminal);
684 inferior_thisrun_terminal = NULL;
688 /* Call set_sigint_trap when you need to pass a signal on to an attached
689 process when handling SIGINT. */
692 pass_signal (int signo)
695 kill (ptid_get_pid (inferior_ptid), SIGINT);
699 static sighandler_t osig;
703 set_sigint_trap (void)
705 struct inferior *inf = current_inferior ();
706 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
708 if (inf->attach_flag || tinfo->run_terminal)
710 osig = signal (SIGINT, pass_signal);
718 clear_sigint_trap (void)
722 signal (SIGINT, osig);
728 /* Create a new session if the inferior will run in a different tty.
729 A session is UNIX's way of grouping processes that share a controlling
730 terminal, so a new one is needed if the inferior terminal will be
731 different from GDB's.
733 Returns the session id of the new session, 0 if no session was created
734 or -1 if an error occurred. */
736 create_tty_session (void)
741 if (!job_control || inferior_thisrun_terminal == 0)
746 warning (_("Failed to create new terminal session: setsid: %s"),
747 safe_strerror (errno));
752 #endif /* HAVE_SETSID */
755 /* Get all the current tty settings (including whether we have a
756 tty at all!). We can't do this in _initialize_inflow because
757 serial_fdopen() won't work until the serial_ops_list is
758 initialized, but we don't want to do it lazily either, so
759 that we can guarantee stdin_serial is opened if there is
762 initialize_stdin_serial (void)
764 stdin_serial = serial_fdopen (0);
768 _initialize_inflow (void)
770 add_info ("terminal", info_terminal_command,
771 _("Print inferior's saved terminal status."));
773 terminal_is_ours = 1;
775 /* OK, figure out whether we have job control. */
778 observer_attach_inferior_exit (inflow_inferior_exit);
781 = register_inferior_data_with_cleanup (NULL, inflow_inferior_data_cleanup);