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