1 /* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1995 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 2 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, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #include "gdbthread.h"
30 #include "gdb_string.h"
38 #define PROCESS_GROUP_TYPE pid_t
43 /* This is only used for the ultra. Does it have pid_t? */
44 #define PROCESS_GROUP_TYPE short
46 #define PROCESS_GROUP_TYPE int
51 kill_command PARAMS ((char *, int));
54 terminal_ours_1 PARAMS ((int));
56 /* Record terminal status separately for debugger and inferior. */
58 static serial_t stdin_serial;
60 /* TTY state for the inferior. We save it whenever the inferior stops, and
61 restore it when it resumes. */
62 static serial_ttystate inferior_ttystate;
64 /* Our own tty state, which we restore every time we need to deal with the
65 terminal. We only set it once, when GDB first starts. The settings of
66 flags which readline saves and restores and unimportant. */
67 static serial_ttystate our_ttystate;
69 /* fcntl flags for us and the inferior. Saved and restored just like
70 {our,inferior}_ttystate. */
71 static int tflags_inferior;
72 static int tflags_ours;
74 #ifdef PROCESS_GROUP_TYPE
75 /* Process group for us and the inferior. Saved and restored just like
76 {our,inferior}_ttystate. */
77 PROCESS_GROUP_TYPE our_process_group;
78 PROCESS_GROUP_TYPE inferior_process_group;
81 /* While the inferior is running, we want SIGINT and SIGQUIT to go to the
82 inferior only. If we have job control, that takes care of it. If not,
83 we save our handlers in these two variables and set SIGINT and SIGQUIT
85 static void (*sigint_ours) ();
86 static void (*sigquit_ours) ();
88 /* The name of the tty (from the `tty' command) that we gave to the inferior
89 when it was last started. */
91 static char *inferior_thisrun_terminal;
93 /* Nonzero if our terminal settings are in effect. Zero if the
94 inferior's settings are in effect. Ignored if !gdb_has_a_terminal
97 static int terminal_is_ours;
99 enum {yes, no, have_not_checked} gdb_has_a_terminal_flag = have_not_checked;
101 /* Does GDB have a terminal (on stdin)? */
103 gdb_has_a_terminal ()
105 switch (gdb_has_a_terminal_flag)
111 case have_not_checked:
112 /* Get all the current tty settings (including whether we have a tty at
113 all!). Can't do this in _initialize_inflow because SERIAL_FDOPEN
114 won't work until the serial_ops_list is initialized. */
117 tflags_ours = fcntl (0, F_GETFL, 0);
120 gdb_has_a_terminal_flag = no;
121 stdin_serial = SERIAL_FDOPEN (0);
122 if (stdin_serial != NULL)
124 our_ttystate = SERIAL_GET_TTY_STATE (stdin_serial);
126 if (our_ttystate != NULL)
128 gdb_has_a_terminal_flag = yes;
130 our_process_group = tcgetpgrp (0);
133 ioctl (0, TIOCGPGRP, &our_process_group);
138 return gdb_has_a_terminal_flag == yes;
140 /* "Can't happen". */
145 /* Macro for printing errors from ioctl operations */
147 #define OOPSY(what) \
149 fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
150 what, strerror (errno))
152 static void terminal_ours_1 PARAMS ((int));
154 /* Initialize the terminal settings we record for the inferior,
155 before we actually run the inferior. */
158 terminal_init_inferior_with_pgrp (pgrp)
161 if (gdb_has_a_terminal ())
163 /* We could just as well copy our_ttystate (if we felt like adding
164 a new function SERIAL_COPY_TTY_STATE). */
165 if (inferior_ttystate)
166 free (inferior_ttystate);
167 inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial);
169 #ifdef PROCESS_GROUP_TYPE
170 inferior_process_group = pgrp;
173 /* Make sure that next time we call terminal_inferior (which will be
174 before the program runs, as it needs to be), we install the new
176 terminal_is_ours = 1;
181 terminal_init_inferior ()
183 #ifdef PROCESS_GROUP_TYPE
185 /* This is for Lynx, and should be cleaned up by having Lynx be a separate
186 debugging target with a version of target_terminal_init_inferior which
187 passes in the process group to a generic routine which does all the work
188 (and the non-threaded child_terminal_init_inferior can just pass in
189 inferior_pid to the same routine). */
190 terminal_init_inferior_with_pgrp (PIDGET (inferior_pid));
192 /* By default, we assume INFERIOR_PID is also the child's process group. */
193 terminal_init_inferior_with_pgrp (inferior_pid);
195 #endif /* PROCESS_GROUP_TYPE */
198 /* Put the inferior's terminal settings into effect.
199 This is preparation for starting or resuming the inferior. */
204 if (gdb_has_a_terminal () && terminal_is_ours
205 && inferior_thisrun_terminal == 0)
210 /* Is there a reason this is being done twice? It happens both
211 places we use F_SETFL, so I'm inclined to think perhaps there
212 is some reason, however perverse. Perhaps not though... */
213 result = fcntl (0, F_SETFL, tflags_inferior);
214 result = fcntl (0, F_SETFL, tflags_inferior);
215 OOPSY ("fcntl F_SETFL");
218 /* Because we were careful to not change in or out of raw mode in
219 terminal_ours, we will not change in our out of raw mode with
220 this call, so we don't flush any input. */
221 result = SERIAL_SET_TTY_STATE (stdin_serial, inferior_ttystate);
222 OOPSY ("setting tty state");
226 sigint_ours = (void (*) ()) signal (SIGINT, SIG_IGN);
227 sigquit_ours = (void (*) ()) signal (SIGQUIT, SIG_IGN);
230 /* If attach_flag is set, we don't know whether we are sharing a
231 terminal with the inferior or not. (attaching a process
232 without a terminal is one case where we do not; attaching a
233 process which we ran from the same shell as GDB via `&' is
234 one case where we do, I think (but perhaps this is not
235 `sharing' in the sense that we need to save and restore tty
236 state)). I don't know if there is any way to tell whether we
237 are sharing a terminal. So what we do is to go through all
238 the saving and restoring of the tty state, but ignore errors
239 setting the process group, which will happen if we are not
240 sharing a terminal). */
245 result = tcsetpgrp (0, inferior_process_group);
251 result = ioctl (0, TIOCSPGRP, &inferior_process_group);
258 terminal_is_ours = 0;
261 /* Put some of our terminal settings into effect,
262 enough to get proper results from our output,
263 but do not change into or out of RAW mode
264 so that no input is discarded.
266 After doing this, either terminal_ours or terminal_inferior
267 should be called to get back to a normal state of affairs. */
270 terminal_ours_for_output ()
275 /* Put our terminal settings into effect.
276 First record the inferior's terminal settings
277 so they can be restored properly later. */
285 /* output_only is not used, and should not be used unless we introduce
286 separate terminal_is_ours and terminal_is_ours_for_output
290 terminal_ours_1 (output_only)
293 /* Checking inferior_thisrun_terminal is necessary so that
294 if GDB is running in the background, it won't block trying
295 to do the ioctl()'s below. Checking gdb_has_a_terminal
296 avoids attempting all the ioctl's when running in batch. */
297 if (inferior_thisrun_terminal != 0 || gdb_has_a_terminal () == 0)
300 if (!terminal_is_ours)
302 /* Ignore this signal since it will happen when we try to set the
307 terminal_is_ours = 1;
311 osigttou = (void (*) ()) signal (SIGTTOU, SIG_IGN);
314 if (inferior_ttystate)
315 free (inferior_ttystate);
316 inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial);
318 inferior_process_group = tcgetpgrp (0);
321 ioctl (0, TIOCGPGRP, &inferior_process_group);
324 /* Here we used to set ICANON in our ttystate, but I believe this
325 was an artifact from before when we used readline. Readline sets
326 the tty state when it needs to.
327 FIXME-maybe: However, query() expects non-raw mode and doesn't
328 use readline. Maybe query should use readline (on the other hand,
329 this only matters for HAVE_SGTTY, not termio or termios, I think). */
331 /* Set tty state to our_ttystate. We don't change in our out of raw
332 mode, to avoid flushing input. We need to do the same thing
333 regardless of output_only, because we don't have separate
334 terminal_is_ours and terminal_is_ours_for_output flags. It's OK,
335 though, since readline will deal with raw mode when/if it needs to.
338 SERIAL_NOFLUSH_SET_TTY_STATE (stdin_serial, our_ttystate,
344 result = tcsetpgrp (0, our_process_group);
346 /* This fails on Ultrix with EINVAL if you run the testsuite
347 in the background with nohup, and then log out. GDB never
348 used to check for an error here, so perhaps there are other
349 such situations as well. */
351 fprintf_unfiltered (gdb_stderr, "[tcsetpgrp failed in terminal_ours: %s]\n",
357 result = ioctl (0, TIOCSPGRP, &our_process_group);
363 signal (SIGTTOU, osigttou);
368 signal (SIGINT, sigint_ours);
369 signal (SIGQUIT, sigquit_ours);
373 tflags_inferior = fcntl (0, F_GETFL, 0);
375 /* Is there a reason this is being done twice? It happens both
376 places we use F_SETFL, so I'm inclined to think perhaps there
377 is some reason, however perverse. Perhaps not though... */
378 result = fcntl (0, F_SETFL, tflags_ours);
379 result = fcntl (0, F_SETFL, tflags_ours);
382 result = result; /* lint */
388 term_info (arg, from_tty)
392 target_terminal_info (arg, from_tty);
397 child_terminal_info (args, from_tty)
401 if (!gdb_has_a_terminal ())
403 printf_filtered ("This GDB does not control a terminal.\n");
407 printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
409 /* First the fcntl flags. */
413 flags = tflags_inferior;
415 printf_filtered ("File descriptor flags = ");
418 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
420 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
421 switch (flags & (O_ACCMODE))
423 case O_RDONLY: printf_filtered ("O_RDONLY"); break;
424 case O_WRONLY: printf_filtered ("O_WRONLY"); break;
425 case O_RDWR: printf_filtered ("O_RDWR"); break;
427 flags &= ~(O_ACCMODE);
430 if (flags & O_NONBLOCK)
431 printf_filtered (" | O_NONBLOCK");
432 flags &= ~O_NONBLOCK;
435 #if defined (O_NDELAY)
436 /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
437 print it as O_NONBLOCK, which is good cause that is what POSIX
438 has, and the flag will already be cleared by the time we get here. */
439 if (flags & O_NDELAY)
440 printf_filtered (" | O_NDELAY");
444 if (flags & O_APPEND)
445 printf_filtered (" | O_APPEND");
448 #if defined (O_BINARY)
449 if (flags & O_BINARY)
450 printf_filtered (" | O_BINARY");
455 printf_filtered (" | 0x%x", flags);
456 printf_filtered ("\n");
459 #ifdef PROCESS_GROUP_TYPE
460 printf_filtered ("Process group = %d\n", inferior_process_group);
463 SERIAL_PRINT_TTY_STATE (stdin_serial, inferior_ttystate);
466 /* NEW_TTY_PREFORK is called before forking a new child process,
467 so we can record the state of ttys in the child to be formed.
468 TTYNAME is null if we are to share the terminal with gdb;
469 or points to a string containing the name of the desired tty.
471 NEW_TTY is called in new child processes under Unix, which will
472 become debugger target processes. This actually switches to
473 the terminal specified in the NEW_TTY_PREFORK call. */
476 new_tty_prefork (ttyname)
479 /* Save the name for later, for determining whether we and the child
480 are sharing a tty. */
481 inferior_thisrun_terminal = ttyname;
489 if (inferior_thisrun_terminal == 0)
491 #if !defined(__GO32__) && !defined(__WIN32__)
493 /* Disconnect the child process from our controlling terminal. On some
494 systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
496 tty = open("/dev/tty", O_RDWR);
501 osigttou = (void (*)()) signal(SIGTTOU, SIG_IGN);
502 ioctl(tty, TIOCNOTTY, 0);
504 signal(SIGTTOU, osigttou);
508 /* Now open the specified new terminal. */
511 tty = open(inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
513 tty = open(inferior_thisrun_terminal, O_RDWR);
517 print_sys_errmsg (inferior_thisrun_terminal, errno);
521 /* Avoid use of dup2; doesn't exist on all systems. */
523 { close (0); dup (tty); }
525 { close (1); dup (tty); }
527 { close (2); dup (tty); }
530 #endif /* !go32 && !win32*/
533 /* Kill the inferior process. Make us have no inferior. */
537 kill_command (arg, from_tty)
541 /* FIXME: This should not really be inferior_pid (or target_has_execution).
542 It should be a distinct flag that indicates that a target is active, cuz
543 some targets don't have processes! */
545 if (inferior_pid == 0)
546 error ("The program is not being run.");
547 if (!query ("Kill the program being debugged? "))
548 error ("Not confirmed.");
551 init_thread_list(); /* Destroy thread info */
553 /* Killing off the inferior can leave us with a core file. If so,
554 print the state we are left in. */
555 if (target_has_stack) {
556 printf_filtered ("In %s,\n", target_longname);
557 if (selected_frame == NULL)
558 fputs_filtered ("No selected stack frame.\n", gdb_stdout);
560 print_stack_frame (selected_frame, selected_frame_level, 1);
564 /* Call set_sigint_trap when you need to pass a signal on to an attached
565 process when handling SIGINT */
572 kill (inferior_pid, SIGINT);
575 static void (*osig)();
580 if (attach_flag || inferior_thisrun_terminal)
582 osig = (void (*) ()) signal (SIGINT, pass_signal);
589 if (attach_flag || inferior_thisrun_terminal)
591 signal (SIGINT, osig);
595 #if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN)
596 static void (*old_sigio) ();
605 signal (SIGIO, handle_sigio);
608 FD_SET (target_activity_fd, &readfds);
609 numfds = select (target_activity_fd + 1, &readfds, NULL, NULL, NULL);
610 if (numfds >= 0 && FD_ISSET (target_activity_fd, &readfds))
612 if ((*target_activity_function) ())
613 kill (inferior_pid, SIGINT);
617 static int old_fcntl_flags;
622 if (target_activity_function)
624 old_sigio = (void (*) ()) signal (SIGIO, handle_sigio);
625 fcntl (target_activity_fd, F_SETOWN, getpid());
626 old_fcntl_flags = fcntl (target_activity_fd, F_GETFL, 0);
627 fcntl (target_activity_fd, F_SETFL, old_fcntl_flags | FASYNC);
634 if (target_activity_function)
636 signal (SIGIO, old_sigio);
637 fcntl (target_activity_fd, F_SETFL, old_fcntl_flags);
640 #else /* No SIGIO. */
644 if (target_activity_function)
651 if (target_activity_function)
654 #endif /* No SIGIO. */
657 /* This is here because this is where we figure out whether we (probably)
658 have job control. Just using job_control only does part of it because
659 setpgid or setpgrp might not exist on a system without job control.
660 It might be considered misplaced (on the other hand, process groups and
661 job control are closely related to ttys).
663 For a more clean implementation, in libiberty, put a setpgid which merely
664 calls setpgrp and a setpgrp which does nothing (any system with job control
665 will have one or the other). */
673 #if defined (NEED_POSIX_SETPGID) || (defined (HAVE_TERMIOS) && defined (HAVE_SETPGID))
674 /* setpgid (0, 0) is supposed to work and mean the same thing as
675 this, but on Ultrix 4.2A it fails with EPERM (and
676 setpgid (getpid (), getpid ()) succeeds). */
677 retval = setpgid (getpid (), getpid ());
679 #if defined (TIOCGPGRP)
680 #if defined(USG) && !defined(SETPGRP_ARGS)
683 retval = setpgrp (getpid (), getpid ());
685 #endif /* TIOCGPGRP. */
686 #endif /* NEED_POSIX_SETPGID */
692 _initialize_inflow ()
694 add_info ("terminal", term_info,
695 "Print inferior's saved terminal status.");
697 add_com ("kill", class_run, kill_command,
698 "Kill execution of program being debugged.");
702 terminal_is_ours = 1;
704 /* OK, figure out whether we have job control. If neither termios nor
705 sgtty (i.e. termio or go32), leave job_control 0. */
707 #if defined (HAVE_TERMIOS)
708 /* Do all systems with termios have the POSIX way of identifying job
709 control? I hope so. */
710 #ifdef _POSIX_JOB_CONTROL
713 #ifdef _SC_JOB_CONTROL
714 job_control = sysconf (_SC_JOB_CONTROL);
716 job_control = 0; /* have to assume the worst */
717 #endif /* _SC_JOB_CONTROL */
718 #endif /* _POSIX_JOB_CONTROL */
719 #endif /* HAVE_TERMIOS */
726 #endif /* TIOCGPGRP */