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