1 /* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright 1986, 1987, 1989, 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
29 #include <sys/types.h>
32 /* Some USG-esque systems (some of which are BSD-esque enough so that USG
33 is not defined) want this header, and it won't do any harm. */
36 #include <sys/param.h>
40 kill_command PARAMS ((char *, int));
43 terminal_ours_1 PARAMS ((int));
45 /* Nonzero if we are debugging an attached outside process
46 rather than an inferior. */
51 /* Record terminal status separately for debugger and inferior. */
53 static serial_t stdin_serial;
55 /* TTY state for the inferior. We save it whenever the inferior stops, and
56 restore it when it resumes. */
57 static serial_ttystate inferior_ttystate;
59 /* Our own tty state, which we restore every time we need to deal with the
60 terminal. We only set it once, when GDB first starts. The settings of
61 flags which readline saves and restores and unimportant. */
62 static serial_ttystate our_ttystate;
64 /* fcntl flags for us and the inferior. Saved and restored just like
65 {our,inferior}_ttystate. */
66 static int tflags_inferior;
67 static int tflags_ours;
69 /* While the inferior is running, we want SIGINT and SIGQUIT to go to the
70 inferior only. If we have job control, that takes care of it. If not,
71 we save our handlers in these two variables and set SIGINT and SIGQUIT
73 static void (*sigint_ours) ();
74 static void (*sigquit_ours) ();
76 /* The name of the tty (from the `tty' command) that we gave to the inferior
77 when it was last started. */
79 static char *inferior_thisrun_terminal;
81 /* Nonzero if our terminal settings are in effect.
82 Zero if the inferior's settings are in effect. */
84 static int terminal_is_ours;
86 enum {yes, no, have_not_checked} gdb_has_a_terminal_flag = have_not_checked;
88 /* Does GDB have a terminal (on stdin)? */
92 switch (gdb_has_a_terminal_flag)
98 case have_not_checked:
99 /* Get all the current tty settings (including whether we have a tty at
100 all!). Can't do this in _initialize_inflow because SERIAL_FDOPEN
101 won't work until the serial_ops_list is initialized. */
103 tflags_ours = fcntl (0, F_GETFL, 0);
105 gdb_has_a_terminal_flag = no;
106 stdin_serial = SERIAL_FDOPEN (0);
107 if (stdin_serial != NULL)
109 our_ttystate = SERIAL_GET_TTY_STATE (stdin_serial);
110 if (our_ttystate != NULL)
111 gdb_has_a_terminal_flag = yes;
114 return gdb_has_a_terminal_flag == yes;
118 /* Macro for printing errors from ioctl operations */
120 #define OOPSY(what) \
122 fprintf(stderr, "[%s failed in terminal_inferior: %s]\n", \
123 what, strerror (errno))
125 static void terminal_ours_1 PARAMS ((int));
127 /* Initialize the terminal settings we record for the inferior,
128 before we actually run the inferior. */
131 terminal_init_inferior ()
133 /* Make sure that our_ttystate (etc) are initialized. */
134 gdb_has_a_terminal ();
136 /* We could just as well copy our_ttystate (if we felt like adding
137 a new function SERIAL_COPY_TTY_STATE). */
138 if (inferior_ttystate)
139 free (inferior_ttystate);
140 inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial);
141 SERIAL_SET_PROCESS_GROUP (stdin_serial, inferior_ttystate, inferior_pid);
143 /* Make sure that next time we call terminal_inferior (which will be
144 before the program runs, as it needs to be), we install the new
146 terminal_is_ours = 1;
149 /* Put the inferior's terminal settings into effect.
150 This is preparation for starting or resuming the inferior. */
155 if (gdb_has_a_terminal () && terminal_is_ours
156 && inferior_thisrun_terminal == 0)
160 /* Is there a reason this is being done twice? It happens both
161 places we use F_SETFL, so I'm inclined to think perhaps there
162 is some reason, however perverse. Perhaps not though... */
163 result = fcntl (0, F_SETFL, tflags_inferior);
164 result = fcntl (0, F_SETFL, tflags_inferior);
165 OOPSY ("fcntl F_SETFL");
167 /* Because we were careful to not change in or out of raw mode in
168 terminal_ours, we will not change in our out of raw mode with
169 this call, so we don't flush any input. */
170 result = SERIAL_SET_TTY_STATE (stdin_serial, inferior_ttystate);
172 /* If attach_flag is set, we don't know whether we are sharing a
173 terminal with the inferior or not. (attaching a process
174 without a terminal is one case where we do not; attaching a
175 process which we ran from the same shell as GDB via `&' is
176 one case where we do, I think (but perhaps this is not
177 `sharing' in the sense that we need to save and restore tty
178 state)). I don't know if there is any way to tell whether we
179 are sharing a terminal. So what we do is to go through all
180 the saving and restoring of terminal state, but ignore errors
181 (which will occur, in tcsetpgrp, if we are not sharing a
185 OOPSY ("setting tty state");
189 sigint_ours = (void (*) ()) signal (SIGINT, SIG_IGN);
190 sigquit_ours = (void (*) ()) signal (SIGQUIT, SIG_IGN);
193 terminal_is_ours = 0;
196 /* Put some of our terminal settings into effect,
197 enough to get proper results from our output,
198 but do not change into or out of RAW mode
199 so that no input is discarded.
201 After doing this, either terminal_ours or terminal_inferior
202 should be called to get back to a normal state of affairs. */
205 terminal_ours_for_output ()
210 /* Put our terminal settings into effect.
211 First record the inferior's terminal settings
212 so they can be restored properly later. */
220 /* output_only is not used, and should not be used unless we introduce
221 separate terminal_is_ours and terminal_is_ours_for_output
225 terminal_ours_1 (output_only)
228 /* Checking inferior_thisrun_terminal is necessary so that
229 if GDB is running in the background, it won't block trying
230 to do the ioctl()'s below. Checking gdb_has_a_terminal
231 avoids attempting all the ioctl's when running in batch. */
232 if (inferior_thisrun_terminal != 0 || gdb_has_a_terminal () == 0)
235 if (!terminal_is_ours)
237 /* Ignore this signal since it will happen when we try to set the
242 terminal_is_ours = 1;
246 osigttou = (void (*) ()) signal (SIGTTOU, SIG_IGN);
249 if (inferior_ttystate)
250 free (inferior_ttystate);
251 inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial);
253 /* Here we used to set ICANON in our ttystate, but I believe this
254 was an artifact from before when we used readline. Readline sets
255 the tty state when it needs to. */
257 /* Set tty state to our_ttystate. We don't change in our out of raw
258 mode, to avoid flushing input. We need to do the same thing
259 regardless of output_only, because we don't have separate
260 terminal_is_ours and terminal_is_ours_for_output flags. It's OK,
261 though, since readline will deal with raw mode when/if it needs to.
263 SERIAL_NOFLUSH_SET_TTY_STATE (stdin_serial, our_ttystate,
268 signal (SIGTTOU, osigttou);
273 signal (SIGINT, sigint_ours);
274 signal (SIGQUIT, sigquit_ours);
277 tflags_inferior = fcntl (0, F_GETFL, 0);
279 /* Is there a reason this is being done twice? It happens both
280 places we use F_SETFL, so I'm inclined to think perhaps there
281 is some reason, however perverse. Perhaps not though... */
282 result = fcntl (0, F_SETFL, tflags_ours);
283 result = fcntl (0, F_SETFL, tflags_ours);
285 result = result; /* lint */
291 term_info (arg, from_tty)
295 target_terminal_info (arg, from_tty);
300 child_terminal_info (args, from_tty)
304 if (!gdb_has_a_terminal ())
306 printf_filtered ("This GDB does not control a terminal.\n");
310 printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
312 /* First the fcntl flags. */
316 flags = tflags_inferior;
318 printf_filtered ("File descriptor flags = ");
321 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
323 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
324 switch (flags & (O_ACCMODE))
326 case O_RDONLY: printf_filtered ("O_RDONLY"); break;
327 case O_WRONLY: printf_filtered ("O_WRONLY"); break;
328 case O_RDWR: printf_filtered ("O_RDWR"); break;
330 flags &= ~(O_ACCMODE);
333 if (flags & O_NONBLOCK)
334 printf_filtered (" | O_NONBLOCK");
335 flags &= ~O_NONBLOCK;
338 #if defined (O_NDELAY)
339 /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
340 print it as O_NONBLOCK, which is good cause that is what POSIX
341 has, and the flag will already be cleared by the time we get here. */
342 if (flags & O_NDELAY)
343 printf_filtered (" | O_NDELAY");
347 if (flags & O_APPEND)
348 printf_filtered (" | O_APPEND");
351 #if defined (O_BINARY)
352 if (flags & O_BINARY)
353 printf_filtered (" | O_BINARY");
358 printf_filtered (" | 0x%x", flags);
359 printf_filtered ("\n");
362 SERIAL_PRINT_TTY_STATE (stdin_serial, inferior_ttystate);
365 /* NEW_TTY_PREFORK is called before forking a new child process,
366 so we can record the state of ttys in the child to be formed.
367 TTYNAME is null if we are to share the terminal with gdb;
368 or points to a string containing the name of the desired tty.
370 NEW_TTY is called in new child processes under Unix, which will
371 become debugger target processes. This actually switches to
372 the terminal specified in the NEW_TTY_PREFORK call. */
375 new_tty_prefork (ttyname)
378 /* Save the name for later, for determining whether we and the child
379 are sharing a tty. */
380 inferior_thisrun_terminal = ttyname;
389 if (inferior_thisrun_terminal == 0)
391 #if !defined(__GO32__)
393 /* Disconnect the child process from our controlling terminal. On some
394 systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
396 tty = open("/dev/tty", O_RDWR);
399 osigttou = (void (*)()) signal(SIGTTOU, SIG_IGN);
400 ioctl(tty, TIOCNOTTY, 0);
402 signal(SIGTTOU, osigttou);
406 /* Now open the specified new terminal. */
409 tty = open(inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
411 tty = open(inferior_thisrun_terminal, O_RDWR);
415 print_sys_errmsg (inferior_thisrun_terminal, errno);
419 /* Avoid use of dup2; doesn't exist on all systems. */
421 { close (0); dup (tty); }
423 { close (1); dup (tty); }
425 { close (2); dup (tty); }
431 /* Kill the inferior process. Make us have no inferior. */
435 kill_command (arg, from_tty)
439 /* Shouldn't this be target_has_execution? FIXME. */
440 if (inferior_pid == 0)
441 error ("The program is not being run.");
442 if (!query ("Kill the program being debugged? "))
443 error ("Not confirmed.");
446 /* Killing off the inferior can leave us with a core file. If so,
447 print the state we are left in. */
448 if (target_has_stack) {
449 printf_filtered ("In %s,\n", current_target->to_longname);
450 if (selected_frame == NULL)
451 fputs_filtered ("No selected stack frame.\n", stdout);
453 print_stack_frame (selected_frame, selected_frame_level, 1);
457 /* The inferior process has died. Long live the inferior! */
460 generic_mourn_inferior ()
464 mark_breakpoints_out ();
465 registers_changed ();
467 #ifdef CLEAR_DEFERRED_STORES
468 /* Delete any pending stores to the inferior... */
469 CLEAR_DEFERRED_STORES;
473 if (target_has_stack) {
474 set_current_frame ( create_new_frame (read_register (FP_REGNUM),
476 select_frame (get_current_frame (), 0);
478 set_current_frame (0);
479 select_frame ((FRAME) 0, -1);
481 /* It is confusing to the user for ignore counts to stick around
482 from previous runs of the inferior. So clear them. */
483 breakpoint_clear_ignore_counts ();
486 /* Call set_sigint_trap when you need to pass a signal on to an attached
487 process when handling SIGINT */
494 kill (inferior_pid, SIGINT);
497 static void (*osig)();
502 osig = (void (*) ()) signal (SIGINT, pass_signal);
508 signal (SIGINT, osig);
512 _initialize_inflow ()
514 add_info ("terminal", term_info,
515 "Print inferior's saved terminal status.");
517 add_com ("kill", class_run, kill_command,
518 "Kill execution of program being debugged.");
522 terminal_is_ours = 1;