Build gmp before configuring gcc
[deliverable/binutils-gdb.git] / gdb / inflow.c
CommitLineData
c906108c 1/* Low level interface to ptrace, for GDB when running under Unix.
28e7fd62 2 Copyright (C) 1986-2013 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
SS
28
29#include "gdb_string.h"
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
a14ed312 49static void 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
a14ed312 212static void terminal_ours_1 (int);
c906108c
SS
213
214/* Initialize the terminal settings we record for the inferior,
215 before we actually run the inferior. */
216
217void
fba45db2 218terminal_init_inferior_with_pgrp (int pgrp)
c906108c
SS
219{
220 if (gdb_has_a_terminal ())
221 {
7e1789f5 222 struct inferior *inf = current_inferior ();
6c95b8df 223 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
7e1789f5 224
6c95b8df 225 xfree (tinfo->ttystate);
1e182ce8
UW
226 tinfo->ttystate = serial_copy_tty_state (stdin_serial,
227 our_terminal_info.ttystate);
c906108c
SS
228
229#ifdef PROCESS_GROUP_TYPE
6c95b8df 230 tinfo->process_group = pgrp;
c906108c
SS
231#endif
232
233 /* Make sure that next time we call terminal_inferior (which will be
c5aa993b
JM
234 before the program runs, as it needs to be), we install the new
235 process group. */
c906108c
SS
236 terminal_is_ours = 1;
237 }
238}
239
a790ad35
SC
240/* Save the terminal settings again. This is necessary for the TUI
241 when it switches to TUI or non-TUI mode; curses changes the terminal
242 and gdb must be able to restore it correctly. */
243
244void
7bda5e4a 245terminal_save_ours (void)
a790ad35
SC
246{
247 if (gdb_has_a_terminal ())
248 {
7e1789f5
PA
249 xfree (our_terminal_info.ttystate);
250 our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);
a790ad35
SC
251 }
252}
253
c906108c 254void
fba45db2 255terminal_init_inferior (void)
c906108c
SS
256{
257#ifdef PROCESS_GROUP_TYPE
258 /* This is for Lynx, and should be cleaned up by having Lynx be a separate
259 debugging target with a version of target_terminal_init_inferior which
260 passes in the process group to a generic routine which does all the work
261 (and the non-threaded child_terminal_init_inferior can just pass in
39f77062 262 inferior_ptid to the same routine). */
c906108c 263 /* We assume INFERIOR_PID is also the child's process group. */
39f77062 264 terminal_init_inferior_with_pgrp (PIDGET (inferior_ptid));
c906108c
SS
265#endif /* PROCESS_GROUP_TYPE */
266}
267
268/* Put the inferior's terminal settings into effect.
269 This is preparation for starting or resuming the inferior. */
270
271void
fba45db2 272terminal_inferior (void)
c906108c 273{
7e1789f5 274 struct inferior *inf;
6c95b8df 275 struct terminal_info *tinfo;
7e1789f5
PA
276
277 if (!terminal_is_ours)
278 return;
279
280 inf = current_inferior ();
6c95b8df 281 tinfo = get_inflow_inferior_data (inf);
7e1789f5
PA
282
283 if (gdb_has_a_terminal ()
6c95b8df
PA
284 && tinfo->ttystate != NULL
285 && tinfo->run_terminal == NULL)
c906108c
SS
286 {
287 int result;
288
289#ifdef F_GETFL
290 /* Is there a reason this is being done twice? It happens both
c5aa993b
JM
291 places we use F_SETFL, so I'm inclined to think perhaps there
292 is some reason, however perverse. Perhaps not though... */
6c95b8df
PA
293 result = fcntl (0, F_SETFL, tinfo->tflags);
294 result = fcntl (0, F_SETFL, tinfo->tflags);
c906108c
SS
295 OOPSY ("fcntl F_SETFL");
296#endif
297
298 /* Because we were careful to not change in or out of raw mode in
c5aa993b
JM
299 terminal_ours, we will not change in our out of raw mode with
300 this call, so we don't flush any input. */
7e1789f5 301 result = serial_set_tty_state (stdin_serial,
6c95b8df 302 tinfo->ttystate);
c906108c
SS
303 OOPSY ("setting tty state");
304
305 if (!job_control)
306 {
c5aa993b 307 sigint_ours = (void (*)()) signal (SIGINT, SIG_IGN);
c906108c 308#ifdef SIGQUIT
c5aa993b 309 sigquit_ours = (void (*)()) signal (SIGQUIT, SIG_IGN);
c906108c
SS
310#endif
311 }
312
313 /* If attach_flag is set, we don't know whether we are sharing a
c5aa993b
JM
314 terminal with the inferior or not. (attaching a process
315 without a terminal is one case where we do not; attaching a
316 process which we ran from the same shell as GDB via `&' is
317 one case where we do, I think (but perhaps this is not
318 `sharing' in the sense that we need to save and restore tty
319 state)). I don't know if there is any way to tell whether we
320 are sharing a terminal. So what we do is to go through all
321 the saving and restoring of the tty state, but ignore errors
322 setting the process group, which will happen if we are not
323 sharing a terminal). */
c906108c
SS
324
325 if (job_control)
326 {
327#ifdef HAVE_TERMIOS
6c95b8df 328 result = tcsetpgrp (0, tinfo->process_group);
181e7f93 329 if (!inf->attach_flag)
c906108c
SS
330 OOPSY ("tcsetpgrp");
331#endif
332
333#ifdef HAVE_SGTTY
6c95b8df 334 result = ioctl (0, TIOCSPGRP, &tinfo->process_group);
181e7f93 335 if (!inf->attach_flag)
c906108c
SS
336 OOPSY ("TIOCSPGRP");
337#endif
338 }
339
340 }
341 terminal_is_ours = 0;
342}
343
344/* Put some of our terminal settings into effect,
345 enough to get proper results from our output,
346 but do not change into or out of RAW mode
347 so that no input is discarded.
348
349 After doing this, either terminal_ours or terminal_inferior
350 should be called to get back to a normal state of affairs. */
351
352void
fba45db2 353terminal_ours_for_output (void)
c906108c
SS
354{
355 terminal_ours_1 (1);
356}
357
358/* Put our terminal settings into effect.
359 First record the inferior's terminal settings
360 so they can be restored properly later. */
361
362void
fba45db2 363terminal_ours (void)
c906108c
SS
364{
365 terminal_ours_1 (0);
366}
367
368/* output_only is not used, and should not be used unless we introduce
369 separate terminal_is_ours and terminal_is_ours_for_output
370 flags. */
371
372static void
fba45db2 373terminal_ours_1 (int output_only)
c906108c 374{
7e1789f5 375 struct inferior *inf;
6c95b8df 376 struct terminal_info *tinfo;
7e1789f5
PA
377
378 if (terminal_is_ours)
379 return;
380
d9d2d8b6
PA
381 terminal_is_ours = 1;
382
7e1789f5 383 /* Checking inferior->run_terminal is necessary so that
c906108c
SS
384 if GDB is running in the background, it won't block trying
385 to do the ioctl()'s below. Checking gdb_has_a_terminal
386 avoids attempting all the ioctl's when running in batch. */
7e1789f5
PA
387
388 inf = current_inferior ();
6c95b8df 389 tinfo = get_inflow_inferior_data (inf);
7e1789f5 390
6c95b8df 391 if (tinfo->run_terminal != NULL || gdb_has_a_terminal () == 0)
c906108c
SS
392 return;
393
c906108c 394 {
4b69c284 395#ifdef SIGTTOU
c906108c 396 /* Ignore this signal since it will happen when we try to set the
c5aa993b 397 pgrp. */
4b69c284
AC
398 void (*osigttou) () = NULL;
399#endif
c906108c
SS
400 int result;
401
c906108c
SS
402#ifdef SIGTTOU
403 if (job_control)
c5aa993b 404 osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN);
c906108c
SS
405#endif
406
6c95b8df
PA
407 xfree (tinfo->ttystate);
408 tinfo->ttystate = serial_get_tty_state (stdin_serial);
64a0ac84 409
49a834f9 410#ifdef PROCESS_GROUP_TYPE
181e7f93 411 if (!inf->attach_flag)
64a0ac84
PA
412 /* If setpgrp failed in terminal_inferior, this would give us
413 our process group instead of the inferior's. See
414 terminal_inferior for details. */
6c95b8df 415 tinfo->process_group = gdb_getpgrp ();
49a834f9 416#endif
c906108c
SS
417
418 /* Here we used to set ICANON in our ttystate, but I believe this
c5aa993b
JM
419 was an artifact from before when we used readline. Readline sets
420 the tty state when it needs to.
421 FIXME-maybe: However, query() expects non-raw mode and doesn't
422 use readline. Maybe query should use readline (on the other hand,
423 this only matters for HAVE_SGTTY, not termio or termios, I think). */
c906108c
SS
424
425 /* Set tty state to our_ttystate. We don't change in our out of raw
c5aa993b
JM
426 mode, to avoid flushing input. We need to do the same thing
427 regardless of output_only, because we don't have separate
428 terminal_is_ours and terminal_is_ours_for_output flags. It's OK,
1777feb0
MS
429 though, since readline will deal with raw mode when/if it needs
430 to. */
c906108c 431
7e1789f5 432 serial_noflush_set_tty_state (stdin_serial, our_terminal_info.ttystate,
6c95b8df 433 tinfo->ttystate);
c906108c
SS
434
435 if (job_control)
436 {
437#ifdef HAVE_TERMIOS
7e1789f5 438 result = tcsetpgrp (0, our_terminal_info.process_group);
c906108c
SS
439#if 0
440 /* This fails on Ultrix with EINVAL if you run the testsuite
441 in the background with nohup, and then log out. GDB never
442 used to check for an error here, so perhaps there are other
443 such situations as well. */
444 if (result == -1)
3e43a32a
MS
445 fprintf_unfiltered (gdb_stderr,
446 "[tcsetpgrp failed in terminal_ours: %s]\n",
dc672865 447 safe_strerror (errno));
c906108c
SS
448#endif
449#endif /* termios */
450
451#ifdef HAVE_SGTTY
7e1789f5 452 result = ioctl (0, TIOCSPGRP, &our_terminal_info.process_group);
c906108c
SS
453#endif
454 }
455
456#ifdef SIGTTOU
457 if (job_control)
458 signal (SIGTTOU, osigttou);
459#endif
460
461 if (!job_control)
462 {
463 signal (SIGINT, sigint_ours);
464#ifdef SIGQUIT
465 signal (SIGQUIT, sigquit_ours);
466#endif
467 }
468
469#ifdef F_GETFL
6c95b8df 470 tinfo->tflags = fcntl (0, F_GETFL, 0);
c906108c
SS
471
472 /* Is there a reason this is being done twice? It happens both
c5aa993b
JM
473 places we use F_SETFL, so I'm inclined to think perhaps there
474 is some reason, however perverse. Perhaps not though... */
7e1789f5
PA
475 result = fcntl (0, F_SETFL, our_terminal_info.tflags);
476 result = fcntl (0, F_SETFL, our_terminal_info.tflags);
c906108c 477#endif
c906108c
SS
478 }
479}
480
6c95b8df
PA
481/* Per-inferior data key. */
482static const struct inferior_data *inflow_inferior_data;
7e1789f5
PA
483
484static void
6c95b8df 485inflow_inferior_data_cleanup (struct inferior *inf, void *arg)
7e1789f5 486{
6c95b8df
PA
487 struct terminal_info *info;
488
489 info = inferior_data (inf, inflow_inferior_data);
490 if (info != NULL)
491 {
492 xfree (info->run_terminal);
1e182ce8 493 xfree (info->ttystate);
6c95b8df
PA
494 xfree (info);
495 }
496}
497
498/* Get the current svr4 data. If none is found yet, add it now. This
499 function always returns a valid object. */
500
501static struct terminal_info *
502get_inflow_inferior_data (struct inferior *inf)
503{
504 struct terminal_info *info;
505
506 info = inferior_data (inf, inflow_inferior_data);
507 if (info == NULL)
508 {
509 info = XZALLOC (struct terminal_info);
510 set_inferior_data (inf, inflow_inferior_data, info);
511 }
7e1789f5 512
6c95b8df 513 return info;
7e1789f5
PA
514}
515
516/* This is a "inferior_exit" observer. Releases the TERMINAL_INFO member
517 of the inferior structure. This field is private to inflow.c, and
518 its type is opaque to the rest of GDB. PID is the target pid of
519 the inferior that is about to be removed from the inferior
520 list. */
521
522static void
a79b8f6e 523inflow_inferior_exit (struct inferior *inf)
7e1789f5 524{
6c95b8df 525 struct terminal_info *info;
7e1789f5 526
6c95b8df
PA
527 info = inferior_data (inf, inflow_inferior_data);
528 if (info != NULL)
529 {
530 xfree (info->run_terminal);
1e182ce8 531 xfree (info->ttystate);
6c95b8df
PA
532 xfree (info);
533 set_inferior_data (inf, inflow_inferior_data, NULL);
534 }
7e1789f5
PA
535}
536
191c4426
PA
537void
538copy_terminal_info (struct inferior *to, struct inferior *from)
539{
6c95b8df
PA
540 struct terminal_info *tinfo_to, *tinfo_from;
541
542 tinfo_to = get_inflow_inferior_data (to);
543 tinfo_from = get_inflow_inferior_data (from);
1e182ce8
UW
544
545 xfree (tinfo_to->run_terminal);
546 xfree (tinfo_to->ttystate);
547
6c95b8df 548 *tinfo_to = *tinfo_from;
1e182ce8 549
6c95b8df
PA
550 if (tinfo_from->run_terminal)
551 tinfo_to->run_terminal
552 = xstrdup (tinfo_from->run_terminal);
1e182ce8
UW
553
554 if (tinfo_from->ttystate)
555 tinfo_to->ttystate
556 = serial_copy_tty_state (stdin_serial, tinfo_from->ttystate);
191c4426
PA
557}
558
c906108c 559void
fba45db2 560term_info (char *arg, int from_tty)
c906108c
SS
561{
562 target_terminal_info (arg, from_tty);
563}
564
c906108c 565void
fba45db2 566child_terminal_info (char *args, int from_tty)
c906108c 567{
7e1789f5 568 struct inferior *inf;
6c95b8df 569 struct terminal_info *tinfo;
7e1789f5 570
c906108c
SS
571 if (!gdb_has_a_terminal ())
572 {
a3f17187 573 printf_filtered (_("This GDB does not control a terminal.\n"));
c906108c
SS
574 return;
575 }
576
7e1789f5
PA
577 if (ptid_equal (inferior_ptid, null_ptid))
578 return;
579
580 inf = current_inferior ();
6c95b8df 581 tinfo = get_inflow_inferior_data (inf);
7e1789f5 582
3e43a32a
MS
583 printf_filtered (_("Inferior's terminal status "
584 "(currently saved by GDB):\n"));
c906108c
SS
585
586 /* First the fcntl flags. */
587 {
588 int flags;
c5aa993b 589
6c95b8df 590 flags = tinfo->tflags;
c906108c
SS
591
592 printf_filtered ("File descriptor flags = ");
593
594#ifndef O_ACCMODE
595#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
596#endif
1777feb0 597 /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */
c906108c
SS
598 switch (flags & (O_ACCMODE))
599 {
c5aa993b
JM
600 case O_RDONLY:
601 printf_filtered ("O_RDONLY");
602 break;
603 case O_WRONLY:
604 printf_filtered ("O_WRONLY");
605 break;
606 case O_RDWR:
607 printf_filtered ("O_RDWR");
608 break;
c906108c
SS
609 }
610 flags &= ~(O_ACCMODE);
611
612#ifdef O_NONBLOCK
c5aa993b 613 if (flags & O_NONBLOCK)
c906108c
SS
614 printf_filtered (" | O_NONBLOCK");
615 flags &= ~O_NONBLOCK;
616#endif
c5aa993b 617
c906108c
SS
618#if defined (O_NDELAY)
619 /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
620 print it as O_NONBLOCK, which is good cause that is what POSIX
621 has, and the flag will already be cleared by the time we get here. */
622 if (flags & O_NDELAY)
623 printf_filtered (" | O_NDELAY");
624 flags &= ~O_NDELAY;
625#endif
626
627 if (flags & O_APPEND)
628 printf_filtered (" | O_APPEND");
629 flags &= ~O_APPEND;
630
631#if defined (O_BINARY)
632 if (flags & O_BINARY)
633 printf_filtered (" | O_BINARY");
634 flags &= ~O_BINARY;
635#endif
636
637 if (flags)
638 printf_filtered (" | 0x%x", flags);
639 printf_filtered ("\n");
640 }
641
642#ifdef PROCESS_GROUP_TYPE
6c95b8df 643 printf_filtered ("Process group = %d\n", (int) tinfo->process_group);
c906108c
SS
644#endif
645
6c95b8df 646 serial_print_tty_state (stdin_serial, tinfo->ttystate, gdb_stdout);
c906108c
SS
647}
648\f
649/* NEW_TTY_PREFORK is called before forking a new child process,
650 so we can record the state of ttys in the child to be formed.
651 TTYNAME is null if we are to share the terminal with gdb;
652 or points to a string containing the name of the desired tty.
653
654 NEW_TTY is called in new child processes under Unix, which will
655 become debugger target processes. This actually switches to
656 the terminal specified in the NEW_TTY_PREFORK call. */
657
658void
3cb3b8df 659new_tty_prefork (const char *ttyname)
c906108c
SS
660{
661 /* Save the name for later, for determining whether we and the child
662 are sharing a tty. */
663 inferior_thisrun_terminal = ttyname;
664}
665
e6d088ec 666#if !defined(__GO32__) && !defined(_WIN32)
bf1d7d9c
JB
667/* If RESULT, assumed to be the return value from a system call, is
668 negative, print the error message indicated by errno and exit.
669 MSG should identify the operation that failed. */
670static void
671check_syscall (const char *msg, int result)
672{
673 if (result < 0)
674 {
675 print_sys_errmsg (msg, errno);
676 _exit (1);
677 }
678}
e6d088ec 679#endif
bf1d7d9c 680
c906108c 681void
fba45db2 682new_tty (void)
c906108c 683{
52f0bd74 684 int tty;
c906108c
SS
685
686 if (inferior_thisrun_terminal == 0)
687 return;
688#if !defined(__GO32__) && !defined(_WIN32)
689#ifdef TIOCNOTTY
690 /* Disconnect the child process from our controlling terminal. On some
691 systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
1777feb0 692 ignore SIGTTOU. */
c5aa993b 693 tty = open ("/dev/tty", O_RDWR);
c906108c
SS
694 if (tty > 0)
695 {
696 void (*osigttou) ();
697
c5aa993b
JM
698 osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN);
699 ioctl (tty, TIOCNOTTY, 0);
700 close (tty);
701 signal (SIGTTOU, osigttou);
c906108c
SS
702 }
703#endif
704
705 /* Now open the specified new terminal. */
c5aa993b 706 tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
bf1d7d9c 707 check_syscall (inferior_thisrun_terminal, tty);
c906108c
SS
708
709 /* Avoid use of dup2; doesn't exist on all systems. */
710 if (tty != 0)
c5aa993b
JM
711 {
712 close (0);
bf1d7d9c 713 check_syscall ("dup'ing tty into fd 0", dup (tty));
c5aa993b 714 }
c906108c 715 if (tty != 1)
c5aa993b
JM
716 {
717 close (1);
bf1d7d9c 718 check_syscall ("dup'ing tty into fd 1", dup (tty));
c5aa993b 719 }
c906108c 720 if (tty != 2)
c5aa993b
JM
721 {
722 close (2);
bf1d7d9c 723 check_syscall ("dup'ing tty into fd 2", dup (tty));
c5aa993b 724 }
83116857
TJB
725
726#ifdef TIOCSCTTY
727 /* Make tty our new controlling terminal. */
728 if (ioctl (tty, TIOCSCTTY, 0) == -1)
729 /* Mention GDB in warning because it will appear in the inferior's
730 terminal instead of GDB's. */
a73c6dcd 731 warning (_("GDB: Failed to set controlling terminal: %s"),
83116857
TJB
732 safe_strerror (errno));
733#endif
734
c906108c 735 if (tty > 2)
c5aa993b
JM
736 close (tty);
737#endif /* !go32 && !win32 */
c906108c 738}
191c4426
PA
739
740/* NEW_TTY_POSTFORK is called after forking a new child process, and
741 adding it to the inferior table, to store the TTYNAME being used by
742 the child, or null if it sharing the terminal with gdb. */
743
744void
745new_tty_postfork (void)
746{
747 /* Save the name for later, for determining whether we and the child
748 are sharing a tty. */
749
750 if (inferior_thisrun_terminal)
6c95b8df
PA
751 {
752 struct inferior *inf = current_inferior ();
753 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
754
755 tinfo->run_terminal = xstrdup (inferior_thisrun_terminal);
756 }
191c4426
PA
757
758 inferior_thisrun_terminal = NULL;
759}
760
c906108c
SS
761\f
762/* Call set_sigint_trap when you need to pass a signal on to an attached
1777feb0 763 process when handling SIGINT. */
c906108c 764
c906108c 765static void
fba45db2 766pass_signal (int signo)
c906108c
SS
767{
768#ifndef _WIN32
39f77062 769 kill (PIDGET (inferior_ptid), SIGINT);
c906108c
SS
770#endif
771}
772
c5aa993b 773static void (*osig) ();
7e1789f5 774static int osig_set;
c906108c
SS
775
776void
fba45db2 777set_sigint_trap (void)
c906108c 778{
181e7f93 779 struct inferior *inf = current_inferior ();
6c95b8df
PA
780 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
781
782 if (inf->attach_flag || tinfo->run_terminal)
c906108c 783 {
c5aa993b 784 osig = (void (*)()) signal (SIGINT, pass_signal);
7e1789f5 785 osig_set = 1;
c906108c 786 }
7e1789f5
PA
787 else
788 osig_set = 0;
c906108c
SS
789}
790
791void
fba45db2 792clear_sigint_trap (void)
c906108c 793{
7e1789f5 794 if (osig_set)
c906108c
SS
795 {
796 signal (SIGINT, osig);
7e1789f5 797 osig_set = 0;
c906108c
SS
798 }
799}
800\f
c906108c 801
83116857
TJB
802/* Create a new session if the inferior will run in a different tty.
803 A session is UNIX's way of grouping processes that share a controlling
804 terminal, so a new one is needed if the inferior terminal will be
805 different from GDB's.
806
807 Returns the session id of the new session, 0 if no session was created
808 or -1 if an error occurred. */
809pid_t
810create_tty_session (void)
811{
812#ifdef HAVE_SETSID
813 pid_t ret;
814
815 if (!job_control || inferior_thisrun_terminal == 0)
816 return 0;
817
818 ret = setsid ();
819 if (ret == -1)
a73c6dcd 820 warning (_("Failed to create new terminal session: setsid: %s"),
83116857
TJB
821 safe_strerror (errno));
822
823 return ret;
824#else
825 return 0;
826#endif /* HAVE_SETSID */
827}
828
c906108c
SS
829/* This is here because this is where we figure out whether we (probably)
830 have job control. Just using job_control only does part of it because
831 setpgid or setpgrp might not exist on a system without job control.
832 It might be considered misplaced (on the other hand, process groups and
833 job control are closely related to ttys).
834
835 For a more clean implementation, in libiberty, put a setpgid which merely
836 calls setpgrp and a setpgrp which does nothing (any system with job control
837 will have one or the other). */
838int
fba45db2 839gdb_setpgid (void)
c906108c
SS
840{
841 int retval = 0;
842
843 if (job_control)
844 {
0200359f
MK
845#if defined (HAVE_TERMIOS) || defined (TIOCGPGRP)
846#ifdef HAVE_SETPGID
847 /* The call setpgid (0, 0) is supposed to work and mean the same
848 thing as this, but on Ultrix 4.2A it fails with EPERM (and
c5aa993b 849 setpgid (getpid (), getpid ()) succeeds). */
c906108c
SS
850 retval = setpgid (getpid (), getpid ());
851#else
0200359f
MK
852#ifdef HAVE_SETPGRP
853#ifdef SETPGRP_VOID
c906108c
SS
854 retval = setpgrp ();
855#else
856 retval = setpgrp (getpid (), getpid ());
0200359f
MK
857#endif
858#endif /* HAVE_SETPGRP */
859#endif /* HAVE_SETPGID */
860#endif /* defined (HAVE_TERMIOS) || defined (TIOCGPGRP) */
c906108c 861 }
0200359f 862
c906108c
SS
863 return retval;
864}
865
0ea3f30e
DJ
866/* Get all the current tty settings (including whether we have a
867 tty at all!). We can't do this in _initialize_inflow because
868 serial_fdopen() won't work until the serial_ops_list is
869 initialized, but we don't want to do it lazily either, so
870 that we can guarantee stdin_serial is opened if there is
871 a terminal. */
872void
873initialize_stdin_serial (void)
874{
875 stdin_serial = serial_fdopen (0);
876}
877
c906108c 878void
fba45db2 879_initialize_inflow (void)
c906108c
SS
880{
881 add_info ("terminal", term_info,
1bedd215 882 _("Print inferior's saved terminal status."));
c906108c 883
7bfc9434
JB
884 add_setshow_auto_boolean_cmd ("interactive-mode", class_support,
885 &interactive_mode, _("\
886Set whether GDB's standard input is a terminal."), _("\
887Show whether GDB's standard input is a terminal."), _("\
888If on, GDB assumes that standard input is a terminal. In practice, it\n\
889means that GDB should wait for the user to answer queries associated to\n\
890commands entered at the command prompt. If off, GDB assumes that standard\n\
891input is not a terminal, and uses the default answer to all queries.\n\
892If auto (the default), determine which mode to use based on the standard\n\
893input settings."),
894 NULL,
895 show_interactive_mode,
896 &setlist, &showlist);
897
c906108c
SS
898 terminal_is_ours = 1;
899
900 /* OK, figure out whether we have job control. If neither termios nor
901 sgtty (i.e. termio or go32), leave job_control 0. */
902
903#if defined (HAVE_TERMIOS)
904 /* Do all systems with termios have the POSIX way of identifying job
905 control? I hope so. */
906#ifdef _POSIX_JOB_CONTROL
907 job_control = 1;
908#else
909#ifdef _SC_JOB_CONTROL
910 job_control = sysconf (_SC_JOB_CONTROL);
911#else
1777feb0 912 job_control = 0; /* Have to assume the worst. */
c5aa993b
JM
913#endif /* _SC_JOB_CONTROL */
914#endif /* _POSIX_JOB_CONTROL */
915#endif /* HAVE_TERMIOS */
c906108c
SS
916
917#ifdef HAVE_SGTTY
918#ifdef TIOCGPGRP
919 job_control = 1;
920#else
921 job_control = 0;
922#endif /* TIOCGPGRP */
923#endif /* sgtty */
7e1789f5 924
7e1789f5 925 observer_attach_inferior_exit (inflow_inferior_exit);
6c95b8df
PA
926
927 inflow_inferior_data
8e260fc0 928 = register_inferior_data_with_cleanup (NULL, inflow_inferior_data_cleanup);
c906108c 929}
This page took 1.051661 seconds and 4 git commands to generate.