* core.c (core_file_command): Print frame at coredump with
[external/binutils.git] / gdb / inflow.c
1 /* Low level interface to ptrace, for GDB when running under Unix.
2    Copyright (C) 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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.  */
19
20 #include <stdio.h>
21 #include "defs.h"
22 #include "param.h"
23 #include "frame.h"
24 #include "inferior.h"
25 #include "command.h"
26 #include "signals.h"
27 #include "terminal.h"
28 #include "target.h"
29
30 #ifdef USG
31 #include <sys/types.h>
32 #endif
33
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.  */
36 #include <fcntl.h>
37
38 #include <sys/param.h>
39 #include <sys/dir.h>
40 #include <signal.h>
41
42 extern struct target_ops child_ops;
43
44 /* Nonzero if we are debugging an attached outside process
45    rather than an inferior.  */
46
47 int attach_flag;
48
49 \f
50 /* Record terminal status separately for debugger and inferior.  */
51
52 static TERMINAL sg_inferior;
53 static TERMINAL sg_ours;
54
55 static int tflags_inferior;
56 static int tflags_ours;
57
58 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
59 static struct tchars tc_inferior;
60 static struct tchars tc_ours;
61 #endif
62
63 #ifdef TIOCGLTC
64 static struct ltchars ltc_inferior;
65 static struct ltchars ltc_ours;
66 #endif
67
68 #ifdef TIOCLGET
69 static int lmode_inferior;
70 static int lmode_ours;
71 #endif
72
73 #ifdef TIOCGPGRP
74 static int pgrp_inferior;
75 static int pgrp_ours;
76 #else
77 static void (*sigint_ours) ();
78 static void (*sigquit_ours) ();
79 #endif /* TIOCGPGRP */
80
81 /* Copy of inferior_io_terminal when inferior was last started.  */
82 static char *inferior_thisrun_terminal;
83
84 static void terminal_ours_1 ();
85
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;
89
90 /* Initialize the terminal settings we record for the inferior,
91    before we actually run the inferior.  */
92
93 void
94 terminal_init_inferior ()
95 {
96   sg_inferior = sg_ours;
97   tflags_inferior = tflags_ours;
98
99 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
100   tc_inferior = tc_ours;
101 #endif
102
103 #ifdef TIOCGLTC
104   ltc_inferior = ltc_ours;
105 #endif
106
107 #ifdef TIOCLGET
108   lmode_inferior = lmode_ours;
109 #endif
110
111 #ifdef TIOCGPGRP
112   pgrp_inferior = inferior_pid;
113 #endif /* TIOCGPGRP */
114
115   terminal_is_ours = 1;
116 }
117
118 /* Put the inferior's terminal settings into effect.
119    This is preparation for starting or resuming the inferior.  */
120
121 void
122 terminal_inferior ()
123 {
124   if (terminal_is_ours && inferior_thisrun_terminal == 0)
125     {
126       fcntl (0, F_SETFL, tflags_inferior);
127       fcntl (0, F_SETFL, tflags_inferior);
128       ioctl (0, TIOCSETN, &sg_inferior);
129
130 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
131       ioctl (0, TIOCSETC, &tc_inferior);
132 #endif
133 #ifdef TIOCGLTC
134       ioctl (0, TIOCSLTC, &ltc_inferior);
135 #endif
136 #ifdef TIOCLGET
137       ioctl (0, TIOCLSET, &lmode_inferior);
138 #endif
139
140 #ifdef TIOCGPGRP
141       ioctl (0, TIOCSPGRP, &pgrp_inferior);
142 #else
143       sigint_ours = (void (*) ()) signal (SIGINT, SIG_IGN);
144       sigquit_ours = (void (*) ()) signal (SIGQUIT, SIG_IGN);
145 #endif /* TIOCGPGRP */
146     }
147   terminal_is_ours = 0;
148 }
149
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.
154
155    After doing this, either terminal_ours or terminal_inferior
156    should be called to get back to a normal state of affairs.  */
157
158 void
159 terminal_ours_for_output ()
160 {
161   terminal_ours_1 (1);
162 }
163
164 /* Put our terminal settings into effect.
165    First record the inferior's terminal settings
166    so they can be restored properly later.  */
167
168 void
169 terminal_ours ()
170 {
171   terminal_ours_1 (0);
172 }
173
174 static void
175 terminal_ours_1 (output_only)
176      int output_only;
177 {
178 #ifdef TIOCGPGRP
179   /* Ignore this signal since it will happen when we try to set the pgrp.  */
180   void (*osigttou) ();
181 #endif /* TIOCGPGRP */
182
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)
189     return;
190
191   if (!terminal_is_ours)
192     {
193       terminal_is_ours = 1;
194
195 #ifdef TIOCGPGRP
196       osigttou = (void (*) ()) signal (SIGTTOU, SIG_IGN);
197
198       ioctl (0, TIOCGPGRP, &pgrp_inferior);
199       ioctl (0, TIOCSPGRP, &pgrp_ours);
200
201       signal (SIGTTOU, osigttou);
202 #else
203       signal (SIGINT, sigint_ours);
204       signal (SIGQUIT, sigquit_ours);
205 #endif /* TIOCGPGRP */
206
207       tflags_inferior = fcntl (0, F_GETFL, 0);
208       ioctl (0, TIOCGETP, &sg_inferior);
209
210 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
211       ioctl (0, TIOCGETC, &tc_inferior);
212 #endif
213 #ifdef TIOCGLTC
214       ioctl (0, TIOCGLTC, &ltc_inferior);
215 #endif
216 #ifdef TIOCLGET
217       ioctl (0, TIOCLGET, &lmode_inferior);
218 #endif
219     }
220
221 #ifdef HAVE_TERMIO
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;
227   if (output_only)
228     sg_ours.sg_flags |= (RAW | CBREAK) & sg_inferior.sg_flags;
229 #endif /* not HAVE_TERMIO */
230
231   fcntl (0, F_SETFL, tflags_ours);
232   fcntl (0, F_SETFL, tflags_ours);
233   ioctl (0, TIOCSETN, &sg_ours);
234
235 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
236   ioctl (0, TIOCSETC, &tc_ours);
237 #endif
238 #ifdef TIOCGLTC
239   ioctl (0, TIOCSLTC, &ltc_ours);
240 #endif
241 #ifdef TIOCLGET
242   ioctl (0, TIOCLSET, &lmode_ours);
243 #endif
244
245 #ifdef HAVE_TERMIO
246   sg_ours.c_lflag |= ICANON;
247 #else /* not HAVE_TERMIO */
248   sg_ours.sg_flags &= ~RAW & ~CBREAK;
249 #endif /* not HAVE_TERMIO */
250 }
251
252 /* ARGSUSED */
253 void
254 term_info (arg, from_tty)
255      char *arg;
256      int from_tty;
257 {
258   target_terminal_info (arg, from_tty);
259 }
260
261 /* ARGSUSED */
262 void
263 child_terminal_info (args, from_tty)
264      char *args;
265      int from_tty;
266 {
267   register int i;
268
269   printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
270
271 #ifdef HAVE_TERMIO
272
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");
281
282 #else /* not HAVE_TERMIO */
283
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);
286
287 #endif /* not HAVE_TERMIO */
288
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");
294 #endif
295
296 #ifdef TIOCGLTC
297   printf_filtered ("ltchars: ");
298   for (i = 0; i < (int)sizeof (struct ltchars); i++)
299     printf_filtered ("0x%x ", ((char *)&ltc_inferior)[i]);
300   printf_filtered ("\n");
301 #endif
302   
303 #ifdef TIOCLGET
304   printf_filtered ("lmode:  0x%x\n", lmode_inferior);
305 #endif
306 }
307 \f
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.
312
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.  */
316
317 new_tty_prefork (ttyname)
318      char *ttyname;
319 {
320   /* Save the name for later, for determining whether we and the child
321      are sharing a tty.  */
322   inferior_thisrun_terminal = ttyname;
323 }
324
325 void
326 new_tty ()
327 {
328   register int tty;
329
330   if (inferior_thisrun_terminal == 0)
331     return;
332
333 #ifdef TIOCNOTTY
334   /* Disconnect the child process from our controlling terminal.  */
335   tty = open("/dev/tty", O_RDWR);
336   if (tty > 0)
337     {
338       ioctl(tty, TIOCNOTTY, 0);
339       close(tty);
340     }
341 #endif
342
343   /* Now open the specified new terminal.  */
344
345   tty = open(inferior_thisrun_terminal, O_RDWR);
346   if (tty == -1)
347     {
348       print_sys_errmsg (inferior_thisrun_terminal, errno);
349       _exit(1);
350     }
351
352   /* Avoid use of dup2; doesn't exist on all systems.  */
353   if (tty != 0)
354     { close (0); dup (tty); }
355   if (tty != 1)
356     { close (1); dup (tty); }
357   if (tty != 2)
358     { close (2); dup (tty); }
359   if (tty > 2)
360     close(tty);
361 }
362 \f
363 /* Kill the inferior process.  Make us have no inferior.  */
364
365 /* ARGSUSED */
366 static void
367 kill_command (arg, from_tty)
368      char *arg;
369      int from_tty;
370 {
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);
376
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);
383     else
384       print_stack_frame (selected_frame, selected_frame_level, 1);
385   }
386 }
387
388 /* The inferior process has died.  Long live the inferior!  */
389
390 void
391 generic_mourn_inferior ()
392 {
393   inferior_pid = 0;
394   attach_flag = 0;
395   mark_breakpoints_out ();
396   registers_changed ();
397
398 #ifdef CLEAR_DEFERRED_STORES
399   /* Delete any pending stores to the inferior... */
400   CLEAR_DEFERRED_STORES;
401 #endif
402
403   reopen_exec_file ();
404   if (target_has_stack) {
405     set_current_frame ( create_new_frame (read_register (FP_REGNUM),
406                                           read_pc ()));
407     select_frame (get_current_frame (), 0);
408   } else {
409     set_current_frame (0);
410     select_frame ((FRAME) 0, -1);
411   }
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 ();
415 }
416
417 void
418 child_mourn_inferior ()
419 {
420   unpush_target (&child_ops);
421   generic_mourn_inferior ();
422 }
423 \f
424 #if 0 
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).  */
429 /* ARGSUSED */
430 static void
431 try_writing_regs_command (arg, from_tty)
432      char *arg;
433      int from_tty;
434 {
435   register int i;
436   register int value;
437
438   if (inferior_pid == 0)
439     error ("There is no inferior process now.");
440
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)
447     {
448       QUIT;
449       errno = 0;
450       value = call_ptrace (3, inferior_pid, i, 0);
451       call_ptrace (6, inferior_pid, i, value);
452       if (errno == 0)
453         {
454           printf (" Succeeded with address 0x%x; value 0x%x (%d).\n",
455                   i, value, value);
456         }
457       else if ((i & 0377) == 0)
458         printf (" Failed at 0x%x.\n", i);
459     }
460 }
461 #endif
462 \f
463 void
464 _initialize_inflow ()
465 {
466   add_info ("terminal", term_info,
467            "Print inferior's saved terminal status.");
468
469 #if 0
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.");
473 #endif
474
475   add_com ("kill", class_run, kill_command,
476            "Kill execution of program being debugged.");
477
478   inferior_pid = 0;
479
480   ioctl (0, TIOCGETP, &sg_ours);
481   tflags_ours = fcntl (0, F_GETFL, 0);
482
483 #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
484   ioctl (0, TIOCGETC, &tc_ours);
485 #endif
486 #ifdef TIOCGLTC
487   ioctl (0, TIOCGLTC, &ltc_ours);
488 #endif
489 #ifdef TIOCLGET
490   ioctl (0, TIOCLGET, &lmode_ours);
491 #endif
492
493 #ifdef TIOCGPGRP
494   ioctl (0, TIOCGPGRP, &pgrp_ours);
495 #endif /* TIOCGPGRP */
496
497   terminal_is_ours = 1;
498 }
499