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. */
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);
263 child_terminal_info (args, from_tty)
269 printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
273 printf_filtered ("fcntl flags = 0x%x, c_iflag = 0x%x, c_oflag = 0x%x,\n",
274 tflags_inferior, sg_inferior.c_iflag, sg_inferior.c_oflag);
275 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
276 sg_inferior.c_cflag, sg_inferior.c_lflag, sg_inferior.c_line);
277 printf_filtered ("c_cc: ");
278 for (i = 0; (i < NCC); i += 1)
279 printf_filtered ("0x%x ", sg_inferior.c_cc[i]);
280 printf_filtered ("\n");
282 #else /* not HAVE_TERMIO */
284 printf_filtered ("fcntl flags = 0x%x, sgttyb.sg_flags = 0x%x, owner pid = %d.\n",
285 tflags_inferior, sg_inferior.sg_flags, pgrp_inferior);
287 #endif /* not HAVE_TERMIO */
289 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
290 printf_filtered ("tchars: ");
291 for (i = 0; i < (int)sizeof (struct tchars); i++)
292 printf_filtered ("0x%x ", ((char *)&tc_inferior)[i]);
293 printf_filtered ("\n");
297 printf_filtered ("ltchars: ");
298 for (i = 0; i < (int)sizeof (struct ltchars); i++)
299 printf_filtered ("0x%x ", ((char *)<c_inferior)[i]);
300 printf_filtered ("\n");
304 printf_filtered ("lmode: 0x%x\n", lmode_inferior);
308 /* NEW_TTY_PREFORK is called before forking a new child process,
309 so we can record the state of ttys in the child to be formed.
310 TTYNAME is null if we are to share the terminal with gdb;
311 or points to a string containing the name of the desired tty.
313 NEW_TTY is called in new child processes under Unix, which will
314 become debugger target processes. This actually switches to
315 the terminal specified in the NEW_TTY_PREFORK call. */
317 new_tty_prefork (ttyname)
320 /* Save the name for later, for determining whether we and the child
321 are sharing a tty. */
322 inferior_thisrun_terminal = ttyname;
330 if (inferior_thisrun_terminal == 0)
334 /* Disconnect the child process from our controlling terminal. */
335 tty = open("/dev/tty", O_RDWR);
338 ioctl(tty, TIOCNOTTY, 0);
343 /* Now open the specified new terminal. */
345 tty = open(inferior_thisrun_terminal, O_RDWR);
348 print_sys_errmsg (inferior_thisrun_terminal, errno);
352 /* Avoid use of dup2; doesn't exist on all systems. */
354 { close (0); dup (tty); }
356 { close (1); dup (tty); }
358 { close (2); dup (tty); }
363 /* Kill the inferior process. Make us have no inferior. */
367 kill_command (arg, from_tty)
371 if (inferior_pid == 0)
372 error ("The program is not being run.");
373 if (!query ("Kill the inferior process? "))
374 error ("Not confirmed.");
375 target_kill (arg, from_tty);
377 /* Killing off the inferior can leave us with a core file. If so,
378 print the state we are left in. */
379 if (target_has_stack) {
380 printf_filtered ("In %s,\n", current_target->to_longname);
381 if (selected_frame == NULL)
382 fputs_filtered ("No selected stack frame.\n", stdout);
384 print_stack_frame (selected_frame, selected_frame_level, 1);
388 /* The inferior process has died. Long live the inferior! */
391 generic_mourn_inferior ()
395 mark_breakpoints_out ();
396 registers_changed ();
398 #ifdef CLEAR_DEFERRED_STORES
399 /* Delete any pending stores to the inferior... */
400 CLEAR_DEFERRED_STORES;
404 if (target_has_stack) {
405 set_current_frame ( create_new_frame (read_register (FP_REGNUM),
407 select_frame (get_current_frame (), 0);
409 set_current_frame (0);
410 select_frame ((FRAME) 0, -1);
412 /* It is confusing to the user for ignore counts to stick around
413 from previous runs of the inferior. So clear them. */
414 breakpoint_clear_ignore_counts ();
418 child_mourn_inferior ()
420 unpush_target (&child_ops);
421 generic_mourn_inferior ();
425 /* This function is just for testing, and on some systems (Sony NewsOS
426 3.2) <sys/user.h> also includes <sys/time.h> which leads to errors
427 (since on this system at least sys/time.h is not protected against
428 multiple inclusion). */
431 try_writing_regs_command (arg, from_tty)
438 if (inferior_pid == 0)
439 error ("There is no inferior process now.");
441 /* A Sun 3/50 or 3/60 (at least) running SunOS 4.0.3 will have a
442 kernel panic if we try to write past the end of the user area.
443 Presumably Sun will fix this bug (it has been reported), but it
444 is tacky to crash the system, so at least on SunOS4 we need to
445 stop writing when we hit the end of the user area. */
446 for (i = 0; i < sizeof (struct user); i += 2)
450 value = call_ptrace (3, inferior_pid, i, 0);
451 call_ptrace (6, inferior_pid, i, value);
454 printf (" Succeeded with address 0x%x; value 0x%x (%d).\n",
457 else if ((i & 0377) == 0)
458 printf (" Failed at 0x%x.\n", i);
464 _initialize_inflow ()
466 add_info ("terminal", term_info,
467 "Print inferior's saved terminal status.");
470 add_com ("try-writing-regs", class_obscure, try_writing_regs_command,
471 "Try writing all locations in inferior's system block.\n\
472 Report which ones can be written.");
475 add_com ("kill", class_run, kill_command,
476 "Kill execution of program being debugged.");
480 ioctl (0, TIOCGETP, &sg_ours);
481 tflags_ours = fcntl (0, F_GETFL, 0);
483 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
484 ioctl (0, TIOCGETC, &tc_ours);
487 ioctl (0, TIOCGLTC, <c_ours);
490 ioctl (0, TIOCLGET, &lmode_ours);
494 ioctl (0, TIOCGPGRP, &pgrp_ours);
495 #endif /* TIOCGPGRP */
497 terminal_is_ours = 1;