inflow.c revision 46283
119370Spst/* Low level interface to ptrace, for GDB when running under Unix. 246283Sdfr Copyright 1986-87, 1989, 1991-92, 1995, 1998 Free Software Foundation, Inc. 319370Spst 419370SpstThis file is part of GDB. 519370Spst 619370SpstThis program is free software; you can redistribute it and/or modify 719370Spstit under the terms of the GNU General Public License as published by 819370Spstthe Free Software Foundation; either version 2 of the License, or 919370Spst(at your option) any later version. 1019370Spst 1119370SpstThis program is distributed in the hope that it will be useful, 1219370Spstbut WITHOUT ANY WARRANTY; without even the implied warranty of 1319370SpstMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1419370SpstGNU General Public License for more details. 1519370Spst 1619370SpstYou should have received a copy of the GNU General Public License 1719370Spstalong with this program; if not, write to the Free Software 1819370SpstFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 1919370Spst 2019370Spst#include "defs.h" 2119370Spst#include "frame.h" 2219370Spst#include "inferior.h" 2319370Spst#include "command.h" 2419370Spst#include "signals.h" 2519370Spst#include "serial.h" 2619370Spst#include "terminal.h" 2719370Spst#include "target.h" 2846283Sdfr#include "gdbthread.h" 2919370Spst 3019370Spst#include "gdb_string.h" 3119370Spst#include <signal.h> 3219370Spst#include <fcntl.h> 3319370Spst#ifdef HAVE_UNISTD_H 3419370Spst#include <unistd.h> 3519370Spst#endif 3619370Spst 3719370Spst#ifdef HAVE_TERMIOS 3819370Spst#define PROCESS_GROUP_TYPE pid_t 3919370Spst#endif 4019370Spst 4146283Sdfr#ifdef HAVE_TERMIO 4246283Sdfr#define PROCESS_GROUP_TYPE int 4346283Sdfr#endif 4446283Sdfr 4519370Spst#ifdef HAVE_SGTTY 4619370Spst#ifdef SHORT_PGRP 4719370Spst/* This is only used for the ultra. Does it have pid_t? */ 4819370Spst#define PROCESS_GROUP_TYPE short 4919370Spst#else 5019370Spst#define PROCESS_GROUP_TYPE int 5119370Spst#endif 5219370Spst#endif /* sgtty */ 5319370Spst 5446283Sdfr#if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN) 5519370Spststatic void 5646283Sdfrhandle_sigio PARAMS ((int)); 5746283Sdfr#endif 5846283Sdfr 5946283Sdfrstatic void 6046283Sdfrpass_signal PARAMS ((int)); 6146283Sdfr 6246283Sdfrstatic void 6319370Spstkill_command PARAMS ((char *, int)); 6419370Spst 6519370Spststatic void 6619370Spstterminal_ours_1 PARAMS ((int)); 6719370Spst 6819370Spst/* Record terminal status separately for debugger and inferior. */ 6919370Spst 7019370Spststatic serial_t stdin_serial; 7119370Spst 7219370Spst/* TTY state for the inferior. We save it whenever the inferior stops, and 7319370Spst restore it when it resumes. */ 7419370Spststatic serial_ttystate inferior_ttystate; 7519370Spst 7619370Spst/* Our own tty state, which we restore every time we need to deal with the 7719370Spst terminal. We only set it once, when GDB first starts. The settings of 7819370Spst flags which readline saves and restores and unimportant. */ 7919370Spststatic serial_ttystate our_ttystate; 8019370Spst 8119370Spst/* fcntl flags for us and the inferior. Saved and restored just like 8219370Spst {our,inferior}_ttystate. */ 8319370Spststatic int tflags_inferior; 8419370Spststatic int tflags_ours; 8519370Spst 8619370Spst#ifdef PROCESS_GROUP_TYPE 8719370Spst/* Process group for us and the inferior. Saved and restored just like 8819370Spst {our,inferior}_ttystate. */ 8919370SpstPROCESS_GROUP_TYPE our_process_group; 9019370SpstPROCESS_GROUP_TYPE inferior_process_group; 9119370Spst#endif 9219370Spst 9319370Spst/* While the inferior is running, we want SIGINT and SIGQUIT to go to the 9419370Spst inferior only. If we have job control, that takes care of it. If not, 9519370Spst we save our handlers in these two variables and set SIGINT and SIGQUIT 9619370Spst to SIG_IGN. */ 9746283Sdfr 9819370Spststatic void (*sigint_ours) (); 9919370Spststatic void (*sigquit_ours) (); 10019370Spst 10119370Spst/* The name of the tty (from the `tty' command) that we gave to the inferior 10219370Spst when it was last started. */ 10319370Spst 10419370Spststatic char *inferior_thisrun_terminal; 10519370Spst 10619370Spst/* Nonzero if our terminal settings are in effect. Zero if the 10719370Spst inferior's settings are in effect. Ignored if !gdb_has_a_terminal 10819370Spst (). */ 10919370Spst 11046283Sdfrint terminal_is_ours; 11119370Spst 11219370Spstenum {yes, no, have_not_checked} gdb_has_a_terminal_flag = have_not_checked; 11319370Spst 11419370Spst/* Does GDB have a terminal (on stdin)? */ 11519370Spstint 11619370Spstgdb_has_a_terminal () 11719370Spst{ 11819370Spst switch (gdb_has_a_terminal_flag) 11919370Spst { 12019370Spst case yes: 12119370Spst return 1; 12219370Spst case no: 12319370Spst return 0; 12419370Spst case have_not_checked: 12519370Spst /* Get all the current tty settings (including whether we have a tty at 12619370Spst all!). Can't do this in _initialize_inflow because SERIAL_FDOPEN 12719370Spst won't work until the serial_ops_list is initialized. */ 12819370Spst 12919370Spst#ifdef F_GETFL 13019370Spst tflags_ours = fcntl (0, F_GETFL, 0); 13119370Spst#endif 13219370Spst 13319370Spst gdb_has_a_terminal_flag = no; 13419370Spst stdin_serial = SERIAL_FDOPEN (0); 13519370Spst if (stdin_serial != NULL) 13619370Spst { 13719370Spst our_ttystate = SERIAL_GET_TTY_STATE (stdin_serial); 13819370Spst 13919370Spst if (our_ttystate != NULL) 14019370Spst { 14119370Spst gdb_has_a_terminal_flag = yes; 14219370Spst#ifdef HAVE_TERMIOS 14319370Spst our_process_group = tcgetpgrp (0); 14419370Spst#endif 14546283Sdfr#ifdef HAVE_TERMIO 14646283Sdfr our_process_group = getpgrp (); 14746283Sdfr#endif 14819370Spst#ifdef HAVE_SGTTY 14919370Spst ioctl (0, TIOCGPGRP, &our_process_group); 15019370Spst#endif 15119370Spst } 15219370Spst } 15319370Spst 15419370Spst return gdb_has_a_terminal_flag == yes; 15519370Spst default: 15619370Spst /* "Can't happen". */ 15719370Spst return 0; 15819370Spst } 15919370Spst} 16019370Spst 16119370Spst/* Macro for printing errors from ioctl operations */ 16219370Spst 16319370Spst#define OOPSY(what) \ 16419370Spst if (result == -1) \ 16519370Spst fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \ 16619370Spst what, strerror (errno)) 16719370Spst 16819370Spststatic void terminal_ours_1 PARAMS ((int)); 16919370Spst 17019370Spst/* Initialize the terminal settings we record for the inferior, 17119370Spst before we actually run the inferior. */ 17219370Spst 17319370Spstvoid 17446283Sdfrterminal_init_inferior_with_pgrp (pgrp) 17546283Sdfr int pgrp; 17619370Spst{ 17719370Spst if (gdb_has_a_terminal ()) 17819370Spst { 17919370Spst /* We could just as well copy our_ttystate (if we felt like adding 18019370Spst a new function SERIAL_COPY_TTY_STATE). */ 18119370Spst if (inferior_ttystate) 18219370Spst free (inferior_ttystate); 18319370Spst inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial); 18446283Sdfr 18519370Spst#ifdef PROCESS_GROUP_TYPE 18646283Sdfr inferior_process_group = pgrp; 18719370Spst#endif 18819370Spst 18919370Spst /* Make sure that next time we call terminal_inferior (which will be 19019370Spst before the program runs, as it needs to be), we install the new 19119370Spst process group. */ 19219370Spst terminal_is_ours = 1; 19319370Spst } 19419370Spst} 19519370Spst 19646283Sdfrvoid 19746283Sdfrterminal_init_inferior () 19846283Sdfr{ 19946283Sdfr#ifdef PROCESS_GROUP_TYPE 20046283Sdfr /* This is for Lynx, and should be cleaned up by having Lynx be a separate 20146283Sdfr debugging target with a version of target_terminal_init_inferior which 20246283Sdfr passes in the process group to a generic routine which does all the work 20346283Sdfr (and the non-threaded child_terminal_init_inferior can just pass in 20446283Sdfr inferior_pid to the same routine). */ 20546283Sdfr /* We assume INFERIOR_PID is also the child's process group. */ 20646283Sdfr terminal_init_inferior_with_pgrp (PIDGET (inferior_pid)); 20746283Sdfr#endif /* PROCESS_GROUP_TYPE */ 20846283Sdfr} 20946283Sdfr 21019370Spst/* Put the inferior's terminal settings into effect. 21119370Spst This is preparation for starting or resuming the inferior. */ 21219370Spst 21319370Spstvoid 21419370Spstterminal_inferior () 21519370Spst{ 21619370Spst if (gdb_has_a_terminal () && terminal_is_ours 21719370Spst && inferior_thisrun_terminal == 0) 21819370Spst { 21919370Spst int result; 22019370Spst 22119370Spst#ifdef F_GETFL 22219370Spst /* Is there a reason this is being done twice? It happens both 22319370Spst places we use F_SETFL, so I'm inclined to think perhaps there 22419370Spst is some reason, however perverse. Perhaps not though... */ 22519370Spst result = fcntl (0, F_SETFL, tflags_inferior); 22619370Spst result = fcntl (0, F_SETFL, tflags_inferior); 22719370Spst OOPSY ("fcntl F_SETFL"); 22819370Spst#endif 22919370Spst 23019370Spst /* Because we were careful to not change in or out of raw mode in 23119370Spst terminal_ours, we will not change in our out of raw mode with 23219370Spst this call, so we don't flush any input. */ 23319370Spst result = SERIAL_SET_TTY_STATE (stdin_serial, inferior_ttystate); 23419370Spst OOPSY ("setting tty state"); 23519370Spst 23619370Spst if (!job_control) 23719370Spst { 23819370Spst sigint_ours = (void (*) ()) signal (SIGINT, SIG_IGN); 23946283Sdfr#ifdef SIGQUIT 24019370Spst sigquit_ours = (void (*) ()) signal (SIGQUIT, SIG_IGN); 24146283Sdfr#endif 24219370Spst } 24319370Spst 24419370Spst /* If attach_flag is set, we don't know whether we are sharing a 24519370Spst terminal with the inferior or not. (attaching a process 24619370Spst without a terminal is one case where we do not; attaching a 24719370Spst process which we ran from the same shell as GDB via `&' is 24819370Spst one case where we do, I think (but perhaps this is not 24919370Spst `sharing' in the sense that we need to save and restore tty 25019370Spst state)). I don't know if there is any way to tell whether we 25119370Spst are sharing a terminal. So what we do is to go through all 25219370Spst the saving and restoring of the tty state, but ignore errors 25319370Spst setting the process group, which will happen if we are not 25419370Spst sharing a terminal). */ 25519370Spst 25619370Spst if (job_control) 25719370Spst { 25819370Spst#ifdef HAVE_TERMIOS 25919370Spst result = tcsetpgrp (0, inferior_process_group); 26019370Spst if (!attach_flag) 26119370Spst OOPSY ("tcsetpgrp"); 26219370Spst#endif 26319370Spst 26419370Spst#ifdef HAVE_SGTTY 26519370Spst result = ioctl (0, TIOCSPGRP, &inferior_process_group); 26619370Spst if (!attach_flag) 26719370Spst OOPSY ("TIOCSPGRP"); 26819370Spst#endif 26919370Spst } 27019370Spst 27119370Spst } 27219370Spst terminal_is_ours = 0; 27319370Spst} 27419370Spst 27519370Spst/* Put some of our terminal settings into effect, 27619370Spst enough to get proper results from our output, 27719370Spst but do not change into or out of RAW mode 27819370Spst so that no input is discarded. 27919370Spst 28019370Spst After doing this, either terminal_ours or terminal_inferior 28119370Spst should be called to get back to a normal state of affairs. */ 28219370Spst 28319370Spstvoid 28419370Spstterminal_ours_for_output () 28519370Spst{ 28619370Spst terminal_ours_1 (1); 28719370Spst} 28819370Spst 28919370Spst/* Put our terminal settings into effect. 29019370Spst First record the inferior's terminal settings 29119370Spst so they can be restored properly later. */ 29219370Spst 29319370Spstvoid 29419370Spstterminal_ours () 29519370Spst{ 29619370Spst terminal_ours_1 (0); 29719370Spst} 29819370Spst 29919370Spst/* output_only is not used, and should not be used unless we introduce 30019370Spst separate terminal_is_ours and terminal_is_ours_for_output 30119370Spst flags. */ 30219370Spst 30319370Spststatic void 30419370Spstterminal_ours_1 (output_only) 30519370Spst int output_only; 30619370Spst{ 30719370Spst /* Checking inferior_thisrun_terminal is necessary so that 30819370Spst if GDB is running in the background, it won't block trying 30919370Spst to do the ioctl()'s below. Checking gdb_has_a_terminal 31019370Spst avoids attempting all the ioctl's when running in batch. */ 31119370Spst if (inferior_thisrun_terminal != 0 || gdb_has_a_terminal () == 0) 31219370Spst return; 31319370Spst 31419370Spst if (!terminal_is_ours) 31519370Spst { 31619370Spst /* Ignore this signal since it will happen when we try to set the 31719370Spst pgrp. */ 31819370Spst void (*osigttou) (); 31919370Spst int result; 32019370Spst 32119370Spst terminal_is_ours = 1; 32219370Spst 32319370Spst#ifdef SIGTTOU 32419370Spst if (job_control) 32519370Spst osigttou = (void (*) ()) signal (SIGTTOU, SIG_IGN); 32619370Spst#endif 32719370Spst 32819370Spst if (inferior_ttystate) 32919370Spst free (inferior_ttystate); 33019370Spst inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial); 33119370Spst#ifdef HAVE_TERMIOS 33219370Spst inferior_process_group = tcgetpgrp (0); 33319370Spst#endif 33446283Sdfr#ifdef HAVE_TERMIO 33546283Sdfr inferior_process_group = getpgrp (); 33646283Sdfr#endif 33719370Spst#ifdef HAVE_SGTTY 33819370Spst ioctl (0, TIOCGPGRP, &inferior_process_group); 33919370Spst#endif 34019370Spst 34119370Spst /* Here we used to set ICANON in our ttystate, but I believe this 34219370Spst was an artifact from before when we used readline. Readline sets 34319370Spst the tty state when it needs to. 34419370Spst FIXME-maybe: However, query() expects non-raw mode and doesn't 34519370Spst use readline. Maybe query should use readline (on the other hand, 34619370Spst this only matters for HAVE_SGTTY, not termio or termios, I think). */ 34719370Spst 34819370Spst /* Set tty state to our_ttystate. We don't change in our out of raw 34919370Spst mode, to avoid flushing input. We need to do the same thing 35019370Spst regardless of output_only, because we don't have separate 35119370Spst terminal_is_ours and terminal_is_ours_for_output flags. It's OK, 35219370Spst though, since readline will deal with raw mode when/if it needs to. 35319370Spst */ 35419370Spst 35519370Spst SERIAL_NOFLUSH_SET_TTY_STATE (stdin_serial, our_ttystate, 35619370Spst inferior_ttystate); 35719370Spst 35819370Spst if (job_control) 35919370Spst { 36019370Spst#ifdef HAVE_TERMIOS 36119370Spst result = tcsetpgrp (0, our_process_group); 36219370Spst#if 0 36319370Spst /* This fails on Ultrix with EINVAL if you run the testsuite 36419370Spst in the background with nohup, and then log out. GDB never 36519370Spst used to check for an error here, so perhaps there are other 36619370Spst such situations as well. */ 36719370Spst if (result == -1) 36819370Spst fprintf_unfiltered (gdb_stderr, "[tcsetpgrp failed in terminal_ours: %s]\n", 36919370Spst strerror (errno)); 37019370Spst#endif 37119370Spst#endif /* termios */ 37219370Spst 37319370Spst#ifdef HAVE_SGTTY 37419370Spst result = ioctl (0, TIOCSPGRP, &our_process_group); 37519370Spst#endif 37619370Spst } 37719370Spst 37819370Spst#ifdef SIGTTOU 37919370Spst if (job_control) 38019370Spst signal (SIGTTOU, osigttou); 38119370Spst#endif 38219370Spst 38319370Spst if (!job_control) 38419370Spst { 38519370Spst signal (SIGINT, sigint_ours); 38646283Sdfr#ifdef SIGQUIT 38719370Spst signal (SIGQUIT, sigquit_ours); 38846283Sdfr#endif 38919370Spst } 39019370Spst 39119370Spst#ifdef F_GETFL 39219370Spst tflags_inferior = fcntl (0, F_GETFL, 0); 39319370Spst 39419370Spst /* Is there a reason this is being done twice? It happens both 39519370Spst places we use F_SETFL, so I'm inclined to think perhaps there 39619370Spst is some reason, however perverse. Perhaps not though... */ 39719370Spst result = fcntl (0, F_SETFL, tflags_ours); 39819370Spst result = fcntl (0, F_SETFL, tflags_ours); 39919370Spst#endif 40019370Spst 40119370Spst result = result; /* lint */ 40219370Spst } 40319370Spst} 40419370Spst 40519370Spst/* ARGSUSED */ 40619370Spstvoid 40719370Spstterm_info (arg, from_tty) 40819370Spst char *arg; 40919370Spst int from_tty; 41019370Spst{ 41119370Spst target_terminal_info (arg, from_tty); 41219370Spst} 41319370Spst 41419370Spst/* ARGSUSED */ 41519370Spstvoid 41619370Spstchild_terminal_info (args, from_tty) 41719370Spst char *args; 41819370Spst int from_tty; 41919370Spst{ 42019370Spst if (!gdb_has_a_terminal ()) 42119370Spst { 42219370Spst printf_filtered ("This GDB does not control a terminal.\n"); 42319370Spst return; 42419370Spst } 42519370Spst 42619370Spst printf_filtered ("Inferior's terminal status (currently saved by GDB):\n"); 42719370Spst 42819370Spst /* First the fcntl flags. */ 42919370Spst { 43019370Spst int flags; 43119370Spst 43219370Spst flags = tflags_inferior; 43319370Spst 43419370Spst printf_filtered ("File descriptor flags = "); 43519370Spst 43619370Spst#ifndef O_ACCMODE 43719370Spst#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR) 43819370Spst#endif 43919370Spst /* (O_ACCMODE) parens are to avoid Ultrix header file bug */ 44019370Spst switch (flags & (O_ACCMODE)) 44119370Spst { 44219370Spst case O_RDONLY: printf_filtered ("O_RDONLY"); break; 44319370Spst case O_WRONLY: printf_filtered ("O_WRONLY"); break; 44419370Spst case O_RDWR: printf_filtered ("O_RDWR"); break; 44519370Spst } 44619370Spst flags &= ~(O_ACCMODE); 44719370Spst 44819370Spst#ifdef O_NONBLOCK 44919370Spst if (flags & O_NONBLOCK) 45019370Spst printf_filtered (" | O_NONBLOCK"); 45119370Spst flags &= ~O_NONBLOCK; 45219370Spst#endif 45319370Spst 45419370Spst#if defined (O_NDELAY) 45519370Spst /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will 45619370Spst print it as O_NONBLOCK, which is good cause that is what POSIX 45719370Spst has, and the flag will already be cleared by the time we get here. */ 45819370Spst if (flags & O_NDELAY) 45919370Spst printf_filtered (" | O_NDELAY"); 46019370Spst flags &= ~O_NDELAY; 46119370Spst#endif 46219370Spst 46319370Spst if (flags & O_APPEND) 46419370Spst printf_filtered (" | O_APPEND"); 46519370Spst flags &= ~O_APPEND; 46619370Spst 46719370Spst#if defined (O_BINARY) 46819370Spst if (flags & O_BINARY) 46919370Spst printf_filtered (" | O_BINARY"); 47019370Spst flags &= ~O_BINARY; 47119370Spst#endif 47219370Spst 47319370Spst if (flags) 47419370Spst printf_filtered (" | 0x%x", flags); 47519370Spst printf_filtered ("\n"); 47619370Spst } 47719370Spst 47819370Spst#ifdef PROCESS_GROUP_TYPE 47919370Spst printf_filtered ("Process group = %d\n", inferior_process_group); 48019370Spst#endif 48119370Spst 48219370Spst SERIAL_PRINT_TTY_STATE (stdin_serial, inferior_ttystate); 48319370Spst} 48419370Spst 48519370Spst/* NEW_TTY_PREFORK is called before forking a new child process, 48619370Spst so we can record the state of ttys in the child to be formed. 48719370Spst TTYNAME is null if we are to share the terminal with gdb; 48819370Spst or points to a string containing the name of the desired tty. 48919370Spst 49019370Spst NEW_TTY is called in new child processes under Unix, which will 49119370Spst become debugger target processes. This actually switches to 49219370Spst the terminal specified in the NEW_TTY_PREFORK call. */ 49319370Spst 49419370Spstvoid 49519370Spstnew_tty_prefork (ttyname) 49619370Spst char *ttyname; 49719370Spst{ 49819370Spst /* Save the name for later, for determining whether we and the child 49919370Spst are sharing a tty. */ 50019370Spst inferior_thisrun_terminal = ttyname; 50119370Spst} 50219370Spst 50319370Spstvoid 50419370Spstnew_tty () 50519370Spst{ 50619370Spst register int tty; 50719370Spst 50819370Spst if (inferior_thisrun_terminal == 0) 50919370Spst return; 51046283Sdfr#if !defined(__GO32__) && !defined(_WIN32) 51119370Spst#ifdef TIOCNOTTY 51219370Spst /* Disconnect the child process from our controlling terminal. On some 51319370Spst systems (SVR4 for example), this may cause a SIGTTOU, so temporarily 51419370Spst ignore SIGTTOU. */ 51519370Spst tty = open("/dev/tty", O_RDWR); 51619370Spst if (tty > 0) 51719370Spst { 51819370Spst void (*osigttou) (); 51919370Spst 52019370Spst osigttou = (void (*)()) signal(SIGTTOU, SIG_IGN); 52119370Spst ioctl(tty, TIOCNOTTY, 0); 52219370Spst close(tty); 52319370Spst signal(SIGTTOU, osigttou); 52419370Spst } 52519370Spst#endif 52619370Spst 52719370Spst /* Now open the specified new terminal. */ 52819370Spst 52919370Spst#ifdef USE_O_NOCTTY 53019370Spst tty = open(inferior_thisrun_terminal, O_RDWR | O_NOCTTY); 53119370Spst#else 53219370Spst tty = open(inferior_thisrun_terminal, O_RDWR); 53319370Spst#endif 53419370Spst if (tty == -1) 53519370Spst { 53619370Spst print_sys_errmsg (inferior_thisrun_terminal, errno); 53719370Spst _exit(1); 53819370Spst } 53919370Spst 54019370Spst /* Avoid use of dup2; doesn't exist on all systems. */ 54119370Spst if (tty != 0) 54219370Spst { close (0); dup (tty); } 54319370Spst if (tty != 1) 54419370Spst { close (1); dup (tty); } 54519370Spst if (tty != 2) 54619370Spst { close (2); dup (tty); } 54719370Spst if (tty > 2) 54819370Spst close(tty); 54919370Spst#endif /* !go32 && !win32*/ 55019370Spst} 55119370Spst 55219370Spst/* Kill the inferior process. Make us have no inferior. */ 55319370Spst 55419370Spst/* ARGSUSED */ 55519370Spststatic void 55619370Spstkill_command (arg, from_tty) 55719370Spst char *arg; 55819370Spst int from_tty; 55919370Spst{ 56019370Spst /* FIXME: This should not really be inferior_pid (or target_has_execution). 56119370Spst It should be a distinct flag that indicates that a target is active, cuz 56219370Spst some targets don't have processes! */ 56319370Spst 56419370Spst if (inferior_pid == 0) 56519370Spst error ("The program is not being run."); 56619370Spst if (!query ("Kill the program being debugged? ")) 56719370Spst error ("Not confirmed."); 56819370Spst target_kill (); 56919370Spst 57019370Spst init_thread_list(); /* Destroy thread info */ 57119370Spst 57219370Spst /* Killing off the inferior can leave us with a core file. If so, 57319370Spst print the state we are left in. */ 57419370Spst if (target_has_stack) { 57519370Spst printf_filtered ("In %s,\n", target_longname); 57619370Spst if (selected_frame == NULL) 57719370Spst fputs_filtered ("No selected stack frame.\n", gdb_stdout); 57819370Spst else 57919370Spst print_stack_frame (selected_frame, selected_frame_level, 1); 58019370Spst } 58119370Spst} 58219370Spst 58319370Spst/* Call set_sigint_trap when you need to pass a signal on to an attached 58419370Spst process when handling SIGINT */ 58519370Spst 58619370Spst/* ARGSUSED */ 58719370Spststatic void 58819370Spstpass_signal (signo) 58919370Spst int signo; 59019370Spst{ 59146283Sdfr#ifndef _WIN32 59246283Sdfr kill (PIDGET (inferior_pid), SIGINT); 59346283Sdfr#endif 59419370Spst} 59519370Spst 59619370Spststatic void (*osig)(); 59719370Spst 59819370Spstvoid 59919370Spstset_sigint_trap() 60019370Spst{ 60119370Spst if (attach_flag || inferior_thisrun_terminal) 60219370Spst { 60319370Spst osig = (void (*) ()) signal (SIGINT, pass_signal); 60419370Spst } 60519370Spst} 60619370Spst 60719370Spstvoid 60819370Spstclear_sigint_trap() 60919370Spst{ 61019370Spst if (attach_flag || inferior_thisrun_terminal) 61119370Spst { 61219370Spst signal (SIGINT, osig); 61319370Spst } 61419370Spst} 61519370Spst 61619370Spst#if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN) 61719370Spststatic void (*old_sigio) (); 61819370Spst 61919370Spststatic void 62019370Spsthandle_sigio (signo) 62119370Spst int signo; 62219370Spst{ 62319370Spst int numfds; 62419370Spst fd_set readfds; 62519370Spst 62619370Spst signal (SIGIO, handle_sigio); 62719370Spst 62819370Spst FD_ZERO (&readfds); 62919370Spst FD_SET (target_activity_fd, &readfds); 63019370Spst numfds = select (target_activity_fd + 1, &readfds, NULL, NULL, NULL); 63119370Spst if (numfds >= 0 && FD_ISSET (target_activity_fd, &readfds)) 63219370Spst { 63346283Sdfr#ifndef _WIN32 63419370Spst if ((*target_activity_function) ()) 63519370Spst kill (inferior_pid, SIGINT); 63646283Sdfr#endif 63719370Spst } 63819370Spst} 63919370Spst 64019370Spststatic int old_fcntl_flags; 64119370Spst 64219370Spstvoid 64319370Spstset_sigio_trap () 64419370Spst{ 64519370Spst if (target_activity_function) 64619370Spst { 64719370Spst old_sigio = (void (*) ()) signal (SIGIO, handle_sigio); 64819370Spst fcntl (target_activity_fd, F_SETOWN, getpid()); 64919370Spst old_fcntl_flags = fcntl (target_activity_fd, F_GETFL, 0); 65019370Spst fcntl (target_activity_fd, F_SETFL, old_fcntl_flags | FASYNC); 65119370Spst } 65219370Spst} 65319370Spst 65419370Spstvoid 65519370Spstclear_sigio_trap () 65619370Spst{ 65719370Spst if (target_activity_function) 65819370Spst { 65919370Spst signal (SIGIO, old_sigio); 66019370Spst fcntl (target_activity_fd, F_SETFL, old_fcntl_flags); 66119370Spst } 66219370Spst} 66319370Spst#else /* No SIGIO. */ 66419370Spstvoid 66519370Spstset_sigio_trap () 66619370Spst{ 66719370Spst if (target_activity_function) 66819370Spst abort (); 66919370Spst} 67019370Spst 67119370Spstvoid 67219370Spstclear_sigio_trap () 67319370Spst{ 67419370Spst if (target_activity_function) 67519370Spst abort (); 67619370Spst} 67719370Spst#endif /* No SIGIO. */ 67819370Spst 67919370Spst 68019370Spst/* This is here because this is where we figure out whether we (probably) 68119370Spst have job control. Just using job_control only does part of it because 68219370Spst setpgid or setpgrp might not exist on a system without job control. 68319370Spst It might be considered misplaced (on the other hand, process groups and 68419370Spst job control are closely related to ttys). 68519370Spst 68619370Spst For a more clean implementation, in libiberty, put a setpgid which merely 68719370Spst calls setpgrp and a setpgrp which does nothing (any system with job control 68819370Spst will have one or the other). */ 68919370Spstint 69019370Spstgdb_setpgid () 69119370Spst{ 69219370Spst int retval = 0; 69319370Spst 69419370Spst if (job_control) 69519370Spst { 69619370Spst#if defined (NEED_POSIX_SETPGID) || (defined (HAVE_TERMIOS) && defined (HAVE_SETPGID)) 69719370Spst /* setpgid (0, 0) is supposed to work and mean the same thing as 69819370Spst this, but on Ultrix 4.2A it fails with EPERM (and 69919370Spst setpgid (getpid (), getpid ()) succeeds). */ 70019370Spst retval = setpgid (getpid (), getpid ()); 70119370Spst#else 70219370Spst#if defined (TIOCGPGRP) 70319370Spst#if defined(USG) && !defined(SETPGRP_ARGS) 70419370Spst retval = setpgrp (); 70519370Spst#else 70619370Spst retval = setpgrp (getpid (), getpid ()); 70719370Spst#endif /* USG */ 70819370Spst#endif /* TIOCGPGRP. */ 70919370Spst#endif /* NEED_POSIX_SETPGID */ 71019370Spst } 71119370Spst return retval; 71219370Spst} 71319370Spst 71419370Spstvoid 71519370Spst_initialize_inflow () 71619370Spst{ 71719370Spst add_info ("terminal", term_info, 71819370Spst "Print inferior's saved terminal status."); 71919370Spst 72019370Spst add_com ("kill", class_run, kill_command, 72119370Spst "Kill execution of program being debugged."); 72219370Spst 72319370Spst inferior_pid = 0; 72419370Spst 72519370Spst terminal_is_ours = 1; 72619370Spst 72719370Spst /* OK, figure out whether we have job control. If neither termios nor 72819370Spst sgtty (i.e. termio or go32), leave job_control 0. */ 72919370Spst 73019370Spst#if defined (HAVE_TERMIOS) 73119370Spst /* Do all systems with termios have the POSIX way of identifying job 73219370Spst control? I hope so. */ 73319370Spst#ifdef _POSIX_JOB_CONTROL 73419370Spst job_control = 1; 73519370Spst#else 73619370Spst#ifdef _SC_JOB_CONTROL 73719370Spst job_control = sysconf (_SC_JOB_CONTROL); 73819370Spst#else 73919370Spst job_control = 0; /* have to assume the worst */ 74019370Spst#endif /* _SC_JOB_CONTROL */ 74119370Spst#endif /* _POSIX_JOB_CONTROL */ 74219370Spst#endif /* HAVE_TERMIOS */ 74319370Spst 74419370Spst#ifdef HAVE_SGTTY 74519370Spst#ifdef TIOCGPGRP 74619370Spst job_control = 1; 74719370Spst#else 74819370Spst job_control = 0; 74919370Spst#endif /* TIOCGPGRP */ 75019370Spst#endif /* sgtty */ 75119370Spst} 752