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