* infrun.c (_initialize_infrun): Alias `i handle' == `i signals'.
[external/binutils.git] / gdb / infrun.c
1 /* Start (run) and stop the inferior process, for GDB.
2    Copyright 1986, 1987, 1988, 1989, 1991, 1992 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 /* Notes on the algorithm used in wait_for_inferior to determine if we
21    just did a subroutine call when stepping.  We have the following
22    information at that point:
23
24                   Current and previous (just before this step) pc.
25                   Current and previous sp.
26                   Current and previous start of current function.
27
28    If the starts of the functions don't match, then
29
30         a) We did a subroutine call.
31
32    In this case, the pc will be at the beginning of a function.
33
34         b) We did a subroutine return.
35
36    Otherwise.
37
38         c) We did a longjmp.
39
40    If we did a longjump, we were doing "nexti", since a next would
41    have attempted to skip over the assembly language routine in which
42    the longjmp is coded and would have simply been the equivalent of a
43    continue.  I consider this ok behaivior.  We'd like one of two
44    things to happen if we are doing a nexti through the longjmp()
45    routine: 1) It behaves as a stepi, or 2) It acts like a continue as
46    above.  Given that this is a special case, and that anybody who
47    thinks that the concept of sub calls is meaningful in the context
48    of a longjmp, I'll take either one.  Let's see what happens.  
49
50    Acts like a subroutine return.  I can handle that with no problem
51    at all.
52
53    -->So: If the current and previous beginnings of the current
54    function don't match, *and* the pc is at the start of a function,
55    we've done a subroutine call.  If the pc is not at the start of a
56    function, we *didn't* do a subroutine call.  
57
58    -->If the beginnings of the current and previous function do match,
59    either: 
60
61         a) We just did a recursive call.
62
63            In this case, we would be at the very beginning of a
64            function and 1) it will have a prologue (don't jump to
65            before prologue, or 2) (we assume here that it doesn't have
66            a prologue) there will have been a change in the stack
67            pointer over the last instruction.  (Ie. it's got to put
68            the saved pc somewhere.  The stack is the usual place.  In
69            a recursive call a register is only an option if there's a
70            prologue to do something with it.  This is even true on
71            register window machines; the prologue sets up the new
72            window.  It might not be true on a register window machine
73            where the call instruction moved the register window
74            itself.  Hmmm.  One would hope that the stack pointer would
75            also change.  If it doesn't, somebody send me a note, and
76            I'll work out a more general theory.
77            bug-gdb@prep.ai.mit.edu).  This is true (albeit slipperly
78            so) on all machines I'm aware of:
79
80               m68k:     Call changes stack pointer.  Regular jumps don't.
81
82               sparc:    Recursive calls must have frames and therefor,
83                         prologues.
84
85               vax:      All calls have frames and hence change the
86                         stack pointer.
87
88         b) We did a return from a recursive call.  I don't see that we
89            have either the ability or the need to distinguish this
90            from an ordinary jump.  The stack frame will be printed
91            when and if the frame pointer changes; if we are in a
92            function without a frame pointer, it's the users own
93            lookout.
94
95         c) We did a jump within a function.  We assume that this is
96            true if we didn't do a recursive call.
97
98         d) We are in no-man's land ("I see no symbols here").  We
99            don't worry about this; it will make calls look like simple
100            jumps (and the stack frames will be printed when the frame
101            pointer moves), which is a reasonably non-violent response.
102 */
103
104 #include "defs.h"
105 #include <string.h>
106 #include "symtab.h"
107 #include "frame.h"
108 #include "inferior.h"
109 #include "breakpoint.h"
110 #include "wait.h"
111 #include "gdbcore.h"
112 #include "command.h"
113 #include "terminal.h"           /* For #ifdef TIOCGPGRP and new_tty */
114 #include "target.h"
115
116 #include <signal.h>
117
118 /* unistd.h is needed to #define X_OK */
119 #ifdef USG
120 #include <unistd.h>
121 #else
122 #include <sys/file.h>
123 #endif
124
125 #ifdef SET_STACK_LIMIT_HUGE
126 #include <sys/time.h>
127 #include <sys/resource.h>
128
129 extern int original_stack_limit;
130 #endif /* SET_STACK_LIMIT_HUGE */
131
132 /* Prototypes for local functions */
133
134 static void
135 signals_info PARAMS ((char *, int));
136
137 static void
138 handle_command PARAMS ((char *, int));
139
140 static void
141 sig_print_info PARAMS ((int));
142
143 static void
144 sig_print_header PARAMS ((void));
145
146 static void
147 remove_step_breakpoint PARAMS ((void));
148
149 static void
150 insert_step_breakpoint PARAMS ((void));
151
152 static void
153 resume PARAMS ((int, int));
154
155 static void
156 resume_cleanups PARAMS ((int));
157
158 extern char **environ;
159
160 extern struct target_ops child_ops;     /* In inftarg.c */
161
162 /* Sigtramp is a routine that the kernel calls (which then calls the
163    signal handler).  On most machines it is a library routine that
164    is linked into the executable.
165
166    This macro, given a program counter value and the name of the
167    function in which that PC resides (which can be null if the
168    name is not known), returns nonzero if the PC and name show
169    that we are in sigtramp.
170
171    On most machines just see if the name is sigtramp (and if we have
172    no name, assume we are not in sigtramp).  */
173 #if !defined (IN_SIGTRAMP)
174 #define IN_SIGTRAMP(pc, name) \
175   (name && !strcmp ("_sigtramp", name))
176 #endif
177
178 /* GET_LONGJMP_TARGET returns the PC at which longjmp() will resume the
179    program.  It needs to examine the jmp_buf argument and extract the PC
180    from it.  The return value is non-zero on success, zero otherwise. */
181 #ifndef GET_LONGJMP_TARGET
182 #define GET_LONGJMP_TARGET(PC_ADDR) 0
183 #endif
184
185
186 /* Some machines have trampoline code that sits between function callers
187    and the actual functions themselves.  If this machine doesn't have
188    such things, disable their processing.  */
189 #ifndef SKIP_TRAMPOLINE_CODE
190 #define SKIP_TRAMPOLINE_CODE(pc)        0
191 #endif
192
193 /* For SVR4 shared libraries, each call goes through a small piece of
194    trampoline code in the ".init" section.  IN_SOLIB_TRAMPOLINE evaluates
195    to nonzero if we are current stopped in one of these. */
196 #ifndef IN_SOLIB_TRAMPOLINE
197 #define IN_SOLIB_TRAMPOLINE(pc,name)    0
198 #endif
199
200 /* Notify other parts of gdb that might care that signal handling may
201    have changed for one or more signals. */
202 #ifndef NOTICE_SIGNAL_HANDLING_CHANGE
203 #define NOTICE_SIGNAL_HANDLING_CHANGE   /* No actions */
204 #endif
205
206 #ifdef TDESC
207 #include "tdesc.h"
208 int safe_to_init_tdesc_context = 0;
209 extern dc_dcontext_t current_context;
210 #endif
211
212 /* Tables of how to react to signals; the user sets them.  */
213
214 static unsigned char *signal_stop;
215 static unsigned char *signal_print;
216 static unsigned char *signal_program;
217
218 #define SET_SIGS(nsigs,sigs,flags) \
219   do { \
220     int signum = (nsigs); \
221     while (signum-- > 0) \
222       if ((sigs)[signum]) \
223         (flags)[signum] = 1; \
224   } while (0)
225
226 #define UNSET_SIGS(nsigs,sigs,flags) \
227   do { \
228     int signum = (nsigs); \
229     while (signum-- > 0) \
230       if ((sigs)[signum]) \
231         (flags)[signum] = 0; \
232   } while (0)
233
234 /* Nonzero if breakpoints are now inserted in the inferior.  */
235 /* Nonstatic for initialization during xxx_create_inferior. FIXME. */
236
237 /*static*/ int breakpoints_inserted;
238
239 /* Function inferior was in as of last step command.  */
240
241 static struct symbol *step_start_function;
242
243 /* Nonzero => address for special breakpoint for resuming stepping.  */
244
245 static CORE_ADDR step_resume_break_address;
246
247 /* Pointer to orig contents of the byte where the special breakpoint is.  */
248
249 static char step_resume_break_shadow[BREAKPOINT_MAX];
250
251 /* Nonzero means the special breakpoint is a duplicate
252    so it has not itself been inserted.  */
253
254 static int step_resume_break_duplicate;
255
256 /* Nonzero if we are expecting a trace trap and should proceed from it.  */
257
258 static int trap_expected;
259
260 /* Nonzero if the next time we try to continue the inferior, it will
261    step one instruction and generate a spurious trace trap.
262    This is used to compensate for a bug in HP-UX.  */
263
264 static int trap_expected_after_continue;
265
266 /* Nonzero means expecting a trace trap
267    and should stop the inferior and return silently when it happens.  */
268
269 int stop_after_trap;
270
271 /* Nonzero means expecting a trap and caller will handle it themselves.
272    It is used after attach, due to attaching to a process;
273    when running in the shell before the child program has been exec'd;
274    and when running some kinds of remote stuff (FIXME?).  */
275
276 int stop_soon_quietly;
277
278 /* Nonzero if pc has been changed by the debugger
279    since the inferior stopped.  */
280
281 int pc_changed;
282
283 /* Nonzero if proceed is being used for a "finish" command or a similar
284    situation when stop_registers should be saved.  */
285
286 int proceed_to_finish;
287
288 /* Save register contents here when about to pop a stack dummy frame,
289    if-and-only-if proceed_to_finish is set.
290    Thus this contains the return value from the called function (assuming
291    values are returned in a register).  */
292
293 char stop_registers[REGISTER_BYTES];
294
295 /* Nonzero if program stopped due to error trying to insert breakpoints.  */
296
297 static int breakpoints_failed;
298
299 /* Nonzero after stop if current stack frame should be printed.  */
300
301 static int stop_print_frame;
302
303 #ifdef NO_SINGLE_STEP
304 extern int one_stepped;         /* From machine dependent code */
305 extern void single_step ();     /* Same. */
306 #endif /* NO_SINGLE_STEP */
307
308 \f
309 /* Things to clean up if we QUIT out of resume ().  */
310 /* ARGSUSED */
311 static void
312 resume_cleanups (arg)
313      int arg;
314 {
315   normal_stop ();
316 }
317
318 /* Resume the inferior, but allow a QUIT.  This is useful if the user
319    wants to interrupt some lengthy single-stepping operation
320    (for child processes, the SIGINT goes to the inferior, and so
321    we get a SIGINT random_signal, but for remote debugging and perhaps
322    other targets, that's not true).
323
324    STEP nonzero if we should step (zero to continue instead).
325    SIG is the signal to give the inferior (zero for none).  */
326 static void
327 resume (step, sig)
328      int step;
329      int sig;
330 {
331   struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
332   QUIT;
333
334 #ifdef NO_SINGLE_STEP
335   if (step) {
336     single_step(sig);   /* Do it the hard way, w/temp breakpoints */
337     step = 0;           /* ...and don't ask hardware to do it.  */
338   }
339 #endif
340
341   /* Handle any optimized stores to the inferior NOW...  */
342 #ifdef DO_DEFERRED_STORES
343   DO_DEFERRED_STORES;
344 #endif
345
346   target_resume (step, sig);
347   discard_cleanups (old_cleanups);
348 }
349
350 \f
351 /* Clear out all variables saying what to do when inferior is continued.
352    First do this, then set the ones you want, then call `proceed'.  */
353
354 void
355 clear_proceed_status ()
356 {
357   trap_expected = 0;
358   step_range_start = 0;
359   step_range_end = 0;
360   step_frame_address = 0;
361   step_over_calls = -1;
362   step_resume_break_address = 0;
363   stop_after_trap = 0;
364   stop_soon_quietly = 0;
365   proceed_to_finish = 0;
366   breakpoint_proceeded = 1;     /* We're about to proceed... */
367
368   /* Discard any remaining commands or status from previous stop.  */
369   bpstat_clear (&stop_bpstat);
370 }
371
372 /* Basic routine for continuing the program in various fashions.
373
374    ADDR is the address to resume at, or -1 for resume where stopped.
375    SIGGNAL is the signal to give it, or 0 for none,
376      or -1 for act according to how it stopped.
377    STEP is nonzero if should trap after one instruction.
378      -1 means return after that and print nothing.
379      You should probably set various step_... variables
380      before calling here, if you are stepping.
381
382    You should call clear_proceed_status before calling proceed.  */
383
384 void
385 proceed (addr, siggnal, step)
386      CORE_ADDR addr;
387      int siggnal;
388      int step;
389 {
390   int oneproc = 0;
391
392   if (step > 0)
393     step_start_function = find_pc_function (read_pc ());
394   if (step < 0)
395     stop_after_trap = 1;
396
397   if (addr == (CORE_ADDR)-1)
398     {
399       /* If there is a breakpoint at the address we will resume at,
400          step one instruction before inserting breakpoints
401          so that we do not stop right away.  */
402
403       if (!pc_changed && breakpoint_here_p (read_pc ()))
404         oneproc = 1;
405     }
406   else
407     {
408       write_register (PC_REGNUM, addr);
409 #ifdef NPC_REGNUM
410       write_register (NPC_REGNUM, addr + 4);
411 #ifdef NNPC_REGNUM
412       write_register (NNPC_REGNUM, addr + 8);
413 #endif
414 #endif
415     }
416
417   if (trap_expected_after_continue)
418     {
419       /* If (step == 0), a trap will be automatically generated after
420          the first instruction is executed.  Force step one
421          instruction to clear this condition.  This should not occur
422          if step is nonzero, but it is harmless in that case.  */
423       oneproc = 1;
424       trap_expected_after_continue = 0;
425     }
426
427   if (oneproc)
428     /* We will get a trace trap after one instruction.
429        Continue it automatically and insert breakpoints then.  */
430     trap_expected = 1;
431   else
432     {
433       int temp = insert_breakpoints ();
434       if (temp)
435         {
436           print_sys_errmsg ("ptrace", temp);
437           error ("Cannot insert breakpoints.\n\
438 The same program may be running in another process.");
439         }
440       breakpoints_inserted = 1;
441     }
442
443   /* Install inferior's terminal modes.  */
444   target_terminal_inferior ();
445
446   if (siggnal >= 0)
447     stop_signal = siggnal;
448   /* If this signal should not be seen by program,
449      give it zero.  Used for debugging signals.  */
450   else if (stop_signal < NSIG && !signal_program[stop_signal])
451     stop_signal= 0;
452
453   /* Resume inferior.  */
454   resume (oneproc || step || bpstat_should_step (), stop_signal);
455
456   /* Wait for it to stop (if not standalone)
457      and in any case decode why it stopped, and act accordingly.  */
458
459   wait_for_inferior ();
460   normal_stop ();
461 }
462
463 /* Record the pc and sp of the program the last time it stopped.
464    These are just used internally by wait_for_inferior, but need
465    to be preserved over calls to it and cleared when the inferior
466    is started.  */
467 static CORE_ADDR prev_pc;
468 static CORE_ADDR prev_sp;
469 static CORE_ADDR prev_func_start;
470 static char *prev_func_name;
471
472 \f
473 /* Start an inferior Unix child process and sets inferior_pid to its pid.
474    EXEC_FILE is the file to run.
475    ALLARGS is a string containing the arguments to the program.
476    ENV is the environment vector to pass.  Errors reported with error().  */
477
478 #ifndef SHELL_FILE
479 #define SHELL_FILE "/bin/sh"
480 #endif
481
482 void
483 child_create_inferior (exec_file, allargs, env)
484      char *exec_file;
485      char *allargs;
486      char **env;
487 {
488   int pid;
489   char *shell_command;
490   char *shell_file;
491   static char default_shell_file[] = SHELL_FILE;
492   int len;
493   int pending_execs;
494   /* Set debug_fork then attach to the child while it sleeps, to debug. */
495   static int debug_fork = 0;
496   /* This is set to the result of setpgrp, which if vforked, will be visible
497      to you in the parent process.  It's only used by humans for debugging.  */
498   static int debug_setpgrp = 657473;
499   char **save_our_env;
500
501   /* The user might want tilde-expansion, and in general probably wants
502      the program to behave the same way as if run from
503      his/her favorite shell.  So we let the shell run it for us.
504      FIXME, this should probably search the local environment (as
505      modified by the setenv command), not the env gdb inherited.  */
506   shell_file = getenv ("SHELL");
507   if (shell_file == NULL)
508     shell_file = default_shell_file;
509   
510   len = 5 + strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop*/ 10;
511   /* If desired, concat something onto the front of ALLARGS.
512      SHELL_COMMAND is the result.  */
513 #ifdef SHELL_COMMAND_CONCAT
514   shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + len);
515   strcpy (shell_command, SHELL_COMMAND_CONCAT);
516 #else
517   shell_command = (char *) alloca (len);
518   shell_command[0] = '\0';
519 #endif
520   strcat (shell_command, "exec ");
521   strcat (shell_command, exec_file);
522   strcat (shell_command, " ");
523   strcat (shell_command, allargs);
524
525   /* exec is said to fail if the executable is open.  */
526   close_exec_file ();
527
528   /* Retain a copy of our environment variables, since the child will
529      replace the value of  environ  and if we're vforked, we have to 
530      restore it.  */
531   save_our_env = environ;
532
533   /* Tell the terminal handling subsystem what tty we plan to run on;
534      it will just record the information for later.  */
535
536   new_tty_prefork (inferior_io_terminal);
537
538   /* It is generally good practice to flush any possible pending stdio
539      output prior to doing a fork, to avoid the possibility of both the
540      parent and child flushing the same data after the fork. */
541
542   fflush (stdout);
543   fflush (stderr);
544
545 #if defined(USG) && !defined(HAVE_VFORK)
546   pid = fork ();
547 #else
548   if (debug_fork)
549     pid = fork ();
550   else
551     pid = vfork ();
552 #endif
553
554   if (pid < 0)
555     perror_with_name ("vfork");
556
557   if (pid == 0)
558     {
559       if (debug_fork) 
560         sleep (debug_fork);
561
562 #ifdef TIOCGPGRP
563       /* Run inferior in a separate process group.  */
564 #ifdef NEED_POSIX_SETPGID
565       debug_setpgrp = setpgid (0, 0);
566 #else
567 #if defined(USG) && !defined(SETPGRP_ARGS)
568       debug_setpgrp = setpgrp ();
569 #else
570       debug_setpgrp = setpgrp (getpid (), getpid ());
571 #endif /* USG */
572 #endif /* NEED_POSIX_SETPGID */
573       if (debug_setpgrp == -1)
574          perror("setpgrp failed in child");
575 #endif /* TIOCGPGRP */
576
577 #ifdef SET_STACK_LIMIT_HUGE
578       /* Reset the stack limit back to what it was.  */
579       {
580         struct rlimit rlim;
581
582         getrlimit (RLIMIT_STACK, &rlim);
583         rlim.rlim_cur = original_stack_limit;
584         setrlimit (RLIMIT_STACK, &rlim);
585       }
586 #endif /* SET_STACK_LIMIT_HUGE */
587
588       /* Ask the tty subsystem to switch to the one we specified earlier
589          (or to share the current terminal, if none was specified).  */
590
591       new_tty ();
592
593       /* Changing the signal handlers for the inferior after
594          a vfork can also change them for the superior, so we don't mess
595          with signals here.  See comments in
596          initialize_signals for how we get the right signal handlers
597          for the inferior.  */
598
599 #ifdef USE_PROC_FS
600       /* Use SVR4 /proc interface */
601       proc_set_exec_trap ();
602 #else
603       /* "Trace me, Dr. Memory!" */
604       call_ptrace (0, 0, (PTRACE_ARG3_TYPE) 0, 0);
605 #endif
606
607       /* There is no execlpe call, so we have to set the environment
608          for our child in the global variable.  If we've vforked, this
609          clobbers the parent, but environ is restored a few lines down
610          in the parent.  By the way, yes we do need to look down the
611          path to find $SHELL.  Rich Pixley says so, and I agree.  */
612       environ = env;
613       execlp (shell_file, shell_file, "-c", shell_command, (char *)0);
614
615       fprintf (stderr, "Cannot exec %s: %s.\n", shell_file,
616                safe_strerror (errno));
617       fflush (stderr);
618       _exit (0177);
619     }
620
621   /* Restore our environment in case a vforked child clob'd it.  */
622   environ = save_our_env;
623
624   /* Now that we have a child process, make it our target.  */
625   push_target (&child_ops);
626
627 #ifdef CREATE_INFERIOR_HOOK
628   CREATE_INFERIOR_HOOK (pid);
629 #endif  
630
631 /* The process was started by the fork that created it,
632    but it will have stopped one instruction after execing the shell.
633    Here we must get it up to actual execution of the real program.  */
634
635   inferior_pid = pid;           /* Needed for wait_for_inferior stuff below */
636
637   clear_proceed_status ();
638
639   /* We will get a trace trap after one instruction.
640      Continue it automatically.  Eventually (after shell does an exec)
641      it will get another trace trap.  Then insert breakpoints and continue.  */
642
643 #ifdef START_INFERIOR_TRAPS_EXPECTED
644   pending_execs = START_INFERIOR_TRAPS_EXPECTED;
645 #else
646   pending_execs = 2;
647 #endif
648
649   init_wait_for_inferior ();
650
651   /* Set up the "saved terminal modes" of the inferior
652      based on what modes we are starting it with.  */
653   target_terminal_init ();
654
655   /* Install inferior's terminal modes.  */
656   target_terminal_inferior ();
657
658   while (1)
659     {
660       stop_soon_quietly = 1;    /* Make wait_for_inferior be quiet */
661       wait_for_inferior ();
662       if (stop_signal != SIGTRAP)
663         {
664           /* Let shell child handle its own signals in its own way */
665           /* FIXME, what if child has exit()ed?  Must exit loop somehow */
666           resume (0, stop_signal);
667         }
668       else
669         {
670           /* We handle SIGTRAP, however; it means child did an exec.  */
671           if (0 == --pending_execs)
672             break;
673           resume (0, 0);                /* Just make it go on */
674         }
675     }
676   stop_soon_quietly = 0;
677
678   /* We are now in the child process of interest, having exec'd the
679      correct program, and are poised at the first instruction of the
680      new program.  */
681 #ifdef SOLIB_CREATE_INFERIOR_HOOK
682   SOLIB_CREATE_INFERIOR_HOOK (pid);
683 #endif
684
685   /* Pedal to the metal.  Away we go.  */
686   proceed ((CORE_ADDR) -1, 0, 0);
687 }
688
689 /* Start remote-debugging of a machine over a serial link.  */
690
691 void
692 start_remote ()
693 {
694   init_wait_for_inferior ();
695   clear_proceed_status ();
696   stop_soon_quietly = 1;
697   trap_expected = 0;
698   wait_for_inferior ();
699   normal_stop ();
700 }
701
702 /* Initialize static vars when a new inferior begins.  */
703
704 void
705 init_wait_for_inferior ()
706 {
707   /* These are meaningless until the first time through wait_for_inferior.  */
708   prev_pc = 0;
709   prev_sp = 0;
710   prev_func_start = 0;
711   prev_func_name = NULL;
712
713   trap_expected_after_continue = 0;
714   breakpoints_inserted = 0;
715   mark_breakpoints_out ();
716   stop_signal = 0;              /* Don't confuse first call to proceed(). */
717 }
718
719
720 /* Attach to process PID, then initialize for debugging it
721    and wait for the trace-trap that results from attaching.  */
722
723 void
724 child_attach (args, from_tty)
725      char *args;
726      int from_tty;
727 {
728   char *exec_file;
729   int pid;
730
731   dont_repeat();
732
733   if (!args)
734     error_no_arg ("process-id to attach");
735
736 #ifndef ATTACH_DETACH
737   error ("Can't attach to a process on this machine.");
738 #else
739   pid = atoi (args);
740
741   if (pid == getpid())          /* Trying to masturbate? */
742     error ("I refuse to debug myself!");
743
744   if (target_has_execution)
745     {
746       if (query ("A program is being debugged already.  Kill it? "))
747         target_kill ();
748       else
749         error ("Inferior not killed.");
750     }
751
752   exec_file = (char *) get_exec_file (1);
753
754   if (from_tty)
755     {
756       printf ("Attaching program: %s pid %d\n",
757               exec_file, pid);
758       fflush (stdout);
759     }
760
761   attach (pid);
762   inferior_pid = pid;
763   push_target (&child_ops);
764
765   mark_breakpoints_out ();
766   target_terminal_init ();
767   clear_proceed_status ();
768   stop_soon_quietly = 1;
769   /*proceed (-1, 0, -2);*/
770   target_terminal_inferior ();
771   wait_for_inferior ();
772 #ifdef SOLIB_ADD
773   SOLIB_ADD ((char *)0, from_tty, (struct target_ops *)0);
774 #endif
775   normal_stop ();
776 #endif  /* ATTACH_DETACH */
777 }
778 \f
779 /* Wait for control to return from inferior to debugger.
780    If inferior gets a signal, we may decide to start it up again
781    instead of returning.  That is why there is a loop in this function.
782    When this function actually returns it means the inferior
783    should be left stopped and GDB should read more commands.  */
784
785 void
786 wait_for_inferior ()
787 {
788   WAITTYPE w;
789   int another_trap;
790   int random_signal;
791   CORE_ADDR stop_sp;
792   CORE_ADDR stop_func_start;
793   char *stop_func_name;
794   CORE_ADDR prologue_pc, tmp;
795   int stop_step_resume_break;
796   struct symtab_and_line sal;
797   int remove_breakpoints_on_following_step = 0;
798   int current_line;
799   int handling_longjmp = 0;     /* FIXME */
800
801   sal = find_pc_line(prev_pc, 0);
802   current_line = sal.line;
803
804   while (1)
805     {
806       /* Clean up saved state that will become invalid.  */
807       pc_changed = 0;
808       flush_cached_frames ();
809       registers_changed ();
810
811       target_wait (&w);
812
813 #ifdef SIGTRAP_STOP_AFTER_LOAD
814
815       /* Somebody called load(2), and it gave us a "trap signal after load".
816          Ignore it gracefully. */
817
818       SIGTRAP_STOP_AFTER_LOAD (w);
819 #endif
820
821       /* See if the process still exists; clean up if it doesn't.  */
822       if (WIFEXITED (w))
823         {
824           target_terminal_ours ();      /* Must do this before mourn anyway */
825           if (WEXITSTATUS (w))
826             printf_filtered ("\nProgram exited with code 0%o.\n", 
827                      (unsigned int)WEXITSTATUS (w));
828           else
829             if (!batch_mode())
830               printf_filtered ("\nProgram exited normally.\n");
831           fflush (stdout);
832           target_mourn_inferior ();
833 #ifdef NO_SINGLE_STEP
834           one_stepped = 0;
835 #endif
836           stop_print_frame = 0;
837           break;
838         }
839       else if (!WIFSTOPPED (w))
840         {
841           stop_print_frame = 0;
842           stop_signal = WTERMSIG (w);
843           target_terminal_ours ();      /* Must do this before mourn anyway */
844           target_kill ();               /* kill mourns as well */
845 #ifdef PRINT_RANDOM_SIGNAL
846           printf_filtered ("\nProgram terminated: ");
847           PRINT_RANDOM_SIGNAL (stop_signal);
848 #else
849           printf_filtered ("\nProgram terminated with signal %d, %s\n",
850                            stop_signal, safe_strsignal (stop_signal));
851 #endif
852           printf_filtered ("The inferior process no longer exists.\n");
853           fflush (stdout);
854 #ifdef NO_SINGLE_STEP
855           one_stepped = 0;
856 #endif
857           break;
858         }
859       
860 #ifdef NO_SINGLE_STEP
861       if (one_stepped)
862         single_step (0);        /* This actually cleans up the ss */
863 #endif /* NO_SINGLE_STEP */
864       
865       stop_pc = read_pc ();
866       set_current_frame ( create_new_frame (read_register (FP_REGNUM),
867                                             read_pc ()));
868       
869       stop_frame_address = FRAME_FP (get_current_frame ());
870       stop_sp = read_register (SP_REGNUM);
871       stop_func_start = 0;
872       stop_func_name = 0;
873       /* Don't care about return value; stop_func_start and stop_func_name
874          will both be 0 if it doesn't work.  */
875       find_pc_partial_function (stop_pc, &stop_func_name, &stop_func_start);
876       stop_func_start += FUNCTION_START_OFFSET;
877       another_trap = 0;
878       bpstat_clear (&stop_bpstat);
879       stop_step = 0;
880       stop_stack_dummy = 0;
881       stop_print_frame = 1;
882       stop_step_resume_break = 0;
883       random_signal = 0;
884       stopped_by_random_signal = 0;
885       breakpoints_failed = 0;
886       
887       /* Look at the cause of the stop, and decide what to do.
888          The alternatives are:
889          1) break; to really stop and return to the debugger,
890          2) drop through to start up again
891          (set another_trap to 1 to single step once)
892          3) set random_signal to 1, and the decision between 1 and 2
893          will be made according to the signal handling tables.  */
894       
895       stop_signal = WSTOPSIG (w);
896       
897       /* First, distinguish signals caused by the debugger from signals
898          that have to do with the program's own actions.
899          Note that breakpoint insns may cause SIGTRAP or SIGILL
900          or SIGEMT, depending on the operating system version.
901          Here we detect when a SIGILL or SIGEMT is really a breakpoint
902          and change it to SIGTRAP.  */
903       
904       if (stop_signal == SIGTRAP
905           || (breakpoints_inserted &&
906               (stop_signal == SIGILL
907 #ifdef SIGEMT
908                || stop_signal == SIGEMT
909 #endif
910             ))
911           || stop_soon_quietly)
912         {
913           if (stop_signal == SIGTRAP && stop_after_trap)
914             {
915               stop_print_frame = 0;
916               break;
917             }
918           if (stop_soon_quietly)
919             break;
920
921           /* Don't even think about breakpoints
922              if just proceeded over a breakpoint.
923
924              However, if we are trying to proceed over a breakpoint
925              and end up in sigtramp, then step_resume_break_address
926              will be set and we should check whether we've hit the
927              step breakpoint.  */
928           if (stop_signal == SIGTRAP && trap_expected
929               && step_resume_break_address == 0)
930             bpstat_clear (&stop_bpstat);
931           else
932             {
933               /* See if there is a breakpoint at the current PC.  */
934 #if DECR_PC_AFTER_BREAK
935               /* Notice the case of stepping through a jump
936                  that lands just after a breakpoint.
937                  Don't confuse that with hitting the breakpoint.
938                  What we check for is that 1) stepping is going on
939                  and 2) the pc before the last insn does not match
940                  the address of the breakpoint before the current pc.  */
941               if (prev_pc == stop_pc - DECR_PC_AFTER_BREAK
942                   || !step_range_end
943                   || step_resume_break_address
944                   || handling_longjmp /* FIXME */)
945 #endif /* DECR_PC_AFTER_BREAK not zero */
946                 {
947                   /* See if we stopped at the special breakpoint for
948                      stepping over a subroutine call.  If both are zero,
949                      this wasn't the reason for the stop.  */
950                   if (step_resume_break_address
951                       && stop_pc - DECR_PC_AFTER_BREAK
952                          == step_resume_break_address)
953                     {
954                       stop_step_resume_break = 1;
955                       if (DECR_PC_AFTER_BREAK)
956                         {
957                           stop_pc -= DECR_PC_AFTER_BREAK;
958                           write_register (PC_REGNUM, stop_pc);
959                           pc_changed = 0;
960                         }
961                     }
962                   else
963                     {
964                       stop_bpstat =
965                         bpstat_stop_status (&stop_pc, stop_frame_address);
966                       /* Following in case break condition called a
967                          function.  */
968                       stop_print_frame = 1;
969                     }
970                 }
971             }
972           
973           if (stop_signal == SIGTRAP)
974             random_signal
975               = !(bpstat_explains_signal (stop_bpstat)
976                   || trap_expected
977                   || stop_step_resume_break
978                   || PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address)
979                   || (step_range_end && !step_resume_break_address));
980           else
981             {
982               random_signal
983                 = !(bpstat_explains_signal (stop_bpstat)
984                     || stop_step_resume_break
985                     /* End of a stack dummy.  Some systems (e.g. Sony
986                        news) give another signal besides SIGTRAP,
987                        so check here as well as above.  */
988                     || PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address)
989                     );
990               if (!random_signal)
991                 stop_signal = SIGTRAP;
992             }
993         }
994       else
995         random_signal = 1;
996       
997       /* For the program's own signals, act according to
998          the signal handling tables.  */
999       
1000       if (random_signal)
1001         {
1002           /* Signal not for debugging purposes.  */
1003           int printed = 0;
1004           
1005           stopped_by_random_signal = 1;
1006           
1007           if (stop_signal >= NSIG
1008               || signal_print[stop_signal])
1009             {
1010               printed = 1;
1011               target_terminal_ours_for_output ();
1012 #ifdef PRINT_RANDOM_SIGNAL
1013               PRINT_RANDOM_SIGNAL (stop_signal);
1014 #else
1015               printf_filtered ("\nProgram received signal %d, %s\n",
1016                                stop_signal, safe_strsignal (stop_signal));
1017 #endif /* PRINT_RANDOM_SIGNAL */
1018               fflush (stdout);
1019             }
1020           if (stop_signal >= NSIG
1021               || signal_stop[stop_signal])
1022             break;
1023           /* If not going to stop, give terminal back
1024              if we took it away.  */
1025           else if (printed)
1026             target_terminal_inferior ();
1027
1028           /* Note that virtually all the code below does `if !random_signal'.
1029              Perhaps this code should end with a goto or continue.  At least
1030              one (now fixed) bug was caused by this -- a !random_signal was
1031              missing in one of the tests below.  */
1032         }
1033
1034       /* Handle cases caused by hitting a breakpoint.  */
1035
1036       if (!random_signal)
1037         if (bpstat_explains_signal (stop_bpstat))
1038           {
1039             CORE_ADDR jmp_buf_pc;
1040
1041             switch (stop_bpstat->breakpoint_at->type) /* FIXME */
1042               {
1043                 /* If we hit the breakpoint at longjmp, disable it for the
1044                    duration of this command.  Then, install a temporary
1045                    breakpoint at the target of the jmp_buf. */
1046               case bp_longjmp:
1047                 disable_longjmp_breakpoint();
1048                 remove_breakpoints ();
1049                 breakpoints_inserted = 0;
1050                 if (!GET_LONGJMP_TARGET(&jmp_buf_pc)) goto keep_going;
1051
1052                 /* Need to blow away step-resume breakpoint, as it
1053                    interferes with us */
1054                 remove_step_breakpoint ();
1055                 step_resume_break_address = 0;
1056                 stop_step_resume_break = 0;
1057
1058 #if 0                           /* FIXME - Need to implement nested temporary breakpoints */
1059                 if (step_over_calls > 0)
1060                   set_longjmp_resume_breakpoint(jmp_buf_pc,
1061                                                 get_current_frame());
1062                 else
1063 #endif                          /* 0 */
1064                   set_longjmp_resume_breakpoint(jmp_buf_pc, NULL);
1065                 handling_longjmp = 1; /* FIXME */
1066                 goto keep_going;
1067
1068               case bp_longjmp_resume:
1069                 remove_breakpoints ();
1070                 breakpoints_inserted = 0;
1071 #if 0                           /* FIXME - Need to implement nested temporary breakpoints */
1072                 if (step_over_calls
1073                     && (stop_frame_address
1074                         INNER_THAN step_frame_address))
1075                   {
1076                     another_trap = 1;
1077                     goto keep_going;
1078                   }
1079 #endif                          /* 0 */
1080                 disable_longjmp_breakpoint();
1081                 handling_longjmp = 0; /* FIXME */
1082                 break;
1083
1084               default:
1085                 fprintf(stderr, "Unknown breakpoint type %d\n",
1086                         stop_bpstat->breakpoint_at->type);
1087               case bp_watchpoint:
1088               case bp_breakpoint:
1089               case bp_until:
1090               case bp_finish:
1091                 /* Does a breakpoint want us to stop?  */
1092                 if (bpstat_stop (stop_bpstat))
1093                   {
1094                     stop_print_frame = bpstat_should_print (stop_bpstat);
1095                     goto stop_stepping;
1096                   }
1097                 /* Otherwise, must remove breakpoints and single-step
1098                    to get us past the one we hit.  */
1099                 else
1100                   {
1101                     remove_breakpoints ();
1102                     remove_step_breakpoint ();
1103                     breakpoints_inserted = 0;
1104                     another_trap = 1;
1105                   }
1106                 break;
1107               }
1108           }
1109         else if (stop_step_resume_break)
1110           {
1111             /* But if we have hit the step-resumption breakpoint,
1112                remove it.  It has done its job getting us here.
1113                The sp test is to make sure that we don't get hung
1114                up in recursive calls in functions without frame
1115                pointers.  If the stack pointer isn't outside of
1116                where the breakpoint was set (within a routine to be
1117                stepped over), we're in the middle of a recursive
1118                call. Not true for reg window machines (sparc)
1119                because the must change frames to call things and
1120                the stack pointer doesn't have to change if it
1121                the bp was set in a routine without a frame (pc can
1122                be stored in some other window).
1123                
1124                The removal of the sp test is to allow calls to
1125                alloca.  Nasty things were happening.  Oh, well,
1126                gdb can only handle one level deep of lack of
1127                frame pointer. */
1128
1129             /*
1130               Disable test for step_frame_address match so that we always stop even if the
1131               frames don't match.  Reason: if we hit the step_resume_breakpoint, there is
1132               no way to temporarily disable it so that we can step past it.  If we leave
1133               the breakpoint in, then we loop forever repeatedly hitting, but never
1134               getting past the breakpoint.  This change keeps nexting over recursive
1135               function calls from hanging gdb.
1136               */
1137 #if 0
1138             if (* step_frame_address == 0
1139                 || (step_frame_address == stop_frame_address))
1140 #endif
1141               {
1142                 remove_step_breakpoint ();
1143                 step_resume_break_address = 0;
1144
1145                 /* If were waiting for a trap, hitting the step_resume_break
1146                    doesn't count as getting it.  */
1147                 if (trap_expected)
1148                   another_trap = 1;
1149               }
1150           }
1151
1152       /* We come here if we hit a breakpoint but should not
1153          stop for it.  Possibly we also were stepping
1154          and should stop for that.  So fall through and
1155          test for stepping.  But, if not stepping,
1156          do not stop.  */
1157
1158       /* If this is the breakpoint at the end of a stack dummy,
1159          just stop silently.  */
1160       if (!random_signal 
1161          && PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address))
1162           {
1163             stop_print_frame = 0;
1164             stop_stack_dummy = 1;
1165 #ifdef HP_OS_BUG
1166             trap_expected_after_continue = 1;
1167 #endif
1168             break;
1169           }
1170       
1171       if (step_resume_break_address)
1172         /* Having a step-resume breakpoint overrides anything
1173            else having to do with stepping commands until
1174            that breakpoint is reached.  */
1175         ;
1176       /* If stepping through a line, keep going if still within it.  */
1177       else if (!random_signal
1178                && step_range_end
1179                && stop_pc >= step_range_start
1180                && stop_pc < step_range_end
1181                /* The step range might include the start of the
1182                   function, so if we are at the start of the
1183                   step range and either the stack or frame pointers
1184                   just changed, we've stepped outside */
1185                && !(stop_pc == step_range_start
1186                     && stop_frame_address
1187                     && (stop_sp INNER_THAN prev_sp
1188                         || stop_frame_address != step_frame_address)))
1189         {
1190           ;
1191         }
1192       
1193       /* We stepped out of the stepping range.  See if that was due
1194          to a subroutine call that we should proceed to the end of.  */
1195       else if (!random_signal && step_range_end)
1196         {
1197           if (stop_func_start)
1198             {
1199               prologue_pc = stop_func_start;
1200               SKIP_PROLOGUE (prologue_pc);
1201             }
1202
1203           /* Did we just take a signal?  */
1204           if (IN_SIGTRAMP (stop_pc, stop_func_name)
1205               && !IN_SIGTRAMP (prev_pc, prev_func_name))
1206             {
1207               /* This code is needed at least in the following case:
1208                  The user types "next" and then a signal arrives (before
1209                  the "next" is done).  */
1210               /* We've just taken a signal; go until we are back to
1211                  the point where we took it and one more.  */
1212               step_resume_break_address = prev_pc;
1213               step_resume_break_duplicate =
1214                 breakpoint_here_p (step_resume_break_address);
1215               if (breakpoints_inserted)
1216                 insert_step_breakpoint ();
1217               /* Make sure that the stepping range gets us past
1218                  that instruction.  */
1219               if (step_range_end == 1)
1220                 step_range_end = (step_range_start = prev_pc) + 1;
1221               remove_breakpoints_on_following_step = 1;
1222               goto save_pc;
1223             }
1224
1225           /* ==> See comments at top of file on this algorithm.  <==*/
1226           
1227           if ((stop_pc == stop_func_start
1228                || IN_SOLIB_TRAMPOLINE (stop_pc, stop_func_name))
1229               && (stop_func_start != prev_func_start
1230                   || prologue_pc != stop_func_start
1231                   || stop_sp != prev_sp))
1232             {
1233               /* It's a subroutine call.
1234                  (0)  If we are not stepping over any calls ("stepi"), we
1235                       just stop.
1236                  (1)  If we're doing a "next", we want to continue through
1237                       the call ("step over the call").
1238                  (2)  If we are in a function-call trampoline (a stub between
1239                       the calling routine and the real function), locate
1240                       the real function and change stop_func_start.
1241                  (3)  If we're doing a "step", and there are no debug symbols
1242                       at the target of the call, we want to continue through
1243                       it ("step over the call").
1244                  (4)  Otherwise, we want to stop soon, after the function
1245                       prologue ("step into the call"). */
1246
1247               if (step_over_calls == 0)
1248                 {
1249                   /* I presume that step_over_calls is only 0 when we're
1250                      supposed to be stepping at the assembly language level. */
1251                   stop_step = 1;
1252                   break;
1253                 }
1254
1255               if (step_over_calls > 0)
1256                 goto step_over_function;
1257
1258               tmp = SKIP_TRAMPOLINE_CODE (stop_pc);
1259               if (tmp != 0)
1260                 stop_func_start = tmp;
1261
1262               if (find_pc_function (stop_func_start) != 0)
1263                 goto step_into_function;
1264
1265 step_over_function:
1266               /* A subroutine call has happened.  */
1267               /* Set a special breakpoint after the return */
1268               step_resume_break_address =
1269                 ADDR_BITS_REMOVE
1270                   (SAVED_PC_AFTER_CALL (get_current_frame ()));
1271               step_resume_break_duplicate
1272                 = breakpoint_here_p (step_resume_break_address);
1273               if (breakpoints_inserted)
1274                 insert_step_breakpoint ();
1275               goto save_pc;
1276
1277 step_into_function:
1278               /* Subroutine call with source code we should not step over.
1279                  Do step to the first line of code in it.  */
1280               SKIP_PROLOGUE (stop_func_start);
1281               sal = find_pc_line (stop_func_start, 0);
1282               /* Use the step_resume_break to step until
1283                  the end of the prologue, even if that involves jumps
1284                  (as it seems to on the vax under 4.2).  */
1285               /* If the prologue ends in the middle of a source line,
1286                  continue to the end of that source line.
1287                  Otherwise, just go to end of prologue.  */
1288 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
1289               /* no, don't either.  It skips any code that's
1290                  legitimately on the first line.  */
1291 #else
1292               if (sal.end && sal.pc != stop_func_start)
1293                 stop_func_start = sal.end;
1294 #endif
1295
1296               if (stop_func_start == stop_pc)
1297                 {
1298                   /* We are already there: stop now.  */
1299                   stop_step = 1;
1300                   break;
1301                 }       
1302               else
1303                 /* Put the step-breakpoint there and go until there. */
1304                 {
1305                   step_resume_break_address = stop_func_start;
1306                   
1307                   step_resume_break_duplicate
1308                     = breakpoint_here_p (step_resume_break_address);
1309                   if (breakpoints_inserted)
1310                     insert_step_breakpoint ();
1311                   /* Do not specify what the fp should be when we stop
1312                      since on some machines the prologue
1313                      is where the new fp value is established.  */
1314                   step_frame_address = 0;
1315                   /* And make sure stepping stops right away then.  */
1316                   step_range_end = step_range_start;
1317                 }
1318               goto save_pc;
1319             }
1320
1321           /* We've wandered out of the step range (but haven't done a
1322              subroutine call or return).  */
1323
1324           sal = find_pc_line(stop_pc, 0);
1325           
1326           if (step_range_end == 1 ||    /* stepi or nexti */
1327               sal.line == 0 ||          /* ...or no line # info */
1328               (stop_pc == sal.pc        /* ...or we're at the start */
1329                && current_line != sal.line)) {  /* of a different line */
1330             /* Stop because we're done stepping.  */
1331             stop_step = 1;
1332             break;
1333           } else {
1334             /* We aren't done stepping, and we have line number info for $pc.
1335                Optimize by setting the step_range for the line.  
1336                (We might not be in the original line, but if we entered a
1337                new line in mid-statement, we continue stepping.  This makes 
1338                things like for(;;) statements work better.)  */
1339             step_range_start = sal.pc;
1340             step_range_end = sal.end;
1341             goto save_pc;
1342           }
1343           /* We never fall through here */
1344         }
1345
1346       if (trap_expected
1347           && IN_SIGTRAMP (stop_pc, stop_func_name)
1348           && !IN_SIGTRAMP (prev_pc, prev_func_name))
1349         {
1350           /* What has happened here is that we have just stepped the inferior
1351              with a signal (because it is a signal which shouldn't make
1352              us stop), thus stepping into sigtramp.
1353
1354              So we need to set a step_resume_break_address breakpoint
1355              and continue until we hit it, and then step.  */
1356           step_resume_break_address = prev_pc;
1357           /* Always 1, I think, but it's probably easier to have
1358              the step_resume_break as usual rather than trying to
1359              re-use the breakpoint which is already there.  */
1360           step_resume_break_duplicate =
1361             breakpoint_here_p (step_resume_break_address);
1362           if (breakpoints_inserted)
1363             insert_step_breakpoint ();
1364           remove_breakpoints_on_following_step = 1;
1365           another_trap = 1;
1366         }
1367
1368 /* My apologies to the gods of structured programming. */
1369 /* Come to this label when you need to resume the inferior.  It's really much
1370    cleaner at this time to do a goto than to try and figure out what the
1371    if-else chain ought to look like!! */
1372
1373     keep_going:
1374
1375 save_pc:
1376       /* Save the pc before execution, to compare with pc after stop.  */
1377       prev_pc = read_pc ();     /* Might have been DECR_AFTER_BREAK */
1378       prev_func_start = stop_func_start; /* Ok, since if DECR_PC_AFTER
1379                                           BREAK is defined, the
1380                                           original pc would not have
1381                                           been at the start of a
1382                                           function. */
1383       prev_func_name = stop_func_name;
1384       prev_sp = stop_sp;
1385
1386       /* If we did not do break;, it means we should keep
1387          running the inferior and not return to debugger.  */
1388
1389       if (trap_expected && stop_signal != SIGTRAP)
1390         {
1391           /* We took a signal (which we are supposed to pass through to
1392              the inferior, else we'd have done a break above) and we
1393              haven't yet gotten our trap.  Simply continue.  */
1394           resume ((step_range_end && !step_resume_break_address)
1395                   || (trap_expected && !step_resume_break_address)
1396                   || bpstat_should_step (),
1397                   stop_signal);
1398         }
1399       else
1400         {
1401           /* Either the trap was not expected, but we are continuing
1402              anyway (the user asked that this signal be passed to the
1403              child)
1404                -- or --
1405              The signal was SIGTRAP, e.g. it was our signal, but we
1406              decided we should resume from it.
1407
1408              We're going to run this baby now!
1409
1410              Insert breakpoints now, unless we are trying
1411              to one-proceed past a breakpoint.  */
1412           /* If we've just finished a special step resume and we don't
1413              want to hit a breakpoint, pull em out.  */
1414           if (!step_resume_break_address &&
1415               remove_breakpoints_on_following_step)
1416             {
1417               remove_breakpoints_on_following_step = 0;
1418               remove_breakpoints ();
1419               breakpoints_inserted = 0;
1420             }
1421           else if (!breakpoints_inserted &&
1422                    (step_resume_break_address != 0 || !another_trap))
1423             {
1424               insert_step_breakpoint ();
1425               breakpoints_failed = insert_breakpoints ();
1426               if (breakpoints_failed)
1427                 break;
1428               breakpoints_inserted = 1;
1429             }
1430
1431           trap_expected = another_trap;
1432
1433           if (stop_signal == SIGTRAP)
1434             stop_signal = 0;
1435
1436 #ifdef SHIFT_INST_REGS
1437           /* I'm not sure when this following segment applies.  I do know, now,
1438              that we shouldn't rewrite the regs when we were stopped by a
1439              random signal from the inferior process.  */
1440
1441           if (!bpstat_explains_signal (stop_bpstat)
1442               && (stop_signal != SIGCLD) 
1443               && !stopped_by_random_signal)
1444             {
1445             CORE_ADDR pc_contents = read_register (PC_REGNUM);
1446             CORE_ADDR npc_contents = read_register (NPC_REGNUM);
1447             if (pc_contents != npc_contents)
1448               {
1449               write_register (NNPC_REGNUM, npc_contents);
1450               write_register (NPC_REGNUM, pc_contents);
1451               }
1452             }
1453 #endif /* SHIFT_INST_REGS */
1454
1455           resume ((!step_resume_break_address
1456                    && !handling_longjmp
1457                    && (step_range_end
1458                        || trap_expected))
1459                   || bpstat_should_step (),
1460                   stop_signal);
1461         }
1462     }
1463
1464  stop_stepping:
1465   if (target_has_execution)
1466     {
1467       /* Assuming the inferior still exists, set these up for next
1468          time, just like we did above if we didn't break out of the
1469          loop.  */
1470       prev_pc = read_pc ();
1471       prev_func_start = stop_func_start;
1472       prev_func_name = stop_func_name;
1473       prev_sp = stop_sp;
1474     }
1475 }
1476 \f
1477 /* Here to return control to GDB when the inferior stops for real.
1478    Print appropriate messages, remove breakpoints, give terminal our modes.
1479
1480    STOP_PRINT_FRAME nonzero means print the executing frame
1481    (pc, function, args, file, line number and line text).
1482    BREAKPOINTS_FAILED nonzero means stop was due to error
1483    attempting to insert breakpoints.  */
1484
1485 void
1486 normal_stop ()
1487 {
1488   /* Make sure that the current_frame's pc is correct.  This
1489      is a correction for setting up the frame info before doing
1490      DECR_PC_AFTER_BREAK */
1491   if (target_has_execution)
1492     (get_current_frame ())->pc = read_pc ();
1493   
1494   if (breakpoints_failed)
1495     {
1496       target_terminal_ours_for_output ();
1497       print_sys_errmsg ("ptrace", breakpoints_failed);
1498       printf_filtered ("Stopped; cannot insert breakpoints.\n\
1499 The same program may be running in another process.\n");
1500     }
1501
1502   if (target_has_execution)
1503     remove_step_breakpoint ();
1504
1505   if (target_has_execution && breakpoints_inserted)
1506     if (remove_breakpoints ())
1507       {
1508         target_terminal_ours_for_output ();
1509         printf_filtered ("Cannot remove breakpoints because program is no longer writable.\n\
1510 It might be running in another process.\n\
1511 Further execution is probably impossible.\n");
1512       }
1513
1514   breakpoints_inserted = 0;
1515
1516   /* Delete the breakpoint we stopped at, if it wants to be deleted.
1517      Delete any breakpoint that is to be deleted at the next stop.  */
1518
1519   breakpoint_auto_delete (stop_bpstat);
1520
1521   /* If an auto-display called a function and that got a signal,
1522      delete that auto-display to avoid an infinite recursion.  */
1523
1524   if (stopped_by_random_signal)
1525     disable_current_display ();
1526
1527   if (step_multi && stop_step)
1528     return;
1529
1530   target_terminal_ours ();
1531
1532   if (!target_has_stack)
1533     return;
1534
1535   /* Select innermost stack frame except on return from a stack dummy routine,
1536      or if the program has exited.  Print it without a level number if
1537      we have changed functions or hit a breakpoint.  Print source line
1538      if we have one.  */
1539   if (!stop_stack_dummy)
1540     {
1541       select_frame (get_current_frame (), 0);
1542
1543       if (stop_print_frame)
1544         {
1545           int source_only;
1546
1547           source_only = bpstat_print (stop_bpstat);
1548           source_only = source_only ||
1549                 (   stop_step
1550                  && step_frame_address == stop_frame_address
1551                  && step_start_function == find_pc_function (stop_pc));
1552
1553           print_stack_frame (selected_frame, -1, source_only? -1: 1);
1554
1555           /* Display the auto-display expressions.  */
1556           do_displays ();
1557         }
1558     }
1559
1560   /* Save the function value return registers, if we care.
1561      We might be about to restore their previous contents.  */
1562   if (proceed_to_finish)
1563     read_register_bytes (0, stop_registers, REGISTER_BYTES);
1564
1565   if (stop_stack_dummy)
1566     {
1567       /* Pop the empty frame that contains the stack dummy.
1568          POP_FRAME ends with a setting of the current frame, so we
1569          can use that next. */
1570       POP_FRAME;
1571       select_frame (get_current_frame (), 0);
1572     }
1573 }
1574 \f
1575 static void
1576 insert_step_breakpoint ()
1577 {
1578   if (step_resume_break_address && !step_resume_break_duplicate)
1579     target_insert_breakpoint (step_resume_break_address,
1580                               step_resume_break_shadow);
1581 }
1582
1583 static void
1584 remove_step_breakpoint ()
1585 {
1586   if (step_resume_break_address && !step_resume_break_duplicate)
1587     target_remove_breakpoint (step_resume_break_address,
1588                               step_resume_break_shadow);
1589 }
1590 \f
1591 int signal_stop_state (signo)
1592      int signo;
1593 {
1594   return ((signo >= 0 && signo < NSIG) ? signal_stop[signo] : 0);
1595 }
1596
1597 int signal_print_state (signo)
1598      int signo;
1599 {
1600   return ((signo >= 0 && signo < NSIG) ? signal_print[signo] : 0);
1601 }
1602
1603 int signal_pass_state (signo)
1604      int signo;
1605 {
1606   return ((signo >= 0 && signo < NSIG) ? signal_program[signo] : 0);
1607 }
1608
1609 static void
1610 sig_print_header ()
1611 {
1612   printf_filtered ("Signal\t\tStop\tPrint\tPass to program\tDescription\n");
1613 }
1614
1615 static void
1616 sig_print_info (number)
1617      int number;
1618 {
1619   char *name;
1620
1621   if ((name = strsigno (number)) == NULL)
1622     printf_filtered ("%d\t\t", number);
1623   else
1624     printf_filtered ("%s (%d)\t", name, number);
1625   printf_filtered ("%s\t", signal_stop[number] ? "Yes" : "No");
1626   printf_filtered ("%s\t", signal_print[number] ? "Yes" : "No");
1627   printf_filtered ("%s\t\t", signal_program[number] ? "Yes" : "No");
1628   printf_filtered ("%s\n", safe_strsignal (number));
1629 }
1630
1631 /* Specify how various signals in the inferior should be handled.  */
1632
1633 static void
1634 handle_command (args, from_tty)
1635      char *args;
1636      int from_tty;
1637 {
1638   char **argv;
1639   int digits, wordlen;
1640   int sigfirst, signum, siglast;
1641   int allsigs;
1642   int nsigs;
1643   unsigned char *sigs;
1644   struct cleanup *old_chain;
1645
1646   if (args == NULL)
1647     {
1648       error_no_arg ("signal to handle");
1649     }
1650
1651   /* Allocate and zero an array of flags for which signals to handle. */
1652
1653   nsigs = signo_max () + 1;
1654   sigs = (unsigned char *) alloca (nsigs);
1655   memset (sigs, 0, nsigs);
1656
1657   /* Break the command line up into args. */
1658
1659   argv = buildargv (args);
1660   if (argv == NULL)
1661     {
1662       nomem (0);
1663     }
1664   old_chain = make_cleanup (freeargv, (char *) argv);
1665
1666   /* Walk through the args, looking for signal numbers, signal names, and
1667      actions.  Signal numbers and signal names may be interspersed with
1668      actions, with the actions being performed for all signals cumulatively
1669      specified.  Signal ranges can be specified as <LOW>-<HIGH>. */
1670
1671   while (*argv != NULL)
1672     {
1673       wordlen = strlen (*argv);
1674       for (digits = 0; isdigit ((*argv)[digits]); digits++) {;}
1675       allsigs = 0;
1676       sigfirst = siglast = -1;
1677
1678       if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
1679         {
1680           /* Apply action to all signals except those used by the
1681              debugger.  Silently skip those. */
1682           allsigs = 1;
1683           sigfirst = 0;
1684           siglast = nsigs - 1;
1685         }
1686       else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
1687         {
1688           SET_SIGS (nsigs, sigs, signal_stop);
1689           SET_SIGS (nsigs, sigs, signal_print);
1690         }
1691       else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
1692         {
1693           UNSET_SIGS (nsigs, sigs, signal_program);
1694         }
1695       else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
1696         {
1697           SET_SIGS (nsigs, sigs, signal_print);
1698         }
1699       else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
1700         {
1701           SET_SIGS (nsigs, sigs, signal_program);
1702         }
1703       else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
1704         {
1705           UNSET_SIGS (nsigs, sigs, signal_stop);
1706         }
1707       else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
1708         {
1709           SET_SIGS (nsigs, sigs, signal_program);
1710         }
1711       else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
1712         {
1713           UNSET_SIGS (nsigs, sigs, signal_print);
1714           UNSET_SIGS (nsigs, sigs, signal_stop);
1715         }
1716       else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
1717         {
1718           UNSET_SIGS (nsigs, sigs, signal_program);
1719         }
1720       else if (digits > 0)
1721         {
1722           sigfirst = siglast = atoi (*argv);
1723           if ((*argv)[digits] == '-')
1724             {
1725               siglast = atoi ((*argv) + digits + 1);
1726             }
1727           if (sigfirst > siglast)
1728             {
1729               /* Bet he didn't figure we'd think of this case... */
1730               signum = sigfirst;
1731               sigfirst = siglast;
1732               siglast = signum;
1733             }
1734           if (sigfirst < 0 || sigfirst >= nsigs)
1735             {
1736               error ("Signal %d not in range 0-%d", sigfirst, nsigs - 1);
1737             }
1738           if (siglast < 0 || siglast >= nsigs)
1739             {
1740               error ("Signal %d not in range 0-%d", siglast, nsigs - 1);
1741             }
1742         }
1743       else if ((signum = strtosigno (*argv)) != 0)
1744         {
1745           sigfirst = siglast = signum;
1746         }
1747       else
1748         {
1749           /* Not a number and not a recognized flag word => complain.  */
1750           error ("Unrecognized or ambiguous flag word: \"%s\".", *argv);
1751         }
1752
1753       /* If any signal numbers or symbol names were found, set flags for
1754          which signals to apply actions to. */
1755
1756       for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
1757         {
1758           switch (signum)
1759             {
1760               case SIGTRAP:
1761               case SIGINT:
1762                 if (!allsigs && !sigs[signum])
1763                   {
1764                     if (query ("%s is used by the debugger.\nAre you sure you want to change it? ", strsigno (signum)))
1765                       {
1766                         sigs[signum] = 1;
1767                       }
1768                     else
1769                       {
1770                         printf ("Not confirmed, unchanged.\n");
1771                         fflush (stdout);
1772                       }
1773                   }
1774                 break;
1775               default:
1776                 sigs[signum] = 1;
1777                 break;
1778             }
1779         }
1780
1781       argv++;
1782     }
1783
1784   NOTICE_SIGNAL_HANDLING_CHANGE;
1785
1786   if (from_tty)
1787     {
1788       /* Show the results.  */
1789       sig_print_header ();
1790       for (signum = 0; signum < nsigs; signum++)
1791         {
1792           if (sigs[signum])
1793             {
1794               sig_print_info (signum);
1795             }
1796         }
1797     }
1798
1799   do_cleanups (old_chain);
1800 }
1801
1802 /* Print current contents of the tables set by the handle command.  */
1803
1804 static void
1805 signals_info (signum_exp, from_tty)
1806      char *signum_exp;
1807      int from_tty;
1808 {
1809   register int i;
1810   sig_print_header ();
1811
1812   if (signum_exp)
1813     {
1814       /* First see if this is a symbol name.  */
1815       i = strtosigno (signum_exp);
1816       if (i == 0)
1817         {
1818           /* Nope, maybe it's an address which evaluates to a signal
1819              number.  */
1820           i = parse_and_eval_address (signum_exp);
1821           if (i >= NSIG || i < 0)
1822             error ("Signal number out of bounds.");
1823         }
1824       sig_print_info (i);
1825       return;
1826     }
1827
1828   printf_filtered ("\n");
1829   for (i = 0; i < NSIG; i++)
1830     {
1831       QUIT;
1832
1833       sig_print_info (i);
1834     }
1835
1836   printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
1837 }
1838 \f
1839 /* Save all of the information associated with the inferior<==>gdb
1840    connection.  INF_STATUS is a pointer to a "struct inferior_status"
1841    (defined in inferior.h).  */
1842
1843 void
1844 save_inferior_status (inf_status, restore_stack_info)
1845      struct inferior_status *inf_status;
1846      int restore_stack_info;
1847 {
1848   inf_status->pc_changed = pc_changed;
1849   inf_status->stop_signal = stop_signal;
1850   inf_status->stop_pc = stop_pc;
1851   inf_status->stop_frame_address = stop_frame_address;
1852   inf_status->stop_step = stop_step;
1853   inf_status->stop_stack_dummy = stop_stack_dummy;
1854   inf_status->stopped_by_random_signal = stopped_by_random_signal;
1855   inf_status->trap_expected = trap_expected;
1856   inf_status->step_range_start = step_range_start;
1857   inf_status->step_range_end = step_range_end;
1858   inf_status->step_frame_address = step_frame_address;
1859   inf_status->step_over_calls = step_over_calls;
1860   inf_status->step_resume_break_address = step_resume_break_address;
1861   inf_status->stop_after_trap = stop_after_trap;
1862   inf_status->stop_soon_quietly = stop_soon_quietly;
1863   /* Save original bpstat chain here; replace it with copy of chain. 
1864      If caller's caller is walking the chain, they'll be happier if we
1865      hand them back the original chain when restore_i_s is called.  */
1866   inf_status->stop_bpstat = stop_bpstat;
1867   stop_bpstat = bpstat_copy (stop_bpstat);
1868   inf_status->breakpoint_proceeded = breakpoint_proceeded;
1869   inf_status->restore_stack_info = restore_stack_info;
1870   inf_status->proceed_to_finish = proceed_to_finish;
1871   
1872   memcpy (inf_status->stop_registers, stop_registers, REGISTER_BYTES);
1873   
1874   record_selected_frame (&(inf_status->selected_frame_address),
1875                          &(inf_status->selected_level));
1876   return;
1877 }
1878
1879 void
1880 restore_inferior_status (inf_status)
1881      struct inferior_status *inf_status;
1882 {
1883   FRAME fid;
1884   int level = inf_status->selected_level;
1885
1886   pc_changed = inf_status->pc_changed;
1887   stop_signal = inf_status->stop_signal;
1888   stop_pc = inf_status->stop_pc;
1889   stop_frame_address = inf_status->stop_frame_address;
1890   stop_step = inf_status->stop_step;
1891   stop_stack_dummy = inf_status->stop_stack_dummy;
1892   stopped_by_random_signal = inf_status->stopped_by_random_signal;
1893   trap_expected = inf_status->trap_expected;
1894   step_range_start = inf_status->step_range_start;
1895   step_range_end = inf_status->step_range_end;
1896   step_frame_address = inf_status->step_frame_address;
1897   step_over_calls = inf_status->step_over_calls;
1898   step_resume_break_address = inf_status->step_resume_break_address;
1899   stop_after_trap = inf_status->stop_after_trap;
1900   stop_soon_quietly = inf_status->stop_soon_quietly;
1901   bpstat_clear (&stop_bpstat);
1902   stop_bpstat = inf_status->stop_bpstat;
1903   breakpoint_proceeded = inf_status->breakpoint_proceeded;
1904   proceed_to_finish = inf_status->proceed_to_finish;
1905
1906   memcpy (stop_registers, inf_status->stop_registers, REGISTER_BYTES);
1907
1908   /* The inferior can be gone if the user types "print exit(0)"
1909      (and perhaps other times).  */
1910   if (target_has_stack && inf_status->restore_stack_info)
1911     {
1912       fid = find_relative_frame (get_current_frame (),
1913                                  &level);
1914
1915       /* If inf_status->selected_frame_address is NULL, there was no
1916          previously selected frame.  */
1917       if (fid == 0 ||
1918           FRAME_FP (fid) != inf_status->selected_frame_address ||
1919           level != 0)
1920         {
1921 #if 1
1922           /* I'm not sure this error message is a good idea.  I have
1923              only seen it occur after "Can't continue previously
1924              requested operation" (we get called from do_cleanups), in
1925              which case it just adds insult to injury (one confusing
1926              error message after another.  Besides which, does the
1927              user really care if we can't restore the previously
1928              selected frame?  */
1929           fprintf (stderr, "Unable to restore previously selected frame.\n");
1930 #endif
1931           select_frame (get_current_frame (), 0);
1932           return;
1933         }
1934       
1935       select_frame (fid, inf_status->selected_level);
1936     }
1937 }
1938
1939 \f
1940 void
1941 _initialize_infrun ()
1942 {
1943   register int i;
1944   register int numsigs;
1945
1946   add_info ("signals", signals_info,
1947             "What debugger does when program gets various signals.\n\
1948 Specify a signal number as argument to print info on that signal only.");
1949   add_info_alias ("handle", "signals", 0);
1950
1951   add_com ("handle", class_run, handle_command,
1952            "Specify how to handle a signal.\n\
1953 Args are signal numbers and actions to apply to those signals.\n\
1954 Signal numbers may be numeric (ex. 11) or symbolic (ex. SIGSEGV).\n\
1955 Numeric ranges may be specified with the form LOW-HIGH (ex. 14-21).\n\
1956 The special arg \"all\" is recognized to mean all signals except those\n\
1957 used by the debugger, typically SIGTRAP and SIGINT.\n\
1958 Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
1959 \"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
1960 Stop means reenter debugger if this signal happens (implies print).\n\
1961 Print means print a message if this signal happens.\n\
1962 Pass means let program see this signal; otherwise program doesn't know.\n\
1963 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
1964 Pass and Stop may be combined.");
1965
1966   numsigs = signo_max () + 1;
1967   signal_stop    = (unsigned char *)    
1968                    xmalloc (sizeof (signal_stop[0]) * numsigs);
1969   signal_print   = (unsigned char *)
1970                    xmalloc (sizeof (signal_print[0]) * numsigs);
1971   signal_program = (unsigned char *)
1972                    xmalloc (sizeof (signal_program[0]) * numsigs);
1973   for (i = 0; i < numsigs; i++)
1974     {
1975       signal_stop[i] = 1;
1976       signal_print[i] = 1;
1977       signal_program[i] = 1;
1978     }
1979
1980   /* Signals caused by debugger's own actions
1981      should not be given to the program afterwards.  */
1982   signal_program[SIGTRAP] = 0;
1983   signal_program[SIGINT] = 0;
1984
1985   /* Signals that are not errors should not normally enter the debugger.  */
1986 #ifdef SIGALRM
1987   signal_stop[SIGALRM] = 0;
1988   signal_print[SIGALRM] = 0;
1989 #endif /* SIGALRM */
1990 #ifdef SIGVTALRM
1991   signal_stop[SIGVTALRM] = 0;
1992   signal_print[SIGVTALRM] = 0;
1993 #endif /* SIGVTALRM */
1994 #ifdef SIGPROF
1995   signal_stop[SIGPROF] = 0;
1996   signal_print[SIGPROF] = 0;
1997 #endif /* SIGPROF */
1998 #ifdef SIGCHLD
1999   signal_stop[SIGCHLD] = 0;
2000   signal_print[SIGCHLD] = 0;
2001 #endif /* SIGCHLD */
2002 #ifdef SIGCLD
2003   signal_stop[SIGCLD] = 0;
2004   signal_print[SIGCLD] = 0;
2005 #endif /* SIGCLD */
2006 #ifdef SIGIO
2007   signal_stop[SIGIO] = 0;
2008   signal_print[SIGIO] = 0;
2009 #endif /* SIGIO */
2010 #ifdef SIGURG
2011   signal_stop[SIGURG] = 0;
2012   signal_print[SIGURG] = 0;
2013 #endif /* SIGURG */
2014 }