| 1 | /* Low level interface to ptrace, for GDB when running under Unix. |
| 2 | Copyright 1986-87, 1989, 1991-92, 1995, 1998 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 2 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, write to the Free Software |
| 18 | Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | #include "defs.h" |
| 22 | #include "frame.h" |
| 23 | #include "inferior.h" |
| 24 | #include "command.h" |
| 25 | #include "signals.h" |
| 26 | #include "serial.h" |
| 27 | #include "terminal.h" |
| 28 | #include "target.h" |
| 29 | #include "gdbthread.h" |
| 30 | |
| 31 | #include "gdb_string.h" |
| 32 | #include <signal.h> |
| 33 | #include <fcntl.h> |
| 34 | #ifdef HAVE_SYS_SELECT_H |
| 35 | #include <sys/select.h> |
| 36 | #endif |
| 37 | |
| 38 | #ifdef HAVE_TERMIOS |
| 39 | #define PROCESS_GROUP_TYPE pid_t |
| 40 | #endif |
| 41 | |
| 42 | #ifdef HAVE_TERMIO |
| 43 | #define PROCESS_GROUP_TYPE int |
| 44 | #endif |
| 45 | |
| 46 | #ifdef HAVE_SGTTY |
| 47 | #ifdef SHORT_PGRP |
| 48 | /* This is only used for the ultra. Does it have pid_t? */ |
| 49 | #define PROCESS_GROUP_TYPE short |
| 50 | #else |
| 51 | #define PROCESS_GROUP_TYPE int |
| 52 | #endif |
| 53 | #endif /* sgtty */ |
| 54 | |
| 55 | #ifdef HAVE_SYS_IOCTL_H |
| 56 | #include <sys/ioctl.h> |
| 57 | #endif |
| 58 | |
| 59 | #if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN) |
| 60 | static void |
| 61 | handle_sigio PARAMS ((int)); |
| 62 | #endif |
| 63 | |
| 64 | extern void _initialize_inflow PARAMS ((void)); |
| 65 | |
| 66 | static void |
| 67 | pass_signal PARAMS ((int)); |
| 68 | |
| 69 | static void |
| 70 | kill_command PARAMS ((char *, int)); |
| 71 | |
| 72 | static void |
| 73 | terminal_ours_1 PARAMS ((int)); |
| 74 | \f |
| 75 | /* Record terminal status separately for debugger and inferior. */ |
| 76 | |
| 77 | static serial_t stdin_serial; |
| 78 | |
| 79 | /* TTY state for the inferior. We save it whenever the inferior stops, and |
| 80 | restore it when it resumes. */ |
| 81 | static serial_ttystate inferior_ttystate; |
| 82 | |
| 83 | /* Our own tty state, which we restore every time we need to deal with the |
| 84 | terminal. We only set it once, when GDB first starts. The settings of |
| 85 | flags which readline saves and restores and unimportant. */ |
| 86 | static serial_ttystate our_ttystate; |
| 87 | |
| 88 | /* fcntl flags for us and the inferior. Saved and restored just like |
| 89 | {our,inferior}_ttystate. */ |
| 90 | static int tflags_inferior; |
| 91 | static int tflags_ours; |
| 92 | |
| 93 | #ifdef PROCESS_GROUP_TYPE |
| 94 | /* Process group for us and the inferior. Saved and restored just like |
| 95 | {our,inferior}_ttystate. */ |
| 96 | PROCESS_GROUP_TYPE our_process_group; |
| 97 | PROCESS_GROUP_TYPE inferior_process_group; |
| 98 | #endif |
| 99 | |
| 100 | /* While the inferior is running, we want SIGINT and SIGQUIT to go to the |
| 101 | inferior only. If we have job control, that takes care of it. If not, |
| 102 | we save our handlers in these two variables and set SIGINT and SIGQUIT |
| 103 | to SIG_IGN. */ |
| 104 | |
| 105 | static void (*sigint_ours) (); |
| 106 | static void (*sigquit_ours) (); |
| 107 | |
| 108 | /* The name of the tty (from the `tty' command) that we gave to the inferior |
| 109 | when it was last started. */ |
| 110 | |
| 111 | static char *inferior_thisrun_terminal; |
| 112 | |
| 113 | /* Nonzero if our terminal settings are in effect. Zero if the |
| 114 | inferior's settings are in effect. Ignored if !gdb_has_a_terminal |
| 115 | (). */ |
| 116 | |
| 117 | int terminal_is_ours; |
| 118 | |
| 119 | enum |
| 120 | { |
| 121 | yes, no, have_not_checked |
| 122 | } |
| 123 | gdb_has_a_terminal_flag = have_not_checked; |
| 124 | |
| 125 | /* Does GDB have a terminal (on stdin)? */ |
| 126 | int |
| 127 | gdb_has_a_terminal () |
| 128 | { |
| 129 | switch (gdb_has_a_terminal_flag) |
| 130 | { |
| 131 | case yes: |
| 132 | return 1; |
| 133 | case no: |
| 134 | return 0; |
| 135 | case have_not_checked: |
| 136 | /* Get all the current tty settings (including whether we have a tty at |
| 137 | all!). Can't do this in _initialize_inflow because SERIAL_FDOPEN |
| 138 | won't work until the serial_ops_list is initialized. */ |
| 139 | |
| 140 | #ifdef F_GETFL |
| 141 | tflags_ours = fcntl (0, F_GETFL, 0); |
| 142 | #endif |
| 143 | |
| 144 | gdb_has_a_terminal_flag = no; |
| 145 | stdin_serial = SERIAL_FDOPEN (0); |
| 146 | if (stdin_serial != NULL) |
| 147 | { |
| 148 | our_ttystate = SERIAL_GET_TTY_STATE (stdin_serial); |
| 149 | |
| 150 | if (our_ttystate != NULL) |
| 151 | { |
| 152 | gdb_has_a_terminal_flag = yes; |
| 153 | #ifdef HAVE_TERMIOS |
| 154 | our_process_group = tcgetpgrp (0); |
| 155 | #endif |
| 156 | #ifdef HAVE_TERMIO |
| 157 | our_process_group = getpgrp (); |
| 158 | #endif |
| 159 | #ifdef HAVE_SGTTY |
| 160 | ioctl (0, TIOCGPGRP, &our_process_group); |
| 161 | #endif |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return gdb_has_a_terminal_flag == yes; |
| 166 | default: |
| 167 | /* "Can't happen". */ |
| 168 | return 0; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /* Macro for printing errors from ioctl operations */ |
| 173 | |
| 174 | #define OOPSY(what) \ |
| 175 | if (result == -1) \ |
| 176 | fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \ |
| 177 | what, strerror (errno)) |
| 178 | |
| 179 | static void terminal_ours_1 PARAMS ((int)); |
| 180 | |
| 181 | /* Initialize the terminal settings we record for the inferior, |
| 182 | before we actually run the inferior. */ |
| 183 | |
| 184 | void |
| 185 | terminal_init_inferior_with_pgrp (pgrp) |
| 186 | int pgrp; |
| 187 | { |
| 188 | if (gdb_has_a_terminal ()) |
| 189 | { |
| 190 | /* We could just as well copy our_ttystate (if we felt like adding |
| 191 | a new function SERIAL_COPY_TTY_STATE). */ |
| 192 | if (inferior_ttystate) |
| 193 | free (inferior_ttystate); |
| 194 | inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial); |
| 195 | |
| 196 | #ifdef PROCESS_GROUP_TYPE |
| 197 | inferior_process_group = pgrp; |
| 198 | #endif |
| 199 | |
| 200 | /* Make sure that next time we call terminal_inferior (which will be |
| 201 | before the program runs, as it needs to be), we install the new |
| 202 | process group. */ |
| 203 | terminal_is_ours = 1; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void |
| 208 | terminal_init_inferior () |
| 209 | { |
| 210 | #ifdef PROCESS_GROUP_TYPE |
| 211 | /* This is for Lynx, and should be cleaned up by having Lynx be a separate |
| 212 | debugging target with a version of target_terminal_init_inferior which |
| 213 | passes in the process group to a generic routine which does all the work |
| 214 | (and the non-threaded child_terminal_init_inferior can just pass in |
| 215 | inferior_pid to the same routine). */ |
| 216 | /* We assume INFERIOR_PID is also the child's process group. */ |
| 217 | terminal_init_inferior_with_pgrp (PIDGET (inferior_pid)); |
| 218 | #endif /* PROCESS_GROUP_TYPE */ |
| 219 | } |
| 220 | |
| 221 | /* Put the inferior's terminal settings into effect. |
| 222 | This is preparation for starting or resuming the inferior. */ |
| 223 | |
| 224 | void |
| 225 | terminal_inferior () |
| 226 | { |
| 227 | if (gdb_has_a_terminal () && terminal_is_ours |
| 228 | && inferior_thisrun_terminal == 0) |
| 229 | { |
| 230 | int result; |
| 231 | |
| 232 | #ifdef F_GETFL |
| 233 | /* Is there a reason this is being done twice? It happens both |
| 234 | places we use F_SETFL, so I'm inclined to think perhaps there |
| 235 | is some reason, however perverse. Perhaps not though... */ |
| 236 | result = fcntl (0, F_SETFL, tflags_inferior); |
| 237 | result = fcntl (0, F_SETFL, tflags_inferior); |
| 238 | OOPSY ("fcntl F_SETFL"); |
| 239 | #endif |
| 240 | |
| 241 | /* Because we were careful to not change in or out of raw mode in |
| 242 | terminal_ours, we will not change in our out of raw mode with |
| 243 | this call, so we don't flush any input. */ |
| 244 | result = SERIAL_SET_TTY_STATE (stdin_serial, inferior_ttystate); |
| 245 | OOPSY ("setting tty state"); |
| 246 | |
| 247 | if (!job_control) |
| 248 | { |
| 249 | sigint_ours = (void (*)()) signal (SIGINT, SIG_IGN); |
| 250 | #ifdef SIGQUIT |
| 251 | sigquit_ours = (void (*)()) signal (SIGQUIT, SIG_IGN); |
| 252 | #endif |
| 253 | } |
| 254 | |
| 255 | /* If attach_flag is set, we don't know whether we are sharing a |
| 256 | terminal with the inferior or not. (attaching a process |
| 257 | without a terminal is one case where we do not; attaching a |
| 258 | process which we ran from the same shell as GDB via `&' is |
| 259 | one case where we do, I think (but perhaps this is not |
| 260 | `sharing' in the sense that we need to save and restore tty |
| 261 | state)). I don't know if there is any way to tell whether we |
| 262 | are sharing a terminal. So what we do is to go through all |
| 263 | the saving and restoring of the tty state, but ignore errors |
| 264 | setting the process group, which will happen if we are not |
| 265 | sharing a terminal). */ |
| 266 | |
| 267 | if (job_control) |
| 268 | { |
| 269 | #ifdef HAVE_TERMIOS |
| 270 | result = tcsetpgrp (0, inferior_process_group); |
| 271 | if (!attach_flag) |
| 272 | OOPSY ("tcsetpgrp"); |
| 273 | #endif |
| 274 | |
| 275 | #ifdef HAVE_SGTTY |
| 276 | result = ioctl (0, TIOCSPGRP, &inferior_process_group); |
| 277 | if (!attach_flag) |
| 278 | OOPSY ("TIOCSPGRP"); |
| 279 | #endif |
| 280 | } |
| 281 | |
| 282 | } |
| 283 | terminal_is_ours = 0; |
| 284 | } |
| 285 | |
| 286 | /* Put some of our terminal settings into effect, |
| 287 | enough to get proper results from our output, |
| 288 | but do not change into or out of RAW mode |
| 289 | so that no input is discarded. |
| 290 | |
| 291 | After doing this, either terminal_ours or terminal_inferior |
| 292 | should be called to get back to a normal state of affairs. */ |
| 293 | |
| 294 | void |
| 295 | terminal_ours_for_output () |
| 296 | { |
| 297 | terminal_ours_1 (1); |
| 298 | } |
| 299 | |
| 300 | /* Put our terminal settings into effect. |
| 301 | First record the inferior's terminal settings |
| 302 | so they can be restored properly later. */ |
| 303 | |
| 304 | void |
| 305 | terminal_ours () |
| 306 | { |
| 307 | terminal_ours_1 (0); |
| 308 | } |
| 309 | |
| 310 | /* output_only is not used, and should not be used unless we introduce |
| 311 | separate terminal_is_ours and terminal_is_ours_for_output |
| 312 | flags. */ |
| 313 | |
| 314 | static void |
| 315 | terminal_ours_1 (output_only) |
| 316 | int output_only; |
| 317 | { |
| 318 | /* Checking inferior_thisrun_terminal is necessary so that |
| 319 | if GDB is running in the background, it won't block trying |
| 320 | to do the ioctl()'s below. Checking gdb_has_a_terminal |
| 321 | avoids attempting all the ioctl's when running in batch. */ |
| 322 | if (inferior_thisrun_terminal != 0 || gdb_has_a_terminal () == 0) |
| 323 | return; |
| 324 | |
| 325 | if (!terminal_is_ours) |
| 326 | { |
| 327 | /* Ignore this signal since it will happen when we try to set the |
| 328 | pgrp. */ |
| 329 | void (*osigttou) (); |
| 330 | int result; |
| 331 | |
| 332 | terminal_is_ours = 1; |
| 333 | |
| 334 | #ifdef SIGTTOU |
| 335 | if (job_control) |
| 336 | osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN); |
| 337 | #endif |
| 338 | |
| 339 | if (inferior_ttystate) |
| 340 | free (inferior_ttystate); |
| 341 | inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial); |
| 342 | #ifdef HAVE_TERMIOS |
| 343 | inferior_process_group = tcgetpgrp (0); |
| 344 | #endif |
| 345 | #ifdef HAVE_TERMIO |
| 346 | inferior_process_group = getpgrp (); |
| 347 | #endif |
| 348 | #ifdef HAVE_SGTTY |
| 349 | ioctl (0, TIOCGPGRP, &inferior_process_group); |
| 350 | #endif |
| 351 | |
| 352 | /* Here we used to set ICANON in our ttystate, but I believe this |
| 353 | was an artifact from before when we used readline. Readline sets |
| 354 | the tty state when it needs to. |
| 355 | FIXME-maybe: However, query() expects non-raw mode and doesn't |
| 356 | use readline. Maybe query should use readline (on the other hand, |
| 357 | this only matters for HAVE_SGTTY, not termio or termios, I think). */ |
| 358 | |
| 359 | /* Set tty state to our_ttystate. We don't change in our out of raw |
| 360 | mode, to avoid flushing input. We need to do the same thing |
| 361 | regardless of output_only, because we don't have separate |
| 362 | terminal_is_ours and terminal_is_ours_for_output flags. It's OK, |
| 363 | though, since readline will deal with raw mode when/if it needs to. |
| 364 | */ |
| 365 | |
| 366 | SERIAL_NOFLUSH_SET_TTY_STATE (stdin_serial, our_ttystate, |
| 367 | inferior_ttystate); |
| 368 | |
| 369 | if (job_control) |
| 370 | { |
| 371 | #ifdef HAVE_TERMIOS |
| 372 | result = tcsetpgrp (0, our_process_group); |
| 373 | #if 0 |
| 374 | /* This fails on Ultrix with EINVAL if you run the testsuite |
| 375 | in the background with nohup, and then log out. GDB never |
| 376 | used to check for an error here, so perhaps there are other |
| 377 | such situations as well. */ |
| 378 | if (result == -1) |
| 379 | fprintf_unfiltered (gdb_stderr, "[tcsetpgrp failed in terminal_ours: %s]\n", |
| 380 | strerror (errno)); |
| 381 | #endif |
| 382 | #endif /* termios */ |
| 383 | |
| 384 | #ifdef HAVE_SGTTY |
| 385 | result = ioctl (0, TIOCSPGRP, &our_process_group); |
| 386 | #endif |
| 387 | } |
| 388 | |
| 389 | #ifdef SIGTTOU |
| 390 | if (job_control) |
| 391 | signal (SIGTTOU, osigttou); |
| 392 | #endif |
| 393 | |
| 394 | if (!job_control) |
| 395 | { |
| 396 | signal (SIGINT, sigint_ours); |
| 397 | #ifdef SIGQUIT |
| 398 | signal (SIGQUIT, sigquit_ours); |
| 399 | #endif |
| 400 | } |
| 401 | |
| 402 | #ifdef F_GETFL |
| 403 | tflags_inferior = fcntl (0, F_GETFL, 0); |
| 404 | |
| 405 | /* Is there a reason this is being done twice? It happens both |
| 406 | places we use F_SETFL, so I'm inclined to think perhaps there |
| 407 | is some reason, however perverse. Perhaps not though... */ |
| 408 | result = fcntl (0, F_SETFL, tflags_ours); |
| 409 | result = fcntl (0, F_SETFL, tflags_ours); |
| 410 | #endif |
| 411 | |
| 412 | result = result; /* lint */ |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /* ARGSUSED */ |
| 417 | void |
| 418 | term_info (arg, from_tty) |
| 419 | char *arg; |
| 420 | int from_tty; |
| 421 | { |
| 422 | target_terminal_info (arg, from_tty); |
| 423 | } |
| 424 | |
| 425 | /* ARGSUSED */ |
| 426 | void |
| 427 | child_terminal_info (args, from_tty) |
| 428 | char *args; |
| 429 | int from_tty; |
| 430 | { |
| 431 | if (!gdb_has_a_terminal ()) |
| 432 | { |
| 433 | printf_filtered ("This GDB does not control a terminal.\n"); |
| 434 | return; |
| 435 | } |
| 436 | |
| 437 | printf_filtered ("Inferior's terminal status (currently saved by GDB):\n"); |
| 438 | |
| 439 | /* First the fcntl flags. */ |
| 440 | { |
| 441 | int flags; |
| 442 | |
| 443 | flags = tflags_inferior; |
| 444 | |
| 445 | printf_filtered ("File descriptor flags = "); |
| 446 | |
| 447 | #ifndef O_ACCMODE |
| 448 | #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR) |
| 449 | #endif |
| 450 | /* (O_ACCMODE) parens are to avoid Ultrix header file bug */ |
| 451 | switch (flags & (O_ACCMODE)) |
| 452 | { |
| 453 | case O_RDONLY: |
| 454 | printf_filtered ("O_RDONLY"); |
| 455 | break; |
| 456 | case O_WRONLY: |
| 457 | printf_filtered ("O_WRONLY"); |
| 458 | break; |
| 459 | case O_RDWR: |
| 460 | printf_filtered ("O_RDWR"); |
| 461 | break; |
| 462 | } |
| 463 | flags &= ~(O_ACCMODE); |
| 464 | |
| 465 | #ifdef O_NONBLOCK |
| 466 | if (flags & O_NONBLOCK) |
| 467 | printf_filtered (" | O_NONBLOCK"); |
| 468 | flags &= ~O_NONBLOCK; |
| 469 | #endif |
| 470 | |
| 471 | #if defined (O_NDELAY) |
| 472 | /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will |
| 473 | print it as O_NONBLOCK, which is good cause that is what POSIX |
| 474 | has, and the flag will already be cleared by the time we get here. */ |
| 475 | if (flags & O_NDELAY) |
| 476 | printf_filtered (" | O_NDELAY"); |
| 477 | flags &= ~O_NDELAY; |
| 478 | #endif |
| 479 | |
| 480 | if (flags & O_APPEND) |
| 481 | printf_filtered (" | O_APPEND"); |
| 482 | flags &= ~O_APPEND; |
| 483 | |
| 484 | #if defined (O_BINARY) |
| 485 | if (flags & O_BINARY) |
| 486 | printf_filtered (" | O_BINARY"); |
| 487 | flags &= ~O_BINARY; |
| 488 | #endif |
| 489 | |
| 490 | if (flags) |
| 491 | printf_filtered (" | 0x%x", flags); |
| 492 | printf_filtered ("\n"); |
| 493 | } |
| 494 | |
| 495 | #ifdef PROCESS_GROUP_TYPE |
| 496 | printf_filtered ("Process group = %d\n", |
| 497 | (int) inferior_process_group); |
| 498 | #endif |
| 499 | |
| 500 | SERIAL_PRINT_TTY_STATE (stdin_serial, inferior_ttystate, gdb_stdout); |
| 501 | } |
| 502 | \f |
| 503 | /* NEW_TTY_PREFORK is called before forking a new child process, |
| 504 | so we can record the state of ttys in the child to be formed. |
| 505 | TTYNAME is null if we are to share the terminal with gdb; |
| 506 | or points to a string containing the name of the desired tty. |
| 507 | |
| 508 | NEW_TTY is called in new child processes under Unix, which will |
| 509 | become debugger target processes. This actually switches to |
| 510 | the terminal specified in the NEW_TTY_PREFORK call. */ |
| 511 | |
| 512 | void |
| 513 | new_tty_prefork (ttyname) |
| 514 | char *ttyname; |
| 515 | { |
| 516 | /* Save the name for later, for determining whether we and the child |
| 517 | are sharing a tty. */ |
| 518 | inferior_thisrun_terminal = ttyname; |
| 519 | } |
| 520 | |
| 521 | void |
| 522 | new_tty () |
| 523 | { |
| 524 | register int tty; |
| 525 | |
| 526 | if (inferior_thisrun_terminal == 0) |
| 527 | return; |
| 528 | #if !defined(__GO32__) && !defined(_WIN32) |
| 529 | #ifdef TIOCNOTTY |
| 530 | /* Disconnect the child process from our controlling terminal. On some |
| 531 | systems (SVR4 for example), this may cause a SIGTTOU, so temporarily |
| 532 | ignore SIGTTOU. */ |
| 533 | tty = open ("/dev/tty", O_RDWR); |
| 534 | if (tty > 0) |
| 535 | { |
| 536 | void (*osigttou) (); |
| 537 | |
| 538 | osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN); |
| 539 | ioctl (tty, TIOCNOTTY, 0); |
| 540 | close (tty); |
| 541 | signal (SIGTTOU, osigttou); |
| 542 | } |
| 543 | #endif |
| 544 | |
| 545 | /* Now open the specified new terminal. */ |
| 546 | |
| 547 | #ifdef USE_O_NOCTTY |
| 548 | tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY); |
| 549 | #else |
| 550 | tty = open (inferior_thisrun_terminal, O_RDWR); |
| 551 | #endif |
| 552 | if (tty == -1) |
| 553 | { |
| 554 | print_sys_errmsg (inferior_thisrun_terminal, errno); |
| 555 | _exit (1); |
| 556 | } |
| 557 | |
| 558 | /* Avoid use of dup2; doesn't exist on all systems. */ |
| 559 | if (tty != 0) |
| 560 | { |
| 561 | close (0); |
| 562 | dup (tty); |
| 563 | } |
| 564 | if (tty != 1) |
| 565 | { |
| 566 | close (1); |
| 567 | dup (tty); |
| 568 | } |
| 569 | if (tty != 2) |
| 570 | { |
| 571 | close (2); |
| 572 | dup (tty); |
| 573 | } |
| 574 | if (tty > 2) |
| 575 | close (tty); |
| 576 | #endif /* !go32 && !win32 */ |
| 577 | } |
| 578 | \f |
| 579 | /* Kill the inferior process. Make us have no inferior. */ |
| 580 | |
| 581 | /* ARGSUSED */ |
| 582 | static void |
| 583 | kill_command (arg, from_tty) |
| 584 | char *arg; |
| 585 | int from_tty; |
| 586 | { |
| 587 | /* FIXME: This should not really be inferior_pid (or target_has_execution). |
| 588 | It should be a distinct flag that indicates that a target is active, cuz |
| 589 | some targets don't have processes! */ |
| 590 | |
| 591 | if (inferior_pid == 0) |
| 592 | error ("The program is not being run."); |
| 593 | if (!query ("Kill the program being debugged? ")) |
| 594 | error ("Not confirmed."); |
| 595 | target_kill (); |
| 596 | |
| 597 | init_thread_list (); /* Destroy thread info */ |
| 598 | |
| 599 | /* Killing off the inferior can leave us with a core file. If so, |
| 600 | print the state we are left in. */ |
| 601 | if (target_has_stack) |
| 602 | { |
| 603 | printf_filtered ("In %s,\n", target_longname); |
| 604 | if (selected_frame == NULL) |
| 605 | fputs_filtered ("No selected stack frame.\n", gdb_stdout); |
| 606 | else |
| 607 | print_stack_frame (selected_frame, selected_frame_level, 1); |
| 608 | } |
| 609 | } |
| 610 | \f |
| 611 | /* Call set_sigint_trap when you need to pass a signal on to an attached |
| 612 | process when handling SIGINT */ |
| 613 | |
| 614 | /* ARGSUSED */ |
| 615 | static void |
| 616 | pass_signal (signo) |
| 617 | int signo; |
| 618 | { |
| 619 | #ifndef _WIN32 |
| 620 | kill (PIDGET (inferior_pid), SIGINT); |
| 621 | #endif |
| 622 | } |
| 623 | |
| 624 | static void (*osig) (); |
| 625 | |
| 626 | void |
| 627 | set_sigint_trap () |
| 628 | { |
| 629 | if (attach_flag || inferior_thisrun_terminal) |
| 630 | { |
| 631 | osig = (void (*)()) signal (SIGINT, pass_signal); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | void |
| 636 | clear_sigint_trap () |
| 637 | { |
| 638 | if (attach_flag || inferior_thisrun_terminal) |
| 639 | { |
| 640 | signal (SIGINT, osig); |
| 641 | } |
| 642 | } |
| 643 | \f |
| 644 | #if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN) |
| 645 | static void (*old_sigio) (); |
| 646 | |
| 647 | static void |
| 648 | handle_sigio (signo) |
| 649 | int signo; |
| 650 | { |
| 651 | int numfds; |
| 652 | fd_set readfds; |
| 653 | |
| 654 | signal (SIGIO, handle_sigio); |
| 655 | |
| 656 | FD_ZERO (&readfds); |
| 657 | FD_SET (target_activity_fd, &readfds); |
| 658 | numfds = select (target_activity_fd + 1, &readfds, NULL, NULL, NULL); |
| 659 | if (numfds >= 0 && FD_ISSET (target_activity_fd, &readfds)) |
| 660 | { |
| 661 | #ifndef _WIN32 |
| 662 | if ((*target_activity_function) ()) |
| 663 | kill (inferior_pid, SIGINT); |
| 664 | #endif |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | static int old_fcntl_flags; |
| 669 | |
| 670 | void |
| 671 | set_sigio_trap () |
| 672 | { |
| 673 | if (target_activity_function) |
| 674 | { |
| 675 | old_sigio = (void (*)()) signal (SIGIO, handle_sigio); |
| 676 | fcntl (target_activity_fd, F_SETOWN, getpid ()); |
| 677 | old_fcntl_flags = fcntl (target_activity_fd, F_GETFL, 0); |
| 678 | fcntl (target_activity_fd, F_SETFL, old_fcntl_flags | FASYNC); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | void |
| 683 | clear_sigio_trap () |
| 684 | { |
| 685 | if (target_activity_function) |
| 686 | { |
| 687 | signal (SIGIO, old_sigio); |
| 688 | fcntl (target_activity_fd, F_SETFL, old_fcntl_flags); |
| 689 | } |
| 690 | } |
| 691 | #else /* No SIGIO. */ |
| 692 | void |
| 693 | set_sigio_trap () |
| 694 | { |
| 695 | if (target_activity_function) |
| 696 | abort (); |
| 697 | } |
| 698 | |
| 699 | void |
| 700 | clear_sigio_trap () |
| 701 | { |
| 702 | if (target_activity_function) |
| 703 | abort (); |
| 704 | } |
| 705 | #endif /* No SIGIO. */ |
| 706 | \f |
| 707 | |
| 708 | /* This is here because this is where we figure out whether we (probably) |
| 709 | have job control. Just using job_control only does part of it because |
| 710 | setpgid or setpgrp might not exist on a system without job control. |
| 711 | It might be considered misplaced (on the other hand, process groups and |
| 712 | job control are closely related to ttys). |
| 713 | |
| 714 | For a more clean implementation, in libiberty, put a setpgid which merely |
| 715 | calls setpgrp and a setpgrp which does nothing (any system with job control |
| 716 | will have one or the other). */ |
| 717 | int |
| 718 | gdb_setpgid () |
| 719 | { |
| 720 | int retval = 0; |
| 721 | |
| 722 | if (job_control) |
| 723 | { |
| 724 | #if defined (NEED_POSIX_SETPGID) || (defined (HAVE_TERMIOS) && defined (HAVE_SETPGID)) |
| 725 | /* setpgid (0, 0) is supposed to work and mean the same thing as |
| 726 | this, but on Ultrix 4.2A it fails with EPERM (and |
| 727 | setpgid (getpid (), getpid ()) succeeds). */ |
| 728 | retval = setpgid (getpid (), getpid ()); |
| 729 | #else |
| 730 | #if defined (TIOCGPGRP) |
| 731 | #if defined(USG) && !defined(SETPGRP_ARGS) |
| 732 | retval = setpgrp (); |
| 733 | #else |
| 734 | retval = setpgrp (getpid (), getpid ()); |
| 735 | #endif /* USG */ |
| 736 | #endif /* TIOCGPGRP. */ |
| 737 | #endif /* NEED_POSIX_SETPGID */ |
| 738 | } |
| 739 | return retval; |
| 740 | } |
| 741 | |
| 742 | void |
| 743 | _initialize_inflow () |
| 744 | { |
| 745 | add_info ("terminal", term_info, |
| 746 | "Print inferior's saved terminal status."); |
| 747 | |
| 748 | add_com ("kill", class_run, kill_command, |
| 749 | "Kill execution of program being debugged."); |
| 750 | |
| 751 | inferior_pid = 0; |
| 752 | |
| 753 | terminal_is_ours = 1; |
| 754 | |
| 755 | /* OK, figure out whether we have job control. If neither termios nor |
| 756 | sgtty (i.e. termio or go32), leave job_control 0. */ |
| 757 | |
| 758 | #if defined (HAVE_TERMIOS) |
| 759 | /* Do all systems with termios have the POSIX way of identifying job |
| 760 | control? I hope so. */ |
| 761 | #ifdef _POSIX_JOB_CONTROL |
| 762 | job_control = 1; |
| 763 | #else |
| 764 | #ifdef _SC_JOB_CONTROL |
| 765 | job_control = sysconf (_SC_JOB_CONTROL); |
| 766 | #else |
| 767 | job_control = 0; /* have to assume the worst */ |
| 768 | #endif /* _SC_JOB_CONTROL */ |
| 769 | #endif /* _POSIX_JOB_CONTROL */ |
| 770 | #endif /* HAVE_TERMIOS */ |
| 771 | |
| 772 | #ifdef HAVE_SGTTY |
| 773 | #ifdef TIOCGPGRP |
| 774 | job_control = 1; |
| 775 | #else |
| 776 | job_control = 0; |
| 777 | #endif /* TIOCGPGRP */ |
| 778 | #endif /* sgtty */ |
| 779 | } |