119370Spst/* Low level interface to ptrace, for GDB when running under Unix. 298944Sobrien Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 3130803Smarcel 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004 4130803Smarcel Free Software Foundation, Inc. 519370Spst 698944Sobrien This file is part of GDB. 719370Spst 898944Sobrien This program is free software; you can redistribute it and/or modify 998944Sobrien it under the terms of the GNU General Public License as published by 1098944Sobrien the Free Software Foundation; either version 2 of the License, or 1198944Sobrien (at your option) any later version. 1219370Spst 1398944Sobrien This program is distributed in the hope that it will be useful, 1498944Sobrien but WITHOUT ANY WARRANTY; without even the implied warranty of 1598944Sobrien MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1698944Sobrien GNU General Public License for more details. 1719370Spst 1898944Sobrien You should have received a copy of the GNU General Public License 1998944Sobrien along with this program; if not, write to the Free Software 2098944Sobrien Foundation, Inc., 59 Temple Place - Suite 330, 2198944Sobrien Boston, MA 02111-1307, USA. */ 2219370Spst 2319370Spst#include "defs.h" 2419370Spst#include "frame.h" 2519370Spst#include "inferior.h" 2619370Spst#include "command.h" 2719370Spst#include "serial.h" 2819370Spst#include "terminal.h" 2919370Spst#include "target.h" 3046283Sdfr#include "gdbthread.h" 3119370Spst 3219370Spst#include "gdb_string.h" 3319370Spst#include <signal.h> 3419370Spst#include <fcntl.h> 3598944Sobrien#ifdef HAVE_SYS_SELECT_H 3698944Sobrien#include <sys/select.h> 3719370Spst#endif 3819370Spst 39130803Smarcel#include "inflow.h" 4019370Spst 4198944Sobrien#ifdef HAVE_SYS_IOCTL_H 4298944Sobrien#include <sys/ioctl.h> 4398944Sobrien#endif 4498944Sobrien 4546283Sdfr#if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN) 4698944Sobrienstatic void handle_sigio (int); 4746283Sdfr#endif 4846283Sdfr 4998944Sobrienextern void _initialize_inflow (void); 5046283Sdfr 5198944Sobrienstatic void pass_signal (int); 5219370Spst 5398944Sobrienstatic void kill_command (char *, int); 5498944Sobrien 5598944Sobrienstatic void terminal_ours_1 (int); 5619370Spst 5719370Spst/* Record terminal status separately for debugger and inferior. */ 5819370Spst 5998944Sobrienstatic struct serial *stdin_serial; 6019370Spst 6119370Spst/* TTY state for the inferior. We save it whenever the inferior stops, and 6219370Spst restore it when it resumes. */ 6319370Spststatic serial_ttystate inferior_ttystate; 6419370Spst 6519370Spst/* Our own tty state, which we restore every time we need to deal with the 6619370Spst terminal. We only set it once, when GDB first starts. The settings of 6719370Spst flags which readline saves and restores and unimportant. */ 6819370Spststatic serial_ttystate our_ttystate; 6919370Spst 7019370Spst/* fcntl flags for us and the inferior. Saved and restored just like 7119370Spst {our,inferior}_ttystate. */ 7219370Spststatic int tflags_inferior; 7319370Spststatic int tflags_ours; 7419370Spst 7519370Spst#ifdef PROCESS_GROUP_TYPE 7619370Spst/* Process group for us and the inferior. Saved and restored just like 7719370Spst {our,inferior}_ttystate. */ 7819370SpstPROCESS_GROUP_TYPE our_process_group; 7919370SpstPROCESS_GROUP_TYPE inferior_process_group; 8019370Spst#endif 8119370Spst 8219370Spst/* While the inferior is running, we want SIGINT and SIGQUIT to go to the 8319370Spst inferior only. If we have job control, that takes care of it. If not, 8419370Spst we save our handlers in these two variables and set SIGINT and SIGQUIT 8519370Spst to SIG_IGN. */ 8646283Sdfr 8719370Spststatic void (*sigint_ours) (); 8819370Spststatic void (*sigquit_ours) (); 8919370Spst 9019370Spst/* The name of the tty (from the `tty' command) that we gave to the inferior 9119370Spst when it was last started. */ 9219370Spst 9319370Spststatic char *inferior_thisrun_terminal; 9419370Spst 9519370Spst/* Nonzero if our terminal settings are in effect. Zero if the 9619370Spst inferior's settings are in effect. Ignored if !gdb_has_a_terminal 9719370Spst (). */ 9819370Spst 9946283Sdfrint terminal_is_ours; 10019370Spst 10198944Sobrienenum 10298944Sobrien { 10398944Sobrien yes, no, have_not_checked 10498944Sobrien } 10598944Sobriengdb_has_a_terminal_flag = have_not_checked; 10619370Spst 10719370Spst/* Does GDB have a terminal (on stdin)? */ 10819370Spstint 10998944Sobriengdb_has_a_terminal (void) 11019370Spst{ 11119370Spst switch (gdb_has_a_terminal_flag) 11219370Spst { 11319370Spst case yes: 11419370Spst return 1; 11519370Spst case no: 11619370Spst return 0; 11719370Spst case have_not_checked: 11898944Sobrien /* Get all the current tty settings (including whether we have a 11998944Sobrien tty at all!). Can't do this in _initialize_inflow because 12098944Sobrien serial_fdopen() won't work until the serial_ops_list is 12198944Sobrien initialized. */ 12219370Spst 12319370Spst#ifdef F_GETFL 12419370Spst tflags_ours = fcntl (0, F_GETFL, 0); 12519370Spst#endif 12619370Spst 12719370Spst gdb_has_a_terminal_flag = no; 12898944Sobrien stdin_serial = serial_fdopen (0); 12919370Spst if (stdin_serial != NULL) 13019370Spst { 13198944Sobrien our_ttystate = serial_get_tty_state (stdin_serial); 13219370Spst 13319370Spst if (our_ttystate != NULL) 13419370Spst { 13519370Spst gdb_has_a_terminal_flag = yes; 13619370Spst#ifdef HAVE_TERMIOS 13719370Spst our_process_group = tcgetpgrp (0); 13819370Spst#endif 13946283Sdfr#ifdef HAVE_TERMIO 14046283Sdfr our_process_group = getpgrp (); 14146283Sdfr#endif 14219370Spst#ifdef HAVE_SGTTY 14319370Spst ioctl (0, TIOCGPGRP, &our_process_group); 14419370Spst#endif 14519370Spst } 14619370Spst } 14719370Spst 14819370Spst return gdb_has_a_terminal_flag == yes; 14919370Spst default: 15019370Spst /* "Can't happen". */ 15119370Spst return 0; 15219370Spst } 15319370Spst} 15419370Spst 15519370Spst/* Macro for printing errors from ioctl operations */ 15619370Spst 15719370Spst#define OOPSY(what) \ 15819370Spst if (result == -1) \ 15919370Spst fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \ 160130803Smarcel what, safe_strerror (errno)) 16119370Spst 16298944Sobrienstatic void terminal_ours_1 (int); 16319370Spst 16419370Spst/* Initialize the terminal settings we record for the inferior, 16519370Spst before we actually run the inferior. */ 16619370Spst 16719370Spstvoid 16898944Sobrienterminal_init_inferior_with_pgrp (int pgrp) 16919370Spst{ 17019370Spst if (gdb_has_a_terminal ()) 17119370Spst { 17298944Sobrien /* We could just as well copy our_ttystate (if we felt like 17398944Sobrien adding a new function serial_copy_tty_state()). */ 17419370Spst if (inferior_ttystate) 17598944Sobrien xfree (inferior_ttystate); 17698944Sobrien inferior_ttystate = serial_get_tty_state (stdin_serial); 17746283Sdfr 17819370Spst#ifdef PROCESS_GROUP_TYPE 17946283Sdfr inferior_process_group = pgrp; 18019370Spst#endif 18119370Spst 18219370Spst /* Make sure that next time we call terminal_inferior (which will be 18398944Sobrien before the program runs, as it needs to be), we install the new 18498944Sobrien process group. */ 18519370Spst terminal_is_ours = 1; 18619370Spst } 18719370Spst} 18819370Spst 189130803Smarcel/* Save the terminal settings again. This is necessary for the TUI 190130803Smarcel when it switches to TUI or non-TUI mode; curses changes the terminal 191130803Smarcel and gdb must be able to restore it correctly. */ 192130803Smarcel 19346283Sdfrvoid 194130803Smarcelterminal_save_ours (void) 195130803Smarcel{ 196130803Smarcel if (gdb_has_a_terminal ()) 197130803Smarcel { 198130803Smarcel /* We could just as well copy our_ttystate (if we felt like adding 199130803Smarcel a new function serial_copy_tty_state). */ 200130803Smarcel if (our_ttystate) 201130803Smarcel xfree (our_ttystate); 202130803Smarcel our_ttystate = serial_get_tty_state (stdin_serial); 203130803Smarcel } 204130803Smarcel} 205130803Smarcel 206130803Smarcelvoid 20798944Sobrienterminal_init_inferior (void) 20846283Sdfr{ 20946283Sdfr#ifdef PROCESS_GROUP_TYPE 21046283Sdfr /* This is for Lynx, and should be cleaned up by having Lynx be a separate 21146283Sdfr debugging target with a version of target_terminal_init_inferior which 21246283Sdfr passes in the process group to a generic routine which does all the work 21346283Sdfr (and the non-threaded child_terminal_init_inferior can just pass in 21498944Sobrien inferior_ptid to the same routine). */ 21546283Sdfr /* We assume INFERIOR_PID is also the child's process group. */ 21698944Sobrien terminal_init_inferior_with_pgrp (PIDGET (inferior_ptid)); 21746283Sdfr#endif /* PROCESS_GROUP_TYPE */ 21846283Sdfr} 21946283Sdfr 22019370Spst/* Put the inferior's terminal settings into effect. 22119370Spst This is preparation for starting or resuming the inferior. */ 22219370Spst 22319370Spstvoid 22498944Sobrienterminal_inferior (void) 22519370Spst{ 22619370Spst if (gdb_has_a_terminal () && terminal_is_ours 227130803Smarcel && inferior_ttystate != NULL 22819370Spst && inferior_thisrun_terminal == 0) 22919370Spst { 23019370Spst int result; 23119370Spst 23219370Spst#ifdef F_GETFL 23319370Spst /* Is there a reason this is being done twice? It happens both 23498944Sobrien places we use F_SETFL, so I'm inclined to think perhaps there 23598944Sobrien is some reason, however perverse. Perhaps not though... */ 23619370Spst result = fcntl (0, F_SETFL, tflags_inferior); 23719370Spst result = fcntl (0, F_SETFL, tflags_inferior); 23819370Spst OOPSY ("fcntl F_SETFL"); 23919370Spst#endif 24019370Spst 24119370Spst /* Because we were careful to not change in or out of raw mode in 24298944Sobrien terminal_ours, we will not change in our out of raw mode with 24398944Sobrien this call, so we don't flush any input. */ 24498944Sobrien result = serial_set_tty_state (stdin_serial, inferior_ttystate); 24519370Spst OOPSY ("setting tty state"); 24619370Spst 24719370Spst if (!job_control) 24819370Spst { 24998944Sobrien sigint_ours = (void (*)()) signal (SIGINT, SIG_IGN); 25046283Sdfr#ifdef SIGQUIT 25198944Sobrien sigquit_ours = (void (*)()) signal (SIGQUIT, SIG_IGN); 25246283Sdfr#endif 25319370Spst } 25419370Spst 25519370Spst /* If attach_flag is set, we don't know whether we are sharing a 25698944Sobrien terminal with the inferior or not. (attaching a process 25798944Sobrien without a terminal is one case where we do not; attaching a 25898944Sobrien process which we ran from the same shell as GDB via `&' is 25998944Sobrien one case where we do, I think (but perhaps this is not 26098944Sobrien `sharing' in the sense that we need to save and restore tty 26198944Sobrien state)). I don't know if there is any way to tell whether we 26298944Sobrien are sharing a terminal. So what we do is to go through all 26398944Sobrien the saving and restoring of the tty state, but ignore errors 26498944Sobrien setting the process group, which will happen if we are not 26598944Sobrien sharing a terminal). */ 26619370Spst 26719370Spst if (job_control) 26819370Spst { 26919370Spst#ifdef HAVE_TERMIOS 27019370Spst result = tcsetpgrp (0, inferior_process_group); 27119370Spst if (!attach_flag) 27219370Spst OOPSY ("tcsetpgrp"); 27319370Spst#endif 27419370Spst 27519370Spst#ifdef HAVE_SGTTY 27619370Spst result = ioctl (0, TIOCSPGRP, &inferior_process_group); 27719370Spst if (!attach_flag) 27819370Spst OOPSY ("TIOCSPGRP"); 27919370Spst#endif 28019370Spst } 28119370Spst 28219370Spst } 28319370Spst terminal_is_ours = 0; 28419370Spst} 28519370Spst 28619370Spst/* Put some of our terminal settings into effect, 28719370Spst enough to get proper results from our output, 28819370Spst but do not change into or out of RAW mode 28919370Spst so that no input is discarded. 29019370Spst 29119370Spst After doing this, either terminal_ours or terminal_inferior 29219370Spst should be called to get back to a normal state of affairs. */ 29319370Spst 29419370Spstvoid 29598944Sobrienterminal_ours_for_output (void) 29619370Spst{ 29719370Spst terminal_ours_1 (1); 29819370Spst} 29919370Spst 30019370Spst/* Put our terminal settings into effect. 30119370Spst First record the inferior's terminal settings 30219370Spst so they can be restored properly later. */ 30319370Spst 30419370Spstvoid 30598944Sobrienterminal_ours (void) 30619370Spst{ 30719370Spst terminal_ours_1 (0); 30819370Spst} 30919370Spst 31019370Spst/* output_only is not used, and should not be used unless we introduce 31119370Spst separate terminal_is_ours and terminal_is_ours_for_output 31219370Spst flags. */ 31319370Spst 31419370Spststatic void 31598944Sobrienterminal_ours_1 (int output_only) 31619370Spst{ 31719370Spst /* Checking inferior_thisrun_terminal is necessary so that 31819370Spst if GDB is running in the background, it won't block trying 31919370Spst to do the ioctl()'s below. Checking gdb_has_a_terminal 32019370Spst avoids attempting all the ioctl's when running in batch. */ 32119370Spst if (inferior_thisrun_terminal != 0 || gdb_has_a_terminal () == 0) 32219370Spst return; 32319370Spst 32419370Spst if (!terminal_is_ours) 32519370Spst { 32698944Sobrien#ifdef SIGTTOU 32719370Spst /* Ignore this signal since it will happen when we try to set the 32898944Sobrien pgrp. */ 32998944Sobrien void (*osigttou) () = NULL; 33098944Sobrien#endif 33119370Spst int result; 33219370Spst 33319370Spst terminal_is_ours = 1; 33419370Spst 33519370Spst#ifdef SIGTTOU 33619370Spst if (job_control) 33798944Sobrien osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN); 33819370Spst#endif 33919370Spst 34019370Spst if (inferior_ttystate) 34198944Sobrien xfree (inferior_ttystate); 34298944Sobrien inferior_ttystate = serial_get_tty_state (stdin_serial); 34319370Spst#ifdef HAVE_TERMIOS 34419370Spst inferior_process_group = tcgetpgrp (0); 34519370Spst#endif 34646283Sdfr#ifdef HAVE_TERMIO 34746283Sdfr inferior_process_group = getpgrp (); 34846283Sdfr#endif 34919370Spst#ifdef HAVE_SGTTY 35019370Spst ioctl (0, TIOCGPGRP, &inferior_process_group); 35119370Spst#endif 35219370Spst 35319370Spst /* Here we used to set ICANON in our ttystate, but I believe this 35498944Sobrien was an artifact from before when we used readline. Readline sets 35598944Sobrien the tty state when it needs to. 35698944Sobrien FIXME-maybe: However, query() expects non-raw mode and doesn't 35798944Sobrien use readline. Maybe query should use readline (on the other hand, 35898944Sobrien this only matters for HAVE_SGTTY, not termio or termios, I think). */ 35919370Spst 36019370Spst /* Set tty state to our_ttystate. We don't change in our out of raw 36198944Sobrien mode, to avoid flushing input. We need to do the same thing 36298944Sobrien regardless of output_only, because we don't have separate 36398944Sobrien terminal_is_ours and terminal_is_ours_for_output flags. It's OK, 36498944Sobrien though, since readline will deal with raw mode when/if it needs to. 36598944Sobrien */ 36619370Spst 36798944Sobrien serial_noflush_set_tty_state (stdin_serial, our_ttystate, 36819370Spst inferior_ttystate); 36919370Spst 37019370Spst if (job_control) 37119370Spst { 37219370Spst#ifdef HAVE_TERMIOS 37319370Spst result = tcsetpgrp (0, our_process_group); 37419370Spst#if 0 37519370Spst /* This fails on Ultrix with EINVAL if you run the testsuite 37619370Spst in the background with nohup, and then log out. GDB never 37719370Spst used to check for an error here, so perhaps there are other 37819370Spst such situations as well. */ 37919370Spst if (result == -1) 38019370Spst fprintf_unfiltered (gdb_stderr, "[tcsetpgrp failed in terminal_ours: %s]\n", 381130803Smarcel safe_strerror (errno)); 38219370Spst#endif 38319370Spst#endif /* termios */ 38419370Spst 38519370Spst#ifdef HAVE_SGTTY 38619370Spst result = ioctl (0, TIOCSPGRP, &our_process_group); 38719370Spst#endif 38819370Spst } 38919370Spst 39019370Spst#ifdef SIGTTOU 39119370Spst if (job_control) 39219370Spst signal (SIGTTOU, osigttou); 39319370Spst#endif 39419370Spst 39519370Spst if (!job_control) 39619370Spst { 39719370Spst signal (SIGINT, sigint_ours); 39846283Sdfr#ifdef SIGQUIT 39919370Spst signal (SIGQUIT, sigquit_ours); 40046283Sdfr#endif 40119370Spst } 40219370Spst 40319370Spst#ifdef F_GETFL 40419370Spst tflags_inferior = fcntl (0, F_GETFL, 0); 40519370Spst 40619370Spst /* Is there a reason this is being done twice? It happens both 40798944Sobrien places we use F_SETFL, so I'm inclined to think perhaps there 40898944Sobrien is some reason, however perverse. Perhaps not though... */ 40919370Spst result = fcntl (0, F_SETFL, tflags_ours); 41019370Spst result = fcntl (0, F_SETFL, tflags_ours); 41119370Spst#endif 41219370Spst 41398944Sobrien result = result; /* lint */ 41419370Spst } 41519370Spst} 41619370Spst 41719370Spstvoid 41898944Sobrienterm_info (char *arg, int from_tty) 41919370Spst{ 42019370Spst target_terminal_info (arg, from_tty); 42119370Spst} 42219370Spst 42319370Spstvoid 42498944Sobrienchild_terminal_info (char *args, int from_tty) 42519370Spst{ 42619370Spst if (!gdb_has_a_terminal ()) 42719370Spst { 42819370Spst printf_filtered ("This GDB does not control a terminal.\n"); 42919370Spst return; 43019370Spst } 43119370Spst 43219370Spst printf_filtered ("Inferior's terminal status (currently saved by GDB):\n"); 43319370Spst 43419370Spst /* First the fcntl flags. */ 43519370Spst { 43619370Spst int flags; 43798944Sobrien 43819370Spst flags = tflags_inferior; 43919370Spst 44019370Spst printf_filtered ("File descriptor flags = "); 44119370Spst 44219370Spst#ifndef O_ACCMODE 44319370Spst#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR) 44419370Spst#endif 44519370Spst /* (O_ACCMODE) parens are to avoid Ultrix header file bug */ 44619370Spst switch (flags & (O_ACCMODE)) 44719370Spst { 44898944Sobrien case O_RDONLY: 44998944Sobrien printf_filtered ("O_RDONLY"); 45098944Sobrien break; 45198944Sobrien case O_WRONLY: 45298944Sobrien printf_filtered ("O_WRONLY"); 45398944Sobrien break; 45498944Sobrien case O_RDWR: 45598944Sobrien printf_filtered ("O_RDWR"); 45698944Sobrien break; 45719370Spst } 45819370Spst flags &= ~(O_ACCMODE); 45919370Spst 46019370Spst#ifdef O_NONBLOCK 46198944Sobrien if (flags & O_NONBLOCK) 46219370Spst printf_filtered (" | O_NONBLOCK"); 46319370Spst flags &= ~O_NONBLOCK; 46419370Spst#endif 46598944Sobrien 46619370Spst#if defined (O_NDELAY) 46719370Spst /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will 46819370Spst print it as O_NONBLOCK, which is good cause that is what POSIX 46919370Spst has, and the flag will already be cleared by the time we get here. */ 47019370Spst if (flags & O_NDELAY) 47119370Spst printf_filtered (" | O_NDELAY"); 47219370Spst flags &= ~O_NDELAY; 47319370Spst#endif 47419370Spst 47519370Spst if (flags & O_APPEND) 47619370Spst printf_filtered (" | O_APPEND"); 47719370Spst flags &= ~O_APPEND; 47819370Spst 47919370Spst#if defined (O_BINARY) 48019370Spst if (flags & O_BINARY) 48119370Spst printf_filtered (" | O_BINARY"); 48219370Spst flags &= ~O_BINARY; 48319370Spst#endif 48419370Spst 48519370Spst if (flags) 48619370Spst printf_filtered (" | 0x%x", flags); 48719370Spst printf_filtered ("\n"); 48819370Spst } 48919370Spst 49019370Spst#ifdef PROCESS_GROUP_TYPE 49198944Sobrien printf_filtered ("Process group = %d\n", 49298944Sobrien (int) inferior_process_group); 49319370Spst#endif 49419370Spst 49598944Sobrien serial_print_tty_state (stdin_serial, inferior_ttystate, gdb_stdout); 49619370Spst} 49719370Spst 49819370Spst/* NEW_TTY_PREFORK is called before forking a new child process, 49919370Spst so we can record the state of ttys in the child to be formed. 50019370Spst TTYNAME is null if we are to share the terminal with gdb; 50119370Spst or points to a string containing the name of the desired tty. 50219370Spst 50319370Spst NEW_TTY is called in new child processes under Unix, which will 50419370Spst become debugger target processes. This actually switches to 50519370Spst the terminal specified in the NEW_TTY_PREFORK call. */ 50619370Spst 50719370Spstvoid 50898944Sobriennew_tty_prefork (char *ttyname) 50919370Spst{ 51019370Spst /* Save the name for later, for determining whether we and the child 51119370Spst are sharing a tty. */ 51219370Spst inferior_thisrun_terminal = ttyname; 51319370Spst} 51419370Spst 51519370Spstvoid 51698944Sobriennew_tty (void) 51719370Spst{ 518130803Smarcel int tty; 51919370Spst 52019370Spst if (inferior_thisrun_terminal == 0) 52119370Spst return; 52246283Sdfr#if !defined(__GO32__) && !defined(_WIN32) 52319370Spst#ifdef TIOCNOTTY 52419370Spst /* Disconnect the child process from our controlling terminal. On some 52519370Spst systems (SVR4 for example), this may cause a SIGTTOU, so temporarily 52619370Spst ignore SIGTTOU. */ 52798944Sobrien tty = open ("/dev/tty", O_RDWR); 52819370Spst if (tty > 0) 52919370Spst { 53019370Spst void (*osigttou) (); 53119370Spst 53298944Sobrien osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN); 53398944Sobrien ioctl (tty, TIOCNOTTY, 0); 53498944Sobrien close (tty); 53598944Sobrien signal (SIGTTOU, osigttou); 53619370Spst } 53719370Spst#endif 53819370Spst 53919370Spst /* Now open the specified new terminal. */ 54019370Spst 54119370Spst#ifdef USE_O_NOCTTY 54298944Sobrien tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY); 54319370Spst#else 54498944Sobrien tty = open (inferior_thisrun_terminal, O_RDWR); 54519370Spst#endif 54619370Spst if (tty == -1) 54719370Spst { 54819370Spst print_sys_errmsg (inferior_thisrun_terminal, errno); 54998944Sobrien _exit (1); 55019370Spst } 55119370Spst 55219370Spst /* Avoid use of dup2; doesn't exist on all systems. */ 55319370Spst if (tty != 0) 55498944Sobrien { 55598944Sobrien close (0); 55698944Sobrien dup (tty); 55798944Sobrien } 55819370Spst if (tty != 1) 55998944Sobrien { 56098944Sobrien close (1); 56198944Sobrien dup (tty); 56298944Sobrien } 56319370Spst if (tty != 2) 56498944Sobrien { 56598944Sobrien close (2); 56698944Sobrien dup (tty); 56798944Sobrien } 56819370Spst if (tty > 2) 56998944Sobrien close (tty); 57098944Sobrien#endif /* !go32 && !win32 */ 57119370Spst} 57219370Spst 57319370Spst/* Kill the inferior process. Make us have no inferior. */ 57419370Spst 57519370Spststatic void 57698944Sobrienkill_command (char *arg, int from_tty) 57719370Spst{ 57898944Sobrien /* FIXME: This should not really be inferior_ptid (or target_has_execution). 57919370Spst It should be a distinct flag that indicates that a target is active, cuz 58019370Spst some targets don't have processes! */ 58119370Spst 58298944Sobrien if (ptid_equal (inferior_ptid, null_ptid)) 58319370Spst error ("The program is not being run."); 58419370Spst if (!query ("Kill the program being debugged? ")) 58519370Spst error ("Not confirmed."); 58619370Spst target_kill (); 58719370Spst 58898944Sobrien init_thread_list (); /* Destroy thread info */ 58919370Spst 59019370Spst /* Killing off the inferior can leave us with a core file. If so, 59119370Spst print the state we are left in. */ 59298944Sobrien if (target_has_stack) 59398944Sobrien { 59498944Sobrien printf_filtered ("In %s,\n", target_longname); 595130803Smarcel if (deprecated_selected_frame == NULL) 59698944Sobrien fputs_filtered ("No selected stack frame.\n", gdb_stdout); 59798944Sobrien else 598130803Smarcel print_stack_frame (deprecated_selected_frame, 599130803Smarcel frame_relative_level (deprecated_selected_frame), 1); 60098944Sobrien } 60119370Spst} 60219370Spst 60319370Spst/* Call set_sigint_trap when you need to pass a signal on to an attached 60419370Spst process when handling SIGINT */ 60519370Spst 60619370Spststatic void 60798944Sobrienpass_signal (int signo) 60819370Spst{ 60946283Sdfr#ifndef _WIN32 61098944Sobrien kill (PIDGET (inferior_ptid), SIGINT); 61146283Sdfr#endif 61219370Spst} 61319370Spst 61498944Sobrienstatic void (*osig) (); 61519370Spst 61619370Spstvoid 61798944Sobrienset_sigint_trap (void) 61819370Spst{ 61919370Spst if (attach_flag || inferior_thisrun_terminal) 62019370Spst { 62198944Sobrien osig = (void (*)()) signal (SIGINT, pass_signal); 62219370Spst } 62319370Spst} 62419370Spst 62519370Spstvoid 62698944Sobrienclear_sigint_trap (void) 62719370Spst{ 62819370Spst if (attach_flag || inferior_thisrun_terminal) 62919370Spst { 63019370Spst signal (SIGINT, osig); 63119370Spst } 63219370Spst} 63319370Spst 63419370Spst#if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN) 63519370Spststatic void (*old_sigio) (); 63619370Spst 63719370Spststatic void 63898944Sobrienhandle_sigio (int signo) 63919370Spst{ 64019370Spst int numfds; 64119370Spst fd_set readfds; 64219370Spst 64319370Spst signal (SIGIO, handle_sigio); 64419370Spst 64519370Spst FD_ZERO (&readfds); 64619370Spst FD_SET (target_activity_fd, &readfds); 64719370Spst numfds = select (target_activity_fd + 1, &readfds, NULL, NULL, NULL); 64819370Spst if (numfds >= 0 && FD_ISSET (target_activity_fd, &readfds)) 64919370Spst { 65046283Sdfr#ifndef _WIN32 65119370Spst if ((*target_activity_function) ()) 65298944Sobrien kill (PIDGET (inferior_ptid), SIGINT); 65346283Sdfr#endif 65419370Spst } 65519370Spst} 65619370Spst 65719370Spststatic int old_fcntl_flags; 65819370Spst 65919370Spstvoid 66098944Sobrienset_sigio_trap (void) 66119370Spst{ 66219370Spst if (target_activity_function) 66319370Spst { 66498944Sobrien old_sigio = (void (*)()) signal (SIGIO, handle_sigio); 66598944Sobrien fcntl (target_activity_fd, F_SETOWN, getpid ()); 66619370Spst old_fcntl_flags = fcntl (target_activity_fd, F_GETFL, 0); 66719370Spst fcntl (target_activity_fd, F_SETFL, old_fcntl_flags | FASYNC); 66819370Spst } 66919370Spst} 67019370Spst 67119370Spstvoid 67298944Sobrienclear_sigio_trap (void) 67319370Spst{ 67419370Spst if (target_activity_function) 67519370Spst { 67619370Spst signal (SIGIO, old_sigio); 67719370Spst fcntl (target_activity_fd, F_SETFL, old_fcntl_flags); 67819370Spst } 67919370Spst} 68019370Spst#else /* No SIGIO. */ 68119370Spstvoid 68298944Sobrienset_sigio_trap (void) 68319370Spst{ 68419370Spst if (target_activity_function) 68598944Sobrien internal_error (__FILE__, __LINE__, "failed internal consistency check"); 68619370Spst} 68719370Spst 68819370Spstvoid 68998944Sobrienclear_sigio_trap (void) 69019370Spst{ 69119370Spst if (target_activity_function) 69298944Sobrien internal_error (__FILE__, __LINE__, "failed internal consistency check"); 69319370Spst} 69419370Spst#endif /* No SIGIO. */ 69519370Spst 69619370Spst 69719370Spst/* This is here because this is where we figure out whether we (probably) 69819370Spst have job control. Just using job_control only does part of it because 69919370Spst setpgid or setpgrp might not exist on a system without job control. 70019370Spst It might be considered misplaced (on the other hand, process groups and 70119370Spst job control are closely related to ttys). 70219370Spst 70319370Spst For a more clean implementation, in libiberty, put a setpgid which merely 70419370Spst calls setpgrp and a setpgrp which does nothing (any system with job control 70519370Spst will have one or the other). */ 70619370Spstint 70798944Sobriengdb_setpgid (void) 70819370Spst{ 70919370Spst int retval = 0; 71019370Spst 71119370Spst if (job_control) 71219370Spst { 71398944Sobrien#if defined (HAVE_TERMIOS) || defined (TIOCGPGRP) 71498944Sobrien#ifdef HAVE_SETPGID 71598944Sobrien /* The call setpgid (0, 0) is supposed to work and mean the same 71698944Sobrien thing as this, but on Ultrix 4.2A it fails with EPERM (and 71798944Sobrien setpgid (getpid (), getpid ()) succeeds). */ 71819370Spst retval = setpgid (getpid (), getpid ()); 71919370Spst#else 72098944Sobrien#ifdef HAVE_SETPGRP 72198944Sobrien#ifdef SETPGRP_VOID 72219370Spst retval = setpgrp (); 72319370Spst#else 72419370Spst retval = setpgrp (getpid (), getpid ()); 72598944Sobrien#endif 72698944Sobrien#endif /* HAVE_SETPGRP */ 72798944Sobrien#endif /* HAVE_SETPGID */ 72898944Sobrien#endif /* defined (HAVE_TERMIOS) || defined (TIOCGPGRP) */ 72919370Spst } 73098944Sobrien 73119370Spst return retval; 73219370Spst} 73319370Spst 73419370Spstvoid 73598944Sobrien_initialize_inflow (void) 73619370Spst{ 73719370Spst add_info ("terminal", term_info, 73898944Sobrien "Print inferior's saved terminal status."); 73919370Spst 74019370Spst add_com ("kill", class_run, kill_command, 74119370Spst "Kill execution of program being debugged."); 74219370Spst 74398944Sobrien inferior_ptid = null_ptid; 74419370Spst 74519370Spst terminal_is_ours = 1; 74619370Spst 74719370Spst /* OK, figure out whether we have job control. If neither termios nor 74819370Spst sgtty (i.e. termio or go32), leave job_control 0. */ 74919370Spst 75019370Spst#if defined (HAVE_TERMIOS) 75119370Spst /* Do all systems with termios have the POSIX way of identifying job 75219370Spst control? I hope so. */ 75319370Spst#ifdef _POSIX_JOB_CONTROL 75419370Spst job_control = 1; 75519370Spst#else 75619370Spst#ifdef _SC_JOB_CONTROL 75719370Spst job_control = sysconf (_SC_JOB_CONTROL); 75819370Spst#else 75998944Sobrien job_control = 0; /* have to assume the worst */ 76098944Sobrien#endif /* _SC_JOB_CONTROL */ 76198944Sobrien#endif /* _POSIX_JOB_CONTROL */ 76298944Sobrien#endif /* HAVE_TERMIOS */ 76319370Spst 76419370Spst#ifdef HAVE_SGTTY 76519370Spst#ifdef TIOCGPGRP 76619370Spst job_control = 1; 76719370Spst#else 76819370Spst job_control = 0; 76919370Spst#endif /* TIOCGPGRP */ 77019370Spst#endif /* sgtty */ 77119370Spst} 772