]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Low level interface to ptrace, for GDB when running under Unix. |
42a4f53d | 2 | Copyright (C) 1986-2019 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" | |
76727919 | 27 | #include "observable.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 | |
0747795c | 37 | #include "common/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 | |
e671cd59 | 49 | static void child_terminal_ours_1 (target_terminal_state); |
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 | |
e671cd59 PA |
57 | corresponding inferior stops, and restore it to the terminal when |
58 | the inferior is resumed in the foreground. */ | |
7e1789f5 PA |
59 | struct terminal_info |
60 | { | |
6509b8eb TT |
61 | terminal_info () = default; |
62 | ~terminal_info (); | |
63 | ||
7e1789f5 PA |
64 | /* The name of the tty (from the `tty' command) that we gave to the |
65 | inferior when it was started. */ | |
6509b8eb | 66 | char *run_terminal = nullptr; |
7e1789f5 PA |
67 | |
68 | /* TTY state. We save it whenever the inferior stops, and restore | |
e671cd59 | 69 | it when it resumes in the foreground. */ |
6509b8eb | 70 | serial_ttystate ttystate {}; |
7e1789f5 | 71 | |
726e1356 | 72 | #ifdef HAVE_TERMIOS_H |
e671cd59 PA |
73 | /* The terminal's foreground process group. Saved whenever the |
74 | inferior stops. This is the pgrp displayed by "info terminal". | |
75 | Note that this may be not the inferior's actual process group, | |
76 | since each inferior that we spawn has its own process group, and | |
77 | only one can be in the foreground at a time. When the inferior | |
78 | resumes, if we can determine the inferior's actual pgrp, then we | |
79 | make that the foreground pgrp instead of what was saved here. | |
80 | While it's a bit arbitrary which inferior's pgrp ends up in the | |
81 | foreground when we resume several inferiors, this at least makes | |
82 | 'resume inf1+inf2' + 'stop all' + 'resume inf2' end up with | |
83 | inf2's pgrp in the foreground instead of inf1's (which would be | |
84 | problematic since it would be left stopped: Ctrl-C wouldn't work, | |
85 | for example). */ | |
6509b8eb | 86 | pid_t process_group = 0; |
7e1789f5 | 87 | #endif |
c906108c | 88 | |
7e1789f5 | 89 | /* fcntl flags. Saved and restored just like ttystate. */ |
6509b8eb | 90 | int tflags = 0; |
7e1789f5 | 91 | }; |
c906108c | 92 | |
7e1789f5 | 93 | /* Our own tty state, which we restore every time we need to deal with |
d9de1fe3 PA |
94 | the terminal. This is set once, when GDB first starts, and then |
95 | whenever we enter/leave TUI mode (gdb_save_tty_state). The | |
96 | settings of flags which readline saves and restores are | |
7e1789f5 PA |
97 | unimportant. */ |
98 | static struct terminal_info our_terminal_info; | |
c906108c | 99 | |
d9de1fe3 PA |
100 | /* Snapshot of the initial tty state taken during initialization of |
101 | GDB, before readline/ncurses have had a chance to change it. This | |
102 | is used as the initial tty state given to each new spawned | |
103 | inferior. Unlike our_terminal_info, this is only ever set | |
104 | once. */ | |
6a06d660 PP |
105 | static serial_ttystate initial_gdb_ttystate; |
106 | ||
6c95b8df PA |
107 | static struct terminal_info *get_inflow_inferior_data (struct inferior *); |
108 | ||
c906108c SS |
109 | /* While the inferior is running, we want SIGINT and SIGQUIT to go to the |
110 | inferior only. If we have job control, that takes care of it. If not, | |
111 | we save our handlers in these two variables and set SIGINT and SIGQUIT | |
112 | to SIG_IGN. */ | |
113 | ||
a40805d4 | 114 | static sighandler_t sigint_ours; |
15766370 | 115 | #ifdef SIGQUIT |
a40805d4 | 116 | static sighandler_t sigquit_ours; |
15766370 | 117 | #endif |
c906108c | 118 | |
7e1789f5 PA |
119 | /* The name of the tty (from the `tty' command) that we're giving to |
120 | the inferior when starting it up. This is only (and should only | |
191c4426 PA |
121 | be) used as a transient global by new_tty_prefork, |
122 | create_tty_session, new_tty and new_tty_postfork, all called from | |
123 | fork_inferior, while forking a new child. */ | |
3cb3b8df | 124 | static const char *inferior_thisrun_terminal; |
c906108c | 125 | |
e671cd59 PA |
126 | /* Track who owns GDB's terminal (is it GDB or some inferior?). While |
127 | target_terminal::is_ours() etc. tracks the core's intention and is | |
128 | independent of the target backend, this tracks the actual state of | |
129 | GDB's own tty. So for example, | |
130 | ||
131 | (target_terminal::is_inferior () && gdb_tty_state == terminal_is_ours) | |
c906108c | 132 | |
e671cd59 PA |
133 | is true when the (native) inferior is not sharing a terminal with |
134 | GDB (e.g., because we attached to an inferior that is running on a | |
135 | different terminal). */ | |
136 | static target_terminal_state gdb_tty_state = target_terminal_state::is_ours; | |
c906108c | 137 | |
d9de1fe3 | 138 | /* See terminal.h. */ |
ea42d6f8 | 139 | |
6a06d660 PP |
140 | void |
141 | set_initial_gdb_ttystate (void) | |
142 | { | |
d9de1fe3 PA |
143 | /* Note we can't do any of this in _initialize_inflow because at |
144 | that point stdin_serial has not been created yet. */ | |
145 | ||
6a06d660 | 146 | initial_gdb_ttystate = serial_get_tty_state (stdin_serial); |
6a06d660 | 147 | |
d9de1fe3 | 148 | if (initial_gdb_ttystate != NULL) |
c906108c | 149 | { |
d9de1fe3 PA |
150 | our_terminal_info.ttystate |
151 | = serial_copy_tty_state (stdin_serial, initial_gdb_ttystate); | |
c906108c | 152 | #ifdef F_GETFL |
7e1789f5 | 153 | our_terminal_info.tflags = fcntl (0, F_GETFL, 0); |
c906108c | 154 | #endif |
726e1356 PA |
155 | #ifdef HAVE_TERMIOS_H |
156 | our_terminal_info.process_group = tcgetpgrp (0); | |
807bddf3 | 157 | #endif |
c906108c SS |
158 | } |
159 | } | |
160 | ||
d9de1fe3 PA |
161 | /* Does GDB have a terminal (on stdin)? */ |
162 | ||
163 | static int | |
164 | gdb_has_a_terminal (void) | |
165 | { | |
166 | return initial_gdb_ttystate != NULL; | |
167 | } | |
168 | ||
c906108c SS |
169 | /* Macro for printing errors from ioctl operations */ |
170 | ||
171 | #define OOPSY(what) \ | |
172 | if (result == -1) \ | |
173 | fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \ | |
dc672865 | 174 | what, safe_strerror (errno)) |
c906108c | 175 | |
c906108c SS |
176 | /* Initialize the terminal settings we record for the inferior, |
177 | before we actually run the inferior. */ | |
178 | ||
179 | void | |
556e5da5 | 180 | child_terminal_init (struct target_ops *self) |
c906108c | 181 | { |
e671cd59 PA |
182 | if (!gdb_has_a_terminal ()) |
183 | return; | |
184 | ||
185 | inferior *inf = current_inferior (); | |
186 | terminal_info *tinfo = get_inflow_inferior_data (inf); | |
6f64ef53 | 187 | |
726e1356 | 188 | #ifdef HAVE_TERMIOS_H |
e671cd59 PA |
189 | /* A child we spawn should be a process group leader (PGID==PID) at |
190 | this point, though that may not be true if we're attaching to an | |
191 | existing process. */ | |
556e5da5 | 192 | tinfo->process_group = inf->pid; |
6f64ef53 PA |
193 | #endif |
194 | ||
e671cd59 PA |
195 | xfree (tinfo->ttystate); |
196 | tinfo->ttystate = serial_copy_tty_state (stdin_serial, initial_gdb_ttystate); | |
c906108c SS |
197 | } |
198 | ||
a790ad35 SC |
199 | /* Save the terminal settings again. This is necessary for the TUI |
200 | when it switches to TUI or non-TUI mode; curses changes the terminal | |
201 | and gdb must be able to restore it correctly. */ | |
202 | ||
203 | void | |
3278a9f5 | 204 | gdb_save_tty_state (void) |
a790ad35 SC |
205 | { |
206 | if (gdb_has_a_terminal ()) | |
207 | { | |
7e1789f5 PA |
208 | xfree (our_terminal_info.ttystate); |
209 | our_terminal_info.ttystate = serial_get_tty_state (stdin_serial); | |
a790ad35 SC |
210 | } |
211 | } | |
212 | ||
e671cd59 PA |
213 | /* Try to determine whether TTY is GDB's input terminal. Returns |
214 | TRIBOOL_UNKNOWN if we can't tell. */ | |
4d4ca2a1 | 215 | |
e671cd59 PA |
216 | static tribool |
217 | is_gdb_terminal (const char *tty) | |
218 | { | |
219 | struct stat gdb_tty; | |
220 | struct stat other_tty; | |
221 | int res; | |
222 | ||
223 | res = stat (tty, &other_tty); | |
224 | if (res == -1) | |
225 | return TRIBOOL_UNKNOWN; | |
226 | ||
227 | res = fstat (STDIN_FILENO, &gdb_tty); | |
228 | if (res == -1) | |
229 | return TRIBOOL_UNKNOWN; | |
230 | ||
231 | return ((gdb_tty.st_dev == other_tty.st_dev | |
232 | && gdb_tty.st_ino == other_tty.st_ino) | |
233 | ? TRIBOOL_TRUE | |
234 | : TRIBOOL_FALSE); | |
235 | } | |
236 | ||
237 | /* Helper for sharing_input_terminal. Try to determine whether | |
238 | inferior INF is using the same TTY for input as GDB is. Returns | |
239 | TRIBOOL_UNKNOWN if we can't tell. */ | |
240 | ||
241 | static tribool | |
242 | sharing_input_terminal_1 (inferior *inf) | |
243 | { | |
244 | /* Using host-dependent code here is fine, because the | |
245 | child_terminal_foo functions are meant to be used by child/native | |
246 | targets. */ | |
247 | #if defined (__linux__) || defined (__sun__) | |
248 | char buf[100]; | |
249 | ||
250 | xsnprintf (buf, sizeof (buf), "/proc/%d/fd/0", inf->pid); | |
251 | return is_gdb_terminal (buf); | |
252 | #else | |
253 | return TRIBOOL_UNKNOWN; | |
254 | #endif | |
255 | } | |
256 | ||
257 | /* Return true if the inferior is using the same TTY for input as GDB | |
258 | is. If this is true, then we save/restore terminal flags/state. | |
259 | ||
260 | This is necessary because if inf->attach_flag is set, we don't | |
261 | offhand know whether we are sharing a terminal with the inferior or | |
262 | not. Attaching a process without a terminal is one case where we | |
263 | do not; attaching a process which we ran from the same shell as GDB | |
264 | via `&' is one case where we do. | |
265 | ||
266 | If we can't determine, we assume the TTY is being shared. This | |
267 | works OK if you're only debugging one inferior. However, if you're | |
268 | debugging more than one inferior, and e.g., one is spawned by GDB | |
269 | with "run" (sharing terminal with GDB), and another is attached to | |
270 | (and running on a different terminal, as is most common), then it | |
271 | matters, because we can only restore the terminal settings of one | |
272 | of the inferiors, and in that scenario, we want to restore the | |
273 | settings of the "run"'ed inferior. | |
274 | ||
275 | Note, this is not the same as determining whether GDB and the | |
276 | inferior are in the same session / connected to the same | |
277 | controlling tty. An inferior (fork child) may call setsid, | |
278 | disconnecting itself from the ctty, while still leaving | |
279 | stdin/stdout/stderr associated with the original terminal. If | |
280 | we're debugging that process, we should also save/restore terminal | |
281 | settings. */ | |
282 | ||
283 | static bool | |
284 | sharing_input_terminal (inferior *inf) | |
285 | { | |
286 | terminal_info *tinfo = get_inflow_inferior_data (inf); | |
287 | ||
288 | tribool res = sharing_input_terminal_1 (inf); | |
289 | ||
290 | if (res == TRIBOOL_UNKNOWN) | |
291 | { | |
292 | /* As fallback, if we can't determine by stat'ing the inferior's | |
293 | tty directly (because it's not supported on this host) and | |
294 | the child was spawned, check whether run_terminal is our tty. | |
295 | This isn't ideal, since this is checking the child's | |
296 | controlling terminal, not the input terminal (which may have | |
297 | been redirected), but is still better than nothing. A false | |
298 | positive ("set inferior-tty" points to our terminal, but I/O | |
299 | was redirected) is much more likely than a false negative | |
300 | ("set inferior-tty" points to some other terminal, and then | |
301 | output was redirected to our terminal), and with a false | |
302 | positive we just end up trying to save/restore terminal | |
303 | settings when we didn't need to or we actually can't. */ | |
304 | if (tinfo->run_terminal != NULL) | |
305 | res = is_gdb_terminal (tinfo->run_terminal); | |
306 | ||
307 | /* If we still can't determine, assume yes. */ | |
308 | if (res == TRIBOOL_UNKNOWN) | |
309 | return true; | |
310 | } | |
311 | ||
312 | return res == TRIBOOL_TRUE; | |
313 | } | |
314 | ||
315 | /* Put the inferior's terminal settings into effect. This is | |
316 | preparation for starting or resuming the inferior. */ | |
c906108c SS |
317 | |
318 | void | |
d6b64346 | 319 | child_terminal_inferior (struct target_ops *self) |
c906108c | 320 | { |
e671cd59 PA |
321 | /* If we resume more than one inferior in the foreground on GDB's |
322 | terminal, then the first inferior's terminal settings "win". | |
323 | Note that every child process is put in its own process group, so | |
324 | the first process that ends up resumed ends up determining which | |
325 | process group the kernel forwards Ctrl-C/Ctrl-Z (SIGINT/SIGTTOU) | |
326 | to. */ | |
327 | if (gdb_tty_state == target_terminal_state::is_inferior) | |
7e1789f5 PA |
328 | return; |
329 | ||
e671cd59 PA |
330 | inferior *inf = current_inferior (); |
331 | terminal_info *tinfo = get_inflow_inferior_data (inf); | |
7e1789f5 PA |
332 | |
333 | if (gdb_has_a_terminal () | |
6c95b8df | 334 | && tinfo->ttystate != NULL |
e671cd59 | 335 | && sharing_input_terminal (inf)) |
c906108c SS |
336 | { |
337 | int result; | |
338 | ||
e671cd59 PA |
339 | /* Ignore SIGTTOU since it will happen when we try to set the |
340 | terminal's state (if gdb_tty_state is currently | |
341 | ours_for_output). */ | |
342 | scoped_ignore_sigttou ignore_sigttou; | |
343 | ||
c906108c | 344 | #ifdef F_GETFL |
6c95b8df | 345 | result = fcntl (0, F_SETFL, tinfo->tflags); |
c906108c SS |
346 | OOPSY ("fcntl F_SETFL"); |
347 | #endif | |
348 | ||
726e1356 | 349 | result = serial_set_tty_state (stdin_serial, tinfo->ttystate); |
c906108c SS |
350 | OOPSY ("setting tty state"); |
351 | ||
352 | if (!job_control) | |
353 | { | |
a40805d4 | 354 | sigint_ours = signal (SIGINT, SIG_IGN); |
c906108c | 355 | #ifdef SIGQUIT |
a40805d4 | 356 | sigquit_ours = signal (SIGQUIT, SIG_IGN); |
c906108c SS |
357 | #endif |
358 | } | |
359 | ||
c906108c SS |
360 | if (job_control) |
361 | { | |
726e1356 | 362 | #ifdef HAVE_TERMIOS_H |
e671cd59 PA |
363 | /* If we can't tell the inferior's actual process group, |
364 | then restore whatever was the foreground pgrp the last | |
365 | time the inferior was running. See also comments | |
366 | describing terminal_state::process_group. */ | |
367 | #ifdef HAVE_GETPGID | |
368 | result = tcsetpgrp (0, getpgid (inf->pid)); | |
369 | #else | |
6c95b8df | 370 | result = tcsetpgrp (0, tinfo->process_group); |
e671cd59 PA |
371 | #endif |
372 | if (result == -1) | |
373 | { | |
374 | #if 0 | |
375 | /* This fails if either GDB has no controlling terminal, | |
376 | e.g., running under 'setsid(1)', or if the inferior | |
377 | is not attached to GDB's controlling terminal. E.g., | |
378 | if it called setsid to create a new session or used | |
379 | the TIOCNOTTY ioctl, or simply if we've attached to a | |
380 | process running on another terminal and we couldn't | |
381 | tell whether it was sharing GDB's terminal (and so | |
382 | assumed yes). */ | |
383 | fprintf_unfiltered | |
384 | (gdb_stderr, | |
385 | "[tcsetpgrp failed in child_terminal_inferior: %s]\n", | |
386 | safe_strerror (errno)); | |
387 | #endif | |
388 | } | |
c906108c | 389 | #endif |
c906108c | 390 | } |
e671cd59 PA |
391 | |
392 | gdb_tty_state = target_terminal_state::is_inferior; | |
c906108c | 393 | } |
c906108c SS |
394 | } |
395 | ||
396 | /* Put some of our terminal settings into effect, | |
397 | enough to get proper results from our output, | |
398 | but do not change into or out of RAW mode | |
399 | so that no input is discarded. | |
400 | ||
401 | After doing this, either terminal_ours or terminal_inferior | |
4d4ca2a1 DE |
402 | should be called to get back to a normal state of affairs. |
403 | ||
404 | N.B. The implementation is (currently) no different than | |
405 | child_terminal_ours. See child_terminal_ours_1. */ | |
c906108c SS |
406 | |
407 | void | |
d6b64346 | 408 | child_terminal_ours_for_output (struct target_ops *self) |
c906108c | 409 | { |
e671cd59 | 410 | child_terminal_ours_1 (target_terminal_state::is_ours_for_output); |
c906108c SS |
411 | } |
412 | ||
413 | /* Put our terminal settings into effect. | |
414 | First record the inferior's terminal settings | |
4d4ca2a1 DE |
415 | so they can be restored properly later. |
416 | ||
417 | N.B. Targets that want to use this with async support must build that | |
418 | support on top of this (e.g., the caller still needs to add stdin to the | |
419 | event loop). E.g., see linux_nat_terminal_ours. */ | |
c906108c SS |
420 | |
421 | void | |
d6b64346 | 422 | child_terminal_ours (struct target_ops *self) |
c906108c | 423 | { |
e671cd59 | 424 | child_terminal_ours_1 (target_terminal_state::is_ours); |
c906108c SS |
425 | } |
426 | ||
e671cd59 PA |
427 | /* Save the current terminal settings in the inferior's terminal_info |
428 | cache. */ | |
c906108c | 429 | |
e671cd59 PA |
430 | void |
431 | child_terminal_save_inferior (struct target_ops *self) | |
c906108c | 432 | { |
e671cd59 PA |
433 | /* Avoid attempting all the ioctl's when running in batch. */ |
434 | if (!gdb_has_a_terminal ()) | |
435 | return; | |
7e1789f5 | 436 | |
e671cd59 PA |
437 | inferior *inf = current_inferior (); |
438 | terminal_info *tinfo = get_inflow_inferior_data (inf); | |
439 | ||
440 | /* No need to save/restore if the inferior is not sharing GDB's | |
441 | tty. */ | |
442 | if (!sharing_input_terminal (inf)) | |
7e1789f5 PA |
443 | return; |
444 | ||
e671cd59 PA |
445 | xfree (tinfo->ttystate); |
446 | tinfo->ttystate = serial_get_tty_state (stdin_serial); | |
d9d2d8b6 | 447 | |
f6cfb427 | 448 | #ifdef HAVE_TERMIOS_H |
e671cd59 | 449 | tinfo->process_group = tcgetpgrp (0); |
f6cfb427 | 450 | #endif |
7e1789f5 | 451 | |
e671cd59 PA |
452 | #ifdef F_GETFL |
453 | tinfo->tflags = fcntl (0, F_GETFL, 0); | |
454 | #endif | |
455 | } | |
456 | ||
457 | /* Switch terminal state to DESIRED_STATE, either is_ours, or | |
458 | is_ours_for_output. */ | |
7e1789f5 | 459 | |
e671cd59 PA |
460 | static void |
461 | child_terminal_ours_1 (target_terminal_state desired_state) | |
462 | { | |
463 | gdb_assert (desired_state != target_terminal_state::is_inferior); | |
464 | ||
465 | /* Avoid attempting all the ioctl's when running in batch. */ | |
466 | if (!gdb_has_a_terminal ()) | |
c906108c | 467 | return; |
e671cd59 PA |
468 | |
469 | if (gdb_tty_state != desired_state) | |
c906108c | 470 | { |
821fc4ae | 471 | int result ATTRIBUTE_UNUSED; |
c906108c | 472 | |
e2c33ac7 PA |
473 | /* Ignore SIGTTOU since it will happen when we try to set the |
474 | terminal's pgrp. */ | |
475 | scoped_ignore_sigttou ignore_sigttou; | |
c906108c | 476 | |
726e1356 PA |
477 | /* Set tty state to our_ttystate. */ |
478 | serial_set_tty_state (stdin_serial, our_terminal_info.ttystate); | |
c906108c | 479 | |
e671cd59 PA |
480 | /* If we only want output, then leave the inferior's pgrp in the |
481 | foreground, so that Ctrl-C/Ctrl-Z reach the inferior | |
482 | directly. */ | |
483 | if (job_control && desired_state == target_terminal_state::is_ours) | |
c906108c | 484 | { |
726e1356 | 485 | #ifdef HAVE_TERMIOS_H |
7e1789f5 | 486 | result = tcsetpgrp (0, our_terminal_info.process_group); |
c906108c SS |
487 | #if 0 |
488 | /* This fails on Ultrix with EINVAL if you run the testsuite | |
489 | in the background with nohup, and then log out. GDB never | |
490 | used to check for an error here, so perhaps there are other | |
491 | such situations as well. */ | |
492 | if (result == -1) | |
3e43a32a | 493 | fprintf_unfiltered (gdb_stderr, |
d6b64346 | 494 | "[tcsetpgrp failed in child_terminal_ours: %s]\n", |
dc672865 | 495 | safe_strerror (errno)); |
c906108c SS |
496 | #endif |
497 | #endif /* termios */ | |
c906108c SS |
498 | } |
499 | ||
e671cd59 | 500 | if (!job_control && desired_state == target_terminal_state::is_ours) |
c906108c SS |
501 | { |
502 | signal (SIGINT, sigint_ours); | |
503 | #ifdef SIGQUIT | |
504 | signal (SIGQUIT, sigquit_ours); | |
505 | #endif | |
506 | } | |
507 | ||
508 | #ifdef F_GETFL | |
7e1789f5 | 509 | result = fcntl (0, F_SETFL, our_terminal_info.tflags); |
c906108c | 510 | #endif |
e671cd59 PA |
511 | |
512 | gdb_tty_state = desired_state; | |
513 | } | |
514 | } | |
515 | ||
516 | /* Interrupt the inferior. Implementation of target_interrupt for | |
517 | child/native targets. */ | |
518 | ||
519 | void | |
520 | child_interrupt (struct target_ops *self) | |
521 | { | |
522 | /* Interrupt the first inferior that has a resumed thread. */ | |
e671cd59 | 523 | thread_info *resumed = NULL; |
08036331 | 524 | for (thread_info *thr : all_non_exited_threads ()) |
e671cd59 PA |
525 | { |
526 | if (thr->executing) | |
527 | { | |
528 | resumed = thr; | |
529 | break; | |
530 | } | |
531 | if (thr->suspend.waitstatus_pending_p) | |
532 | resumed = thr; | |
533 | } | |
534 | ||
535 | if (resumed != NULL) | |
536 | { | |
537 | /* Note that unlike pressing Ctrl-C on the controlling terminal, | |
538 | here we only interrupt one process, not the whole process | |
539 | group. This is because interrupting a process group (with | |
540 | either Ctrl-C or with kill(3) with negative PID) sends a | |
541 | SIGINT to each process in the process group, and we may not | |
542 | be debugging all processes in the process group. */ | |
f6cfb427 | 543 | #ifndef _WIN32 |
e671cd59 | 544 | kill (resumed->inf->pid, SIGINT); |
f6cfb427 | 545 | #endif |
e671cd59 PA |
546 | } |
547 | } | |
548 | ||
549 | /* Pass a Ctrl-C to the inferior as-if a Ctrl-C was pressed while the | |
550 | inferior was in the foreground. Implementation of | |
551 | target_pass_ctrlc for child/native targets. */ | |
552 | ||
553 | void | |
554 | child_pass_ctrlc (struct target_ops *self) | |
555 | { | |
556 | gdb_assert (!target_terminal::is_ours ()); | |
557 | ||
558 | #ifdef HAVE_TERMIOS_H | |
559 | if (job_control) | |
560 | { | |
561 | pid_t term_pgrp = tcgetpgrp (0); | |
562 | ||
563 | /* If there's any inferior sharing our terminal, pass the SIGINT | |
564 | to the terminal's foreground process group. This acts just | |
565 | like the user typed a ^C on the terminal while the inferior | |
566 | was in the foreground. Note that using a negative process | |
567 | number in kill() is a System V-ism. The proper BSD interface | |
568 | is killpg(). However, all modern BSDs support the System V | |
569 | interface too. */ | |
570 | ||
571 | if (term_pgrp != -1 && term_pgrp != our_terminal_info.process_group) | |
572 | { | |
573 | kill (-term_pgrp, SIGINT); | |
574 | return; | |
575 | } | |
576 | } | |
577 | #endif | |
578 | ||
579 | /* Otherwise, pass the Ctrl-C to the first inferior that was resumed | |
580 | in the foreground. */ | |
08036331 | 581 | for (inferior *inf : all_inferiors ()) |
e671cd59 PA |
582 | { |
583 | if (inf->terminal_state != target_terminal_state::is_ours) | |
584 | { | |
585 | gdb_assert (inf->pid != 0); | |
586 | ||
f6cfb427 | 587 | #ifndef _WIN32 |
e671cd59 | 588 | kill (inf->pid, SIGINT); |
f6cfb427 | 589 | #endif |
e671cd59 PA |
590 | return; |
591 | } | |
c906108c | 592 | } |
e671cd59 PA |
593 | |
594 | /* If no inferior was resumed in the foreground, then how did the | |
595 | !is_ours assert above pass? */ | |
596 | gdb_assert_not_reached ("no inferior resumed in the fg found"); | |
c906108c SS |
597 | } |
598 | ||
6c95b8df | 599 | /* Per-inferior data key. */ |
6509b8eb | 600 | static const struct inferior_key<terminal_info> inflow_inferior_data; |
7e1789f5 | 601 | |
6509b8eb | 602 | terminal_info::~terminal_info () |
7e1789f5 | 603 | { |
6509b8eb TT |
604 | xfree (run_terminal); |
605 | xfree (ttystate); | |
6c95b8df PA |
606 | } |
607 | ||
608 | /* Get the current svr4 data. If none is found yet, add it now. This | |
609 | function always returns a valid object. */ | |
610 | ||
611 | static struct terminal_info * | |
612 | get_inflow_inferior_data (struct inferior *inf) | |
613 | { | |
614 | struct terminal_info *info; | |
615 | ||
6509b8eb | 616 | info = inflow_inferior_data.get (inf); |
6c95b8df | 617 | if (info == NULL) |
6509b8eb | 618 | info = inflow_inferior_data.emplace (inf); |
7e1789f5 | 619 | |
6c95b8df | 620 | return info; |
7e1789f5 PA |
621 | } |
622 | ||
623 | /* This is a "inferior_exit" observer. Releases the TERMINAL_INFO member | |
624 | of the inferior structure. This field is private to inflow.c, and | |
625 | its type is opaque to the rest of GDB. PID is the target pid of | |
626 | the inferior that is about to be removed from the inferior | |
627 | list. */ | |
628 | ||
629 | static void | |
a79b8f6e | 630 | inflow_inferior_exit (struct inferior *inf) |
7e1789f5 | 631 | { |
e671cd59 | 632 | inf->terminal_state = target_terminal_state::is_ours; |
6509b8eb | 633 | inflow_inferior_data.clear (inf); |
7e1789f5 PA |
634 | } |
635 | ||
191c4426 PA |
636 | void |
637 | copy_terminal_info (struct inferior *to, struct inferior *from) | |
638 | { | |
6c95b8df PA |
639 | struct terminal_info *tinfo_to, *tinfo_from; |
640 | ||
641 | tinfo_to = get_inflow_inferior_data (to); | |
642 | tinfo_from = get_inflow_inferior_data (from); | |
1e182ce8 UW |
643 | |
644 | xfree (tinfo_to->run_terminal); | |
645 | xfree (tinfo_to->ttystate); | |
646 | ||
6c95b8df | 647 | *tinfo_to = *tinfo_from; |
1e182ce8 | 648 | |
6c95b8df PA |
649 | if (tinfo_from->run_terminal) |
650 | tinfo_to->run_terminal | |
651 | = xstrdup (tinfo_from->run_terminal); | |
1e182ce8 UW |
652 | |
653 | if (tinfo_from->ttystate) | |
654 | tinfo_to->ttystate | |
655 | = serial_copy_tty_state (stdin_serial, tinfo_from->ttystate); | |
e671cd59 PA |
656 | |
657 | to->terminal_state = from->terminal_state; | |
191c4426 PA |
658 | } |
659 | ||
35ed81d4 SM |
660 | /* See terminal.h. */ |
661 | ||
662 | void | |
663 | swap_terminal_info (inferior *a, inferior *b) | |
664 | { | |
6509b8eb TT |
665 | terminal_info *info_a = inflow_inferior_data.get (a); |
666 | terminal_info *info_b = inflow_inferior_data.get (b); | |
35ed81d4 | 667 | |
6509b8eb TT |
668 | inflow_inferior_data.set (a, info_b); |
669 | inflow_inferior_data.set (b, info_a); | |
35ed81d4 SM |
670 | |
671 | std::swap (a->terminal_state, b->terminal_state); | |
672 | } | |
673 | ||
c906108c | 674 | void |
1d12d88f | 675 | info_terminal_command (const char *arg, int from_tty) |
c906108c | 676 | { |
223ffa71 | 677 | target_terminal::info (arg, from_tty); |
c906108c SS |
678 | } |
679 | ||
c906108c | 680 | void |
0a4f40a2 | 681 | child_terminal_info (struct target_ops *self, const char *args, int from_tty) |
c906108c | 682 | { |
7e1789f5 | 683 | struct inferior *inf; |
6c95b8df | 684 | struct terminal_info *tinfo; |
7e1789f5 | 685 | |
c906108c SS |
686 | if (!gdb_has_a_terminal ()) |
687 | { | |
a3f17187 | 688 | printf_filtered (_("This GDB does not control a terminal.\n")); |
c906108c SS |
689 | return; |
690 | } | |
691 | ||
d7e15655 | 692 | if (inferior_ptid == null_ptid) |
7e1789f5 PA |
693 | return; |
694 | ||
695 | inf = current_inferior (); | |
6c95b8df | 696 | tinfo = get_inflow_inferior_data (inf); |
7e1789f5 | 697 | |
3e43a32a MS |
698 | printf_filtered (_("Inferior's terminal status " |
699 | "(currently saved by GDB):\n")); | |
c906108c SS |
700 | |
701 | /* First the fcntl flags. */ | |
702 | { | |
703 | int flags; | |
c5aa993b | 704 | |
6c95b8df | 705 | flags = tinfo->tflags; |
c906108c SS |
706 | |
707 | printf_filtered ("File descriptor flags = "); | |
708 | ||
709 | #ifndef O_ACCMODE | |
710 | #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR) | |
711 | #endif | |
1777feb0 | 712 | /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */ |
c906108c SS |
713 | switch (flags & (O_ACCMODE)) |
714 | { | |
c5aa993b JM |
715 | case O_RDONLY: |
716 | printf_filtered ("O_RDONLY"); | |
717 | break; | |
718 | case O_WRONLY: | |
719 | printf_filtered ("O_WRONLY"); | |
720 | break; | |
721 | case O_RDWR: | |
722 | printf_filtered ("O_RDWR"); | |
723 | break; | |
c906108c SS |
724 | } |
725 | flags &= ~(O_ACCMODE); | |
726 | ||
727 | #ifdef O_NONBLOCK | |
c5aa993b | 728 | if (flags & O_NONBLOCK) |
c906108c SS |
729 | printf_filtered (" | O_NONBLOCK"); |
730 | flags &= ~O_NONBLOCK; | |
731 | #endif | |
c5aa993b | 732 | |
c906108c SS |
733 | #if defined (O_NDELAY) |
734 | /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will | |
735 | print it as O_NONBLOCK, which is good cause that is what POSIX | |
736 | has, and the flag will already be cleared by the time we get here. */ | |
737 | if (flags & O_NDELAY) | |
738 | printf_filtered (" | O_NDELAY"); | |
739 | flags &= ~O_NDELAY; | |
740 | #endif | |
741 | ||
742 | if (flags & O_APPEND) | |
743 | printf_filtered (" | O_APPEND"); | |
744 | flags &= ~O_APPEND; | |
745 | ||
746 | #if defined (O_BINARY) | |
747 | if (flags & O_BINARY) | |
748 | printf_filtered (" | O_BINARY"); | |
749 | flags &= ~O_BINARY; | |
750 | #endif | |
751 | ||
752 | if (flags) | |
753 | printf_filtered (" | 0x%x", flags); | |
754 | printf_filtered ("\n"); | |
755 | } | |
756 | ||
726e1356 | 757 | #ifdef HAVE_TERMIOS_H |
6c95b8df | 758 | printf_filtered ("Process group = %d\n", (int) tinfo->process_group); |
c906108c SS |
759 | #endif |
760 | ||
6c95b8df | 761 | serial_print_tty_state (stdin_serial, tinfo->ttystate, gdb_stdout); |
c906108c SS |
762 | } |
763 | \f | |
764 | /* NEW_TTY_PREFORK is called before forking a new child process, | |
765 | so we can record the state of ttys in the child to be formed. | |
766 | TTYNAME is null if we are to share the terminal with gdb; | |
767 | or points to a string containing the name of the desired tty. | |
768 | ||
769 | NEW_TTY is called in new child processes under Unix, which will | |
770 | become debugger target processes. This actually switches to | |
771 | the terminal specified in the NEW_TTY_PREFORK call. */ | |
772 | ||
773 | void | |
3cb3b8df | 774 | new_tty_prefork (const char *ttyname) |
c906108c SS |
775 | { |
776 | /* Save the name for later, for determining whether we and the child | |
777 | are sharing a tty. */ | |
778 | inferior_thisrun_terminal = ttyname; | |
779 | } | |
780 | ||
e6d088ec | 781 | #if !defined(__GO32__) && !defined(_WIN32) |
bf1d7d9c JB |
782 | /* If RESULT, assumed to be the return value from a system call, is |
783 | negative, print the error message indicated by errno and exit. | |
784 | MSG should identify the operation that failed. */ | |
785 | static void | |
786 | check_syscall (const char *msg, int result) | |
787 | { | |
788 | if (result < 0) | |
789 | { | |
790 | print_sys_errmsg (msg, errno); | |
791 | _exit (1); | |
792 | } | |
793 | } | |
e6d088ec | 794 | #endif |
bf1d7d9c | 795 | |
c906108c | 796 | void |
fba45db2 | 797 | new_tty (void) |
c906108c | 798 | { |
c906108c SS |
799 | if (inferior_thisrun_terminal == 0) |
800 | return; | |
801 | #if !defined(__GO32__) && !defined(_WIN32) | |
15766370 TT |
802 | int tty; |
803 | ||
c906108c SS |
804 | #ifdef TIOCNOTTY |
805 | /* Disconnect the child process from our controlling terminal. On some | |
806 | systems (SVR4 for example), this may cause a SIGTTOU, so temporarily | |
1777feb0 | 807 | ignore SIGTTOU. */ |
c5aa993b | 808 | tty = open ("/dev/tty", O_RDWR); |
c906108c SS |
809 | if (tty > 0) |
810 | { | |
e2c33ac7 | 811 | scoped_ignore_sigttou ignore_sigttou; |
c906108c | 812 | |
c5aa993b JM |
813 | ioctl (tty, TIOCNOTTY, 0); |
814 | close (tty); | |
c906108c SS |
815 | } |
816 | #endif | |
817 | ||
818 | /* Now open the specified new terminal. */ | |
c5aa993b | 819 | tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY); |
bf1d7d9c | 820 | check_syscall (inferior_thisrun_terminal, tty); |
c906108c SS |
821 | |
822 | /* Avoid use of dup2; doesn't exist on all systems. */ | |
823 | if (tty != 0) | |
c5aa993b JM |
824 | { |
825 | close (0); | |
bf1d7d9c | 826 | check_syscall ("dup'ing tty into fd 0", dup (tty)); |
c5aa993b | 827 | } |
c906108c | 828 | if (tty != 1) |
c5aa993b JM |
829 | { |
830 | close (1); | |
bf1d7d9c | 831 | check_syscall ("dup'ing tty into fd 1", dup (tty)); |
c5aa993b | 832 | } |
c906108c | 833 | if (tty != 2) |
c5aa993b JM |
834 | { |
835 | close (2); | |
bf1d7d9c | 836 | check_syscall ("dup'ing tty into fd 2", dup (tty)); |
c5aa993b | 837 | } |
83116857 TJB |
838 | |
839 | #ifdef TIOCSCTTY | |
840 | /* Make tty our new controlling terminal. */ | |
841 | if (ioctl (tty, TIOCSCTTY, 0) == -1) | |
842 | /* Mention GDB in warning because it will appear in the inferior's | |
843 | terminal instead of GDB's. */ | |
a73c6dcd | 844 | warning (_("GDB: Failed to set controlling terminal: %s"), |
83116857 TJB |
845 | safe_strerror (errno)); |
846 | #endif | |
847 | ||
c906108c | 848 | if (tty > 2) |
c5aa993b JM |
849 | close (tty); |
850 | #endif /* !go32 && !win32 */ | |
c906108c | 851 | } |
191c4426 PA |
852 | |
853 | /* NEW_TTY_POSTFORK is called after forking a new child process, and | |
854 | adding it to the inferior table, to store the TTYNAME being used by | |
855 | the child, or null if it sharing the terminal with gdb. */ | |
856 | ||
857 | void | |
858 | new_tty_postfork (void) | |
859 | { | |
860 | /* Save the name for later, for determining whether we and the child | |
861 | are sharing a tty. */ | |
862 | ||
863 | if (inferior_thisrun_terminal) | |
6c95b8df PA |
864 | { |
865 | struct inferior *inf = current_inferior (); | |
866 | struct terminal_info *tinfo = get_inflow_inferior_data (inf); | |
867 | ||
868 | tinfo->run_terminal = xstrdup (inferior_thisrun_terminal); | |
869 | } | |
191c4426 PA |
870 | |
871 | inferior_thisrun_terminal = NULL; | |
872 | } | |
873 | ||
c906108c SS |
874 | \f |
875 | /* Call set_sigint_trap when you need to pass a signal on to an attached | |
1777feb0 | 876 | process when handling SIGINT. */ |
c906108c | 877 | |
c906108c | 878 | static void |
fba45db2 | 879 | pass_signal (int signo) |
c906108c SS |
880 | { |
881 | #ifndef _WIN32 | |
e99b03dc | 882 | kill (inferior_ptid.pid (), SIGINT); |
c906108c SS |
883 | #endif |
884 | } | |
885 | ||
a40805d4 | 886 | static sighandler_t osig; |
7e1789f5 | 887 | static int osig_set; |
c906108c SS |
888 | |
889 | void | |
fba45db2 | 890 | set_sigint_trap (void) |
c906108c | 891 | { |
181e7f93 | 892 | struct inferior *inf = current_inferior (); |
6c95b8df PA |
893 | struct terminal_info *tinfo = get_inflow_inferior_data (inf); |
894 | ||
895 | if (inf->attach_flag || tinfo->run_terminal) | |
c906108c | 896 | { |
a40805d4 | 897 | osig = signal (SIGINT, pass_signal); |
7e1789f5 | 898 | osig_set = 1; |
c906108c | 899 | } |
7e1789f5 PA |
900 | else |
901 | osig_set = 0; | |
c906108c SS |
902 | } |
903 | ||
904 | void | |
fba45db2 | 905 | clear_sigint_trap (void) |
c906108c | 906 | { |
7e1789f5 | 907 | if (osig_set) |
c906108c SS |
908 | { |
909 | signal (SIGINT, osig); | |
7e1789f5 | 910 | osig_set = 0; |
c906108c SS |
911 | } |
912 | } | |
913 | \f | |
c906108c | 914 | |
83116857 TJB |
915 | /* Create a new session if the inferior will run in a different tty. |
916 | A session is UNIX's way of grouping processes that share a controlling | |
917 | terminal, so a new one is needed if the inferior terminal will be | |
918 | different from GDB's. | |
919 | ||
920 | Returns the session id of the new session, 0 if no session was created | |
921 | or -1 if an error occurred. */ | |
922 | pid_t | |
923 | create_tty_session (void) | |
924 | { | |
925 | #ifdef HAVE_SETSID | |
926 | pid_t ret; | |
927 | ||
928 | if (!job_control || inferior_thisrun_terminal == 0) | |
929 | return 0; | |
930 | ||
931 | ret = setsid (); | |
932 | if (ret == -1) | |
a73c6dcd | 933 | warning (_("Failed to create new terminal session: setsid: %s"), |
83116857 TJB |
934 | safe_strerror (errno)); |
935 | ||
936 | return ret; | |
937 | #else | |
938 | return 0; | |
939 | #endif /* HAVE_SETSID */ | |
940 | } | |
941 | ||
0ea3f30e DJ |
942 | /* Get all the current tty settings (including whether we have a |
943 | tty at all!). We can't do this in _initialize_inflow because | |
944 | serial_fdopen() won't work until the serial_ops_list is | |
945 | initialized, but we don't want to do it lazily either, so | |
946 | that we can guarantee stdin_serial is opened if there is | |
947 | a terminal. */ | |
948 | void | |
949 | initialize_stdin_serial (void) | |
950 | { | |
951 | stdin_serial = serial_fdopen (0); | |
952 | } | |
953 | ||
c906108c | 954 | void |
fba45db2 | 955 | _initialize_inflow (void) |
c906108c | 956 | { |
11db9430 | 957 | add_info ("terminal", info_terminal_command, |
1bedd215 | 958 | _("Print inferior's saved terminal status.")); |
c906108c | 959 | |
15652511 SDJ |
960 | /* OK, figure out whether we have job control. */ |
961 | have_job_control (); | |
7e1789f5 | 962 | |
76727919 | 963 | gdb::observers::inferior_exit.attach (inflow_inferior_exit); |
c906108c | 964 | } |