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