use remote-utils facilities for baud_rate
[deliverable/binutils-gdb.git] / gdb / inflow.c
CommitLineData
bd5635a1 1/* Low level interface to ptrace, for GDB when running under Unix.
ee0613d1 2 Copyright 1986, 1987, 1989, 1991, 1992 Free Software Foundation, Inc.
bd5635a1
RP
3
4This file is part of GDB.
5
715d2e06 6This program is free software; you can redistribute it and/or modify
bd5635a1 7it under the terms of the GNU General Public License as published by
715d2e06
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
715d2e06 11This program is distributed in the hope that it will be useful,
bd5635a1
RP
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
715d2e06
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 19
bd5635a1 20#include "defs.h"
bd5635a1
RP
21#include "frame.h"
22#include "inferior.h"
23#include "command.h"
24#include "signals.h"
a88797b5 25#include "serial.h"
bd5635a1
RP
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>
bd5635a1
RP
37#include <signal.h>
38
e676a15f
FF
39static void
40kill_command PARAMS ((char *, int));
41
42static void
43terminal_ours_1 PARAMS ((int));
2a5ec41d 44
bd5635a1
RP
45/* Nonzero if we are debugging an attached outside process
46 rather than an inferior. */
47
48int attach_flag;
49
50\f
51/* Record terminal status separately for debugger and inferior. */
52
a88797b5 53static serial_t stdin_serial;
bd5635a1 54
a88797b5
FF
55/* TTY state for the inferior. We save it whenever the inferior stops, and
56 restore it when it resumes. */
57static serial_ttystate inferior_ttystate;
bd5635a1 58
a88797b5
FF
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. */
62static serial_ttystate our_ttystate;
bd5635a1 63
a88797b5
FF
64/* fcntl flags for us and the inferior. Saved and restored just like
65 {our,inferior}_ttystate. */
66static int tflags_inferior;
67static int tflags_ours;
bd5635a1 68
a88797b5
FF
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. */
bd5635a1
RP
73static void (*sigint_ours) ();
74static void (*sigquit_ours) ();
bd5635a1 75
2a5ec41d
JG
76/* The name of the tty (from the `tty' command) that we gave to the inferior
77 when it was last started. */
bd5635a1 78
2a5ec41d 79static char *inferior_thisrun_terminal;
bd5635a1
RP
80
81/* Nonzero if our terminal settings are in effect.
82 Zero if the inferior's settings are in effect. */
2a5ec41d 83
bd5635a1
RP
84static int terminal_is_ours;
85
a88797b5
FF
86enum {yes, no, have_not_checked} gdb_has_a_terminal_flag = have_not_checked;
87
88/* Does GDB have a terminal (on stdin)? */
89int
90gdb_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
2a5ec41d
JG
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
a88797b5 125static void terminal_ours_1 PARAMS ((int));
2a5ec41d 126
bd5635a1
RP
127/* Initialize the terminal settings we record for the inferior,
128 before we actually run the inferior. */
129
130void
131terminal_init_inferior ()
132{
a88797b5
FF
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. */
bd5635a1
RP
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
152void
153terminal_inferior ()
154{
a88797b5
FF
155 if (gdb_has_a_terminal () && terminal_is_ours
156 && inferior_thisrun_terminal == 0)
bd5635a1 157 {
a88797b5
FF
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... */
2a5ec41d
JG
163 result = fcntl (0, F_SETFL, tflags_inferior);
164 result = fcntl (0, F_SETFL, tflags_inferior);
165 OOPSY ("fcntl F_SETFL");
bd5635a1 166
a88797b5
FF
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 }
bd5635a1
RP
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
204void
205terminal_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
214void
215terminal_ours ()
216{
217 terminal_ours_1 (0);
218}
219
a88797b5
FF
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
bd5635a1
RP
224static void
225terminal_ours_1 (output_only)
226 int output_only;
227{
2a5ec41d 228 /* Checking inferior_thisrun_terminal is necessary so that
bd5635a1 229 if GDB is running in the background, it won't block trying
2a5ec41d
JG
230 to do the ioctl()'s below. Checking gdb_has_a_terminal
231 avoids attempting all the ioctl's when running in batch. */
a88797b5 232 if (inferior_thisrun_terminal != 0 || gdb_has_a_terminal () == 0)
bd5635a1
RP
233 return;
234
235 if (!terminal_is_ours)
236 {
a88797b5
FF
237 /* Ignore this signal since it will happen when we try to set the
238 pgrp. */
239 void (*osigttou) ();
240 int result;
241
bd5635a1
RP
242 terminal_is_ours = 1;
243
a88797b5
FF
244#ifdef SIGTTOU
245 if (job_control)
246 osigttou = (void (*) ()) signal (SIGTTOU, SIG_IGN);
247#endif
bd5635a1 248
a88797b5
FF
249 if (inferior_ttystate)
250 free (inferior_ttystate);
251 inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial);
bd5635a1 252
a88797b5
FF
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. */
bd5635a1 256
a88797b5
FF
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);
bd5635a1 265
a88797b5
FF
266#ifdef SIGTTOU
267 if (job_control)
268 signal (SIGTTOU, osigttou);
bd5635a1 269#endif
bd5635a1 270
a88797b5
FF
271 if (!job_control)
272 {
273 signal (SIGINT, sigint_ours);
274 signal (SIGQUIT, sigquit_ours);
275 }
bd5635a1 276
a88797b5 277 tflags_inferior = fcntl (0, F_GETFL, 0);
e676a15f 278
a88797b5
FF
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 }
bd5635a1
RP
287}
288
289/* ARGSUSED */
290void
291term_info (arg, from_tty)
292 char *arg;
293 int from_tty;
294{
295 target_terminal_info (arg, from_tty);
296}
297
715d2e06 298/* ARGSUSED */
bd5635a1
RP
299void
300child_terminal_info (args, from_tty)
301 char *args;
302 int from_tty;
303{
a88797b5
FF
304 if (!gdb_has_a_terminal ())
305 {
306 printf_filtered ("This GDB does not control a terminal.\n");
307 return;
308 }
bd5635a1
RP
309
310 printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
311
a88797b5
FF
312 /* First the fcntl flags. */
313 {
314 int flags;
315
316 flags = tflags_inferior;
bd5635a1 317
a88797b5 318 printf_filtered ("File descriptor flags = ");
bd5635a1 319
a88797b5
FF
320#ifndef O_ACCMODE
321#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
bd5635a1 322#endif
a88797b5
FF
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);
bd5635a1 331
a88797b5
FF
332#ifdef O_NONBLOCK
333 if (flags & O_NONBLOCK)
334 printf_filtered (" | O_NONBLOCK");
335 flags &= ~O_NONBLOCK;
bd5635a1 336#endif
a88797b5
FF
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;
bd5635a1 345#endif
a88797b5
FF
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;
e676a15f
FF
355#endif
356
a88797b5
FF
357 if (flags)
358 printf_filtered (" | 0x%x", flags);
359 printf_filtered ("\n");
360 }
361
362 SERIAL_PRINT_TTY_STATE (stdin_serial, inferior_ttystate);
bd5635a1
RP
363}
364\f
715d2e06
JG
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.
bd5635a1 369
715d2e06
JG
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
7d9884b9 374void
715d2e06 375new_tty_prefork (ttyname)
bd5635a1
RP
376 char *ttyname;
377{
bd5635a1
RP
378 /* Save the name for later, for determining whether we and the child
379 are sharing a tty. */
380 inferior_thisrun_terminal = ttyname;
715d2e06
JG
381}
382
383void
384new_tty ()
385{
386 register int tty;
e676a15f 387 void (*osigttou) ();
715d2e06
JG
388
389 if (inferior_thisrun_terminal == 0)
bd5635a1 390 return;
e676a15f 391#if !defined(__GO32__)
bd5635a1 392#ifdef TIOCNOTTY
e676a15f
FF
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. */
bd5635a1
RP
396 tty = open("/dev/tty", O_RDWR);
397 if (tty > 0)
398 {
e676a15f 399 osigttou = (void (*)()) signal(SIGTTOU, SIG_IGN);
bd5635a1
RP
400 ioctl(tty, TIOCNOTTY, 0);
401 close(tty);
a88797b5 402 signal(SIGTTOU, osigttou);
bd5635a1
RP
403 }
404#endif
405
406 /* Now open the specified new terminal. */
407
7d9884b9
JG
408#ifdef USE_O_NOCTTY
409 tty = open(inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
410#else
715d2e06 411 tty = open(inferior_thisrun_terminal, O_RDWR);
7d9884b9 412#endif
bd5635a1
RP
413 if (tty == -1)
414 {
715d2e06 415 print_sys_errmsg (inferior_thisrun_terminal, errno);
bd5635a1
RP
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);
a88797b5 428#endif /* !go32 */
bd5635a1
RP
429}
430\f
431/* Kill the inferior process. Make us have no inferior. */
432
433/* ARGSUSED */
434static void
435kill_command (arg, from_tty)
436 char *arg;
437 int from_tty;
438{
a88797b5 439 /* Shouldn't this be target_has_execution? FIXME. */
bd5635a1
RP
440 if (inferior_pid == 0)
441 error ("The program is not being run.");
a88797b5 442 if (!query ("Kill the program being debugged? "))
bd5635a1 443 error ("Not confirmed.");
ee0613d1 444 target_kill ();
777bef06
JK
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
cadbb07a 453 print_stack_frame (selected_frame, selected_frame_level, 1);
777bef06 454 }
bd5635a1
RP
455}
456
457/* The inferior process has died. Long live the inferior! */
458
459void
460generic_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
bd5635a1 472 reopen_exec_file ();
777bef06 473 if (target_has_stack) {
bd5635a1
RP
474 set_current_frame ( create_new_frame (read_register (FP_REGNUM),
475 read_pc ()));
777bef06
JK
476 select_frame (get_current_frame (), 0);
477 } else {
bd5635a1 478 set_current_frame (0);
777bef06
JK
479 select_frame ((FRAME) 0, -1);
480 }
bd5635a1
RP
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}
bd5635a1 485\f
a88797b5
FF
486/* Call set_sigint_trap when you need to pass a signal on to an attached
487 process when handling SIGINT */
488
bd5635a1
RP
489/* ARGSUSED */
490static void
a88797b5
FF
491pass_signal (signo)
492 int signo;
bd5635a1 493{
a88797b5
FF
494 kill (inferior_pid, SIGINT);
495}
bd5635a1 496
a88797b5
FF
497static void (*osig)();
498
499void
500set_sigint_trap()
501{
502 osig = (void (*) ()) signal (SIGINT, pass_signal);
503}
504
505void
506clear_sigint_trap()
507{
508 signal (SIGINT, osig);
bd5635a1 509}
bd5635a1
RP
510\f
511void
512_initialize_inflow ()
513{
514 add_info ("terminal", term_info,
515 "Print inferior's saved terminal status.");
516
bd5635a1
RP
517 add_com ("kill", class_run, kill_command,
518 "Kill execution of program being debugged.");
519
520 inferior_pid = 0;
521
bd5635a1
RP
522 terminal_is_ours = 1;
523}
This page took 0.134717 seconds and 4 git commands to generate.