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. Zero if the
82 inferior's settings are in effect. Ignored if !gdb_has_a_terminal
85 static int terminal_is_ours
;
87 enum {yes
, no
, have_not_checked
} gdb_has_a_terminal_flag
= have_not_checked
;
89 /* Does GDB have a terminal (on stdin)? */
93 switch (gdb_has_a_terminal_flag
)
99 case have_not_checked
:
100 /* Get all the current tty settings (including whether we have a tty at
101 all!). Can't do this in _initialize_inflow because SERIAL_FDOPEN
102 won't work until the serial_ops_list is initialized. */
105 tflags_ours
= fcntl (0, F_GETFL
, 0);
108 gdb_has_a_terminal_flag
= no
;
109 stdin_serial
= SERIAL_FDOPEN (0);
110 if (stdin_serial
!= NULL
)
112 our_ttystate
= SERIAL_GET_TTY_STATE (stdin_serial
);
113 if (our_ttystate
!= NULL
)
114 gdb_has_a_terminal_flag
= yes
;
117 return gdb_has_a_terminal_flag
== yes
;
121 /* Macro for printing errors from ioctl operations */
123 #define OOPSY(what) \
125 fprintf(stderr, "[%s failed in terminal_inferior: %s]\n", \
126 what, strerror (errno))
128 static void terminal_ours_1
PARAMS ((int));
130 /* Initialize the terminal settings we record for the inferior,
131 before we actually run the inferior. */
134 terminal_init_inferior ()
136 if (gdb_has_a_terminal ())
138 /* We could just as well copy our_ttystate (if we felt like adding
139 a new function SERIAL_COPY_TTY_STATE). */
140 if (inferior_ttystate
)
141 free (inferior_ttystate
);
142 inferior_ttystate
= SERIAL_GET_TTY_STATE (stdin_serial
);
143 SERIAL_SET_PROCESS_GROUP (stdin_serial
, inferior_ttystate
, inferior_pid
);
145 /* Make sure that next time we call terminal_inferior (which will be
146 before the program runs, as it needs to be), we install the new
148 terminal_is_ours
= 1;
152 /* Put the inferior's terminal settings into effect.
153 This is preparation for starting or resuming the inferior. */
158 if (gdb_has_a_terminal () && terminal_is_ours
159 && inferior_thisrun_terminal
== 0)
164 /* Is there a reason this is being done twice? It happens both
165 places we use F_SETFL, so I'm inclined to think perhaps there
166 is some reason, however perverse. Perhaps not though... */
167 result
= fcntl (0, F_SETFL
, tflags_inferior
);
168 result
= fcntl (0, F_SETFL
, tflags_inferior
);
169 OOPSY ("fcntl F_SETFL");
172 /* Because we were careful to not change in or out of raw mode in
173 terminal_ours, we will not change in our out of raw mode with
174 this call, so we don't flush any input. */
175 result
= SERIAL_SET_TTY_STATE (stdin_serial
, inferior_ttystate
);
177 /* If attach_flag is set, we don't know whether we are sharing a
178 terminal with the inferior or not. (attaching a process
179 without a terminal is one case where we do not; attaching a
180 process which we ran from the same shell as GDB via `&' is
181 one case where we do, I think (but perhaps this is not
182 `sharing' in the sense that we need to save and restore tty
183 state)). I don't know if there is any way to tell whether we
184 are sharing a terminal. So what we do is to go through all
185 the saving and restoring of terminal state, but ignore errors
186 (which will occur, in tcsetpgrp, if we are not sharing a
190 OOPSY ("setting tty state");
194 sigint_ours
= (void (*) ()) signal (SIGINT
, SIG_IGN
);
195 sigquit_ours
= (void (*) ()) signal (SIGQUIT
, SIG_IGN
);
198 terminal_is_ours
= 0;
201 /* Put some of our terminal settings into effect,
202 enough to get proper results from our output,
203 but do not change into or out of RAW mode
204 so that no input is discarded.
206 After doing this, either terminal_ours or terminal_inferior
207 should be called to get back to a normal state of affairs. */
210 terminal_ours_for_output ()
215 /* Put our terminal settings into effect.
216 First record the inferior's terminal settings
217 so they can be restored properly later. */
225 /* output_only is not used, and should not be used unless we introduce
226 separate terminal_is_ours and terminal_is_ours_for_output
230 terminal_ours_1 (output_only
)
233 /* Checking inferior_thisrun_terminal is necessary so that
234 if GDB is running in the background, it won't block trying
235 to do the ioctl()'s below. Checking gdb_has_a_terminal
236 avoids attempting all the ioctl's when running in batch. */
237 if (inferior_thisrun_terminal
!= 0 || gdb_has_a_terminal () == 0)
240 if (!terminal_is_ours
)
242 /* Ignore this signal since it will happen when we try to set the
247 terminal_is_ours
= 1;
251 osigttou
= (void (*) ()) signal (SIGTTOU
, SIG_IGN
);
254 if (inferior_ttystate
)
255 free (inferior_ttystate
);
256 inferior_ttystate
= SERIAL_GET_TTY_STATE (stdin_serial
);
258 /* Here we used to set ICANON in our ttystate, but I believe this
259 was an artifact from before when we used readline. Readline sets
260 the tty state when it needs to. */
262 /* Set tty state to our_ttystate. We don't change in our out of raw
263 mode, to avoid flushing input. We need to do the same thing
264 regardless of output_only, because we don't have separate
265 terminal_is_ours and terminal_is_ours_for_output flags. It's OK,
266 though, since readline will deal with raw mode when/if it needs to.
268 SERIAL_NOFLUSH_SET_TTY_STATE (stdin_serial
, our_ttystate
,
273 signal (SIGTTOU
, osigttou
);
278 signal (SIGINT
, sigint_ours
);
279 signal (SIGQUIT
, sigquit_ours
);
283 tflags_inferior
= fcntl (0, F_GETFL
, 0);
285 /* Is there a reason this is being done twice? It happens both
286 places we use F_SETFL, so I'm inclined to think perhaps there
287 is some reason, however perverse. Perhaps not though... */
288 result
= fcntl (0, F_SETFL
, tflags_ours
);
289 result
= fcntl (0, F_SETFL
, tflags_ours
);
292 result
= result
; /* lint */
298 term_info (arg
, from_tty
)
302 target_terminal_info (arg
, from_tty
);
307 child_terminal_info (args
, from_tty
)
311 if (!gdb_has_a_terminal ())
313 printf_filtered ("This GDB does not control a terminal.\n");
317 printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
319 /* First the fcntl flags. */
323 flags
= tflags_inferior
;
325 printf_filtered ("File descriptor flags = ");
328 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
330 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
331 switch (flags
& (O_ACCMODE
))
333 case O_RDONLY
: printf_filtered ("O_RDONLY"); break;
334 case O_WRONLY
: printf_filtered ("O_WRONLY"); break;
335 case O_RDWR
: printf_filtered ("O_RDWR"); break;
337 flags
&= ~(O_ACCMODE
);
340 if (flags
& O_NONBLOCK
)
341 printf_filtered (" | O_NONBLOCK");
342 flags
&= ~O_NONBLOCK
;
345 #if defined (O_NDELAY)
346 /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
347 print it as O_NONBLOCK, which is good cause that is what POSIX
348 has, and the flag will already be cleared by the time we get here. */
349 if (flags
& O_NDELAY
)
350 printf_filtered (" | O_NDELAY");
354 if (flags
& O_APPEND
)
355 printf_filtered (" | O_APPEND");
358 #if defined (O_BINARY)
359 if (flags
& O_BINARY
)
360 printf_filtered (" | O_BINARY");
365 printf_filtered (" | 0x%x", flags
);
366 printf_filtered ("\n");
369 SERIAL_PRINT_TTY_STATE (stdin_serial
, inferior_ttystate
);
372 /* NEW_TTY_PREFORK is called before forking a new child process,
373 so we can record the state of ttys in the child to be formed.
374 TTYNAME is null if we are to share the terminal with gdb;
375 or points to a string containing the name of the desired tty.
377 NEW_TTY is called in new child processes under Unix, which will
378 become debugger target processes. This actually switches to
379 the terminal specified in the NEW_TTY_PREFORK call. */
382 new_tty_prefork (ttyname
)
385 /* Save the name for later, for determining whether we and the child
386 are sharing a tty. */
387 inferior_thisrun_terminal
= ttyname
;
395 if (inferior_thisrun_terminal
== 0)
397 #if !defined(__GO32__)
399 /* Disconnect the child process from our controlling terminal. On some
400 systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
402 tty
= open("/dev/tty", O_RDWR
);
407 osigttou
= (void (*)()) signal(SIGTTOU
, SIG_IGN
);
408 ioctl(tty
, TIOCNOTTY
, 0);
410 signal(SIGTTOU
, osigttou
);
414 /* Now open the specified new terminal. */
417 tty
= open(inferior_thisrun_terminal
, O_RDWR
| O_NOCTTY
);
419 tty
= open(inferior_thisrun_terminal
, O_RDWR
);
423 print_sys_errmsg (inferior_thisrun_terminal
, errno
);
427 /* Avoid use of dup2; doesn't exist on all systems. */
429 { close (0); dup (tty
); }
431 { close (1); dup (tty
); }
433 { close (2); dup (tty
); }
439 /* Kill the inferior process. Make us have no inferior. */
443 kill_command (arg
, from_tty
)
447 /* Shouldn't this be target_has_execution? FIXME. */
448 if (inferior_pid
== 0)
449 error ("The program is not being run.");
450 if (!query ("Kill the program being debugged? "))
451 error ("Not confirmed.");
454 /* Killing off the inferior can leave us with a core file. If so,
455 print the state we are left in. */
456 if (target_has_stack
) {
457 printf_filtered ("In %s,\n", current_target
->to_longname
);
458 if (selected_frame
== NULL
)
459 fputs_filtered ("No selected stack frame.\n", stdout
);
461 print_stack_frame (selected_frame
, selected_frame_level
, 1);
465 /* The inferior process has died. Long live the inferior! */
468 generic_mourn_inferior ()
472 mark_breakpoints_out ();
473 registers_changed ();
475 #ifdef CLEAR_DEFERRED_STORES
476 /* Delete any pending stores to the inferior... */
477 CLEAR_DEFERRED_STORES
;
481 flush_cached_frames ();
482 if (target_has_stack
) {
483 set_current_frame ( create_new_frame (read_register (FP_REGNUM
),
485 select_frame (get_current_frame (), 0);
487 set_current_frame (0);
488 select_frame ((FRAME
) 0, -1);
490 /* It is confusing to the user for ignore counts to stick around
491 from previous runs of the inferior. So clear them. */
492 breakpoint_clear_ignore_counts ();
495 /* Call set_sigint_trap when you need to pass a signal on to an attached
496 process when handling SIGINT */
503 kill (inferior_pid
, SIGINT
);
506 static void (*osig
)();
511 osig
= (void (*) ()) signal (SIGINT
, pass_signal
);
517 signal (SIGINT
, osig
);
521 _initialize_inflow ()
523 add_info ("terminal", term_info
,
524 "Print inferior's saved terminal status.");
526 add_com ("kill", class_run
, kill_command
,
527 "Kill execution of program being debugged.");
531 terminal_is_ours
= 1;