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