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