windows-nat: Don't change current_event.dwThreadId in handle_output_debug_string()
[deliverable/binutils-gdb.git] / gdb / inflow.c
CommitLineData
c906108c 1/* Low level interface to ptrace, for GDB when running under Unix.
32d0add0 2 Copyright (C) 1986-2015 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"
7e1789f5 27#include "observer.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"
c906108c 34
c2d11a7d
JM
35#ifdef HAVE_SYS_IOCTL_H
36#include <sys/ioctl.h>
37#endif
38
f2acbe1c
MK
39#ifndef O_NOCTTY
40#define O_NOCTTY 0
41#endif
42
a14ed312 43extern void _initialize_inflow (void);
392a587b 44
a14ed312 45static void pass_signal (int);
c906108c 46
d6b64346 47static void child_terminal_ours_1 (int);
c906108c
SS
48\f
49/* Record terminal status separately for debugger and inferior. */
50
819cc324 51static struct serial *stdin_serial;
c906108c 52
7e1789f5
PA
53/* Terminal related info we need to keep track of. Each inferior
54 holds an instance of this structure --- we save it whenever the
55 corresponding inferior stops, and restore it to the foreground
56 inferior when it resumes. */
57struct terminal_info
58{
59 /* The name of the tty (from the `tty' command) that we gave to the
60 inferior when it was started. */
191c4426 61 char *run_terminal;
7e1789f5
PA
62
63 /* TTY state. We save it whenever the inferior stops, and restore
64 it when it resumes. */
65 serial_ttystate ttystate;
66
67#ifdef PROCESS_GROUP_TYPE
68 /* Process group. Saved and restored just like ttystate. */
69 PROCESS_GROUP_TYPE process_group;
70#endif
c906108c 71
7e1789f5
PA
72 /* fcntl flags. Saved and restored just like ttystate. */
73 int tflags;
74};
c906108c 75
7e1789f5
PA
76/* Our own tty state, which we restore every time we need to deal with
77 the terminal. This is only set once, when GDB first starts. The
78 settings of flags which readline saves and restores and
79 unimportant. */
80static struct terminal_info our_terminal_info;
c906108c 81
fa5af12a
PP
82/* Snapshot of our own tty state taken during initialization of GDB.
83 This is used as the initial tty state given to each new inferior. */
6a06d660
PP
84static serial_ttystate initial_gdb_ttystate;
85
6c95b8df
PA
86static struct terminal_info *get_inflow_inferior_data (struct inferior *);
87
c906108c 88#ifdef PROCESS_GROUP_TYPE
7e1789f5
PA
89
90/* Return the process group of the current inferior. */
91
92PROCESS_GROUP_TYPE
93inferior_process_group (void)
94{
6c95b8df 95 return get_inflow_inferior_data (current_inferior ())->process_group;
7e1789f5 96}
c906108c
SS
97#endif
98
99/* While the inferior is running, we want SIGINT and SIGQUIT to go to the
100 inferior only. If we have job control, that takes care of it. If not,
101 we save our handlers in these two variables and set SIGINT and SIGQUIT
102 to SIG_IGN. */
103
104static void (*sigint_ours) ();
105static void (*sigquit_ours) ();
106
7e1789f5
PA
107/* The name of the tty (from the `tty' command) that we're giving to
108 the inferior when starting it up. This is only (and should only
191c4426
PA
109 be) used as a transient global by new_tty_prefork,
110 create_tty_session, new_tty and new_tty_postfork, all called from
111 fork_inferior, while forking a new child. */
3cb3b8df 112static const char *inferior_thisrun_terminal;
c906108c
SS
113
114/* Nonzero if our terminal settings are in effect. Zero if the
115 inferior's settings are in effect. Ignored if !gdb_has_a_terminal
116 (). */
117
118int terminal_is_ours;
119
64a0ac84
PA
120#ifdef PROCESS_GROUP_TYPE
121static PROCESS_GROUP_TYPE
122gdb_getpgrp (void)
123{
124 int process_group = -1;
abbb1732 125
64a0ac84
PA
126#ifdef HAVE_TERMIOS
127 process_group = tcgetpgrp (0);
128#endif
129#ifdef HAVE_TERMIO
130 process_group = getpgrp ();
131#endif
132#ifdef HAVE_SGTTY
133 ioctl (0, TIOCGPGRP, &process_group);
134#endif
135 return process_group;
136}
137#endif
138
c5aa993b
JM
139enum
140 {
141 yes, no, have_not_checked
142 }
143gdb_has_a_terminal_flag = have_not_checked;
c906108c 144
7bfc9434
JB
145/* The value of the "interactive-mode" setting. */
146static enum auto_boolean interactive_mode = AUTO_BOOLEAN_AUTO;
147
148/* Implement the "show interactive-mode" option. */
149
150static void
151show_interactive_mode (struct ui_file *file, int from_tty,
152 struct cmd_list_element *c,
153 const char *value)
154{
155 if (interactive_mode == AUTO_BOOLEAN_AUTO)
156 fprintf_filtered (file, "Debugger's interactive mode "
157 "is %s (currently %s).\n",
158 value, gdb_has_a_terminal () ? "on" : "off");
159 else
160 fprintf_filtered (file, "Debugger's interactive mode is %s.\n", value);
161}
162
6a06d660 163/* Set the initial tty state that is to be inherited by new inferiors. */
ea42d6f8 164
6a06d660
PP
165void
166set_initial_gdb_ttystate (void)
167{
168 initial_gdb_ttystate = serial_get_tty_state (stdin_serial);
169}
170
c906108c
SS
171/* Does GDB have a terminal (on stdin)? */
172int
fba45db2 173gdb_has_a_terminal (void)
c906108c 174{
7bfc9434 175 if (interactive_mode != AUTO_BOOLEAN_AUTO)
6b0c4c1f 176 return interactive_mode == AUTO_BOOLEAN_TRUE;
7bfc9434 177
c906108c
SS
178 switch (gdb_has_a_terminal_flag)
179 {
180 case yes:
181 return 1;
182 case no:
183 return 0;
184 case have_not_checked:
2cd58942
AC
185 /* Get all the current tty settings (including whether we have a
186 tty at all!). Can't do this in _initialize_inflow because
187 serial_fdopen() won't work until the serial_ops_list is
188 initialized. */
c906108c
SS
189
190#ifdef F_GETFL
7e1789f5 191 our_terminal_info.tflags = fcntl (0, F_GETFL, 0);
c906108c
SS
192#endif
193
194 gdb_has_a_terminal_flag = no;
c906108c
SS
195 if (stdin_serial != NULL)
196 {
7e1789f5 197 our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);
c906108c 198
7e1789f5 199 if (our_terminal_info.ttystate != NULL)
c906108c
SS
200 {
201 gdb_has_a_terminal_flag = yes;
807bddf3 202#ifdef PROCESS_GROUP_TYPE
7e1789f5 203 our_terminal_info.process_group = gdb_getpgrp ();
807bddf3 204#endif
c906108c
SS
205 }
206 }
207
208 return gdb_has_a_terminal_flag == yes;
209 default:
210 /* "Can't happen". */
211 return 0;
212 }
213}
214
215/* Macro for printing errors from ioctl operations */
216
217#define OOPSY(what) \
218 if (result == -1) \
219 fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
dc672865 220 what, safe_strerror (errno))
c906108c 221
c906108c
SS
222/* Initialize the terminal settings we record for the inferior,
223 before we actually run the inferior. */
224
225void
d6b64346 226child_terminal_init_with_pgrp (int pgrp)
c906108c 227{
6f64ef53
PA
228 struct inferior *inf = current_inferior ();
229 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
230
231#ifdef PROCESS_GROUP_TYPE
232 /* Store the process group even without a terminal as it is used not
233 only to reset the tty foreground process group, but also to
234 interrupt the inferior. */
235 tinfo->process_group = pgrp;
236#endif
237
c906108c
SS
238 if (gdb_has_a_terminal ())
239 {
6c95b8df 240 xfree (tinfo->ttystate);
1e182ce8 241 tinfo->ttystate = serial_copy_tty_state (stdin_serial,
6a06d660 242 initial_gdb_ttystate);
c906108c 243
c906108c 244 /* Make sure that next time we call terminal_inferior (which will be
c5aa993b
JM
245 before the program runs, as it needs to be), we install the new
246 process group. */
c906108c
SS
247 terminal_is_ours = 1;
248 }
249}
250
a790ad35
SC
251/* Save the terminal settings again. This is necessary for the TUI
252 when it switches to TUI or non-TUI mode; curses changes the terminal
253 and gdb must be able to restore it correctly. */
254
255void
3278a9f5 256gdb_save_tty_state (void)
a790ad35
SC
257{
258 if (gdb_has_a_terminal ())
259 {
7e1789f5
PA
260 xfree (our_terminal_info.ttystate);
261 our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);
a790ad35
SC
262 }
263}
264
c906108c 265void
d6b64346 266child_terminal_init (struct target_ops *self)
c906108c
SS
267{
268#ifdef PROCESS_GROUP_TYPE
d6b64346
PA
269 /* This is for Lynx, and should be cleaned up by having Lynx be a
270 separate debugging target with a version of target_terminal_init
271 which passes in the process group to a generic routine which does
272 all the work (and the non-threaded child_terminal_init can just
273 pass in inferior_ptid to the same routine). */
c906108c 274 /* We assume INFERIOR_PID is also the child's process group. */
d6b64346 275 child_terminal_init_with_pgrp (ptid_get_pid (inferior_ptid));
c906108c
SS
276#endif /* PROCESS_GROUP_TYPE */
277}
278
279/* Put the inferior's terminal settings into effect.
4d4ca2a1
DE
280 This is preparation for starting or resuming the inferior.
281
282 N.B. Targets that want to use this with async support must build that
283 support on top of this (e.g., the caller still needs to remove stdin
284 from the event loop). E.g., see linux_nat_terminal_inferior. */
c906108c
SS
285
286void
d6b64346 287child_terminal_inferior (struct target_ops *self)
c906108c 288{
7e1789f5 289 struct inferior *inf;
6c95b8df 290 struct terminal_info *tinfo;
7e1789f5
PA
291
292 if (!terminal_is_ours)
293 return;
294
295 inf = current_inferior ();
6c95b8df 296 tinfo = get_inflow_inferior_data (inf);
7e1789f5
PA
297
298 if (gdb_has_a_terminal ()
6c95b8df
PA
299 && tinfo->ttystate != NULL
300 && tinfo->run_terminal == NULL)
c906108c
SS
301 {
302 int result;
303
304#ifdef F_GETFL
305 /* Is there a reason this is being done twice? It happens both
c5aa993b
JM
306 places we use F_SETFL, so I'm inclined to think perhaps there
307 is some reason, however perverse. Perhaps not though... */
6c95b8df
PA
308 result = fcntl (0, F_SETFL, tinfo->tflags);
309 result = fcntl (0, F_SETFL, tinfo->tflags);
c906108c
SS
310 OOPSY ("fcntl F_SETFL");
311#endif
312
313 /* Because we were careful to not change in or out of raw mode in
c5aa993b
JM
314 terminal_ours, we will not change in our out of raw mode with
315 this call, so we don't flush any input. */
7e1789f5 316 result = serial_set_tty_state (stdin_serial,
6c95b8df 317 tinfo->ttystate);
c906108c
SS
318 OOPSY ("setting tty state");
319
320 if (!job_control)
321 {
c5aa993b 322 sigint_ours = (void (*)()) signal (SIGINT, SIG_IGN);
c906108c 323#ifdef SIGQUIT
c5aa993b 324 sigquit_ours = (void (*)()) signal (SIGQUIT, SIG_IGN);
c906108c
SS
325#endif
326 }
327
328 /* If attach_flag is set, we don't know whether we are sharing a
c5aa993b
JM
329 terminal with the inferior or not. (attaching a process
330 without a terminal is one case where we do not; attaching a
331 process which we ran from the same shell as GDB via `&' is
332 one case where we do, I think (but perhaps this is not
333 `sharing' in the sense that we need to save and restore tty
334 state)). I don't know if there is any way to tell whether we
335 are sharing a terminal. So what we do is to go through all
336 the saving and restoring of the tty state, but ignore errors
337 setting the process group, which will happen if we are not
338 sharing a terminal). */
c906108c
SS
339
340 if (job_control)
341 {
342#ifdef HAVE_TERMIOS
6c95b8df 343 result = tcsetpgrp (0, tinfo->process_group);
181e7f93 344 if (!inf->attach_flag)
c906108c
SS
345 OOPSY ("tcsetpgrp");
346#endif
347
348#ifdef HAVE_SGTTY
6c95b8df 349 result = ioctl (0, TIOCSPGRP, &tinfo->process_group);
181e7f93 350 if (!inf->attach_flag)
c906108c
SS
351 OOPSY ("TIOCSPGRP");
352#endif
353 }
354
355 }
356 terminal_is_ours = 0;
357}
358
359/* Put some of our terminal settings into effect,
360 enough to get proper results from our output,
361 but do not change into or out of RAW mode
362 so that no input is discarded.
363
364 After doing this, either terminal_ours or terminal_inferior
4d4ca2a1
DE
365 should be called to get back to a normal state of affairs.
366
367 N.B. The implementation is (currently) no different than
368 child_terminal_ours. See child_terminal_ours_1. */
c906108c
SS
369
370void
d6b64346 371child_terminal_ours_for_output (struct target_ops *self)
c906108c 372{
d6b64346 373 child_terminal_ours_1 (1);
c906108c
SS
374}
375
376/* Put our terminal settings into effect.
377 First record the inferior's terminal settings
4d4ca2a1
DE
378 so they can be restored properly later.
379
380 N.B. Targets that want to use this with async support must build that
381 support on top of this (e.g., the caller still needs to add stdin to the
382 event loop). E.g., see linux_nat_terminal_ours. */
c906108c
SS
383
384void
d6b64346 385child_terminal_ours (struct target_ops *self)
c906108c 386{
d6b64346 387 child_terminal_ours_1 (0);
c906108c
SS
388}
389
390/* output_only is not used, and should not be used unless we introduce
391 separate terminal_is_ours and terminal_is_ours_for_output
392 flags. */
393
394static void
d6b64346 395child_terminal_ours_1 (int output_only)
c906108c 396{
7e1789f5 397 struct inferior *inf;
6c95b8df 398 struct terminal_info *tinfo;
7e1789f5
PA
399
400 if (terminal_is_ours)
401 return;
402
d9d2d8b6
PA
403 terminal_is_ours = 1;
404
7e1789f5 405 /* Checking inferior->run_terminal is necessary so that
c906108c
SS
406 if GDB is running in the background, it won't block trying
407 to do the ioctl()'s below. Checking gdb_has_a_terminal
408 avoids attempting all the ioctl's when running in batch. */
7e1789f5
PA
409
410 inf = current_inferior ();
6c95b8df 411 tinfo = get_inflow_inferior_data (inf);
7e1789f5 412
6c95b8df 413 if (tinfo->run_terminal != NULL || gdb_has_a_terminal () == 0)
c906108c
SS
414 return;
415
c906108c 416 {
4b69c284 417#ifdef SIGTTOU
c906108c 418 /* Ignore this signal since it will happen when we try to set the
c5aa993b 419 pgrp. */
4b69c284
AC
420 void (*osigttou) () = NULL;
421#endif
c906108c
SS
422 int result;
423
c906108c
SS
424#ifdef SIGTTOU
425 if (job_control)
c5aa993b 426 osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN);
c906108c
SS
427#endif
428
6c95b8df
PA
429 xfree (tinfo->ttystate);
430 tinfo->ttystate = serial_get_tty_state (stdin_serial);
64a0ac84 431
49a834f9 432#ifdef PROCESS_GROUP_TYPE
181e7f93 433 if (!inf->attach_flag)
64a0ac84
PA
434 /* If setpgrp failed in terminal_inferior, this would give us
435 our process group instead of the inferior's. See
436 terminal_inferior for details. */
6c95b8df 437 tinfo->process_group = gdb_getpgrp ();
49a834f9 438#endif
c906108c
SS
439
440 /* Here we used to set ICANON in our ttystate, but I believe this
c5aa993b
JM
441 was an artifact from before when we used readline. Readline sets
442 the tty state when it needs to.
443 FIXME-maybe: However, query() expects non-raw mode and doesn't
444 use readline. Maybe query should use readline (on the other hand,
445 this only matters for HAVE_SGTTY, not termio or termios, I think). */
c906108c
SS
446
447 /* Set tty state to our_ttystate. We don't change in our out of raw
c5aa993b
JM
448 mode, to avoid flushing input. We need to do the same thing
449 regardless of output_only, because we don't have separate
450 terminal_is_ours and terminal_is_ours_for_output flags. It's OK,
1777feb0
MS
451 though, since readline will deal with raw mode when/if it needs
452 to. */
c906108c 453
7e1789f5 454 serial_noflush_set_tty_state (stdin_serial, our_terminal_info.ttystate,
6c95b8df 455 tinfo->ttystate);
c906108c
SS
456
457 if (job_control)
458 {
459#ifdef HAVE_TERMIOS
7e1789f5 460 result = tcsetpgrp (0, our_terminal_info.process_group);
c906108c
SS
461#if 0
462 /* This fails on Ultrix with EINVAL if you run the testsuite
463 in the background with nohup, and then log out. GDB never
464 used to check for an error here, so perhaps there are other
465 such situations as well. */
466 if (result == -1)
3e43a32a 467 fprintf_unfiltered (gdb_stderr,
d6b64346 468 "[tcsetpgrp failed in child_terminal_ours: %s]\n",
dc672865 469 safe_strerror (errno));
c906108c
SS
470#endif
471#endif /* termios */
472
473#ifdef HAVE_SGTTY
7e1789f5 474 result = ioctl (0, TIOCSPGRP, &our_terminal_info.process_group);
c906108c
SS
475#endif
476 }
477
478#ifdef SIGTTOU
479 if (job_control)
480 signal (SIGTTOU, osigttou);
481#endif
482
483 if (!job_control)
484 {
485 signal (SIGINT, sigint_ours);
486#ifdef SIGQUIT
487 signal (SIGQUIT, sigquit_ours);
488#endif
489 }
490
491#ifdef F_GETFL
6c95b8df 492 tinfo->tflags = fcntl (0, F_GETFL, 0);
c906108c
SS
493
494 /* Is there a reason this is being done twice? It happens both
c5aa993b
JM
495 places we use F_SETFL, so I'm inclined to think perhaps there
496 is some reason, however perverse. Perhaps not though... */
7e1789f5
PA
497 result = fcntl (0, F_SETFL, our_terminal_info.tflags);
498 result = fcntl (0, F_SETFL, our_terminal_info.tflags);
c906108c 499#endif
c906108c
SS
500 }
501}
502
6c95b8df
PA
503/* Per-inferior data key. */
504static const struct inferior_data *inflow_inferior_data;
7e1789f5
PA
505
506static void
6c95b8df 507inflow_inferior_data_cleanup (struct inferior *inf, void *arg)
7e1789f5 508{
487ad57c 509 struct terminal_info *info = arg;
6c95b8df 510
487ad57c
YQ
511 xfree (info->run_terminal);
512 xfree (info->ttystate);
513 xfree (info);
6c95b8df
PA
514}
515
516/* Get the current svr4 data. If none is found yet, add it now. This
517 function always returns a valid object. */
518
519static struct terminal_info *
520get_inflow_inferior_data (struct inferior *inf)
521{
522 struct terminal_info *info;
523
524 info = inferior_data (inf, inflow_inferior_data);
525 if (info == NULL)
526 {
41bf6aca 527 info = XCNEW (struct terminal_info);
6c95b8df
PA
528 set_inferior_data (inf, inflow_inferior_data, info);
529 }
7e1789f5 530
6c95b8df 531 return info;
7e1789f5
PA
532}
533
534/* This is a "inferior_exit" observer. Releases the TERMINAL_INFO member
535 of the inferior structure. This field is private to inflow.c, and
536 its type is opaque to the rest of GDB. PID is the target pid of
537 the inferior that is about to be removed from the inferior
538 list. */
539
540static void
a79b8f6e 541inflow_inferior_exit (struct inferior *inf)
7e1789f5 542{
6c95b8df 543 struct terminal_info *info;
7e1789f5 544
6c95b8df
PA
545 info = inferior_data (inf, inflow_inferior_data);
546 if (info != NULL)
547 {
548 xfree (info->run_terminal);
1e182ce8 549 xfree (info->ttystate);
6c95b8df
PA
550 xfree (info);
551 set_inferior_data (inf, inflow_inferior_data, NULL);
552 }
7e1789f5
PA
553}
554
191c4426
PA
555void
556copy_terminal_info (struct inferior *to, struct inferior *from)
557{
6c95b8df
PA
558 struct terminal_info *tinfo_to, *tinfo_from;
559
560 tinfo_to = get_inflow_inferior_data (to);
561 tinfo_from = get_inflow_inferior_data (from);
1e182ce8
UW
562
563 xfree (tinfo_to->run_terminal);
564 xfree (tinfo_to->ttystate);
565
6c95b8df 566 *tinfo_to = *tinfo_from;
1e182ce8 567
6c95b8df
PA
568 if (tinfo_from->run_terminal)
569 tinfo_to->run_terminal
570 = xstrdup (tinfo_from->run_terminal);
1e182ce8
UW
571
572 if (tinfo_from->ttystate)
573 tinfo_to->ttystate
574 = serial_copy_tty_state (stdin_serial, tinfo_from->ttystate);
191c4426
PA
575}
576
c906108c 577void
fba45db2 578term_info (char *arg, int from_tty)
c906108c
SS
579{
580 target_terminal_info (arg, from_tty);
581}
582
c906108c 583void
0a4f40a2 584child_terminal_info (struct target_ops *self, const char *args, int from_tty)
c906108c 585{
7e1789f5 586 struct inferior *inf;
6c95b8df 587 struct terminal_info *tinfo;
7e1789f5 588
c906108c
SS
589 if (!gdb_has_a_terminal ())
590 {
a3f17187 591 printf_filtered (_("This GDB does not control a terminal.\n"));
c906108c
SS
592 return;
593 }
594
7e1789f5
PA
595 if (ptid_equal (inferior_ptid, null_ptid))
596 return;
597
598 inf = current_inferior ();
6c95b8df 599 tinfo = get_inflow_inferior_data (inf);
7e1789f5 600
3e43a32a
MS
601 printf_filtered (_("Inferior's terminal status "
602 "(currently saved by GDB):\n"));
c906108c
SS
603
604 /* First the fcntl flags. */
605 {
606 int flags;
c5aa993b 607
6c95b8df 608 flags = tinfo->tflags;
c906108c
SS
609
610 printf_filtered ("File descriptor flags = ");
611
612#ifndef O_ACCMODE
613#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
614#endif
1777feb0 615 /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */
c906108c
SS
616 switch (flags & (O_ACCMODE))
617 {
c5aa993b
JM
618 case O_RDONLY:
619 printf_filtered ("O_RDONLY");
620 break;
621 case O_WRONLY:
622 printf_filtered ("O_WRONLY");
623 break;
624 case O_RDWR:
625 printf_filtered ("O_RDWR");
626 break;
c906108c
SS
627 }
628 flags &= ~(O_ACCMODE);
629
630#ifdef O_NONBLOCK
c5aa993b 631 if (flags & O_NONBLOCK)
c906108c
SS
632 printf_filtered (" | O_NONBLOCK");
633 flags &= ~O_NONBLOCK;
634#endif
c5aa993b 635
c906108c
SS
636#if defined (O_NDELAY)
637 /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
638 print it as O_NONBLOCK, which is good cause that is what POSIX
639 has, and the flag will already be cleared by the time we get here. */
640 if (flags & O_NDELAY)
641 printf_filtered (" | O_NDELAY");
642 flags &= ~O_NDELAY;
643#endif
644
645 if (flags & O_APPEND)
646 printf_filtered (" | O_APPEND");
647 flags &= ~O_APPEND;
648
649#if defined (O_BINARY)
650 if (flags & O_BINARY)
651 printf_filtered (" | O_BINARY");
652 flags &= ~O_BINARY;
653#endif
654
655 if (flags)
656 printf_filtered (" | 0x%x", flags);
657 printf_filtered ("\n");
658 }
659
660#ifdef PROCESS_GROUP_TYPE
6c95b8df 661 printf_filtered ("Process group = %d\n", (int) tinfo->process_group);
c906108c
SS
662#endif
663
6c95b8df 664 serial_print_tty_state (stdin_serial, tinfo->ttystate, gdb_stdout);
c906108c
SS
665}
666\f
667/* NEW_TTY_PREFORK is called before forking a new child process,
668 so we can record the state of ttys in the child to be formed.
669 TTYNAME is null if we are to share the terminal with gdb;
670 or points to a string containing the name of the desired tty.
671
672 NEW_TTY is called in new child processes under Unix, which will
673 become debugger target processes. This actually switches to
674 the terminal specified in the NEW_TTY_PREFORK call. */
675
676void
3cb3b8df 677new_tty_prefork (const char *ttyname)
c906108c
SS
678{
679 /* Save the name for later, for determining whether we and the child
680 are sharing a tty. */
681 inferior_thisrun_terminal = ttyname;
682}
683
e6d088ec 684#if !defined(__GO32__) && !defined(_WIN32)
bf1d7d9c
JB
685/* If RESULT, assumed to be the return value from a system call, is
686 negative, print the error message indicated by errno and exit.
687 MSG should identify the operation that failed. */
688static void
689check_syscall (const char *msg, int result)
690{
691 if (result < 0)
692 {
693 print_sys_errmsg (msg, errno);
694 _exit (1);
695 }
696}
e6d088ec 697#endif
bf1d7d9c 698
c906108c 699void
fba45db2 700new_tty (void)
c906108c 701{
52f0bd74 702 int tty;
c906108c
SS
703
704 if (inferior_thisrun_terminal == 0)
705 return;
706#if !defined(__GO32__) && !defined(_WIN32)
707#ifdef TIOCNOTTY
708 /* Disconnect the child process from our controlling terminal. On some
709 systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
1777feb0 710 ignore SIGTTOU. */
c5aa993b 711 tty = open ("/dev/tty", O_RDWR);
c906108c
SS
712 if (tty > 0)
713 {
714 void (*osigttou) ();
715
c5aa993b
JM
716 osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN);
717 ioctl (tty, TIOCNOTTY, 0);
718 close (tty);
719 signal (SIGTTOU, osigttou);
c906108c
SS
720 }
721#endif
722
723 /* Now open the specified new terminal. */
c5aa993b 724 tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
bf1d7d9c 725 check_syscall (inferior_thisrun_terminal, tty);
c906108c
SS
726
727 /* Avoid use of dup2; doesn't exist on all systems. */
728 if (tty != 0)
c5aa993b
JM
729 {
730 close (0);
bf1d7d9c 731 check_syscall ("dup'ing tty into fd 0", dup (tty));
c5aa993b 732 }
c906108c 733 if (tty != 1)
c5aa993b
JM
734 {
735 close (1);
bf1d7d9c 736 check_syscall ("dup'ing tty into fd 1", dup (tty));
c5aa993b 737 }
c906108c 738 if (tty != 2)
c5aa993b
JM
739 {
740 close (2);
bf1d7d9c 741 check_syscall ("dup'ing tty into fd 2", dup (tty));
c5aa993b 742 }
83116857
TJB
743
744#ifdef TIOCSCTTY
745 /* Make tty our new controlling terminal. */
746 if (ioctl (tty, TIOCSCTTY, 0) == -1)
747 /* Mention GDB in warning because it will appear in the inferior's
748 terminal instead of GDB's. */
a73c6dcd 749 warning (_("GDB: Failed to set controlling terminal: %s"),
83116857
TJB
750 safe_strerror (errno));
751#endif
752
c906108c 753 if (tty > 2)
c5aa993b
JM
754 close (tty);
755#endif /* !go32 && !win32 */
c906108c 756}
191c4426
PA
757
758/* NEW_TTY_POSTFORK is called after forking a new child process, and
759 adding it to the inferior table, to store the TTYNAME being used by
760 the child, or null if it sharing the terminal with gdb. */
761
762void
763new_tty_postfork (void)
764{
765 /* Save the name for later, for determining whether we and the child
766 are sharing a tty. */
767
768 if (inferior_thisrun_terminal)
6c95b8df
PA
769 {
770 struct inferior *inf = current_inferior ();
771 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
772
773 tinfo->run_terminal = xstrdup (inferior_thisrun_terminal);
774 }
191c4426
PA
775
776 inferior_thisrun_terminal = NULL;
777}
778
c906108c
SS
779\f
780/* Call set_sigint_trap when you need to pass a signal on to an attached
1777feb0 781 process when handling SIGINT. */
c906108c 782
c906108c 783static void
fba45db2 784pass_signal (int signo)
c906108c
SS
785{
786#ifndef _WIN32
dfd4cc63 787 kill (ptid_get_pid (inferior_ptid), SIGINT);
c906108c
SS
788#endif
789}
790
c5aa993b 791static void (*osig) ();
7e1789f5 792static int osig_set;
c906108c
SS
793
794void
fba45db2 795set_sigint_trap (void)
c906108c 796{
181e7f93 797 struct inferior *inf = current_inferior ();
6c95b8df
PA
798 struct terminal_info *tinfo = get_inflow_inferior_data (inf);
799
800 if (inf->attach_flag || tinfo->run_terminal)
c906108c 801 {
c5aa993b 802 osig = (void (*)()) signal (SIGINT, pass_signal);
7e1789f5 803 osig_set = 1;
c906108c 804 }
7e1789f5
PA
805 else
806 osig_set = 0;
c906108c
SS
807}
808
809void
fba45db2 810clear_sigint_trap (void)
c906108c 811{
7e1789f5 812 if (osig_set)
c906108c
SS
813 {
814 signal (SIGINT, osig);
7e1789f5 815 osig_set = 0;
c906108c
SS
816 }
817}
818\f
c906108c 819
83116857
TJB
820/* Create a new session if the inferior will run in a different tty.
821 A session is UNIX's way of grouping processes that share a controlling
822 terminal, so a new one is needed if the inferior terminal will be
823 different from GDB's.
824
825 Returns the session id of the new session, 0 if no session was created
826 or -1 if an error occurred. */
827pid_t
828create_tty_session (void)
829{
830#ifdef HAVE_SETSID
831 pid_t ret;
832
833 if (!job_control || inferior_thisrun_terminal == 0)
834 return 0;
835
836 ret = setsid ();
837 if (ret == -1)
a73c6dcd 838 warning (_("Failed to create new terminal session: setsid: %s"),
83116857
TJB
839 safe_strerror (errno));
840
841 return ret;
842#else
843 return 0;
844#endif /* HAVE_SETSID */
845}
846
c906108c
SS
847/* This is here because this is where we figure out whether we (probably)
848 have job control. Just using job_control only does part of it because
849 setpgid or setpgrp might not exist on a system without job control.
850 It might be considered misplaced (on the other hand, process groups and
851 job control are closely related to ttys).
852
853 For a more clean implementation, in libiberty, put a setpgid which merely
854 calls setpgrp and a setpgrp which does nothing (any system with job control
855 will have one or the other). */
856int
fba45db2 857gdb_setpgid (void)
c906108c
SS
858{
859 int retval = 0;
860
861 if (job_control)
862 {
0200359f
MK
863#if defined (HAVE_TERMIOS) || defined (TIOCGPGRP)
864#ifdef HAVE_SETPGID
865 /* The call setpgid (0, 0) is supposed to work and mean the same
866 thing as this, but on Ultrix 4.2A it fails with EPERM (and
c5aa993b 867 setpgid (getpid (), getpid ()) succeeds). */
c906108c
SS
868 retval = setpgid (getpid (), getpid ());
869#else
0200359f
MK
870#ifdef HAVE_SETPGRP
871#ifdef SETPGRP_VOID
c906108c
SS
872 retval = setpgrp ();
873#else
874 retval = setpgrp (getpid (), getpid ());
0200359f
MK
875#endif
876#endif /* HAVE_SETPGRP */
877#endif /* HAVE_SETPGID */
878#endif /* defined (HAVE_TERMIOS) || defined (TIOCGPGRP) */
c906108c 879 }
0200359f 880
c906108c
SS
881 return retval;
882}
883
0ea3f30e
DJ
884/* Get all the current tty settings (including whether we have a
885 tty at all!). We can't do this in _initialize_inflow because
886 serial_fdopen() won't work until the serial_ops_list is
887 initialized, but we don't want to do it lazily either, so
888 that we can guarantee stdin_serial is opened if there is
889 a terminal. */
890void
891initialize_stdin_serial (void)
892{
893 stdin_serial = serial_fdopen (0);
894}
895
c906108c 896void
fba45db2 897_initialize_inflow (void)
c906108c
SS
898{
899 add_info ("terminal", term_info,
1bedd215 900 _("Print inferior's saved terminal status."));
c906108c 901
7bfc9434
JB
902 add_setshow_auto_boolean_cmd ("interactive-mode", class_support,
903 &interactive_mode, _("\
904Set whether GDB's standard input is a terminal."), _("\
905Show whether GDB's standard input is a terminal."), _("\
906If on, GDB assumes that standard input is a terminal. In practice, it\n\
907means that GDB should wait for the user to answer queries associated to\n\
908commands entered at the command prompt. If off, GDB assumes that standard\n\
909input is not a terminal, and uses the default answer to all queries.\n\
910If auto (the default), determine which mode to use based on the standard\n\
911input settings."),
912 NULL,
913 show_interactive_mode,
914 &setlist, &showlist);
915
c906108c
SS
916 terminal_is_ours = 1;
917
918 /* OK, figure out whether we have job control. If neither termios nor
919 sgtty (i.e. termio or go32), leave job_control 0. */
920
921#if defined (HAVE_TERMIOS)
922 /* Do all systems with termios have the POSIX way of identifying job
923 control? I hope so. */
924#ifdef _POSIX_JOB_CONTROL
925 job_control = 1;
926#else
927#ifdef _SC_JOB_CONTROL
928 job_control = sysconf (_SC_JOB_CONTROL);
929#else
1777feb0 930 job_control = 0; /* Have to assume the worst. */
c5aa993b
JM
931#endif /* _SC_JOB_CONTROL */
932#endif /* _POSIX_JOB_CONTROL */
933#endif /* HAVE_TERMIOS */
c906108c
SS
934
935#ifdef HAVE_SGTTY
936#ifdef TIOCGPGRP
937 job_control = 1;
938#else
939 job_control = 0;
940#endif /* TIOCGPGRP */
941#endif /* sgtty */
7e1789f5 942
7e1789f5 943 observer_attach_inferior_exit (inflow_inferior_exit);
6c95b8df
PA
944
945 inflow_inferior_data
8e260fc0 946 = register_inferior_data_with_cleanup (NULL, inflow_inferior_data_cleanup);
c906108c 947}
This page took 1.46352 seconds and 4 git commands to generate.