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 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.
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.
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. */
30 #include <sys/types.h>
33 /* Some USG-esque systems (some of which are BSD-esque enough so that USG
34 is not defined) want this header, and it won't do any harm. */
37 #include <sys/param.h>
41 extern struct target_ops child_ops;
43 /* Nonzero if we are debugging an attached outside process
44 rather than an inferior. */
49 /* Record terminal status separately for debugger and inferior. */
51 static TERMINAL sg_inferior;
52 static TERMINAL sg_ours;
54 static int tflags_inferior;
55 static int tflags_ours;
57 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
58 static struct tchars tc_inferior;
59 static struct tchars tc_ours;
62 #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
63 static struct ltchars ltc_inferior;
64 static struct ltchars ltc_ours;
68 static int lmode_inferior;
69 static int lmode_ours;
74 static short pgrp_inferior;
75 static short pgrp_ours;
77 static int pgrp_inferior;
81 static void (*sigint_ours) ();
82 static void (*sigquit_ours) ();
83 #endif /* TIOCGPGRP */
85 /* Copy of inferior_io_terminal when inferior was last started. */
86 static char *inferior_thisrun_terminal;
88 static void terminal_ours_1 ();
90 /* Nonzero if our terminal settings are in effect.
91 Zero if the inferior's settings are in effect. */
92 static int terminal_is_ours;
94 /* Initialize the terminal settings we record for the inferior,
95 before we actually run the inferior. */
98 terminal_init_inferior ()
100 sg_inferior = sg_ours;
101 tflags_inferior = tflags_ours;
103 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
104 tc_inferior = tc_ours;
107 #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
108 ltc_inferior = ltc_ours;
112 lmode_inferior = lmode_ours;
116 pgrp_inferior = inferior_pid;
117 #endif /* TIOCGPGRP */
119 terminal_is_ours = 1;
122 /* Put the inferior's terminal settings into effect.
123 This is preparation for starting or resuming the inferior. */
128 if (terminal_is_ours && inferior_thisrun_terminal == 0)
130 fcntl (0, F_SETFL, tflags_inferior);
131 fcntl (0, F_SETFL, tflags_inferior);
132 ioctl (0, TIOCSETN, &sg_inferior);
134 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
135 ioctl (0, TIOCSETC, &tc_inferior);
137 #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
138 ioctl (0, TIOCSLTC, <c_inferior);
141 ioctl (0, TIOCLSET, &lmode_inferior);
145 ioctl (0, TIOCSPGRP, &pgrp_inferior);
147 sigint_ours = (void (*) ()) signal (SIGINT, SIG_IGN);
148 sigquit_ours = (void (*) ()) signal (SIGQUIT, SIG_IGN);
149 #endif /* TIOCGPGRP */
151 terminal_is_ours = 0;
154 /* Put some of our terminal settings into effect,
155 enough to get proper results from our output,
156 but do not change into or out of RAW mode
157 so that no input is discarded.
159 After doing this, either terminal_ours or terminal_inferior
160 should be called to get back to a normal state of affairs. */
163 terminal_ours_for_output ()
168 /* Put our terminal settings into effect.
169 First record the inferior's terminal settings
170 so they can be restored properly later. */
179 terminal_ours_1 (output_only)
183 /* Ignore this signal since it will happen when we try to set the pgrp. */
185 #endif /* TIOCGPGRP */
187 /* The check for inferior_thisrun_terminal had been commented out
188 when the call to ioctl (TIOCNOTTY) was commented out.
189 Checking inferior_thisrun_terminal is necessary so that
190 if GDB is running in the background, it won't block trying
191 to do the ioctl()'s below. */
192 if (inferior_thisrun_terminal != 0)
195 if (!terminal_is_ours)
197 terminal_is_ours = 1;
200 osigttou = (void (*) ()) signal (SIGTTOU, SIG_IGN);
202 ioctl (0, TIOCGPGRP, &pgrp_inferior);
203 ioctl (0, TIOCSPGRP, &pgrp_ours);
205 signal (SIGTTOU, osigttou);
207 signal (SIGINT, sigint_ours);
208 signal (SIGQUIT, sigquit_ours);
209 #endif /* TIOCGPGRP */
211 tflags_inferior = fcntl (0, F_GETFL, 0);
212 ioctl (0, TIOCGETP, &sg_inferior);
214 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
215 ioctl (0, TIOCGETC, &tc_inferior);
217 #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
218 ioctl (0, TIOCGLTC, <c_inferior);
221 ioctl (0, TIOCLGET, &lmode_inferior);
226 sg_ours.c_lflag |= ICANON;
227 if (output_only && !(sg_inferior.c_lflag & ICANON))
228 sg_ours.c_lflag &= ~ICANON;
229 #else /* not HAVE_TERMIO */
230 sg_ours.sg_flags &= ~RAW & ~CBREAK;
232 sg_ours.sg_flags |= (RAW | CBREAK) & sg_inferior.sg_flags;
233 #endif /* not HAVE_TERMIO */
235 fcntl (0, F_SETFL, tflags_ours);
236 fcntl (0, F_SETFL, tflags_ours);
237 ioctl (0, TIOCSETN, &sg_ours);
239 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
240 ioctl (0, TIOCSETC, &tc_ours);
242 #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
243 ioctl (0, TIOCSLTC, <c_ours);
246 ioctl (0, TIOCLSET, &lmode_ours);
250 sg_ours.c_lflag |= ICANON;
251 #else /* not HAVE_TERMIO */
252 sg_ours.sg_flags &= ~RAW & ~CBREAK;
253 #endif /* not HAVE_TERMIO */
258 term_info (arg, from_tty)
262 target_terminal_info (arg, from_tty);
267 child_terminal_info (args, from_tty)
273 printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
277 printf_filtered ("fcntl flags = 0x%x, c_iflag = 0x%x, c_oflag = 0x%x,\n",
278 tflags_inferior, sg_inferior.c_iflag, sg_inferior.c_oflag);
279 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
280 sg_inferior.c_cflag, sg_inferior.c_lflag, sg_inferior.c_line);
281 printf_filtered ("c_cc: ");
282 for (i = 0; (i < NCC); i += 1)
283 printf_filtered ("0x%x ", sg_inferior.c_cc[i]);
284 printf_filtered ("\n");
286 #else /* not HAVE_TERMIO */
288 printf_filtered ("fcntl flags = 0x%x, sgttyb.sg_flags = 0x%x, owner pid = %d.\n",
289 tflags_inferior, sg_inferior.sg_flags, pgrp_inferior);
291 #endif /* not HAVE_TERMIO */
293 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
294 printf_filtered ("tchars: ");
295 for (i = 0; i < (int)sizeof (struct tchars); i++)
296 printf_filtered ("0x%x ", ((char *)&tc_inferior)[i]);
297 printf_filtered ("\n");
300 #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
301 printf_filtered ("ltchars: ");
302 for (i = 0; i < (int)sizeof (struct ltchars); i++)
303 printf_filtered ("0x%x ", ((char *)<c_inferior)[i]);
304 printf_filtered ("\n");
308 printf_filtered ("lmode: 0x%x\n", lmode_inferior);
312 /* NEW_TTY_PREFORK is called before forking a new child process,
313 so we can record the state of ttys in the child to be formed.
314 TTYNAME is null if we are to share the terminal with gdb;
315 or points to a string containing the name of the desired tty.
317 NEW_TTY is called in new child processes under Unix, which will
318 become debugger target processes. This actually switches to
319 the terminal specified in the NEW_TTY_PREFORK call. */
322 new_tty_prefork (ttyname)
325 /* Save the name for later, for determining whether we and the child
326 are sharing a tty. */
327 inferior_thisrun_terminal = ttyname;
335 if (inferior_thisrun_terminal == 0)
339 /* Disconnect the child process from our controlling terminal. */
340 tty = open("/dev/tty", O_RDWR);
343 ioctl(tty, TIOCNOTTY, 0);
348 /* Now open the specified new terminal. */
351 tty = open(inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
353 tty = open(inferior_thisrun_terminal, O_RDWR);
357 print_sys_errmsg (inferior_thisrun_terminal, errno);
361 /* Avoid use of dup2; doesn't exist on all systems. */
363 { close (0); dup (tty); }
365 { close (1); dup (tty); }
367 { close (2); dup (tty); }
372 /* Kill the inferior process. Make us have no inferior. */
376 kill_command (arg, from_tty)
380 if (inferior_pid == 0)
381 error ("The program is not being run.");
382 if (!query ("Kill the inferior process? "))
383 error ("Not confirmed.");
384 target_kill (arg, from_tty);
386 /* Killing off the inferior can leave us with a core file. If so,
387 print the state we are left in. */
388 if (target_has_stack) {
389 printf_filtered ("In %s,\n", current_target->to_longname);
390 if (selected_frame == NULL)
391 fputs_filtered ("No selected stack frame.\n", stdout);
393 print_stack_frame (selected_frame, selected_frame_level, 1);
397 /* The inferior process has died. Long live the inferior! */
400 generic_mourn_inferior ()
404 mark_breakpoints_out ();
405 registers_changed ();
407 #ifdef CLEAR_DEFERRED_STORES
408 /* Delete any pending stores to the inferior... */
409 CLEAR_DEFERRED_STORES;
413 if (target_has_stack) {
414 set_current_frame ( create_new_frame (read_register (FP_REGNUM),
416 select_frame (get_current_frame (), 0);
418 set_current_frame (0);
419 select_frame ((FRAME) 0, -1);
421 /* It is confusing to the user for ignore counts to stick around
422 from previous runs of the inferior. So clear them. */
423 breakpoint_clear_ignore_counts ();
427 child_mourn_inferior ()
429 unpush_target (&child_ops);
430 generic_mourn_inferior ();
434 /* This function is just for testing, and on some systems (Sony NewsOS
435 3.2) <sys/user.h> also includes <sys/time.h> which leads to errors
436 (since on this system at least sys/time.h is not protected against
437 multiple inclusion). */
440 try_writing_regs_command (arg, from_tty)
447 if (inferior_pid == 0)
448 error ("There is no inferior process now.");
450 /* A Sun 3/50 or 3/60 (at least) running SunOS 4.0.3 will have a
451 kernel panic if we try to write past the end of the user area.
452 Presumably Sun will fix this bug (it has been reported), but it
453 is tacky to crash the system, so at least on SunOS4 we need to
454 stop writing when we hit the end of the user area. */
455 for (i = 0; i < sizeof (struct user); i += 2)
459 value = call_ptrace (3, inferior_pid, i, 0);
460 call_ptrace (6, inferior_pid, i, value);
463 printf (" Succeeded with address 0x%x; value 0x%x (%d).\n",
466 else if ((i & 0377) == 0)
467 printf (" Failed at 0x%x.\n", i);
473 _initialize_inflow ()
475 add_info ("terminal", term_info,
476 "Print inferior's saved terminal status.");
479 add_com ("try-writing-regs", class_obscure, try_writing_regs_command,
480 "Try writing all locations in inferior's system block.\n\
481 Report which ones can be written.");
484 add_com ("kill", class_run, kill_command,
485 "Kill execution of program being debugged.");
489 ioctl (0, TIOCGETP, &sg_ours);
490 tflags_ours = fcntl (0, F_GETFL, 0);
492 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
493 ioctl (0, TIOCGETC, &tc_ours);
495 #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
496 ioctl (0, TIOCGLTC, <c_ours);
499 ioctl (0, TIOCLGET, &lmode_ours);
503 ioctl (0, TIOCGPGRP, &pgrp_ours);
504 #endif /* TIOCGPGRP */
506 terminal_is_ours = 1;