| 1 | /* Low level interface to ptrace, for GDB when running under Unix. |
| 2 | Copyright (C) 1986, 1987, 1989, 1991 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GDB. |
| 5 | |
| 6 | GDB 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 1, or (at your option) |
| 9 | any later version. |
| 10 | |
| 11 | GDB 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 GDB; see the file COPYING. If not, write to |
| 18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include "defs.h" |
| 22 | #include "param.h" |
| 23 | #include "frame.h" |
| 24 | #include "inferior.h" |
| 25 | #include "command.h" |
| 26 | #include "signals.h" |
| 27 | #include "terminal.h" |
| 28 | #include "target.h" |
| 29 | |
| 30 | #ifdef USG |
| 31 | #include <sys/types.h> |
| 32 | #endif |
| 33 | |
| 34 | /* Some USG-esque systems (some of which are BSD-esque enough so that USG |
| 35 | is not defined) want this header, and it won't do any harm. */ |
| 36 | #include <fcntl.h> |
| 37 | |
| 38 | #include <sys/param.h> |
| 39 | #include <sys/dir.h> |
| 40 | #include <signal.h> |
| 41 | |
| 42 | extern struct target_ops child_ops; |
| 43 | |
| 44 | /* Nonzero if we are debugging an attached outside process |
| 45 | rather than an inferior. */ |
| 46 | |
| 47 | int attach_flag; |
| 48 | |
| 49 | \f |
| 50 | /* Record terminal status separately for debugger and inferior. */ |
| 51 | |
| 52 | static TERMINAL sg_inferior; |
| 53 | static TERMINAL sg_ours; |
| 54 | |
| 55 | static int tflags_inferior; |
| 56 | static int tflags_ours; |
| 57 | |
| 58 | #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN) |
| 59 | static struct tchars tc_inferior; |
| 60 | static struct tchars tc_ours; |
| 61 | #endif |
| 62 | |
| 63 | #ifdef TIOCGLTC |
| 64 | static struct ltchars ltc_inferior; |
| 65 | static struct ltchars ltc_ours; |
| 66 | #endif |
| 67 | |
| 68 | #ifdef TIOCLGET |
| 69 | static int lmode_inferior; |
| 70 | static int lmode_ours; |
| 71 | #endif |
| 72 | |
| 73 | #ifdef TIOCGPGRP |
| 74 | static int pgrp_inferior; |
| 75 | static int pgrp_ours; |
| 76 | #else |
| 77 | static void (*sigint_ours) (); |
| 78 | static void (*sigquit_ours) (); |
| 79 | #endif /* TIOCGPGRP */ |
| 80 | |
| 81 | /* Copy of inferior_io_terminal when inferior was last started. */ |
| 82 | static char *inferior_thisrun_terminal; |
| 83 | |
| 84 | static void terminal_ours_1 (); |
| 85 | |
| 86 | /* Nonzero if our terminal settings are in effect. |
| 87 | Zero if the inferior's settings are in effect. */ |
| 88 | static int terminal_is_ours; |
| 89 | |
| 90 | /* Initialize the terminal settings we record for the inferior, |
| 91 | before we actually run the inferior. */ |
| 92 | |
| 93 | void |
| 94 | terminal_init_inferior () |
| 95 | { |
| 96 | sg_inferior = sg_ours; |
| 97 | tflags_inferior = tflags_ours; |
| 98 | |
| 99 | #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN) |
| 100 | tc_inferior = tc_ours; |
| 101 | #endif |
| 102 | |
| 103 | #ifdef TIOCGLTC |
| 104 | ltc_inferior = ltc_ours; |
| 105 | #endif |
| 106 | |
| 107 | #ifdef TIOCLGET |
| 108 | lmode_inferior = lmode_ours; |
| 109 | #endif |
| 110 | |
| 111 | #ifdef TIOCGPGRP |
| 112 | pgrp_inferior = inferior_pid; |
| 113 | #endif /* TIOCGPGRP */ |
| 114 | |
| 115 | terminal_is_ours = 1; |
| 116 | } |
| 117 | |
| 118 | /* Put the inferior's terminal settings into effect. |
| 119 | This is preparation for starting or resuming the inferior. */ |
| 120 | |
| 121 | void |
| 122 | terminal_inferior () |
| 123 | { |
| 124 | if (terminal_is_ours && inferior_thisrun_terminal == 0) |
| 125 | { |
| 126 | fcntl (0, F_SETFL, tflags_inferior); |
| 127 | fcntl (0, F_SETFL, tflags_inferior); |
| 128 | ioctl (0, TIOCSETN, &sg_inferior); |
| 129 | |
| 130 | #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN) |
| 131 | ioctl (0, TIOCSETC, &tc_inferior); |
| 132 | #endif |
| 133 | #ifdef TIOCGLTC |
| 134 | ioctl (0, TIOCSLTC, <c_inferior); |
| 135 | #endif |
| 136 | #ifdef TIOCLGET |
| 137 | ioctl (0, TIOCLSET, &lmode_inferior); |
| 138 | #endif |
| 139 | |
| 140 | #ifdef TIOCGPGRP |
| 141 | ioctl (0, TIOCSPGRP, &pgrp_inferior); |
| 142 | #else |
| 143 | sigint_ours = (void (*) ()) signal (SIGINT, SIG_IGN); |
| 144 | sigquit_ours = (void (*) ()) signal (SIGQUIT, SIG_IGN); |
| 145 | #endif /* TIOCGPGRP */ |
| 146 | } |
| 147 | terminal_is_ours = 0; |
| 148 | } |
| 149 | |
| 150 | /* Put some of our terminal settings into effect, |
| 151 | enough to get proper results from our output, |
| 152 | but do not change into or out of RAW mode |
| 153 | so that no input is discarded. |
| 154 | |
| 155 | After doing this, either terminal_ours or terminal_inferior |
| 156 | should be called to get back to a normal state of affairs. */ |
| 157 | |
| 158 | void |
| 159 | terminal_ours_for_output () |
| 160 | { |
| 161 | terminal_ours_1 (1); |
| 162 | } |
| 163 | |
| 164 | /* Put our terminal settings into effect. |
| 165 | First record the inferior's terminal settings |
| 166 | so they can be restored properly later. */ |
| 167 | |
| 168 | void |
| 169 | terminal_ours () |
| 170 | { |
| 171 | terminal_ours_1 (0); |
| 172 | } |
| 173 | |
| 174 | static void |
| 175 | terminal_ours_1 (output_only) |
| 176 | int output_only; |
| 177 | { |
| 178 | #ifdef TIOCGPGRP |
| 179 | /* Ignore this signal since it will happen when we try to set the pgrp. */ |
| 180 | void (*osigttou) (); |
| 181 | #endif /* TIOCGPGRP */ |
| 182 | |
| 183 | /* The check for inferior_thisrun_terminal had been commented out |
| 184 | when the call to ioctl (TIOCNOTTY) was commented out. |
| 185 | Checking inferior_thisrun_terminal is necessary so that |
| 186 | if GDB is running in the background, it won't block trying |
| 187 | to do the ioctl()'s below. */ |
| 188 | if (inferior_thisrun_terminal != 0) |
| 189 | return; |
| 190 | |
| 191 | if (!terminal_is_ours) |
| 192 | { |
| 193 | terminal_is_ours = 1; |
| 194 | |
| 195 | #ifdef TIOCGPGRP |
| 196 | osigttou = (void (*) ()) signal (SIGTTOU, SIG_IGN); |
| 197 | |
| 198 | ioctl (0, TIOCGPGRP, &pgrp_inferior); |
| 199 | ioctl (0, TIOCSPGRP, &pgrp_ours); |
| 200 | |
| 201 | signal (SIGTTOU, osigttou); |
| 202 | #else |
| 203 | signal (SIGINT, sigint_ours); |
| 204 | signal (SIGQUIT, sigquit_ours); |
| 205 | #endif /* TIOCGPGRP */ |
| 206 | |
| 207 | tflags_inferior = fcntl (0, F_GETFL, 0); |
| 208 | ioctl (0, TIOCGETP, &sg_inferior); |
| 209 | |
| 210 | #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN) |
| 211 | ioctl (0, TIOCGETC, &tc_inferior); |
| 212 | #endif |
| 213 | #ifdef TIOCGLTC |
| 214 | ioctl (0, TIOCGLTC, <c_inferior); |
| 215 | #endif |
| 216 | #ifdef TIOCLGET |
| 217 | ioctl (0, TIOCLGET, &lmode_inferior); |
| 218 | #endif |
| 219 | } |
| 220 | |
| 221 | #ifdef HAVE_TERMIO |
| 222 | sg_ours.c_lflag |= ICANON; |
| 223 | if (output_only && !(sg_inferior.c_lflag & ICANON)) |
| 224 | sg_ours.c_lflag &= ~ICANON; |
| 225 | #else /* not HAVE_TERMIO */ |
| 226 | sg_ours.sg_flags &= ~RAW & ~CBREAK; |
| 227 | if (output_only) |
| 228 | sg_ours.sg_flags |= (RAW | CBREAK) & sg_inferior.sg_flags; |
| 229 | #endif /* not HAVE_TERMIO */ |
| 230 | |
| 231 | fcntl (0, F_SETFL, tflags_ours); |
| 232 | fcntl (0, F_SETFL, tflags_ours); |
| 233 | ioctl (0, TIOCSETN, &sg_ours); |
| 234 | |
| 235 | #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN) |
| 236 | ioctl (0, TIOCSETC, &tc_ours); |
| 237 | #endif |
| 238 | #ifdef TIOCGLTC |
| 239 | ioctl (0, TIOCSLTC, <c_ours); |
| 240 | #endif |
| 241 | #ifdef TIOCLGET |
| 242 | ioctl (0, TIOCLSET, &lmode_ours); |
| 243 | #endif |
| 244 | |
| 245 | #ifdef HAVE_TERMIO |
| 246 | sg_ours.c_lflag |= ICANON; |
| 247 | #else /* not HAVE_TERMIO */ |
| 248 | sg_ours.sg_flags &= ~RAW & ~CBREAK; |
| 249 | #endif /* not HAVE_TERMIO */ |
| 250 | } |
| 251 | |
| 252 | /* ARGSUSED */ |
| 253 | void |
| 254 | term_info (arg, from_tty) |
| 255 | char *arg; |
| 256 | int from_tty; |
| 257 | { |
| 258 | target_terminal_info (arg, from_tty); |
| 259 | } |
| 260 | |
| 261 | void |
| 262 | child_terminal_info (args, from_tty) |
| 263 | char *args; |
| 264 | int from_tty; |
| 265 | { |
| 266 | register int i; |
| 267 | |
| 268 | printf_filtered ("Inferior's terminal status (currently saved by GDB):\n"); |
| 269 | |
| 270 | #ifdef HAVE_TERMIO |
| 271 | |
| 272 | printf_filtered ("fcntl flags = 0x%x, c_iflag = 0x%x, c_oflag = 0x%x,\n", |
| 273 | tflags_inferior, sg_inferior.c_iflag, sg_inferior.c_oflag); |
| 274 | printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n", |
| 275 | sg_inferior.c_cflag, sg_inferior.c_lflag, sg_inferior.c_line); |
| 276 | printf_filtered ("c_cc: "); |
| 277 | for (i = 0; (i < NCC); i += 1) |
| 278 | printf_filtered ("0x%x ", sg_inferior.c_cc[i]); |
| 279 | printf_filtered ("\n"); |
| 280 | |
| 281 | #else /* not HAVE_TERMIO */ |
| 282 | |
| 283 | printf_filtered ("fcntl flags = 0x%x, sgttyb.sg_flags = 0x%x, owner pid = %d.\n", |
| 284 | tflags_inferior, sg_inferior.sg_flags, pgrp_inferior); |
| 285 | |
| 286 | #endif /* not HAVE_TERMIO */ |
| 287 | |
| 288 | #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN) |
| 289 | printf_filtered ("tchars: "); |
| 290 | for (i = 0; i < (int)sizeof (struct tchars); i++) |
| 291 | printf_filtered ("0x%x ", ((char *)&tc_inferior)[i]); |
| 292 | printf_filtered ("\n"); |
| 293 | #endif |
| 294 | |
| 295 | #ifdef TIOCGLTC |
| 296 | printf_filtered ("ltchars: "); |
| 297 | for (i = 0; i < (int)sizeof (struct ltchars); i++) |
| 298 | printf_filtered ("0x%x ", ((char *)<c_inferior)[i]); |
| 299 | printf_filtered ("\n"); |
| 300 | #endif |
| 301 | |
| 302 | #ifdef TIOCLGET |
| 303 | printf_filtered ("lmode: 0x%x\n", lmode_inferior); |
| 304 | #endif |
| 305 | } |
| 306 | \f |
| 307 | /* NEW_TTY is called in new child processes under Unix, which will |
| 308 | become debugger target processes. |
| 309 | If the TTYNAME argument is non-null, we switch to that tty for further |
| 310 | input and output. In either case, we remember the setup. */ |
| 311 | |
| 312 | void |
| 313 | new_tty (ttyname) |
| 314 | char *ttyname; |
| 315 | { |
| 316 | register int tty; |
| 317 | |
| 318 | /* Save the name for later, for determining whether we and the child |
| 319 | are sharing a tty. */ |
| 320 | inferior_thisrun_terminal = ttyname; |
| 321 | if (ttyname == 0) |
| 322 | return; |
| 323 | |
| 324 | #ifdef TIOCNOTTY |
| 325 | /* Disconnect the child process from our controlling terminal. */ |
| 326 | tty = open("/dev/tty", O_RDWR); |
| 327 | if (tty > 0) |
| 328 | { |
| 329 | ioctl(tty, TIOCNOTTY, 0); |
| 330 | close(tty); |
| 331 | } |
| 332 | #endif |
| 333 | |
| 334 | /* Now open the specified new terminal. */ |
| 335 | |
| 336 | tty = open(ttyname, O_RDWR); |
| 337 | if (tty == -1) |
| 338 | { |
| 339 | print_sys_errmsg (ttyname, errno); |
| 340 | _exit(1); |
| 341 | } |
| 342 | |
| 343 | /* Avoid use of dup2; doesn't exist on all systems. */ |
| 344 | if (tty != 0) |
| 345 | { close (0); dup (tty); } |
| 346 | if (tty != 1) |
| 347 | { close (1); dup (tty); } |
| 348 | if (tty != 2) |
| 349 | { close (2); dup (tty); } |
| 350 | if (tty > 2) |
| 351 | close(tty); |
| 352 | } |
| 353 | \f |
| 354 | /* Kill the inferior process. Make us have no inferior. */ |
| 355 | |
| 356 | /* ARGSUSED */ |
| 357 | static void |
| 358 | kill_command (arg, from_tty) |
| 359 | char *arg; |
| 360 | int from_tty; |
| 361 | { |
| 362 | if (inferior_pid == 0) |
| 363 | error ("The program is not being run."); |
| 364 | if (!query ("Kill the inferior process? ")) |
| 365 | error ("Not confirmed."); |
| 366 | target_kill (arg, from_tty); |
| 367 | |
| 368 | /* Killing off the inferior can leave us with a core file. If so, |
| 369 | print the state we are left in. */ |
| 370 | if (target_has_stack) { |
| 371 | printf_filtered ("In %s,\n", current_target->to_longname); |
| 372 | if (selected_frame == NULL) |
| 373 | fputs_filtered ("No selected stack frame.\n", stdout); |
| 374 | else |
| 375 | print_sel_frame (0); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | /* The inferior process has died. Long live the inferior! */ |
| 380 | |
| 381 | void |
| 382 | generic_mourn_inferior () |
| 383 | { |
| 384 | inferior_pid = 0; |
| 385 | attach_flag = 0; |
| 386 | mark_breakpoints_out (); |
| 387 | registers_changed (); |
| 388 | |
| 389 | #ifdef CLEAR_DEFERRED_STORES |
| 390 | /* Delete any pending stores to the inferior... */ |
| 391 | CLEAR_DEFERRED_STORES; |
| 392 | #endif |
| 393 | |
| 394 | reopen_exec_file (); |
| 395 | if (target_has_stack) { |
| 396 | set_current_frame ( create_new_frame (read_register (FP_REGNUM), |
| 397 | read_pc ())); |
| 398 | select_frame (get_current_frame (), 0); |
| 399 | } else { |
| 400 | set_current_frame (0); |
| 401 | select_frame ((FRAME) 0, -1); |
| 402 | } |
| 403 | /* It is confusing to the user for ignore counts to stick around |
| 404 | from previous runs of the inferior. So clear them. */ |
| 405 | breakpoint_clear_ignore_counts (); |
| 406 | } |
| 407 | |
| 408 | void |
| 409 | child_mourn_inferior () |
| 410 | { |
| 411 | unpush_target (&child_ops); |
| 412 | generic_mourn_inferior (); |
| 413 | } |
| 414 | \f |
| 415 | #if 0 |
| 416 | /* This function is just for testing, and on some systems (Sony NewsOS |
| 417 | 3.2) <sys/user.h> also includes <sys/time.h> which leads to errors |
| 418 | (since on this system at least sys/time.h is not protected against |
| 419 | multiple inclusion). */ |
| 420 | /* ARGSUSED */ |
| 421 | static void |
| 422 | try_writing_regs_command (arg, from_tty) |
| 423 | char *arg; |
| 424 | int from_tty; |
| 425 | { |
| 426 | register int i; |
| 427 | register int value; |
| 428 | |
| 429 | if (inferior_pid == 0) |
| 430 | error ("There is no inferior process now."); |
| 431 | |
| 432 | /* A Sun 3/50 or 3/60 (at least) running SunOS 4.0.3 will have a |
| 433 | kernel panic if we try to write past the end of the user area. |
| 434 | Presumably Sun will fix this bug (it has been reported), but it |
| 435 | is tacky to crash the system, so at least on SunOS4 we need to |
| 436 | stop writing when we hit the end of the user area. */ |
| 437 | for (i = 0; i < sizeof (struct user); i += 2) |
| 438 | { |
| 439 | QUIT; |
| 440 | errno = 0; |
| 441 | value = call_ptrace (3, inferior_pid, i, 0); |
| 442 | call_ptrace (6, inferior_pid, i, value); |
| 443 | if (errno == 0) |
| 444 | { |
| 445 | printf (" Succeeded with address 0x%x; value 0x%x (%d).\n", |
| 446 | i, value, value); |
| 447 | } |
| 448 | else if ((i & 0377) == 0) |
| 449 | printf (" Failed at 0x%x.\n", i); |
| 450 | } |
| 451 | } |
| 452 | #endif |
| 453 | \f |
| 454 | void |
| 455 | _initialize_inflow () |
| 456 | { |
| 457 | add_info ("terminal", term_info, |
| 458 | "Print inferior's saved terminal status."); |
| 459 | |
| 460 | #if 0 |
| 461 | add_com ("try-writing-regs", class_obscure, try_writing_regs_command, |
| 462 | "Try writing all locations in inferior's system block.\n\ |
| 463 | Report which ones can be written."); |
| 464 | #endif |
| 465 | |
| 466 | add_com ("kill", class_run, kill_command, |
| 467 | "Kill execution of program being debugged."); |
| 468 | |
| 469 | inferior_pid = 0; |
| 470 | |
| 471 | ioctl (0, TIOCGETP, &sg_ours); |
| 472 | fcntl (0, F_GETFL, tflags_ours); |
| 473 | |
| 474 | #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN) |
| 475 | ioctl (0, TIOCGETC, &tc_ours); |
| 476 | #endif |
| 477 | #ifdef TIOCGLTC |
| 478 | ioctl (0, TIOCGLTC, <c_ours); |
| 479 | #endif |
| 480 | #ifdef TIOCLGET |
| 481 | ioctl (0, TIOCLGET, &lmode_ours); |
| 482 | #endif |
| 483 | |
| 484 | #ifdef TIOCGPGRP |
| 485 | ioctl (0, TIOCGPGRP, &pgrp_ours); |
| 486 | #endif /* TIOCGPGRP */ |
| 487 | |
| 488 | terminal_is_ours = 1; |
| 489 | } |
| 490 | |