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