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 kill_command (char *, int);
56 static void terminal_ours_1 (int);
58 /* Record terminal status separately for debugger and inferior. */
60 static struct serial
*stdin_serial
;
62 /* Terminal related info we need to keep track of. Each inferior
63 holds an instance of this structure --- we save it whenever the
64 corresponding inferior stops, and restore it to the foreground
65 inferior when it resumes. */
68 /* The name of the tty (from the `tty' command) that we gave to the
69 inferior when it was started. */
72 /* TTY state. We save it whenever the inferior stops, and restore
73 it when it resumes. */
74 serial_ttystate ttystate
;
76 #ifdef PROCESS_GROUP_TYPE
77 /* Process group. Saved and restored just like ttystate. */
78 PROCESS_GROUP_TYPE process_group
;
81 /* fcntl flags. Saved and restored just like ttystate. */
85 /* Our own tty state, which we restore every time we need to deal with
86 the terminal. This is only set once, when GDB first starts. The
87 settings of flags which readline saves and restores and
89 static struct terminal_info our_terminal_info
;
91 #ifdef PROCESS_GROUP_TYPE
93 /* Return the process group of the current inferior. */
96 inferior_process_group (void)
98 return current_inferior ()->terminal_info
->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 ();
207 /* We could just as well copy our_ttystate (if we felt like
208 adding a new function serial_copy_tty_state()). */
209 xfree (inf
->terminal_info
->ttystate
);
210 inf
->terminal_info
->ttystate
211 = serial_get_tty_state (stdin_serial
);
213 #ifdef PROCESS_GROUP_TYPE
214 inf
->terminal_info
->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
;
262 if (!terminal_is_ours
)
265 inf
= current_inferior ();
267 if (gdb_has_a_terminal ()
268 && inf
->terminal_info
->ttystate
!= NULL
269 && inf
->terminal_info
->run_terminal
== NULL
)
274 /* Is there a reason this is being done twice? It happens both
275 places we use F_SETFL, so I'm inclined to think perhaps there
276 is some reason, however perverse. Perhaps not though... */
277 result
= fcntl (0, F_SETFL
, inf
->terminal_info
->tflags
);
278 result
= fcntl (0, F_SETFL
, inf
->terminal_info
->tflags
);
279 OOPSY ("fcntl F_SETFL");
282 /* Because we were careful to not change in or out of raw mode in
283 terminal_ours, we will not change in our out of raw mode with
284 this call, so we don't flush any input. */
285 result
= serial_set_tty_state (stdin_serial
,
286 inf
->terminal_info
->ttystate
);
287 OOPSY ("setting tty state");
291 sigint_ours
= (void (*)()) signal (SIGINT
, SIG_IGN
);
293 sigquit_ours
= (void (*)()) signal (SIGQUIT
, SIG_IGN
);
297 /* If attach_flag is set, we don't know whether we are sharing a
298 terminal with the inferior or not. (attaching a process
299 without a terminal is one case where we do not; attaching a
300 process which we ran from the same shell as GDB via `&' is
301 one case where we do, I think (but perhaps this is not
302 `sharing' in the sense that we need to save and restore tty
303 state)). I don't know if there is any way to tell whether we
304 are sharing a terminal. So what we do is to go through all
305 the saving and restoring of the tty state, but ignore errors
306 setting the process group, which will happen if we are not
307 sharing a terminal). */
312 result
= tcsetpgrp (0, inf
->terminal_info
->process_group
);
313 if (!inf
->attach_flag
)
318 result
= ioctl (0, TIOCSPGRP
, &inf
->terminal_info
->process_group
);
319 if (!inf
->attach_flag
)
325 terminal_is_ours
= 0;
328 /* Put some of our terminal settings into effect,
329 enough to get proper results from our output,
330 but do not change into or out of RAW mode
331 so that no input is discarded.
333 After doing this, either terminal_ours or terminal_inferior
334 should be called to get back to a normal state of affairs. */
337 terminal_ours_for_output (void)
342 /* Put our terminal settings into effect.
343 First record the inferior's terminal settings
344 so they can be restored properly later. */
352 /* output_only is not used, and should not be used unless we introduce
353 separate terminal_is_ours and terminal_is_ours_for_output
357 terminal_ours_1 (int output_only
)
359 struct inferior
*inf
;
361 if (terminal_is_ours
)
364 /* Checking inferior->run_terminal is necessary so that
365 if GDB is running in the background, it won't block trying
366 to do the ioctl()'s below. Checking gdb_has_a_terminal
367 avoids attempting all the ioctl's when running in batch. */
369 inf
= current_inferior ();
371 if (inf
->terminal_info
->run_terminal
!= NULL
|| gdb_has_a_terminal () == 0)
374 if (!terminal_is_ours
)
377 /* Ignore this signal since it will happen when we try to set the
379 void (*osigttou
) () = NULL
;
383 terminal_is_ours
= 1;
387 osigttou
= (void (*)()) signal (SIGTTOU
, SIG_IGN
);
390 xfree (inf
->terminal_info
->ttystate
);
391 inf
->terminal_info
->ttystate
= serial_get_tty_state (stdin_serial
);
393 #ifdef PROCESS_GROUP_TYPE
394 if (!inf
->attach_flag
)
395 /* If setpgrp failed in terminal_inferior, this would give us
396 our process group instead of the inferior's. See
397 terminal_inferior for details. */
398 inf
->terminal_info
->process_group
= gdb_getpgrp ();
401 /* Here we used to set ICANON in our ttystate, but I believe this
402 was an artifact from before when we used readline. Readline sets
403 the tty state when it needs to.
404 FIXME-maybe: However, query() expects non-raw mode and doesn't
405 use readline. Maybe query should use readline (on the other hand,
406 this only matters for HAVE_SGTTY, not termio or termios, I think). */
408 /* Set tty state to our_ttystate. We don't change in our out of raw
409 mode, to avoid flushing input. We need to do the same thing
410 regardless of output_only, because we don't have separate
411 terminal_is_ours and terminal_is_ours_for_output flags. It's OK,
412 though, since readline will deal with raw mode when/if it needs to.
415 serial_noflush_set_tty_state (stdin_serial
, our_terminal_info
.ttystate
,
416 inf
->terminal_info
->ttystate
);
421 result
= tcsetpgrp (0, our_terminal_info
.process_group
);
423 /* This fails on Ultrix with EINVAL if you run the testsuite
424 in the background with nohup, and then log out. GDB never
425 used to check for an error here, so perhaps there are other
426 such situations as well. */
428 fprintf_unfiltered (gdb_stderr
, "[tcsetpgrp failed in terminal_ours: %s]\n",
429 safe_strerror (errno
));
434 result
= ioctl (0, TIOCSPGRP
, &our_terminal_info
.process_group
);
440 signal (SIGTTOU
, osigttou
);
445 signal (SIGINT
, sigint_ours
);
447 signal (SIGQUIT
, sigquit_ours
);
452 inf
->terminal_info
->tflags
= fcntl (0, F_GETFL
, 0);
454 /* Is there a reason this is being done twice? It happens both
455 places we use F_SETFL, so I'm inclined to think perhaps there
456 is some reason, however perverse. Perhaps not though... */
457 result
= fcntl (0, F_SETFL
, our_terminal_info
.tflags
);
458 result
= fcntl (0, F_SETFL
, our_terminal_info
.tflags
);
463 /* This is a "new_inferior" observer. It's business is to allocate
464 the TERMINAL_INFO member of the inferior structure. This field is
465 private to inflow.c, and its type is opaque to the rest of GDB.
466 PID is the target pid of the inferior that has just been added to
467 the inferior list. */
470 inflow_new_inferior (int pid
)
472 struct inferior
*inf
= find_inferior_pid (pid
);
474 inf
->terminal_info
= XZALLOC (struct terminal_info
);
477 /* This is a "inferior_exit" observer. Releases the TERMINAL_INFO member
478 of the inferior structure. This field is private to inflow.c, and
479 its type is opaque to the rest of GDB. PID is the target pid of
480 the inferior that is about to be removed from the inferior
484 inflow_inferior_exit (int pid
)
486 struct inferior
*inf
= find_inferior_pid (pid
);
488 xfree (inf
->terminal_info
->run_terminal
);
489 xfree (inf
->terminal_info
);
490 inf
->terminal_info
= NULL
;
494 copy_terminal_info (struct inferior
*to
, struct inferior
*from
)
496 *to
->terminal_info
= *from
->terminal_info
;
497 if (from
->terminal_info
->run_terminal
)
498 to
->terminal_info
->run_terminal
= from
->terminal_info
->run_terminal
;
502 term_info (char *arg
, int from_tty
)
504 target_terminal_info (arg
, from_tty
);
508 child_terminal_info (char *args
, int from_tty
)
510 struct inferior
*inf
;
512 if (!gdb_has_a_terminal ())
514 printf_filtered (_("This GDB does not control a terminal.\n"));
518 if (ptid_equal (inferior_ptid
, null_ptid
))
521 inf
= current_inferior ();
523 printf_filtered (_("Inferior's terminal status (currently saved by GDB):\n"));
525 /* First the fcntl flags. */
529 flags
= inf
->terminal_info
->tflags
;
531 printf_filtered ("File descriptor flags = ");
534 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
536 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
537 switch (flags
& (O_ACCMODE
))
540 printf_filtered ("O_RDONLY");
543 printf_filtered ("O_WRONLY");
546 printf_filtered ("O_RDWR");
549 flags
&= ~(O_ACCMODE
);
552 if (flags
& O_NONBLOCK
)
553 printf_filtered (" | O_NONBLOCK");
554 flags
&= ~O_NONBLOCK
;
557 #if defined (O_NDELAY)
558 /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
559 print it as O_NONBLOCK, which is good cause that is what POSIX
560 has, and the flag will already be cleared by the time we get here. */
561 if (flags
& O_NDELAY
)
562 printf_filtered (" | O_NDELAY");
566 if (flags
& O_APPEND
)
567 printf_filtered (" | O_APPEND");
570 #if defined (O_BINARY)
571 if (flags
& O_BINARY
)
572 printf_filtered (" | O_BINARY");
577 printf_filtered (" | 0x%x", flags
);
578 printf_filtered ("\n");
581 #ifdef PROCESS_GROUP_TYPE
582 printf_filtered ("Process group = %d\n",
583 (int) inf
->terminal_info
->process_group
);
586 serial_print_tty_state (stdin_serial
,
587 inf
->terminal_info
->ttystate
,
591 /* NEW_TTY_PREFORK is called before forking a new child process,
592 so we can record the state of ttys in the child to be formed.
593 TTYNAME is null if we are to share the terminal with gdb;
594 or points to a string containing the name of the desired tty.
596 NEW_TTY is called in new child processes under Unix, which will
597 become debugger target processes. This actually switches to
598 the terminal specified in the NEW_TTY_PREFORK call. */
601 new_tty_prefork (const char *ttyname
)
603 /* Save the name for later, for determining whether we and the child
604 are sharing a tty. */
605 inferior_thisrun_terminal
= ttyname
;
609 /* If RESULT, assumed to be the return value from a system call, is
610 negative, print the error message indicated by errno and exit.
611 MSG should identify the operation that failed. */
613 check_syscall (const char *msg
, int result
)
617 print_sys_errmsg (msg
, errno
);
627 if (inferior_thisrun_terminal
== 0)
629 #if !defined(__GO32__) && !defined(_WIN32)
631 /* Disconnect the child process from our controlling terminal. On some
632 systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
634 tty
= open ("/dev/tty", O_RDWR
);
639 osigttou
= (void (*)()) signal (SIGTTOU
, SIG_IGN
);
640 ioctl (tty
, TIOCNOTTY
, 0);
642 signal (SIGTTOU
, osigttou
);
646 /* Now open the specified new terminal. */
647 tty
= open (inferior_thisrun_terminal
, O_RDWR
| O_NOCTTY
);
648 check_syscall (inferior_thisrun_terminal
, tty
);
650 /* Avoid use of dup2; doesn't exist on all systems. */
654 check_syscall ("dup'ing tty into fd 0", dup (tty
));
659 check_syscall ("dup'ing tty into fd 1", dup (tty
));
664 check_syscall ("dup'ing tty into fd 2", dup (tty
));
668 /* Make tty our new controlling terminal. */
669 if (ioctl (tty
, TIOCSCTTY
, 0) == -1)
670 /* Mention GDB in warning because it will appear in the inferior's
671 terminal instead of GDB's. */
672 warning ("GDB: Failed to set controlling terminal: %s",
673 safe_strerror (errno
));
678 #endif /* !go32 && !win32 */
681 /* NEW_TTY_POSTFORK is called after forking a new child process, and
682 adding it to the inferior table, to store the TTYNAME being used by
683 the child, or null if it sharing the terminal with gdb. */
686 new_tty_postfork (void)
688 /* Save the name for later, for determining whether we and the child
689 are sharing a tty. */
691 if (inferior_thisrun_terminal
)
692 current_inferior ()->terminal_info
->run_terminal
693 = xstrdup (inferior_thisrun_terminal
);
695 inferior_thisrun_terminal
= NULL
;
699 /* Kill the inferior process. Make us have no inferior. */
702 kill_command (char *arg
, int from_tty
)
704 /* FIXME: This should not really be inferior_ptid (or target_has_execution).
705 It should be a distinct flag that indicates that a target is active, cuz
706 some targets don't have processes! */
708 if (ptid_equal (inferior_ptid
, null_ptid
))
709 error (_("The program is not being run."));
710 if (!query (_("Kill the program being debugged? ")))
711 error (_("Not confirmed."));
714 /* If the current target interface claims there's still execution,
715 then don't mess with threads of other processes. */
716 if (!target_has_execution
)
718 init_thread_list (); /* Destroy thread info */
720 /* Killing off the inferior can leave us with a core file. If
721 so, print the state we are left in. */
722 if (target_has_stack
)
724 printf_filtered (_("In %s,\n"), target_longname
);
725 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
728 bfd_cache_close_all ();
731 /* Call set_sigint_trap when you need to pass a signal on to an attached
732 process when handling SIGINT */
735 pass_signal (int signo
)
738 kill (PIDGET (inferior_ptid
), SIGINT
);
742 static void (*osig
) ();
746 set_sigint_trap (void)
748 struct inferior
*inf
= current_inferior ();
749 if (inf
->attach_flag
|| inf
->terminal_info
->run_terminal
)
751 osig
= (void (*)()) signal (SIGINT
, pass_signal
);
759 clear_sigint_trap (void)
763 signal (SIGINT
, osig
);
769 /* Create a new session if the inferior will run in a different tty.
770 A session is UNIX's way of grouping processes that share a controlling
771 terminal, so a new one is needed if the inferior terminal will be
772 different from GDB's.
774 Returns the session id of the new session, 0 if no session was created
775 or -1 if an error occurred. */
777 create_tty_session (void)
782 if (!job_control
|| inferior_thisrun_terminal
== 0)
787 warning ("Failed to create new terminal session: setsid: %s",
788 safe_strerror (errno
));
793 #endif /* HAVE_SETSID */
796 /* This is here because this is where we figure out whether we (probably)
797 have job control. Just using job_control only does part of it because
798 setpgid or setpgrp might not exist on a system without job control.
799 It might be considered misplaced (on the other hand, process groups and
800 job control are closely related to ttys).
802 For a more clean implementation, in libiberty, put a setpgid which merely
803 calls setpgrp and a setpgrp which does nothing (any system with job control
804 will have one or the other). */
812 #if defined (HAVE_TERMIOS) || defined (TIOCGPGRP)
814 /* The call setpgid (0, 0) is supposed to work and mean the same
815 thing as this, but on Ultrix 4.2A it fails with EPERM (and
816 setpgid (getpid (), getpid ()) succeeds). */
817 retval
= setpgid (getpid (), getpid ());
823 retval
= setpgrp (getpid (), getpid ());
825 #endif /* HAVE_SETPGRP */
826 #endif /* HAVE_SETPGID */
827 #endif /* defined (HAVE_TERMIOS) || defined (TIOCGPGRP) */
833 /* Get all the current tty settings (including whether we have a
834 tty at all!). We can't do this in _initialize_inflow because
835 serial_fdopen() won't work until the serial_ops_list is
836 initialized, but we don't want to do it lazily either, so
837 that we can guarantee stdin_serial is opened if there is
840 initialize_stdin_serial (void)
842 stdin_serial
= serial_fdopen (0);
846 _initialize_inflow (void)
848 add_info ("terminal", term_info
,
849 _("Print inferior's saved terminal status."));
851 add_com ("kill", class_run
, kill_command
,
852 _("Kill execution of program being debugged."));
854 inferior_ptid
= null_ptid
;
856 terminal_is_ours
= 1;
858 /* OK, figure out whether we have job control. If neither termios nor
859 sgtty (i.e. termio or go32), leave job_control 0. */
861 #if defined (HAVE_TERMIOS)
862 /* Do all systems with termios have the POSIX way of identifying job
863 control? I hope so. */
864 #ifdef _POSIX_JOB_CONTROL
867 #ifdef _SC_JOB_CONTROL
868 job_control
= sysconf (_SC_JOB_CONTROL
);
870 job_control
= 0; /* have to assume the worst */
871 #endif /* _SC_JOB_CONTROL */
872 #endif /* _POSIX_JOB_CONTROL */
873 #endif /* HAVE_TERMIOS */
880 #endif /* TIOCGPGRP */
883 observer_attach_new_inferior (inflow_new_inferior
);
884 observer_attach_inferior_exit (inflow_inferior_exit
);