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