inflow.c revision 19370
1/* Low level interface to ptrace, for GDB when running under Unix.
2   Copyright 1986, 1987, 1989, 1991, 1992, 1995 Free Software Foundation, Inc.
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20#include "defs.h"
21#include "frame.h"
22#include "inferior.h"
23#include "command.h"
24#include "signals.h"
25#include "serial.h"
26#include "terminal.h"
27#include "target.h"
28#include "thread.h"
29
30#include "gdb_string.h"
31#include <signal.h>
32#include <fcntl.h>
33#ifdef HAVE_UNISTD_H
34#include <unistd.h>
35#endif
36
37#ifdef HAVE_TERMIOS
38#define PROCESS_GROUP_TYPE pid_t
39#endif
40
41#ifdef HAVE_SGTTY
42#ifdef SHORT_PGRP
43/* This is only used for the ultra.  Does it have pid_t?  */
44#define PROCESS_GROUP_TYPE short
45#else
46#define PROCESS_GROUP_TYPE int
47#endif
48#endif /* sgtty */
49
50static void
51kill_command PARAMS ((char *, int));
52
53static void
54terminal_ours_1 PARAMS ((int));
55
56/* Record terminal status separately for debugger and inferior.  */
57
58static serial_t stdin_serial;
59
60/* TTY state for the inferior.  We save it whenever the inferior stops, and
61   restore it when it resumes.  */
62static serial_ttystate inferior_ttystate;
63
64/* Our own tty state, which we restore every time we need to deal with the
65   terminal.  We only set it once, when GDB first starts.  The settings of
66   flags which readline saves and restores and unimportant.  */
67static serial_ttystate our_ttystate;
68
69/* fcntl flags for us and the inferior.  Saved and restored just like
70   {our,inferior}_ttystate.  */
71static int tflags_inferior;
72static int tflags_ours;
73
74#ifdef PROCESS_GROUP_TYPE
75/* Process group for us and the inferior.  Saved and restored just like
76   {our,inferior}_ttystate.  */
77PROCESS_GROUP_TYPE our_process_group;
78PROCESS_GROUP_TYPE inferior_process_group;
79#endif
80
81/* While the inferior is running, we want SIGINT and SIGQUIT to go to the
82   inferior only.  If we have job control, that takes care of it.  If not,
83   we save our handlers in these two variables and set SIGINT and SIGQUIT
84   to SIG_IGN.  */
85static void (*sigint_ours) ();
86static void (*sigquit_ours) ();
87
88/* The name of the tty (from the `tty' command) that we gave to the inferior
89   when it was last started.  */
90
91static char *inferior_thisrun_terminal;
92
93/* Nonzero if our terminal settings are in effect.  Zero if the
94   inferior's settings are in effect.  Ignored if !gdb_has_a_terminal
95   ().  */
96
97static int terminal_is_ours;
98
99enum {yes, no, have_not_checked} gdb_has_a_terminal_flag = have_not_checked;
100
101/* Does GDB have a terminal (on stdin)?  */
102int
103gdb_has_a_terminal ()
104{
105  switch (gdb_has_a_terminal_flag)
106    {
107    case yes:
108      return 1;
109    case no:
110      return 0;
111    case have_not_checked:
112      /* Get all the current tty settings (including whether we have a tty at
113	 all!).  Can't do this in _initialize_inflow because SERIAL_FDOPEN
114	 won't work until the serial_ops_list is initialized.  */
115
116#ifdef F_GETFL
117      tflags_ours = fcntl (0, F_GETFL, 0);
118#endif
119
120      gdb_has_a_terminal_flag = no;
121      stdin_serial = SERIAL_FDOPEN (0);
122      if (stdin_serial != NULL)
123	{
124	  our_ttystate = SERIAL_GET_TTY_STATE (stdin_serial);
125
126	  if (our_ttystate != NULL)
127	    {
128	      gdb_has_a_terminal_flag = yes;
129#ifdef HAVE_TERMIOS
130	      our_process_group = tcgetpgrp (0);
131#endif
132#ifdef HAVE_SGTTY
133	      ioctl (0, TIOCGPGRP, &our_process_group);
134#endif
135	    }
136	}
137
138      return gdb_has_a_terminal_flag == yes;
139    default:
140      /* "Can't happen".  */
141      return 0;
142    }
143}
144
145/* Macro for printing errors from ioctl operations */
146
147#define	OOPSY(what)	\
148  if (result == -1)	\
149    fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
150	    what, strerror (errno))
151
152static void terminal_ours_1 PARAMS ((int));
153
154/* Initialize the terminal settings we record for the inferior,
155   before we actually run the inferior.  */
156
157void
158terminal_init_inferior ()
159{
160  if (gdb_has_a_terminal ())
161    {
162      /* We could just as well copy our_ttystate (if we felt like adding
163	 a new function SERIAL_COPY_TTY_STATE).  */
164      if (inferior_ttystate)
165	free (inferior_ttystate);
166      inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial);
167#ifdef PROCESS_GROUP_TYPE
168#ifdef PIDGET
169      /* This is for Lynx, and should be cleaned up by having Lynx be
170	 a separate debugging target with a version of
171	 target_terminal_init_inferior which passes in the process
172	 group to a generic routine which does all the work (and the
173	 non-threaded child_terminal_init_inferior can just pass in
174	 inferior_pid to the same routine).  */
175      inferior_process_group = PIDGET (inferior_pid);
176#else
177      inferior_process_group = inferior_pid;
178#endif
179#endif
180
181      /* Make sure that next time we call terminal_inferior (which will be
182	 before the program runs, as it needs to be), we install the new
183	 process group.  */
184      terminal_is_ours = 1;
185    }
186}
187
188/* Put the inferior's terminal settings into effect.
189   This is preparation for starting or resuming the inferior.  */
190
191void
192terminal_inferior ()
193{
194  if (gdb_has_a_terminal () && terminal_is_ours
195      && inferior_thisrun_terminal == 0)
196    {
197      int result;
198
199#ifdef F_GETFL
200      /* Is there a reason this is being done twice?  It happens both
201	 places we use F_SETFL, so I'm inclined to think perhaps there
202	 is some reason, however perverse.  Perhaps not though...  */
203      result = fcntl (0, F_SETFL, tflags_inferior);
204      result = fcntl (0, F_SETFL, tflags_inferior);
205      OOPSY ("fcntl F_SETFL");
206#endif
207
208      /* Because we were careful to not change in or out of raw mode in
209	 terminal_ours, we will not change in our out of raw mode with
210	 this call, so we don't flush any input.  */
211      result = SERIAL_SET_TTY_STATE (stdin_serial, inferior_ttystate);
212      OOPSY ("setting tty state");
213
214      if (!job_control)
215	{
216	  sigint_ours = (void (*) ()) signal (SIGINT, SIG_IGN);
217	  sigquit_ours = (void (*) ()) signal (SIGQUIT, SIG_IGN);
218	}
219
220      /* If attach_flag is set, we don't know whether we are sharing a
221	 terminal with the inferior or not.  (attaching a process
222	 without a terminal is one case where we do not; attaching a
223	 process which we ran from the same shell as GDB via `&' is
224	 one case where we do, I think (but perhaps this is not
225	 `sharing' in the sense that we need to save and restore tty
226	 state)).  I don't know if there is any way to tell whether we
227	 are sharing a terminal.  So what we do is to go through all
228	 the saving and restoring of the tty state, but ignore errors
229	 setting the process group, which will happen if we are not
230	 sharing a terminal).  */
231
232      if (job_control)
233	{
234#ifdef HAVE_TERMIOS
235	  result = tcsetpgrp (0, inferior_process_group);
236	  if (!attach_flag)
237	    OOPSY ("tcsetpgrp");
238#endif
239
240#ifdef HAVE_SGTTY
241	  result = ioctl (0, TIOCSPGRP, &inferior_process_group);
242	  if (!attach_flag)
243	    OOPSY ("TIOCSPGRP");
244#endif
245	}
246
247    }
248  terminal_is_ours = 0;
249}
250
251/* Put some of our terminal settings into effect,
252   enough to get proper results from our output,
253   but do not change into or out of RAW mode
254   so that no input is discarded.
255
256   After doing this, either terminal_ours or terminal_inferior
257   should be called to get back to a normal state of affairs.  */
258
259void
260terminal_ours_for_output ()
261{
262  terminal_ours_1 (1);
263}
264
265/* Put our terminal settings into effect.
266   First record the inferior's terminal settings
267   so they can be restored properly later.  */
268
269void
270terminal_ours ()
271{
272  terminal_ours_1 (0);
273}
274
275/* output_only is not used, and should not be used unless we introduce
276   separate terminal_is_ours and terminal_is_ours_for_output
277   flags.  */
278
279static void
280terminal_ours_1 (output_only)
281     int output_only;
282{
283  /* Checking inferior_thisrun_terminal is necessary so that
284     if GDB is running in the background, it won't block trying
285     to do the ioctl()'s below.  Checking gdb_has_a_terminal
286     avoids attempting all the ioctl's when running in batch.  */
287  if (inferior_thisrun_terminal != 0 || gdb_has_a_terminal () == 0)
288    return;
289
290  if (!terminal_is_ours)
291    {
292      /* Ignore this signal since it will happen when we try to set the
293	 pgrp.  */
294      void (*osigttou) ();
295      int result;
296
297      terminal_is_ours = 1;
298
299#ifdef SIGTTOU
300      if (job_control)
301	osigttou = (void (*) ()) signal (SIGTTOU, SIG_IGN);
302#endif
303
304      if (inferior_ttystate)
305	free (inferior_ttystate);
306      inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial);
307#ifdef HAVE_TERMIOS
308      inferior_process_group = tcgetpgrp (0);
309#endif
310#ifdef HAVE_SGTTY
311      ioctl (0, TIOCGPGRP, &inferior_process_group);
312#endif
313
314      /* Here we used to set ICANON in our ttystate, but I believe this
315	 was an artifact from before when we used readline.  Readline sets
316	 the tty state when it needs to.
317	 FIXME-maybe: However, query() expects non-raw mode and doesn't
318	 use readline.  Maybe query should use readline (on the other hand,
319	 this only matters for HAVE_SGTTY, not termio or termios, I think).  */
320
321      /* Set tty state to our_ttystate.  We don't change in our out of raw
322	 mode, to avoid flushing input.  We need to do the same thing
323	 regardless of output_only, because we don't have separate
324	 terminal_is_ours and terminal_is_ours_for_output flags.  It's OK,
325	 though, since readline will deal with raw mode when/if it needs to.
326	 */
327
328      SERIAL_NOFLUSH_SET_TTY_STATE (stdin_serial, our_ttystate,
329				    inferior_ttystate);
330
331      if (job_control)
332	{
333#ifdef HAVE_TERMIOS
334	  result = tcsetpgrp (0, our_process_group);
335#if 0
336	  /* This fails on Ultrix with EINVAL if you run the testsuite
337	     in the background with nohup, and then log out.  GDB never
338	     used to check for an error here, so perhaps there are other
339	     such situations as well.  */
340	  if (result == -1)
341	    fprintf_unfiltered (gdb_stderr, "[tcsetpgrp failed in terminal_ours: %s]\n",
342		     strerror (errno));
343#endif
344#endif /* termios */
345
346#ifdef HAVE_SGTTY
347	  result = ioctl (0, TIOCSPGRP, &our_process_group);
348#endif
349	}
350
351#ifdef SIGTTOU
352      if (job_control)
353	signal (SIGTTOU, osigttou);
354#endif
355
356      if (!job_control)
357	{
358	  signal (SIGINT, sigint_ours);
359	  signal (SIGQUIT, sigquit_ours);
360	}
361
362#ifdef F_GETFL
363      tflags_inferior = fcntl (0, F_GETFL, 0);
364
365      /* Is there a reason this is being done twice?  It happens both
366	 places we use F_SETFL, so I'm inclined to think perhaps there
367	 is some reason, however perverse.  Perhaps not though...  */
368      result = fcntl (0, F_SETFL, tflags_ours);
369      result = fcntl (0, F_SETFL, tflags_ours);
370#endif
371
372      result = result;	/* lint */
373    }
374}
375
376/* ARGSUSED */
377void
378term_info (arg, from_tty)
379     char *arg;
380     int from_tty;
381{
382  target_terminal_info (arg, from_tty);
383}
384
385/* ARGSUSED */
386void
387child_terminal_info (args, from_tty)
388     char *args;
389     int from_tty;
390{
391  if (!gdb_has_a_terminal ())
392    {
393      printf_filtered ("This GDB does not control a terminal.\n");
394      return;
395    }
396
397  printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
398
399  /* First the fcntl flags.  */
400  {
401    int flags;
402
403    flags = tflags_inferior;
404
405    printf_filtered ("File descriptor flags = ");
406
407#ifndef O_ACCMODE
408#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
409#endif
410    /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
411    switch (flags & (O_ACCMODE))
412      {
413      case O_RDONLY: printf_filtered ("O_RDONLY"); break;
414      case O_WRONLY: printf_filtered ("O_WRONLY"); break;
415      case O_RDWR: printf_filtered ("O_RDWR"); break;
416      }
417    flags &= ~(O_ACCMODE);
418
419#ifdef O_NONBLOCK
420    if (flags & O_NONBLOCK)
421      printf_filtered (" | O_NONBLOCK");
422    flags &= ~O_NONBLOCK;
423#endif
424
425#if defined (O_NDELAY)
426    /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
427       print it as O_NONBLOCK, which is good cause that is what POSIX
428       has, and the flag will already be cleared by the time we get here.  */
429    if (flags & O_NDELAY)
430      printf_filtered (" | O_NDELAY");
431    flags &= ~O_NDELAY;
432#endif
433
434    if (flags & O_APPEND)
435      printf_filtered (" | O_APPEND");
436    flags &= ~O_APPEND;
437
438#if defined (O_BINARY)
439    if (flags & O_BINARY)
440      printf_filtered (" | O_BINARY");
441    flags &= ~O_BINARY;
442#endif
443
444    if (flags)
445      printf_filtered (" | 0x%x", flags);
446    printf_filtered ("\n");
447  }
448
449#ifdef PROCESS_GROUP_TYPE
450  printf_filtered ("Process group = %d\n", inferior_process_group);
451#endif
452
453  SERIAL_PRINT_TTY_STATE (stdin_serial, inferior_ttystate);
454}
455
456/* NEW_TTY_PREFORK is called before forking a new child process,
457   so we can record the state of ttys in the child to be formed.
458   TTYNAME is null if we are to share the terminal with gdb;
459   or points to a string containing the name of the desired tty.
460
461   NEW_TTY is called in new child processes under Unix, which will
462   become debugger target processes.  This actually switches to
463   the terminal specified in the NEW_TTY_PREFORK call.  */
464
465void
466new_tty_prefork (ttyname)
467     char *ttyname;
468{
469  /* Save the name for later, for determining whether we and the child
470     are sharing a tty.  */
471  inferior_thisrun_terminal = ttyname;
472}
473
474void
475new_tty ()
476{
477  register int tty;
478
479  if (inferior_thisrun_terminal == 0)
480    return;
481#if !defined(__GO32__) && !defined(__WIN32__)
482#ifdef TIOCNOTTY
483  /* Disconnect the child process from our controlling terminal.  On some
484     systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
485     ignore SIGTTOU. */
486  tty = open("/dev/tty", O_RDWR);
487  if (tty > 0)
488    {
489      void (*osigttou) ();
490
491      osigttou = (void (*)()) signal(SIGTTOU, SIG_IGN);
492      ioctl(tty, TIOCNOTTY, 0);
493      close(tty);
494      signal(SIGTTOU, osigttou);
495    }
496#endif
497
498  /* Now open the specified new terminal.  */
499
500#ifdef USE_O_NOCTTY
501  tty = open(inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
502#else
503  tty = open(inferior_thisrun_terminal, O_RDWR);
504#endif
505  if (tty == -1)
506    {
507      print_sys_errmsg (inferior_thisrun_terminal, errno);
508      _exit(1);
509    }
510
511  /* Avoid use of dup2; doesn't exist on all systems.  */
512  if (tty != 0)
513    { close (0); dup (tty); }
514  if (tty != 1)
515    { close (1); dup (tty); }
516  if (tty != 2)
517    { close (2); dup (tty); }
518  if (tty > 2)
519    close(tty);
520#endif /* !go32 && !win32*/
521}
522
523/* Kill the inferior process.  Make us have no inferior.  */
524
525/* ARGSUSED */
526static void
527kill_command (arg, from_tty)
528     char *arg;
529     int from_tty;
530{
531  /* FIXME:  This should not really be inferior_pid (or target_has_execution).
532     It should be a distinct flag that indicates that a target is active, cuz
533     some targets don't have processes! */
534
535  if (inferior_pid == 0)
536    error ("The program is not being run.");
537  if (!query ("Kill the program being debugged? "))
538    error ("Not confirmed.");
539  target_kill ();
540
541  init_thread_list();		/* Destroy thread info */
542
543  /* Killing off the inferior can leave us with a core file.  If so,
544     print the state we are left in.  */
545  if (target_has_stack) {
546    printf_filtered ("In %s,\n", target_longname);
547    if (selected_frame == NULL)
548      fputs_filtered ("No selected stack frame.\n", gdb_stdout);
549    else
550      print_stack_frame (selected_frame, selected_frame_level, 1);
551  }
552}
553
554/* Call set_sigint_trap when you need to pass a signal on to an attached
555   process when handling SIGINT */
556
557/* ARGSUSED */
558static void
559pass_signal (signo)
560    int signo;
561{
562  kill (inferior_pid, SIGINT);
563}
564
565static void (*osig)();
566
567void
568set_sigint_trap()
569{
570  if (attach_flag || inferior_thisrun_terminal)
571    {
572      osig = (void (*) ()) signal (SIGINT, pass_signal);
573    }
574}
575
576void
577clear_sigint_trap()
578{
579  if (attach_flag || inferior_thisrun_terminal)
580    {
581      signal (SIGINT, osig);
582    }
583}
584
585#if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN)
586static void (*old_sigio) ();
587
588static void
589handle_sigio (signo)
590     int signo;
591{
592  int numfds;
593  fd_set readfds;
594
595  signal (SIGIO, handle_sigio);
596
597  FD_ZERO (&readfds);
598  FD_SET (target_activity_fd, &readfds);
599  numfds = select (target_activity_fd + 1, &readfds, NULL, NULL, NULL);
600  if (numfds >= 0 && FD_ISSET (target_activity_fd, &readfds))
601    {
602      if ((*target_activity_function) ())
603	kill (inferior_pid, SIGINT);
604    }
605}
606
607static int old_fcntl_flags;
608
609void
610set_sigio_trap ()
611{
612  if (target_activity_function)
613    {
614      old_sigio = (void (*) ()) signal (SIGIO, handle_sigio);
615      fcntl (target_activity_fd, F_SETOWN, getpid());
616      old_fcntl_flags = fcntl (target_activity_fd, F_GETFL, 0);
617      fcntl (target_activity_fd, F_SETFL, old_fcntl_flags | FASYNC);
618    }
619}
620
621void
622clear_sigio_trap ()
623{
624  if (target_activity_function)
625    {
626      signal (SIGIO, old_sigio);
627      fcntl (target_activity_fd, F_SETFL, old_fcntl_flags);
628    }
629}
630#else /* No SIGIO.  */
631void
632set_sigio_trap ()
633{
634  if (target_activity_function)
635    abort ();
636}
637
638void
639clear_sigio_trap ()
640{
641  if (target_activity_function)
642    abort ();
643}
644#endif /* No SIGIO.  */
645
646
647/* This is here because this is where we figure out whether we (probably)
648   have job control.  Just using job_control only does part of it because
649   setpgid or setpgrp might not exist on a system without job control.
650   It might be considered misplaced (on the other hand, process groups and
651   job control are closely related to ttys).
652
653   For a more clean implementation, in libiberty, put a setpgid which merely
654   calls setpgrp and a setpgrp which does nothing (any system with job control
655   will have one or the other).  */
656int
657gdb_setpgid ()
658{
659  int retval = 0;
660
661  if (job_control)
662    {
663#if defined (NEED_POSIX_SETPGID) || (defined (HAVE_TERMIOS) && defined (HAVE_SETPGID))
664      /* setpgid (0, 0) is supposed to work and mean the same thing as
665	 this, but on Ultrix 4.2A it fails with EPERM (and
666	 setpgid (getpid (), getpid ()) succeeds).  */
667      retval = setpgid (getpid (), getpid ());
668#else
669#if defined (TIOCGPGRP)
670#if defined(USG) && !defined(SETPGRP_ARGS)
671      retval = setpgrp ();
672#else
673      retval = setpgrp (getpid (), getpid ());
674#endif /* USG */
675#endif /* TIOCGPGRP.  */
676#endif /* NEED_POSIX_SETPGID */
677    }
678  return retval;
679}
680
681void
682_initialize_inflow ()
683{
684  add_info ("terminal", term_info,
685	   "Print inferior's saved terminal status.");
686
687  add_com ("kill", class_run, kill_command,
688	   "Kill execution of program being debugged.");
689
690  inferior_pid = 0;
691
692  terminal_is_ours = 1;
693
694  /* OK, figure out whether we have job control.  If neither termios nor
695     sgtty (i.e. termio or go32), leave job_control 0.  */
696
697#if defined (HAVE_TERMIOS)
698  /* Do all systems with termios have the POSIX way of identifying job
699     control?  I hope so.  */
700#ifdef _POSIX_JOB_CONTROL
701  job_control = 1;
702#else
703#ifdef _SC_JOB_CONTROL
704  job_control = sysconf (_SC_JOB_CONTROL);
705#else
706  job_control = 0;	/* have to assume the worst */
707#endif	/* _SC_JOB_CONTROL */
708#endif	/* _POSIX_JOB_CONTROL */
709#endif	/* HAVE_TERMIOS */
710
711#ifdef HAVE_SGTTY
712#ifdef TIOCGPGRP
713  job_control = 1;
714#else
715  job_control = 0;
716#endif /* TIOCGPGRP */
717#endif /* sgtty */
718}
719