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