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