]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Low level interface to ptrace, for GDB when running under Unix. |
61baf725 | 2 | Copyright (C) 1986-2017 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" |
726e1356 PA |
34 | #ifdef HAVE_TERMIOS_H |
35 | #include <termios.h> | |
36 | #endif | |
15652511 | 37 | #include "job-control.h" |
c906108c | 38 | |
c2d11a7d JM |
39 | #ifdef HAVE_SYS_IOCTL_H |
40 | #include <sys/ioctl.h> | |
41 | #endif | |
42 | ||
f2acbe1c MK |
43 | #ifndef O_NOCTTY |
44 | #define O_NOCTTY 0 | |
45 | #endif | |
46 | ||
a14ed312 | 47 | static void pass_signal (int); |
c906108c | 48 | |
d6b64346 | 49 | static void child_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 | ||
726e1356 | 69 | #ifdef HAVE_TERMIOS_H |
7e1789f5 | 70 | /* Process group. Saved and restored just like ttystate. */ |
726e1356 | 71 | pid_t process_group; |
7e1789f5 | 72 | #endif |
c906108c | 73 | |
7e1789f5 PA |
74 | /* fcntl flags. Saved and restored just like ttystate. */ |
75 | int tflags; | |
76 | }; | |
c906108c | 77 | |
7e1789f5 | 78 | /* Our own tty state, which we restore every time we need to deal with |
d9de1fe3 PA |
79 | the terminal. This is set once, when GDB first starts, and then |
80 | whenever we enter/leave TUI mode (gdb_save_tty_state). The | |
81 | settings of flags which readline saves and restores are | |
7e1789f5 PA |
82 | unimportant. */ |
83 | static struct terminal_info our_terminal_info; | |
c906108c | 84 | |
d9de1fe3 PA |
85 | /* Snapshot of the initial tty state taken during initialization of |
86 | GDB, before readline/ncurses have had a chance to change it. This | |
87 | is used as the initial tty state given to each new spawned | |
88 | inferior. Unlike our_terminal_info, this is only ever set | |
89 | once. */ | |
6a06d660 PP |
90 | static serial_ttystate initial_gdb_ttystate; |
91 | ||
6c95b8df PA |
92 | static struct terminal_info *get_inflow_inferior_data (struct inferior *); |
93 | ||
726e1356 | 94 | #ifdef HAVE_TERMIOS_H |
7e1789f5 PA |
95 | |
96 | /* Return the process group of the current inferior. */ | |
97 | ||
726e1356 | 98 | pid_t |
7e1789f5 PA |
99 | inferior_process_group (void) |
100 | { | |
6c95b8df | 101 | return get_inflow_inferior_data (current_inferior ())->process_group; |
7e1789f5 | 102 | } |
c906108c SS |
103 | #endif |
104 | ||
105 | /* While the inferior is running, we want SIGINT and SIGQUIT to go to the | |
106 | inferior only. If we have job control, that takes care of it. If not, | |
107 | we save our handlers in these two variables and set SIGINT and SIGQUIT | |
108 | to SIG_IGN. */ | |
109 | ||
a40805d4 PA |
110 | static sighandler_t sigint_ours; |
111 | static sighandler_t sigquit_ours; | |
c906108c | 112 | |
7e1789f5 PA |
113 | /* The name of the tty (from the `tty' command) that we're giving to |
114 | the inferior when starting it up. This is only (and should only | |
191c4426 PA |
115 | be) used as a transient global by new_tty_prefork, |
116 | create_tty_session, new_tty and new_tty_postfork, all called from | |
117 | fork_inferior, while forking a new child. */ | |
3cb3b8df | 118 | static const char *inferior_thisrun_terminal; |
c906108c SS |
119 | |
120 | /* Nonzero if our terminal settings are in effect. Zero if the | |
121 | inferior's settings are in effect. Ignored if !gdb_has_a_terminal | |
122 | (). */ | |
123 | ||
124 | int terminal_is_ours; | |
125 | ||
d9de1fe3 | 126 | /* See terminal.h. */ |
ea42d6f8 | 127 | |
6a06d660 PP |
128 | void |
129 | set_initial_gdb_ttystate (void) | |
130 | { | |
d9de1fe3 PA |
131 | /* Note we can't do any of this in _initialize_inflow because at |
132 | that point stdin_serial has not been created yet. */ | |
133 | ||
6a06d660 | 134 | initial_gdb_ttystate = serial_get_tty_state (stdin_serial); |
6a06d660 | 135 | |
d9de1fe3 | 136 | if (initial_gdb_ttystate != NULL) |
c906108c | 137 | { |
d9de1fe3 PA |
138 | our_terminal_info.ttystate |
139 | = serial_copy_tty_state (stdin_serial, initial_gdb_ttystate); | |
c906108c | 140 | #ifdef F_GETFL |
7e1789f5 | 141 | our_terminal_info.tflags = fcntl (0, F_GETFL, 0); |
c906108c | 142 | #endif |
726e1356 PA |
143 | #ifdef HAVE_TERMIOS_H |
144 | our_terminal_info.process_group = tcgetpgrp (0); | |
807bddf3 | 145 | #endif |
c906108c SS |
146 | } |
147 | } | |
148 | ||
d9de1fe3 PA |
149 | /* Does GDB have a terminal (on stdin)? */ |
150 | ||
151 | static int | |
152 | gdb_has_a_terminal (void) | |
153 | { | |
154 | return initial_gdb_ttystate != NULL; | |
155 | } | |
156 | ||
c906108c SS |
157 | /* Macro for printing errors from ioctl operations */ |
158 | ||
159 | #define OOPSY(what) \ | |
160 | if (result == -1) \ | |
161 | fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \ | |
dc672865 | 162 | what, safe_strerror (errno)) |
c906108c | 163 | |
c906108c SS |
164 | /* Initialize the terminal settings we record for the inferior, |
165 | before we actually run the inferior. */ | |
166 | ||
167 | void | |
556e5da5 | 168 | child_terminal_init (struct target_ops *self) |
c906108c | 169 | { |
6f64ef53 PA |
170 | struct inferior *inf = current_inferior (); |
171 | struct terminal_info *tinfo = get_inflow_inferior_data (inf); | |
172 | ||
726e1356 | 173 | #ifdef HAVE_TERMIOS_H |
6f64ef53 PA |
174 | /* Store the process group even without a terminal as it is used not |
175 | only to reset the tty foreground process group, but also to | |
556e5da5 PA |
176 | interrupt the inferior. A child we spawn should be a process |
177 | group leader (PGID==PID) at this point, though that may not be | |
178 | true if we're attaching to an existing process. */ | |
179 | tinfo->process_group = inf->pid; | |
6f64ef53 PA |
180 | #endif |
181 | ||
c906108c SS |
182 | if (gdb_has_a_terminal ()) |
183 | { | |
6c95b8df | 184 | xfree (tinfo->ttystate); |
1e182ce8 | 185 | tinfo->ttystate = serial_copy_tty_state (stdin_serial, |
6a06d660 | 186 | initial_gdb_ttystate); |
c906108c | 187 | |
c906108c | 188 | /* Make sure that next time we call terminal_inferior (which will be |
c5aa993b JM |
189 | before the program runs, as it needs to be), we install the new |
190 | process group. */ | |
c906108c SS |
191 | terminal_is_ours = 1; |
192 | } | |
193 | } | |
194 | ||
a790ad35 SC |
195 | /* Save the terminal settings again. This is necessary for the TUI |
196 | when it switches to TUI or non-TUI mode; curses changes the terminal | |
197 | and gdb must be able to restore it correctly. */ | |
198 | ||
199 | void | |
3278a9f5 | 200 | gdb_save_tty_state (void) |
a790ad35 SC |
201 | { |
202 | if (gdb_has_a_terminal ()) | |
203 | { | |
7e1789f5 PA |
204 | xfree (our_terminal_info.ttystate); |
205 | our_terminal_info.ttystate = serial_get_tty_state (stdin_serial); | |
a790ad35 SC |
206 | } |
207 | } | |
208 | ||
c906108c | 209 | /* Put the inferior's terminal settings into effect. |
4d4ca2a1 DE |
210 | This is preparation for starting or resuming the inferior. |
211 | ||
212 | N.B. Targets that want to use this with async support must build that | |
213 | support on top of this (e.g., the caller still needs to remove stdin | |
214 | from the event loop). E.g., see linux_nat_terminal_inferior. */ | |
c906108c SS |
215 | |
216 | void | |
d6b64346 | 217 | child_terminal_inferior (struct target_ops *self) |
c906108c | 218 | { |
7e1789f5 | 219 | struct inferior *inf; |
6c95b8df | 220 | struct terminal_info *tinfo; |
7e1789f5 PA |
221 | |
222 | if (!terminal_is_ours) | |
223 | return; | |
224 | ||
225 | inf = current_inferior (); | |
6c95b8df | 226 | tinfo = get_inflow_inferior_data (inf); |
7e1789f5 PA |
227 | |
228 | if (gdb_has_a_terminal () | |
6c95b8df PA |
229 | && tinfo->ttystate != NULL |
230 | && tinfo->run_terminal == NULL) | |
c906108c SS |
231 | { |
232 | int result; | |
233 | ||
234 | #ifdef F_GETFL | |
6c95b8df | 235 | result = fcntl (0, F_SETFL, tinfo->tflags); |
c906108c SS |
236 | OOPSY ("fcntl F_SETFL"); |
237 | #endif | |
238 | ||
726e1356 | 239 | result = serial_set_tty_state (stdin_serial, tinfo->ttystate); |
c906108c SS |
240 | OOPSY ("setting tty state"); |
241 | ||
242 | if (!job_control) | |
243 | { | |
a40805d4 | 244 | sigint_ours = signal (SIGINT, SIG_IGN); |
c906108c | 245 | #ifdef SIGQUIT |
a40805d4 | 246 | sigquit_ours = signal (SIGQUIT, SIG_IGN); |
c906108c SS |
247 | #endif |
248 | } | |
249 | ||
250 | /* If attach_flag is set, we don't know whether we are sharing a | |
c5aa993b JM |
251 | terminal with the inferior or not. (attaching a process |
252 | without a terminal is one case where we do not; attaching a | |
253 | process which we ran from the same shell as GDB via `&' is | |
254 | one case where we do, I think (but perhaps this is not | |
255 | `sharing' in the sense that we need to save and restore tty | |
256 | state)). I don't know if there is any way to tell whether we | |
257 | are sharing a terminal. So what we do is to go through all | |
258 | the saving and restoring of the tty state, but ignore errors | |
259 | setting the process group, which will happen if we are not | |
260 | sharing a terminal). */ | |
c906108c SS |
261 | |
262 | if (job_control) | |
263 | { | |
726e1356 | 264 | #ifdef HAVE_TERMIOS_H |
6c95b8df | 265 | result = tcsetpgrp (0, tinfo->process_group); |
181e7f93 | 266 | if (!inf->attach_flag) |
c906108c SS |
267 | OOPSY ("tcsetpgrp"); |
268 | #endif | |
c906108c | 269 | } |
c906108c SS |
270 | } |
271 | terminal_is_ours = 0; | |
272 | } | |
273 | ||
274 | /* Put some of our terminal settings into effect, | |
275 | enough to get proper results from our output, | |
276 | but do not change into or out of RAW mode | |
277 | so that no input is discarded. | |
278 | ||
279 | After doing this, either terminal_ours or terminal_inferior | |
4d4ca2a1 DE |
280 | should be called to get back to a normal state of affairs. |
281 | ||
282 | N.B. The implementation is (currently) no different than | |
283 | child_terminal_ours. See child_terminal_ours_1. */ | |
c906108c SS |
284 | |
285 | void | |
d6b64346 | 286 | child_terminal_ours_for_output (struct target_ops *self) |
c906108c | 287 | { |
d6b64346 | 288 | child_terminal_ours_1 (1); |
c906108c SS |
289 | } |
290 | ||
291 | /* Put our terminal settings into effect. | |
292 | First record the inferior's terminal settings | |
4d4ca2a1 DE |
293 | so they can be restored properly later. |
294 | ||
295 | N.B. Targets that want to use this with async support must build that | |
296 | support on top of this (e.g., the caller still needs to add stdin to the | |
297 | event loop). E.g., see linux_nat_terminal_ours. */ | |
c906108c SS |
298 | |
299 | void | |
d6b64346 | 300 | child_terminal_ours (struct target_ops *self) |
c906108c | 301 | { |
d6b64346 | 302 | child_terminal_ours_1 (0); |
c906108c SS |
303 | } |
304 | ||
305 | /* output_only is not used, and should not be used unless we introduce | |
306 | separate terminal_is_ours and terminal_is_ours_for_output | |
307 | flags. */ | |
308 | ||
309 | static void | |
d6b64346 | 310 | child_terminal_ours_1 (int output_only) |
c906108c | 311 | { |
7e1789f5 | 312 | struct inferior *inf; |
6c95b8df | 313 | struct terminal_info *tinfo; |
7e1789f5 PA |
314 | |
315 | if (terminal_is_ours) | |
316 | return; | |
317 | ||
d9d2d8b6 PA |
318 | terminal_is_ours = 1; |
319 | ||
7e1789f5 | 320 | /* Checking inferior->run_terminal is necessary so that |
c906108c SS |
321 | if GDB is running in the background, it won't block trying |
322 | to do the ioctl()'s below. Checking gdb_has_a_terminal | |
323 | avoids attempting all the ioctl's when running in batch. */ | |
7e1789f5 PA |
324 | |
325 | inf = current_inferior (); | |
6c95b8df | 326 | tinfo = get_inflow_inferior_data (inf); |
7e1789f5 | 327 | |
6c95b8df | 328 | if (tinfo->run_terminal != NULL || gdb_has_a_terminal () == 0) |
c906108c | 329 | return; |
a579cd9a | 330 | else |
c906108c | 331 | { |
4b69c284 | 332 | #ifdef SIGTTOU |
c906108c | 333 | /* Ignore this signal since it will happen when we try to set the |
c5aa993b | 334 | pgrp. */ |
a40805d4 | 335 | sighandler_t osigttou = NULL; |
4b69c284 | 336 | #endif |
821fc4ae | 337 | int result ATTRIBUTE_UNUSED; |
c906108c | 338 | |
c906108c SS |
339 | #ifdef SIGTTOU |
340 | if (job_control) | |
a40805d4 | 341 | osigttou = signal (SIGTTOU, SIG_IGN); |
c906108c SS |
342 | #endif |
343 | ||
6c95b8df PA |
344 | xfree (tinfo->ttystate); |
345 | tinfo->ttystate = serial_get_tty_state (stdin_serial); | |
64a0ac84 | 346 | |
726e1356 | 347 | #ifdef HAVE_TERMIOS_H |
181e7f93 | 348 | if (!inf->attach_flag) |
726e1356 | 349 | /* If tcsetpgrp failed in terminal_inferior, this would give us |
64a0ac84 PA |
350 | our process group instead of the inferior's. See |
351 | terminal_inferior for details. */ | |
726e1356 | 352 | tinfo->process_group = tcgetpgrp (0); |
49a834f9 | 353 | #endif |
c906108c | 354 | |
726e1356 PA |
355 | /* Set tty state to our_ttystate. */ |
356 | serial_set_tty_state (stdin_serial, our_terminal_info.ttystate); | |
c906108c SS |
357 | |
358 | if (job_control) | |
359 | { | |
726e1356 | 360 | #ifdef HAVE_TERMIOS_H |
7e1789f5 | 361 | result = tcsetpgrp (0, our_terminal_info.process_group); |
c906108c SS |
362 | #if 0 |
363 | /* This fails on Ultrix with EINVAL if you run the testsuite | |
364 | in the background with nohup, and then log out. GDB never | |
365 | used to check for an error here, so perhaps there are other | |
366 | such situations as well. */ | |
367 | if (result == -1) | |
3e43a32a | 368 | fprintf_unfiltered (gdb_stderr, |
d6b64346 | 369 | "[tcsetpgrp failed in child_terminal_ours: %s]\n", |
dc672865 | 370 | safe_strerror (errno)); |
c906108c SS |
371 | #endif |
372 | #endif /* termios */ | |
c906108c SS |
373 | } |
374 | ||
375 | #ifdef SIGTTOU | |
376 | if (job_control) | |
377 | signal (SIGTTOU, osigttou); | |
378 | #endif | |
379 | ||
380 | if (!job_control) | |
381 | { | |
382 | signal (SIGINT, sigint_ours); | |
383 | #ifdef SIGQUIT | |
384 | signal (SIGQUIT, sigquit_ours); | |
385 | #endif | |
386 | } | |
387 | ||
388 | #ifdef F_GETFL | |
6c95b8df | 389 | tinfo->tflags = fcntl (0, F_GETFL, 0); |
7e1789f5 | 390 | result = fcntl (0, F_SETFL, our_terminal_info.tflags); |
c906108c | 391 | #endif |
c906108c SS |
392 | } |
393 | } | |
394 | ||
6c95b8df PA |
395 | /* Per-inferior data key. */ |
396 | static const struct inferior_data *inflow_inferior_data; | |
7e1789f5 PA |
397 | |
398 | static void | |
6c95b8df | 399 | inflow_inferior_data_cleanup (struct inferior *inf, void *arg) |
7e1789f5 | 400 | { |
9a3c8263 | 401 | struct terminal_info *info = (struct terminal_info *) arg; |
6c95b8df | 402 | |
487ad57c YQ |
403 | xfree (info->run_terminal); |
404 | xfree (info->ttystate); | |
405 | xfree (info); | |
6c95b8df PA |
406 | } |
407 | ||
408 | /* Get the current svr4 data. If none is found yet, add it now. This | |
409 | function always returns a valid object. */ | |
410 | ||
411 | static struct terminal_info * | |
412 | get_inflow_inferior_data (struct inferior *inf) | |
413 | { | |
414 | struct terminal_info *info; | |
415 | ||
9a3c8263 | 416 | info = (struct terminal_info *) inferior_data (inf, inflow_inferior_data); |
6c95b8df PA |
417 | if (info == NULL) |
418 | { | |
41bf6aca | 419 | info = XCNEW (struct terminal_info); |
6c95b8df PA |
420 | set_inferior_data (inf, inflow_inferior_data, info); |
421 | } | |
7e1789f5 | 422 | |
6c95b8df | 423 | return info; |
7e1789f5 PA |
424 | } |
425 | ||
426 | /* This is a "inferior_exit" observer. Releases the TERMINAL_INFO member | |
427 | of the inferior structure. This field is private to inflow.c, and | |
428 | its type is opaque to the rest of GDB. PID is the target pid of | |
429 | the inferior that is about to be removed from the inferior | |
430 | list. */ | |
431 | ||
432 | static void | |
a79b8f6e | 433 | inflow_inferior_exit (struct inferior *inf) |
7e1789f5 | 434 | { |
6c95b8df | 435 | struct terminal_info *info; |
7e1789f5 | 436 | |
9a3c8263 | 437 | info = (struct terminal_info *) inferior_data (inf, inflow_inferior_data); |
6c95b8df PA |
438 | if (info != NULL) |
439 | { | |
440 | xfree (info->run_terminal); | |
1e182ce8 | 441 | xfree (info->ttystate); |
6c95b8df PA |
442 | xfree (info); |
443 | set_inferior_data (inf, inflow_inferior_data, NULL); | |
444 | } | |
7e1789f5 PA |
445 | } |
446 | ||
191c4426 PA |
447 | void |
448 | copy_terminal_info (struct inferior *to, struct inferior *from) | |
449 | { | |
6c95b8df PA |
450 | struct terminal_info *tinfo_to, *tinfo_from; |
451 | ||
452 | tinfo_to = get_inflow_inferior_data (to); | |
453 | tinfo_from = get_inflow_inferior_data (from); | |
1e182ce8 UW |
454 | |
455 | xfree (tinfo_to->run_terminal); | |
456 | xfree (tinfo_to->ttystate); | |
457 | ||
6c95b8df | 458 | *tinfo_to = *tinfo_from; |
1e182ce8 | 459 | |
6c95b8df PA |
460 | if (tinfo_from->run_terminal) |
461 | tinfo_to->run_terminal | |
462 | = xstrdup (tinfo_from->run_terminal); | |
1e182ce8 UW |
463 | |
464 | if (tinfo_from->ttystate) | |
465 | tinfo_to->ttystate | |
466 | = serial_copy_tty_state (stdin_serial, tinfo_from->ttystate); | |
191c4426 PA |
467 | } |
468 | ||
c906108c | 469 | void |
1d12d88f | 470 | info_terminal_command (const char *arg, int from_tty) |
c906108c | 471 | { |
223ffa71 | 472 | target_terminal::info (arg, from_tty); |
c906108c SS |
473 | } |
474 | ||
c906108c | 475 | void |
0a4f40a2 | 476 | child_terminal_info (struct target_ops *self, const char *args, int from_tty) |
c906108c | 477 | { |
7e1789f5 | 478 | struct inferior *inf; |
6c95b8df | 479 | struct terminal_info *tinfo; |
7e1789f5 | 480 | |
c906108c SS |
481 | if (!gdb_has_a_terminal ()) |
482 | { | |
a3f17187 | 483 | printf_filtered (_("This GDB does not control a terminal.\n")); |
c906108c SS |
484 | return; |
485 | } | |
486 | ||
7e1789f5 PA |
487 | if (ptid_equal (inferior_ptid, null_ptid)) |
488 | return; | |
489 | ||
490 | inf = current_inferior (); | |
6c95b8df | 491 | tinfo = get_inflow_inferior_data (inf); |
7e1789f5 | 492 | |
3e43a32a MS |
493 | printf_filtered (_("Inferior's terminal status " |
494 | "(currently saved by GDB):\n")); | |
c906108c SS |
495 | |
496 | /* First the fcntl flags. */ | |
497 | { | |
498 | int flags; | |
c5aa993b | 499 | |
6c95b8df | 500 | flags = tinfo->tflags; |
c906108c SS |
501 | |
502 | printf_filtered ("File descriptor flags = "); | |
503 | ||
504 | #ifndef O_ACCMODE | |
505 | #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR) | |
506 | #endif | |
1777feb0 | 507 | /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */ |
c906108c SS |
508 | switch (flags & (O_ACCMODE)) |
509 | { | |
c5aa993b JM |
510 | case O_RDONLY: |
511 | printf_filtered ("O_RDONLY"); | |
512 | break; | |
513 | case O_WRONLY: | |
514 | printf_filtered ("O_WRONLY"); | |
515 | break; | |
516 | case O_RDWR: | |
517 | printf_filtered ("O_RDWR"); | |
518 | break; | |
c906108c SS |
519 | } |
520 | flags &= ~(O_ACCMODE); | |
521 | ||
522 | #ifdef O_NONBLOCK | |
c5aa993b | 523 | if (flags & O_NONBLOCK) |
c906108c SS |
524 | printf_filtered (" | O_NONBLOCK"); |
525 | flags &= ~O_NONBLOCK; | |
526 | #endif | |
c5aa993b | 527 | |
c906108c SS |
528 | #if defined (O_NDELAY) |
529 | /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will | |
530 | print it as O_NONBLOCK, which is good cause that is what POSIX | |
531 | has, and the flag will already be cleared by the time we get here. */ | |
532 | if (flags & O_NDELAY) | |
533 | printf_filtered (" | O_NDELAY"); | |
534 | flags &= ~O_NDELAY; | |
535 | #endif | |
536 | ||
537 | if (flags & O_APPEND) | |
538 | printf_filtered (" | O_APPEND"); | |
539 | flags &= ~O_APPEND; | |
540 | ||
541 | #if defined (O_BINARY) | |
542 | if (flags & O_BINARY) | |
543 | printf_filtered (" | O_BINARY"); | |
544 | flags &= ~O_BINARY; | |
545 | #endif | |
546 | ||
547 | if (flags) | |
548 | printf_filtered (" | 0x%x", flags); | |
549 | printf_filtered ("\n"); | |
550 | } | |
551 | ||
726e1356 | 552 | #ifdef HAVE_TERMIOS_H |
6c95b8df | 553 | printf_filtered ("Process group = %d\n", (int) tinfo->process_group); |
c906108c SS |
554 | #endif |
555 | ||
6c95b8df | 556 | serial_print_tty_state (stdin_serial, tinfo->ttystate, gdb_stdout); |
c906108c SS |
557 | } |
558 | \f | |
559 | /* NEW_TTY_PREFORK is called before forking a new child process, | |
560 | so we can record the state of ttys in the child to be formed. | |
561 | TTYNAME is null if we are to share the terminal with gdb; | |
562 | or points to a string containing the name of the desired tty. | |
563 | ||
564 | NEW_TTY is called in new child processes under Unix, which will | |
565 | become debugger target processes. This actually switches to | |
566 | the terminal specified in the NEW_TTY_PREFORK call. */ | |
567 | ||
568 | void | |
3cb3b8df | 569 | new_tty_prefork (const char *ttyname) |
c906108c SS |
570 | { |
571 | /* Save the name for later, for determining whether we and the child | |
572 | are sharing a tty. */ | |
573 | inferior_thisrun_terminal = ttyname; | |
574 | } | |
575 | ||
e6d088ec | 576 | #if !defined(__GO32__) && !defined(_WIN32) |
bf1d7d9c JB |
577 | /* If RESULT, assumed to be the return value from a system call, is |
578 | negative, print the error message indicated by errno and exit. | |
579 | MSG should identify the operation that failed. */ | |
580 | static void | |
581 | check_syscall (const char *msg, int result) | |
582 | { | |
583 | if (result < 0) | |
584 | { | |
585 | print_sys_errmsg (msg, errno); | |
586 | _exit (1); | |
587 | } | |
588 | } | |
e6d088ec | 589 | #endif |
bf1d7d9c | 590 | |
c906108c | 591 | void |
fba45db2 | 592 | new_tty (void) |
c906108c | 593 | { |
52f0bd74 | 594 | int tty; |
c906108c SS |
595 | |
596 | if (inferior_thisrun_terminal == 0) | |
597 | return; | |
598 | #if !defined(__GO32__) && !defined(_WIN32) | |
599 | #ifdef TIOCNOTTY | |
600 | /* Disconnect the child process from our controlling terminal. On some | |
601 | systems (SVR4 for example), this may cause a SIGTTOU, so temporarily | |
1777feb0 | 602 | ignore SIGTTOU. */ |
c5aa993b | 603 | tty = open ("/dev/tty", O_RDWR); |
c906108c SS |
604 | if (tty > 0) |
605 | { | |
a40805d4 | 606 | sighandler_t osigttou; |
c906108c | 607 | |
a40805d4 | 608 | osigttou = signal (SIGTTOU, SIG_IGN); |
c5aa993b JM |
609 | ioctl (tty, TIOCNOTTY, 0); |
610 | close (tty); | |
611 | signal (SIGTTOU, osigttou); | |
c906108c SS |
612 | } |
613 | #endif | |
614 | ||
615 | /* Now open the specified new terminal. */ | |
c5aa993b | 616 | tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY); |
bf1d7d9c | 617 | check_syscall (inferior_thisrun_terminal, tty); |
c906108c SS |
618 | |
619 | /* Avoid use of dup2; doesn't exist on all systems. */ | |
620 | if (tty != 0) | |
c5aa993b JM |
621 | { |
622 | close (0); | |
bf1d7d9c | 623 | check_syscall ("dup'ing tty into fd 0", dup (tty)); |
c5aa993b | 624 | } |
c906108c | 625 | if (tty != 1) |
c5aa993b JM |
626 | { |
627 | close (1); | |
bf1d7d9c | 628 | check_syscall ("dup'ing tty into fd 1", dup (tty)); |
c5aa993b | 629 | } |
c906108c | 630 | if (tty != 2) |
c5aa993b JM |
631 | { |
632 | close (2); | |
bf1d7d9c | 633 | check_syscall ("dup'ing tty into fd 2", dup (tty)); |
c5aa993b | 634 | } |
83116857 TJB |
635 | |
636 | #ifdef TIOCSCTTY | |
637 | /* Make tty our new controlling terminal. */ | |
638 | if (ioctl (tty, TIOCSCTTY, 0) == -1) | |
639 | /* Mention GDB in warning because it will appear in the inferior's | |
640 | terminal instead of GDB's. */ | |
a73c6dcd | 641 | warning (_("GDB: Failed to set controlling terminal: %s"), |
83116857 TJB |
642 | safe_strerror (errno)); |
643 | #endif | |
644 | ||
c906108c | 645 | if (tty > 2) |
c5aa993b JM |
646 | close (tty); |
647 | #endif /* !go32 && !win32 */ | |
c906108c | 648 | } |
191c4426 PA |
649 | |
650 | /* NEW_TTY_POSTFORK is called after forking a new child process, and | |
651 | adding it to the inferior table, to store the TTYNAME being used by | |
652 | the child, or null if it sharing the terminal with gdb. */ | |
653 | ||
654 | void | |
655 | new_tty_postfork (void) | |
656 | { | |
657 | /* Save the name for later, for determining whether we and the child | |
658 | are sharing a tty. */ | |
659 | ||
660 | if (inferior_thisrun_terminal) | |
6c95b8df PA |
661 | { |
662 | struct inferior *inf = current_inferior (); | |
663 | struct terminal_info *tinfo = get_inflow_inferior_data (inf); | |
664 | ||
665 | tinfo->run_terminal = xstrdup (inferior_thisrun_terminal); | |
666 | } | |
191c4426 PA |
667 | |
668 | inferior_thisrun_terminal = NULL; | |
669 | } | |
670 | ||
c906108c SS |
671 | \f |
672 | /* Call set_sigint_trap when you need to pass a signal on to an attached | |
1777feb0 | 673 | process when handling SIGINT. */ |
c906108c | 674 | |
c906108c | 675 | static void |
fba45db2 | 676 | pass_signal (int signo) |
c906108c SS |
677 | { |
678 | #ifndef _WIN32 | |
dfd4cc63 | 679 | kill (ptid_get_pid (inferior_ptid), SIGINT); |
c906108c SS |
680 | #endif |
681 | } | |
682 | ||
a40805d4 | 683 | static sighandler_t osig; |
7e1789f5 | 684 | static int osig_set; |
c906108c SS |
685 | |
686 | void | |
fba45db2 | 687 | set_sigint_trap (void) |
c906108c | 688 | { |
181e7f93 | 689 | struct inferior *inf = current_inferior (); |
6c95b8df PA |
690 | struct terminal_info *tinfo = get_inflow_inferior_data (inf); |
691 | ||
692 | if (inf->attach_flag || tinfo->run_terminal) | |
c906108c | 693 | { |
a40805d4 | 694 | osig = signal (SIGINT, pass_signal); |
7e1789f5 | 695 | osig_set = 1; |
c906108c | 696 | } |
7e1789f5 PA |
697 | else |
698 | osig_set = 0; | |
c906108c SS |
699 | } |
700 | ||
701 | void | |
fba45db2 | 702 | clear_sigint_trap (void) |
c906108c | 703 | { |
7e1789f5 | 704 | if (osig_set) |
c906108c SS |
705 | { |
706 | signal (SIGINT, osig); | |
7e1789f5 | 707 | osig_set = 0; |
c906108c SS |
708 | } |
709 | } | |
710 | \f | |
c906108c | 711 | |
83116857 TJB |
712 | /* Create a new session if the inferior will run in a different tty. |
713 | A session is UNIX's way of grouping processes that share a controlling | |
714 | terminal, so a new one is needed if the inferior terminal will be | |
715 | different from GDB's. | |
716 | ||
717 | Returns the session id of the new session, 0 if no session was created | |
718 | or -1 if an error occurred. */ | |
719 | pid_t | |
720 | create_tty_session (void) | |
721 | { | |
722 | #ifdef HAVE_SETSID | |
723 | pid_t ret; | |
724 | ||
725 | if (!job_control || inferior_thisrun_terminal == 0) | |
726 | return 0; | |
727 | ||
728 | ret = setsid (); | |
729 | if (ret == -1) | |
a73c6dcd | 730 | warning (_("Failed to create new terminal session: setsid: %s"), |
83116857 TJB |
731 | safe_strerror (errno)); |
732 | ||
733 | return ret; | |
734 | #else | |
735 | return 0; | |
736 | #endif /* HAVE_SETSID */ | |
737 | } | |
738 | ||
0ea3f30e DJ |
739 | /* Get all the current tty settings (including whether we have a |
740 | tty at all!). We can't do this in _initialize_inflow because | |
741 | serial_fdopen() won't work until the serial_ops_list is | |
742 | initialized, but we don't want to do it lazily either, so | |
743 | that we can guarantee stdin_serial is opened if there is | |
744 | a terminal. */ | |
745 | void | |
746 | initialize_stdin_serial (void) | |
747 | { | |
748 | stdin_serial = serial_fdopen (0); | |
749 | } | |
750 | ||
c906108c | 751 | void |
fba45db2 | 752 | _initialize_inflow (void) |
c906108c | 753 | { |
11db9430 | 754 | add_info ("terminal", info_terminal_command, |
1bedd215 | 755 | _("Print inferior's saved terminal status.")); |
c906108c | 756 | |
c906108c SS |
757 | terminal_is_ours = 1; |
758 | ||
15652511 SDJ |
759 | /* OK, figure out whether we have job control. */ |
760 | have_job_control (); | |
7e1789f5 | 761 | |
7e1789f5 | 762 | observer_attach_inferior_exit (inflow_inferior_exit); |
6c95b8df PA |
763 | |
764 | inflow_inferior_data | |
8e260fc0 | 765 | = register_inferior_data_with_cleanup (NULL, inflow_inferior_data_cleanup); |
c906108c | 766 | } |