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