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