1 /* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1986-2014 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"
32 #include "gdb_select.h"
37 #ifdef HAVE_SYS_IOCTL_H
38 #include <sys/ioctl.h>
45 extern void _initialize_inflow (void);
47 static void pass_signal (int);
49 static void 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
;
69 #ifdef PROCESS_GROUP_TYPE
70 /* Process group. Saved and restored just like ttystate. */
71 PROCESS_GROUP_TYPE process_group
;
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 only set once, when GDB first starts. The
80 settings of flags which readline saves and restores and
82 static struct terminal_info our_terminal_info
;
84 static struct terminal_info
*get_inflow_inferior_data (struct inferior
*);
86 #ifdef PROCESS_GROUP_TYPE
88 /* Return the process group of the current inferior. */
91 inferior_process_group (void)
93 return get_inflow_inferior_data (current_inferior ())->process_group
;
97 /* While the inferior is running, we want SIGINT and SIGQUIT to go to the
98 inferior only. If we have job control, that takes care of it. If not,
99 we save our handlers in these two variables and set SIGINT and SIGQUIT
102 static void (*sigint_ours
) ();
103 static void (*sigquit_ours
) ();
105 /* The name of the tty (from the `tty' command) that we're giving to
106 the inferior when starting it up. This is only (and should only
107 be) used as a transient global by new_tty_prefork,
108 create_tty_session, new_tty and new_tty_postfork, all called from
109 fork_inferior, while forking a new child. */
110 static const char *inferior_thisrun_terminal
;
112 /* Nonzero if our terminal settings are in effect. Zero if the
113 inferior's settings are in effect. Ignored if !gdb_has_a_terminal
116 int terminal_is_ours
;
118 #ifdef PROCESS_GROUP_TYPE
119 static PROCESS_GROUP_TYPE
122 int process_group
= -1;
125 process_group
= tcgetpgrp (0);
128 process_group
= getpgrp ();
131 ioctl (0, TIOCGPGRP
, &process_group
);
133 return process_group
;
139 yes
, no
, have_not_checked
141 gdb_has_a_terminal_flag
= have_not_checked
;
143 /* The value of the "interactive-mode" setting. */
144 static enum auto_boolean interactive_mode
= AUTO_BOOLEAN_AUTO
;
146 /* Implement the "show interactive-mode" option. */
149 show_interactive_mode (struct ui_file
*file
, int from_tty
,
150 struct cmd_list_element
*c
,
153 if (interactive_mode
== AUTO_BOOLEAN_AUTO
)
154 fprintf_filtered (file
, "Debugger's interactive mode "
155 "is %s (currently %s).\n",
156 value
, gdb_has_a_terminal () ? "on" : "off");
158 fprintf_filtered (file
, "Debugger's interactive mode is %s.\n", value
);
161 /* Does GDB have a terminal (on stdin)? */
163 gdb_has_a_terminal (void)
165 if (interactive_mode
!= AUTO_BOOLEAN_AUTO
)
166 return interactive_mode
== AUTO_BOOLEAN_TRUE
;
168 switch (gdb_has_a_terminal_flag
)
174 case have_not_checked
:
175 /* Get all the current tty settings (including whether we have a
176 tty at all!). Can't do this in _initialize_inflow because
177 serial_fdopen() won't work until the serial_ops_list is
181 our_terminal_info
.tflags
= fcntl (0, F_GETFL
, 0);
184 gdb_has_a_terminal_flag
= no
;
185 if (stdin_serial
!= NULL
)
187 our_terminal_info
.ttystate
= serial_get_tty_state (stdin_serial
);
189 if (our_terminal_info
.ttystate
!= NULL
)
191 gdb_has_a_terminal_flag
= yes
;
192 #ifdef PROCESS_GROUP_TYPE
193 our_terminal_info
.process_group
= gdb_getpgrp ();
198 return gdb_has_a_terminal_flag
== yes
;
200 /* "Can't happen". */
205 /* Macro for printing errors from ioctl operations */
207 #define OOPSY(what) \
209 fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
210 what, safe_strerror (errno))
212 static void terminal_ours_1 (int);
214 /* Initialize the terminal settings we record for the inferior,
215 before we actually run the inferior. */
218 terminal_init_inferior_with_pgrp (int pgrp
)
220 struct inferior
*inf
= current_inferior ();
221 struct terminal_info
*tinfo
= get_inflow_inferior_data (inf
);
223 #ifdef PROCESS_GROUP_TYPE
224 /* Store the process group even without a terminal as it is used not
225 only to reset the tty foreground process group, but also to
226 interrupt the inferior. */
227 tinfo
->process_group
= pgrp
;
230 if (gdb_has_a_terminal ())
232 xfree (tinfo
->ttystate
);
233 tinfo
->ttystate
= serial_copy_tty_state (stdin_serial
,
234 our_terminal_info
.ttystate
);
236 /* Make sure that next time we call terminal_inferior (which will be
237 before the program runs, as it needs to be), we install the new
239 terminal_is_ours
= 1;
243 /* Save the terminal settings again. This is necessary for the TUI
244 when it switches to TUI or non-TUI mode; curses changes the terminal
245 and gdb must be able to restore it correctly. */
248 terminal_save_ours (void)
250 if (gdb_has_a_terminal ())
252 xfree (our_terminal_info
.ttystate
);
253 our_terminal_info
.ttystate
= serial_get_tty_state (stdin_serial
);
258 terminal_init_inferior (struct target_ops
*self
)
260 #ifdef PROCESS_GROUP_TYPE
261 /* This is for Lynx, and should be cleaned up by having Lynx be a separate
262 debugging target with a version of target_terminal_init_inferior which
263 passes in the process group to a generic routine which does all the work
264 (and the non-threaded child_terminal_init_inferior can just pass in
265 inferior_ptid to the same routine). */
266 /* We assume INFERIOR_PID is also the child's process group. */
267 terminal_init_inferior_with_pgrp (ptid_get_pid (inferior_ptid
));
268 #endif /* PROCESS_GROUP_TYPE */
271 /* Put the inferior's terminal settings into effect.
272 This is preparation for starting or resuming the inferior. */
275 terminal_inferior (struct target_ops
*self
)
277 struct inferior
*inf
;
278 struct terminal_info
*tinfo
;
280 if (!terminal_is_ours
)
283 inf
= current_inferior ();
284 tinfo
= get_inflow_inferior_data (inf
);
286 if (gdb_has_a_terminal ()
287 && tinfo
->ttystate
!= NULL
288 && tinfo
->run_terminal
== NULL
)
293 /* Is there a reason this is being done twice? It happens both
294 places we use F_SETFL, so I'm inclined to think perhaps there
295 is some reason, however perverse. Perhaps not though... */
296 result
= fcntl (0, F_SETFL
, tinfo
->tflags
);
297 result
= fcntl (0, F_SETFL
, tinfo
->tflags
);
298 OOPSY ("fcntl F_SETFL");
301 /* Because we were careful to not change in or out of raw mode in
302 terminal_ours, we will not change in our out of raw mode with
303 this call, so we don't flush any input. */
304 result
= serial_set_tty_state (stdin_serial
,
306 OOPSY ("setting tty state");
310 sigint_ours
= (void (*)()) signal (SIGINT
, SIG_IGN
);
312 sigquit_ours
= (void (*)()) signal (SIGQUIT
, SIG_IGN
);
316 /* If attach_flag is set, we don't know whether we are sharing a
317 terminal with the inferior or not. (attaching a process
318 without a terminal is one case where we do not; attaching a
319 process which we ran from the same shell as GDB via `&' is
320 one case where we do, I think (but perhaps this is not
321 `sharing' in the sense that we need to save and restore tty
322 state)). I don't know if there is any way to tell whether we
323 are sharing a terminal. So what we do is to go through all
324 the saving and restoring of the tty state, but ignore errors
325 setting the process group, which will happen if we are not
326 sharing a terminal). */
331 result
= tcsetpgrp (0, tinfo
->process_group
);
332 if (!inf
->attach_flag
)
337 result
= ioctl (0, TIOCSPGRP
, &tinfo
->process_group
);
338 if (!inf
->attach_flag
)
344 terminal_is_ours
= 0;
347 /* Put some of our terminal settings into effect,
348 enough to get proper results from our output,
349 but do not change into or out of RAW mode
350 so that no input is discarded.
352 After doing this, either terminal_ours or terminal_inferior
353 should be called to get back to a normal state of affairs. */
356 terminal_ours_for_output (struct target_ops
*self
)
361 /* Put our terminal settings into effect.
362 First record the inferior's terminal settings
363 so they can be restored properly later. */
366 terminal_ours (struct target_ops
*self
)
371 /* output_only is not used, and should not be used unless we introduce
372 separate terminal_is_ours and terminal_is_ours_for_output
376 terminal_ours_1 (int output_only
)
378 struct inferior
*inf
;
379 struct terminal_info
*tinfo
;
381 if (terminal_is_ours
)
384 terminal_is_ours
= 1;
386 /* Checking inferior->run_terminal is necessary so that
387 if GDB is running in the background, it won't block trying
388 to do the ioctl()'s below. Checking gdb_has_a_terminal
389 avoids attempting all the ioctl's when running in batch. */
391 inf
= current_inferior ();
392 tinfo
= get_inflow_inferior_data (inf
);
394 if (tinfo
->run_terminal
!= NULL
|| gdb_has_a_terminal () == 0)
399 /* Ignore this signal since it will happen when we try to set the
401 void (*osigttou
) () = NULL
;
407 osigttou
= (void (*)()) signal (SIGTTOU
, SIG_IGN
);
410 xfree (tinfo
->ttystate
);
411 tinfo
->ttystate
= serial_get_tty_state (stdin_serial
);
413 #ifdef PROCESS_GROUP_TYPE
414 if (!inf
->attach_flag
)
415 /* If setpgrp failed in terminal_inferior, this would give us
416 our process group instead of the inferior's. See
417 terminal_inferior for details. */
418 tinfo
->process_group
= gdb_getpgrp ();
421 /* Here we used to set ICANON in our ttystate, but I believe this
422 was an artifact from before when we used readline. Readline sets
423 the tty state when it needs to.
424 FIXME-maybe: However, query() expects non-raw mode and doesn't
425 use readline. Maybe query should use readline (on the other hand,
426 this only matters for HAVE_SGTTY, not termio or termios, I think). */
428 /* Set tty state to our_ttystate. We don't change in our out of raw
429 mode, to avoid flushing input. We need to do the same thing
430 regardless of output_only, because we don't have separate
431 terminal_is_ours and terminal_is_ours_for_output flags. It's OK,
432 though, since readline will deal with raw mode when/if it needs
435 serial_noflush_set_tty_state (stdin_serial
, our_terminal_info
.ttystate
,
441 result
= tcsetpgrp (0, our_terminal_info
.process_group
);
443 /* This fails on Ultrix with EINVAL if you run the testsuite
444 in the background with nohup, and then log out. GDB never
445 used to check for an error here, so perhaps there are other
446 such situations as well. */
448 fprintf_unfiltered (gdb_stderr
,
449 "[tcsetpgrp failed in terminal_ours: %s]\n",
450 safe_strerror (errno
));
455 result
= ioctl (0, TIOCSPGRP
, &our_terminal_info
.process_group
);
461 signal (SIGTTOU
, osigttou
);
466 signal (SIGINT
, sigint_ours
);
468 signal (SIGQUIT
, sigquit_ours
);
473 tinfo
->tflags
= fcntl (0, F_GETFL
, 0);
475 /* Is there a reason this is being done twice? It happens both
476 places we use F_SETFL, so I'm inclined to think perhaps there
477 is some reason, however perverse. Perhaps not though... */
478 result
= fcntl (0, F_SETFL
, our_terminal_info
.tflags
);
479 result
= fcntl (0, F_SETFL
, our_terminal_info
.tflags
);
484 /* Per-inferior data key. */
485 static const struct inferior_data
*inflow_inferior_data
;
488 inflow_inferior_data_cleanup (struct inferior
*inf
, void *arg
)
490 struct terminal_info
*info
= arg
;
492 xfree (info
->run_terminal
);
493 xfree (info
->ttystate
);
497 /* Get the current svr4 data. If none is found yet, add it now. This
498 function always returns a valid object. */
500 static struct terminal_info
*
501 get_inflow_inferior_data (struct inferior
*inf
)
503 struct terminal_info
*info
;
505 info
= inferior_data (inf
, inflow_inferior_data
);
508 info
= XCNEW (struct terminal_info
);
509 set_inferior_data (inf
, inflow_inferior_data
, info
);
515 /* This is a "inferior_exit" observer. Releases the TERMINAL_INFO member
516 of the inferior structure. This field is private to inflow.c, and
517 its type is opaque to the rest of GDB. PID is the target pid of
518 the inferior that is about to be removed from the inferior
522 inflow_inferior_exit (struct inferior
*inf
)
524 struct terminal_info
*info
;
526 info
= inferior_data (inf
, inflow_inferior_data
);
529 xfree (info
->run_terminal
);
530 xfree (info
->ttystate
);
532 set_inferior_data (inf
, inflow_inferior_data
, NULL
);
537 copy_terminal_info (struct inferior
*to
, struct inferior
*from
)
539 struct terminal_info
*tinfo_to
, *tinfo_from
;
541 tinfo_to
= get_inflow_inferior_data (to
);
542 tinfo_from
= get_inflow_inferior_data (from
);
544 xfree (tinfo_to
->run_terminal
);
545 xfree (tinfo_to
->ttystate
);
547 *tinfo_to
= *tinfo_from
;
549 if (tinfo_from
->run_terminal
)
550 tinfo_to
->run_terminal
551 = xstrdup (tinfo_from
->run_terminal
);
553 if (tinfo_from
->ttystate
)
555 = serial_copy_tty_state (stdin_serial
, tinfo_from
->ttystate
);
559 term_info (char *arg
, int from_tty
)
561 target_terminal_info (arg
, from_tty
);
565 child_terminal_info (const char *args
, int from_tty
)
567 struct inferior
*inf
;
568 struct terminal_info
*tinfo
;
570 if (!gdb_has_a_terminal ())
572 printf_filtered (_("This GDB does not control a terminal.\n"));
576 if (ptid_equal (inferior_ptid
, null_ptid
))
579 inf
= current_inferior ();
580 tinfo
= get_inflow_inferior_data (inf
);
582 printf_filtered (_("Inferior's terminal status "
583 "(currently saved by GDB):\n"));
585 /* First the fcntl flags. */
589 flags
= tinfo
->tflags
;
591 printf_filtered ("File descriptor flags = ");
594 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
596 /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */
597 switch (flags
& (O_ACCMODE
))
600 printf_filtered ("O_RDONLY");
603 printf_filtered ("O_WRONLY");
606 printf_filtered ("O_RDWR");
609 flags
&= ~(O_ACCMODE
);
612 if (flags
& O_NONBLOCK
)
613 printf_filtered (" | O_NONBLOCK");
614 flags
&= ~O_NONBLOCK
;
617 #if defined (O_NDELAY)
618 /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
619 print it as O_NONBLOCK, which is good cause that is what POSIX
620 has, and the flag will already be cleared by the time we get here. */
621 if (flags
& O_NDELAY
)
622 printf_filtered (" | O_NDELAY");
626 if (flags
& O_APPEND
)
627 printf_filtered (" | O_APPEND");
630 #if defined (O_BINARY)
631 if (flags
& O_BINARY
)
632 printf_filtered (" | O_BINARY");
637 printf_filtered (" | 0x%x", flags
);
638 printf_filtered ("\n");
641 #ifdef PROCESS_GROUP_TYPE
642 printf_filtered ("Process group = %d\n", (int) tinfo
->process_group
);
645 serial_print_tty_state (stdin_serial
, tinfo
->ttystate
, gdb_stdout
);
648 /* NEW_TTY_PREFORK is called before forking a new child process,
649 so we can record the state of ttys in the child to be formed.
650 TTYNAME is null if we are to share the terminal with gdb;
651 or points to a string containing the name of the desired tty.
653 NEW_TTY is called in new child processes under Unix, which will
654 become debugger target processes. This actually switches to
655 the terminal specified in the NEW_TTY_PREFORK call. */
658 new_tty_prefork (const char *ttyname
)
660 /* Save the name for later, for determining whether we and the child
661 are sharing a tty. */
662 inferior_thisrun_terminal
= ttyname
;
665 #if !defined(__GO32__) && !defined(_WIN32)
666 /* If RESULT, assumed to be the return value from a system call, is
667 negative, print the error message indicated by errno and exit.
668 MSG should identify the operation that failed. */
670 check_syscall (const char *msg
, int result
)
674 print_sys_errmsg (msg
, errno
);
685 if (inferior_thisrun_terminal
== 0)
687 #if !defined(__GO32__) && !defined(_WIN32)
689 /* Disconnect the child process from our controlling terminal. On some
690 systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
692 tty
= open ("/dev/tty", O_RDWR
);
697 osigttou
= (void (*)()) signal (SIGTTOU
, SIG_IGN
);
698 ioctl (tty
, TIOCNOTTY
, 0);
700 signal (SIGTTOU
, osigttou
);
704 /* Now open the specified new terminal. */
705 tty
= open (inferior_thisrun_terminal
, O_RDWR
| O_NOCTTY
);
706 check_syscall (inferior_thisrun_terminal
, tty
);
708 /* Avoid use of dup2; doesn't exist on all systems. */
712 check_syscall ("dup'ing tty into fd 0", dup (tty
));
717 check_syscall ("dup'ing tty into fd 1", dup (tty
));
722 check_syscall ("dup'ing tty into fd 2", dup (tty
));
726 /* Make tty our new controlling terminal. */
727 if (ioctl (tty
, TIOCSCTTY
, 0) == -1)
728 /* Mention GDB in warning because it will appear in the inferior's
729 terminal instead of GDB's. */
730 warning (_("GDB: Failed to set controlling terminal: %s"),
731 safe_strerror (errno
));
736 #endif /* !go32 && !win32 */
739 /* NEW_TTY_POSTFORK is called after forking a new child process, and
740 adding it to the inferior table, to store the TTYNAME being used by
741 the child, or null if it sharing the terminal with gdb. */
744 new_tty_postfork (void)
746 /* Save the name for later, for determining whether we and the child
747 are sharing a tty. */
749 if (inferior_thisrun_terminal
)
751 struct inferior
*inf
= current_inferior ();
752 struct terminal_info
*tinfo
= get_inflow_inferior_data (inf
);
754 tinfo
->run_terminal
= xstrdup (inferior_thisrun_terminal
);
757 inferior_thisrun_terminal
= NULL
;
761 /* Call set_sigint_trap when you need to pass a signal on to an attached
762 process when handling SIGINT. */
765 pass_signal (int signo
)
768 kill (ptid_get_pid (inferior_ptid
), SIGINT
);
772 static void (*osig
) ();
776 set_sigint_trap (void)
778 struct inferior
*inf
= current_inferior ();
779 struct terminal_info
*tinfo
= get_inflow_inferior_data (inf
);
781 if (inf
->attach_flag
|| tinfo
->run_terminal
)
783 osig
= (void (*)()) signal (SIGINT
, pass_signal
);
791 clear_sigint_trap (void)
795 signal (SIGINT
, osig
);
801 /* Create a new session if the inferior will run in a different tty.
802 A session is UNIX's way of grouping processes that share a controlling
803 terminal, so a new one is needed if the inferior terminal will be
804 different from GDB's.
806 Returns the session id of the new session, 0 if no session was created
807 or -1 if an error occurred. */
809 create_tty_session (void)
814 if (!job_control
|| inferior_thisrun_terminal
== 0)
819 warning (_("Failed to create new terminal session: setsid: %s"),
820 safe_strerror (errno
));
825 #endif /* HAVE_SETSID */
828 /* This is here because this is where we figure out whether we (probably)
829 have job control. Just using job_control only does part of it because
830 setpgid or setpgrp might not exist on a system without job control.
831 It might be considered misplaced (on the other hand, process groups and
832 job control are closely related to ttys).
834 For a more clean implementation, in libiberty, put a setpgid which merely
835 calls setpgrp and a setpgrp which does nothing (any system with job control
836 will have one or the other). */
844 #if defined (HAVE_TERMIOS) || defined (TIOCGPGRP)
846 /* The call setpgid (0, 0) is supposed to work and mean the same
847 thing as this, but on Ultrix 4.2A it fails with EPERM (and
848 setpgid (getpid (), getpid ()) succeeds). */
849 retval
= setpgid (getpid (), getpid ());
855 retval
= setpgrp (getpid (), getpid ());
857 #endif /* HAVE_SETPGRP */
858 #endif /* HAVE_SETPGID */
859 #endif /* defined (HAVE_TERMIOS) || defined (TIOCGPGRP) */
865 /* Get all the current tty settings (including whether we have a
866 tty at all!). We can't do this in _initialize_inflow because
867 serial_fdopen() won't work until the serial_ops_list is
868 initialized, but we don't want to do it lazily either, so
869 that we can guarantee stdin_serial is opened if there is
872 initialize_stdin_serial (void)
874 stdin_serial
= serial_fdopen (0);
878 _initialize_inflow (void)
880 add_info ("terminal", term_info
,
881 _("Print inferior's saved terminal status."));
883 add_setshow_auto_boolean_cmd ("interactive-mode", class_support
,
884 &interactive_mode
, _("\
885 Set whether GDB's standard input is a terminal."), _("\
886 Show whether GDB's standard input is a terminal."), _("\
887 If on, GDB assumes that standard input is a terminal. In practice, it\n\
888 means that GDB should wait for the user to answer queries associated to\n\
889 commands entered at the command prompt. If off, GDB assumes that standard\n\
890 input is not a terminal, and uses the default answer to all queries.\n\
891 If auto (the default), determine which mode to use based on the standard\n\
894 show_interactive_mode
,
895 &setlist
, &showlist
);
897 terminal_is_ours
= 1;
899 /* OK, figure out whether we have job control. If neither termios nor
900 sgtty (i.e. termio or go32), leave job_control 0. */
902 #if defined (HAVE_TERMIOS)
903 /* Do all systems with termios have the POSIX way of identifying job
904 control? I hope so. */
905 #ifdef _POSIX_JOB_CONTROL
908 #ifdef _SC_JOB_CONTROL
909 job_control
= sysconf (_SC_JOB_CONTROL
);
911 job_control
= 0; /* Have to assume the worst. */
912 #endif /* _SC_JOB_CONTROL */
913 #endif /* _POSIX_JOB_CONTROL */
914 #endif /* HAVE_TERMIOS */
921 #endif /* TIOCGPGRP */
924 observer_attach_inferior_exit (inflow_inferior_exit
);
927 = register_inferior_data_with_cleanup (NULL
, inflow_inferior_data_cleanup
);