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