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