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