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