Don't set terminal flags twice in a row
[binutils-gdb.git] / gdb / inflow.c
1 /* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1986-2017 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include "defs.h"
20 #include "frame.h"
21 #include "inferior.h"
22 #include "command.h"
23 #include "serial.h"
24 #include "terminal.h"
25 #include "target.h"
26 #include "gdbthread.h"
27 #include "observer.h"
28 #include <signal.h>
29 #include <fcntl.h>
30 #include "gdb_select.h"
31
32 #include "inflow.h"
33 #include "gdbcmd.h"
34 #ifdef HAVE_TERMIOS_H
35 #include <termios.h>
36 #endif
37 #include "job-control.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 static void pass_signal (int);
48
49 static void child_terminal_ours_1 (int);
50 \f
51 /* Record terminal status separately for debugger and inferior. */
52
53 static struct serial *stdin_serial;
54
55 /* Terminal related info we need to keep track of. Each inferior
56 holds an instance of this structure --- we save it whenever the
57 corresponding inferior stops, and restore it to the foreground
58 inferior when it resumes. */
59 struct terminal_info
60 {
61 /* The name of the tty (from the `tty' command) that we gave to the
62 inferior when it was started. */
63 char *run_terminal;
64
65 /* TTY state. We save it whenever the inferior stops, and restore
66 it when it resumes. */
67 serial_ttystate ttystate;
68
69 #ifdef HAVE_TERMIOS_H
70 /* Process group. Saved and restored just like ttystate. */
71 pid_t process_group;
72 #endif
73
74 /* fcntl flags. Saved and restored just like ttystate. */
75 int tflags;
76 };
77
78 /* Our own tty state, which we restore every time we need to deal with
79 the terminal. This is set once, when GDB first starts, and then
80 whenever we enter/leave TUI mode (gdb_save_tty_state). The
81 settings of flags which readline saves and restores are
82 unimportant. */
83 static struct terminal_info our_terminal_info;
84
85 /* Snapshot of the initial tty state taken during initialization of
86 GDB, before readline/ncurses have had a chance to change it. This
87 is used as the initial tty state given to each new spawned
88 inferior. Unlike our_terminal_info, this is only ever set
89 once. */
90 static serial_ttystate initial_gdb_ttystate;
91
92 static struct terminal_info *get_inflow_inferior_data (struct inferior *);
93
94 #ifdef HAVE_TERMIOS_H
95
96 /* Return the process group of the current inferior. */
97
98 pid_t
99 inferior_process_group (void)
100 {
101 return get_inflow_inferior_data (current_inferior ())->process_group;
102 }
103 #endif
104
105 /* While the inferior is running, we want SIGINT and SIGQUIT to go to the
106 inferior only. If we have job control, that takes care of it. If not,
107 we save our handlers in these two variables and set SIGINT and SIGQUIT
108 to SIG_IGN. */
109
110 static sighandler_t sigint_ours;
111 static sighandler_t sigquit_ours;
112
113 /* The name of the tty (from the `tty' command) that we're giving to
114 the inferior when starting it up. This is only (and should only
115 be) used as a transient global by new_tty_prefork,
116 create_tty_session, new_tty and new_tty_postfork, all called from
117 fork_inferior, while forking a new child. */
118 static const char *inferior_thisrun_terminal;
119
120 /* Nonzero if our terminal settings are in effect. Zero if the
121 inferior's settings are in effect. Ignored if !gdb_has_a_terminal
122 (). */
123
124 int terminal_is_ours;
125
126 /* See terminal.h. */
127
128 void
129 set_initial_gdb_ttystate (void)
130 {
131 /* Note we can't do any of this in _initialize_inflow because at
132 that point stdin_serial has not been created yet. */
133
134 initial_gdb_ttystate = serial_get_tty_state (stdin_serial);
135
136 if (initial_gdb_ttystate != NULL)
137 {
138 our_terminal_info.ttystate
139 = serial_copy_tty_state (stdin_serial, initial_gdb_ttystate);
140 #ifdef F_GETFL
141 our_terminal_info.tflags = fcntl (0, F_GETFL, 0);
142 #endif
143 #ifdef HAVE_TERMIOS_H
144 our_terminal_info.process_group = tcgetpgrp (0);
145 #endif
146 }
147 }
148
149 /* Does GDB have a terminal (on stdin)? */
150
151 static int
152 gdb_has_a_terminal (void)
153 {
154 return initial_gdb_ttystate != NULL;
155 }
156
157 /* Macro for printing errors from ioctl operations */
158
159 #define OOPSY(what) \
160 if (result == -1) \
161 fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
162 what, safe_strerror (errno))
163
164 /* Initialize the terminal settings we record for the inferior,
165 before we actually run the inferior. */
166
167 void
168 child_terminal_init_with_pgrp (int pgrp)
169 {
170 struct inferior *inf = current_inferior ();
171 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
172
173 #ifdef HAVE_TERMIOS_H
174 /* Store the process group even without a terminal as it is used not
175 only to reset the tty foreground process group, but also to
176 interrupt the inferior. */
177 tinfo->process_group = pgrp;
178 #endif
179
180 if (gdb_has_a_terminal ())
181 {
182 xfree (tinfo->ttystate);
183 tinfo->ttystate = serial_copy_tty_state (stdin_serial,
184 initial_gdb_ttystate);
185
186 /* Make sure that next time we call terminal_inferior (which will be
187 before the program runs, as it needs to be), we install the new
188 process group. */
189 terminal_is_ours = 1;
190 }
191 }
192
193 /* Save the terminal settings again. This is necessary for the TUI
194 when it switches to TUI or non-TUI mode; curses changes the terminal
195 and gdb must be able to restore it correctly. */
196
197 void
198 gdb_save_tty_state (void)
199 {
200 if (gdb_has_a_terminal ())
201 {
202 xfree (our_terminal_info.ttystate);
203 our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);
204 }
205 }
206
207 void
208 child_terminal_init (struct target_ops *self)
209 {
210 #ifdef HAVE_TERMIOS_H
211 /* This is for Lynx, and should be cleaned up by having Lynx be a
212 separate debugging target with a version of target_terminal::init
213 which passes in the process group to a generic routine which does
214 all the work (and the non-threaded child_terminal_init can just
215 pass in inferior_ptid to the same routine). */
216 /* We assume INFERIOR_PID is also the child's process group. */
217 child_terminal_init_with_pgrp (ptid_get_pid (inferior_ptid));
218 #endif /* HAVE_TERMIOS_H */
219 }
220
221 /* Put the inferior's terminal settings into effect.
222 This is preparation for starting or resuming the inferior.
223
224 N.B. Targets that want to use this with async support must build that
225 support on top of this (e.g., the caller still needs to remove stdin
226 from the event loop). E.g., see linux_nat_terminal_inferior. */
227
228 void
229 child_terminal_inferior (struct target_ops *self)
230 {
231 struct inferior *inf;
232 struct terminal_info *tinfo;
233
234 if (!terminal_is_ours)
235 return;
236
237 inf = current_inferior ();
238 tinfo = get_inflow_inferior_data (inf);
239
240 if (gdb_has_a_terminal ()
241 && tinfo->ttystate != NULL
242 && tinfo->run_terminal == NULL)
243 {
244 int result;
245
246 #ifdef F_GETFL
247 result = fcntl (0, F_SETFL, tinfo->tflags);
248 OOPSY ("fcntl F_SETFL");
249 #endif
250
251 result = serial_set_tty_state (stdin_serial, tinfo->ttystate);
252 OOPSY ("setting tty state");
253
254 if (!job_control)
255 {
256 sigint_ours = signal (SIGINT, SIG_IGN);
257 #ifdef SIGQUIT
258 sigquit_ours = signal (SIGQUIT, SIG_IGN);
259 #endif
260 }
261
262 /* If attach_flag is set, we don't know whether we are sharing a
263 terminal with the inferior or not. (attaching a process
264 without a terminal is one case where we do not; attaching a
265 process which we ran from the same shell as GDB via `&' is
266 one case where we do, I think (but perhaps this is not
267 `sharing' in the sense that we need to save and restore tty
268 state)). I don't know if there is any way to tell whether we
269 are sharing a terminal. So what we do is to go through all
270 the saving and restoring of the tty state, but ignore errors
271 setting the process group, which will happen if we are not
272 sharing a terminal). */
273
274 if (job_control)
275 {
276 #ifdef HAVE_TERMIOS_H
277 result = tcsetpgrp (0, tinfo->process_group);
278 if (!inf->attach_flag)
279 OOPSY ("tcsetpgrp");
280 #endif
281 }
282 }
283 terminal_is_ours = 0;
284 }
285
286 /* Put some of our terminal settings into effect,
287 enough to get proper results from our output,
288 but do not change into or out of RAW mode
289 so that no input is discarded.
290
291 After doing this, either terminal_ours or terminal_inferior
292 should be called to get back to a normal state of affairs.
293
294 N.B. The implementation is (currently) no different than
295 child_terminal_ours. See child_terminal_ours_1. */
296
297 void
298 child_terminal_ours_for_output (struct target_ops *self)
299 {
300 child_terminal_ours_1 (1);
301 }
302
303 /* Put our terminal settings into effect.
304 First record the inferior's terminal settings
305 so they can be restored properly later.
306
307 N.B. Targets that want to use this with async support must build that
308 support on top of this (e.g., the caller still needs to add stdin to the
309 event loop). E.g., see linux_nat_terminal_ours. */
310
311 void
312 child_terminal_ours (struct target_ops *self)
313 {
314 child_terminal_ours_1 (0);
315 }
316
317 /* output_only is not used, and should not be used unless we introduce
318 separate terminal_is_ours and terminal_is_ours_for_output
319 flags. */
320
321 static void
322 child_terminal_ours_1 (int output_only)
323 {
324 struct inferior *inf;
325 struct terminal_info *tinfo;
326
327 if (terminal_is_ours)
328 return;
329
330 terminal_is_ours = 1;
331
332 /* Checking inferior->run_terminal is necessary so that
333 if GDB is running in the background, it won't block trying
334 to do the ioctl()'s below. Checking gdb_has_a_terminal
335 avoids attempting all the ioctl's when running in batch. */
336
337 inf = current_inferior ();
338 tinfo = get_inflow_inferior_data (inf);
339
340 if (tinfo->run_terminal != NULL || gdb_has_a_terminal () == 0)
341 return;
342 else
343 {
344 #ifdef SIGTTOU
345 /* Ignore this signal since it will happen when we try to set the
346 pgrp. */
347 sighandler_t osigttou = NULL;
348 #endif
349 int result ATTRIBUTE_UNUSED;
350
351 #ifdef SIGTTOU
352 if (job_control)
353 osigttou = signal (SIGTTOU, SIG_IGN);
354 #endif
355
356 xfree (tinfo->ttystate);
357 tinfo->ttystate = serial_get_tty_state (stdin_serial);
358
359 #ifdef HAVE_TERMIOS_H
360 if (!inf->attach_flag)
361 /* If tcsetpgrp failed in terminal_inferior, this would give us
362 our process group instead of the inferior's. See
363 terminal_inferior for details. */
364 tinfo->process_group = tcgetpgrp (0);
365 #endif
366
367 /* Set tty state to our_ttystate. */
368 serial_set_tty_state (stdin_serial, our_terminal_info.ttystate);
369
370 if (job_control)
371 {
372 #ifdef HAVE_TERMIOS_H
373 result = tcsetpgrp (0, our_terminal_info.process_group);
374 #if 0
375 /* This fails on Ultrix with EINVAL if you run the testsuite
376 in the background with nohup, and then log out. GDB never
377 used to check for an error here, so perhaps there are other
378 such situations as well. */
379 if (result == -1)
380 fprintf_unfiltered (gdb_stderr,
381 "[tcsetpgrp failed in child_terminal_ours: %s]\n",
382 safe_strerror (errno));
383 #endif
384 #endif /* termios */
385 }
386
387 #ifdef SIGTTOU
388 if (job_control)
389 signal (SIGTTOU, osigttou);
390 #endif
391
392 if (!job_control)
393 {
394 signal (SIGINT, sigint_ours);
395 #ifdef SIGQUIT
396 signal (SIGQUIT, sigquit_ours);
397 #endif
398 }
399
400 #ifdef F_GETFL
401 tinfo->tflags = fcntl (0, F_GETFL, 0);
402 result = fcntl (0, F_SETFL, our_terminal_info.tflags);
403 #endif
404 }
405 }
406
407 /* Per-inferior data key. */
408 static const struct inferior_data *inflow_inferior_data;
409
410 static void
411 inflow_inferior_data_cleanup (struct inferior *inf, void *arg)
412 {
413 struct terminal_info *info = (struct terminal_info *) arg;
414
415 xfree (info->run_terminal);
416 xfree (info->ttystate);
417 xfree (info);
418 }
419
420 /* Get the current svr4 data. If none is found yet, add it now. This
421 function always returns a valid object. */
422
423 static struct terminal_info *
424 get_inflow_inferior_data (struct inferior *inf)
425 {
426 struct terminal_info *info;
427
428 info = (struct terminal_info *) inferior_data (inf, inflow_inferior_data);
429 if (info == NULL)
430 {
431 info = XCNEW (struct terminal_info);
432 set_inferior_data (inf, inflow_inferior_data, info);
433 }
434
435 return info;
436 }
437
438 /* This is a "inferior_exit" observer. Releases the TERMINAL_INFO member
439 of the inferior structure. This field is private to inflow.c, and
440 its type is opaque to the rest of GDB. PID is the target pid of
441 the inferior that is about to be removed from the inferior
442 list. */
443
444 static void
445 inflow_inferior_exit (struct inferior *inf)
446 {
447 struct terminal_info *info;
448
449 info = (struct terminal_info *) inferior_data (inf, inflow_inferior_data);
450 if (info != NULL)
451 {
452 xfree (info->run_terminal);
453 xfree (info->ttystate);
454 xfree (info);
455 set_inferior_data (inf, inflow_inferior_data, NULL);
456 }
457 }
458
459 void
460 copy_terminal_info (struct inferior *to, struct inferior *from)
461 {
462 struct terminal_info *tinfo_to, *tinfo_from;
463
464 tinfo_to = get_inflow_inferior_data (to);
465 tinfo_from = get_inflow_inferior_data (from);
466
467 xfree (tinfo_to->run_terminal);
468 xfree (tinfo_to->ttystate);
469
470 *tinfo_to = *tinfo_from;
471
472 if (tinfo_from->run_terminal)
473 tinfo_to->run_terminal
474 = xstrdup (tinfo_from->run_terminal);
475
476 if (tinfo_from->ttystate)
477 tinfo_to->ttystate
478 = serial_copy_tty_state (stdin_serial, tinfo_from->ttystate);
479 }
480
481 void
482 info_terminal_command (char *arg, int from_tty)
483 {
484 target_terminal::info (arg, from_tty);
485 }
486
487 void
488 child_terminal_info (struct target_ops *self, const char *args, int from_tty)
489 {
490 struct inferior *inf;
491 struct terminal_info *tinfo;
492
493 if (!gdb_has_a_terminal ())
494 {
495 printf_filtered (_("This GDB does not control a terminal.\n"));
496 return;
497 }
498
499 if (ptid_equal (inferior_ptid, null_ptid))
500 return;
501
502 inf = current_inferior ();
503 tinfo = get_inflow_inferior_data (inf);
504
505 printf_filtered (_("Inferior's terminal status "
506 "(currently saved by GDB):\n"));
507
508 /* First the fcntl flags. */
509 {
510 int flags;
511
512 flags = tinfo->tflags;
513
514 printf_filtered ("File descriptor flags = ");
515
516 #ifndef O_ACCMODE
517 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
518 #endif
519 /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */
520 switch (flags & (O_ACCMODE))
521 {
522 case O_RDONLY:
523 printf_filtered ("O_RDONLY");
524 break;
525 case O_WRONLY:
526 printf_filtered ("O_WRONLY");
527 break;
528 case O_RDWR:
529 printf_filtered ("O_RDWR");
530 break;
531 }
532 flags &= ~(O_ACCMODE);
533
534 #ifdef O_NONBLOCK
535 if (flags & O_NONBLOCK)
536 printf_filtered (" | O_NONBLOCK");
537 flags &= ~O_NONBLOCK;
538 #endif
539
540 #if defined (O_NDELAY)
541 /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
542 print it as O_NONBLOCK, which is good cause that is what POSIX
543 has, and the flag will already be cleared by the time we get here. */
544 if (flags & O_NDELAY)
545 printf_filtered (" | O_NDELAY");
546 flags &= ~O_NDELAY;
547 #endif
548
549 if (flags & O_APPEND)
550 printf_filtered (" | O_APPEND");
551 flags &= ~O_APPEND;
552
553 #if defined (O_BINARY)
554 if (flags & O_BINARY)
555 printf_filtered (" | O_BINARY");
556 flags &= ~O_BINARY;
557 #endif
558
559 if (flags)
560 printf_filtered (" | 0x%x", flags);
561 printf_filtered ("\n");
562 }
563
564 #ifdef HAVE_TERMIOS_H
565 printf_filtered ("Process group = %d\n", (int) tinfo->process_group);
566 #endif
567
568 serial_print_tty_state (stdin_serial, tinfo->ttystate, gdb_stdout);
569 }
570 \f
571 /* NEW_TTY_PREFORK is called before forking a new child process,
572 so we can record the state of ttys in the child to be formed.
573 TTYNAME is null if we are to share the terminal with gdb;
574 or points to a string containing the name of the desired tty.
575
576 NEW_TTY is called in new child processes under Unix, which will
577 become debugger target processes. This actually switches to
578 the terminal specified in the NEW_TTY_PREFORK call. */
579
580 void
581 new_tty_prefork (const char *ttyname)
582 {
583 /* Save the name for later, for determining whether we and the child
584 are sharing a tty. */
585 inferior_thisrun_terminal = ttyname;
586 }
587
588 #if !defined(__GO32__) && !defined(_WIN32)
589 /* If RESULT, assumed to be the return value from a system call, is
590 negative, print the error message indicated by errno and exit.
591 MSG should identify the operation that failed. */
592 static void
593 check_syscall (const char *msg, int result)
594 {
595 if (result < 0)
596 {
597 print_sys_errmsg (msg, errno);
598 _exit (1);
599 }
600 }
601 #endif
602
603 void
604 new_tty (void)
605 {
606 int tty;
607
608 if (inferior_thisrun_terminal == 0)
609 return;
610 #if !defined(__GO32__) && !defined(_WIN32)
611 #ifdef TIOCNOTTY
612 /* Disconnect the child process from our controlling terminal. On some
613 systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
614 ignore SIGTTOU. */
615 tty = open ("/dev/tty", O_RDWR);
616 if (tty > 0)
617 {
618 sighandler_t osigttou;
619
620 osigttou = signal (SIGTTOU, SIG_IGN);
621 ioctl (tty, TIOCNOTTY, 0);
622 close (tty);
623 signal (SIGTTOU, osigttou);
624 }
625 #endif
626
627 /* Now open the specified new terminal. */
628 tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
629 check_syscall (inferior_thisrun_terminal, tty);
630
631 /* Avoid use of dup2; doesn't exist on all systems. */
632 if (tty != 0)
633 {
634 close (0);
635 check_syscall ("dup'ing tty into fd 0", dup (tty));
636 }
637 if (tty != 1)
638 {
639 close (1);
640 check_syscall ("dup'ing tty into fd 1", dup (tty));
641 }
642 if (tty != 2)
643 {
644 close (2);
645 check_syscall ("dup'ing tty into fd 2", dup (tty));
646 }
647
648 #ifdef TIOCSCTTY
649 /* Make tty our new controlling terminal. */
650 if (ioctl (tty, TIOCSCTTY, 0) == -1)
651 /* Mention GDB in warning because it will appear in the inferior's
652 terminal instead of GDB's. */
653 warning (_("GDB: Failed to set controlling terminal: %s"),
654 safe_strerror (errno));
655 #endif
656
657 if (tty > 2)
658 close (tty);
659 #endif /* !go32 && !win32 */
660 }
661
662 /* NEW_TTY_POSTFORK is called after forking a new child process, and
663 adding it to the inferior table, to store the TTYNAME being used by
664 the child, or null if it sharing the terminal with gdb. */
665
666 void
667 new_tty_postfork (void)
668 {
669 /* Save the name for later, for determining whether we and the child
670 are sharing a tty. */
671
672 if (inferior_thisrun_terminal)
673 {
674 struct inferior *inf = current_inferior ();
675 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
676
677 tinfo->run_terminal = xstrdup (inferior_thisrun_terminal);
678 }
679
680 inferior_thisrun_terminal = NULL;
681 }
682
683 \f
684 /* Call set_sigint_trap when you need to pass a signal on to an attached
685 process when handling SIGINT. */
686
687 static void
688 pass_signal (int signo)
689 {
690 #ifndef _WIN32
691 kill (ptid_get_pid (inferior_ptid), SIGINT);
692 #endif
693 }
694
695 static sighandler_t osig;
696 static int osig_set;
697
698 void
699 set_sigint_trap (void)
700 {
701 struct inferior *inf = current_inferior ();
702 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
703
704 if (inf->attach_flag || tinfo->run_terminal)
705 {
706 osig = signal (SIGINT, pass_signal);
707 osig_set = 1;
708 }
709 else
710 osig_set = 0;
711 }
712
713 void
714 clear_sigint_trap (void)
715 {
716 if (osig_set)
717 {
718 signal (SIGINT, osig);
719 osig_set = 0;
720 }
721 }
722 \f
723
724 /* Create a new session if the inferior will run in a different tty.
725 A session is UNIX's way of grouping processes that share a controlling
726 terminal, so a new one is needed if the inferior terminal will be
727 different from GDB's.
728
729 Returns the session id of the new session, 0 if no session was created
730 or -1 if an error occurred. */
731 pid_t
732 create_tty_session (void)
733 {
734 #ifdef HAVE_SETSID
735 pid_t ret;
736
737 if (!job_control || inferior_thisrun_terminal == 0)
738 return 0;
739
740 ret = setsid ();
741 if (ret == -1)
742 warning (_("Failed to create new terminal session: setsid: %s"),
743 safe_strerror (errno));
744
745 return ret;
746 #else
747 return 0;
748 #endif /* HAVE_SETSID */
749 }
750
751 /* Get all the current tty settings (including whether we have a
752 tty at all!). We can't do this in _initialize_inflow because
753 serial_fdopen() won't work until the serial_ops_list is
754 initialized, but we don't want to do it lazily either, so
755 that we can guarantee stdin_serial is opened if there is
756 a terminal. */
757 void
758 initialize_stdin_serial (void)
759 {
760 stdin_serial = serial_fdopen (0);
761 }
762
763 void
764 _initialize_inflow (void)
765 {
766 add_info ("terminal", info_terminal_command,
767 _("Print inferior's saved terminal status."));
768
769 terminal_is_ours = 1;
770
771 /* OK, figure out whether we have job control. */
772 have_job_control ();
773
774 observer_attach_inferior_exit (inflow_inferior_exit);
775
776 inflow_inferior_data
777 = register_inferior_data_with_cleanup (NULL, inflow_inferior_data_cleanup);
778 }