Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Low level interface to ptrace, for GDB when running under Unix. |
197e01b6 | 2 | Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, |
0fb0cc75 JB |
3 | 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, |
4 | 2009 Free Software Foundation, Inc. | |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 11 | (at your option) any later version. |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b | 18 | You should have received a copy of the GNU General Public License |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
20 | |
21 | #include "defs.h" | |
22 | #include "frame.h" | |
23 | #include "inferior.h" | |
24 | #include "command.h" | |
c906108c SS |
25 | #include "serial.h" |
26 | #include "terminal.h" | |
27 | #include "target.h" | |
28 | #include "gdbthread.h" | |
7e1789f5 | 29 | #include "observer.h" |
c906108c SS |
30 | |
31 | #include "gdb_string.h" | |
32 | #include <signal.h> | |
33 | #include <fcntl.h> | |
0ea3f30e | 34 | #include "gdb_select.h" |
c906108c | 35 | |
44270758 | 36 | #include "inflow.h" |
c906108c | 37 | |
c2d11a7d JM |
38 | #ifdef HAVE_SYS_IOCTL_H |
39 | #include <sys/ioctl.h> | |
40 | #endif | |
41 | ||
f2acbe1c MK |
42 | #ifndef O_NOCTTY |
43 | #define O_NOCTTY 0 | |
44 | #endif | |
45 | ||
c906108c | 46 | #if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN) |
a14ed312 | 47 | static void handle_sigio (int); |
c906108c SS |
48 | #endif |
49 | ||
a14ed312 | 50 | extern void _initialize_inflow (void); |
392a587b | 51 | |
a14ed312 | 52 | static void pass_signal (int); |
c906108c | 53 | |
a14ed312 | 54 | static void terminal_ours_1 (int); |
c906108c SS |
55 | \f |
56 | /* Record terminal status separately for debugger and inferior. */ | |
57 | ||
819cc324 | 58 | static struct serial *stdin_serial; |
c906108c | 59 | |
7e1789f5 PA |
60 | /* Terminal related info we need to keep track of. Each inferior |
61 | holds an instance of this structure --- we save it whenever the | |
62 | corresponding inferior stops, and restore it to the foreground | |
63 | inferior when it resumes. */ | |
64 | struct terminal_info | |
65 | { | |
66 | /* The name of the tty (from the `tty' command) that we gave to the | |
67 | inferior when it was started. */ | |
191c4426 | 68 | char *run_terminal; |
7e1789f5 PA |
69 | |
70 | /* TTY state. We save it whenever the inferior stops, and restore | |
71 | it when it resumes. */ | |
72 | serial_ttystate ttystate; | |
73 | ||
74 | #ifdef PROCESS_GROUP_TYPE | |
75 | /* Process group. Saved and restored just like ttystate. */ | |
76 | PROCESS_GROUP_TYPE process_group; | |
77 | #endif | |
c906108c | 78 | |
7e1789f5 PA |
79 | /* fcntl flags. Saved and restored just like ttystate. */ |
80 | int tflags; | |
81 | }; | |
c906108c | 82 | |
7e1789f5 PA |
83 | /* Our own tty state, which we restore every time we need to deal with |
84 | the terminal. This is only set once, when GDB first starts. The | |
85 | settings of flags which readline saves and restores and | |
86 | unimportant. */ | |
87 | static struct terminal_info our_terminal_info; | |
c906108c SS |
88 | |
89 | #ifdef PROCESS_GROUP_TYPE | |
7e1789f5 PA |
90 | |
91 | /* Return the process group of the current inferior. */ | |
92 | ||
93 | PROCESS_GROUP_TYPE | |
94 | inferior_process_group (void) | |
95 | { | |
96 | return current_inferior ()->terminal_info->process_group; | |
97 | } | |
c906108c SS |
98 | #endif |
99 | ||
100 | /* While the inferior is running, we want SIGINT and SIGQUIT to go to the | |
101 | inferior only. If we have job control, that takes care of it. If not, | |
102 | we save our handlers in these two variables and set SIGINT and SIGQUIT | |
103 | to SIG_IGN. */ | |
104 | ||
105 | static void (*sigint_ours) (); | |
106 | static void (*sigquit_ours) (); | |
107 | ||
7e1789f5 PA |
108 | /* The name of the tty (from the `tty' command) that we're giving to |
109 | the inferior when starting it up. This is only (and should only | |
191c4426 PA |
110 | be) used as a transient global by new_tty_prefork, |
111 | create_tty_session, new_tty and new_tty_postfork, all called from | |
112 | fork_inferior, while forking a new child. */ | |
3cb3b8df | 113 | static const char *inferior_thisrun_terminal; |
c906108c SS |
114 | |
115 | /* Nonzero if our terminal settings are in effect. Zero if the | |
116 | inferior's settings are in effect. Ignored if !gdb_has_a_terminal | |
117 | (). */ | |
118 | ||
119 | int terminal_is_ours; | |
120 | ||
64a0ac84 PA |
121 | #ifdef PROCESS_GROUP_TYPE |
122 | static PROCESS_GROUP_TYPE | |
123 | gdb_getpgrp (void) | |
124 | { | |
125 | int process_group = -1; | |
126 | #ifdef HAVE_TERMIOS | |
127 | process_group = tcgetpgrp (0); | |
128 | #endif | |
129 | #ifdef HAVE_TERMIO | |
130 | process_group = getpgrp (); | |
131 | #endif | |
132 | #ifdef HAVE_SGTTY | |
133 | ioctl (0, TIOCGPGRP, &process_group); | |
134 | #endif | |
135 | return process_group; | |
136 | } | |
137 | #endif | |
138 | ||
c5aa993b JM |
139 | enum |
140 | { | |
141 | yes, no, have_not_checked | |
142 | } | |
143 | gdb_has_a_terminal_flag = have_not_checked; | |
c906108c SS |
144 | |
145 | /* Does GDB have a terminal (on stdin)? */ | |
146 | int | |
fba45db2 | 147 | gdb_has_a_terminal (void) |
c906108c SS |
148 | { |
149 | switch (gdb_has_a_terminal_flag) | |
150 | { | |
151 | case yes: | |
152 | return 1; | |
153 | case no: | |
154 | return 0; | |
155 | case have_not_checked: | |
2cd58942 AC |
156 | /* Get all the current tty settings (including whether we have a |
157 | tty at all!). Can't do this in _initialize_inflow because | |
158 | serial_fdopen() won't work until the serial_ops_list is | |
159 | initialized. */ | |
c906108c SS |
160 | |
161 | #ifdef F_GETFL | |
7e1789f5 | 162 | our_terminal_info.tflags = fcntl (0, F_GETFL, 0); |
c906108c SS |
163 | #endif |
164 | ||
165 | gdb_has_a_terminal_flag = no; | |
c906108c SS |
166 | if (stdin_serial != NULL) |
167 | { | |
7e1789f5 | 168 | our_terminal_info.ttystate = serial_get_tty_state (stdin_serial); |
c906108c | 169 | |
7e1789f5 | 170 | if (our_terminal_info.ttystate != NULL) |
c906108c SS |
171 | { |
172 | gdb_has_a_terminal_flag = yes; | |
807bddf3 | 173 | #ifdef PROCESS_GROUP_TYPE |
7e1789f5 | 174 | our_terminal_info.process_group = gdb_getpgrp (); |
807bddf3 | 175 | #endif |
c906108c SS |
176 | } |
177 | } | |
178 | ||
179 | return gdb_has_a_terminal_flag == yes; | |
180 | default: | |
181 | /* "Can't happen". */ | |
182 | return 0; | |
183 | } | |
184 | } | |
185 | ||
186 | /* Macro for printing errors from ioctl operations */ | |
187 | ||
188 | #define OOPSY(what) \ | |
189 | if (result == -1) \ | |
190 | fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \ | |
dc672865 | 191 | what, safe_strerror (errno)) |
c906108c | 192 | |
a14ed312 | 193 | static void terminal_ours_1 (int); |
c906108c SS |
194 | |
195 | /* Initialize the terminal settings we record for the inferior, | |
196 | before we actually run the inferior. */ | |
197 | ||
198 | void | |
fba45db2 | 199 | terminal_init_inferior_with_pgrp (int pgrp) |
c906108c SS |
200 | { |
201 | if (gdb_has_a_terminal ()) | |
202 | { | |
7e1789f5 PA |
203 | struct inferior *inf = current_inferior (); |
204 | ||
2cd58942 AC |
205 | /* We could just as well copy our_ttystate (if we felt like |
206 | adding a new function serial_copy_tty_state()). */ | |
7e1789f5 PA |
207 | xfree (inf->terminal_info->ttystate); |
208 | inf->terminal_info->ttystate | |
209 | = serial_get_tty_state (stdin_serial); | |
c906108c SS |
210 | |
211 | #ifdef PROCESS_GROUP_TYPE | |
7e1789f5 | 212 | inf->terminal_info->process_group = pgrp; |
c906108c SS |
213 | #endif |
214 | ||
215 | /* Make sure that next time we call terminal_inferior (which will be | |
c5aa993b JM |
216 | before the program runs, as it needs to be), we install the new |
217 | process group. */ | |
c906108c SS |
218 | terminal_is_ours = 1; |
219 | } | |
220 | } | |
221 | ||
a790ad35 SC |
222 | /* Save the terminal settings again. This is necessary for the TUI |
223 | when it switches to TUI or non-TUI mode; curses changes the terminal | |
224 | and gdb must be able to restore it correctly. */ | |
225 | ||
226 | void | |
7bda5e4a | 227 | terminal_save_ours (void) |
a790ad35 SC |
228 | { |
229 | if (gdb_has_a_terminal ()) | |
230 | { | |
231 | /* We could just as well copy our_ttystate (if we felt like adding | |
232 | a new function serial_copy_tty_state). */ | |
7e1789f5 PA |
233 | xfree (our_terminal_info.ttystate); |
234 | our_terminal_info.ttystate = serial_get_tty_state (stdin_serial); | |
a790ad35 SC |
235 | } |
236 | } | |
237 | ||
c906108c | 238 | void |
fba45db2 | 239 | terminal_init_inferior (void) |
c906108c SS |
240 | { |
241 | #ifdef PROCESS_GROUP_TYPE | |
242 | /* This is for Lynx, and should be cleaned up by having Lynx be a separate | |
243 | debugging target with a version of target_terminal_init_inferior which | |
244 | passes in the process group to a generic routine which does all the work | |
245 | (and the non-threaded child_terminal_init_inferior can just pass in | |
39f77062 | 246 | inferior_ptid to the same routine). */ |
c906108c | 247 | /* We assume INFERIOR_PID is also the child's process group. */ |
39f77062 | 248 | terminal_init_inferior_with_pgrp (PIDGET (inferior_ptid)); |
c906108c SS |
249 | #endif /* PROCESS_GROUP_TYPE */ |
250 | } | |
251 | ||
252 | /* Put the inferior's terminal settings into effect. | |
253 | This is preparation for starting or resuming the inferior. */ | |
254 | ||
255 | void | |
fba45db2 | 256 | terminal_inferior (void) |
c906108c | 257 | { |
7e1789f5 PA |
258 | struct inferior *inf; |
259 | ||
260 | if (!terminal_is_ours) | |
261 | return; | |
262 | ||
263 | inf = current_inferior (); | |
264 | ||
265 | if (gdb_has_a_terminal () | |
266 | && inf->terminal_info->ttystate != NULL | |
267 | && inf->terminal_info->run_terminal == NULL) | |
c906108c SS |
268 | { |
269 | int result; | |
270 | ||
271 | #ifdef F_GETFL | |
272 | /* Is there a reason this is being done twice? It happens both | |
c5aa993b JM |
273 | places we use F_SETFL, so I'm inclined to think perhaps there |
274 | is some reason, however perverse. Perhaps not though... */ | |
7e1789f5 PA |
275 | result = fcntl (0, F_SETFL, inf->terminal_info->tflags); |
276 | result = fcntl (0, F_SETFL, inf->terminal_info->tflags); | |
c906108c SS |
277 | OOPSY ("fcntl F_SETFL"); |
278 | #endif | |
279 | ||
280 | /* Because we were careful to not change in or out of raw mode in | |
c5aa993b JM |
281 | terminal_ours, we will not change in our out of raw mode with |
282 | this call, so we don't flush any input. */ | |
7e1789f5 PA |
283 | result = serial_set_tty_state (stdin_serial, |
284 | inf->terminal_info->ttystate); | |
c906108c SS |
285 | OOPSY ("setting tty state"); |
286 | ||
287 | if (!job_control) | |
288 | { | |
c5aa993b | 289 | sigint_ours = (void (*)()) signal (SIGINT, SIG_IGN); |
c906108c | 290 | #ifdef SIGQUIT |
c5aa993b | 291 | sigquit_ours = (void (*)()) signal (SIGQUIT, SIG_IGN); |
c906108c SS |
292 | #endif |
293 | } | |
294 | ||
295 | /* If attach_flag is set, we don't know whether we are sharing a | |
c5aa993b JM |
296 | terminal with the inferior or not. (attaching a process |
297 | without a terminal is one case where we do not; attaching a | |
298 | process which we ran from the same shell as GDB via `&' is | |
299 | one case where we do, I think (but perhaps this is not | |
300 | `sharing' in the sense that we need to save and restore tty | |
301 | state)). I don't know if there is any way to tell whether we | |
302 | are sharing a terminal. So what we do is to go through all | |
303 | the saving and restoring of the tty state, but ignore errors | |
304 | setting the process group, which will happen if we are not | |
305 | sharing a terminal). */ | |
c906108c SS |
306 | |
307 | if (job_control) | |
308 | { | |
309 | #ifdef HAVE_TERMIOS | |
7e1789f5 | 310 | result = tcsetpgrp (0, inf->terminal_info->process_group); |
181e7f93 | 311 | if (!inf->attach_flag) |
c906108c SS |
312 | OOPSY ("tcsetpgrp"); |
313 | #endif | |
314 | ||
315 | #ifdef HAVE_SGTTY | |
7e1789f5 | 316 | result = ioctl (0, TIOCSPGRP, &inf->terminal_info->process_group); |
181e7f93 | 317 | if (!inf->attach_flag) |
c906108c SS |
318 | OOPSY ("TIOCSPGRP"); |
319 | #endif | |
320 | } | |
321 | ||
322 | } | |
323 | terminal_is_ours = 0; | |
324 | } | |
325 | ||
326 | /* Put some of our terminal settings into effect, | |
327 | enough to get proper results from our output, | |
328 | but do not change into or out of RAW mode | |
329 | so that no input is discarded. | |
330 | ||
331 | After doing this, either terminal_ours or terminal_inferior | |
332 | should be called to get back to a normal state of affairs. */ | |
333 | ||
334 | void | |
fba45db2 | 335 | terminal_ours_for_output (void) |
c906108c SS |
336 | { |
337 | terminal_ours_1 (1); | |
338 | } | |
339 | ||
340 | /* Put our terminal settings into effect. | |
341 | First record the inferior's terminal settings | |
342 | so they can be restored properly later. */ | |
343 | ||
344 | void | |
fba45db2 | 345 | terminal_ours (void) |
c906108c SS |
346 | { |
347 | terminal_ours_1 (0); | |
348 | } | |
349 | ||
350 | /* output_only is not used, and should not be used unless we introduce | |
351 | separate terminal_is_ours and terminal_is_ours_for_output | |
352 | flags. */ | |
353 | ||
354 | static void | |
fba45db2 | 355 | terminal_ours_1 (int output_only) |
c906108c | 356 | { |
7e1789f5 PA |
357 | struct inferior *inf; |
358 | ||
359 | if (terminal_is_ours) | |
360 | return; | |
361 | ||
d9d2d8b6 PA |
362 | terminal_is_ours = 1; |
363 | ||
7e1789f5 | 364 | /* Checking inferior->run_terminal is necessary so that |
c906108c SS |
365 | if GDB is running in the background, it won't block trying |
366 | to do the ioctl()'s below. Checking gdb_has_a_terminal | |
367 | avoids attempting all the ioctl's when running in batch. */ | |
7e1789f5 PA |
368 | |
369 | inf = current_inferior (); | |
370 | ||
371 | if (inf->terminal_info->run_terminal != NULL || gdb_has_a_terminal () == 0) | |
c906108c SS |
372 | return; |
373 | ||
c906108c | 374 | { |
4b69c284 | 375 | #ifdef SIGTTOU |
c906108c | 376 | /* Ignore this signal since it will happen when we try to set the |
c5aa993b | 377 | pgrp. */ |
4b69c284 AC |
378 | void (*osigttou) () = NULL; |
379 | #endif | |
c906108c SS |
380 | int result; |
381 | ||
c906108c SS |
382 | #ifdef SIGTTOU |
383 | if (job_control) | |
c5aa993b | 384 | osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN); |
c906108c SS |
385 | #endif |
386 | ||
7e1789f5 PA |
387 | xfree (inf->terminal_info->ttystate); |
388 | inf->terminal_info->ttystate = serial_get_tty_state (stdin_serial); | |
64a0ac84 | 389 | |
49a834f9 | 390 | #ifdef PROCESS_GROUP_TYPE |
181e7f93 | 391 | if (!inf->attach_flag) |
64a0ac84 PA |
392 | /* If setpgrp failed in terminal_inferior, this would give us |
393 | our process group instead of the inferior's. See | |
394 | terminal_inferior for details. */ | |
7e1789f5 | 395 | inf->terminal_info->process_group = gdb_getpgrp (); |
49a834f9 | 396 | #endif |
c906108c SS |
397 | |
398 | /* Here we used to set ICANON in our ttystate, but I believe this | |
c5aa993b JM |
399 | was an artifact from before when we used readline. Readline sets |
400 | the tty state when it needs to. | |
401 | FIXME-maybe: However, query() expects non-raw mode and doesn't | |
402 | use readline. Maybe query should use readline (on the other hand, | |
403 | this only matters for HAVE_SGTTY, not termio or termios, I think). */ | |
c906108c SS |
404 | |
405 | /* Set tty state to our_ttystate. We don't change in our out of raw | |
c5aa993b JM |
406 | mode, to avoid flushing input. We need to do the same thing |
407 | regardless of output_only, because we don't have separate | |
408 | terminal_is_ours and terminal_is_ours_for_output flags. It's OK, | |
409 | though, since readline will deal with raw mode when/if it needs to. | |
410 | */ | |
c906108c | 411 | |
7e1789f5 PA |
412 | serial_noflush_set_tty_state (stdin_serial, our_terminal_info.ttystate, |
413 | inf->terminal_info->ttystate); | |
c906108c SS |
414 | |
415 | if (job_control) | |
416 | { | |
417 | #ifdef HAVE_TERMIOS | |
7e1789f5 | 418 | result = tcsetpgrp (0, our_terminal_info.process_group); |
c906108c SS |
419 | #if 0 |
420 | /* This fails on Ultrix with EINVAL if you run the testsuite | |
421 | in the background with nohup, and then log out. GDB never | |
422 | used to check for an error here, so perhaps there are other | |
423 | such situations as well. */ | |
424 | if (result == -1) | |
425 | fprintf_unfiltered (gdb_stderr, "[tcsetpgrp failed in terminal_ours: %s]\n", | |
dc672865 | 426 | safe_strerror (errno)); |
c906108c SS |
427 | #endif |
428 | #endif /* termios */ | |
429 | ||
430 | #ifdef HAVE_SGTTY | |
7e1789f5 | 431 | result = ioctl (0, TIOCSPGRP, &our_terminal_info.process_group); |
c906108c SS |
432 | #endif |
433 | } | |
434 | ||
435 | #ifdef SIGTTOU | |
436 | if (job_control) | |
437 | signal (SIGTTOU, osigttou); | |
438 | #endif | |
439 | ||
440 | if (!job_control) | |
441 | { | |
442 | signal (SIGINT, sigint_ours); | |
443 | #ifdef SIGQUIT | |
444 | signal (SIGQUIT, sigquit_ours); | |
445 | #endif | |
446 | } | |
447 | ||
448 | #ifdef F_GETFL | |
7e1789f5 | 449 | inf->terminal_info->tflags = fcntl (0, F_GETFL, 0); |
c906108c SS |
450 | |
451 | /* Is there a reason this is being done twice? It happens both | |
c5aa993b JM |
452 | places we use F_SETFL, so I'm inclined to think perhaps there |
453 | is some reason, however perverse. Perhaps not though... */ | |
7e1789f5 PA |
454 | result = fcntl (0, F_SETFL, our_terminal_info.tflags); |
455 | result = fcntl (0, F_SETFL, our_terminal_info.tflags); | |
c906108c | 456 | #endif |
c906108c SS |
457 | } |
458 | } | |
459 | ||
7e1789f5 PA |
460 | /* This is a "new_inferior" observer. It's business is to allocate |
461 | the TERMINAL_INFO member of the inferior structure. This field is | |
462 | private to inflow.c, and its type is opaque to the rest of GDB. | |
463 | PID is the target pid of the inferior that has just been added to | |
464 | the inferior list. */ | |
465 | ||
466 | static void | |
467 | inflow_new_inferior (int pid) | |
468 | { | |
469 | struct inferior *inf = find_inferior_pid (pid); | |
470 | ||
471 | inf->terminal_info = XZALLOC (struct terminal_info); | |
472 | } | |
473 | ||
474 | /* This is a "inferior_exit" observer. Releases the TERMINAL_INFO member | |
475 | of the inferior structure. This field is private to inflow.c, and | |
476 | its type is opaque to the rest of GDB. PID is the target pid of | |
477 | the inferior that is about to be removed from the inferior | |
478 | list. */ | |
479 | ||
480 | static void | |
481 | inflow_inferior_exit (int pid) | |
482 | { | |
483 | struct inferior *inf = find_inferior_pid (pid); | |
484 | ||
191c4426 | 485 | xfree (inf->terminal_info->run_terminal); |
7e1789f5 PA |
486 | xfree (inf->terminal_info); |
487 | inf->terminal_info = NULL; | |
488 | } | |
489 | ||
191c4426 PA |
490 | void |
491 | copy_terminal_info (struct inferior *to, struct inferior *from) | |
492 | { | |
493 | *to->terminal_info = *from->terminal_info; | |
494 | if (from->terminal_info->run_terminal) | |
98b54c18 PA |
495 | to->terminal_info->run_terminal |
496 | = xstrdup (from->terminal_info->run_terminal); | |
191c4426 PA |
497 | } |
498 | ||
c906108c | 499 | void |
fba45db2 | 500 | term_info (char *arg, int from_tty) |
c906108c SS |
501 | { |
502 | target_terminal_info (arg, from_tty); | |
503 | } | |
504 | ||
c906108c | 505 | void |
fba45db2 | 506 | child_terminal_info (char *args, int from_tty) |
c906108c | 507 | { |
7e1789f5 PA |
508 | struct inferior *inf; |
509 | ||
c906108c SS |
510 | if (!gdb_has_a_terminal ()) |
511 | { | |
a3f17187 | 512 | printf_filtered (_("This GDB does not control a terminal.\n")); |
c906108c SS |
513 | return; |
514 | } | |
515 | ||
7e1789f5 PA |
516 | if (ptid_equal (inferior_ptid, null_ptid)) |
517 | return; | |
518 | ||
519 | inf = current_inferior (); | |
520 | ||
a3f17187 | 521 | printf_filtered (_("Inferior's terminal status (currently saved by GDB):\n")); |
c906108c SS |
522 | |
523 | /* First the fcntl flags. */ | |
524 | { | |
525 | int flags; | |
c5aa993b | 526 | |
7e1789f5 | 527 | flags = inf->terminal_info->tflags; |
c906108c SS |
528 | |
529 | printf_filtered ("File descriptor flags = "); | |
530 | ||
531 | #ifndef O_ACCMODE | |
532 | #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR) | |
533 | #endif | |
534 | /* (O_ACCMODE) parens are to avoid Ultrix header file bug */ | |
535 | switch (flags & (O_ACCMODE)) | |
536 | { | |
c5aa993b JM |
537 | case O_RDONLY: |
538 | printf_filtered ("O_RDONLY"); | |
539 | break; | |
540 | case O_WRONLY: | |
541 | printf_filtered ("O_WRONLY"); | |
542 | break; | |
543 | case O_RDWR: | |
544 | printf_filtered ("O_RDWR"); | |
545 | break; | |
c906108c SS |
546 | } |
547 | flags &= ~(O_ACCMODE); | |
548 | ||
549 | #ifdef O_NONBLOCK | |
c5aa993b | 550 | if (flags & O_NONBLOCK) |
c906108c SS |
551 | printf_filtered (" | O_NONBLOCK"); |
552 | flags &= ~O_NONBLOCK; | |
553 | #endif | |
c5aa993b | 554 | |
c906108c SS |
555 | #if defined (O_NDELAY) |
556 | /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will | |
557 | print it as O_NONBLOCK, which is good cause that is what POSIX | |
558 | has, and the flag will already be cleared by the time we get here. */ | |
559 | if (flags & O_NDELAY) | |
560 | printf_filtered (" | O_NDELAY"); | |
561 | flags &= ~O_NDELAY; | |
562 | #endif | |
563 | ||
564 | if (flags & O_APPEND) | |
565 | printf_filtered (" | O_APPEND"); | |
566 | flags &= ~O_APPEND; | |
567 | ||
568 | #if defined (O_BINARY) | |
569 | if (flags & O_BINARY) | |
570 | printf_filtered (" | O_BINARY"); | |
571 | flags &= ~O_BINARY; | |
572 | #endif | |
573 | ||
574 | if (flags) | |
575 | printf_filtered (" | 0x%x", flags); | |
576 | printf_filtered ("\n"); | |
577 | } | |
578 | ||
579 | #ifdef PROCESS_GROUP_TYPE | |
2acceee2 | 580 | printf_filtered ("Process group = %d\n", |
7e1789f5 | 581 | (int) inf->terminal_info->process_group); |
c906108c SS |
582 | #endif |
583 | ||
7e1789f5 PA |
584 | serial_print_tty_state (stdin_serial, |
585 | inf->terminal_info->ttystate, | |
586 | gdb_stdout); | |
c906108c SS |
587 | } |
588 | \f | |
589 | /* NEW_TTY_PREFORK is called before forking a new child process, | |
590 | so we can record the state of ttys in the child to be formed. | |
591 | TTYNAME is null if we are to share the terminal with gdb; | |
592 | or points to a string containing the name of the desired tty. | |
593 | ||
594 | NEW_TTY is called in new child processes under Unix, which will | |
595 | become debugger target processes. This actually switches to | |
596 | the terminal specified in the NEW_TTY_PREFORK call. */ | |
597 | ||
598 | void | |
3cb3b8df | 599 | new_tty_prefork (const char *ttyname) |
c906108c SS |
600 | { |
601 | /* Save the name for later, for determining whether we and the child | |
602 | are sharing a tty. */ | |
603 | inferior_thisrun_terminal = ttyname; | |
604 | } | |
605 | ||
bf1d7d9c JB |
606 | |
607 | /* If RESULT, assumed to be the return value from a system call, is | |
608 | negative, print the error message indicated by errno and exit. | |
609 | MSG should identify the operation that failed. */ | |
610 | static void | |
611 | check_syscall (const char *msg, int result) | |
612 | { | |
613 | if (result < 0) | |
614 | { | |
615 | print_sys_errmsg (msg, errno); | |
616 | _exit (1); | |
617 | } | |
618 | } | |
619 | ||
c906108c | 620 | void |
fba45db2 | 621 | new_tty (void) |
c906108c | 622 | { |
52f0bd74 | 623 | int tty; |
c906108c SS |
624 | |
625 | if (inferior_thisrun_terminal == 0) | |
626 | return; | |
627 | #if !defined(__GO32__) && !defined(_WIN32) | |
628 | #ifdef TIOCNOTTY | |
629 | /* Disconnect the child process from our controlling terminal. On some | |
630 | systems (SVR4 for example), this may cause a SIGTTOU, so temporarily | |
631 | ignore SIGTTOU. */ | |
c5aa993b | 632 | tty = open ("/dev/tty", O_RDWR); |
c906108c SS |
633 | if (tty > 0) |
634 | { | |
635 | void (*osigttou) (); | |
636 | ||
c5aa993b JM |
637 | osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN); |
638 | ioctl (tty, TIOCNOTTY, 0); | |
639 | close (tty); | |
640 | signal (SIGTTOU, osigttou); | |
c906108c SS |
641 | } |
642 | #endif | |
643 | ||
644 | /* Now open the specified new terminal. */ | |
c5aa993b | 645 | tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY); |
bf1d7d9c | 646 | check_syscall (inferior_thisrun_terminal, tty); |
c906108c SS |
647 | |
648 | /* Avoid use of dup2; doesn't exist on all systems. */ | |
649 | if (tty != 0) | |
c5aa993b JM |
650 | { |
651 | close (0); | |
bf1d7d9c | 652 | check_syscall ("dup'ing tty into fd 0", dup (tty)); |
c5aa993b | 653 | } |
c906108c | 654 | if (tty != 1) |
c5aa993b JM |
655 | { |
656 | close (1); | |
bf1d7d9c | 657 | check_syscall ("dup'ing tty into fd 1", dup (tty)); |
c5aa993b | 658 | } |
c906108c | 659 | if (tty != 2) |
c5aa993b JM |
660 | { |
661 | close (2); | |
bf1d7d9c | 662 | check_syscall ("dup'ing tty into fd 2", dup (tty)); |
c5aa993b | 663 | } |
83116857 TJB |
664 | |
665 | #ifdef TIOCSCTTY | |
666 | /* Make tty our new controlling terminal. */ | |
667 | if (ioctl (tty, TIOCSCTTY, 0) == -1) | |
668 | /* Mention GDB in warning because it will appear in the inferior's | |
669 | terminal instead of GDB's. */ | |
670 | warning ("GDB: Failed to set controlling terminal: %s", | |
671 | safe_strerror (errno)); | |
672 | #endif | |
673 | ||
c906108c | 674 | if (tty > 2) |
c5aa993b JM |
675 | close (tty); |
676 | #endif /* !go32 && !win32 */ | |
c906108c | 677 | } |
191c4426 PA |
678 | |
679 | /* NEW_TTY_POSTFORK is called after forking a new child process, and | |
680 | adding it to the inferior table, to store the TTYNAME being used by | |
681 | the child, or null if it sharing the terminal with gdb. */ | |
682 | ||
683 | void | |
684 | new_tty_postfork (void) | |
685 | { | |
686 | /* Save the name for later, for determining whether we and the child | |
687 | are sharing a tty. */ | |
688 | ||
689 | if (inferior_thisrun_terminal) | |
690 | current_inferior ()->terminal_info->run_terminal | |
691 | = xstrdup (inferior_thisrun_terminal); | |
692 | ||
693 | inferior_thisrun_terminal = NULL; | |
694 | } | |
695 | ||
c906108c SS |
696 | \f |
697 | /* Call set_sigint_trap when you need to pass a signal on to an attached | |
698 | process when handling SIGINT */ | |
699 | ||
c906108c | 700 | static void |
fba45db2 | 701 | pass_signal (int signo) |
c906108c SS |
702 | { |
703 | #ifndef _WIN32 | |
39f77062 | 704 | kill (PIDGET (inferior_ptid), SIGINT); |
c906108c SS |
705 | #endif |
706 | } | |
707 | ||
c5aa993b | 708 | static void (*osig) (); |
7e1789f5 | 709 | static int osig_set; |
c906108c SS |
710 | |
711 | void | |
fba45db2 | 712 | set_sigint_trap (void) |
c906108c | 713 | { |
181e7f93 | 714 | struct inferior *inf = current_inferior (); |
7e1789f5 | 715 | if (inf->attach_flag || inf->terminal_info->run_terminal) |
c906108c | 716 | { |
c5aa993b | 717 | osig = (void (*)()) signal (SIGINT, pass_signal); |
7e1789f5 | 718 | osig_set = 1; |
c906108c | 719 | } |
7e1789f5 PA |
720 | else |
721 | osig_set = 0; | |
c906108c SS |
722 | } |
723 | ||
724 | void | |
fba45db2 | 725 | clear_sigint_trap (void) |
c906108c | 726 | { |
7e1789f5 | 727 | if (osig_set) |
c906108c SS |
728 | { |
729 | signal (SIGINT, osig); | |
7e1789f5 | 730 | osig_set = 0; |
c906108c SS |
731 | } |
732 | } | |
733 | \f | |
c906108c | 734 | |
83116857 TJB |
735 | /* Create a new session if the inferior will run in a different tty. |
736 | A session is UNIX's way of grouping processes that share a controlling | |
737 | terminal, so a new one is needed if the inferior terminal will be | |
738 | different from GDB's. | |
739 | ||
740 | Returns the session id of the new session, 0 if no session was created | |
741 | or -1 if an error occurred. */ | |
742 | pid_t | |
743 | create_tty_session (void) | |
744 | { | |
745 | #ifdef HAVE_SETSID | |
746 | pid_t ret; | |
747 | ||
748 | if (!job_control || inferior_thisrun_terminal == 0) | |
749 | return 0; | |
750 | ||
751 | ret = setsid (); | |
752 | if (ret == -1) | |
753 | warning ("Failed to create new terminal session: setsid: %s", | |
754 | safe_strerror (errno)); | |
755 | ||
756 | return ret; | |
757 | #else | |
758 | return 0; | |
759 | #endif /* HAVE_SETSID */ | |
760 | } | |
761 | ||
c906108c SS |
762 | /* This is here because this is where we figure out whether we (probably) |
763 | have job control. Just using job_control only does part of it because | |
764 | setpgid or setpgrp might not exist on a system without job control. | |
765 | It might be considered misplaced (on the other hand, process groups and | |
766 | job control are closely related to ttys). | |
767 | ||
768 | For a more clean implementation, in libiberty, put a setpgid which merely | |
769 | calls setpgrp and a setpgrp which does nothing (any system with job control | |
770 | will have one or the other). */ | |
771 | int | |
fba45db2 | 772 | gdb_setpgid (void) |
c906108c SS |
773 | { |
774 | int retval = 0; | |
775 | ||
776 | if (job_control) | |
777 | { | |
0200359f MK |
778 | #if defined (HAVE_TERMIOS) || defined (TIOCGPGRP) |
779 | #ifdef HAVE_SETPGID | |
780 | /* The call setpgid (0, 0) is supposed to work and mean the same | |
781 | thing as this, but on Ultrix 4.2A it fails with EPERM (and | |
c5aa993b | 782 | setpgid (getpid (), getpid ()) succeeds). */ |
c906108c SS |
783 | retval = setpgid (getpid (), getpid ()); |
784 | #else | |
0200359f MK |
785 | #ifdef HAVE_SETPGRP |
786 | #ifdef SETPGRP_VOID | |
c906108c SS |
787 | retval = setpgrp (); |
788 | #else | |
789 | retval = setpgrp (getpid (), getpid ()); | |
0200359f MK |
790 | #endif |
791 | #endif /* HAVE_SETPGRP */ | |
792 | #endif /* HAVE_SETPGID */ | |
793 | #endif /* defined (HAVE_TERMIOS) || defined (TIOCGPGRP) */ | |
c906108c | 794 | } |
0200359f | 795 | |
c906108c SS |
796 | return retval; |
797 | } | |
798 | ||
0ea3f30e DJ |
799 | /* Get all the current tty settings (including whether we have a |
800 | tty at all!). We can't do this in _initialize_inflow because | |
801 | serial_fdopen() won't work until the serial_ops_list is | |
802 | initialized, but we don't want to do it lazily either, so | |
803 | that we can guarantee stdin_serial is opened if there is | |
804 | a terminal. */ | |
805 | void | |
806 | initialize_stdin_serial (void) | |
807 | { | |
808 | stdin_serial = serial_fdopen (0); | |
809 | } | |
810 | ||
c906108c | 811 | void |
fba45db2 | 812 | _initialize_inflow (void) |
c906108c SS |
813 | { |
814 | add_info ("terminal", term_info, | |
1bedd215 | 815 | _("Print inferior's saved terminal status.")); |
c906108c | 816 | |
c906108c SS |
817 | terminal_is_ours = 1; |
818 | ||
819 | /* OK, figure out whether we have job control. If neither termios nor | |
820 | sgtty (i.e. termio or go32), leave job_control 0. */ | |
821 | ||
822 | #if defined (HAVE_TERMIOS) | |
823 | /* Do all systems with termios have the POSIX way of identifying job | |
824 | control? I hope so. */ | |
825 | #ifdef _POSIX_JOB_CONTROL | |
826 | job_control = 1; | |
827 | #else | |
828 | #ifdef _SC_JOB_CONTROL | |
829 | job_control = sysconf (_SC_JOB_CONTROL); | |
830 | #else | |
c5aa993b JM |
831 | job_control = 0; /* have to assume the worst */ |
832 | #endif /* _SC_JOB_CONTROL */ | |
833 | #endif /* _POSIX_JOB_CONTROL */ | |
834 | #endif /* HAVE_TERMIOS */ | |
c906108c SS |
835 | |
836 | #ifdef HAVE_SGTTY | |
837 | #ifdef TIOCGPGRP | |
838 | job_control = 1; | |
839 | #else | |
840 | job_control = 0; | |
841 | #endif /* TIOCGPGRP */ | |
842 | #endif /* sgtty */ | |
7e1789f5 PA |
843 | |
844 | observer_attach_new_inferior (inflow_new_inferior); | |
845 | observer_attach_inferior_exit (inflow_inferior_exit); | |
c906108c | 846 | } |