1 /* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
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)
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.
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. */
31 #include <sys/types.h>
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. */
38 #include <sys/param.h>
42 extern struct target_ops child_ops;
44 /* Nonzero if we are debugging an attached outside process
45 rather than an inferior. */
50 /* Record terminal status separately for debugger and inferior. */
52 static TERMINAL sg_inferior;
53 static TERMINAL sg_ours;
55 static int tflags_inferior;
56 static int tflags_ours;
58 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
59 static struct tchars tc_inferior;
60 static struct tchars tc_ours;
64 static struct ltchars ltc_inferior;
65 static struct ltchars ltc_ours;
69 static int lmode_inferior;
70 static int lmode_ours;
74 static int pgrp_inferior;
77 static void (*sigint_ours) ();
78 static void (*sigquit_ours) ();
79 #endif /* TIOCGPGRP */
81 /* Copy of inferior_io_terminal when inferior was last started. */
82 static char *inferior_thisrun_terminal;
84 static void terminal_ours_1 ();
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;
90 /* Initialize the terminal settings we record for the inferior,
91 before we actually run the inferior. */
94 terminal_init_inferior ()
96 sg_inferior = sg_ours;
97 tflags_inferior = tflags_ours;
99 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
100 tc_inferior = tc_ours;
104 ltc_inferior = ltc_ours;
108 lmode_inferior = lmode_ours;
112 pgrp_inferior = inferior_pid;
113 #endif /* TIOCGPGRP */
115 terminal_is_ours = 1;
118 /* Put the inferior's terminal settings into effect.
119 This is preparation for starting or resuming the inferior. */
124 if (terminal_is_ours && inferior_thisrun_terminal == 0)
126 fcntl (0, F_SETFL, tflags_inferior);
127 fcntl (0, F_SETFL, tflags_inferior);
128 ioctl (0, TIOCSETN, &sg_inferior);
130 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
131 ioctl (0, TIOCSETC, &tc_inferior);
134 ioctl (0, TIOCSLTC, <c_inferior);
137 ioctl (0, TIOCLSET, &lmode_inferior);
141 ioctl (0, TIOCSPGRP, &pgrp_inferior);
143 sigint_ours = (void (*) ()) signal (SIGINT, SIG_IGN);
144 sigquit_ours = (void (*) ()) signal (SIGQUIT, SIG_IGN);
145 #endif /* TIOCGPGRP */
147 terminal_is_ours = 0;
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.
155 After doing this, either terminal_ours or terminal_inferior
156 should be called to get back to a normal state of affairs. */
159 terminal_ours_for_output ()
164 /* Put our terminal settings into effect.
165 First record the inferior's terminal settings
166 so they can be restored properly later. */
175 terminal_ours_1 (output_only)
179 /* Ignore this signal since it will happen when we try to set the pgrp. */
181 #endif /* TIOCGPGRP */
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)
191 if (!terminal_is_ours)
193 terminal_is_ours = 1;
196 osigttou = (void (*) ()) signal (SIGTTOU, SIG_IGN);
198 ioctl (0, TIOCGPGRP, &pgrp_inferior);
199 ioctl (0, TIOCSPGRP, &pgrp_ours);
201 signal (SIGTTOU, osigttou);
203 signal (SIGINT, sigint_ours);
204 signal (SIGQUIT, sigquit_ours);
205 #endif /* TIOCGPGRP */
207 tflags_inferior = fcntl (0, F_GETFL, 0);
208 ioctl (0, TIOCGETP, &sg_inferior);
210 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
211 ioctl (0, TIOCGETC, &tc_inferior);
214 ioctl (0, TIOCGLTC, <c_inferior);
217 ioctl (0, TIOCLGET, &lmode_inferior);
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;
228 sg_ours.sg_flags |= (RAW | CBREAK) & sg_inferior.sg_flags;
229 #endif /* not HAVE_TERMIO */
231 fcntl (0, F_SETFL, tflags_ours);
232 fcntl (0, F_SETFL, tflags_ours);
233 ioctl (0, TIOCSETN, &sg_ours);
235 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
236 ioctl (0, TIOCSETC, &tc_ours);
239 ioctl (0, TIOCSLTC, <c_ours);
242 ioctl (0, TIOCLSET, &lmode_ours);
246 sg_ours.c_lflag |= ICANON;
247 #else /* not HAVE_TERMIO */
248 sg_ours.sg_flags &= ~RAW & ~CBREAK;
249 #endif /* not HAVE_TERMIO */
254 term_info (arg, from_tty)
258 target_terminal_info (arg, from_tty);
262 child_terminal_info (args, from_tty)
268 printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
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");
281 #else /* not HAVE_TERMIO */
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);
286 #endif /* not HAVE_TERMIO */
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");
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");
303 printf_filtered ("lmode: 0x%x\n", lmode_inferior);
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. */
318 /* Save the name for later, for determining whether we and the child
319 are sharing a tty. */
320 inferior_thisrun_terminal = ttyname;
325 /* Disconnect the child process from our controlling terminal. */
326 tty = open("/dev/tty", O_RDWR);
329 ioctl(tty, TIOCNOTTY, 0);
334 /* Now open the specified new terminal. */
336 tty = open(ttyname, O_RDWR);
339 print_sys_errmsg (ttyname, errno);
343 /* Avoid use of dup2; doesn't exist on all systems. */
345 { close (0); dup (tty); }
347 { close (1); dup (tty); }
349 { close (2); dup (tty); }
354 /* Kill the inferior process. Make us have no inferior. */
358 kill_command (arg, from_tty)
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);
369 /* The inferior process has died. Long live the inferior! */
372 generic_mourn_inferior ()
376 mark_breakpoints_out ();
377 registers_changed ();
379 #ifdef CLEAR_DEFERRED_STORES
380 /* Delete any pending stores to the inferior... */
381 CLEAR_DEFERRED_STORES;
384 select_frame ((FRAME) 0, -1);
386 if (target_has_stack)
387 set_current_frame ( create_new_frame (read_register (FP_REGNUM),
390 set_current_frame (0);
391 /* It is confusing to the user for ignore counts to stick around
392 from previous runs of the inferior. So clear them. */
393 breakpoint_clear_ignore_counts ();
397 child_mourn_inferior ()
399 unpush_target (&child_ops);
400 generic_mourn_inferior ();
404 /* This function is just for testing, and on some systems (Sony NewsOS
405 3.2) <sys/user.h> also includes <sys/time.h> which leads to errors
406 (since on this system at least sys/time.h is not protected against
407 multiple inclusion). */
410 try_writing_regs_command (arg, from_tty)
417 if (inferior_pid == 0)
418 error ("There is no inferior process now.");
420 /* A Sun 3/50 or 3/60 (at least) running SunOS 4.0.3 will have a
421 kernel panic if we try to write past the end of the user area.
422 Presumably Sun will fix this bug (it has been reported), but it
423 is tacky to crash the system, so at least on SunOS4 we need to
424 stop writing when we hit the end of the user area. */
425 for (i = 0; i < sizeof (struct user); i += 2)
429 value = call_ptrace (3, inferior_pid, i, 0);
430 call_ptrace (6, inferior_pid, i, value);
433 printf (" Succeeded with address 0x%x; value 0x%x (%d).\n",
436 else if ((i & 0377) == 0)
437 printf (" Failed at 0x%x.\n", i);
443 _initialize_inflow ()
445 add_info ("terminal", term_info,
446 "Print inferior's saved terminal status.");
449 add_com ("try-writing-regs", class_obscure, try_writing_regs_command,
450 "Try writing all locations in inferior's system block.\n\
451 Report which ones can be written.");
454 add_com ("kill", class_run, kill_command,
455 "Kill execution of program being debugged.");
459 ioctl (0, TIOCGETP, &sg_ours);
460 fcntl (0, F_GETFL, tflags_ours);
462 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
463 ioctl (0, TIOCGETC, &tc_ours);
466 ioctl (0, TIOCGLTC, <c_ours);
469 ioctl (0, TIOCLGET, &lmode_ours);
473 ioctl (0, TIOCGPGRP, &pgrp_ours);
474 #endif /* TIOCGPGRP */
476 terminal_is_ours = 1;