9 date 89.03.27.20.12.35; author gnu; state Exp;
14 date 89.02.09.23.23.40; author gnu; state Exp;
19 date 89.02.09.22.28.04; author gnu; state Exp;
30 @General portability changes. Make various local terminal control
31 parameter processing #ifdef the particular IOCTL used to get them.
32 This handles various Sys V/Berkeley merges. Also avoid vfork
36 @/* Low level interface to ptrace, for GDB when running under Unix.
37 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
39 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
40 WARRANTY. No author or distributor accepts responsibility to anyone
41 for the consequences of using it or for whether it serves any
42 particular purpose or works at all, unless he says so in writing.
43 Refer to the GDB General Public License for full details.
45 Everyone is granted permission to copy, modify and redistribute GDB,
46 but only under the conditions described in the GDB General Public
47 License. A copy of this license is supposed to have been given to you
48 along with GDB so you can know your rights and responsibilities. It
49 should be in a file named COPYING. Among other things, the copyright
50 notice and this notice must be preserved on all copies.
52 In other words, go ahead and share GDB, but don't try to stop
53 anyone else from sharing it farther. Help stamp out software hoarding!
61 #include <sys/types.h>
66 #include <sys/param.h>
73 #define TIOCGETP TCGETA
75 #define TIOCSETN TCSETA
77 #define TIOCSETP TCSETAF
78 #define TERMINAL struct termio
80 #include <sys/ioctl.h>
83 #define TERMINAL struct sgttyb
86 #ifdef SET_STACK_LIMIT_HUGE
88 #include <sys/resource.h>
89 extern int original_stack_limit;
90 #endif /* SET_STACK_LIMIT_HUGE */
94 /* Nonzero if we are debugging an attached outside process
95 rather than an inferior. */
100 /* Record terminal status separately for debugger and inferior. */
102 static TERMINAL sg_inferior;
103 static TERMINAL sg_ours;
105 static int tflags_inferior;
106 static int tflags_ours;
109 static struct tchars tc_inferior;
110 static struct tchars tc_ours;
114 static struct ltchars ltc_inferior;
115 static struct ltchars ltc_ours;
116 #endif /* TIOCGLTC */
119 static int lmode_inferior;
120 static int lmode_ours;
124 static int pgrp_inferior;
125 static int pgrp_ours;
127 static int (*sigint_ours) ();
128 static int (*sigquit_ours) ();
129 #endif /* TIOCGPGRP */
131 /* Copy of inferior_io_terminal when inferior was last started. */
132 static char *inferior_thisrun_terminal;
134 static void terminal_ours_1 ();
136 /* Nonzero if our terminal settings are in effect.
137 Zero if the inferior's settings are in effect. */
138 static int terminal_is_ours;
140 /* Initialize the terminal settings we record for the inferior,
141 before we actually run the inferior. */
144 terminal_init_inferior ()
146 if (remote_debugging)
149 sg_inferior = sg_ours;
150 tflags_inferior = tflags_ours;
153 tc_inferior = tc_ours;
157 ltc_inferior = ltc_ours;
161 lmode_inferior = lmode_ours;
165 pgrp_inferior = inferior_pid;
166 #endif /* TIOCGPGRP */
168 terminal_is_ours = 1;
171 /* Put the inferior's terminal settings into effect.
172 This is preparation for starting or resuming the inferior. */
177 if (remote_debugging)
180 if (terminal_is_ours) /* && inferior_thisrun_terminal == 0) */
182 fcntl (0, F_SETFL, tflags_inferior);
183 fcntl (0, F_SETFL, tflags_inferior);
184 ioctl (0, TIOCSETN, &sg_inferior);
186 ioctl (0, TIOCSETC, &tc_inferior);
189 ioctl (0, TIOCSLTC, <c_inferior);
192 ioctl (0, TIOCLSET, &lmode_inferior);
196 ioctl (0, TIOCSPGRP, &pgrp_inferior);
198 sigint_ours = (int (*) ()) signal (SIGINT, SIG_IGN);
199 sigquit_ours = (int (*) ()) signal (SIGQUIT, SIG_IGN);
200 #endif /* TIOCGPGRP */
202 terminal_is_ours = 0;
205 /* Put some of our terminal settings into effect,
206 enough to get proper results from our output,
207 but do not change into or out of RAW mode
208 so that no input is discarded.
210 After doing this, either terminal_ours or terminal_inferior
211 should be called to get back to a normal state of affairs. */
214 terminal_ours_for_output ()
216 if (remote_debugging)
222 /* Put our terminal settings into effect.
223 First record the inferior's terminal settings
224 so they can be restored properly later. */
229 if (remote_debugging)
236 terminal_ours_1 (output_only)
240 /* Ignore this signal since it will happen when we try to set the pgrp. */
242 #endif /* TIOCGPGRP */
244 if (!terminal_is_ours) /* && inferior_thisrun_terminal == 0) */
246 terminal_is_ours = 1;
249 osigttou = signal (SIGTTOU, SIG_IGN);
251 ioctl (0, TIOCGPGRP, &pgrp_inferior);
252 ioctl (0, TIOCSPGRP, &pgrp_ours);
254 signal (SIGTTOU, osigttou);
256 signal (SIGINT, sigint_ours);
257 signal (SIGQUIT, sigquit_ours);
258 #endif /* TIOCGPGRP */
260 tflags_inferior = fcntl (0, F_GETFL, 0);
261 ioctl (0, TIOCGETP, &sg_inferior);
264 ioctl (0, TIOCGETC, &tc_inferior);
267 ioctl (0, TIOCGLTC, <c_inferior);
270 ioctl (0, TIOCLGET, &lmode_inferior);
275 sg_ours.c_lflag |= ICANON;
276 if (output_only && !(sg_inferior.c_lflag & ICANON))
277 sg_ours.c_lflag &= ~ICANON;
278 #else /* not HAVE_TERMIO */
279 sg_ours.sg_flags &= ~RAW & ~CBREAK;
281 sg_ours.sg_flags |= (RAW | CBREAK) & sg_inferior.sg_flags;
282 #endif /* not HAVE_TERMIO */
284 fcntl (0, F_SETFL, tflags_ours);
285 fcntl (0, F_SETFL, tflags_ours);
286 ioctl (0, TIOCSETN, &sg_ours);
289 ioctl (0, TIOCSETC, &tc_ours);
292 ioctl (0, TIOCSLTC, <c_ours);
295 ioctl (0, TIOCLSET, &lmode_ours);
300 sg_ours.c_lflag |= ICANON;
301 #else /* not HAVE_TERMIO */
302 sg_ours.sg_flags &= ~RAW & ~CBREAK;
303 #endif /* not HAVE_TERMIO */
307 term_status_command ()
311 if (remote_debugging)
313 printf ("No terminal status when remote debugging.\n");
317 printf ("Inferior's terminal status (currently saved by GDB):\n");
321 printf ("fcntl flags = 0x%x, c_iflag = 0x%x, c_oflag = 0x%x,\n",
322 tflags_inferior, sg_inferior.c_iflag, sg_inferior.c_oflag);
323 printf ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
324 sg_inferior.c_cflag, sg_inferior.c_lflag, sg_inferior.c_line);
326 for (i = 0; (i < NCC); i += 1)
327 printf ("0x%x ", sg_inferior.c_cc[i]);
330 #else /* not HAVE_TERMIO */
332 printf ("fcntl flags = 0x%x, sgttyb.sg_flags = 0x%x, owner pid = %d.\n",
333 tflags_inferior, sg_inferior.sg_flags, pgrp_inferior);
335 #endif /* not HAVE_TERMIO */
339 for (i = 0; i < sizeof (struct tchars); i++)
340 printf ("0x%x ", ((char *)&tc_inferior)[i]);
345 printf ("ltchars: ");
346 for (i = 0; i < sizeof (struct ltchars); i++)
347 printf ("0x%x ", ((char *)<c_inferior)[i]);
349 ioctl (0, TIOCSLTC, <c_ours);
353 printf ("lmode: %x\n", lmode_inferior);
365 /* Disconnect the child process from our controlling terminal. */
366 tty = open("/dev/tty", O_RDWR);
369 ioctl(tty, TIOCNOTTY, 0);
374 /* Now open the specified new terminal. */
376 tty = open(ttyname, O_RDWR);
386 /* Start an inferior process and returns its pid.
387 ALLARGS is a string containing shell command to run the program.
388 ENV is the environment vector to pass. */
391 #define SHELL_FILE "/bin/sh"
395 create_inferior (allargs, env)
402 extern char *sys_errlist[];
405 /* If desired, concat something onto the front of ALLARGS.
406 SHELL_COMMAND is the result. */
407 #ifdef SHELL_COMMAND_CONCAT
408 shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + strlen (allargs) + 1);
409 strcpy (shell_command, SHELL_COMMAND_CONCAT);
410 strcat (shell_command, allargs);
412 shell_command = allargs;
415 /* exec is said to fail if the executable is open. */
420 perror_with_name ("fork");
425 /* Run inferior in a separate process group. */
426 setpgrp (getpid (), getpid ());
427 #endif /* TIOCGPGRP */
429 #ifdef SET_STACK_LIMIT_HUGE
430 /* Reset the stack limit back to what it was. */
434 getrlimit (RLIMIT_STACK, &rlim);
435 rlim.rlim_cur = original_stack_limit;
436 setrlimit (RLIMIT_STACK, &rlim);
438 #endif /* SET_STACK_LIMIT_HUGE */
441 inferior_thisrun_terminal = inferior_io_terminal;
442 if (inferior_io_terminal != 0)
443 new_tty (inferior_io_terminal);
445 /* Not needed on Sun, at least, and loses there
446 because it clobbers the superior. */
447 /*??? signal (SIGQUIT, SIG_DFL);
448 signal (SIGINT, SIG_DFL); */
451 execle (SHELL_FILE, "sh", "-c", shell_command, 0, env);
453 fprintf (stderr, "Cannot exec %s: %s.\n", SHELL_FILE,
454 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
461 /* Kill the inferior process. Make us have no inferior. */
466 if (remote_debugging)
468 if (inferior_pid == 0)
469 error ("The program is not being run.");
470 if (!query ("Kill the inferior process? "))
471 error ("Not confirmed.");
480 mark_breakpoints_out ();
481 select_frame ( (FRAME) 0, -1);
483 if (have_core_file_p ())
484 set_current_frame ( create_new_frame (read_register (FP_REGNUM),
489 try_writing_regs_command ()
495 if (inferior_pid == 0)
496 error ("There is no inferior process now.");
498 for (i = 0; ; i += 2)
502 value = call_ptrace (3, inferior_pid, i, 0);
503 call_ptrace (6, inferior_pid, i, value);
506 printf (" Succeeded with address 0x%x; value 0x%x (%d).\n",
509 else if ((i & 0377) == 0)
510 printf (" Failed at 0x%x.\n", i);
515 _initialize_inflow ()
517 add_com ("term-status", class_obscure, term_status_command,
518 "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.");
524 add_com ("kill", class_run, kill_command,
525 "Kill execution of program being debugged.");
529 ioctl (0, TIOCGETP, &sg_ours);
530 fcntl (0, F_GETFL, tflags_ours);
533 ioctl (0, TIOCGETC, &tc_ours);
536 ioctl (0, TIOCGLTC, <c_ours);
539 ioctl (0, TIOCLGET, &lmode_ours);
543 ioctl (0, TIOCGPGRP, &pgrp_ours);
544 #endif /* TIOCGPGRP */
546 terminal_is_ours = 1;
554 @When the inferior process dies, deselect the current frame so that
555 the "where" ("backtrace") command will not think there's a stack.
560 #include <sys/fcntl.h>
562 /* May be unnecessary since many parts of inflow.c
563 have migrated to *-infdep.c */
565 #include <sys/user.h>
575 #endif /* TIOCGLTC */
578 tc_inferior = tc_ours;
582 #endif /* TIOCGLTC */
585 ioctl (0, TIOCSETC, &tc_inferior);
589 #endif /* TIOCGLTC */
592 ioctl (0, TIOCGETC, &tc_inferior);
596 #endif /* TIOCGLTC */
599 ioctl (0, TIOCSETC, &tc_ours);
603 #endif /* TIOCGLTC */
606 printf ("fcntl flags = 0x%x, lmode = 0x%x,\nsgttyb.sg_flags = 0x%x, owner pid = %d.\n",
607 tflags_inferior, lmode_inferior,
608 sg_inferior.sg_flags, pgrp_inferior);
613 #endif /* not HAVE_TERMIO */
619 perror_with_name ("vfork");
622 ioctl (0, TIOCGETC, &tc_ours);
626 #endif /* TIOCGLTC */