Changes from the FSF for Hurd thread support.
[deliverable/binutils-gdb.git] / gdb / inflow.c
1 /* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1995 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "command.h"
24 #include "signals.h"
25 #include "serial.h"
26 #include "terminal.h"
27 #include "target.h"
28 #include "gdbthread.h"
29
30 #include "gdb_string.h"
31 #include <signal.h>
32 #include <fcntl.h>
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36
37 #ifdef HAVE_TERMIOS
38 #define PROCESS_GROUP_TYPE pid_t
39 #endif
40
41 #ifdef HAVE_SGTTY
42 #ifdef SHORT_PGRP
43 /* This is only used for the ultra. Does it have pid_t? */
44 #define PROCESS_GROUP_TYPE short
45 #else
46 #define PROCESS_GROUP_TYPE int
47 #endif
48 #endif /* sgtty */
49
50 static void
51 kill_command PARAMS ((char *, int));
52
53 static void
54 terminal_ours_1 PARAMS ((int));
55 \f
56 /* Record terminal status separately for debugger and inferior. */
57
58 static serial_t stdin_serial;
59
60 /* TTY state for the inferior. We save it whenever the inferior stops, and
61 restore it when it resumes. */
62 static serial_ttystate inferior_ttystate;
63
64 /* Our own tty state, which we restore every time we need to deal with the
65 terminal. We only set it once, when GDB first starts. The settings of
66 flags which readline saves and restores and unimportant. */
67 static serial_ttystate our_ttystate;
68
69 /* fcntl flags for us and the inferior. Saved and restored just like
70 {our,inferior}_ttystate. */
71 static int tflags_inferior;
72 static int tflags_ours;
73
74 #ifdef PROCESS_GROUP_TYPE
75 /* Process group for us and the inferior. Saved and restored just like
76 {our,inferior}_ttystate. */
77 PROCESS_GROUP_TYPE our_process_group;
78 PROCESS_GROUP_TYPE inferior_process_group;
79 #endif
80
81 /* While the inferior is running, we want SIGINT and SIGQUIT to go to the
82 inferior only. If we have job control, that takes care of it. If not,
83 we save our handlers in these two variables and set SIGINT and SIGQUIT
84 to SIG_IGN. */
85 static void (*sigint_ours) ();
86 static void (*sigquit_ours) ();
87
88 /* The name of the tty (from the `tty' command) that we gave to the inferior
89 when it was last started. */
90
91 static char *inferior_thisrun_terminal;
92
93 /* Nonzero if our terminal settings are in effect. Zero if the
94 inferior's settings are in effect. Ignored if !gdb_has_a_terminal
95 (). */
96
97 static int terminal_is_ours;
98
99 enum {yes, no, have_not_checked} gdb_has_a_terminal_flag = have_not_checked;
100
101 /* Does GDB have a terminal (on stdin)? */
102 int
103 gdb_has_a_terminal ()
104 {
105 switch (gdb_has_a_terminal_flag)
106 {
107 case yes:
108 return 1;
109 case no:
110 return 0;
111 case have_not_checked:
112 /* Get all the current tty settings (including whether we have a tty at
113 all!). Can't do this in _initialize_inflow because SERIAL_FDOPEN
114 won't work until the serial_ops_list is initialized. */
115
116 #ifdef F_GETFL
117 tflags_ours = fcntl (0, F_GETFL, 0);
118 #endif
119
120 gdb_has_a_terminal_flag = no;
121 stdin_serial = SERIAL_FDOPEN (0);
122 if (stdin_serial != NULL)
123 {
124 our_ttystate = SERIAL_GET_TTY_STATE (stdin_serial);
125
126 if (our_ttystate != NULL)
127 {
128 gdb_has_a_terminal_flag = yes;
129 #ifdef HAVE_TERMIOS
130 our_process_group = tcgetpgrp (0);
131 #endif
132 #ifdef HAVE_SGTTY
133 ioctl (0, TIOCGPGRP, &our_process_group);
134 #endif
135 }
136 }
137
138 return gdb_has_a_terminal_flag == yes;
139 default:
140 /* "Can't happen". */
141 return 0;
142 }
143 }
144
145 /* Macro for printing errors from ioctl operations */
146
147 #define OOPSY(what) \
148 if (result == -1) \
149 fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
150 what, strerror (errno))
151
152 static void terminal_ours_1 PARAMS ((int));
153
154 /* Initialize the terminal settings we record for the inferior,
155 before we actually run the inferior. */
156
157 void
158 terminal_init_inferior_with_pgrp (pgrp)
159 int pgrp;
160 {
161 if (gdb_has_a_terminal ())
162 {
163 /* We could just as well copy our_ttystate (if we felt like adding
164 a new function SERIAL_COPY_TTY_STATE). */
165 if (inferior_ttystate)
166 free (inferior_ttystate);
167 inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial);
168
169 #ifdef PROCESS_GROUP_TYPE
170 inferior_process_group = pgrp;
171 #endif
172
173 /* Make sure that next time we call terminal_inferior (which will be
174 before the program runs, as it needs to be), we install the new
175 process group. */
176 terminal_is_ours = 1;
177 }
178 }
179
180 void
181 terminal_init_inferior ()
182 {
183 #ifdef PROCESS_GROUP_TYPE
184 #ifdef PIDGET
185 /* This is for Lynx, and should be cleaned up by having Lynx be a separate
186 debugging target with a version of target_terminal_init_inferior which
187 passes in the process group to a generic routine which does all the work
188 (and the non-threaded child_terminal_init_inferior can just pass in
189 inferior_pid to the same routine). */
190 terminal_init_inferior_with_pgrp (PIDGET (inferior_pid));
191 #else
192 /* By default, we assume INFERIOR_PID is also the child's process group. */
193 terminal_init_inferior_with_pgrp (inferior_pid);
194 #endif
195 #endif /* PROCESS_GROUP_TYPE */
196 }
197
198 /* Put the inferior's terminal settings into effect.
199 This is preparation for starting or resuming the inferior. */
200
201 void
202 terminal_inferior ()
203 {
204 if (gdb_has_a_terminal () && terminal_is_ours
205 && inferior_thisrun_terminal == 0)
206 {
207 int result;
208
209 #ifdef F_GETFL
210 /* Is there a reason this is being done twice? It happens both
211 places we use F_SETFL, so I'm inclined to think perhaps there
212 is some reason, however perverse. Perhaps not though... */
213 result = fcntl (0, F_SETFL, tflags_inferior);
214 result = fcntl (0, F_SETFL, tflags_inferior);
215 OOPSY ("fcntl F_SETFL");
216 #endif
217
218 /* Because we were careful to not change in or out of raw mode in
219 terminal_ours, we will not change in our out of raw mode with
220 this call, so we don't flush any input. */
221 result = SERIAL_SET_TTY_STATE (stdin_serial, inferior_ttystate);
222 OOPSY ("setting tty state");
223
224 if (!job_control)
225 {
226 sigint_ours = (void (*) ()) signal (SIGINT, SIG_IGN);
227 sigquit_ours = (void (*) ()) signal (SIGQUIT, SIG_IGN);
228 }
229
230 /* If attach_flag is set, we don't know whether we are sharing a
231 terminal with the inferior or not. (attaching a process
232 without a terminal is one case where we do not; attaching a
233 process which we ran from the same shell as GDB via `&' is
234 one case where we do, I think (but perhaps this is not
235 `sharing' in the sense that we need to save and restore tty
236 state)). I don't know if there is any way to tell whether we
237 are sharing a terminal. So what we do is to go through all
238 the saving and restoring of the tty state, but ignore errors
239 setting the process group, which will happen if we are not
240 sharing a terminal). */
241
242 if (job_control)
243 {
244 #ifdef HAVE_TERMIOS
245 result = tcsetpgrp (0, inferior_process_group);
246 if (!attach_flag)
247 OOPSY ("tcsetpgrp");
248 #endif
249
250 #ifdef HAVE_SGTTY
251 result = ioctl (0, TIOCSPGRP, &inferior_process_group);
252 if (!attach_flag)
253 OOPSY ("TIOCSPGRP");
254 #endif
255 }
256
257 }
258 terminal_is_ours = 0;
259 }
260
261 /* Put some of our terminal settings into effect,
262 enough to get proper results from our output,
263 but do not change into or out of RAW mode
264 so that no input is discarded.
265
266 After doing this, either terminal_ours or terminal_inferior
267 should be called to get back to a normal state of affairs. */
268
269 void
270 terminal_ours_for_output ()
271 {
272 terminal_ours_1 (1);
273 }
274
275 /* Put our terminal settings into effect.
276 First record the inferior's terminal settings
277 so they can be restored properly later. */
278
279 void
280 terminal_ours ()
281 {
282 terminal_ours_1 (0);
283 }
284
285 /* output_only is not used, and should not be used unless we introduce
286 separate terminal_is_ours and terminal_is_ours_for_output
287 flags. */
288
289 static void
290 terminal_ours_1 (output_only)
291 int output_only;
292 {
293 /* Checking inferior_thisrun_terminal is necessary so that
294 if GDB is running in the background, it won't block trying
295 to do the ioctl()'s below. Checking gdb_has_a_terminal
296 avoids attempting all the ioctl's when running in batch. */
297 if (inferior_thisrun_terminal != 0 || gdb_has_a_terminal () == 0)
298 return;
299
300 if (!terminal_is_ours)
301 {
302 /* Ignore this signal since it will happen when we try to set the
303 pgrp. */
304 void (*osigttou) ();
305 int result;
306
307 terminal_is_ours = 1;
308
309 #ifdef SIGTTOU
310 if (job_control)
311 osigttou = (void (*) ()) signal (SIGTTOU, SIG_IGN);
312 #endif
313
314 if (inferior_ttystate)
315 free (inferior_ttystate);
316 inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial);
317 #ifdef HAVE_TERMIOS
318 inferior_process_group = tcgetpgrp (0);
319 #endif
320 #ifdef HAVE_SGTTY
321 ioctl (0, TIOCGPGRP, &inferior_process_group);
322 #endif
323
324 /* Here we used to set ICANON in our ttystate, but I believe this
325 was an artifact from before when we used readline. Readline sets
326 the tty state when it needs to.
327 FIXME-maybe: However, query() expects non-raw mode and doesn't
328 use readline. Maybe query should use readline (on the other hand,
329 this only matters for HAVE_SGTTY, not termio or termios, I think). */
330
331 /* Set tty state to our_ttystate. We don't change in our out of raw
332 mode, to avoid flushing input. We need to do the same thing
333 regardless of output_only, because we don't have separate
334 terminal_is_ours and terminal_is_ours_for_output flags. It's OK,
335 though, since readline will deal with raw mode when/if it needs to.
336 */
337
338 SERIAL_NOFLUSH_SET_TTY_STATE (stdin_serial, our_ttystate,
339 inferior_ttystate);
340
341 if (job_control)
342 {
343 #ifdef HAVE_TERMIOS
344 result = tcsetpgrp (0, our_process_group);
345 #if 0
346 /* This fails on Ultrix with EINVAL if you run the testsuite
347 in the background with nohup, and then log out. GDB never
348 used to check for an error here, so perhaps there are other
349 such situations as well. */
350 if (result == -1)
351 fprintf_unfiltered (gdb_stderr, "[tcsetpgrp failed in terminal_ours: %s]\n",
352 strerror (errno));
353 #endif
354 #endif /* termios */
355
356 #ifdef HAVE_SGTTY
357 result = ioctl (0, TIOCSPGRP, &our_process_group);
358 #endif
359 }
360
361 #ifdef SIGTTOU
362 if (job_control)
363 signal (SIGTTOU, osigttou);
364 #endif
365
366 if (!job_control)
367 {
368 signal (SIGINT, sigint_ours);
369 signal (SIGQUIT, sigquit_ours);
370 }
371
372 #ifdef F_GETFL
373 tflags_inferior = fcntl (0, F_GETFL, 0);
374
375 /* Is there a reason this is being done twice? It happens both
376 places we use F_SETFL, so I'm inclined to think perhaps there
377 is some reason, however perverse. Perhaps not though... */
378 result = fcntl (0, F_SETFL, tflags_ours);
379 result = fcntl (0, F_SETFL, tflags_ours);
380 #endif
381
382 result = result; /* lint */
383 }
384 }
385
386 /* ARGSUSED */
387 void
388 term_info (arg, from_tty)
389 char *arg;
390 int from_tty;
391 {
392 target_terminal_info (arg, from_tty);
393 }
394
395 /* ARGSUSED */
396 void
397 child_terminal_info (args, from_tty)
398 char *args;
399 int from_tty;
400 {
401 if (!gdb_has_a_terminal ())
402 {
403 printf_filtered ("This GDB does not control a terminal.\n");
404 return;
405 }
406
407 printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
408
409 /* First the fcntl flags. */
410 {
411 int flags;
412
413 flags = tflags_inferior;
414
415 printf_filtered ("File descriptor flags = ");
416
417 #ifndef O_ACCMODE
418 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
419 #endif
420 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
421 switch (flags & (O_ACCMODE))
422 {
423 case O_RDONLY: printf_filtered ("O_RDONLY"); break;
424 case O_WRONLY: printf_filtered ("O_WRONLY"); break;
425 case O_RDWR: printf_filtered ("O_RDWR"); break;
426 }
427 flags &= ~(O_ACCMODE);
428
429 #ifdef O_NONBLOCK
430 if (flags & O_NONBLOCK)
431 printf_filtered (" | O_NONBLOCK");
432 flags &= ~O_NONBLOCK;
433 #endif
434
435 #if defined (O_NDELAY)
436 /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
437 print it as O_NONBLOCK, which is good cause that is what POSIX
438 has, and the flag will already be cleared by the time we get here. */
439 if (flags & O_NDELAY)
440 printf_filtered (" | O_NDELAY");
441 flags &= ~O_NDELAY;
442 #endif
443
444 if (flags & O_APPEND)
445 printf_filtered (" | O_APPEND");
446 flags &= ~O_APPEND;
447
448 #if defined (O_BINARY)
449 if (flags & O_BINARY)
450 printf_filtered (" | O_BINARY");
451 flags &= ~O_BINARY;
452 #endif
453
454 if (flags)
455 printf_filtered (" | 0x%x", flags);
456 printf_filtered ("\n");
457 }
458
459 #ifdef PROCESS_GROUP_TYPE
460 printf_filtered ("Process group = %d\n", inferior_process_group);
461 #endif
462
463 SERIAL_PRINT_TTY_STATE (stdin_serial, inferior_ttystate);
464 }
465 \f
466 /* NEW_TTY_PREFORK is called before forking a new child process,
467 so we can record the state of ttys in the child to be formed.
468 TTYNAME is null if we are to share the terminal with gdb;
469 or points to a string containing the name of the desired tty.
470
471 NEW_TTY is called in new child processes under Unix, which will
472 become debugger target processes. This actually switches to
473 the terminal specified in the NEW_TTY_PREFORK call. */
474
475 void
476 new_tty_prefork (ttyname)
477 char *ttyname;
478 {
479 /* Save the name for later, for determining whether we and the child
480 are sharing a tty. */
481 inferior_thisrun_terminal = ttyname;
482 }
483
484 void
485 new_tty ()
486 {
487 register int tty;
488
489 if (inferior_thisrun_terminal == 0)
490 return;
491 #if !defined(__GO32__) && !defined(__WIN32__)
492 #ifdef TIOCNOTTY
493 /* Disconnect the child process from our controlling terminal. On some
494 systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
495 ignore SIGTTOU. */
496 tty = open("/dev/tty", O_RDWR);
497 if (tty > 0)
498 {
499 void (*osigttou) ();
500
501 osigttou = (void (*)()) signal(SIGTTOU, SIG_IGN);
502 ioctl(tty, TIOCNOTTY, 0);
503 close(tty);
504 signal(SIGTTOU, osigttou);
505 }
506 #endif
507
508 /* Now open the specified new terminal. */
509
510 #ifdef USE_O_NOCTTY
511 tty = open(inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
512 #else
513 tty = open(inferior_thisrun_terminal, O_RDWR);
514 #endif
515 if (tty == -1)
516 {
517 print_sys_errmsg (inferior_thisrun_terminal, errno);
518 _exit(1);
519 }
520
521 /* Avoid use of dup2; doesn't exist on all systems. */
522 if (tty != 0)
523 { close (0); dup (tty); }
524 if (tty != 1)
525 { close (1); dup (tty); }
526 if (tty != 2)
527 { close (2); dup (tty); }
528 if (tty > 2)
529 close(tty);
530 #endif /* !go32 && !win32*/
531 }
532 \f
533 /* Kill the inferior process. Make us have no inferior. */
534
535 /* ARGSUSED */
536 static void
537 kill_command (arg, from_tty)
538 char *arg;
539 int from_tty;
540 {
541 /* FIXME: This should not really be inferior_pid (or target_has_execution).
542 It should be a distinct flag that indicates that a target is active, cuz
543 some targets don't have processes! */
544
545 if (inferior_pid == 0)
546 error ("The program is not being run.");
547 if (!query ("Kill the program being debugged? "))
548 error ("Not confirmed.");
549 target_kill ();
550
551 init_thread_list(); /* Destroy thread info */
552
553 /* Killing off the inferior can leave us with a core file. If so,
554 print the state we are left in. */
555 if (target_has_stack) {
556 printf_filtered ("In %s,\n", target_longname);
557 if (selected_frame == NULL)
558 fputs_filtered ("No selected stack frame.\n", gdb_stdout);
559 else
560 print_stack_frame (selected_frame, selected_frame_level, 1);
561 }
562 }
563 \f
564 /* Call set_sigint_trap when you need to pass a signal on to an attached
565 process when handling SIGINT */
566
567 /* ARGSUSED */
568 static void
569 pass_signal (signo)
570 int signo;
571 {
572 kill (inferior_pid, SIGINT);
573 }
574
575 static void (*osig)();
576
577 void
578 set_sigint_trap()
579 {
580 if (attach_flag || inferior_thisrun_terminal)
581 {
582 osig = (void (*) ()) signal (SIGINT, pass_signal);
583 }
584 }
585
586 void
587 clear_sigint_trap()
588 {
589 if (attach_flag || inferior_thisrun_terminal)
590 {
591 signal (SIGINT, osig);
592 }
593 }
594 \f
595 #if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN)
596 static void (*old_sigio) ();
597
598 static void
599 handle_sigio (signo)
600 int signo;
601 {
602 int numfds;
603 fd_set readfds;
604
605 signal (SIGIO, handle_sigio);
606
607 FD_ZERO (&readfds);
608 FD_SET (target_activity_fd, &readfds);
609 numfds = select (target_activity_fd + 1, &readfds, NULL, NULL, NULL);
610 if (numfds >= 0 && FD_ISSET (target_activity_fd, &readfds))
611 {
612 if ((*target_activity_function) ())
613 kill (inferior_pid, SIGINT);
614 }
615 }
616
617 static int old_fcntl_flags;
618
619 void
620 set_sigio_trap ()
621 {
622 if (target_activity_function)
623 {
624 old_sigio = (void (*) ()) signal (SIGIO, handle_sigio);
625 fcntl (target_activity_fd, F_SETOWN, getpid());
626 old_fcntl_flags = fcntl (target_activity_fd, F_GETFL, 0);
627 fcntl (target_activity_fd, F_SETFL, old_fcntl_flags | FASYNC);
628 }
629 }
630
631 void
632 clear_sigio_trap ()
633 {
634 if (target_activity_function)
635 {
636 signal (SIGIO, old_sigio);
637 fcntl (target_activity_fd, F_SETFL, old_fcntl_flags);
638 }
639 }
640 #else /* No SIGIO. */
641 void
642 set_sigio_trap ()
643 {
644 if (target_activity_function)
645 abort ();
646 }
647
648 void
649 clear_sigio_trap ()
650 {
651 if (target_activity_function)
652 abort ();
653 }
654 #endif /* No SIGIO. */
655 \f
656
657 /* This is here because this is where we figure out whether we (probably)
658 have job control. Just using job_control only does part of it because
659 setpgid or setpgrp might not exist on a system without job control.
660 It might be considered misplaced (on the other hand, process groups and
661 job control are closely related to ttys).
662
663 For a more clean implementation, in libiberty, put a setpgid which merely
664 calls setpgrp and a setpgrp which does nothing (any system with job control
665 will have one or the other). */
666 int
667 gdb_setpgid ()
668 {
669 int retval = 0;
670
671 if (job_control)
672 {
673 #if defined (NEED_POSIX_SETPGID) || (defined (HAVE_TERMIOS) && defined (HAVE_SETPGID))
674 /* setpgid (0, 0) is supposed to work and mean the same thing as
675 this, but on Ultrix 4.2A it fails with EPERM (and
676 setpgid (getpid (), getpid ()) succeeds). */
677 retval = setpgid (getpid (), getpid ());
678 #else
679 #if defined (TIOCGPGRP)
680 #if defined(USG) && !defined(SETPGRP_ARGS)
681 retval = setpgrp ();
682 #else
683 retval = setpgrp (getpid (), getpid ());
684 #endif /* USG */
685 #endif /* TIOCGPGRP. */
686 #endif /* NEED_POSIX_SETPGID */
687 }
688 return retval;
689 }
690
691 void
692 _initialize_inflow ()
693 {
694 add_info ("terminal", term_info,
695 "Print inferior's saved terminal status.");
696
697 add_com ("kill", class_run, kill_command,
698 "Kill execution of program being debugged.");
699
700 inferior_pid = 0;
701
702 terminal_is_ours = 1;
703
704 /* OK, figure out whether we have job control. If neither termios nor
705 sgtty (i.e. termio or go32), leave job_control 0. */
706
707 #if defined (HAVE_TERMIOS)
708 /* Do all systems with termios have the POSIX way of identifying job
709 control? I hope so. */
710 #ifdef _POSIX_JOB_CONTROL
711 job_control = 1;
712 #else
713 #ifdef _SC_JOB_CONTROL
714 job_control = sysconf (_SC_JOB_CONTROL);
715 #else
716 job_control = 0; /* have to assume the worst */
717 #endif /* _SC_JOB_CONTROL */
718 #endif /* _POSIX_JOB_CONTROL */
719 #endif /* HAVE_TERMIOS */
720
721 #ifdef HAVE_SGTTY
722 #ifdef TIOCGPGRP
723 job_control = 1;
724 #else
725 job_control = 0;
726 #endif /* TIOCGPGRP */
727 #endif /* sgtty */
728 }
This page took 0.044874 seconds and 5 git commands to generate.