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., 675 Mass Ave, Cambridge, MA 02139, USA. */
34 #if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY) && !defined (__GO32__) && !defined(WIN32)
38 #if defined (HAVE_TERMIOS)
44 #define PROCESS_GROUP_TYPE pid_t
49 /* This is only used for the ultra. Does it have pid_t? */
50 #define PROCESS_GROUP_TYPE short
52 #define PROCESS_GROUP_TYPE int
57 kill_command PARAMS ((char *, int));
60 terminal_ours_1 PARAMS ((int));
62 /* Record terminal status separately for debugger and inferior. */
64 static serial_t stdin_serial;
66 /* TTY state for the inferior. We save it whenever the inferior stops, and
67 restore it when it resumes. */
68 static serial_ttystate inferior_ttystate;
70 /* Our own tty state, which we restore every time we need to deal with the
71 terminal. We only set it once, when GDB first starts. The settings of
72 flags which readline saves and restores and unimportant. */
73 static serial_ttystate our_ttystate;
75 /* fcntl flags for us and the inferior. Saved and restored just like
76 {our,inferior}_ttystate. */
77 static int tflags_inferior;
78 static int tflags_ours;
80 #ifdef PROCESS_GROUP_TYPE
81 /* Process group for us and the inferior. Saved and restored just like
82 {our,inferior}_ttystate. */
83 PROCESS_GROUP_TYPE our_process_group;
84 PROCESS_GROUP_TYPE inferior_process_group;
87 /* While the inferior is running, we want SIGINT and SIGQUIT to go to the
88 inferior only. If we have job control, that takes care of it. If not,
89 we save our handlers in these two variables and set SIGINT and SIGQUIT
91 static void (*sigint_ours) ();
92 static void (*sigquit_ours) ();
94 /* The name of the tty (from the `tty' command) that we gave to the inferior
95 when it was last started. */
97 static char *inferior_thisrun_terminal;
99 /* Nonzero if our terminal settings are in effect. Zero if the
100 inferior's settings are in effect. Ignored if !gdb_has_a_terminal
103 static int terminal_is_ours;
105 enum {yes, no, have_not_checked} gdb_has_a_terminal_flag = have_not_checked;
107 /* Does GDB have a terminal (on stdin)? */
109 gdb_has_a_terminal ()
111 switch (gdb_has_a_terminal_flag)
117 case have_not_checked:
118 /* Get all the current tty settings (including whether we have a tty at
119 all!). Can't do this in _initialize_inflow because SERIAL_FDOPEN
120 won't work until the serial_ops_list is initialized. */
123 tflags_ours = fcntl (0, F_GETFL, 0);
126 gdb_has_a_terminal_flag = no;
127 stdin_serial = SERIAL_FDOPEN (0);
128 if (stdin_serial != NULL)
130 our_ttystate = SERIAL_GET_TTY_STATE (stdin_serial);
132 if (our_ttystate != NULL)
134 gdb_has_a_terminal_flag = yes;
136 our_process_group = tcgetpgrp (0);
139 ioctl (0, TIOCGPGRP, &our_process_group);
144 return gdb_has_a_terminal_flag == yes;
146 /* "Can't happen". */
151 /* Macro for printing errors from ioctl operations */
153 #define OOPSY(what) \
155 fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
156 what, strerror (errno))
158 static void terminal_ours_1 PARAMS ((int));
160 /* Initialize the terminal settings we record for the inferior,
161 before we actually run the inferior. */
164 terminal_init_inferior ()
166 if (gdb_has_a_terminal ())
168 /* We could just as well copy our_ttystate (if we felt like adding
169 a new function SERIAL_COPY_TTY_STATE). */
170 if (inferior_ttystate)
171 free (inferior_ttystate);
172 inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial);
173 #ifdef PROCESS_GROUP_TYPE
175 /* This is for Lynx, and should be cleaned up by having Lynx be
176 a separate debugging target with a version of
177 target_terminal_init_inferior which passes in the process
178 group to a generic routine which does all the work (and the
179 non-threaded child_terminal_init_inferior can just pass in
180 inferior_pid to the same routine). */
181 inferior_process_group = PIDGET (inferior_pid);
183 inferior_process_group = inferior_pid;
187 /* Make sure that next time we call terminal_inferior (which will be
188 before the program runs, as it needs to be), we install the new
190 terminal_is_ours = 1;
194 /* Put the inferior's terminal settings into effect.
195 This is preparation for starting or resuming the inferior. */
200 if (gdb_has_a_terminal () && terminal_is_ours
201 && inferior_thisrun_terminal == 0)
206 /* Is there a reason this is being done twice? It happens both
207 places we use F_SETFL, so I'm inclined to think perhaps there
208 is some reason, however perverse. Perhaps not though... */
209 result = fcntl (0, F_SETFL, tflags_inferior);
210 result = fcntl (0, F_SETFL, tflags_inferior);
211 OOPSY ("fcntl F_SETFL");
214 /* Because we were careful to not change in or out of raw mode in
215 terminal_ours, we will not change in our out of raw mode with
216 this call, so we don't flush any input. */
217 result = SERIAL_SET_TTY_STATE (stdin_serial, inferior_ttystate);
218 OOPSY ("setting tty state");
222 sigint_ours = (void (*) ()) signal (SIGINT, SIG_IGN);
223 sigquit_ours = (void (*) ()) signal (SIGQUIT, SIG_IGN);
226 /* If attach_flag is set, we don't know whether we are sharing a
227 terminal with the inferior or not. (attaching a process
228 without a terminal is one case where we do not; attaching a
229 process which we ran from the same shell as GDB via `&' is
230 one case where we do, I think (but perhaps this is not
231 `sharing' in the sense that we need to save and restore tty
232 state)). I don't know if there is any way to tell whether we
233 are sharing a terminal. So what we do is to go through all
234 the saving and restoring of the tty state, but ignore errors
235 setting the process group, which will happen if we are not
236 sharing a terminal). */
241 result = tcsetpgrp (0, inferior_process_group);
247 result = ioctl (0, TIOCSPGRP, &inferior_process_group);
254 terminal_is_ours = 0;
257 /* Put some of our terminal settings into effect,
258 enough to get proper results from our output,
259 but do not change into or out of RAW mode
260 so that no input is discarded.
262 After doing this, either terminal_ours or terminal_inferior
263 should be called to get back to a normal state of affairs. */
266 terminal_ours_for_output ()
271 /* Put our terminal settings into effect.
272 First record the inferior's terminal settings
273 so they can be restored properly later. */
281 /* output_only is not used, and should not be used unless we introduce
282 separate terminal_is_ours and terminal_is_ours_for_output
286 terminal_ours_1 (output_only)
289 /* Checking inferior_thisrun_terminal is necessary so that
290 if GDB is running in the background, it won't block trying
291 to do the ioctl()'s below. Checking gdb_has_a_terminal
292 avoids attempting all the ioctl's when running in batch. */
293 if (inferior_thisrun_terminal != 0 || gdb_has_a_terminal () == 0)
296 if (!terminal_is_ours)
298 /* Ignore this signal since it will happen when we try to set the
303 terminal_is_ours = 1;
307 osigttou = (void (*) ()) signal (SIGTTOU, SIG_IGN);
310 if (inferior_ttystate)
311 free (inferior_ttystate);
312 inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial);
314 inferior_process_group = tcgetpgrp (0);
317 ioctl (0, TIOCGPGRP, &inferior_process_group);
320 /* Here we used to set ICANON in our ttystate, but I believe this
321 was an artifact from before when we used readline. Readline sets
322 the tty state when it needs to.
323 FIXME-maybe: However, query() expects non-raw mode and doesn't
324 use readline. Maybe query should use readline (on the other hand,
325 this only matters for HAVE_SGTTY, not termio or termios, I think). */
327 /* Set tty state to our_ttystate. We don't change in our out of raw
328 mode, to avoid flushing input. We need to do the same thing
329 regardless of output_only, because we don't have separate
330 terminal_is_ours and terminal_is_ours_for_output flags. It's OK,
331 though, since readline will deal with raw mode when/if it needs to.
334 SERIAL_NOFLUSH_SET_TTY_STATE (stdin_serial, our_ttystate,
340 result = tcsetpgrp (0, our_process_group);
342 /* This fails on Ultrix with EINVAL if you run the testsuite
343 in the background with nohup, and then log out. GDB never
344 used to check for an error here, so perhaps there are other
345 such situations as well. */
347 fprintf_unfiltered (gdb_stderr, "[tcsetpgrp failed in terminal_ours: %s]\n",
353 result = ioctl (0, TIOCSPGRP, &our_process_group);
359 signal (SIGTTOU, osigttou);
364 signal (SIGINT, sigint_ours);
365 signal (SIGQUIT, sigquit_ours);
369 tflags_inferior = fcntl (0, F_GETFL, 0);
371 /* Is there a reason this is being done twice? It happens both
372 places we use F_SETFL, so I'm inclined to think perhaps there
373 is some reason, however perverse. Perhaps not though... */
374 result = fcntl (0, F_SETFL, tflags_ours);
375 result = fcntl (0, F_SETFL, tflags_ours);
378 result = result; /* lint */
384 term_info (arg, from_tty)
388 target_terminal_info (arg, from_tty);
393 child_terminal_info (args, from_tty)
397 if (!gdb_has_a_terminal ())
399 printf_filtered ("This GDB does not control a terminal.\n");
403 printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
405 /* First the fcntl flags. */
409 flags = tflags_inferior;
411 printf_filtered ("File descriptor flags = ");
414 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
416 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
417 switch (flags & (O_ACCMODE))
419 case O_RDONLY: printf_filtered ("O_RDONLY"); break;
420 case O_WRONLY: printf_filtered ("O_WRONLY"); break;
421 case O_RDWR: printf_filtered ("O_RDWR"); break;
423 flags &= ~(O_ACCMODE);
426 if (flags & O_NONBLOCK)
427 printf_filtered (" | O_NONBLOCK");
428 flags &= ~O_NONBLOCK;
431 #if defined (O_NDELAY)
432 /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
433 print it as O_NONBLOCK, which is good cause that is what POSIX
434 has, and the flag will already be cleared by the time we get here. */
435 if (flags & O_NDELAY)
436 printf_filtered (" | O_NDELAY");
440 if (flags & O_APPEND)
441 printf_filtered (" | O_APPEND");
444 #if defined (O_BINARY)
445 if (flags & O_BINARY)
446 printf_filtered (" | O_BINARY");
451 printf_filtered (" | 0x%x", flags);
452 printf_filtered ("\n");
455 #ifdef PROCESS_GROUP_TYPE
456 printf_filtered ("Process group = %d\n", inferior_process_group);
459 SERIAL_PRINT_TTY_STATE (stdin_serial, inferior_ttystate);
462 /* NEW_TTY_PREFORK is called before forking a new child process,
463 so we can record the state of ttys in the child to be formed.
464 TTYNAME is null if we are to share the terminal with gdb;
465 or points to a string containing the name of the desired tty.
467 NEW_TTY is called in new child processes under Unix, which will
468 become debugger target processes. This actually switches to
469 the terminal specified in the NEW_TTY_PREFORK call. */
472 new_tty_prefork (ttyname)
475 /* Save the name for later, for determining whether we and the child
476 are sharing a tty. */
477 inferior_thisrun_terminal = ttyname;
485 if (inferior_thisrun_terminal == 0)
487 #if !defined(__GO32__) && !defined(WIN32)
489 /* Disconnect the child process from our controlling terminal. On some
490 systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
492 tty = open("/dev/tty", O_RDWR);
497 osigttou = (void (*)()) signal(SIGTTOU, SIG_IGN);
498 ioctl(tty, TIOCNOTTY, 0);
500 signal(SIGTTOU, osigttou);
504 /* Now open the specified new terminal. */
507 tty = open(inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
509 tty = open(inferior_thisrun_terminal, O_RDWR);
513 print_sys_errmsg (inferior_thisrun_terminal, errno);
517 /* Avoid use of dup2; doesn't exist on all systems. */
519 { close (0); dup (tty); }
521 { close (1); dup (tty); }
523 { close (2); dup (tty); }
526 #endif /* !go32 && !win32*/
529 /* Kill the inferior process. Make us have no inferior. */
533 kill_command (arg, from_tty)
537 /* FIXME: This should not really be inferior_pid (or target_has_execution).
538 It should be a distinct flag that indicates that a target is active, cuz
539 some targets don't have processes! */
541 if (inferior_pid == 0)
542 error ("The program is not being run.");
543 if (!query ("Kill the program being debugged? "))
544 error ("Not confirmed.");
547 init_thread_list(); /* Destroy thread info */
549 /* Killing off the inferior can leave us with a core file. If so,
550 print the state we are left in. */
551 if (target_has_stack) {
552 printf_filtered ("In %s,\n", target_longname);
553 if (selected_frame == NULL)
554 fputs_filtered ("No selected stack frame.\n", gdb_stdout);
556 print_stack_frame (selected_frame, selected_frame_level, 1);
560 /* Call set_sigint_trap when you need to pass a signal on to an attached
561 process when handling SIGINT */
568 kill (inferior_pid, SIGINT);
571 static void (*osig)();
576 if (attach_flag || inferior_thisrun_terminal)
578 osig = (void (*) ()) signal (SIGINT, pass_signal);
585 if (attach_flag || inferior_thisrun_terminal)
587 signal (SIGINT, osig);
591 #if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN)
592 static void (*old_sigio) ();
601 signal (SIGIO, handle_sigio);
604 FD_SET (target_activity_fd, &readfds);
605 numfds = select (target_activity_fd + 1, &readfds, NULL, NULL, NULL);
606 if (numfds >= 0 && FD_ISSET (target_activity_fd, &readfds))
608 if ((*target_activity_function) ())
609 kill (inferior_pid, SIGINT);
613 static int old_fcntl_flags;
618 if (target_activity_function)
620 old_sigio = (void (*) ()) signal (SIGIO, handle_sigio);
621 fcntl (target_activity_fd, F_SETOWN, getpid());
622 old_fcntl_flags = fcntl (target_activity_fd, F_GETFL, 0);
623 fcntl (target_activity_fd, F_SETFL, old_fcntl_flags | FASYNC);
630 if (target_activity_function)
632 signal (SIGIO, old_sigio);
633 fcntl (target_activity_fd, F_SETFL, old_fcntl_flags);
636 #else /* No SIGIO. */
640 if (target_activity_function)
647 if (target_activity_function)
650 #endif /* No SIGIO. */
653 /* This is here because this is where we figure out whether we (probably)
654 have job control. Just using job_control only does part of it because
655 setpgid or setpgrp might not exist on a system without job control.
656 It might be considered misplaced (on the other hand, process groups and
657 job control are closely related to ttys).
659 For a more clean implementation, in libiberty, put a setpgid which merely
660 calls setpgrp and a setpgrp which does nothing (any system with job control
661 will have one or the other). */
669 #if defined (NEED_POSIX_SETPGID) || defined (HAVE_TERMIOS)
670 /* Do all systems with termios have setpgid? I hope so. */
671 /* setpgid (0, 0) is supposed to work and mean the same thing as
672 this, but on Ultrix 4.2A it fails with EPERM (and
673 setpgid (getpid (), getpid ()) succeeds). */
674 retval = setpgid (getpid (), getpid ());
676 #if defined (TIOCGPGRP)
677 #if defined(USG) && !defined(SETPGRP_ARGS)
680 retval = setpgrp (getpid (), getpid ());
682 #endif /* TIOCGPGRP. */
683 #endif /* NEED_POSIX_SETPGID */
689 _initialize_inflow ()
691 add_info ("terminal", term_info,
692 "Print inferior's saved terminal status.");
694 add_com ("kill", class_run, kill_command,
695 "Kill execution of program being debugged.");
699 terminal_is_ours = 1;
701 /* OK, figure out whether we have job control. If neither termios nor
702 sgtty (i.e. termio or go32), leave job_control 0. */
704 #if defined (HAVE_TERMIOS)
705 /* Do all systems with termios have the POSIX way of identifying job
706 control? I hope so. */
707 #ifdef _POSIX_JOB_CONTROL
710 job_control = sysconf (_SC_JOB_CONTROL);
719 #endif /* TIOCGPGRP */