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