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