]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/inflow.c
* inflow.c (pass_signal): Signal handlers take one int arg;
[thirdparty/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 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 "target.h"
27
28 #ifdef USG
29 #include <sys/types.h>
30 #endif
31
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. */
34 #include <fcntl.h>
35
36 #include <sys/param.h>
37 #include <signal.h>
38
39 static void
40 kill_command PARAMS ((char *, int));
41
42 static void
43 terminal_ours_1 PARAMS ((int));
44
45 /* Nonzero if we are debugging an attached outside process
46 rather than an inferior. */
47
48 int attach_flag;
49
50 \f
51 /* Record terminal status separately for debugger and inferior. */
52
53 static serial_t stdin_serial;
54
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;
58
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;
63
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;
68
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
72 to SIG_IGN. */
73 static void (*sigint_ours) ();
74 static void (*sigquit_ours) ();
75
76 /* The name of the tty (from the `tty' command) that we gave to the inferior
77 when it was last started. */
78
79 static char *inferior_thisrun_terminal;
80
81 /* Nonzero if our terminal settings are in effect.
82 Zero if the inferior's settings are in effect. */
83
84 static int terminal_is_ours;
85
86 enum {yes, no, have_not_checked} gdb_has_a_terminal_flag = have_not_checked;
87
88 /* Does GDB have a terminal (on stdin)? */
89 int
90 gdb_has_a_terminal ()
91 {
92 switch (gdb_has_a_terminal_flag)
93 {
94 case yes:
95 return 1;
96 case no:
97 return 0;
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. */
102
103 tflags_ours = fcntl (0, F_GETFL, 0);
104
105 gdb_has_a_terminal_flag = no;
106 stdin_serial = SERIAL_FDOPEN (0);
107 if (stdin_serial != NULL)
108 {
109 our_ttystate = SERIAL_GET_TTY_STATE (stdin_serial);
110 if (our_ttystate != NULL)
111 gdb_has_a_terminal_flag = yes;
112 }
113
114 return gdb_has_a_terminal_flag == yes;
115 }
116 }
117
118 /* Macro for printing errors from ioctl operations */
119
120 #define OOPSY(what) \
121 if (result == -1) \
122 fprintf(stderr, "[%s failed in terminal_inferior: %s]\n", \
123 what, strerror (errno))
124
125 static void terminal_ours_1 PARAMS ((int));
126
127 /* Initialize the terminal settings we record for the inferior,
128 before we actually run the inferior. */
129
130 void
131 terminal_init_inferior ()
132 {
133 /* Make sure that our_ttystate (etc) are initialized. */
134 gdb_has_a_terminal ();
135
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);
142
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
145 process group. */
146 terminal_is_ours = 1;
147 }
148
149 /* Put the inferior's terminal settings into effect.
150 This is preparation for starting or resuming the inferior. */
151
152 void
153 terminal_inferior ()
154 {
155 if (gdb_has_a_terminal () && terminal_is_ours
156 && inferior_thisrun_terminal == 0)
157 {
158 int result;
159
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");
166
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);
171
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
182 terminal). */
183
184 if (!attach_flag)
185 OOPSY ("setting tty state");
186
187 if (!job_control)
188 {
189 sigint_ours = (void (*) ()) signal (SIGINT, SIG_IGN);
190 sigquit_ours = (void (*) ()) signal (SIGQUIT, SIG_IGN);
191 }
192 }
193 terminal_is_ours = 0;
194 }
195
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.
200
201 After doing this, either terminal_ours or terminal_inferior
202 should be called to get back to a normal state of affairs. */
203
204 void
205 terminal_ours_for_output ()
206 {
207 terminal_ours_1 (1);
208 }
209
210 /* Put our terminal settings into effect.
211 First record the inferior's terminal settings
212 so they can be restored properly later. */
213
214 void
215 terminal_ours ()
216 {
217 terminal_ours_1 (0);
218 }
219
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
222 flags. */
223
224 static void
225 terminal_ours_1 (output_only)
226 int output_only;
227 {
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)
233 return;
234
235 if (!terminal_is_ours)
236 {
237 /* Ignore this signal since it will happen when we try to set the
238 pgrp. */
239 void (*osigttou) ();
240 int result;
241
242 terminal_is_ours = 1;
243
244 #ifdef SIGTTOU
245 if (job_control)
246 osigttou = (void (*) ()) signal (SIGTTOU, SIG_IGN);
247 #endif
248
249 if (inferior_ttystate)
250 free (inferior_ttystate);
251 inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial);
252
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. */
256
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.
262 */
263 SERIAL_NOFLUSH_SET_TTY_STATE (stdin_serial, our_ttystate,
264 inferior_ttystate);
265
266 #ifdef SIGTTOU
267 if (job_control)
268 signal (SIGTTOU, osigttou);
269 #endif
270
271 if (!job_control)
272 {
273 signal (SIGINT, sigint_ours);
274 signal (SIGQUIT, sigquit_ours);
275 }
276
277 tflags_inferior = fcntl (0, F_GETFL, 0);
278
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);
284
285 result = result; /* lint */
286 }
287 }
288
289 /* ARGSUSED */
290 void
291 term_info (arg, from_tty)
292 char *arg;
293 int from_tty;
294 {
295 target_terminal_info (arg, from_tty);
296 }
297
298 /* ARGSUSED */
299 void
300 child_terminal_info (args, from_tty)
301 char *args;
302 int from_tty;
303 {
304 if (!gdb_has_a_terminal ())
305 {
306 printf_filtered ("This GDB does not control a terminal.\n");
307 return;
308 }
309
310 printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
311
312 /* First the fcntl flags. */
313 {
314 int flags;
315
316 flags = tflags_inferior;
317
318 printf_filtered ("File descriptor flags = ");
319
320 #ifndef O_ACCMODE
321 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
322 #endif
323 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
324 switch (flags & (O_ACCMODE))
325 {
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;
329 }
330 flags &= ~(O_ACCMODE);
331
332 #ifdef O_NONBLOCK
333 if (flags & O_NONBLOCK)
334 printf_filtered (" | O_NONBLOCK");
335 flags &= ~O_NONBLOCK;
336 #endif
337
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");
344 flags &= ~O_NDELAY;
345 #endif
346
347 if (flags & O_APPEND)
348 printf_filtered (" | O_APPEND");
349 flags &= ~O_APPEND;
350
351 #if defined (O_BINARY)
352 if (flags & O_BINARY)
353 printf_filtered (" | O_BINARY");
354 flags &= ~O_BINARY;
355 #endif
356
357 if (flags)
358 printf_filtered (" | 0x%x", flags);
359 printf_filtered ("\n");
360 }
361
362 SERIAL_PRINT_TTY_STATE (stdin_serial, inferior_ttystate);
363 }
364 \f
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.
369
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. */
373
374 void
375 new_tty_prefork (ttyname)
376 char *ttyname;
377 {
378 /* Save the name for later, for determining whether we and the child
379 are sharing a tty. */
380 inferior_thisrun_terminal = ttyname;
381 }
382
383 void
384 new_tty ()
385 {
386 register int tty;
387 void (*osigttou) ();
388
389 if (inferior_thisrun_terminal == 0)
390 return;
391 #if !defined(__GO32__)
392 #ifdef TIOCNOTTY
393 /* Disconnect the child process from our controlling terminal. On some
394 systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
395 ignore SIGTTOU. */
396 tty = open("/dev/tty", O_RDWR);
397 if (tty > 0)
398 {
399 osigttou = (void (*)()) signal(SIGTTOU, SIG_IGN);
400 ioctl(tty, TIOCNOTTY, 0);
401 close(tty);
402 signal(SIGTTOU, osigttou);
403 }
404 #endif
405
406 /* Now open the specified new terminal. */
407
408 #ifdef USE_O_NOCTTY
409 tty = open(inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
410 #else
411 tty = open(inferior_thisrun_terminal, O_RDWR);
412 #endif
413 if (tty == -1)
414 {
415 print_sys_errmsg (inferior_thisrun_terminal, errno);
416 _exit(1);
417 }
418
419 /* Avoid use of dup2; doesn't exist on all systems. */
420 if (tty != 0)
421 { close (0); dup (tty); }
422 if (tty != 1)
423 { close (1); dup (tty); }
424 if (tty != 2)
425 { close (2); dup (tty); }
426 if (tty > 2)
427 close(tty);
428 #endif /* !go32 */
429 }
430 \f
431 /* Kill the inferior process. Make us have no inferior. */
432
433 /* ARGSUSED */
434 static void
435 kill_command (arg, from_tty)
436 char *arg;
437 int from_tty;
438 {
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.");
444 target_kill ();
445
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);
452 else
453 print_stack_frame (selected_frame, selected_frame_level, 1);
454 }
455 }
456
457 /* The inferior process has died. Long live the inferior! */
458
459 void
460 generic_mourn_inferior ()
461 {
462 inferior_pid = 0;
463 attach_flag = 0;
464 mark_breakpoints_out ();
465 registers_changed ();
466
467 #ifdef CLEAR_DEFERRED_STORES
468 /* Delete any pending stores to the inferior... */
469 CLEAR_DEFERRED_STORES;
470 #endif
471
472 reopen_exec_file ();
473 if (target_has_stack) {
474 set_current_frame ( create_new_frame (read_register (FP_REGNUM),
475 read_pc ()));
476 select_frame (get_current_frame (), 0);
477 } else {
478 set_current_frame (0);
479 select_frame ((FRAME) 0, -1);
480 }
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 ();
484 }
485 \f
486 /* Call set_sigint_trap when you need to pass a signal on to an attached
487 process when handling SIGINT */
488
489 /* ARGSUSED */
490 static void
491 pass_signal (signo)
492 int signo;
493 {
494 kill (inferior_pid, SIGINT);
495 }
496
497 static void (*osig)();
498
499 void
500 set_sigint_trap()
501 {
502 osig = (void (*) ()) signal (SIGINT, pass_signal);
503 }
504
505 void
506 clear_sigint_trap()
507 {
508 signal (SIGINT, osig);
509 }
510 \f
511 void
512 _initialize_inflow ()
513 {
514 add_info ("terminal", term_info,
515 "Print inferior's saved terminal status.");
516
517 add_com ("kill", class_run, kill_command,
518 "Kill execution of program being debugged.");
519
520 inferior_pid = 0;
521
522 terminal_is_ours = 1;
523 }