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