inflow.c revision 98944
119370Spst/* Low level interface to ptrace, for GDB when running under Unix. 298944Sobrien Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 398944Sobrien 1996, 1998, 1999, 2000, 2001 Free Software Foundation, Inc. 419370Spst 598944Sobrien This file is part of GDB. 619370Spst 798944Sobrien This program is free software; you can redistribute it and/or modify 898944Sobrien it under the terms of the GNU General Public License as published by 998944Sobrien the Free Software Foundation; either version 2 of the License, or 1098944Sobrien (at your option) any later version. 1119370Spst 1298944Sobrien This program is distributed in the hope that it will be useful, 1398944Sobrien but WITHOUT ANY WARRANTY; without even the implied warranty of 1498944Sobrien MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1598944Sobrien GNU General Public License for more details. 1619370Spst 1798944Sobrien You should have received a copy of the GNU General Public License 1898944Sobrien along with this program; if not, write to the Free Software 1998944Sobrien Foundation, Inc., 59 Temple Place - Suite 330, 2098944Sobrien Boston, MA 02111-1307, USA. */ 2119370Spst 2219370Spst#include "defs.h" 2319370Spst#include "frame.h" 2419370Spst#include "inferior.h" 2519370Spst#include "command.h" 2619370Spst#include "serial.h" 2719370Spst#include "terminal.h" 2819370Spst#include "target.h" 2946283Sdfr#include "gdbthread.h" 3019370Spst 3119370Spst#include "gdb_string.h" 3219370Spst#include <signal.h> 3319370Spst#include <fcntl.h> 3498944Sobrien#ifdef HAVE_SYS_SELECT_H 3598944Sobrien#include <sys/select.h> 3619370Spst#endif 3719370Spst 3819370Spst#ifdef HAVE_TERMIOS 3919370Spst#define PROCESS_GROUP_TYPE pid_t 4019370Spst#endif 4119370Spst 4246283Sdfr#ifdef HAVE_TERMIO 4346283Sdfr#define PROCESS_GROUP_TYPE int 4446283Sdfr#endif 4546283Sdfr 4619370Spst#ifdef HAVE_SGTTY 4719370Spst#ifdef SHORT_PGRP 4819370Spst/* This is only used for the ultra. Does it have pid_t? */ 4919370Spst#define PROCESS_GROUP_TYPE short 5019370Spst#else 5119370Spst#define PROCESS_GROUP_TYPE int 5219370Spst#endif 5319370Spst#endif /* sgtty */ 5419370Spst 5598944Sobrien#ifdef HAVE_SYS_IOCTL_H 5698944Sobrien#include <sys/ioctl.h> 5798944Sobrien#endif 5898944Sobrien 5946283Sdfr#if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN) 6098944Sobrienstatic void handle_sigio (int); 6146283Sdfr#endif 6246283Sdfr 6398944Sobrienextern void _initialize_inflow (void); 6446283Sdfr 6598944Sobrienstatic void pass_signal (int); 6619370Spst 6798944Sobrienstatic void kill_command (char *, int); 6898944Sobrien 6998944Sobrienstatic void terminal_ours_1 (int); 7019370Spst 7119370Spst/* Record terminal status separately for debugger and inferior. */ 7219370Spst 7398944Sobrienstatic struct serial *stdin_serial; 7419370Spst 7519370Spst/* TTY state for the inferior. We save it whenever the inferior stops, and 7619370Spst restore it when it resumes. */ 7719370Spststatic serial_ttystate inferior_ttystate; 7819370Spst 7919370Spst/* Our own tty state, which we restore every time we need to deal with the 8019370Spst terminal. We only set it once, when GDB first starts. The settings of 8119370Spst flags which readline saves and restores and unimportant. */ 8219370Spststatic serial_ttystate our_ttystate; 8319370Spst 8419370Spst/* fcntl flags for us and the inferior. Saved and restored just like 8519370Spst {our,inferior}_ttystate. */ 8619370Spststatic int tflags_inferior; 8719370Spststatic int tflags_ours; 8819370Spst 8919370Spst#ifdef PROCESS_GROUP_TYPE 9019370Spst/* Process group for us and the inferior. Saved and restored just like 9119370Spst {our,inferior}_ttystate. */ 9219370SpstPROCESS_GROUP_TYPE our_process_group; 9319370SpstPROCESS_GROUP_TYPE inferior_process_group; 9419370Spst#endif 9519370Spst 9619370Spst/* While the inferior is running, we want SIGINT and SIGQUIT to go to the 9719370Spst inferior only. If we have job control, that takes care of it. If not, 9819370Spst we save our handlers in these two variables and set SIGINT and SIGQUIT 9919370Spst to SIG_IGN. */ 10046283Sdfr 10119370Spststatic void (*sigint_ours) (); 10219370Spststatic void (*sigquit_ours) (); 10319370Spst 10419370Spst/* The name of the tty (from the `tty' command) that we gave to the inferior 10519370Spst when it was last started. */ 10619370Spst 10719370Spststatic char *inferior_thisrun_terminal; 10819370Spst 10919370Spst/* Nonzero if our terminal settings are in effect. Zero if the 11019370Spst inferior's settings are in effect. Ignored if !gdb_has_a_terminal 11119370Spst (). */ 11219370Spst 11346283Sdfrint terminal_is_ours; 11419370Spst 11598944Sobrienenum 11698944Sobrien { 11798944Sobrien yes, no, have_not_checked 11898944Sobrien } 11998944Sobriengdb_has_a_terminal_flag = have_not_checked; 12019370Spst 12119370Spst/* Does GDB have a terminal (on stdin)? */ 12219370Spstint 12398944Sobriengdb_has_a_terminal (void) 12419370Spst{ 12519370Spst switch (gdb_has_a_terminal_flag) 12619370Spst { 12719370Spst case yes: 12819370Spst return 1; 12919370Spst case no: 13019370Spst return 0; 13119370Spst case have_not_checked: 13298944Sobrien /* Get all the current tty settings (including whether we have a 13398944Sobrien tty at all!). Can't do this in _initialize_inflow because 13498944Sobrien serial_fdopen() won't work until the serial_ops_list is 13598944Sobrien initialized. */ 13619370Spst 13719370Spst#ifdef F_GETFL 13819370Spst tflags_ours = fcntl (0, F_GETFL, 0); 13919370Spst#endif 14019370Spst 14119370Spst gdb_has_a_terminal_flag = no; 14298944Sobrien stdin_serial = serial_fdopen (0); 14319370Spst if (stdin_serial != NULL) 14419370Spst { 14598944Sobrien our_ttystate = serial_get_tty_state (stdin_serial); 14619370Spst 14719370Spst if (our_ttystate != NULL) 14819370Spst { 14919370Spst gdb_has_a_terminal_flag = yes; 15019370Spst#ifdef HAVE_TERMIOS 15119370Spst our_process_group = tcgetpgrp (0); 15219370Spst#endif 15346283Sdfr#ifdef HAVE_TERMIO 15446283Sdfr our_process_group = getpgrp (); 15546283Sdfr#endif 15619370Spst#ifdef HAVE_SGTTY 15719370Spst ioctl (0, TIOCGPGRP, &our_process_group); 15819370Spst#endif 15919370Spst } 16019370Spst } 16119370Spst 16219370Spst return gdb_has_a_terminal_flag == yes; 16319370Spst default: 16419370Spst /* "Can't happen". */ 16519370Spst return 0; 16619370Spst } 16719370Spst} 16819370Spst 16919370Spst/* Macro for printing errors from ioctl operations */ 17019370Spst 17119370Spst#define OOPSY(what) \ 17219370Spst if (result == -1) \ 17319370Spst fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \ 17419370Spst what, strerror (errno)) 17519370Spst 17698944Sobrienstatic void terminal_ours_1 (int); 17719370Spst 17819370Spst/* Initialize the terminal settings we record for the inferior, 17919370Spst before we actually run the inferior. */ 18019370Spst 18119370Spstvoid 18298944Sobrienterminal_init_inferior_with_pgrp (int pgrp) 18319370Spst{ 18419370Spst if (gdb_has_a_terminal ()) 18519370Spst { 18698944Sobrien /* We could just as well copy our_ttystate (if we felt like 18798944Sobrien adding a new function serial_copy_tty_state()). */ 18819370Spst if (inferior_ttystate) 18998944Sobrien xfree (inferior_ttystate); 19098944Sobrien inferior_ttystate = serial_get_tty_state (stdin_serial); 19146283Sdfr 19219370Spst#ifdef PROCESS_GROUP_TYPE 19346283Sdfr inferior_process_group = pgrp; 19419370Spst#endif 19519370Spst 19619370Spst /* Make sure that next time we call terminal_inferior (which will be 19798944Sobrien before the program runs, as it needs to be), we install the new 19898944Sobrien process group. */ 19919370Spst terminal_is_ours = 1; 20019370Spst } 20119370Spst} 20219370Spst 20346283Sdfrvoid 20498944Sobrienterminal_init_inferior (void) 20546283Sdfr{ 20646283Sdfr#ifdef PROCESS_GROUP_TYPE 20746283Sdfr /* This is for Lynx, and should be cleaned up by having Lynx be a separate 20846283Sdfr debugging target with a version of target_terminal_init_inferior which 20946283Sdfr passes in the process group to a generic routine which does all the work 21046283Sdfr (and the non-threaded child_terminal_init_inferior can just pass in 21198944Sobrien inferior_ptid to the same routine). */ 21246283Sdfr /* We assume INFERIOR_PID is also the child's process group. */ 21398944Sobrien terminal_init_inferior_with_pgrp (PIDGET (inferior_ptid)); 21446283Sdfr#endif /* PROCESS_GROUP_TYPE */ 21546283Sdfr} 21646283Sdfr 21719370Spst/* Put the inferior's terminal settings into effect. 21819370Spst This is preparation for starting or resuming the inferior. */ 21919370Spst 22019370Spstvoid 22198944Sobrienterminal_inferior (void) 22219370Spst{ 22319370Spst if (gdb_has_a_terminal () && terminal_is_ours 22419370Spst && inferior_thisrun_terminal == 0) 22519370Spst { 22619370Spst int result; 22719370Spst 22819370Spst#ifdef F_GETFL 22919370Spst /* Is there a reason this is being done twice? It happens both 23098944Sobrien places we use F_SETFL, so I'm inclined to think perhaps there 23198944Sobrien is some reason, however perverse. Perhaps not though... */ 23219370Spst result = fcntl (0, F_SETFL, tflags_inferior); 23319370Spst result = fcntl (0, F_SETFL, tflags_inferior); 23419370Spst OOPSY ("fcntl F_SETFL"); 23519370Spst#endif 23619370Spst 23719370Spst /* Because we were careful to not change in or out of raw mode in 23898944Sobrien terminal_ours, we will not change in our out of raw mode with 23998944Sobrien this call, so we don't flush any input. */ 24098944Sobrien result = serial_set_tty_state (stdin_serial, inferior_ttystate); 24119370Spst OOPSY ("setting tty state"); 24219370Spst 24319370Spst if (!job_control) 24419370Spst { 24598944Sobrien sigint_ours = (void (*)()) signal (SIGINT, SIG_IGN); 24646283Sdfr#ifdef SIGQUIT 24798944Sobrien sigquit_ours = (void (*)()) signal (SIGQUIT, SIG_IGN); 24846283Sdfr#endif 24919370Spst } 25019370Spst 25119370Spst /* If attach_flag is set, we don't know whether we are sharing a 25298944Sobrien terminal with the inferior or not. (attaching a process 25398944Sobrien without a terminal is one case where we do not; attaching a 25498944Sobrien process which we ran from the same shell as GDB via `&' is 25598944Sobrien one case where we do, I think (but perhaps this is not 25698944Sobrien `sharing' in the sense that we need to save and restore tty 25798944Sobrien state)). I don't know if there is any way to tell whether we 25898944Sobrien are sharing a terminal. So what we do is to go through all 25998944Sobrien the saving and restoring of the tty state, but ignore errors 26098944Sobrien setting the process group, which will happen if we are not 26198944Sobrien sharing a terminal). */ 26219370Spst 26319370Spst if (job_control) 26419370Spst { 26519370Spst#ifdef HAVE_TERMIOS 26619370Spst result = tcsetpgrp (0, inferior_process_group); 26719370Spst if (!attach_flag) 26819370Spst OOPSY ("tcsetpgrp"); 26919370Spst#endif 27019370Spst 27119370Spst#ifdef HAVE_SGTTY 27219370Spst result = ioctl (0, TIOCSPGRP, &inferior_process_group); 27319370Spst if (!attach_flag) 27419370Spst OOPSY ("TIOCSPGRP"); 27519370Spst#endif 27619370Spst } 27719370Spst 27819370Spst } 27919370Spst terminal_is_ours = 0; 28019370Spst} 28119370Spst 28219370Spst/* Put some of our terminal settings into effect, 28319370Spst enough to get proper results from our output, 28419370Spst but do not change into or out of RAW mode 28519370Spst so that no input is discarded. 28619370Spst 28719370Spst After doing this, either terminal_ours or terminal_inferior 28819370Spst should be called to get back to a normal state of affairs. */ 28919370Spst 29019370Spstvoid 29198944Sobrienterminal_ours_for_output (void) 29219370Spst{ 29319370Spst terminal_ours_1 (1); 29419370Spst} 29519370Spst 29619370Spst/* Put our terminal settings into effect. 29719370Spst First record the inferior's terminal settings 29819370Spst so they can be restored properly later. */ 29919370Spst 30019370Spstvoid 30198944Sobrienterminal_ours (void) 30219370Spst{ 30319370Spst terminal_ours_1 (0); 30419370Spst} 30519370Spst 30619370Spst/* output_only is not used, and should not be used unless we introduce 30719370Spst separate terminal_is_ours and terminal_is_ours_for_output 30819370Spst flags. */ 30919370Spst 31019370Spststatic void 31198944Sobrienterminal_ours_1 (int output_only) 31219370Spst{ 31319370Spst /* Checking inferior_thisrun_terminal is necessary so that 31419370Spst if GDB is running in the background, it won't block trying 31519370Spst to do the ioctl()'s below. Checking gdb_has_a_terminal 31619370Spst avoids attempting all the ioctl's when running in batch. */ 31719370Spst if (inferior_thisrun_terminal != 0 || gdb_has_a_terminal () == 0) 31819370Spst return; 31919370Spst 32019370Spst if (!terminal_is_ours) 32119370Spst { 32298944Sobrien#ifdef SIGTTOU 32319370Spst /* Ignore this signal since it will happen when we try to set the 32498944Sobrien pgrp. */ 32598944Sobrien void (*osigttou) () = NULL; 32698944Sobrien#endif 32719370Spst int result; 32819370Spst 32919370Spst terminal_is_ours = 1; 33019370Spst 33119370Spst#ifdef SIGTTOU 33219370Spst if (job_control) 33398944Sobrien osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN); 33419370Spst#endif 33519370Spst 33619370Spst if (inferior_ttystate) 33798944Sobrien xfree (inferior_ttystate); 33898944Sobrien inferior_ttystate = serial_get_tty_state (stdin_serial); 33919370Spst#ifdef HAVE_TERMIOS 34019370Spst inferior_process_group = tcgetpgrp (0); 34119370Spst#endif 34246283Sdfr#ifdef HAVE_TERMIO 34346283Sdfr inferior_process_group = getpgrp (); 34446283Sdfr#endif 34519370Spst#ifdef HAVE_SGTTY 34619370Spst ioctl (0, TIOCGPGRP, &inferior_process_group); 34719370Spst#endif 34819370Spst 34919370Spst /* Here we used to set ICANON in our ttystate, but I believe this 35098944Sobrien was an artifact from before when we used readline. Readline sets 35198944Sobrien the tty state when it needs to. 35298944Sobrien FIXME-maybe: However, query() expects non-raw mode and doesn't 35398944Sobrien use readline. Maybe query should use readline (on the other hand, 35498944Sobrien this only matters for HAVE_SGTTY, not termio or termios, I think). */ 35519370Spst 35619370Spst /* Set tty state to our_ttystate. We don't change in our out of raw 35798944Sobrien mode, to avoid flushing input. We need to do the same thing 35898944Sobrien regardless of output_only, because we don't have separate 35998944Sobrien terminal_is_ours and terminal_is_ours_for_output flags. It's OK, 36098944Sobrien though, since readline will deal with raw mode when/if it needs to. 36198944Sobrien */ 36219370Spst 36398944Sobrien serial_noflush_set_tty_state (stdin_serial, our_ttystate, 36419370Spst inferior_ttystate); 36519370Spst 36619370Spst if (job_control) 36719370Spst { 36819370Spst#ifdef HAVE_TERMIOS 36919370Spst result = tcsetpgrp (0, our_process_group); 37019370Spst#if 0 37119370Spst /* This fails on Ultrix with EINVAL if you run the testsuite 37219370Spst in the background with nohup, and then log out. GDB never 37319370Spst used to check for an error here, so perhaps there are other 37419370Spst such situations as well. */ 37519370Spst if (result == -1) 37619370Spst fprintf_unfiltered (gdb_stderr, "[tcsetpgrp failed in terminal_ours: %s]\n", 37798944Sobrien strerror (errno)); 37819370Spst#endif 37919370Spst#endif /* termios */ 38019370Spst 38119370Spst#ifdef HAVE_SGTTY 38219370Spst result = ioctl (0, TIOCSPGRP, &our_process_group); 38319370Spst#endif 38419370Spst } 38519370Spst 38619370Spst#ifdef SIGTTOU 38719370Spst if (job_control) 38819370Spst signal (SIGTTOU, osigttou); 38919370Spst#endif 39019370Spst 39119370Spst if (!job_control) 39219370Spst { 39319370Spst signal (SIGINT, sigint_ours); 39446283Sdfr#ifdef SIGQUIT 39519370Spst signal (SIGQUIT, sigquit_ours); 39646283Sdfr#endif 39719370Spst } 39819370Spst 39919370Spst#ifdef F_GETFL 40019370Spst tflags_inferior = fcntl (0, F_GETFL, 0); 40119370Spst 40219370Spst /* Is there a reason this is being done twice? It happens both 40398944Sobrien places we use F_SETFL, so I'm inclined to think perhaps there 40498944Sobrien is some reason, however perverse. Perhaps not though... */ 40519370Spst result = fcntl (0, F_SETFL, tflags_ours); 40619370Spst result = fcntl (0, F_SETFL, tflags_ours); 40719370Spst#endif 40819370Spst 40998944Sobrien result = result; /* lint */ 41019370Spst } 41119370Spst} 41219370Spst 41319370Spst/* ARGSUSED */ 41419370Spstvoid 41598944Sobrienterm_info (char *arg, int from_tty) 41619370Spst{ 41719370Spst target_terminal_info (arg, from_tty); 41819370Spst} 41919370Spst 42019370Spst/* ARGSUSED */ 42119370Spstvoid 42298944Sobrienchild_terminal_info (char *args, int from_tty) 42319370Spst{ 42419370Spst if (!gdb_has_a_terminal ()) 42519370Spst { 42619370Spst printf_filtered ("This GDB does not control a terminal.\n"); 42719370Spst return; 42819370Spst } 42919370Spst 43019370Spst printf_filtered ("Inferior's terminal status (currently saved by GDB):\n"); 43119370Spst 43219370Spst /* First the fcntl flags. */ 43319370Spst { 43419370Spst int flags; 43598944Sobrien 43619370Spst flags = tflags_inferior; 43719370Spst 43819370Spst printf_filtered ("File descriptor flags = "); 43919370Spst 44019370Spst#ifndef O_ACCMODE 44119370Spst#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR) 44219370Spst#endif 44319370Spst /* (O_ACCMODE) parens are to avoid Ultrix header file bug */ 44419370Spst switch (flags & (O_ACCMODE)) 44519370Spst { 44698944Sobrien case O_RDONLY: 44798944Sobrien printf_filtered ("O_RDONLY"); 44898944Sobrien break; 44998944Sobrien case O_WRONLY: 45098944Sobrien printf_filtered ("O_WRONLY"); 45198944Sobrien break; 45298944Sobrien case O_RDWR: 45398944Sobrien printf_filtered ("O_RDWR"); 45498944Sobrien break; 45519370Spst } 45619370Spst flags &= ~(O_ACCMODE); 45719370Spst 45819370Spst#ifdef O_NONBLOCK 45998944Sobrien if (flags & O_NONBLOCK) 46019370Spst printf_filtered (" | O_NONBLOCK"); 46119370Spst flags &= ~O_NONBLOCK; 46219370Spst#endif 46398944Sobrien 46419370Spst#if defined (O_NDELAY) 46519370Spst /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will 46619370Spst print it as O_NONBLOCK, which is good cause that is what POSIX 46719370Spst has, and the flag will already be cleared by the time we get here. */ 46819370Spst if (flags & O_NDELAY) 46919370Spst printf_filtered (" | O_NDELAY"); 47019370Spst flags &= ~O_NDELAY; 47119370Spst#endif 47219370Spst 47319370Spst if (flags & O_APPEND) 47419370Spst printf_filtered (" | O_APPEND"); 47519370Spst flags &= ~O_APPEND; 47619370Spst 47719370Spst#if defined (O_BINARY) 47819370Spst if (flags & O_BINARY) 47919370Spst printf_filtered (" | O_BINARY"); 48019370Spst flags &= ~O_BINARY; 48119370Spst#endif 48219370Spst 48319370Spst if (flags) 48419370Spst printf_filtered (" | 0x%x", flags); 48519370Spst printf_filtered ("\n"); 48619370Spst } 48719370Spst 48819370Spst#ifdef PROCESS_GROUP_TYPE 48998944Sobrien printf_filtered ("Process group = %d\n", 49098944Sobrien (int) inferior_process_group); 49119370Spst#endif 49219370Spst 49398944Sobrien serial_print_tty_state (stdin_serial, inferior_ttystate, gdb_stdout); 49419370Spst} 49519370Spst 49619370Spst/* NEW_TTY_PREFORK is called before forking a new child process, 49719370Spst so we can record the state of ttys in the child to be formed. 49819370Spst TTYNAME is null if we are to share the terminal with gdb; 49919370Spst or points to a string containing the name of the desired tty. 50019370Spst 50119370Spst NEW_TTY is called in new child processes under Unix, which will 50219370Spst become debugger target processes. This actually switches to 50319370Spst the terminal specified in the NEW_TTY_PREFORK call. */ 50419370Spst 50519370Spstvoid 50698944Sobriennew_tty_prefork (char *ttyname) 50719370Spst{ 50819370Spst /* Save the name for later, for determining whether we and the child 50919370Spst are sharing a tty. */ 51019370Spst inferior_thisrun_terminal = ttyname; 51119370Spst} 51219370Spst 51319370Spstvoid 51498944Sobriennew_tty (void) 51519370Spst{ 51619370Spst register int tty; 51719370Spst 51819370Spst if (inferior_thisrun_terminal == 0) 51919370Spst return; 52046283Sdfr#if !defined(__GO32__) && !defined(_WIN32) 52119370Spst#ifdef TIOCNOTTY 52219370Spst /* Disconnect the child process from our controlling terminal. On some 52319370Spst systems (SVR4 for example), this may cause a SIGTTOU, so temporarily 52419370Spst ignore SIGTTOU. */ 52598944Sobrien tty = open ("/dev/tty", O_RDWR); 52619370Spst if (tty > 0) 52719370Spst { 52819370Spst void (*osigttou) (); 52919370Spst 53098944Sobrien osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN); 53198944Sobrien ioctl (tty, TIOCNOTTY, 0); 53298944Sobrien close (tty); 53398944Sobrien signal (SIGTTOU, osigttou); 53419370Spst } 53519370Spst#endif 53619370Spst 53719370Spst /* Now open the specified new terminal. */ 53819370Spst 53919370Spst#ifdef USE_O_NOCTTY 54098944Sobrien tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY); 54119370Spst#else 54298944Sobrien tty = open (inferior_thisrun_terminal, O_RDWR); 54319370Spst#endif 54419370Spst if (tty == -1) 54519370Spst { 54619370Spst print_sys_errmsg (inferior_thisrun_terminal, errno); 54798944Sobrien _exit (1); 54819370Spst } 54919370Spst 55019370Spst /* Avoid use of dup2; doesn't exist on all systems. */ 55119370Spst if (tty != 0) 55298944Sobrien { 55398944Sobrien close (0); 55498944Sobrien dup (tty); 55598944Sobrien } 55619370Spst if (tty != 1) 55798944Sobrien { 55898944Sobrien close (1); 55998944Sobrien dup (tty); 56098944Sobrien } 56119370Spst if (tty != 2) 56298944Sobrien { 56398944Sobrien close (2); 56498944Sobrien dup (tty); 56598944Sobrien } 56619370Spst if (tty > 2) 56798944Sobrien close (tty); 56898944Sobrien#endif /* !go32 && !win32 */ 56919370Spst} 57019370Spst 57119370Spst/* Kill the inferior process. Make us have no inferior. */ 57219370Spst 57319370Spst/* ARGSUSED */ 57419370Spststatic void 57598944Sobrienkill_command (char *arg, int from_tty) 57619370Spst{ 57798944Sobrien /* FIXME: This should not really be inferior_ptid (or target_has_execution). 57819370Spst It should be a distinct flag that indicates that a target is active, cuz 57919370Spst some targets don't have processes! */ 58019370Spst 58198944Sobrien if (ptid_equal (inferior_ptid, null_ptid)) 58219370Spst error ("The program is not being run."); 58319370Spst if (!query ("Kill the program being debugged? ")) 58419370Spst error ("Not confirmed."); 58519370Spst target_kill (); 58619370Spst 58798944Sobrien init_thread_list (); /* Destroy thread info */ 58819370Spst 58919370Spst /* Killing off the inferior can leave us with a core file. If so, 59019370Spst print the state we are left in. */ 59198944Sobrien if (target_has_stack) 59298944Sobrien { 59398944Sobrien printf_filtered ("In %s,\n", target_longname); 59498944Sobrien if (selected_frame == NULL) 59598944Sobrien fputs_filtered ("No selected stack frame.\n", gdb_stdout); 59698944Sobrien else 59798944Sobrien print_stack_frame (selected_frame, selected_frame_level, 1); 59898944Sobrien } 59919370Spst} 60019370Spst 60119370Spst/* Call set_sigint_trap when you need to pass a signal on to an attached 60219370Spst process when handling SIGINT */ 60319370Spst 60419370Spst/* ARGSUSED */ 60519370Spststatic void 60698944Sobrienpass_signal (int signo) 60719370Spst{ 60846283Sdfr#ifndef _WIN32 60998944Sobrien kill (PIDGET (inferior_ptid), SIGINT); 61046283Sdfr#endif 61119370Spst} 61219370Spst 61398944Sobrienstatic void (*osig) (); 61419370Spst 61519370Spstvoid 61698944Sobrienset_sigint_trap (void) 61719370Spst{ 61819370Spst if (attach_flag || inferior_thisrun_terminal) 61919370Spst { 62098944Sobrien osig = (void (*)()) signal (SIGINT, pass_signal); 62119370Spst } 62219370Spst} 62319370Spst 62419370Spstvoid 62598944Sobrienclear_sigint_trap (void) 62619370Spst{ 62719370Spst if (attach_flag || inferior_thisrun_terminal) 62819370Spst { 62919370Spst signal (SIGINT, osig); 63019370Spst } 63119370Spst} 63219370Spst 63319370Spst#if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN) 63419370Spststatic void (*old_sigio) (); 63519370Spst 63619370Spststatic void 63798944Sobrienhandle_sigio (int signo) 63819370Spst{ 63919370Spst int numfds; 64019370Spst fd_set readfds; 64119370Spst 64219370Spst signal (SIGIO, handle_sigio); 64319370Spst 64419370Spst FD_ZERO (&readfds); 64519370Spst FD_SET (target_activity_fd, &readfds); 64619370Spst numfds = select (target_activity_fd + 1, &readfds, NULL, NULL, NULL); 64719370Spst if (numfds >= 0 && FD_ISSET (target_activity_fd, &readfds)) 64819370Spst { 64946283Sdfr#ifndef _WIN32 65019370Spst if ((*target_activity_function) ()) 65198944Sobrien kill (PIDGET (inferior_ptid), SIGINT); 65246283Sdfr#endif 65319370Spst } 65419370Spst} 65519370Spst 65619370Spststatic int old_fcntl_flags; 65719370Spst 65819370Spstvoid 65998944Sobrienset_sigio_trap (void) 66019370Spst{ 66119370Spst if (target_activity_function) 66219370Spst { 66398944Sobrien old_sigio = (void (*)()) signal (SIGIO, handle_sigio); 66498944Sobrien fcntl (target_activity_fd, F_SETOWN, getpid ()); 66519370Spst old_fcntl_flags = fcntl (target_activity_fd, F_GETFL, 0); 66619370Spst fcntl (target_activity_fd, F_SETFL, old_fcntl_flags | FASYNC); 66719370Spst } 66819370Spst} 66919370Spst 67019370Spstvoid 67198944Sobrienclear_sigio_trap (void) 67219370Spst{ 67319370Spst if (target_activity_function) 67419370Spst { 67519370Spst signal (SIGIO, old_sigio); 67619370Spst fcntl (target_activity_fd, F_SETFL, old_fcntl_flags); 67719370Spst } 67819370Spst} 67919370Spst#else /* No SIGIO. */ 68019370Spstvoid 68198944Sobrienset_sigio_trap (void) 68219370Spst{ 68319370Spst if (target_activity_function) 68498944Sobrien internal_error (__FILE__, __LINE__, "failed internal consistency check"); 68519370Spst} 68619370Spst 68719370Spstvoid 68898944Sobrienclear_sigio_trap (void) 68919370Spst{ 69019370Spst if (target_activity_function) 69198944Sobrien internal_error (__FILE__, __LINE__, "failed internal consistency check"); 69219370Spst} 69319370Spst#endif /* No SIGIO. */ 69419370Spst 69519370Spst 69619370Spst/* This is here because this is where we figure out whether we (probably) 69719370Spst have job control. Just using job_control only does part of it because 69819370Spst setpgid or setpgrp might not exist on a system without job control. 69919370Spst It might be considered misplaced (on the other hand, process groups and 70019370Spst job control are closely related to ttys). 70119370Spst 70219370Spst For a more clean implementation, in libiberty, put a setpgid which merely 70319370Spst calls setpgrp and a setpgrp which does nothing (any system with job control 70419370Spst will have one or the other). */ 70519370Spstint 70698944Sobriengdb_setpgid (void) 70719370Spst{ 70819370Spst int retval = 0; 70919370Spst 71019370Spst if (job_control) 71119370Spst { 71298944Sobrien#if defined (HAVE_TERMIOS) || defined (TIOCGPGRP) 71398944Sobrien#ifdef HAVE_SETPGID 71498944Sobrien /* The call setpgid (0, 0) is supposed to work and mean the same 71598944Sobrien thing as this, but on Ultrix 4.2A it fails with EPERM (and 71698944Sobrien setpgid (getpid (), getpid ()) succeeds). */ 71719370Spst retval = setpgid (getpid (), getpid ()); 71819370Spst#else 71998944Sobrien#ifdef HAVE_SETPGRP 72098944Sobrien#ifdef SETPGRP_VOID 72119370Spst retval = setpgrp (); 72219370Spst#else 72319370Spst retval = setpgrp (getpid (), getpid ()); 72498944Sobrien#endif 72598944Sobrien#endif /* HAVE_SETPGRP */ 72698944Sobrien#endif /* HAVE_SETPGID */ 72798944Sobrien#endif /* defined (HAVE_TERMIOS) || defined (TIOCGPGRP) */ 72819370Spst } 72998944Sobrien 73019370Spst return retval; 73119370Spst} 73219370Spst 73319370Spstvoid 73498944Sobrien_initialize_inflow (void) 73519370Spst{ 73619370Spst add_info ("terminal", term_info, 73798944Sobrien "Print inferior's saved terminal status."); 73819370Spst 73919370Spst add_com ("kill", class_run, kill_command, 74019370Spst "Kill execution of program being debugged."); 74119370Spst 74298944Sobrien inferior_ptid = null_ptid; 74319370Spst 74419370Spst terminal_is_ours = 1; 74519370Spst 74619370Spst /* OK, figure out whether we have job control. If neither termios nor 74719370Spst sgtty (i.e. termio or go32), leave job_control 0. */ 74819370Spst 74919370Spst#if defined (HAVE_TERMIOS) 75019370Spst /* Do all systems with termios have the POSIX way of identifying job 75119370Spst control? I hope so. */ 75219370Spst#ifdef _POSIX_JOB_CONTROL 75319370Spst job_control = 1; 75419370Spst#else 75519370Spst#ifdef _SC_JOB_CONTROL 75619370Spst job_control = sysconf (_SC_JOB_CONTROL); 75719370Spst#else 75898944Sobrien job_control = 0; /* have to assume the worst */ 75998944Sobrien#endif /* _SC_JOB_CONTROL */ 76098944Sobrien#endif /* _POSIX_JOB_CONTROL */ 76198944Sobrien#endif /* HAVE_TERMIOS */ 76219370Spst 76319370Spst#ifdef HAVE_SGTTY 76419370Spst#ifdef TIOCGPGRP 76519370Spst job_control = 1; 76619370Spst#else 76719370Spst job_control = 0; 76819370Spst#endif /* TIOCGPGRP */ 76919370Spst#endif /* sgtty */ 77019370Spst} 771