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