GDB (xrefs)
Loading...
Searching...
No Matches
inflow.c
Go to the documentation of this file.
1/* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1986-2023 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 "observable.h"
28#include <signal.h>
29#include <fcntl.h>
30#include "gdbsupport/gdb_select.h"
31
32#include "gdbcmd.h"
33#ifdef HAVE_TERMIOS_H
34#include <termios.h>
35#endif
36#include "gdbsupport/job-control.h"
37#include "gdbsupport/scoped_ignore_sigttou.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
47static void pass_signal (int);
48
50
51/* Record terminal status separately for debugger and inferior. */
52
53static 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 terminal when
58 the inferior is resumed in the foreground. */
60{
61 terminal_info () = default;
63
65
66 /* The name of the tty (from the `tty' command) that we gave to the
67 inferior when it was started. */
68 std::string run_terminal;
69
70 /* TTY state. We save it whenever the inferior stops, and restore
71 it when it resumes in the foreground. */
73
74#ifdef HAVE_TERMIOS_H
75 /* The terminal's foreground process group. Saved whenever the
76 inferior stops. This is the pgrp displayed by "info terminal".
77 Note that this may be not the inferior's actual process group,
78 since each inferior that we spawn has its own process group, and
79 only one can be in the foreground at a time. When the inferior
80 resumes, if we can determine the inferior's actual pgrp, then we
81 make that the foreground pgrp instead of what was saved here.
82 While it's a bit arbitrary which inferior's pgrp ends up in the
83 foreground when we resume several inferiors, this at least makes
84 'resume inf1+inf2' + 'stop all' + 'resume inf2' end up with
85 inf2's pgrp in the foreground instead of inf1's (which would be
86 problematic since it would be left stopped: Ctrl-C wouldn't work,
87 for example). */
88 pid_t process_group = 0;
89#endif
90
91 /* fcntl flags. Saved and restored just like ttystate. */
92 int tflags = 0;
93};
94
95/* Our own tty state, which we restore every time we need to deal with
96 the terminal. This is set once, when GDB first starts, and then
97 whenever we enter/leave TUI mode (gdb_save_tty_state). The
98 settings of flags which readline saves and restores are
99 unimportant. */
101
102/* Snapshot of the initial tty state taken during initialization of
103 GDB, before readline/ncurses have had a chance to change it. This
104 is used as the initial tty state given to each new spawned
105 inferior. Unlike our_terminal_info, this is only ever set
106 once. */
108
109static struct terminal_info *get_inflow_inferior_data (struct inferior *);
110
111/* While the inferior is running, we want SIGINT and SIGQUIT to go to the
112 inferior only. If we have job control, that takes care of it. If not,
113 we save our handlers in these two variables and set SIGINT and SIGQUIT
114 to SIG_IGN. */
115
116static gdb::optional<sighandler_t> sigint_ours;
117#ifdef SIGQUIT
118static gdb::optional<sighandler_t> sigquit_ours;
119#endif
120
121/* The name of the tty (from the `tty' command) that we're giving to
122 the inferior when starting it up. This is only (and should only
123 be) used as a transient global by new_tty_prefork,
124 create_tty_session, new_tty and new_tty_postfork, all called from
125 fork_inferior, while forking a new child. */
126static std::string inferior_thisrun_terminal;
127
128/* Track who owns GDB's terminal (is it GDB or some inferior?). While
129 target_terminal::is_ours() etc. tracks the core's intention and is
130 independent of the target backend, this tracks the actual state of
131 GDB's own tty. So for example,
132
133 (target_terminal::is_inferior () && gdb_tty_state == terminal_is_ours)
134
135 is true when the (native) inferior is not sharing a terminal with
136 GDB (e.g., because we attached to an inferior that is running on a
137 different terminal). */
139
140/* See terminal.h. */
141
142void
144{
145 /* Note we can't do any of this in _initialize_inflow because at
146 that point stdin_serial has not been created yet. */
147
149
150 if (initial_gdb_ttystate != NULL)
151 {
154#ifdef F_GETFL
155 our_terminal_info.tflags = fcntl (0, F_GETFL, 0);
156#endif
157#ifdef HAVE_TERMIOS_H
158 our_terminal_info.process_group = tcgetpgrp (0);
159#endif
160 }
161}
162
163/* Does GDB have a terminal (on stdin)? */
164
165static int
167{
168 return initial_gdb_ttystate != NULL;
169}
170
171/* Macro for printing errors from ioctl operations */
172
173#define OOPSY(what) \
174 if (result == -1) \
175 gdb_printf(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
176 what, safe_strerror (errno))
177
178/* Initialize the terminal settings we record for the inferior,
179 before we actually run the inferior. */
180
181void
183{
184 if (!gdb_has_a_terminal ())
185 return;
186
189
190#ifdef HAVE_TERMIOS_H
191 /* A child we spawn should be a process group leader (PGID==PID) at
192 this point, though that may not be true if we're attaching to an
193 existing process. */
194 tinfo->process_group = inf->pid;
195#endif
196
197 xfree (tinfo->ttystate);
199}
200
201/* Save the terminal settings again. This is necessary for the TUI
202 when it switches to TUI or non-TUI mode; curses changes the terminal
203 and gdb must be able to restore it correctly. */
204
205void
214
215/* See inferior.h. */
216
217tribool
218is_gdb_terminal (const char *tty)
219{
220 struct stat gdb_tty;
221 struct stat other_tty;
222 int res;
223
224 res = stat (tty, &other_tty);
225 if (res == -1)
226 return TRIBOOL_UNKNOWN;
227
228 res = fstat (STDIN_FILENO, &gdb_tty);
229 if (res == -1)
230 return TRIBOOL_UNKNOWN;
231
232 return ((gdb_tty.st_dev == other_tty.st_dev
233 && gdb_tty.st_ino == other_tty.st_ino)
234 ? TRIBOOL_TRUE
235 : TRIBOOL_FALSE);
236}
237
238/* Return true if the inferior is using the same TTY for input as GDB
239 is. If this is true, then we save/restore terminal flags/state.
240
241 This is necessary because if inf->attach_flag is set, we don't
242 offhand know whether we are sharing a terminal with the inferior or
243 not. Attaching a process without a terminal is one case where we
244 do not; attaching a process which we ran from the same shell as GDB
245 via `&' is one case where we do.
246
247 If we can't determine, we assume the TTY is being shared. This
248 works OK if you're only debugging one inferior. However, if you're
249 debugging more than one inferior, and e.g., one is spawned by GDB
250 with "run" (sharing terminal with GDB), and another is attached to
251 (and running on a different terminal, as is most common), then it
252 matters, because we can only restore the terminal settings of one
253 of the inferiors, and in that scenario, we want to restore the
254 settings of the "run"'ed inferior.
255
256 Note, this is not the same as determining whether GDB and the
257 inferior are in the same session / connected to the same
258 controlling tty. An inferior (fork child) may call setsid,
259 disconnecting itself from the ctty, while still leaving
260 stdin/stdout/stderr associated with the original terminal. If
261 we're debugging that process, we should also save/restore terminal
262 settings. */
263
264static bool
266{
268
269 tribool res = sharing_input_terminal (inf->pid);
270
271 if (res == TRIBOOL_UNKNOWN)
272 {
273 /* As fallback, if we can't determine by stat'ing the inferior's
274 tty directly (because it's not supported on this host) and
275 the child was spawned, check whether run_terminal is our tty.
276 This isn't ideal, since this is checking the child's
277 controlling terminal, not the input terminal (which may have
278 been redirected), but is still better than nothing. A false
279 positive ("set inferior-tty" points to our terminal, but I/O
280 was redirected) is much more likely than a false negative
281 ("set inferior-tty" points to some other terminal, and then
282 output was redirected to our terminal), and with a false
283 positive we just end up trying to save/restore terminal
284 settings when we didn't need to or we actually can't. */
285 if (!tinfo->run_terminal.empty ())
286 res = is_gdb_terminal (tinfo->run_terminal.c_str ());
287
288 /* If we still can't determine, assume yes. */
289 if (res == TRIBOOL_UNKNOWN)
290 return true;
291 }
292
293 return res == TRIBOOL_TRUE;
294}
295
296/* Put the inferior's terminal settings into effect. This is
297 preparation for starting or resuming the inferior. */
298
299void
301{
302 /* If we resume more than one inferior in the foreground on GDB's
303 terminal, then the first inferior's terminal settings "win".
304 Note that every child process is put in its own process group, so
305 the first process that ends up resumed ends up determining which
306 process group the kernel forwards Ctrl-C/Ctrl-Z (SIGINT/SIGTTOU)
307 to. */
309 return;
310
313
314 if (gdb_has_a_terminal ()
315 && tinfo->ttystate != NULL
317 {
318 int result;
319
320 /* Ignore SIGTTOU since it will happen when we try to set the
321 terminal's state (if gdb_tty_state is currently
322 ours_for_output). */
323 scoped_ignore_sigttou ignore_sigttou;
324
325#ifdef F_GETFL
326 result = fcntl (0, F_SETFL, tinfo->tflags);
327 OOPSY ("fcntl F_SETFL");
328#endif
329
330 result = serial_set_tty_state (stdin_serial, tinfo->ttystate);
331 OOPSY ("setting tty state");
332
333 if (!job_control)
334 {
336#ifdef SIGQUIT
337 sigquit_ours = signal (SIGQUIT, SIG_IGN);
338#endif
339 }
340
341 if (job_control)
342 {
343#ifdef HAVE_TERMIOS_H
344 /* If we can't tell the inferior's actual process group,
345 then restore whatever was the foreground pgrp the last
346 time the inferior was running. See also comments
347 describing terminal_state::process_group. */
348#ifdef HAVE_GETPGID
349 result = tcsetpgrp (0, getpgid (inf->pid));
350#else
351 result = tcsetpgrp (0, tinfo->process_group);
352#endif
353 if (result == -1)
354 {
355#if 0
356 /* This fails if either GDB has no controlling terminal,
357 e.g., running under 'setsid(1)', or if the inferior
358 is not attached to GDB's controlling terminal. E.g.,
359 if it called setsid to create a new session or used
360 the TIOCNOTTY ioctl, or simply if we've attached to a
361 process running on another terminal and we couldn't
362 tell whether it was sharing GDB's terminal (and so
363 assumed yes). */
365 (gdb_stderr,
366 "[tcsetpgrp failed in child_terminal_inferior: %s]\n",
367 safe_strerror (errno));
368#endif
369 }
370#endif
371 }
372
374 }
375}
376
377/* Put some of our terminal settings into effect,
378 enough to get proper results from our output,
379 but do not change into or out of RAW mode
380 so that no input is discarded.
381
382 After doing this, either terminal_ours or terminal_inferior
383 should be called to get back to a normal state of affairs.
384
385 N.B. The implementation is (currently) no different than
386 child_terminal_ours. See child_terminal_ours_1. */
387
388void
393
394/* Put our terminal settings into effect.
395 First record the inferior's terminal settings
396 so they can be restored properly later.
397
398 N.B. Targets that want to use this with async support must build that
399 support on top of this (e.g., the caller still needs to add stdin to the
400 event loop). E.g., see linux_nat_terminal_ours. */
401
402void
407
408/* Save the current terminal settings in the inferior's terminal_info
409 cache. */
410
411void
413{
414 /* Avoid attempting all the ioctl's when running in batch. */
415 if (!gdb_has_a_terminal ())
416 return;
417
420
421 /* No need to save/restore if the inferior is not sharing GDB's
422 tty. */
424 return;
425
426 xfree (tinfo->ttystate);
428
429#ifdef HAVE_TERMIOS_H
430 tinfo->process_group = tcgetpgrp (0);
431#endif
432
433#ifdef F_GETFL
434 tinfo->tflags = fcntl (0, F_GETFL, 0);
435#endif
436}
437
438/* Switch terminal state to DESIRED_STATE, either is_ours, or
439 is_ours_for_output. */
440
441static void
443{
444 gdb_assert (desired_state != target_terminal_state::is_inferior);
445
446 /* Avoid attempting all the ioctl's when running in batch. */
447 if (!gdb_has_a_terminal ())
448 return;
449
450 if (gdb_tty_state != desired_state)
451 {
452 int result ATTRIBUTE_UNUSED;
453
454 /* Ignore SIGTTOU since it will happen when we try to set the
455 terminal's pgrp. */
456 scoped_ignore_sigttou ignore_sigttou;
457
458 /* Set tty state to our_ttystate. */
460
461 /* If we only want output, then leave the inferior's pgrp in the
462 foreground, so that Ctrl-C/Ctrl-Z reach the inferior
463 directly. */
464 if (job_control && desired_state == target_terminal_state::is_ours)
465 {
466#ifdef HAVE_TERMIOS_H
467 result = tcsetpgrp (0, our_terminal_info.process_group);
468#if 0
469 /* This fails on Ultrix with EINVAL if you run the testsuite
470 in the background with nohup, and then log out. GDB never
471 used to check for an error here, so perhaps there are other
472 such situations as well. */
473 if (result == -1)
475 "[tcsetpgrp failed in child_terminal_ours: %s]\n",
476 safe_strerror (errno));
477#endif
478#endif /* termios */
479 }
480
481 if (!job_control && desired_state == target_terminal_state::is_ours)
482 {
483 if (sigint_ours.has_value ())
485 sigint_ours.reset ();
486#ifdef SIGQUIT
487 if (sigquit_ours.has_value ())
488 signal (SIGQUIT, *sigquit_ours);
489 sigquit_ours.reset ();
490#endif
491 }
492
493#ifdef F_GETFL
494 result = fcntl (0, F_SETFL, our_terminal_info.tflags);
495#endif
496
497 gdb_tty_state = desired_state;
498 }
499}
500
501/* Interrupt the inferior. Implementation of target_interrupt for
502 child/native targets. */
503
504void
506{
507 /* Interrupt the first inferior that has a resumed thread. */
508 thread_info *resumed = NULL;
509 for (thread_info *thr : all_non_exited_threads ())
510 {
511 if (thr->executing ())
512 {
513 resumed = thr;
514 break;
515 }
516 if (thr->has_pending_waitstatus ())
517 resumed = thr;
518 }
519
520 if (resumed != NULL)
521 {
522 /* Note that unlike pressing Ctrl-C on the controlling terminal,
523 here we only interrupt one process, not the whole process
524 group. This is because interrupting a process group (with
525 either Ctrl-C or with kill(3) with negative PID) sends a
526 SIGINT to each process in the process group, and we may not
527 be debugging all processes in the process group. */
528#ifndef _WIN32
529 kill (resumed->inf->pid, SIGINT);
530#endif
531 }
532}
533
534/* Pass a Ctrl-C to the inferior as-if a Ctrl-C was pressed while the
535 inferior was in the foreground. Implementation of
536 target_pass_ctrlc for child/native targets. */
537
538void
540{
541 gdb_assert (!target_terminal::is_ours ());
542
543#ifdef HAVE_TERMIOS_H
544 if (job_control)
545 {
546 pid_t term_pgrp = tcgetpgrp (0);
547
548 /* If there's any inferior sharing our terminal, pass the SIGINT
549 to the terminal's foreground process group. This acts just
550 like the user typed a ^C on the terminal while the inferior
551 was in the foreground. Note that using a negative process
552 number in kill() is a System V-ism. The proper BSD interface
553 is killpg(). However, all modern BSDs support the System V
554 interface too. */
555
556 if (term_pgrp != -1 && term_pgrp != our_terminal_info.process_group)
557 {
558 kill (-term_pgrp, SIGINT);
559 return;
560 }
561 }
562#endif
563
564 /* Otherwise, pass the Ctrl-C to the first inferior that was resumed
565 in the foreground. */
566 for (inferior *inf : all_inferiors ())
567 {
568 if (inf->terminal_state != target_terminal_state::is_ours)
569 {
570 gdb_assert (inf->pid != 0);
571
572#ifndef _WIN32
573 kill (inf->pid, SIGINT);
574#endif
575 return;
576 }
577 }
578
579 /* If no inferior was resumed in the foreground, then how did the
580 !is_ours assert above pass? */
581 gdb_assert_not_reached ("no inferior resumed in the fg found");
582}
583
584/* Per-inferior data key. */
586
591
592/* Get the current svr4 data. If none is found yet, add it now. This
593 function always returns a valid object. */
594
595static struct terminal_info *
597{
598 struct terminal_info *info;
599
601 if (info == NULL)
602 info = inflow_inferior_data.emplace (inf);
603
604 return info;
605}
606
607/* This is a "inferior_exit" observer. Releases the TERMINAL_INFO member
608 of the inferior structure. This field is private to inflow.c, and
609 its type is opaque to the rest of GDB. PID is the target pid of
610 the inferior that is about to be removed from the inferior
611 list. */
612
613static void
615{
616 inf->terminal_state = target_terminal_state::is_ours;
618}
619
620void
621copy_terminal_info (struct inferior *to, struct inferior *from)
622{
623 struct terminal_info *tinfo_to, *tinfo_from;
624
625 tinfo_to = get_inflow_inferior_data (to);
626 tinfo_from = get_inflow_inferior_data (from);
627
628 xfree (tinfo_to->ttystate);
629
630 *tinfo_to = *tinfo_from;
631
632 if (tinfo_from->ttystate)
633 tinfo_to->ttystate
635
636 to->terminal_state = from->terminal_state;
637}
638
639/* See terminal.h. */
640
641void
643{
646
647 inflow_inferior_data.set (a, info_b);
648 inflow_inferior_data.set (b, info_a);
649
650 std::swap (a->terminal_state, b->terminal_state);
651}
652
653static void
654info_terminal_command (const char *arg, int from_tty)
655{
656 target_terminal::info (arg, from_tty);
657}
658
659void
660child_terminal_info (struct target_ops *self, const char *args, int from_tty)
661{
662 struct inferior *inf;
663 struct terminal_info *tinfo;
664
665 if (!gdb_has_a_terminal ())
666 {
667 gdb_printf (_("This GDB does not control a terminal.\n"));
668 return;
669 }
670
671 if (inferior_ptid == null_ptid)
672 return;
673
676
677 gdb_printf (_("Inferior's terminal status "
678 "(currently saved by GDB):\n"));
679
680 /* First the fcntl flags. */
681 {
682 int flags;
683
684 flags = tinfo->tflags;
685
686 gdb_printf ("File descriptor flags = ");
687
688#ifndef O_ACCMODE
689#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
690#endif
691 /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */
692 switch (flags & (O_ACCMODE))
693 {
694 case O_RDONLY:
695 gdb_printf ("O_RDONLY");
696 break;
697 case O_WRONLY:
698 gdb_printf ("O_WRONLY");
699 break;
700 case O_RDWR:
701 gdb_printf ("O_RDWR");
702 break;
703 }
704 flags &= ~(O_ACCMODE);
705
706#ifdef O_NONBLOCK
707 if (flags & O_NONBLOCK)
708 gdb_printf (" | O_NONBLOCK");
709 flags &= ~O_NONBLOCK;
710#endif
711
712#if defined (O_NDELAY)
713 /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
714 print it as O_NONBLOCK, which is good cause that is what POSIX
715 has, and the flag will already be cleared by the time we get here. */
716 if (flags & O_NDELAY)
717 gdb_printf (" | O_NDELAY");
718 flags &= ~O_NDELAY;
719#endif
720
721 if (flags & O_APPEND)
722 gdb_printf (" | O_APPEND");
723 flags &= ~O_APPEND;
724
725#if defined (O_BINARY)
726 if (flags & O_BINARY)
727 gdb_printf (" | O_BINARY");
728 flags &= ~O_BINARY;
729#endif
730
731 if (flags)
732 gdb_printf (" | 0x%x", flags);
733 gdb_printf ("\n");
734 }
735
736#ifdef HAVE_TERMIOS_H
737 gdb_printf ("Process group = %d\n", (int) tinfo->process_group);
738#endif
739
741}
742
743/* NEW_TTY_PREFORK is called before forking a new child process,
744 so we can record the state of ttys in the child to be formed.
745 TTYNAME is empty if we are to share the terminal with gdb;
746 otherwise it contains the name of the desired tty.
747
748 NEW_TTY is called in new child processes under Unix, which will
749 become debugger target processes. This actually switches to
750 the terminal specified in the NEW_TTY_PREFORK call. */
751
752void
753new_tty_prefork (std::string ttyname)
754{
755 /* Save the name for later, for determining whether we and the child
756 are sharing a tty. */
757 inferior_thisrun_terminal = std::move (ttyname);
758}
759
760#if !defined(__GO32__) && !defined(_WIN32)
761/* If RESULT, assumed to be the return value from a system call, is
762 negative, print the error message indicated by errno and exit.
763 MSG should identify the operation that failed. */
764static void
765check_syscall (const char *msg, int result)
766{
767 if (result < 0)
768 {
769 gdb_printf (gdb_stderr, "%s:%s.\n", msg,
770 safe_strerror (errno));
771 _exit (1);
772 }
773}
774#endif
775
776void
778{
779 if (inferior_thisrun_terminal.empty ())
780 return;
781#if !defined(__GO32__) && !defined(_WIN32)
782 int tty;
783
784#ifdef TIOCNOTTY
785 /* Disconnect the child process from our controlling terminal. On some
786 systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
787 ignore SIGTTOU. */
788 tty = open ("/dev/tty", O_RDWR);
789 if (tty >= 0)
790 {
791 scoped_ignore_sigttou ignore_sigttou;
792
793 ioctl (tty, TIOCNOTTY, 0);
794 close (tty);
795 }
796#endif
797
798 /* Now open the specified new terminal. */
799 tty = open (inferior_thisrun_terminal.c_str (), O_RDWR | O_NOCTTY);
801
802 /* Avoid use of dup2; doesn't exist on all systems. */
803 if (tty != 0)
804 {
805 close (0);
806 check_syscall ("dup'ing tty into fd 0", dup (tty));
807 }
808 if (tty != 1)
809 {
810 close (1);
811 check_syscall ("dup'ing tty into fd 1", dup (tty));
812 }
813 if (tty != 2)
814 {
815 close (2);
816 check_syscall ("dup'ing tty into fd 2", dup (tty));
817 }
818
819#ifdef TIOCSCTTY
820 /* Make tty our new controlling terminal. */
821 if (ioctl (tty, TIOCSCTTY, 0) == -1)
822 /* Mention GDB in warning because it will appear in the inferior's
823 terminal instead of GDB's. */
824 warning (_("GDB: Failed to set controlling terminal: %s"),
825 safe_strerror (errno));
826#endif
827
828 if (tty > 2)
829 close (tty);
830#endif /* !go32 && !win32 */
831}
832
833/* NEW_TTY_POSTFORK is called after forking a new child process, and
834 adding it to the inferior table, to store the TTYNAME being used by
835 the child, or empty if it sharing the terminal with gdb. */
836
837void
839{
840 /* Save the name for later, for determining whether we and the child
841 are sharing a tty. */
842
843 struct inferior *inf = current_inferior ();
845
846 tinfo->run_terminal = std::move (inferior_thisrun_terminal);
848}
849
850
851/* Call set_sigint_trap when you need to pass a signal on to an attached
852 process when handling SIGINT. */
853
854static void
855pass_signal (int signo)
856{
857#ifndef _WIN32
858 kill (inferior_ptid.pid (), SIGINT);
859#endif
860}
861
862static sighandler_t osig;
863static int osig_set;
864
865void
867{
868 struct inferior *inf = current_inferior ();
870
871 if (inf->attach_flag || !tinfo->run_terminal.empty ())
872 {
874 osig_set = 1;
875 }
876 else
877 osig_set = 0;
878}
879
880void
882{
883 if (osig_set)
884 {
886 osig_set = 0;
887 }
888}
889
890
891/* Create a new session if the inferior will run in a different tty.
892 A session is UNIX's way of grouping processes that share a controlling
893 terminal, so a new one is needed if the inferior terminal will be
894 different from GDB's.
895
896 Returns the session id of the new session, 0 if no session was created
897 or -1 if an error occurred. */
898pid_t
900{
901#ifdef HAVE_SETSID
902 pid_t ret;
903
904 if (!job_control || inferior_thisrun_terminal.empty ())
905 return 0;
906
907 ret = setsid ();
908 if (ret == -1)
909 warning (_("Failed to create new terminal session: setsid: %s"),
910 safe_strerror (errno));
911
912 return ret;
913#else
914 return 0;
915#endif /* HAVE_SETSID */
916}
917
918/* Get all the current tty settings (including whether we have a
919 tty at all!). We can't do this in _initialize_inflow because
920 serial_fdopen() won't work until the serial_ops_list is
921 initialized, but we don't want to do it lazily either, so
922 that we can guarantee stdin_serial is opened if there is
923 a terminal. */
924void
929
930void _initialize_inflow ();
931void
933{
934 add_info ("terminal", info_terminal_command,
935 _("Print inferior's saved terminal status."));
936
937 /* OK, figure out whether we have job control. */
938 have_job_control ();
939
941}
void xfree(void *)
int pid
Definition inferior.h:561
target_terminal_state terminal_state
Definition inferior.h:586
void set(unsigned key, void *datum)
Definition registry.h:204
void * get(unsigned key)
Definition registry.h:211
static void info(const char *arg, int from_tty)
Definition target.c:1106
static bool is_ours()
Definition target.h:189
struct inferior * inf
Definition gdbthread.h:301
struct cmd_list_element * add_info(const char *name, cmd_simple_func_ftype *fun, const char *doc)
#define O_BINARY
Definition defs.h:114
all_non_exited_threads_range all_non_exited_threads(process_stratum_target *proc_target=nullptr, ptid_t filter_ptid=minus_one_ptid)
Definition gdbthread.h:753
mach_port_t kern_return_t mach_port_t mach_msg_type_name_t msgportsPoly mach_port_t kern_return_t pid_t pid mach_port_t kern_return_t mach_port_t task mach_port_t kern_return_t int flags
Definition gnu-nat.c:1861
pid_t tcgetpgrp(int fd)
Definition go32-nat.c:2129
int tcsetpgrp(int fd, pid_t pgid)
Definition go32-nat.c:2138
ptid_t inferior_ptid
Definition infcmd.c:74
struct inferior * current_inferior(void)
Definition inferior.c:55
all_inferiors_range all_inferiors(process_stratum_target *proc_target=nullptr)
Definition inferior.h:821
c_c_handler_ftype * install_sigint_handler(c_c_handler_ftype *fn)
Definition mingw-hdep.c:425
void new_tty_postfork(void)
Definition inflow.c:838
static struct terminal_info our_terminal_info
Definition inflow.c:100
void child_terminal_info(struct target_ops *self, const char *args, int from_tty)
Definition inflow.c:660
static void check_syscall(const char *msg, int result)
Definition inflow.c:765
static std::string inferior_thisrun_terminal
Definition inflow.c:126
#define O_NOCTTY
Definition inflow.c:44
void initialize_stdin_serial(void)
Definition inflow.c:925
void copy_terminal_info(struct inferior *to, struct inferior *from)
Definition inflow.c:621
static gdb::optional< sighandler_t > sigint_ours
Definition inflow.c:116
tribool is_gdb_terminal(const char *tty)
Definition inflow.c:218
void set_sigint_trap(void)
Definition inflow.c:866
void child_terminal_ours(struct target_ops *self)
Definition inflow.c:403
static void info_terminal_command(const char *arg, int from_tty)
Definition inflow.c:654
#define O_ACCMODE
static bool sharing_input_terminal(inferior *inf)
Definition inflow.c:265
void child_terminal_init(struct target_ops *self)
Definition inflow.c:182
void new_tty_prefork(std::string ttyname)
Definition inflow.c:753
static serial_ttystate initial_gdb_ttystate
Definition inflow.c:107
static sighandler_t osig
Definition inflow.c:862
void child_pass_ctrlc(struct target_ops *self)
Definition inflow.c:539
void gdb_save_tty_state(void)
Definition inflow.c:206
void child_terminal_ours_for_output(struct target_ops *self)
Definition inflow.c:389
void child_interrupt(struct target_ops *self)
Definition inflow.c:505
static void child_terminal_ours_1(target_terminal_state)
Definition inflow.c:442
void child_terminal_inferior(struct target_ops *self)
Definition inflow.c:300
void new_tty(void)
Definition inflow.c:777
static void pass_signal(int)
Definition inflow.c:855
static struct terminal_info * get_inflow_inferior_data(struct inferior *)
Definition inflow.c:596
void _initialize_inflow()
Definition inflow.c:932
static const registry< inferior >::key< terminal_info > inflow_inferior_data
Definition inflow.c:585
pid_t create_tty_session(void)
Definition inflow.c:899
void child_terminal_save_inferior(struct target_ops *self)
Definition inflow.c:412
void set_initial_gdb_ttystate(void)
Definition inflow.c:143
void clear_sigint_trap(void)
Definition inflow.c:881
static void inflow_inferior_exit(struct inferior *inf)
Definition inflow.c:614
static int gdb_has_a_terminal(void)
Definition inflow.c:166
static int osig_set
Definition inflow.c:863
static target_terminal_state gdb_tty_state
Definition inflow.c:138
#define OOPSY(what)
Definition inflow.c:173
void swap_terminal_info(inferior *a, inferior *b)
Definition inflow.c:642
static struct serial * stdin_serial
Definition inflow.c:53
observable< struct inferior * > inferior_exit
void serial_print_tty_state(struct serial *scb, serial_ttystate ttystate, struct ui_file *stream)
Definition serial.c:508
int serial_set_tty_state(struct serial *scb, serial_ttystate ttystate)
Definition serial.c:502
serial_ttystate serial_copy_tty_state(struct serial *scb, serial_ttystate ttystate)
Definition serial.c:496
serial_ttystate serial_get_tty_state(struct serial *scb)
Definition serial.c:490
struct serial * serial_fdopen(const int fd)
Definition serial.c:303
void * serial_ttystate
Definition serial.h:35
Definition gnu-nat.c:153
pid_t pid
Definition gnu-nat.c:165
terminal_info()=default
terminal_info & operator=(const terminal_info &)=default
serial_ttystate ttystate
Definition inflow.c:72
std::string run_terminal
Definition inflow.c:68
target_terminal_state
Definition target.h:134
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
#define gdb_stderr
Definition utils.h:187
#define gdb_stdout
Definition utils.h:182