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