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