fix typo during interactive_mode check in gdb_has_a_terminal
[deliverable/binutils-gdb.git] / gdb / inflow.c
1 /* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 2009, 2010, 2011 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "frame.h"
23 #include "inferior.h"
24 #include "command.h"
25 #include "serial.h"
26 #include "terminal.h"
27 #include "target.h"
28 #include "gdbthread.h"
29 #include "observer.h"
30
31 #include "gdb_string.h"
32 #include <signal.h>
33 #include <fcntl.h>
34 #include "gdb_select.h"
35
36 #include "inflow.h"
37 #include "gdbcmd.h"
38
39 #ifdef HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
41 #endif
42
43 #ifndef O_NOCTTY
44 #define O_NOCTTY 0
45 #endif
46
47 extern void _initialize_inflow (void);
48
49 static void pass_signal (int);
50
51 static void terminal_ours_1 (int);
52 \f
53 /* Record terminal status separately for debugger and inferior. */
54
55 static struct serial *stdin_serial;
56
57 /* Terminal related info we need to keep track of. Each inferior
58 holds an instance of this structure --- we save it whenever the
59 corresponding inferior stops, and restore it to the foreground
60 inferior when it resumes. */
61 struct terminal_info
62 {
63 /* The name of the tty (from the `tty' command) that we gave to the
64 inferior when it was started. */
65 char *run_terminal;
66
67 /* TTY state. We save it whenever the inferior stops, and restore
68 it when it resumes. */
69 serial_ttystate ttystate;
70
71 #ifdef PROCESS_GROUP_TYPE
72 /* Process group. Saved and restored just like ttystate. */
73 PROCESS_GROUP_TYPE process_group;
74 #endif
75
76 /* fcntl flags. Saved and restored just like ttystate. */
77 int tflags;
78 };
79
80 /* Our own tty state, which we restore every time we need to deal with
81 the terminal. This is only set once, when GDB first starts. The
82 settings of flags which readline saves and restores and
83 unimportant. */
84 static struct terminal_info our_terminal_info;
85
86 static struct terminal_info *get_inflow_inferior_data (struct inferior *);
87
88 #ifdef PROCESS_GROUP_TYPE
89
90 /* Return the process group of the current inferior. */
91
92 PROCESS_GROUP_TYPE
93 inferior_process_group (void)
94 {
95 return get_inflow_inferior_data (current_inferior ())->process_group;
96 }
97 #endif
98
99 /* While the inferior is running, we want SIGINT and SIGQUIT to go to the
100 inferior only. If we have job control, that takes care of it. If not,
101 we save our handlers in these two variables and set SIGINT and SIGQUIT
102 to SIG_IGN. */
103
104 static void (*sigint_ours) ();
105 static void (*sigquit_ours) ();
106
107 /* The name of the tty (from the `tty' command) that we're giving to
108 the inferior when starting it up. This is only (and should only
109 be) used as a transient global by new_tty_prefork,
110 create_tty_session, new_tty and new_tty_postfork, all called from
111 fork_inferior, while forking a new child. */
112 static const char *inferior_thisrun_terminal;
113
114 /* Nonzero if our terminal settings are in effect. Zero if the
115 inferior's settings are in effect. Ignored if !gdb_has_a_terminal
116 (). */
117
118 int terminal_is_ours;
119
120 #ifdef PROCESS_GROUP_TYPE
121 static PROCESS_GROUP_TYPE
122 gdb_getpgrp (void)
123 {
124 int process_group = -1;
125
126 #ifdef HAVE_TERMIOS
127 process_group = tcgetpgrp (0);
128 #endif
129 #ifdef HAVE_TERMIO
130 process_group = getpgrp ();
131 #endif
132 #ifdef HAVE_SGTTY
133 ioctl (0, TIOCGPGRP, &process_group);
134 #endif
135 return process_group;
136 }
137 #endif
138
139 enum
140 {
141 yes, no, have_not_checked
142 }
143 gdb_has_a_terminal_flag = have_not_checked;
144
145 /* The value of the "interactive-mode" setting. */
146 static enum auto_boolean interactive_mode = AUTO_BOOLEAN_AUTO;
147
148 /* Implement the "show interactive-mode" option. */
149
150 static void
151 show_interactive_mode (struct ui_file *file, int from_tty,
152 struct cmd_list_element *c,
153 const char *value)
154 {
155 if (interactive_mode == AUTO_BOOLEAN_AUTO)
156 fprintf_filtered (file, "Debugger's interactive mode "
157 "is %s (currently %s).\n",
158 value, gdb_has_a_terminal () ? "on" : "off");
159 else
160 fprintf_filtered (file, "Debugger's interactive mode is %s.\n", value);
161 }
162
163 /* Does GDB have a terminal (on stdin)? */
164 int
165 gdb_has_a_terminal (void)
166 {
167 if (interactive_mode != AUTO_BOOLEAN_AUTO)
168 return interactive_mode == AUTO_BOOLEAN_TRUE;
169
170 switch (gdb_has_a_terminal_flag)
171 {
172 case yes:
173 return 1;
174 case no:
175 return 0;
176 case have_not_checked:
177 /* Get all the current tty settings (including whether we have a
178 tty at all!). Can't do this in _initialize_inflow because
179 serial_fdopen() won't work until the serial_ops_list is
180 initialized. */
181
182 #ifdef F_GETFL
183 our_terminal_info.tflags = fcntl (0, F_GETFL, 0);
184 #endif
185
186 gdb_has_a_terminal_flag = no;
187 if (stdin_serial != NULL)
188 {
189 our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);
190
191 if (our_terminal_info.ttystate != NULL)
192 {
193 gdb_has_a_terminal_flag = yes;
194 #ifdef PROCESS_GROUP_TYPE
195 our_terminal_info.process_group = gdb_getpgrp ();
196 #endif
197 }
198 }
199
200 return gdb_has_a_terminal_flag == yes;
201 default:
202 /* "Can't happen". */
203 return 0;
204 }
205 }
206
207 /* Macro for printing errors from ioctl operations */
208
209 #define OOPSY(what) \
210 if (result == -1) \
211 fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
212 what, safe_strerror (errno))
213
214 static void terminal_ours_1 (int);
215
216 /* Initialize the terminal settings we record for the inferior,
217 before we actually run the inferior. */
218
219 void
220 terminal_init_inferior_with_pgrp (int pgrp)
221 {
222 if (gdb_has_a_terminal ())
223 {
224 struct inferior *inf = current_inferior ();
225 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
226
227 /* We could just as well copy our_ttystate (if we felt like
228 adding a new function serial_copy_tty_state()). */
229 xfree (tinfo->ttystate);
230 tinfo->ttystate = serial_get_tty_state (stdin_serial);
231
232 #ifdef PROCESS_GROUP_TYPE
233 tinfo->process_group = pgrp;
234 #endif
235
236 /* Make sure that next time we call terminal_inferior (which will be
237 before the program runs, as it needs to be), we install the new
238 process group. */
239 terminal_is_ours = 1;
240 }
241 }
242
243 /* Save the terminal settings again. This is necessary for the TUI
244 when it switches to TUI or non-TUI mode; curses changes the terminal
245 and gdb must be able to restore it correctly. */
246
247 void
248 terminal_save_ours (void)
249 {
250 if (gdb_has_a_terminal ())
251 {
252 /* We could just as well copy our_ttystate (if we felt like adding
253 a new function serial_copy_tty_state). */
254 xfree (our_terminal_info.ttystate);
255 our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);
256 }
257 }
258
259 void
260 terminal_init_inferior (void)
261 {
262 #ifdef PROCESS_GROUP_TYPE
263 /* This is for Lynx, and should be cleaned up by having Lynx be a separate
264 debugging target with a version of target_terminal_init_inferior which
265 passes in the process group to a generic routine which does all the work
266 (and the non-threaded child_terminal_init_inferior can just pass in
267 inferior_ptid to the same routine). */
268 /* We assume INFERIOR_PID is also the child's process group. */
269 terminal_init_inferior_with_pgrp (PIDGET (inferior_ptid));
270 #endif /* PROCESS_GROUP_TYPE */
271 }
272
273 /* Put the inferior's terminal settings into effect.
274 This is preparation for starting or resuming the inferior. */
275
276 void
277 terminal_inferior (void)
278 {
279 struct inferior *inf;
280 struct terminal_info *tinfo;
281
282 if (!terminal_is_ours)
283 return;
284
285 inf = current_inferior ();
286 tinfo = get_inflow_inferior_data (inf);
287
288 if (gdb_has_a_terminal ()
289 && tinfo->ttystate != NULL
290 && tinfo->run_terminal == NULL)
291 {
292 int result;
293
294 #ifdef F_GETFL
295 /* Is there a reason this is being done twice? It happens both
296 places we use F_SETFL, so I'm inclined to think perhaps there
297 is some reason, however perverse. Perhaps not though... */
298 result = fcntl (0, F_SETFL, tinfo->tflags);
299 result = fcntl (0, F_SETFL, tinfo->tflags);
300 OOPSY ("fcntl F_SETFL");
301 #endif
302
303 /* Because we were careful to not change in or out of raw mode in
304 terminal_ours, we will not change in our out of raw mode with
305 this call, so we don't flush any input. */
306 result = serial_set_tty_state (stdin_serial,
307 tinfo->ttystate);
308 OOPSY ("setting tty state");
309
310 if (!job_control)
311 {
312 sigint_ours = (void (*)()) signal (SIGINT, SIG_IGN);
313 #ifdef SIGQUIT
314 sigquit_ours = (void (*)()) signal (SIGQUIT, SIG_IGN);
315 #endif
316 }
317
318 /* If attach_flag is set, we don't know whether we are sharing a
319 terminal with the inferior or not. (attaching a process
320 without a terminal is one case where we do not; attaching a
321 process which we ran from the same shell as GDB via `&' is
322 one case where we do, I think (but perhaps this is not
323 `sharing' in the sense that we need to save and restore tty
324 state)). I don't know if there is any way to tell whether we
325 are sharing a terminal. So what we do is to go through all
326 the saving and restoring of the tty state, but ignore errors
327 setting the process group, which will happen if we are not
328 sharing a terminal). */
329
330 if (job_control)
331 {
332 #ifdef HAVE_TERMIOS
333 result = tcsetpgrp (0, tinfo->process_group);
334 if (!inf->attach_flag)
335 OOPSY ("tcsetpgrp");
336 #endif
337
338 #ifdef HAVE_SGTTY
339 result = ioctl (0, TIOCSPGRP, &tinfo->process_group);
340 if (!inf->attach_flag)
341 OOPSY ("TIOCSPGRP");
342 #endif
343 }
344
345 }
346 terminal_is_ours = 0;
347 }
348
349 /* Put some of our terminal settings into effect,
350 enough to get proper results from our output,
351 but do not change into or out of RAW mode
352 so that no input is discarded.
353
354 After doing this, either terminal_ours or terminal_inferior
355 should be called to get back to a normal state of affairs. */
356
357 void
358 terminal_ours_for_output (void)
359 {
360 terminal_ours_1 (1);
361 }
362
363 /* Put our terminal settings into effect.
364 First record the inferior's terminal settings
365 so they can be restored properly later. */
366
367 void
368 terminal_ours (void)
369 {
370 terminal_ours_1 (0);
371 }
372
373 /* output_only is not used, and should not be used unless we introduce
374 separate terminal_is_ours and terminal_is_ours_for_output
375 flags. */
376
377 static void
378 terminal_ours_1 (int output_only)
379 {
380 struct inferior *inf;
381 struct terminal_info *tinfo;
382
383 if (terminal_is_ours)
384 return;
385
386 terminal_is_ours = 1;
387
388 /* Checking inferior->run_terminal is necessary so that
389 if GDB is running in the background, it won't block trying
390 to do the ioctl()'s below. Checking gdb_has_a_terminal
391 avoids attempting all the ioctl's when running in batch. */
392
393 inf = current_inferior ();
394 tinfo = get_inflow_inferior_data (inf);
395
396 if (tinfo->run_terminal != NULL || gdb_has_a_terminal () == 0)
397 return;
398
399 {
400 #ifdef SIGTTOU
401 /* Ignore this signal since it will happen when we try to set the
402 pgrp. */
403 void (*osigttou) () = NULL;
404 #endif
405 int result;
406
407 #ifdef SIGTTOU
408 if (job_control)
409 osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN);
410 #endif
411
412 xfree (tinfo->ttystate);
413 tinfo->ttystate = serial_get_tty_state (stdin_serial);
414
415 #ifdef PROCESS_GROUP_TYPE
416 if (!inf->attach_flag)
417 /* If setpgrp failed in terminal_inferior, this would give us
418 our process group instead of the inferior's. See
419 terminal_inferior for details. */
420 tinfo->process_group = gdb_getpgrp ();
421 #endif
422
423 /* Here we used to set ICANON in our ttystate, but I believe this
424 was an artifact from before when we used readline. Readline sets
425 the tty state when it needs to.
426 FIXME-maybe: However, query() expects non-raw mode and doesn't
427 use readline. Maybe query should use readline (on the other hand,
428 this only matters for HAVE_SGTTY, not termio or termios, I think). */
429
430 /* Set tty state to our_ttystate. We don't change in our out of raw
431 mode, to avoid flushing input. We need to do the same thing
432 regardless of output_only, because we don't have separate
433 terminal_is_ours and terminal_is_ours_for_output flags. It's OK,
434 though, since readline will deal with raw mode when/if it needs
435 to. */
436
437 serial_noflush_set_tty_state (stdin_serial, our_terminal_info.ttystate,
438 tinfo->ttystate);
439
440 if (job_control)
441 {
442 #ifdef HAVE_TERMIOS
443 result = tcsetpgrp (0, our_terminal_info.process_group);
444 #if 0
445 /* This fails on Ultrix with EINVAL if you run the testsuite
446 in the background with nohup, and then log out. GDB never
447 used to check for an error here, so perhaps there are other
448 such situations as well. */
449 if (result == -1)
450 fprintf_unfiltered (gdb_stderr,
451 "[tcsetpgrp failed in terminal_ours: %s]\n",
452 safe_strerror (errno));
453 #endif
454 #endif /* termios */
455
456 #ifdef HAVE_SGTTY
457 result = ioctl (0, TIOCSPGRP, &our_terminal_info.process_group);
458 #endif
459 }
460
461 #ifdef SIGTTOU
462 if (job_control)
463 signal (SIGTTOU, osigttou);
464 #endif
465
466 if (!job_control)
467 {
468 signal (SIGINT, sigint_ours);
469 #ifdef SIGQUIT
470 signal (SIGQUIT, sigquit_ours);
471 #endif
472 }
473
474 #ifdef F_GETFL
475 tinfo->tflags = fcntl (0, F_GETFL, 0);
476
477 /* Is there a reason this is being done twice? It happens both
478 places we use F_SETFL, so I'm inclined to think perhaps there
479 is some reason, however perverse. Perhaps not though... */
480 result = fcntl (0, F_SETFL, our_terminal_info.tflags);
481 result = fcntl (0, F_SETFL, our_terminal_info.tflags);
482 #endif
483 }
484 }
485
486 /* Per-inferior data key. */
487 static const struct inferior_data *inflow_inferior_data;
488
489 static void
490 inflow_inferior_data_cleanup (struct inferior *inf, void *arg)
491 {
492 struct terminal_info *info;
493
494 info = inferior_data (inf, inflow_inferior_data);
495 if (info != NULL)
496 {
497 xfree (info->run_terminal);
498 xfree (info);
499 }
500 }
501
502 /* Get the current svr4 data. If none is found yet, add it now. This
503 function always returns a valid object. */
504
505 static struct terminal_info *
506 get_inflow_inferior_data (struct inferior *inf)
507 {
508 struct terminal_info *info;
509
510 info = inferior_data (inf, inflow_inferior_data);
511 if (info == NULL)
512 {
513 info = XZALLOC (struct terminal_info);
514 set_inferior_data (inf, inflow_inferior_data, info);
515 }
516
517 return info;
518 }
519
520 /* This is a "inferior_exit" observer. Releases the TERMINAL_INFO member
521 of the inferior structure. This field is private to inflow.c, and
522 its type is opaque to the rest of GDB. PID is the target pid of
523 the inferior that is about to be removed from the inferior
524 list. */
525
526 static void
527 inflow_inferior_exit (struct inferior *inf)
528 {
529 struct terminal_info *info;
530
531 info = inferior_data (inf, inflow_inferior_data);
532 if (info != NULL)
533 {
534 xfree (info->run_terminal);
535 xfree (info);
536 set_inferior_data (inf, inflow_inferior_data, NULL);
537 }
538 }
539
540 void
541 copy_terminal_info (struct inferior *to, struct inferior *from)
542 {
543 struct terminal_info *tinfo_to, *tinfo_from;
544
545 tinfo_to = get_inflow_inferior_data (to);
546 tinfo_from = get_inflow_inferior_data (from);
547 *tinfo_to = *tinfo_from;
548 if (tinfo_from->run_terminal)
549 tinfo_to->run_terminal
550 = xstrdup (tinfo_from->run_terminal);
551 }
552
553 void
554 term_info (char *arg, int from_tty)
555 {
556 target_terminal_info (arg, from_tty);
557 }
558
559 void
560 child_terminal_info (char *args, int from_tty)
561 {
562 struct inferior *inf;
563 struct terminal_info *tinfo;
564
565 if (!gdb_has_a_terminal ())
566 {
567 printf_filtered (_("This GDB does not control a terminal.\n"));
568 return;
569 }
570
571 if (ptid_equal (inferior_ptid, null_ptid))
572 return;
573
574 inf = current_inferior ();
575 tinfo = get_inflow_inferior_data (inf);
576
577 printf_filtered (_("Inferior's terminal status "
578 "(currently saved by GDB):\n"));
579
580 /* First the fcntl flags. */
581 {
582 int flags;
583
584 flags = tinfo->tflags;
585
586 printf_filtered ("File descriptor flags = ");
587
588 #ifndef O_ACCMODE
589 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
590 #endif
591 /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */
592 switch (flags & (O_ACCMODE))
593 {
594 case O_RDONLY:
595 printf_filtered ("O_RDONLY");
596 break;
597 case O_WRONLY:
598 printf_filtered ("O_WRONLY");
599 break;
600 case O_RDWR:
601 printf_filtered ("O_RDWR");
602 break;
603 }
604 flags &= ~(O_ACCMODE);
605
606 #ifdef O_NONBLOCK
607 if (flags & O_NONBLOCK)
608 printf_filtered (" | O_NONBLOCK");
609 flags &= ~O_NONBLOCK;
610 #endif
611
612 #if defined (O_NDELAY)
613 /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
614 print it as O_NONBLOCK, which is good cause that is what POSIX
615 has, and the flag will already be cleared by the time we get here. */
616 if (flags & O_NDELAY)
617 printf_filtered (" | O_NDELAY");
618 flags &= ~O_NDELAY;
619 #endif
620
621 if (flags & O_APPEND)
622 printf_filtered (" | O_APPEND");
623 flags &= ~O_APPEND;
624
625 #if defined (O_BINARY)
626 if (flags & O_BINARY)
627 printf_filtered (" | O_BINARY");
628 flags &= ~O_BINARY;
629 #endif
630
631 if (flags)
632 printf_filtered (" | 0x%x", flags);
633 printf_filtered ("\n");
634 }
635
636 #ifdef PROCESS_GROUP_TYPE
637 printf_filtered ("Process group = %d\n", (int) tinfo->process_group);
638 #endif
639
640 serial_print_tty_state (stdin_serial, tinfo->ttystate, gdb_stdout);
641 }
642 \f
643 /* NEW_TTY_PREFORK is called before forking a new child process,
644 so we can record the state of ttys in the child to be formed.
645 TTYNAME is null if we are to share the terminal with gdb;
646 or points to a string containing the name of the desired tty.
647
648 NEW_TTY is called in new child processes under Unix, which will
649 become debugger target processes. This actually switches to
650 the terminal specified in the NEW_TTY_PREFORK call. */
651
652 void
653 new_tty_prefork (const char *ttyname)
654 {
655 /* Save the name for later, for determining whether we and the child
656 are sharing a tty. */
657 inferior_thisrun_terminal = ttyname;
658 }
659
660 #if !defined(__GO32__) && !defined(_WIN32)
661 /* If RESULT, assumed to be the return value from a system call, is
662 negative, print the error message indicated by errno and exit.
663 MSG should identify the operation that failed. */
664 static void
665 check_syscall (const char *msg, int result)
666 {
667 if (result < 0)
668 {
669 print_sys_errmsg (msg, errno);
670 _exit (1);
671 }
672 }
673 #endif
674
675 void
676 new_tty (void)
677 {
678 int tty;
679
680 if (inferior_thisrun_terminal == 0)
681 return;
682 #if !defined(__GO32__) && !defined(_WIN32)
683 #ifdef TIOCNOTTY
684 /* Disconnect the child process from our controlling terminal. On some
685 systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
686 ignore SIGTTOU. */
687 tty = open ("/dev/tty", O_RDWR);
688 if (tty > 0)
689 {
690 void (*osigttou) ();
691
692 osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN);
693 ioctl (tty, TIOCNOTTY, 0);
694 close (tty);
695 signal (SIGTTOU, osigttou);
696 }
697 #endif
698
699 /* Now open the specified new terminal. */
700 tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
701 check_syscall (inferior_thisrun_terminal, tty);
702
703 /* Avoid use of dup2; doesn't exist on all systems. */
704 if (tty != 0)
705 {
706 close (0);
707 check_syscall ("dup'ing tty into fd 0", dup (tty));
708 }
709 if (tty != 1)
710 {
711 close (1);
712 check_syscall ("dup'ing tty into fd 1", dup (tty));
713 }
714 if (tty != 2)
715 {
716 close (2);
717 check_syscall ("dup'ing tty into fd 2", dup (tty));
718 }
719
720 #ifdef TIOCSCTTY
721 /* Make tty our new controlling terminal. */
722 if (ioctl (tty, TIOCSCTTY, 0) == -1)
723 /* Mention GDB in warning because it will appear in the inferior's
724 terminal instead of GDB's. */
725 warning (_("GDB: Failed to set controlling terminal: %s"),
726 safe_strerror (errno));
727 #endif
728
729 if (tty > 2)
730 close (tty);
731 #endif /* !go32 && !win32 */
732 }
733
734 /* NEW_TTY_POSTFORK is called after forking a new child process, and
735 adding it to the inferior table, to store the TTYNAME being used by
736 the child, or null if it sharing the terminal with gdb. */
737
738 void
739 new_tty_postfork (void)
740 {
741 /* Save the name for later, for determining whether we and the child
742 are sharing a tty. */
743
744 if (inferior_thisrun_terminal)
745 {
746 struct inferior *inf = current_inferior ();
747 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
748
749 tinfo->run_terminal = xstrdup (inferior_thisrun_terminal);
750 }
751
752 inferior_thisrun_terminal = NULL;
753 }
754
755 \f
756 /* Call set_sigint_trap when you need to pass a signal on to an attached
757 process when handling SIGINT. */
758
759 static void
760 pass_signal (int signo)
761 {
762 #ifndef _WIN32
763 kill (PIDGET (inferior_ptid), SIGINT);
764 #endif
765 }
766
767 static void (*osig) ();
768 static int osig_set;
769
770 void
771 set_sigint_trap (void)
772 {
773 struct inferior *inf = current_inferior ();
774 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
775
776 if (inf->attach_flag || tinfo->run_terminal)
777 {
778 osig = (void (*)()) signal (SIGINT, pass_signal);
779 osig_set = 1;
780 }
781 else
782 osig_set = 0;
783 }
784
785 void
786 clear_sigint_trap (void)
787 {
788 if (osig_set)
789 {
790 signal (SIGINT, osig);
791 osig_set = 0;
792 }
793 }
794 \f
795
796 /* Create a new session if the inferior will run in a different tty.
797 A session is UNIX's way of grouping processes that share a controlling
798 terminal, so a new one is needed if the inferior terminal will be
799 different from GDB's.
800
801 Returns the session id of the new session, 0 if no session was created
802 or -1 if an error occurred. */
803 pid_t
804 create_tty_session (void)
805 {
806 #ifdef HAVE_SETSID
807 pid_t ret;
808
809 if (!job_control || inferior_thisrun_terminal == 0)
810 return 0;
811
812 ret = setsid ();
813 if (ret == -1)
814 warning (_("Failed to create new terminal session: setsid: %s"),
815 safe_strerror (errno));
816
817 return ret;
818 #else
819 return 0;
820 #endif /* HAVE_SETSID */
821 }
822
823 /* This is here because this is where we figure out whether we (probably)
824 have job control. Just using job_control only does part of it because
825 setpgid or setpgrp might not exist on a system without job control.
826 It might be considered misplaced (on the other hand, process groups and
827 job control are closely related to ttys).
828
829 For a more clean implementation, in libiberty, put a setpgid which merely
830 calls setpgrp and a setpgrp which does nothing (any system with job control
831 will have one or the other). */
832 int
833 gdb_setpgid (void)
834 {
835 int retval = 0;
836
837 if (job_control)
838 {
839 #if defined (HAVE_TERMIOS) || defined (TIOCGPGRP)
840 #ifdef HAVE_SETPGID
841 /* The call setpgid (0, 0) is supposed to work and mean the same
842 thing as this, but on Ultrix 4.2A it fails with EPERM (and
843 setpgid (getpid (), getpid ()) succeeds). */
844 retval = setpgid (getpid (), getpid ());
845 #else
846 #ifdef HAVE_SETPGRP
847 #ifdef SETPGRP_VOID
848 retval = setpgrp ();
849 #else
850 retval = setpgrp (getpid (), getpid ());
851 #endif
852 #endif /* HAVE_SETPGRP */
853 #endif /* HAVE_SETPGID */
854 #endif /* defined (HAVE_TERMIOS) || defined (TIOCGPGRP) */
855 }
856
857 return retval;
858 }
859
860 /* Get all the current tty settings (including whether we have a
861 tty at all!). We can't do this in _initialize_inflow because
862 serial_fdopen() won't work until the serial_ops_list is
863 initialized, but we don't want to do it lazily either, so
864 that we can guarantee stdin_serial is opened if there is
865 a terminal. */
866 void
867 initialize_stdin_serial (void)
868 {
869 stdin_serial = serial_fdopen (0);
870 }
871
872 void
873 _initialize_inflow (void)
874 {
875 add_info ("terminal", term_info,
876 _("Print inferior's saved terminal status."));
877
878 add_setshow_auto_boolean_cmd ("interactive-mode", class_support,
879 &interactive_mode, _("\
880 Set whether GDB's standard input is a terminal."), _("\
881 Show whether GDB's standard input is a terminal."), _("\
882 If on, GDB assumes that standard input is a terminal. In practice, it\n\
883 means that GDB should wait for the user to answer queries associated to\n\
884 commands entered at the command prompt. If off, GDB assumes that standard\n\
885 input is not a terminal, and uses the default answer to all queries.\n\
886 If auto (the default), determine which mode to use based on the standard\n\
887 input settings."),
888 NULL,
889 show_interactive_mode,
890 &setlist, &showlist);
891
892 terminal_is_ours = 1;
893
894 /* OK, figure out whether we have job control. If neither termios nor
895 sgtty (i.e. termio or go32), leave job_control 0. */
896
897 #if defined (HAVE_TERMIOS)
898 /* Do all systems with termios have the POSIX way of identifying job
899 control? I hope so. */
900 #ifdef _POSIX_JOB_CONTROL
901 job_control = 1;
902 #else
903 #ifdef _SC_JOB_CONTROL
904 job_control = sysconf (_SC_JOB_CONTROL);
905 #else
906 job_control = 0; /* Have to assume the worst. */
907 #endif /* _SC_JOB_CONTROL */
908 #endif /* _POSIX_JOB_CONTROL */
909 #endif /* HAVE_TERMIOS */
910
911 #ifdef HAVE_SGTTY
912 #ifdef TIOCGPGRP
913 job_control = 1;
914 #else
915 job_control = 0;
916 #endif /* TIOCGPGRP */
917 #endif /* sgtty */
918
919 observer_attach_inferior_exit (inflow_inferior_exit);
920
921 inflow_inferior_data
922 = register_inferior_data_with_cleanup (inflow_inferior_data_cleanup);
923 }
This page took 0.083852 seconds and 5 git commands to generate.