1 /* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1986, 1987, 1989 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. */
27 #include <sys/types.h>
30 /* Some USG-esque systems (some of which are BSD-esque enough so that USG
31 is not defined) want this header, and it won't do any harm. */
34 #include <sys/param.h>
41 #define TIOCGETP TCGETA
43 #define TIOCSETN TCSETA
45 #define TIOCSETP TCSETAF
46 #define TERMINAL struct termio
48 #include <sys/ioctl.h>
51 #define TERMINAL struct sgttyb
54 #ifdef SET_STACK_LIMIT_HUGE
56 #include <sys/resource.h>
57 extern int original_stack_limit
;
58 #endif /* SET_STACK_LIMIT_HUGE */
62 /* Nonzero if we are debugging an attached outside process
63 rather than an inferior. */
68 /* Record terminal status separately for debugger and inferior. */
70 static TERMINAL sg_inferior
;
71 static TERMINAL sg_ours
;
73 static int tflags_inferior
;
74 static int tflags_ours
;
76 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
77 static struct tchars tc_inferior
;
78 static struct tchars tc_ours
;
82 static struct ltchars ltc_inferior
;
83 static struct ltchars ltc_ours
;
87 static int lmode_inferior
;
88 static int lmode_ours
;
92 static int pgrp_inferior
;
95 static int (*sigint_ours
) ();
96 static int (*sigquit_ours
) ();
97 #endif /* TIOCGPGRP */
99 /* Copy of inferior_io_terminal when inferior was last started. */
100 static char *inferior_thisrun_terminal
;
102 static void terminal_ours_1 ();
104 /* Nonzero if our terminal settings are in effect.
105 Zero if the inferior's settings are in effect. */
106 static int terminal_is_ours
;
108 /* Initialize the terminal settings we record for the inferior,
109 before we actually run the inferior. */
112 terminal_init_inferior ()
114 if (remote_debugging
)
117 sg_inferior
= sg_ours
;
118 tflags_inferior
= tflags_ours
;
120 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
121 tc_inferior
= tc_ours
;
125 ltc_inferior
= ltc_ours
;
129 lmode_inferior
= lmode_ours
;
133 pgrp_inferior
= inferior_pid
;
134 #endif /* TIOCGPGRP */
136 terminal_is_ours
= 1;
139 /* Put the inferior's terminal settings into effect.
140 This is preparation for starting or resuming the inferior. */
145 if (remote_debugging
)
148 if (terminal_is_ours
) /* && inferior_thisrun_terminal == 0) */
150 fcntl (0, F_SETFL
, tflags_inferior
);
151 fcntl (0, F_SETFL
, tflags_inferior
);
152 ioctl (0, TIOCSETN
, &sg_inferior
);
154 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
155 ioctl (0, TIOCSETC
, &tc_inferior
);
158 ioctl (0, TIOCSLTC
, <c_inferior
);
161 ioctl (0, TIOCLSET
, &lmode_inferior
);
165 ioctl (0, TIOCSPGRP
, &pgrp_inferior
);
167 sigint_ours
= (int (*) ()) signal (SIGINT
, SIG_IGN
);
168 sigquit_ours
= (int (*) ()) signal (SIGQUIT
, SIG_IGN
);
169 #endif /* TIOCGPGRP */
171 terminal_is_ours
= 0;
174 /* Put some of our terminal settings into effect,
175 enough to get proper results from our output,
176 but do not change into or out of RAW mode
177 so that no input is discarded.
179 After doing this, either terminal_ours or terminal_inferior
180 should be called to get back to a normal state of affairs. */
183 terminal_ours_for_output ()
185 if (remote_debugging
)
191 /* Put our terminal settings into effect.
192 First record the inferior's terminal settings
193 so they can be restored properly later. */
198 if (remote_debugging
)
205 terminal_ours_1 (output_only
)
209 /* Ignore this signal since it will happen when we try to set the pgrp. */
211 #endif /* TIOCGPGRP */
213 if (!terminal_is_ours
) /* && inferior_thisrun_terminal == 0) */
215 terminal_is_ours
= 1;
218 osigttou
= (int (*) ()) signal (SIGTTOU
, SIG_IGN
);
220 ioctl (0, TIOCGPGRP
, &pgrp_inferior
);
221 ioctl (0, TIOCSPGRP
, &pgrp_ours
);
223 signal (SIGTTOU
, osigttou
);
225 signal (SIGINT
, sigint_ours
);
226 signal (SIGQUIT
, sigquit_ours
);
227 #endif /* TIOCGPGRP */
229 tflags_inferior
= fcntl (0, F_GETFL
, 0);
230 ioctl (0, TIOCGETP
, &sg_inferior
);
232 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
233 ioctl (0, TIOCGETC
, &tc_inferior
);
236 ioctl (0, TIOCGLTC
, <c_inferior
);
239 ioctl (0, TIOCLGET
, &lmode_inferior
);
244 sg_ours
.c_lflag
|= ICANON
;
245 if (output_only
&& !(sg_inferior
.c_lflag
& ICANON
))
246 sg_ours
.c_lflag
&= ~ICANON
;
247 #else /* not HAVE_TERMIO */
248 sg_ours
.sg_flags
&= ~RAW
& ~CBREAK
;
250 sg_ours
.sg_flags
|= (RAW
| CBREAK
) & sg_inferior
.sg_flags
;
251 #endif /* not HAVE_TERMIO */
253 fcntl (0, F_SETFL
, tflags_ours
);
254 fcntl (0, F_SETFL
, tflags_ours
);
255 ioctl (0, TIOCSETN
, &sg_ours
);
257 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
258 ioctl (0, TIOCSETC
, &tc_ours
);
261 ioctl (0, TIOCSLTC
, <c_ours
);
264 ioctl (0, TIOCLSET
, &lmode_ours
);
268 sg_ours
.c_lflag
|= ICANON
;
269 #else /* not HAVE_TERMIO */
270 sg_ours
.sg_flags
&= ~RAW
& ~CBREAK
;
271 #endif /* not HAVE_TERMIO */
275 term_status_command ()
279 if (remote_debugging
)
281 printf_filtered ("No terminal status when remote debugging.\n");
285 printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
289 printf_filtered ("fcntl flags = 0x%x, c_iflag = 0x%x, c_oflag = 0x%x,\n",
290 tflags_inferior
, sg_inferior
.c_iflag
, sg_inferior
.c_oflag
);
291 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
292 sg_inferior
.c_cflag
, sg_inferior
.c_lflag
, sg_inferior
.c_line
);
293 printf_filtered ("c_cc: ");
294 for (i
= 0; (i
< NCC
); i
+= 1)
295 printf_filtered ("0x%x ", sg_inferior
.c_cc
[i
]);
296 printf_filtered ("\n");
298 #else /* not HAVE_TERMIO */
300 printf_filtered ("fcntl flags = 0x%x, sgttyb.sg_flags = 0x%x, owner pid = %d.\n",
301 tflags_inferior
, sg_inferior
.sg_flags
, pgrp_inferior
);
303 #endif /* not HAVE_TERMIO */
305 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
306 printf_filtered ("tchars: ");
307 for (i
= 0; i
< sizeof (struct tchars
); i
++)
308 printf_filtered ("0x%x ", ((char *)&tc_inferior
)[i
]);
309 printf_filtered ("\n");
313 printf_filtered ("ltchars: ");
314 for (i
= 0; i
< sizeof (struct ltchars
); i
++)
315 printf_filtered ("0x%x ", ((char *)<c_inferior
)[i
]);
316 printf_filtered ("\n");
317 ioctl (0, TIOCSLTC
, <c_ours
);
321 printf_filtered ("lmode: %x\n", lmode_inferior
);
333 /* Disconnect the child process from our controlling terminal. */
334 tty
= open("/dev/tty", O_RDWR
);
337 ioctl(tty
, TIOCNOTTY
, 0);
342 /* Now open the specified new terminal. */
344 tty
= open(ttyname
, O_RDWR
);
348 /* Avoid use of dup2; doesn't exist on all systems. */
350 { close (0); dup (tty
); }
352 { close (1); dup (tty
); }
354 { close (2); dup (tty
); }
359 /* Start an inferior process and returns its pid.
360 ALLARGS is a string containing shell command to run the program.
361 ENV is the environment vector to pass. */
364 #define SHELL_FILE "/bin/sh"
368 create_inferior (allargs
, env
)
375 extern char *sys_errlist
[];
378 /* If desired, concat something onto the front of ALLARGS.
379 SHELL_COMMAND is the result. */
380 #ifdef SHELL_COMMAND_CONCAT
381 shell_command
= (char *) alloca (strlen (SHELL_COMMAND_CONCAT
) + strlen (allargs
) + 1);
382 strcpy (shell_command
, SHELL_COMMAND_CONCAT
);
383 strcat (shell_command
, allargs
);
385 shell_command
= allargs
;
388 /* exec is said to fail if the executable is open. */
391 #if defined(USG) && !defined(HAVE_VFORK)
398 perror_with_name ("vfork");
403 /* Run inferior in a separate process group. */
404 setpgrp (getpid (), getpid ());
405 #endif /* TIOCGPGRP */
407 #ifdef SET_STACK_LIMIT_HUGE
408 /* Reset the stack limit back to what it was. */
412 getrlimit (RLIMIT_STACK
, &rlim
);
413 rlim
.rlim_cur
= original_stack_limit
;
414 setrlimit (RLIMIT_STACK
, &rlim
);
416 #endif /* SET_STACK_LIMIT_HUGE */
419 inferior_thisrun_terminal
= inferior_io_terminal
;
420 if (inferior_io_terminal
!= 0)
421 new_tty (inferior_io_terminal
);
423 /* It seems that changing the signal handlers for the inferior after
424 a vfork also changes them for the superior. See comments in
425 initialize_signals for how we get the right signal handlers
427 /* Not needed on Sun, at least, and loses there
428 because it clobbers the superior. */
429 /*??? signal (SIGQUIT, SIG_DFL);
430 signal (SIGINT, SIG_DFL); */
433 execle (SHELL_FILE
, "sh", "-c", shell_command
, 0, env
);
435 fprintf (stderr
, "Cannot exec %s: %s.\n", SHELL_FILE
,
436 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
441 #ifdef CREATE_INFERIOR_HOOK
442 CREATE_INFERIOR_HOOK (pid
);
447 /* Kill the inferior process. Make us have no inferior. */
452 if (remote_debugging
)
454 if (inferior_pid
== 0)
455 error ("The program is not being run.");
456 if (!query ("Kill the inferior process? "))
457 error ("Not confirmed.");
466 mark_breakpoints_out ();
467 select_frame ((FRAME
) 0, -1);
469 if (have_core_file_p ())
470 set_current_frame ( create_new_frame (read_register (FP_REGNUM
),
473 set_current_frame (0);
477 /* This function is just for testing, and on some systems (Sony NewsOS
478 3.2) <sys/user.h> also includes <sys/time.h> which leads to errors
479 (since on this system at least sys/time.h is not protected against
480 multiple inclusion). */
482 try_writing_regs_command ()
488 if (inferior_pid
== 0)
489 error ("There is no inferior process now.");
491 /* A Sun 3/50 or 3/60 (at least) running SunOS 4.0.3 will have a
492 kernel panic if we try to write past the end of the user area.
493 Presumably Sun will fix this bug (it has been reported), but it
494 is tacky to crash the system, so at least on SunOS4 we need to
495 stop writing when we hit the end of the user area. */
496 for (i
= 0; i
< sizeof (struct user
); i
+= 2)
500 value
= call_ptrace (3, inferior_pid
, i
, 0);
501 call_ptrace (6, inferior_pid
, i
, value
);
504 printf (" Succeeded with address 0x%x; value 0x%x (%d).\n",
507 else if ((i
& 0377) == 0)
508 printf (" Failed at 0x%x.\n", i
);
514 _initialize_inflow ()
516 add_com ("term-status", class_obscure
, term_status_command
,
517 "Print info on inferior's saved terminal status.");
520 add_com ("try-writing-regs", class_obscure
, try_writing_regs_command
,
521 "Try writing all locations in inferior's system block.\n\
522 Report which ones can be written.");
525 add_com ("kill", class_run
, kill_command
,
526 "Kill execution of program being debugged.");
530 ioctl (0, TIOCGETP
, &sg_ours
);
531 fcntl (0, F_GETFL
, tflags_ours
);
533 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
534 ioctl (0, TIOCGETC
, &tc_ours
);
537 ioctl (0, TIOCGLTC
, <c_ours
);
540 ioctl (0, TIOCLGET
, &lmode_ours
);
544 ioctl (0, TIOCGPGRP
, &pgrp_ours
);
545 #endif /* TIOCGPGRP */
547 terminal_is_ours
= 1;