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