| 1 | /* Low level interface to ptrace, for GDB when running under Unix. |
| 2 | Copyright 1986, 1987, 1989, 1991, 1992 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "frame.h" |
| 22 | #include "inferior.h" |
| 23 | #include "command.h" |
| 24 | #include "signals.h" |
| 25 | #include "serial.h" |
| 26 | #include "target.h" |
| 27 | |
| 28 | #ifdef USG |
| 29 | #include <sys/types.h> |
| 30 | #endif |
| 31 | |
| 32 | /* Some USG-esque systems (some of which are BSD-esque enough so that USG |
| 33 | is not defined) want this header, and it won't do any harm. */ |
| 34 | #include <fcntl.h> |
| 35 | |
| 36 | #include <sys/param.h> |
| 37 | #include <signal.h> |
| 38 | |
| 39 | static void |
| 40 | kill_command PARAMS ((char *, int)); |
| 41 | |
| 42 | static void |
| 43 | terminal_ours_1 PARAMS ((int)); |
| 44 | |
| 45 | /* Nonzero if we are debugging an attached outside process |
| 46 | rather than an inferior. */ |
| 47 | |
| 48 | int attach_flag; |
| 49 | |
| 50 | \f |
| 51 | /* Record terminal status separately for debugger and inferior. */ |
| 52 | |
| 53 | static serial_t stdin_serial; |
| 54 | |
| 55 | /* TTY state for the inferior. We save it whenever the inferior stops, and |
| 56 | restore it when it resumes. */ |
| 57 | static serial_ttystate inferior_ttystate; |
| 58 | |
| 59 | /* Our own tty state, which we restore every time we need to deal with the |
| 60 | terminal. We only set it once, when GDB first starts. The settings of |
| 61 | flags which readline saves and restores and unimportant. */ |
| 62 | static serial_ttystate our_ttystate; |
| 63 | |
| 64 | /* fcntl flags for us and the inferior. Saved and restored just like |
| 65 | {our,inferior}_ttystate. */ |
| 66 | static int tflags_inferior; |
| 67 | static int tflags_ours; |
| 68 | |
| 69 | /* While the inferior is running, we want SIGINT and SIGQUIT to go to the |
| 70 | inferior only. If we have job control, that takes care of it. If not, |
| 71 | we save our handlers in these two variables and set SIGINT and SIGQUIT |
| 72 | to SIG_IGN. */ |
| 73 | static void (*sigint_ours) (); |
| 74 | static void (*sigquit_ours) (); |
| 75 | |
| 76 | /* The name of the tty (from the `tty' command) that we gave to the inferior |
| 77 | when it was last started. */ |
| 78 | |
| 79 | static char *inferior_thisrun_terminal; |
| 80 | |
| 81 | /* Nonzero if our terminal settings are in effect. Zero if the |
| 82 | inferior's settings are in effect. Ignored if !gdb_has_a_terminal |
| 83 | (). */ |
| 84 | |
| 85 | static int terminal_is_ours; |
| 86 | |
| 87 | enum {yes, no, have_not_checked} gdb_has_a_terminal_flag = have_not_checked; |
| 88 | |
| 89 | /* Does GDB have a terminal (on stdin)? */ |
| 90 | int |
| 91 | gdb_has_a_terminal () |
| 92 | { |
| 93 | switch (gdb_has_a_terminal_flag) |
| 94 | { |
| 95 | case yes: |
| 96 | return 1; |
| 97 | case no: |
| 98 | return 0; |
| 99 | case have_not_checked: |
| 100 | /* Get all the current tty settings (including whether we have a tty at |
| 101 | all!). Can't do this in _initialize_inflow because SERIAL_FDOPEN |
| 102 | won't work until the serial_ops_list is initialized. */ |
| 103 | |
| 104 | #ifdef F_GETFL |
| 105 | tflags_ours = fcntl (0, F_GETFL, 0); |
| 106 | #endif |
| 107 | |
| 108 | gdb_has_a_terminal_flag = no; |
| 109 | stdin_serial = SERIAL_FDOPEN (0); |
| 110 | if (stdin_serial != NULL) |
| 111 | { |
| 112 | our_ttystate = SERIAL_GET_TTY_STATE (stdin_serial); |
| 113 | if (our_ttystate != NULL) |
| 114 | gdb_has_a_terminal_flag = yes; |
| 115 | } |
| 116 | |
| 117 | return gdb_has_a_terminal_flag == yes; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /* Macro for printing errors from ioctl operations */ |
| 122 | |
| 123 | #define OOPSY(what) \ |
| 124 | if (result == -1) \ |
| 125 | fprintf(stderr, "[%s failed in terminal_inferior: %s]\n", \ |
| 126 | what, strerror (errno)) |
| 127 | |
| 128 | static void terminal_ours_1 PARAMS ((int)); |
| 129 | |
| 130 | /* Initialize the terminal settings we record for the inferior, |
| 131 | before we actually run the inferior. */ |
| 132 | |
| 133 | void |
| 134 | terminal_init_inferior () |
| 135 | { |
| 136 | if (gdb_has_a_terminal ()) |
| 137 | { |
| 138 | /* We could just as well copy our_ttystate (if we felt like adding |
| 139 | a new function SERIAL_COPY_TTY_STATE). */ |
| 140 | if (inferior_ttystate) |
| 141 | free (inferior_ttystate); |
| 142 | inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial); |
| 143 | SERIAL_SET_PROCESS_GROUP (stdin_serial, inferior_ttystate, inferior_pid); |
| 144 | |
| 145 | /* Make sure that next time we call terminal_inferior (which will be |
| 146 | before the program runs, as it needs to be), we install the new |
| 147 | process group. */ |
| 148 | terminal_is_ours = 1; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /* Put the inferior's terminal settings into effect. |
| 153 | This is preparation for starting or resuming the inferior. */ |
| 154 | |
| 155 | void |
| 156 | terminal_inferior () |
| 157 | { |
| 158 | if (gdb_has_a_terminal () && terminal_is_ours |
| 159 | && inferior_thisrun_terminal == 0) |
| 160 | { |
| 161 | int result; |
| 162 | |
| 163 | #ifdef F_GETFL |
| 164 | /* Is there a reason this is being done twice? It happens both |
| 165 | places we use F_SETFL, so I'm inclined to think perhaps there |
| 166 | is some reason, however perverse. Perhaps not though... */ |
| 167 | result = fcntl (0, F_SETFL, tflags_inferior); |
| 168 | result = fcntl (0, F_SETFL, tflags_inferior); |
| 169 | OOPSY ("fcntl F_SETFL"); |
| 170 | #endif |
| 171 | |
| 172 | /* Because we were careful to not change in or out of raw mode in |
| 173 | terminal_ours, we will not change in our out of raw mode with |
| 174 | this call, so we don't flush any input. */ |
| 175 | result = SERIAL_SET_TTY_STATE (stdin_serial, inferior_ttystate); |
| 176 | |
| 177 | /* If attach_flag is set, we don't know whether we are sharing a |
| 178 | terminal with the inferior or not. (attaching a process |
| 179 | without a terminal is one case where we do not; attaching a |
| 180 | process which we ran from the same shell as GDB via `&' is |
| 181 | one case where we do, I think (but perhaps this is not |
| 182 | `sharing' in the sense that we need to save and restore tty |
| 183 | state)). I don't know if there is any way to tell whether we |
| 184 | are sharing a terminal. So what we do is to go through all |
| 185 | the saving and restoring of terminal state, but ignore errors |
| 186 | (which will occur, in tcsetpgrp, if we are not sharing a |
| 187 | terminal). */ |
| 188 | |
| 189 | if (!attach_flag) |
| 190 | OOPSY ("setting tty state"); |
| 191 | |
| 192 | if (!job_control) |
| 193 | { |
| 194 | sigint_ours = (void (*) ()) signal (SIGINT, SIG_IGN); |
| 195 | sigquit_ours = (void (*) ()) signal (SIGQUIT, SIG_IGN); |
| 196 | } |
| 197 | } |
| 198 | terminal_is_ours = 0; |
| 199 | } |
| 200 | |
| 201 | /* Put some of our terminal settings into effect, |
| 202 | enough to get proper results from our output, |
| 203 | but do not change into or out of RAW mode |
| 204 | so that no input is discarded. |
| 205 | |
| 206 | After doing this, either terminal_ours or terminal_inferior |
| 207 | should be called to get back to a normal state of affairs. */ |
| 208 | |
| 209 | void |
| 210 | terminal_ours_for_output () |
| 211 | { |
| 212 | terminal_ours_1 (1); |
| 213 | } |
| 214 | |
| 215 | /* Put our terminal settings into effect. |
| 216 | First record the inferior's terminal settings |
| 217 | so they can be restored properly later. */ |
| 218 | |
| 219 | void |
| 220 | terminal_ours () |
| 221 | { |
| 222 | terminal_ours_1 (0); |
| 223 | } |
| 224 | |
| 225 | /* output_only is not used, and should not be used unless we introduce |
| 226 | separate terminal_is_ours and terminal_is_ours_for_output |
| 227 | flags. */ |
| 228 | |
| 229 | static void |
| 230 | terminal_ours_1 (output_only) |
| 231 | int output_only; |
| 232 | { |
| 233 | /* Checking inferior_thisrun_terminal is necessary so that |
| 234 | if GDB is running in the background, it won't block trying |
| 235 | to do the ioctl()'s below. Checking gdb_has_a_terminal |
| 236 | avoids attempting all the ioctl's when running in batch. */ |
| 237 | if (inferior_thisrun_terminal != 0 || gdb_has_a_terminal () == 0) |
| 238 | return; |
| 239 | |
| 240 | if (!terminal_is_ours) |
| 241 | { |
| 242 | /* Ignore this signal since it will happen when we try to set the |
| 243 | pgrp. */ |
| 244 | void (*osigttou) (); |
| 245 | int result; |
| 246 | |
| 247 | terminal_is_ours = 1; |
| 248 | |
| 249 | #ifdef SIGTTOU |
| 250 | if (job_control) |
| 251 | osigttou = (void (*) ()) signal (SIGTTOU, SIG_IGN); |
| 252 | #endif |
| 253 | |
| 254 | if (inferior_ttystate) |
| 255 | free (inferior_ttystate); |
| 256 | inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial); |
| 257 | |
| 258 | /* Here we used to set ICANON in our ttystate, but I believe this |
| 259 | was an artifact from before when we used readline. Readline sets |
| 260 | the tty state when it needs to. */ |
| 261 | |
| 262 | /* Set tty state to our_ttystate. We don't change in our out of raw |
| 263 | mode, to avoid flushing input. We need to do the same thing |
| 264 | regardless of output_only, because we don't have separate |
| 265 | terminal_is_ours and terminal_is_ours_for_output flags. It's OK, |
| 266 | though, since readline will deal with raw mode when/if it needs to. |
| 267 | */ |
| 268 | SERIAL_NOFLUSH_SET_TTY_STATE (stdin_serial, our_ttystate, |
| 269 | inferior_ttystate); |
| 270 | |
| 271 | #ifdef SIGTTOU |
| 272 | if (job_control) |
| 273 | signal (SIGTTOU, osigttou); |
| 274 | #endif |
| 275 | |
| 276 | if (!job_control) |
| 277 | { |
| 278 | signal (SIGINT, sigint_ours); |
| 279 | signal (SIGQUIT, sigquit_ours); |
| 280 | } |
| 281 | |
| 282 | #ifdef F_GETFL |
| 283 | tflags_inferior = fcntl (0, F_GETFL, 0); |
| 284 | |
| 285 | /* Is there a reason this is being done twice? It happens both |
| 286 | places we use F_SETFL, so I'm inclined to think perhaps there |
| 287 | is some reason, however perverse. Perhaps not though... */ |
| 288 | result = fcntl (0, F_SETFL, tflags_ours); |
| 289 | result = fcntl (0, F_SETFL, tflags_ours); |
| 290 | #endif |
| 291 | |
| 292 | result = result; /* lint */ |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /* ARGSUSED */ |
| 297 | void |
| 298 | term_info (arg, from_tty) |
| 299 | char *arg; |
| 300 | int from_tty; |
| 301 | { |
| 302 | target_terminal_info (arg, from_tty); |
| 303 | } |
| 304 | |
| 305 | /* ARGSUSED */ |
| 306 | void |
| 307 | child_terminal_info (args, from_tty) |
| 308 | char *args; |
| 309 | int from_tty; |
| 310 | { |
| 311 | if (!gdb_has_a_terminal ()) |
| 312 | { |
| 313 | printf_filtered ("This GDB does not control a terminal.\n"); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | printf_filtered ("Inferior's terminal status (currently saved by GDB):\n"); |
| 318 | |
| 319 | /* First the fcntl flags. */ |
| 320 | { |
| 321 | int flags; |
| 322 | |
| 323 | flags = tflags_inferior; |
| 324 | |
| 325 | printf_filtered ("File descriptor flags = "); |
| 326 | |
| 327 | #ifndef O_ACCMODE |
| 328 | #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR) |
| 329 | #endif |
| 330 | /* (O_ACCMODE) parens are to avoid Ultrix header file bug */ |
| 331 | switch (flags & (O_ACCMODE)) |
| 332 | { |
| 333 | case O_RDONLY: printf_filtered ("O_RDONLY"); break; |
| 334 | case O_WRONLY: printf_filtered ("O_WRONLY"); break; |
| 335 | case O_RDWR: printf_filtered ("O_RDWR"); break; |
| 336 | } |
| 337 | flags &= ~(O_ACCMODE); |
| 338 | |
| 339 | #ifdef O_NONBLOCK |
| 340 | if (flags & O_NONBLOCK) |
| 341 | printf_filtered (" | O_NONBLOCK"); |
| 342 | flags &= ~O_NONBLOCK; |
| 343 | #endif |
| 344 | |
| 345 | #if defined (O_NDELAY) |
| 346 | /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will |
| 347 | print it as O_NONBLOCK, which is good cause that is what POSIX |
| 348 | has, and the flag will already be cleared by the time we get here. */ |
| 349 | if (flags & O_NDELAY) |
| 350 | printf_filtered (" | O_NDELAY"); |
| 351 | flags &= ~O_NDELAY; |
| 352 | #endif |
| 353 | |
| 354 | if (flags & O_APPEND) |
| 355 | printf_filtered (" | O_APPEND"); |
| 356 | flags &= ~O_APPEND; |
| 357 | |
| 358 | #if defined (O_BINARY) |
| 359 | if (flags & O_BINARY) |
| 360 | printf_filtered (" | O_BINARY"); |
| 361 | flags &= ~O_BINARY; |
| 362 | #endif |
| 363 | |
| 364 | if (flags) |
| 365 | printf_filtered (" | 0x%x", flags); |
| 366 | printf_filtered ("\n"); |
| 367 | } |
| 368 | |
| 369 | SERIAL_PRINT_TTY_STATE (stdin_serial, inferior_ttystate); |
| 370 | } |
| 371 | \f |
| 372 | /* NEW_TTY_PREFORK is called before forking a new child process, |
| 373 | so we can record the state of ttys in the child to be formed. |
| 374 | TTYNAME is null if we are to share the terminal with gdb; |
| 375 | or points to a string containing the name of the desired tty. |
| 376 | |
| 377 | NEW_TTY is called in new child processes under Unix, which will |
| 378 | become debugger target processes. This actually switches to |
| 379 | the terminal specified in the NEW_TTY_PREFORK call. */ |
| 380 | |
| 381 | void |
| 382 | new_tty_prefork (ttyname) |
| 383 | char *ttyname; |
| 384 | { |
| 385 | /* Save the name for later, for determining whether we and the child |
| 386 | are sharing a tty. */ |
| 387 | inferior_thisrun_terminal = ttyname; |
| 388 | } |
| 389 | |
| 390 | void |
| 391 | new_tty () |
| 392 | { |
| 393 | register int tty; |
| 394 | |
| 395 | if (inferior_thisrun_terminal == 0) |
| 396 | return; |
| 397 | #if !defined(__GO32__) |
| 398 | #ifdef TIOCNOTTY |
| 399 | /* Disconnect the child process from our controlling terminal. On some |
| 400 | systems (SVR4 for example), this may cause a SIGTTOU, so temporarily |
| 401 | ignore SIGTTOU. */ |
| 402 | tty = open("/dev/tty", O_RDWR); |
| 403 | if (tty > 0) |
| 404 | { |
| 405 | void (*osigttou) (); |
| 406 | |
| 407 | osigttou = (void (*)()) signal(SIGTTOU, SIG_IGN); |
| 408 | ioctl(tty, TIOCNOTTY, 0); |
| 409 | close(tty); |
| 410 | signal(SIGTTOU, osigttou); |
| 411 | } |
| 412 | #endif |
| 413 | |
| 414 | /* Now open the specified new terminal. */ |
| 415 | |
| 416 | #ifdef USE_O_NOCTTY |
| 417 | tty = open(inferior_thisrun_terminal, O_RDWR | O_NOCTTY); |
| 418 | #else |
| 419 | tty = open(inferior_thisrun_terminal, O_RDWR); |
| 420 | #endif |
| 421 | if (tty == -1) |
| 422 | { |
| 423 | print_sys_errmsg (inferior_thisrun_terminal, errno); |
| 424 | _exit(1); |
| 425 | } |
| 426 | |
| 427 | /* Avoid use of dup2; doesn't exist on all systems. */ |
| 428 | if (tty != 0) |
| 429 | { close (0); dup (tty); } |
| 430 | if (tty != 1) |
| 431 | { close (1); dup (tty); } |
| 432 | if (tty != 2) |
| 433 | { close (2); dup (tty); } |
| 434 | if (tty > 2) |
| 435 | close(tty); |
| 436 | #endif /* !go32 */ |
| 437 | } |
| 438 | \f |
| 439 | /* Kill the inferior process. Make us have no inferior. */ |
| 440 | |
| 441 | /* ARGSUSED */ |
| 442 | static void |
| 443 | kill_command (arg, from_tty) |
| 444 | char *arg; |
| 445 | int from_tty; |
| 446 | { |
| 447 | /* Shouldn't this be target_has_execution? FIXME. */ |
| 448 | if (inferior_pid == 0) |
| 449 | error ("The program is not being run."); |
| 450 | if (!query ("Kill the program being debugged? ")) |
| 451 | error ("Not confirmed."); |
| 452 | target_kill (); |
| 453 | |
| 454 | /* Killing off the inferior can leave us with a core file. If so, |
| 455 | print the state we are left in. */ |
| 456 | if (target_has_stack) { |
| 457 | printf_filtered ("In %s,\n", current_target->to_longname); |
| 458 | if (selected_frame == NULL) |
| 459 | fputs_filtered ("No selected stack frame.\n", stdout); |
| 460 | else |
| 461 | print_stack_frame (selected_frame, selected_frame_level, 1); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /* The inferior process has died. Long live the inferior! */ |
| 466 | |
| 467 | void |
| 468 | generic_mourn_inferior () |
| 469 | { |
| 470 | inferior_pid = 0; |
| 471 | attach_flag = 0; |
| 472 | breakpoint_init_inferior (); |
| 473 | registers_changed (); |
| 474 | |
| 475 | #ifdef CLEAR_DEFERRED_STORES |
| 476 | /* Delete any pending stores to the inferior... */ |
| 477 | CLEAR_DEFERRED_STORES; |
| 478 | #endif |
| 479 | |
| 480 | reopen_exec_file (); |
| 481 | reinit_frame_cache (); |
| 482 | |
| 483 | /* It is confusing to the user for ignore counts to stick around |
| 484 | from previous runs of the inferior. So clear them. */ |
| 485 | breakpoint_clear_ignore_counts (); |
| 486 | } |
| 487 | \f |
| 488 | /* Call set_sigint_trap when you need to pass a signal on to an attached |
| 489 | process when handling SIGINT */ |
| 490 | |
| 491 | /* ARGSUSED */ |
| 492 | static void |
| 493 | pass_signal (signo) |
| 494 | int signo; |
| 495 | { |
| 496 | kill (inferior_pid, SIGINT); |
| 497 | } |
| 498 | |
| 499 | static void (*osig)(); |
| 500 | |
| 501 | void |
| 502 | set_sigint_trap() |
| 503 | { |
| 504 | osig = (void (*) ()) signal (SIGINT, pass_signal); |
| 505 | } |
| 506 | |
| 507 | void |
| 508 | clear_sigint_trap() |
| 509 | { |
| 510 | signal (SIGINT, osig); |
| 511 | } |
| 512 | \f |
| 513 | void |
| 514 | _initialize_inflow () |
| 515 | { |
| 516 | add_info ("terminal", term_info, |
| 517 | "Print inferior's saved terminal status."); |
| 518 | |
| 519 | add_com ("kill", class_run, kill_command, |
| 520 | "Kill execution of program being debugged."); |
| 521 | |
| 522 | inferior_pid = 0; |
| 523 | |
| 524 | terminal_is_ours = 1; |
| 525 | } |