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