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