1 /* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 2009 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "gdbthread.h"
31 #include "gdb_string.h"
34 #include "gdb_select.h"
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
46 #if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN)
47 static void handle_sigio (int);
50 extern void _initialize_inflow (void);
52 static void pass_signal (int);
54 static void terminal_ours_1 (int);
56 /* Record terminal status separately for debugger and inferior. */
58 static struct serial
*stdin_serial
;
60 /* Terminal related info we need to keep track of. Each inferior
61 holds an instance of this structure --- we save it whenever the
62 corresponding inferior stops, and restore it to the foreground
63 inferior when it resumes. */
66 /* The name of the tty (from the `tty' command) that we gave to the
67 inferior when it was started. */
70 /* TTY state. We save it whenever the inferior stops, and restore
71 it when it resumes. */
72 serial_ttystate ttystate
;
74 #ifdef PROCESS_GROUP_TYPE
75 /* Process group. Saved and restored just like ttystate. */
76 PROCESS_GROUP_TYPE process_group
;
79 /* fcntl flags. Saved and restored just like ttystate. */
83 /* Our own tty state, which we restore every time we need to deal with
84 the terminal. This is only set once, when GDB first starts. The
85 settings of flags which readline saves and restores and
87 static struct terminal_info our_terminal_info
;
89 static struct terminal_info
*get_inflow_inferior_data (struct inferior
*);
91 #ifdef PROCESS_GROUP_TYPE
93 /* Return the process group of the current inferior. */
96 inferior_process_group (void)
98 return get_inflow_inferior_data (current_inferior ())->process_group
;
102 /* While the inferior is running, we want SIGINT and SIGQUIT to go to the
103 inferior only. If we have job control, that takes care of it. If not,
104 we save our handlers in these two variables and set SIGINT and SIGQUIT
107 static void (*sigint_ours
) ();
108 static void (*sigquit_ours
) ();
110 /* The name of the tty (from the `tty' command) that we're giving to
111 the inferior when starting it up. This is only (and should only
112 be) used as a transient global by new_tty_prefork,
113 create_tty_session, new_tty and new_tty_postfork, all called from
114 fork_inferior, while forking a new child. */
115 static const char *inferior_thisrun_terminal
;
117 /* Nonzero if our terminal settings are in effect. Zero if the
118 inferior's settings are in effect. Ignored if !gdb_has_a_terminal
121 int terminal_is_ours
;
123 #ifdef PROCESS_GROUP_TYPE
124 static PROCESS_GROUP_TYPE
127 int process_group
= -1;
129 process_group
= tcgetpgrp (0);
132 process_group
= getpgrp ();
135 ioctl (0, TIOCGPGRP
, &process_group
);
137 return process_group
;
143 yes
, no
, have_not_checked
145 gdb_has_a_terminal_flag
= have_not_checked
;
147 /* Does GDB have a terminal (on stdin)? */
149 gdb_has_a_terminal (void)
151 switch (gdb_has_a_terminal_flag
)
157 case have_not_checked
:
158 /* Get all the current tty settings (including whether we have a
159 tty at all!). Can't do this in _initialize_inflow because
160 serial_fdopen() won't work until the serial_ops_list is
164 our_terminal_info
.tflags
= fcntl (0, F_GETFL
, 0);
167 gdb_has_a_terminal_flag
= no
;
168 if (stdin_serial
!= NULL
)
170 our_terminal_info
.ttystate
= serial_get_tty_state (stdin_serial
);
172 if (our_terminal_info
.ttystate
!= NULL
)
174 gdb_has_a_terminal_flag
= yes
;
175 #ifdef PROCESS_GROUP_TYPE
176 our_terminal_info
.process_group
= gdb_getpgrp ();
181 return gdb_has_a_terminal_flag
== yes
;
183 /* "Can't happen". */
188 /* Macro for printing errors from ioctl operations */
190 #define OOPSY(what) \
192 fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
193 what, safe_strerror (errno))
195 static void terminal_ours_1 (int);
197 /* Initialize the terminal settings we record for the inferior,
198 before we actually run the inferior. */
201 terminal_init_inferior_with_pgrp (int pgrp
)
203 if (gdb_has_a_terminal ())
205 struct inferior
*inf
= current_inferior ();
206 struct terminal_info
*tinfo
= get_inflow_inferior_data (inf
);
208 /* We could just as well copy our_ttystate (if we felt like
209 adding a new function serial_copy_tty_state()). */
210 xfree (tinfo
->ttystate
);
211 tinfo
->ttystate
= serial_get_tty_state (stdin_serial
);
213 #ifdef PROCESS_GROUP_TYPE
214 tinfo
->process_group
= pgrp
;
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 terminal_save_ours (void)
231 if (gdb_has_a_terminal ())
233 /* We could just as well copy our_ttystate (if we felt like adding
234 a new function serial_copy_tty_state). */
235 xfree (our_terminal_info
.ttystate
);
236 our_terminal_info
.ttystate
= serial_get_tty_state (stdin_serial
);
241 terminal_init_inferior (void)
243 #ifdef PROCESS_GROUP_TYPE
244 /* This is for Lynx, and should be cleaned up by having Lynx be a separate
245 debugging target with a version of target_terminal_init_inferior which
246 passes in the process group to a generic routine which does all the work
247 (and the non-threaded child_terminal_init_inferior can just pass in
248 inferior_ptid to the same routine). */
249 /* We assume INFERIOR_PID is also the child's process group. */
250 terminal_init_inferior_with_pgrp (PIDGET (inferior_ptid
));
251 #endif /* PROCESS_GROUP_TYPE */
254 /* Put the inferior's terminal settings into effect.
255 This is preparation for starting or resuming the inferior. */
258 terminal_inferior (void)
260 struct inferior
*inf
;
261 struct terminal_info
*tinfo
;
263 if (!terminal_is_ours
)
266 inf
= current_inferior ();
267 tinfo
= get_inflow_inferior_data (inf
);
269 if (gdb_has_a_terminal ()
270 && tinfo
->ttystate
!= NULL
271 && tinfo
->run_terminal
== NULL
)
276 /* Is there a reason this is being done twice? It happens both
277 places we use F_SETFL, so I'm inclined to think perhaps there
278 is some reason, however perverse. Perhaps not though... */
279 result
= fcntl (0, F_SETFL
, tinfo
->tflags
);
280 result
= fcntl (0, F_SETFL
, tinfo
->tflags
);
281 OOPSY ("fcntl F_SETFL");
284 /* Because we were careful to not change in or out of raw mode in
285 terminal_ours, we will not change in our out of raw mode with
286 this call, so we don't flush any input. */
287 result
= serial_set_tty_state (stdin_serial
,
289 OOPSY ("setting tty state");
293 sigint_ours
= (void (*)()) signal (SIGINT
, SIG_IGN
);
295 sigquit_ours
= (void (*)()) signal (SIGQUIT
, SIG_IGN
);
299 /* If attach_flag is set, we don't know whether we are sharing a
300 terminal with the inferior or not. (attaching a process
301 without a terminal is one case where we do not; attaching a
302 process which we ran from the same shell as GDB via `&' is
303 one case where we do, I think (but perhaps this is not
304 `sharing' in the sense that we need to save and restore tty
305 state)). I don't know if there is any way to tell whether we
306 are sharing a terminal. So what we do is to go through all
307 the saving and restoring of the tty state, but ignore errors
308 setting the process group, which will happen if we are not
309 sharing a terminal). */
314 result
= tcsetpgrp (0, tinfo
->process_group
);
315 if (!inf
->attach_flag
)
320 result
= ioctl (0, TIOCSPGRP
, &tinfo
->process_group
);
321 if (!inf
->attach_flag
)
327 terminal_is_ours
= 0;
330 /* Put some of our terminal settings into effect,
331 enough to get proper results from our output,
332 but do not change into or out of RAW mode
333 so that no input is discarded.
335 After doing this, either terminal_ours or terminal_inferior
336 should be called to get back to a normal state of affairs. */
339 terminal_ours_for_output (void)
344 /* Put our terminal settings into effect.
345 First record the inferior's terminal settings
346 so they can be restored properly later. */
354 /* output_only is not used, and should not be used unless we introduce
355 separate terminal_is_ours and terminal_is_ours_for_output
359 terminal_ours_1 (int output_only
)
361 struct inferior
*inf
;
362 struct terminal_info
*tinfo
;
364 if (terminal_is_ours
)
367 terminal_is_ours
= 1;
369 /* Checking inferior->run_terminal is necessary so that
370 if GDB is running in the background, it won't block trying
371 to do the ioctl()'s below. Checking gdb_has_a_terminal
372 avoids attempting all the ioctl's when running in batch. */
374 inf
= current_inferior ();
375 tinfo
= get_inflow_inferior_data (inf
);
377 if (tinfo
->run_terminal
!= NULL
|| gdb_has_a_terminal () == 0)
382 /* Ignore this signal since it will happen when we try to set the
384 void (*osigttou
) () = NULL
;
390 osigttou
= (void (*)()) signal (SIGTTOU
, SIG_IGN
);
393 xfree (tinfo
->ttystate
);
394 tinfo
->ttystate
= serial_get_tty_state (stdin_serial
);
396 #ifdef PROCESS_GROUP_TYPE
397 if (!inf
->attach_flag
)
398 /* If setpgrp failed in terminal_inferior, this would give us
399 our process group instead of the inferior's. See
400 terminal_inferior for details. */
401 tinfo
->process_group
= gdb_getpgrp ();
404 /* Here we used to set ICANON in our ttystate, but I believe this
405 was an artifact from before when we used readline. Readline sets
406 the tty state when it needs to.
407 FIXME-maybe: However, query() expects non-raw mode and doesn't
408 use readline. Maybe query should use readline (on the other hand,
409 this only matters for HAVE_SGTTY, not termio or termios, I think). */
411 /* Set tty state to our_ttystate. We don't change in our out of raw
412 mode, to avoid flushing input. We need to do the same thing
413 regardless of output_only, because we don't have separate
414 terminal_is_ours and terminal_is_ours_for_output flags. It's OK,
415 though, since readline will deal with raw mode when/if it needs to.
418 serial_noflush_set_tty_state (stdin_serial
, our_terminal_info
.ttystate
,
424 result
= tcsetpgrp (0, our_terminal_info
.process_group
);
426 /* This fails on Ultrix with EINVAL if you run the testsuite
427 in the background with nohup, and then log out. GDB never
428 used to check for an error here, so perhaps there are other
429 such situations as well. */
431 fprintf_unfiltered (gdb_stderr
, "[tcsetpgrp failed in terminal_ours: %s]\n",
432 safe_strerror (errno
));
437 result
= ioctl (0, TIOCSPGRP
, &our_terminal_info
.process_group
);
443 signal (SIGTTOU
, osigttou
);
448 signal (SIGINT
, sigint_ours
);
450 signal (SIGQUIT
, sigquit_ours
);
455 tinfo
->tflags
= fcntl (0, F_GETFL
, 0);
457 /* Is there a reason this is being done twice? It happens both
458 places we use F_SETFL, so I'm inclined to think perhaps there
459 is some reason, however perverse. Perhaps not though... */
460 result
= fcntl (0, F_SETFL
, our_terminal_info
.tflags
);
461 result
= fcntl (0, F_SETFL
, our_terminal_info
.tflags
);
466 /* Per-inferior data key. */
467 static const struct inferior_data
*inflow_inferior_data
;
470 inflow_inferior_data_cleanup (struct inferior
*inf
, void *arg
)
472 struct terminal_info
*info
;
474 info
= inferior_data (inf
, inflow_inferior_data
);
477 xfree (info
->run_terminal
);
482 /* Get the current svr4 data. If none is found yet, add it now. This
483 function always returns a valid object. */
485 static struct terminal_info
*
486 get_inflow_inferior_data (struct inferior
*inf
)
488 struct terminal_info
*info
;
490 info
= inferior_data (inf
, inflow_inferior_data
);
493 info
= XZALLOC (struct terminal_info
);
494 set_inferior_data (inf
, inflow_inferior_data
, info
);
500 /* This is a "inferior_exit" observer. Releases the TERMINAL_INFO member
501 of the inferior structure. This field is private to inflow.c, and
502 its type is opaque to the rest of GDB. PID is the target pid of
503 the inferior that is about to be removed from the inferior
507 inflow_inferior_exit (int pid
)
509 struct inferior
*inf
= find_inferior_pid (pid
);
510 struct terminal_info
*info
;
512 info
= inferior_data (inf
, inflow_inferior_data
);
515 xfree (info
->run_terminal
);
517 set_inferior_data (inf
, inflow_inferior_data
, NULL
);
522 copy_terminal_info (struct inferior
*to
, struct inferior
*from
)
524 struct terminal_info
*tinfo_to
, *tinfo_from
;
526 tinfo_to
= get_inflow_inferior_data (to
);
527 tinfo_from
= get_inflow_inferior_data (from
);
528 *tinfo_to
= *tinfo_from
;
529 if (tinfo_from
->run_terminal
)
530 tinfo_to
->run_terminal
531 = xstrdup (tinfo_from
->run_terminal
);
535 term_info (char *arg
, int from_tty
)
537 target_terminal_info (arg
, from_tty
);
541 child_terminal_info (char *args
, int from_tty
)
543 struct inferior
*inf
;
544 struct terminal_info
*tinfo
;
546 if (!gdb_has_a_terminal ())
548 printf_filtered (_("This GDB does not control a terminal.\n"));
552 if (ptid_equal (inferior_ptid
, null_ptid
))
555 inf
= current_inferior ();
556 tinfo
= get_inflow_inferior_data (inf
);
558 printf_filtered (_("Inferior's terminal status (currently saved by GDB):\n"));
560 /* First the fcntl flags. */
564 flags
= tinfo
->tflags
;
566 printf_filtered ("File descriptor flags = ");
569 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
571 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
572 switch (flags
& (O_ACCMODE
))
575 printf_filtered ("O_RDONLY");
578 printf_filtered ("O_WRONLY");
581 printf_filtered ("O_RDWR");
584 flags
&= ~(O_ACCMODE
);
587 if (flags
& O_NONBLOCK
)
588 printf_filtered (" | O_NONBLOCK");
589 flags
&= ~O_NONBLOCK
;
592 #if defined (O_NDELAY)
593 /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
594 print it as O_NONBLOCK, which is good cause that is what POSIX
595 has, and the flag will already be cleared by the time we get here. */
596 if (flags
& O_NDELAY
)
597 printf_filtered (" | O_NDELAY");
601 if (flags
& O_APPEND
)
602 printf_filtered (" | O_APPEND");
605 #if defined (O_BINARY)
606 if (flags
& O_BINARY
)
607 printf_filtered (" | O_BINARY");
612 printf_filtered (" | 0x%x", flags
);
613 printf_filtered ("\n");
616 #ifdef PROCESS_GROUP_TYPE
617 printf_filtered ("Process group = %d\n", (int) tinfo
->process_group
);
620 serial_print_tty_state (stdin_serial
, tinfo
->ttystate
, gdb_stdout
);
623 /* NEW_TTY_PREFORK is called before forking a new child process,
624 so we can record the state of ttys in the child to be formed.
625 TTYNAME is null if we are to share the terminal with gdb;
626 or points to a string containing the name of the desired tty.
628 NEW_TTY is called in new child processes under Unix, which will
629 become debugger target processes. This actually switches to
630 the terminal specified in the NEW_TTY_PREFORK call. */
633 new_tty_prefork (const char *ttyname
)
635 /* Save the name for later, for determining whether we and the child
636 are sharing a tty. */
637 inferior_thisrun_terminal
= ttyname
;
641 /* If RESULT, assumed to be the return value from a system call, is
642 negative, print the error message indicated by errno and exit.
643 MSG should identify the operation that failed. */
645 check_syscall (const char *msg
, int result
)
649 print_sys_errmsg (msg
, errno
);
659 if (inferior_thisrun_terminal
== 0)
661 #if !defined(__GO32__) && !defined(_WIN32)
663 /* Disconnect the child process from our controlling terminal. On some
664 systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
666 tty
= open ("/dev/tty", O_RDWR
);
671 osigttou
= (void (*)()) signal (SIGTTOU
, SIG_IGN
);
672 ioctl (tty
, TIOCNOTTY
, 0);
674 signal (SIGTTOU
, osigttou
);
678 /* Now open the specified new terminal. */
679 tty
= open (inferior_thisrun_terminal
, O_RDWR
| O_NOCTTY
);
680 check_syscall (inferior_thisrun_terminal
, tty
);
682 /* Avoid use of dup2; doesn't exist on all systems. */
686 check_syscall ("dup'ing tty into fd 0", dup (tty
));
691 check_syscall ("dup'ing tty into fd 1", dup (tty
));
696 check_syscall ("dup'ing tty into fd 2", dup (tty
));
700 /* Make tty our new controlling terminal. */
701 if (ioctl (tty
, TIOCSCTTY
, 0) == -1)
702 /* Mention GDB in warning because it will appear in the inferior's
703 terminal instead of GDB's. */
704 warning ("GDB: Failed to set controlling terminal: %s",
705 safe_strerror (errno
));
710 #endif /* !go32 && !win32 */
713 /* NEW_TTY_POSTFORK is called after forking a new child process, and
714 adding it to the inferior table, to store the TTYNAME being used by
715 the child, or null if it sharing the terminal with gdb. */
718 new_tty_postfork (void)
720 /* Save the name for later, for determining whether we and the child
721 are sharing a tty. */
723 if (inferior_thisrun_terminal
)
725 struct inferior
*inf
= current_inferior ();
726 struct terminal_info
*tinfo
= get_inflow_inferior_data (inf
);
728 tinfo
->run_terminal
= xstrdup (inferior_thisrun_terminal
);
731 inferior_thisrun_terminal
= NULL
;
735 /* Call set_sigint_trap when you need to pass a signal on to an attached
736 process when handling SIGINT */
739 pass_signal (int signo
)
742 kill (PIDGET (inferior_ptid
), SIGINT
);
746 static void (*osig
) ();
750 set_sigint_trap (void)
752 struct inferior
*inf
= current_inferior ();
753 struct terminal_info
*tinfo
= get_inflow_inferior_data (inf
);
755 if (inf
->attach_flag
|| tinfo
->run_terminal
)
757 osig
= (void (*)()) signal (SIGINT
, pass_signal
);
765 clear_sigint_trap (void)
769 signal (SIGINT
, osig
);
775 /* Create a new session if the inferior will run in a different tty.
776 A session is UNIX's way of grouping processes that share a controlling
777 terminal, so a new one is needed if the inferior terminal will be
778 different from GDB's.
780 Returns the session id of the new session, 0 if no session was created
781 or -1 if an error occurred. */
783 create_tty_session (void)
788 if (!job_control
|| inferior_thisrun_terminal
== 0)
793 warning ("Failed to create new terminal session: setsid: %s",
794 safe_strerror (errno
));
799 #endif /* HAVE_SETSID */
802 /* This is here because this is where we figure out whether we (probably)
803 have job control. Just using job_control only does part of it because
804 setpgid or setpgrp might not exist on a system without job control.
805 It might be considered misplaced (on the other hand, process groups and
806 job control are closely related to ttys).
808 For a more clean implementation, in libiberty, put a setpgid which merely
809 calls setpgrp and a setpgrp which does nothing (any system with job control
810 will have one or the other). */
818 #if defined (HAVE_TERMIOS) || defined (TIOCGPGRP)
820 /* The call setpgid (0, 0) is supposed to work and mean the same
821 thing as this, but on Ultrix 4.2A it fails with EPERM (and
822 setpgid (getpid (), getpid ()) succeeds). */
823 retval
= setpgid (getpid (), getpid ());
829 retval
= setpgrp (getpid (), getpid ());
831 #endif /* HAVE_SETPGRP */
832 #endif /* HAVE_SETPGID */
833 #endif /* defined (HAVE_TERMIOS) || defined (TIOCGPGRP) */
839 /* Get all the current tty settings (including whether we have a
840 tty at all!). We can't do this in _initialize_inflow because
841 serial_fdopen() won't work until the serial_ops_list is
842 initialized, but we don't want to do it lazily either, so
843 that we can guarantee stdin_serial is opened if there is
846 initialize_stdin_serial (void)
848 stdin_serial
= serial_fdopen (0);
852 _initialize_inflow (void)
854 add_info ("terminal", term_info
,
855 _("Print inferior's saved terminal status."));
857 terminal_is_ours
= 1;
859 /* OK, figure out whether we have job control. If neither termios nor
860 sgtty (i.e. termio or go32), leave job_control 0. */
862 #if defined (HAVE_TERMIOS)
863 /* Do all systems with termios have the POSIX way of identifying job
864 control? I hope so. */
865 #ifdef _POSIX_JOB_CONTROL
868 #ifdef _SC_JOB_CONTROL
869 job_control
= sysconf (_SC_JOB_CONTROL
);
871 job_control
= 0; /* have to assume the worst */
872 #endif /* _SC_JOB_CONTROL */
873 #endif /* _POSIX_JOB_CONTROL */
874 #endif /* HAVE_TERMIOS */
881 #endif /* TIOCGPGRP */
884 observer_attach_inferior_exit (inflow_inferior_exit
);
887 = register_inferior_data_with_cleanup (inflow_inferior_data_cleanup
);