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