1 /* Low level interface to ptrace, for GDB when running under Unix.
2 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
26 #include <sys/types.h>
31 #include <sys/param.h>
38 #define TIOCGETP TCGETA
40 #define TIOCSETN TCSETA
42 #define TIOCSETP TCSETAF
43 #define TERMINAL struct termio
45 #include <sys/ioctl.h>
48 #define TERMINAL struct sgttyb
51 #ifdef SET_STACK_LIMIT_HUGE
53 #include <sys/resource.h>
54 extern int original_stack_limit;
55 #endif /* SET_STACK_LIMIT_HUGE */
59 /* Nonzero if we are debugging an attached outside process
60 rather than an inferior. */
65 /* Record terminal status separately for debugger and inferior. */
67 static TERMINAL sg_inferior;
68 static TERMINAL sg_ours;
70 static int tflags_inferior;
71 static int tflags_ours;
74 static struct tchars tc_inferior;
75 static struct tchars tc_ours;
79 static struct ltchars ltc_inferior;
80 static struct ltchars ltc_ours;
84 static int lmode_inferior;
85 static int lmode_ours;
89 static int pgrp_inferior;
92 static int (*sigint_ours) ();
93 static int (*sigquit_ours) ();
94 #endif /* TIOCGPGRP */
96 /* Copy of inferior_io_terminal when inferior was last started. */
97 static char *inferior_thisrun_terminal;
99 static void terminal_ours_1 ();
101 /* Nonzero if our terminal settings are in effect.
102 Zero if the inferior's settings are in effect. */
103 static int terminal_is_ours;
105 /* Initialize the terminal settings we record for the inferior,
106 before we actually run the inferior. */
109 terminal_init_inferior ()
111 if (remote_debugging)
114 sg_inferior = sg_ours;
115 tflags_inferior = tflags_ours;
118 tc_inferior = tc_ours;
122 ltc_inferior = ltc_ours;
126 lmode_inferior = lmode_ours;
130 pgrp_inferior = inferior_pid;
131 #endif /* TIOCGPGRP */
133 terminal_is_ours = 1;
136 /* Put the inferior's terminal settings into effect.
137 This is preparation for starting or resuming the inferior. */
142 if (remote_debugging)
145 if (terminal_is_ours) /* && inferior_thisrun_terminal == 0) */
147 fcntl (0, F_SETFL, tflags_inferior);
148 fcntl (0, F_SETFL, tflags_inferior);
149 ioctl (0, TIOCSETN, &sg_inferior);
151 ioctl (0, TIOCSETC, &tc_inferior);
154 ioctl (0, TIOCSLTC, <c_inferior);
157 ioctl (0, TIOCLSET, &lmode_inferior);
161 ioctl (0, TIOCSPGRP, &pgrp_inferior);
163 sigint_ours = (int (*) ()) signal (SIGINT, SIG_IGN);
164 sigquit_ours = (int (*) ()) signal (SIGQUIT, SIG_IGN);
165 #endif /* TIOCGPGRP */
167 terminal_is_ours = 0;
170 /* Put some of our terminal settings into effect,
171 enough to get proper results from our output,
172 but do not change into or out of RAW mode
173 so that no input is discarded.
175 After doing this, either terminal_ours or terminal_inferior
176 should be called to get back to a normal state of affairs. */
179 terminal_ours_for_output ()
181 if (remote_debugging)
187 /* Put our terminal settings into effect.
188 First record the inferior's terminal settings
189 so they can be restored properly later. */
194 if (remote_debugging)
201 terminal_ours_1 (output_only)
205 /* Ignore this signal since it will happen when we try to set the pgrp. */
207 #endif /* TIOCGPGRP */
209 if (!terminal_is_ours) /* && inferior_thisrun_terminal == 0) */
211 terminal_is_ours = 1;
214 osigttou = signal (SIGTTOU, SIG_IGN);
216 ioctl (0, TIOCGPGRP, &pgrp_inferior);
217 ioctl (0, TIOCSPGRP, &pgrp_ours);
219 signal (SIGTTOU, osigttou);
221 signal (SIGINT, sigint_ours);
222 signal (SIGQUIT, sigquit_ours);
223 #endif /* TIOCGPGRP */
225 tflags_inferior = fcntl (0, F_GETFL, 0);
226 ioctl (0, TIOCGETP, &sg_inferior);
229 ioctl (0, TIOCGETC, &tc_inferior);
232 ioctl (0, TIOCGLTC, <c_inferior);
235 ioctl (0, TIOCLGET, &lmode_inferior);
240 sg_ours.c_lflag |= ICANON;
241 if (output_only && !(sg_inferior.c_lflag & ICANON))
242 sg_ours.c_lflag &= ~ICANON;
243 #else /* not HAVE_TERMIO */
244 sg_ours.sg_flags &= ~RAW & ~CBREAK;
246 sg_ours.sg_flags |= (RAW | CBREAK) & sg_inferior.sg_flags;
247 #endif /* not HAVE_TERMIO */
249 fcntl (0, F_SETFL, tflags_ours);
250 fcntl (0, F_SETFL, tflags_ours);
251 ioctl (0, TIOCSETN, &sg_ours);
254 ioctl (0, TIOCSETC, &tc_ours);
257 ioctl (0, TIOCSLTC, <c_ours);
260 ioctl (0, TIOCLSET, &lmode_ours);
265 sg_ours.c_lflag |= ICANON;
266 #else /* not HAVE_TERMIO */
267 sg_ours.sg_flags &= ~RAW & ~CBREAK;
268 #endif /* not HAVE_TERMIO */
272 term_status_command ()
276 if (remote_debugging)
278 printf ("No terminal status when remote debugging.\n");
282 printf ("Inferior's terminal status (currently saved by GDB):\n");
286 printf ("fcntl flags = 0x%x, c_iflag = 0x%x, c_oflag = 0x%x,\n",
287 tflags_inferior, sg_inferior.c_iflag, sg_inferior.c_oflag);
288 printf ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
289 sg_inferior.c_cflag, sg_inferior.c_lflag, sg_inferior.c_line);
291 for (i = 0; (i < NCC); i += 1)
292 printf ("0x%x ", sg_inferior.c_cc[i]);
295 #else /* not HAVE_TERMIO */
297 printf ("fcntl flags = 0x%x, sgttyb.sg_flags = 0x%x, owner pid = %d.\n",
298 tflags_inferior, sg_inferior.sg_flags, pgrp_inferior);
300 #endif /* not HAVE_TERMIO */
304 for (i = 0; i < sizeof (struct tchars); i++)
305 printf ("0x%x ", ((char *)&tc_inferior)[i]);
310 printf ("ltchars: ");
311 for (i = 0; i < sizeof (struct ltchars); i++)
312 printf ("0x%x ", ((char *)<c_inferior)[i]);
314 ioctl (0, TIOCSLTC, <c_ours);
318 printf ("lmode: %x\n", lmode_inferior);
330 /* Disconnect the child process from our controlling terminal. */
331 tty = open("/dev/tty", O_RDWR);
334 ioctl(tty, TIOCNOTTY, 0);
339 /* Now open the specified new terminal. */
341 tty = open(ttyname, O_RDWR);
351 /* Start an inferior process and returns its pid.
352 ALLARGS is a string containing shell command to run the program.
353 ENV is the environment vector to pass. */
356 #define SHELL_FILE "/bin/sh"
360 create_inferior (allargs, env)
367 extern char *sys_errlist[];
370 /* If desired, concat something onto the front of ALLARGS.
371 SHELL_COMMAND is the result. */
372 #ifdef SHELL_COMMAND_CONCAT
373 shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + strlen (allargs) + 1);
374 strcpy (shell_command, SHELL_COMMAND_CONCAT);
375 strcat (shell_command, allargs);
377 shell_command = allargs;
380 /* exec is said to fail if the executable is open. */
385 perror_with_name ("fork");
390 /* Run inferior in a separate process group. */
391 setpgrp (getpid (), getpid ());
392 #endif /* TIOCGPGRP */
394 #ifdef SET_STACK_LIMIT_HUGE
395 /* Reset the stack limit back to what it was. */
399 getrlimit (RLIMIT_STACK, &rlim);
400 rlim.rlim_cur = original_stack_limit;
401 setrlimit (RLIMIT_STACK, &rlim);
403 #endif /* SET_STACK_LIMIT_HUGE */
406 inferior_thisrun_terminal = inferior_io_terminal;
407 if (inferior_io_terminal != 0)
408 new_tty (inferior_io_terminal);
410 /* Not needed on Sun, at least, and loses there
411 because it clobbers the superior. */
412 /*??? signal (SIGQUIT, SIG_DFL);
413 signal (SIGINT, SIG_DFL); */
416 execle (SHELL_FILE, "sh", "-c", shell_command, 0, env);
418 fprintf (stderr, "Cannot exec %s: %s.\n", SHELL_FILE,
419 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
426 /* Kill the inferior process. Make us have no inferior. */
431 if (remote_debugging)
433 if (inferior_pid == 0)
434 error ("The program is not being run.");
435 if (!query ("Kill the inferior process? "))
436 error ("Not confirmed.");
445 mark_breakpoints_out ();
446 select_frame ( (FRAME) 0, -1);
448 if (have_core_file_p ())
449 set_current_frame ( create_new_frame (read_register (FP_REGNUM),
454 try_writing_regs_command ()
460 if (inferior_pid == 0)
461 error ("There is no inferior process now.");
463 for (i = 0; ; i += 2)
467 value = call_ptrace (3, inferior_pid, i, 0);
468 call_ptrace (6, inferior_pid, i, value);
471 printf (" Succeeded with address 0x%x; value 0x%x (%d).\n",
474 else if ((i & 0377) == 0)
475 printf (" Failed at 0x%x.\n", i);
480 _initialize_inflow ()
482 add_com ("term-status", class_obscure, term_status_command,
483 "Print info on inferior's saved terminal status.");
485 add_com ("try-writing-regs", class_obscure, try_writing_regs_command,
486 "Try writing all locations in inferior's system block.\n\
487 Report which ones can be written.");
489 add_com ("kill", class_run, kill_command,
490 "Kill execution of program being debugged.");
494 ioctl (0, TIOCGETP, &sg_ours);
495 fcntl (0, F_GETFL, tflags_ours);
498 ioctl (0, TIOCGETC, &tc_ours);
501 ioctl (0, TIOCGLTC, <c_ours);
504 ioctl (0, TIOCLGET, &lmode_ours);
508 ioctl (0, TIOCGPGRP, &pgrp_ours);
509 #endif /* TIOCGPGRP */
511 terminal_is_ours = 1;