cosmetic fixups, mainly long lines.
[external/binutils.git] / gdb / infrun.c
1 /* Target-struct-independent code to start (run) and stop an inferior process.
2    Copyright 1986, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 1998
3    Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "defs.h"
22 #include "gdb_string.h"
23 #include <ctype.h>
24 #include "symtab.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "breakpoint.h"
28 #include "wait.h"
29 #include "gdbcore.h"
30 #include "gdbcmd.h"
31 #include "target.h"
32 #include "gdbthread.h"
33 #include "annotate.h"
34 #include "symfile.h" /* for overlay functions */
35
36 #include <signal.h>
37
38 /* Prototypes for local functions */
39
40 static void signals_info PARAMS ((char *, int));
41
42 static void handle_command PARAMS ((char *, int));
43
44 static void sig_print_info PARAMS ((enum target_signal));
45
46 static void sig_print_header PARAMS ((void));
47
48 static void resume_cleanups PARAMS ((int));
49
50 static int hook_stop_stub PARAMS ((char *));
51
52 static void delete_breakpoint_current_contents PARAMS ((PTR));
53
54 int  inferior_ignoring_startup_exec_events = 0;
55 int  inferior_ignoring_leading_exec_events = 0;
56
57 #ifdef HPUXHPPA
58 /* wait_for_inferior and normal_stop use this to notify the user
59    when the inferior stopped in a different thread than it had been
60    running in. */
61 static int  switched_from_inferior_pid;
62 #endif
63
64 /* resume and wait_for_inferior use this to ensure that when
65    stepping over a hit breakpoint in a threaded application
66    only the thread that hit the breakpoint is stepped and the
67    other threads don't continue.  This prevents having another
68    thread run past the breakpoint while it is temporarily
69    removed.
70
71    This is not thread-specific, so it isn't saved as part of
72    the infrun state.
73
74    Versions of gdb which don't use the "step == this thread steps
75    and others continue" model but instead use the "step == this
76    thread steps and others wait" shouldn't do this. */
77 static int thread_step_needed = 0;
78
79 void _initialize_infrun PARAMS ((void));
80
81 /* GET_LONGJMP_TARGET returns the PC at which longjmp() will resume the
82    program.  It needs to examine the jmp_buf argument and extract the PC
83    from it.  The return value is non-zero on success, zero otherwise. */
84
85 #ifndef GET_LONGJMP_TARGET
86 #define GET_LONGJMP_TARGET(PC_ADDR) 0
87 #endif
88
89
90 /* Some machines have trampoline code that sits between function callers
91    and the actual functions themselves.  If this machine doesn't have
92    such things, disable their processing.  */
93
94 #ifndef SKIP_TRAMPOLINE_CODE
95 #define SKIP_TRAMPOLINE_CODE(pc)        0
96 #endif
97
98 /* Dynamic function trampolines are similar to solib trampolines in that they
99    are between the caller and the callee.  The difference is that when you
100    enter a dynamic trampoline, you can't determine the callee's address.  Some
101    (usually complex) code needs to run in the dynamic trampoline to figure out
102    the callee's address.  This macro is usually called twice.  First, when we
103    enter the trampoline (looks like a normal function call at that point).  It
104    should return the PC of a point within the trampoline where the callee's
105    address is known.  Second, when we hit the breakpoint, this routine returns
106    the callee's address.  At that point, things proceed as per a step resume
107    breakpoint.  */
108
109 #ifndef DYNAMIC_TRAMPOLINE_NEXTPC
110 #define DYNAMIC_TRAMPOLINE_NEXTPC(pc) 0
111 #endif
112
113 /* On SVR4 based systems, determining the callee's address is exceedingly
114    difficult and depends on the implementation of the run time loader.
115    If we are stepping at the source level, we single step until we exit
116    the run time loader code and reach the callee's address.  */
117
118 #ifndef IN_SOLIB_DYNSYM_RESOLVE_CODE
119 #define IN_SOLIB_DYNSYM_RESOLVE_CODE(pc) 0
120 #endif
121
122 /* For SVR4 shared libraries, each call goes through a small piece of
123    trampoline code in the ".plt" section.  IN_SOLIB_CALL_TRAMPOLINE evaluates
124    to nonzero if we are current stopped in one of these. */
125
126 #ifndef IN_SOLIB_CALL_TRAMPOLINE
127 #define IN_SOLIB_CALL_TRAMPOLINE(pc,name)       0
128 #endif
129
130 /* In some shared library schemes, the return path from a shared library
131    call may need to go through a trampoline too.  */
132
133 #ifndef IN_SOLIB_RETURN_TRAMPOLINE
134 #define IN_SOLIB_RETURN_TRAMPOLINE(pc,name)     0
135 #endif
136
137 /* On MIPS16, a function that returns a floating point value may call
138    a library helper function to copy the return value to a floating point
139    register.  The IGNORE_HELPER_CALL macro returns non-zero if we
140    should ignore (i.e. step over) this function call.  */
141 #ifndef IGNORE_HELPER_CALL
142 #define IGNORE_HELPER_CALL(pc)  0
143 #endif
144
145 /* On some systems, the PC may be left pointing at an instruction that  won't
146    actually be executed.  This is usually indicated by a bit in the PSW.  If
147    we find ourselves in such a state, then we step the target beyond the
148    nullified instruction before returning control to the user so as to avoid
149    confusion. */
150
151 #ifndef INSTRUCTION_NULLIFIED
152 #define INSTRUCTION_NULLIFIED 0
153 #endif
154
155 /* Tables of how to react to signals; the user sets them.  */
156
157 static unsigned char *signal_stop;
158 static unsigned char *signal_print;
159 static unsigned char *signal_program;
160
161 #define SET_SIGS(nsigs,sigs,flags) \
162   do { \
163     int signum = (nsigs); \
164     while (signum-- > 0) \
165       if ((sigs)[signum]) \
166         (flags)[signum] = 1; \
167   } while (0)
168
169 #define UNSET_SIGS(nsigs,sigs,flags) \
170   do { \
171     int signum = (nsigs); \
172     while (signum-- > 0) \
173       if ((sigs)[signum]) \
174         (flags)[signum] = 0; \
175   } while (0)
176
177
178 /* Command list pointer for the "stop" placeholder.  */
179
180 static struct cmd_list_element *stop_command;
181
182 /* Nonzero if breakpoints are now inserted in the inferior.  */
183
184 static int breakpoints_inserted;
185
186 /* Function inferior was in as of last step command.  */
187
188 static struct symbol *step_start_function;
189
190 /* Nonzero if we are expecting a trace trap and should proceed from it.  */
191
192 static int trap_expected;
193
194 #ifdef SOLIB_ADD
195 /* Nonzero if we want to give control to the user when we're notified
196    of shared library events by the dynamic linker.  */
197 static int stop_on_solib_events;
198 #endif
199
200 #ifdef HP_OS_BUG
201 /* Nonzero if the next time we try to continue the inferior, it will
202    step one instruction and generate a spurious trace trap.
203    This is used to compensate for a bug in HP-UX.  */
204
205 static int trap_expected_after_continue;
206 #endif
207
208 /* Nonzero means expecting a trace trap
209    and should stop the inferior and return silently when it happens.  */
210
211 int stop_after_trap;
212
213 /* Nonzero means expecting a trap and caller will handle it themselves.
214    It is used after attach, due to attaching to a process;
215    when running in the shell before the child program has been exec'd;
216    and when running some kinds of remote stuff (FIXME?).  */
217
218 int stop_soon_quietly;
219
220 /* Nonzero if proceed is being used for a "finish" command or a similar
221    situation when stop_registers should be saved.  */
222
223 int proceed_to_finish;
224
225 /* Save register contents here when about to pop a stack dummy frame,
226    if-and-only-if proceed_to_finish is set.
227    Thus this contains the return value from the called function (assuming
228    values are returned in a register).  */
229
230 char stop_registers[REGISTER_BYTES];
231
232 /* Nonzero if program stopped due to error trying to insert breakpoints.  */
233
234 static int breakpoints_failed;
235
236 /* Nonzero after stop if current stack frame should be printed.  */
237
238 static int stop_print_frame;
239
240 static struct breakpoint *step_resume_breakpoint = NULL;
241 static struct breakpoint *through_sigtramp_breakpoint = NULL;
242
243 /* On some platforms (e.g., HP-UX), hardware watchpoints have bad
244    interactions with an inferior that is running a kernel function
245    (aka, a system call or "syscall").  wait_for_inferior therefore
246    may have a need to know when the inferior is in a syscall.  This
247    is a count of the number of inferior threads which are known to
248    currently be running in a syscall. */
249 static int  number_of_threads_in_syscalls;
250
251 /* This is used to remember when a fork, vfork or exec event
252    was caught by a catchpoint, and thus the event is to be
253    followed at the next resume of the inferior, and not
254    immediately. */
255 static struct {
256   enum target_waitkind  kind;
257   struct {
258     int  parent_pid;
259     int  saw_parent_fork;
260     int  child_pid;
261     int  saw_child_fork;
262     int  saw_child_exec;
263   } fork_event;
264   char *  execd_pathname;
265 } pending_follow;
266
267 /* Some platforms don't allow us to do anything meaningful with a
268    vforked child until it has exec'd.  Vforked processes on such
269    platforms can only be followed after they've exec'd.
270
271    When this is set to 0, a vfork can be immediately followed,
272    and an exec can be followed merely as an exec.  When this is
273    set to 1, a vfork event has been seen, but cannot be followed
274    until the exec is seen.
275
276    (In the latter case, inferior_pid is still the parent of the
277    vfork, and pending_follow.fork_event.child_pid is the child.  The
278    appropriate process is followed, according to the setting of
279    follow-fork-mode.) */
280 static int  follow_vfork_when_exec;
281
282 static char *  follow_fork_mode_kind_names [] = {
283 /* ??rehrauer:  The "both" option is broken, by what may be a 10.20
284    kernel problem.  It's also not terribly useful without a GUI to
285    help the user drive two debuggers.  So for now, I'm disabling
286    the "both" option.
287   "parent", "child", "both", "ask" };
288   */
289   "parent", "child", "ask" };
290
291 static char *  follow_fork_mode_string = NULL;
292
293 \f
294 #if defined(HPUXHPPA)
295 static void
296 follow_inferior_fork (parent_pid, child_pid, has_forked, has_vforked)
297   int  parent_pid;
298   int  child_pid;
299   int  has_forked;
300   int  has_vforked;
301 {
302   int  followed_parent = 0;
303   int  followed_child = 0;
304   int  ima_clone = 0;
305
306   /* Which process did the user want us to follow? */
307   char *  follow_mode =
308     savestring (follow_fork_mode_string, strlen (follow_fork_mode_string));
309
310   /* Or, did the user not know, and want us to ask? */
311   if (STREQ (follow_fork_mode_string, "ask"))
312     {
313       char  requested_mode[100];
314
315       free (follow_mode);
316       error ("\"ask\" mode NYI");
317       follow_mode = savestring (requested_mode, strlen (requested_mode));
318     }
319
320   /* If we're to be following the parent, then detach from child_pid.
321      We're already following the parent, so need do nothing explicit
322      for it. */
323   if (STREQ (follow_mode, "parent"))
324     {
325       followed_parent = 1;
326
327       /* We're already attached to the parent, by default. */
328
329       /* Before detaching from the child, remove all breakpoints from
330          it.  (This won't actually modify the breakpoint list, but will
331          physically remove the breakpoints from the child.) */
332       if (! has_vforked || ! follow_vfork_when_exec)
333         {
334           detach_breakpoints (child_pid);
335           SOLIB_REMOVE_INFERIOR_HOOK (child_pid);
336         }
337
338       /* Detach from the child. */
339       dont_repeat ();
340
341       target_require_detach (child_pid, "", 1);
342     }
343
344   /* If we're to be following the child, then attach to it, detach
345      from inferior_pid, and set inferior_pid to child_pid. */
346   else if (STREQ (follow_mode, "child"))
347     {
348       char  child_pid_spelling [100];  /* Arbitrary length. */
349
350       followed_child = 1;
351
352       /* Before detaching from the parent, detach all breakpoints from
353          the child.  But only if we're forking, or if we follow vforks
354          as soon as they happen.  (If we're following vforks only when
355          the child has exec'd, then it's very wrong to try to write
356          back the "shadow contents" of inserted breakpoints now -- they
357          belong to the child's pre-exec'd a.out.) */
358       if (! has_vforked || ! follow_vfork_when_exec)
359         {
360           detach_breakpoints (child_pid);
361         }
362
363       /* Before detaching from the parent, remove all breakpoints from it. */
364       remove_breakpoints ();
365
366       /* Also reset the solib inferior hook from the parent. */
367       SOLIB_REMOVE_INFERIOR_HOOK (inferior_pid);
368
369       /* Detach from the parent. */
370       dont_repeat ();
371       target_detach (NULL, 1);
372
373       /* Attach to the child. */
374       inferior_pid = child_pid;
375       sprintf (child_pid_spelling, "%d", child_pid);
376       dont_repeat ();
377
378       target_require_attach (child_pid_spelling, 1);
379
380       /* Was there a step_resume breakpoint?  (There was if the user
381          did a "next" at the fork() call.)  If so, explicitly reset its
382          thread number.
383
384          step_resumes are a form of bp that are made to be per-thread.
385          Since we created the step_resume bp when the parent process
386          was being debugged, and now are switching to the child process,
387          from the breakpoint package's viewpoint, that's a switch of
388          "threads".  We must update the bp's notion of which thread
389          it is for, or it'll be ignored when it triggers... */
390       if (step_resume_breakpoint && 
391           (! has_vforked || ! follow_vfork_when_exec))
392         breakpoint_re_set_thread (step_resume_breakpoint);
393
394       /* Reinsert all breakpoints in the child.  (The user may've set
395          breakpoints after catching the fork, in which case those
396          actually didn't get set in the child, but only in the parent.) */
397       if (! has_vforked || ! follow_vfork_when_exec)
398         {
399           breakpoint_re_set ();
400           insert_breakpoints ();
401         }
402     }
403
404   /* If we're to be following both parent and child, then fork ourselves,
405      and attach the debugger clone to the child. */
406   else if (STREQ (follow_mode, "both"))
407     {
408       char  pid_suffix [100];  /* Arbitrary length. */
409
410       /* Clone ourselves to follow the child.  This is the end of our
411        involvement with child_pid; our clone will take it from here... */
412       dont_repeat ();
413       target_clone_and_follow_inferior (child_pid, &followed_child);
414       followed_parent = !followed_child;
415
416       /* We continue to follow the parent.  To help distinguish the two
417          debuggers, though, both we and our clone will reset our prompts. */
418       sprintf (pid_suffix, "[%d] ", inferior_pid);
419       set_prompt (strcat (get_prompt (), pid_suffix));
420     }
421
422   /* The parent and child of a vfork share the same address space.
423      Also, on some targets the order in which vfork and exec events
424      are received for parent in child requires some delicate handling
425      of the events.
426
427      For instance, on ptrace-based HPUX we receive the child's vfork
428      event first, at which time the parent has been suspended by the
429      OS and is essentially untouchable until the child's exit or second
430      exec event arrives.  At that time, the parent's vfork event is
431      delivered to us, and that's when we see and decide how to follow
432      the vfork.  But to get to that point, we must continue the child
433      until it execs or exits.  To do that smoothly, all breakpoints
434      must be removed from the child, in case there are any set between
435      the vfork() and exec() calls.  But removing them from the child
436      also removes them from the parent, due to the shared-address-space
437      nature of a vfork'd parent and child.  On HPUX, therefore, we must
438      take care to restore the bp's to the parent before we continue it.
439      Else, it's likely that we may not stop in the expected place.  (The
440      worst scenario is when the user tries to step over a vfork() call;
441      the step-resume bp must be restored for the step to properly stop
442      in the parent after the call completes!)
443
444      Sequence of events, as reported to gdb from HPUX:
445
446            Parent        Child           Action for gdb to take
447          -------------------------------------------------------
448         1                VFORK               Continue child
449         2                EXEC
450         3                EXEC or EXIT
451         4  VFORK */
452   if (has_vforked)
453     {
454       target_post_follow_vfork (parent_pid,
455                                 followed_parent,
456                                 child_pid,
457                                 followed_child);
458     }
459
460   pending_follow.fork_event.saw_parent_fork = 0;
461   pending_follow.fork_event.saw_child_fork = 0;
462
463   free (follow_mode);
464 }
465
466 static void
467 follow_fork (parent_pid, child_pid)
468   int  parent_pid;
469   int  child_pid;
470 {
471   follow_inferior_fork (parent_pid, child_pid, 1, 0);
472 }
473
474
475 /* Forward declaration. */
476 static void follow_exec PARAMS((int, char *));
477
478 static void
479 follow_vfork (parent_pid, child_pid)
480   int  parent_pid;
481   int  child_pid;
482 {
483   follow_inferior_fork (parent_pid, child_pid, 0, 1);
484
485   /* Did we follow the child?  Had it exec'd before we saw the parent vfork? */
486   if (pending_follow.fork_event.saw_child_exec && (inferior_pid == child_pid))
487     {
488       pending_follow.fork_event.saw_child_exec = 0;
489       pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
490       follow_exec (inferior_pid, pending_follow.execd_pathname);
491       free (pending_follow.execd_pathname);
492     }
493 }
494 #endif /* HPUXHPPA */
495
496 static void
497 follow_exec (pid, execd_pathname)
498   int  pid;
499   char *  execd_pathname;
500 {
501 #ifdef HPUXHPPA
502   int  saved_pid = pid;
503   extern struct target_ops  child_ops;
504
505   /* Did this exec() follow a vfork()?  If so, we must follow the
506      vfork now too.  Do it before following the exec. */
507   if (follow_vfork_when_exec && 
508       (pending_follow.kind == TARGET_WAITKIND_VFORKED))
509     {
510       pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
511       follow_vfork (inferior_pid, pending_follow.fork_event.child_pid);
512       follow_vfork_when_exec = 0;
513       saved_pid = inferior_pid;
514
515       /* Did we follow the parent?  If so, we're done.  If we followed
516          the child then we must also follow its exec(). */
517       if (inferior_pid == pending_follow.fork_event.parent_pid)
518         return;
519     }
520
521   /* This is an exec event that we actually wish to pay attention to.
522      Refresh our symbol table to the newly exec'd program, remove any
523      momentary bp's, etc.
524
525      If there are breakpoints, they aren't really inserted now,
526      since the exec() transformed our inferior into a fresh set
527      of instructions.
528
529      We want to preserve symbolic breakpoints on the list, since
530      we have hopes that they can be reset after the new a.out's
531      symbol table is read.
532
533      However, any "raw" breakpoints must be removed from the list
534      (e.g., the solib bp's), since their address is probably invalid
535      now.
536
537      And, we DON'T want to call delete_breakpoints() here, since
538      that may write the bp's "shadow contents" (the instruction
539      value that was overwritten witha TRAP instruction).  Since
540      we now have a new a.out, those shadow contents aren't valid. */
541   update_breakpoints_after_exec ();
542
543   /* If there was one, it's gone now.  We cannot truly step-to-next
544      statement through an exec(). */
545   step_resume_breakpoint = NULL;
546   step_range_start = 0;
547   step_range_end = 0;
548
549   /* If there was one, it's gone now. */
550   through_sigtramp_breakpoint = NULL;
551
552   /* What is this a.out's name? */
553   printf_unfiltered ("Executing new program: %s\n", execd_pathname);
554
555   /* We've followed the inferior through an exec.  Therefore, the
556      inferior has essentially been killed & reborn. */
557   gdb_flush (gdb_stdout);
558   target_mourn_inferior ();
559   inferior_pid = saved_pid; /* Because mourn_inferior resets inferior_pid. */
560   push_target (&child_ops);
561
562   /* That a.out is now the one to use. */
563   exec_file_attach (execd_pathname, 0);
564
565   /* And also is where symbols can be found. */
566   symbol_file_command (execd_pathname, 0);
567
568   /* Reset the shared library package.  This ensures that we get
569      a shlib event when the child reaches "_start", at which point
570      the dld will have had a chance to initialize the child. */
571   SOLIB_RESTART ();
572   SOLIB_CREATE_INFERIOR_HOOK (inferior_pid);
573
574   /* Reinsert all breakpoints.  (Those which were symbolic have
575      been reset to the proper address in the new a.out, thanks
576      to symbol_file_command...) */
577   insert_breakpoints ();
578
579   /* The next resume of this inferior should bring it to the shlib
580      startup breakpoints.  (If the user had also set bp's on
581      "main" from the old (parent) process, then they'll auto-
582      matically get reset there in the new process.) */
583 #endif
584 }
585
586 /* Non-zero if we just simulating a single-step.  This is needed
587    because we cannot remove the breakpoints in the inferior process
588    until after the `wait' in `wait_for_inferior'.  */
589 static int singlestep_breakpoints_inserted_p = 0;
590
591 \f
592 /* Things to clean up if we QUIT out of resume ().  */
593 /* ARGSUSED */
594 static void
595 resume_cleanups (arg)
596      int arg;
597 {
598   normal_stop ();
599 }
600
601 /* Resume the inferior, but allow a QUIT.  This is useful if the user
602    wants to interrupt some lengthy single-stepping operation
603    (for child processes, the SIGINT goes to the inferior, and so
604    we get a SIGINT random_signal, but for remote debugging and perhaps
605    other targets, that's not true).
606
607    STEP nonzero if we should step (zero to continue instead).
608    SIG is the signal to give the inferior (zero for none).  */
609 void
610 resume (step, sig)
611      int step;
612      enum target_signal sig;
613 {
614   int should_resume = 1;
615   struct cleanup *old_cleanups = make_cleanup ((make_cleanup_func) 
616                                                resume_cleanups, 0);
617   QUIT;
618
619 #ifdef CANNOT_STEP_BREAKPOINT
620   /* Most targets can step a breakpoint instruction, thus executing it
621      normally.  But if this one cannot, just continue and we will hit
622      it anyway.  */
623   if (step && breakpoints_inserted && breakpoint_here_p (read_pc ()))
624     step = 0;
625 #endif
626
627   if (SOFTWARE_SINGLE_STEP_P && step)
628     {
629       /* Do it the hard way, w/temp breakpoints */
630       SOFTWARE_SINGLE_STEP (sig, 1 /*insert-breakpoints*/);
631       /* ...and don't ask hardware to do it.  */
632       step = 0;
633       /* and do not pull these breakpoints until after a `wait' in
634          `wait_for_inferior' */
635       singlestep_breakpoints_inserted_p = 1;
636     }
637
638   /* Handle any optimized stores to the inferior NOW...  */
639 #ifdef DO_DEFERRED_STORES
640   DO_DEFERRED_STORES;
641 #endif
642
643 #ifdef HPUXHPPA
644   /* If there were any forks/vforks/execs that were caught and are
645      now to be followed, then do so. */
646   switch (pending_follow.kind)
647     {
648       case (TARGET_WAITKIND_FORKED):
649         pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
650         follow_fork (inferior_pid, pending_follow.fork_event.child_pid);
651         break;
652
653       case (TARGET_WAITKIND_VFORKED):
654         {
655           int  saw_child_exec = pending_follow.fork_event.saw_child_exec;
656
657           pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
658           follow_vfork (inferior_pid, pending_follow.fork_event.child_pid);
659
660           /* Did we follow the child, but not yet see the child's exec event?
661              If so, then it actually ought to be waiting for us; we respond to
662              parent vfork events.  We don't actually want to resume the child
663              in this situation; we want to just get its exec event. */
664           if (! saw_child_exec && 
665               (inferior_pid == pending_follow.fork_event.child_pid))
666             should_resume = 0;
667         }
668         break;
669
670       case (TARGET_WAITKIND_EXECD):
671         /* If we saw a vfork event but couldn't follow it until we saw
672            an exec, then now might be the time! */
673         pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
674         /* follow_exec is called as soon as the exec event is seen. */
675         break;
676
677       default:
678         break;
679     }
680 #endif /* HPUXHPPA */
681
682   /* Install inferior's terminal modes.  */
683   target_terminal_inferior ();
684
685   if (should_resume)
686     {
687 #ifdef HPUXHPPA
688       if (thread_step_needed) 
689         {
690           /* We stopped on a BPT instruction;
691              don't continue other threads and
692              just step this thread. */
693           thread_step_needed = 0;
694           
695           if (!breakpoint_here_p(read_pc()))
696             {
697               /* Breakpoint deleted: ok to do regular resume
698                  where all the threads either step or continue. */
699               target_resume (-1, step, sig);
700             }  
701           else
702             {
703               if (!step)
704                 {
705                   warning ( "Internal error, changing continue to step." );
706                   remove_breakpoints ();
707                   breakpoints_inserted = 0;
708                   trap_expected = 1;
709                   step = 1;
710                 }
711             
712               target_resume (inferior_pid, step, sig);
713             }
714         }
715       else
716 #endif /* HPUXHPPA */
717
718         /* Vanilla resume. */
719         target_resume (-1, step, sig);
720     }
721     
722   discard_cleanups (old_cleanups);
723 }
724
725 \f
726 /* Clear out all variables saying what to do when inferior is continued.
727    First do this, then set the ones you want, then call `proceed'.  */
728
729 void
730 clear_proceed_status ()
731 {
732   trap_expected = 0;
733   step_range_start = 0;
734   step_range_end = 0;
735   step_frame_address = 0;
736   step_over_calls = -1;
737   stop_after_trap = 0;
738   stop_soon_quietly = 0;
739   proceed_to_finish = 0;
740   breakpoint_proceeded = 1;     /* We're about to proceed... */
741
742   /* Discard any remaining commands or status from previous stop.  */
743   bpstat_clear (&stop_bpstat);
744 }
745
746 /* Basic routine for continuing the program in various fashions.
747
748    ADDR is the address to resume at, or -1 for resume where stopped.
749    SIGGNAL is the signal to give it, or 0 for none,
750      or -1 for act according to how it stopped.
751    STEP is nonzero if should trap after one instruction.
752      -1 means return after that and print nothing.
753      You should probably set various step_... variables
754      before calling here, if you are stepping.
755
756    You should call clear_proceed_status before calling proceed.  */
757
758 void
759 proceed (addr, siggnal, step)
760      CORE_ADDR addr;
761      enum target_signal siggnal;
762      int step;
763 {
764   int oneproc = 0;
765
766   if (step > 0)
767     step_start_function = find_pc_function (read_pc ());
768   if (step < 0)
769     stop_after_trap = 1;
770
771   if (addr == (CORE_ADDR)-1)
772     {
773       /* If there is a breakpoint at the address we will resume at,
774          step one instruction before inserting breakpoints
775          so that we do not stop right away (and report a second
776          hit at this breakpoint).  */
777
778       if (read_pc () == stop_pc && breakpoint_here_p (read_pc ()))
779         oneproc = 1;
780
781 #ifndef STEP_SKIPS_DELAY
782 #define STEP_SKIPS_DELAY(pc) (0)
783 #define STEP_SKIPS_DELAY_P (0)
784 #endif
785       /* Check breakpoint_here_p first, because breakpoint_here_p is fast
786          (it just checks internal GDB data structures) and STEP_SKIPS_DELAY
787          is slow (it needs to read memory from the target).  */
788       if (STEP_SKIPS_DELAY_P
789           && breakpoint_here_p (read_pc () + 4)
790           && STEP_SKIPS_DELAY (read_pc ()))
791         oneproc = 1;
792     }
793   else
794     {
795       write_pc (addr);
796
797       /* New address; we don't need to single-step a thread
798          over a breakpoint we just hit, 'cause we aren't
799          continuing from there.
800        
801          It's not worth worrying about the case where a user
802          asks for a "jump" at the current PC--if they get the
803          hiccup of re-hiting a hit breakpoint, what else do
804          they expect? */
805       thread_step_needed = 0;
806     }
807
808 #ifdef PREPARE_TO_PROCEED
809   /* In a multi-threaded task we may select another thread
810      and then continue or step.
811    
812      But if the old thread was stopped at a breakpoint, it
813      will immediately cause another breakpoint stop without
814      any execution (i.e. it will report a breakpoint hit
815      incorrectly).  So we must step over it first.
816    
817      PREPARE_TO_PROCEED checks the current thread against the thread
818      that reported the most recent event.  If a step-over is required
819      it returns TRUE and sets the current thread to the old thread. */
820   if (PREPARE_TO_PROCEED() && breakpoint_here_p (read_pc()))
821     {
822       oneproc = 1;
823       thread_step_needed = 1;
824     }
825
826 #endif /* PREPARE_TO_PROCEED */
827
828 #ifdef HP_OS_BUG
829   if (trap_expected_after_continue)
830     {
831       /* If (step == 0), a trap will be automatically generated after
832          the first instruction is executed.  Force step one
833          instruction to clear this condition.  This should not occur
834          if step is nonzero, but it is harmless in that case.  */
835       oneproc = 1;
836       trap_expected_after_continue = 0;
837     }
838 #endif /* HP_OS_BUG */
839
840   if (oneproc)
841     /* We will get a trace trap after one instruction.
842        Continue it automatically and insert breakpoints then.  */
843     trap_expected = 1;
844   else
845     {
846       int temp = insert_breakpoints ();
847       if (temp)
848         {
849           print_sys_errmsg ("ptrace", temp);
850           error ("Cannot insert breakpoints.\n\
851 The same program may be running in another process.");
852         }
853
854       breakpoints_inserted = 1;
855     }
856
857   if (siggnal != TARGET_SIGNAL_DEFAULT)
858     stop_signal = siggnal;
859   /* If this signal should not be seen by program,
860      give it zero.  Used for debugging signals.  */
861   else if (!signal_program[stop_signal])
862     stop_signal = TARGET_SIGNAL_0;
863
864   annotate_starting ();
865
866   /* Make sure that output from GDB appears before output from the
867      inferior.  */
868   gdb_flush (gdb_stdout);
869
870   /* Resume inferior.  */
871   resume (oneproc || step || bpstat_should_step (), stop_signal);
872
873   /* Wait for it to stop (if not standalone)
874      and in any case decode why it stopped, and act accordingly.  */
875
876   wait_for_inferior ();
877   normal_stop ();
878 }
879
880 /* Record the pc and sp of the program the last time it stopped.
881    These are just used internally by wait_for_inferior, but need
882    to be preserved over calls to it and cleared when the inferior
883    is started.  */
884 static CORE_ADDR prev_pc;
885 static CORE_ADDR prev_func_start;
886 static char *prev_func_name;
887
888 \f
889 /* Start remote-debugging of a machine over a serial link.  */
890
891 void
892 start_remote ()
893 {
894   init_thread_list ();
895   init_wait_for_inferior ();
896   stop_soon_quietly = 1;
897   trap_expected = 0;
898   wait_for_inferior ();
899   normal_stop ();
900 }
901
902 /* Initialize static vars when a new inferior begins.  */
903
904 void
905 init_wait_for_inferior ()
906 {
907   /* These are meaningless until the first time through wait_for_inferior.  */
908   prev_pc = 0;
909   prev_func_start = 0;
910   prev_func_name = NULL;
911
912 #ifdef HP_OS_BUG
913   trap_expected_after_continue = 0;
914 #endif
915   breakpoints_inserted = 0;
916   breakpoint_init_inferior (inf_starting);
917
918   /* Don't confuse first call to proceed(). */
919   stop_signal = TARGET_SIGNAL_0;
920
921   /* The first resume is not following a fork/vfork/exec. */
922   pending_follow.kind = TARGET_WAITKIND_SPURIOUS;  /* I.e., none. */
923   pending_follow.fork_event.saw_parent_fork = 0;
924   pending_follow.fork_event.saw_child_fork = 0;
925   pending_follow.fork_event.saw_child_exec = 0;
926
927   /* See wait_for_inferior's handling of SYSCALL_ENTRY/RETURN events. */
928   number_of_threads_in_syscalls = 0;
929
930   clear_proceed_status ();
931 }
932
933 static void
934 delete_breakpoint_current_contents (arg)
935      PTR arg;
936 {
937   struct breakpoint **breakpointp = (struct breakpoint **)arg;
938   if (*breakpointp != NULL) 
939     {
940      delete_breakpoint (*breakpointp);
941      *breakpointp = NULL;
942     }
943 }
944 \f
945 /* Wait for control to return from inferior to debugger.
946    If inferior gets a signal, we may decide to start it up again
947    instead of returning.  That is why there is a loop in this function.
948    When this function actually returns it means the inferior
949    should be left stopped and GDB should read more commands.  */
950
951 void
952 wait_for_inferior ()
953 {
954   struct cleanup *old_cleanups;
955   struct target_waitstatus w;
956   int another_trap;
957   int random_signal = 0;
958   CORE_ADDR stop_func_start;
959   CORE_ADDR stop_func_end;
960   char *stop_func_name;
961 #if 0
962   CORE_ADDR prologue_pc = 0;
963 #endif
964   CORE_ADDR tmp;
965   struct symtab_and_line sal;
966   int remove_breakpoints_on_following_step = 0;
967   int current_line;
968   struct symtab *current_symtab;
969   int handling_longjmp = 0;     /* FIXME */
970   int pid;
971   int  saved_inferior_pid;
972   int update_step_sp = 0;
973   int  stepping_through_solib_after_catch = 0;
974   bpstat  stepping_through_solib_catchpoints = NULL;
975   int  enable_hw_watchpoints_after_wait = 0;
976   int stepping_through_sigtramp = 0;
977   int  new_thread_event;
978
979 #ifdef HAVE_NONSTEPPABLE_WATCHPOINT
980   int  stepped_after_stopped_by_watchpoint;
981 #endif
982
983   old_cleanups = make_cleanup (delete_breakpoint_current_contents,
984                                &step_resume_breakpoint);
985   make_cleanup (delete_breakpoint_current_contents,
986                 &through_sigtramp_breakpoint);
987   sal = find_pc_line(prev_pc, 0);
988   current_line = sal.line;
989   current_symtab = sal.symtab;
990
991   /* Are we stepping?  */
992 #define CURRENTLY_STEPPING() \
993   ((through_sigtramp_breakpoint == NULL \
994     && !handling_longjmp \
995     && ((step_range_end && step_resume_breakpoint == NULL) \
996         || trap_expected)) \
997    || stepping_through_solib_after_catch \
998    || bpstat_should_step ())
999     ;
1000   thread_step_needed = 0;
1001
1002 #ifdef HPUXHPPA
1003   /* We'll update this if & when we switch to a new thread. */
1004   switched_from_inferior_pid = inferior_pid;
1005 #endif
1006
1007   while (1)
1008     {
1009       extern int overlay_cache_invalid; /* declared in symfile.h */
1010
1011       overlay_cache_invalid = 1;
1012
1013       /* We have to invalidate the registers BEFORE calling target_wait because
1014          they can be loaded from the target while in target_wait.  This makes
1015          remote debugging a bit more efficient for those targets that provide
1016          critical registers as part of their normal status mechanism. */
1017
1018       registers_changed ();
1019
1020       if (target_wait_hook)
1021         pid = target_wait_hook (-1, &w);
1022       else
1023         pid = target_wait (-1, &w);
1024
1025       /* Since we've done a wait, we have a new event.  Don't carry
1026          over any expectations about needing to step over a
1027          breakpoint. */
1028       thread_step_needed = 0;
1029       
1030       /* See comments where a TARGET_WAITKIND_SYSCALL_RETURN event is
1031          serviced in this loop, below. */
1032       if (enable_hw_watchpoints_after_wait)
1033         {
1034           TARGET_ENABLE_HW_WATCHPOINTS(inferior_pid);
1035           enable_hw_watchpoints_after_wait = 0;
1036         }
1037
1038
1039 #ifdef HAVE_NONSTEPPABLE_WATCHPOINT
1040 stepped_after_stopped_by_watchpoint = 0;
1041 #endif
1042
1043     /* Gross.
1044
1045        We goto this label from elsewhere in wait_for_inferior when we want
1046        to continue the main loop without calling "wait" and trashing the
1047        waitstatus contained in W.  */
1048     have_waited:
1049
1050       flush_cached_frames ();
1051
1052       /* If it's a new process, add it to the thread database */
1053
1054       new_thread_event = ((pid != inferior_pid) && !in_thread_list (pid));
1055     
1056       if (w.kind != TARGET_WAITKIND_EXITED
1057           && w.kind != TARGET_WAITKIND_SIGNALLED
1058           && new_thread_event)
1059         {
1060           add_thread (pid);
1061
1062
1063 #ifdef HPUXHPPA
1064           fprintf_unfiltered (gdb_stderr, "[New %s]\n", 
1065                               target_pid_or_tid_to_str (pid));
1066
1067 #else
1068           printf_filtered ("[New %s]\n", target_pid_to_str (pid));
1069 #endif
1070
1071 #if 0
1072           /* NOTE: This block is ONLY meant to be invoked in case of a
1073              "thread creation event"!  If it is invoked for any other 
1074              sort of event (such as a new thread landing on a breakpoint),
1075              the event will be discarded, which is almost certainly
1076              a bad thing! 
1077              
1078              To avoid this, the low-level module (eg. target_wait) 
1079              should call in_thread_list and add_thread, so that the
1080              new thread is known by the time we get here.  */
1081
1082           /* We may want to consider not doing a resume here in order
1083              to give the user a chance to play with the new thread.
1084              It might be good to make that a user-settable option.  */
1085
1086           /* At this point, all threads are stopped (happens
1087              automatically in either the OS or the native code).
1088              Therefore we need to continue all threads in order to
1089              make progress.  */
1090
1091           target_resume (-1, 0, TARGET_SIGNAL_0);
1092           continue;
1093 #endif
1094        }
1095
1096       switch (w.kind)
1097         {
1098         case TARGET_WAITKIND_LOADED:
1099           /* Ignore gracefully during startup of the inferior, as it
1100              might be the shell which has just loaded some objects,
1101              otherwise add the symbols for the newly loaded objects.  */
1102 #ifdef SOLIB_ADD
1103           if (!stop_soon_quietly)
1104             {
1105               extern int auto_solib_add;
1106
1107               /* Remove breakpoints, SOLIB_ADD might adjust
1108                  breakpoint addresses via breakpoint_re_set.  */
1109               if (breakpoints_inserted)
1110                 remove_breakpoints ();
1111
1112               /* Check for any newly added shared libraries if we're
1113                  supposed to be adding them automatically.  */
1114               if (auto_solib_add)
1115                 {
1116                   /* Switch terminal for any messages produced by
1117                      breakpoint_re_set.  */
1118                   target_terminal_ours_for_output ();
1119                   SOLIB_ADD (NULL, 0, NULL);
1120                   target_terminal_inferior ();
1121                 }
1122
1123               /* Reinsert breakpoints and continue.  */
1124               if (breakpoints_inserted)
1125                 insert_breakpoints ();
1126             }
1127 #endif
1128           resume (0, TARGET_SIGNAL_0);
1129           continue;
1130
1131         case TARGET_WAITKIND_SPURIOUS:
1132           resume (0, TARGET_SIGNAL_0);
1133           continue;
1134
1135         case TARGET_WAITKIND_EXITED:
1136           target_terminal_ours ();      /* Must do this before mourn anyway */
1137           annotate_exited (w.value.integer);
1138           if (w.value.integer)
1139             printf_filtered ("\nProgram exited with code 0%o.\n", 
1140                              (unsigned int)w.value.integer);
1141           else
1142             printf_filtered ("\nProgram exited normally.\n");
1143
1144           /* Record the exit code in the convenience variable $_exitcode, so
1145              that the user can inspect this again later.  */
1146           set_internalvar (lookup_internalvar ("_exitcode"),
1147                            value_from_longest (builtin_type_int, 
1148                                                (LONGEST) w.value.integer));
1149           gdb_flush (gdb_stdout);
1150           target_mourn_inferior ();
1151           singlestep_breakpoints_inserted_p = 0; /*SOFTWARE_SINGLE_STEP_P*/
1152           stop_print_frame = 0;
1153           goto stop_stepping;
1154
1155         case TARGET_WAITKIND_SIGNALLED:
1156           stop_print_frame = 0;
1157           stop_signal = w.value.sig;
1158           target_terminal_ours ();      /* Must do this before mourn anyway */
1159           annotate_signalled ();
1160
1161           /* This looks pretty bogus to me.  Doesn't TARGET_WAITKIND_SIGNALLED
1162              mean it is already dead?  This has been here since GDB 2.8, so
1163              perhaps it means rms didn't understand unix waitstatuses?
1164              For the moment I'm just kludging around this in remote.c
1165              rather than trying to change it here --kingdon, 5 Dec 1994.  */
1166           target_kill ();               /* kill mourns as well */
1167
1168           printf_filtered ("\nProgram terminated with signal ");
1169           annotate_signal_name ();
1170           printf_filtered ("%s", target_signal_to_name (stop_signal));
1171           annotate_signal_name_end ();
1172           printf_filtered (", ");
1173           annotate_signal_string ();
1174           printf_filtered ("%s", target_signal_to_string (stop_signal));
1175           annotate_signal_string_end ();
1176           printf_filtered (".\n");
1177
1178           printf_filtered ("The program no longer exists.\n");
1179           gdb_flush (gdb_stdout);
1180           singlestep_breakpoints_inserted_p = 0; /*SOFTWARE_SINGLE_STEP_P*/
1181           goto stop_stepping;
1182
1183        /* The following are the only cases in which we keep going;
1184            the above cases end in a continue or goto. */
1185         case TARGET_WAITKIND_FORKED:
1186           stop_signal = TARGET_SIGNAL_TRAP;
1187           pending_follow.kind = w.kind;
1188
1189           /* Ignore fork events reported for the parent; we're only
1190              interested in reacting to forks of the child.  Note that
1191              we expect the child's fork event to be available if we
1192              waited for it now. */
1193           if (inferior_pid == pid)
1194             {
1195               pending_follow.fork_event.saw_parent_fork = 1;
1196               pending_follow.fork_event.parent_pid = pid;
1197               pending_follow.fork_event.child_pid = w.value.related_pid;
1198               continue;
1199             }
1200           else
1201             {
1202               pending_follow.fork_event.saw_child_fork = 1;
1203               pending_follow.fork_event.child_pid = pid;
1204               pending_follow.fork_event.parent_pid = w.value.related_pid;
1205             }
1206
1207           stop_pc = read_pc_pid (pid);
1208           saved_inferior_pid = inferior_pid;
1209           inferior_pid = pid;
1210           stop_bpstat = bpstat_stop_status 
1211             (&stop_pc,
1212 #if DECR_PC_AFTER_BREAK
1213              (prev_pc != stop_pc - DECR_PC_AFTER_BREAK
1214               && CURRENTLY_STEPPING ())
1215 #else /* DECR_PC_AFTER_BREAK zero */
1216              0
1217 #endif /* DECR_PC_AFTER_BREAK zero */
1218              );
1219           random_signal = ! bpstat_explains_signal (stop_bpstat);
1220           inferior_pid = saved_inferior_pid;
1221           goto process_event_stop_test;
1222
1223         /* If this a platform which doesn't allow a debugger to touch a
1224            vfork'd inferior until after it exec's, then we'd best keep
1225            our fingers entirely off the inferior, other than continuing
1226            it.  This has the unfortunate side-effect that catchpoints
1227            of vforks will be ignored.  But since the platform doesn't
1228            allow the inferior be touched at vfork time, there's really
1229            little choice. */
1230         case TARGET_WAITKIND_VFORKED:
1231           stop_signal = TARGET_SIGNAL_TRAP;
1232           pending_follow.kind = w.kind;
1233
1234           /* Is this a vfork of the parent?  If so, then give any
1235              vfork catchpoints a chance to trigger now.  (It's
1236              dangerous to do so if the child canot be touched until
1237              it execs, and the child has not yet exec'd.  We probably
1238              should warn the user to that effect when the catchpoint
1239              triggers...) */
1240           if (pid == inferior_pid)
1241             {
1242               pending_follow.fork_event.saw_parent_fork = 1;
1243               pending_follow.fork_event.parent_pid = pid;
1244               pending_follow.fork_event.child_pid = w.value.related_pid;
1245             }
1246
1247           /* If we've seen the child's vfork event but cannot really touch
1248              the child until it execs, then we must continue the child now.
1249              Else, give any vfork catchpoints a chance to trigger now. */
1250           else
1251             {
1252               pending_follow.fork_event.saw_child_fork = 1;
1253               pending_follow.fork_event.child_pid = pid;
1254               pending_follow.fork_event.parent_pid = w.value.related_pid;
1255               target_post_startup_inferior (pending_follow.fork_event.child_pid);
1256               follow_vfork_when_exec = ! target_can_follow_vfork_prior_to_exec ();
1257               if (follow_vfork_when_exec)
1258                 {
1259                   target_resume (pid, 0, TARGET_SIGNAL_0);
1260                   continue;
1261                 }
1262             }
1263
1264           stop_pc = read_pc ();
1265           stop_bpstat = bpstat_stop_status 
1266             (&stop_pc,
1267 #if DECR_PC_AFTER_BREAK
1268              (prev_pc != stop_pc - DECR_PC_AFTER_BREAK
1269               && CURRENTLY_STEPPING ())
1270 #else /* DECR_PC_AFTER_BREAK zero */
1271              0
1272 #endif /* DECR_PC_AFTER_BREAK zero */
1273              );
1274           random_signal = ! bpstat_explains_signal (stop_bpstat);
1275           goto process_event_stop_test;
1276
1277         case TARGET_WAITKIND_EXECD:
1278           stop_signal = TARGET_SIGNAL_TRAP;
1279
1280           /* Is this a target which reports multiple exec events per actual
1281              call to exec()?  (HP-UX using ptrace does, for example.)  If so,
1282              ignore all but the last one.  Just resume the exec'r, and wait
1283              for the next exec event. */
1284           if (inferior_ignoring_leading_exec_events)
1285             {
1286               inferior_ignoring_leading_exec_events--;
1287               if (pending_follow.kind == TARGET_WAITKIND_VFORKED)
1288                 ENSURE_VFORKING_PARENT_REMAINS_STOPPED(pending_follow.fork_event.parent_pid);
1289               target_resume (pid, 0, TARGET_SIGNAL_0);
1290               continue;
1291             }
1292           inferior_ignoring_leading_exec_events =
1293             target_reported_exec_events_per_exec_call () - 1;
1294
1295           pending_follow.execd_pathname = savestring (w.value.execd_pathname,
1296                                                       strlen (w.value.execd_pathname));
1297
1298           /* Did inferior_pid exec, or did a (possibly not-yet-followed)
1299              child of a vfork exec?
1300
1301              ??rehrauer: This is unabashedly an HP-UX specific thing.  On
1302              HP-UX, events associated with a vforking inferior come in
1303              threes: a vfork event for the child (always first), followed
1304              a vfork event for the parent and an exec event for the child.
1305              The latter two can come in either order.
1306
1307              If we get the parent vfork event first, life's good: We follow
1308              either the parent or child, and then the child's exec event is
1309              a "don't care".
1310
1311              But if we get the child's exec event first, then we delay
1312              responding to it until we handle the parent's vfork.  Because,
1313              otherwise we can't satisfy a "catch vfork". */
1314           if (pending_follow.kind == TARGET_WAITKIND_VFORKED)
1315             {
1316               pending_follow.fork_event.saw_child_exec = 1;
1317
1318               /* On some targets, the child must be resumed before
1319                  the parent vfork event is delivered.  A single-step
1320                  suffices. */
1321               if (RESUME_EXECD_VFORKING_CHILD_TO_GET_PARENT_VFORK())
1322                 target_resume (pid, 1, TARGET_SIGNAL_0);
1323               /* We expect the parent vfork event to be available now. */
1324               continue;
1325             }
1326
1327           /* This causes the eventpoints and symbol table to be reset.  Must
1328              do this now, before trying to determine whether to stop. */
1329           follow_exec (inferior_pid, pending_follow.execd_pathname);
1330           free (pending_follow.execd_pathname);
1331
1332           stop_pc = read_pc_pid (pid);
1333           saved_inferior_pid = inferior_pid;
1334           inferior_pid = pid;
1335           stop_bpstat = bpstat_stop_status 
1336             (&stop_pc,
1337 #if DECR_PC_AFTER_BREAK
1338              (prev_pc != stop_pc - DECR_PC_AFTER_BREAK
1339               && CURRENTLY_STEPPING ())
1340 #else /* DECR_PC_AFTER_BREAK zero */
1341              0
1342 #endif /* DECR_PC_AFTER_BREAK zero */
1343              );
1344           random_signal = ! bpstat_explains_signal (stop_bpstat);
1345           inferior_pid = saved_inferior_pid;
1346           goto process_event_stop_test;
1347
1348         /* These syscall events are returned on HP-UX, as part of its
1349            implementation of page-protection-based "hardware" watchpoints.
1350            HP-UX has unfortunate interactions between page-protections and
1351            some system calls.  Our solution is to disable hardware watches
1352            when a system call is entered, and reenable them when the syscall
1353            completes.  The downside of this is that we may miss the precise
1354            point at which a watched piece of memory is modified.  "Oh well."
1355
1356            Note that we may have multiple threads running, which may each
1357            enter syscalls at roughly the same time.  Since we don't have a
1358            good notion currently of whether a watched piece of memory is
1359            thread-private, we'd best not have any page-protections active
1360            when any thread is in a syscall.  Thus, we only want to reenable
1361            hardware watches when no threads are in a syscall.
1362
1363            Also, be careful not to try to gather much state about a thread
1364            that's in a syscall.  It's frequently a losing proposition. */
1365         case TARGET_WAITKIND_SYSCALL_ENTRY:
1366           number_of_threads_in_syscalls++;
1367           if (number_of_threads_in_syscalls == 1)
1368             {
1369               TARGET_DISABLE_HW_WATCHPOINTS(inferior_pid);
1370             }
1371           resume (0, TARGET_SIGNAL_0);
1372           continue;
1373
1374         /* Before examining the threads further, step this thread to
1375            get it entirely out of the syscall.  (We get notice of the
1376            event when the thread is just on the verge of exiting a
1377            syscall.  Stepping one instruction seems to get it back
1378            into user code.)
1379
1380            Note that although the logical place to reenable h/w watches
1381            is here, we cannot.  We cannot reenable them before stepping
1382            the thread (this causes the next wait on the thread to hang).
1383
1384            Nor can we enable them after stepping until we've done a wait.
1385            Thus, we simply set the flag enable_hw_watchpoints_after_wait
1386            here, which will be serviced immediately after the target
1387            is waited on. */
1388         case TARGET_WAITKIND_SYSCALL_RETURN:
1389           target_resume (pid, 1, TARGET_SIGNAL_0);
1390
1391           if (number_of_threads_in_syscalls > 0)
1392             {
1393               number_of_threads_in_syscalls--;
1394               enable_hw_watchpoints_after_wait =
1395                 (number_of_threads_in_syscalls == 0);
1396             }
1397           continue;
1398
1399         case TARGET_WAITKIND_STOPPED:
1400           stop_signal = w.value.sig;
1401           break;
1402         }
1403
1404       /* We may want to consider not doing a resume here in order to give
1405          the user a chance to play with the new thread.  It might be good
1406          to make that a user-settable option.  */
1407
1408       /* At this point, all threads are stopped (happens automatically in
1409          either the OS or the native code).  Therefore we need to continue
1410          all threads in order to make progress.  */
1411       if (new_thread_event)
1412         {
1413           target_resume (-1, 0, TARGET_SIGNAL_0);
1414           continue;
1415         }
1416
1417       stop_pc = read_pc_pid (pid);
1418
1419       /* See if a thread hit a thread-specific breakpoint that was meant for
1420          another thread.  If so, then step that thread past the breakpoint,
1421          and continue it.  */
1422
1423       if (stop_signal == TARGET_SIGNAL_TRAP)
1424         {
1425           if (SOFTWARE_SINGLE_STEP_P && singlestep_breakpoints_inserted_p)
1426             random_signal = 0;
1427           else
1428             if (breakpoints_inserted
1429                 && breakpoint_here_p (stop_pc - DECR_PC_AFTER_BREAK))
1430               {
1431                 random_signal = 0;
1432                 if (!breakpoint_thread_match (stop_pc - DECR_PC_AFTER_BREAK, 
1433                                               pid))
1434                   {
1435                     int  remove_status;
1436
1437                     /* Saw a breakpoint, but it was hit by the wrong thread. 
1438                        Just continue. */
1439                     write_pc_pid (stop_pc - DECR_PC_AFTER_BREAK, pid);
1440
1441                     remove_status = remove_breakpoints ();
1442                     /* Did we fail to remove breakpoints?  If so, try
1443                        to set the PC past the bp.  (There's at least
1444                        one situation in which we can fail to remove
1445                        the bp's: On HP-UX's that use ttrace, we can't
1446                        change the address space of a vforking child
1447                        process until the child exits (well, okay, not
1448                        then either :-) or execs. */
1449                     if (remove_status != 0)
1450                       {
1451                         write_pc_pid (stop_pc - DECR_PC_AFTER_BREAK + 4, pid);
1452                       }
1453                     else
1454                       { /* Single step */
1455                         target_resume (pid, 1, TARGET_SIGNAL_0); 
1456                         /* FIXME: What if a signal arrives instead of the 
1457                            single-step happening?  */
1458
1459                         if (target_wait_hook)
1460                           target_wait_hook (pid, &w);
1461                         else
1462                           target_wait (pid, &w);
1463                         insert_breakpoints ();
1464                       }
1465
1466                     /* We need to restart all the threads now.  */
1467                     target_resume (-1, 0, TARGET_SIGNAL_0);
1468                     continue;
1469                   }
1470                 else
1471                   {
1472                     /* This breakpoint matches--either it is the right
1473                        thread or it's a generic breakpoint for all threads.
1474                        Remember that we'll need to step just _this_ thread
1475                        on any following user continuation! */
1476                     thread_step_needed = 1;
1477                   }  
1478               }
1479         }
1480       else
1481         random_signal = 1;
1482
1483       /* See if something interesting happened to the non-current thread.  If
1484          so, then switch to that thread, and eventually give control back to
1485          the user.
1486
1487          Note that if there's any kind of pending follow (i.e., of a fork,
1488          vfork or exec), we don't want to do this now.  Rather, we'll let
1489          the next resume handle it. */
1490       if ((pid != inferior_pid) &&
1491           (pending_follow.kind == TARGET_WAITKIND_SPURIOUS))
1492         {
1493           int printed = 0;
1494
1495           /* If it's a random signal for a non-current thread, notify user
1496              if he's expressed an interest. */
1497           if (random_signal
1498               && signal_print[stop_signal])
1499             {
1500 /* ??rehrauer: I don't understand the rationale for this code.  If the
1501    inferior will stop as a result of this signal, then the act of handling
1502    the stop ought to print a message that's couches the stoppage in user
1503    terms, e.g., "Stopped for breakpoint/watchpoint".  If the inferior
1504    won't stop as a result of the signal -- i.e., if the signal is merely
1505    a side-effect of something GDB's doing "under the covers" for the
1506    user, such as stepping threads over a breakpoint they shouldn't stop
1507    for -- then the message seems to be a serious annoyance at best.
1508
1509    For now, remove the message altogether. */
1510 #if 0
1511               printed = 1;
1512               target_terminal_ours_for_output ();
1513               printf_filtered ("\nProgram received signal %s, %s.\n",
1514                                target_signal_to_name (stop_signal),
1515                                target_signal_to_string (stop_signal));
1516               gdb_flush (gdb_stdout);
1517 #endif
1518             }
1519
1520           /* If it's not SIGTRAP and not a signal we want to stop for, then
1521              continue the thread. */
1522
1523           if (stop_signal != TARGET_SIGNAL_TRAP
1524               && !signal_stop[stop_signal])
1525             {
1526               if (printed)
1527                 target_terminal_inferior ();
1528
1529               /* Clear the signal if it should not be passed.  */
1530               if (signal_program[stop_signal] == 0)
1531                 stop_signal = TARGET_SIGNAL_0;
1532
1533               target_resume (pid, 0, stop_signal);
1534               continue;
1535             }
1536
1537           /* It's a SIGTRAP or a signal we're interested in.  Switch threads,
1538              and fall into the rest of wait_for_inferior().  */
1539
1540           /* Save infrun state for the old thread.  */
1541           save_infrun_state (inferior_pid, prev_pc,
1542                              prev_func_start, prev_func_name,
1543                              trap_expected, step_resume_breakpoint,
1544                              through_sigtramp_breakpoint,
1545                              step_range_start, step_range_end,
1546                              step_frame_address, handling_longjmp,
1547                              another_trap,
1548                              stepping_through_solib_after_catch,
1549                              stepping_through_solib_catchpoints,
1550                              stepping_through_sigtramp);
1551
1552 #ifdef HPUXHPPA
1553           switched_from_inferior_pid = inferior_pid;
1554 #endif
1555
1556           inferior_pid = pid;
1557
1558           /* Load infrun state for the new thread.  */
1559           load_infrun_state (inferior_pid, &prev_pc,
1560                              &prev_func_start, &prev_func_name,
1561                              &trap_expected, &step_resume_breakpoint,
1562                              &through_sigtramp_breakpoint,
1563                              &step_range_start, &step_range_end,
1564                              &step_frame_address, &handling_longjmp,
1565                              &another_trap,
1566                              &stepping_through_solib_after_catch,
1567                              &stepping_through_solib_catchpoints,
1568                              &stepping_through_sigtramp);
1569
1570           if (context_hook)
1571             context_hook (pid_to_thread_id (pid));
1572           
1573           printf_filtered ("[Switching to %s]\n", target_pid_to_str (pid));
1574           flush_cached_frames ();
1575         }
1576
1577       if (SOFTWARE_SINGLE_STEP_P && singlestep_breakpoints_inserted_p)
1578         {
1579           /* Pull the single step breakpoints out of the target. */
1580           SOFTWARE_SINGLE_STEP (0, 0);
1581           singlestep_breakpoints_inserted_p = 0;
1582         }
1583       
1584       /* If PC is pointing at a nullified instruction, then step beyond
1585          it so that the user won't be confused when GDB appears to be ready
1586          to execute it. */
1587
1588 #if 0 /* XXX DEBUG */
1589       printf ("infrun.c:1607: pc = 0x%x\n", read_pc ());
1590 #endif
1591       /*      if (INSTRUCTION_NULLIFIED && CURRENTLY_STEPPING ()) */
1592       if (INSTRUCTION_NULLIFIED)
1593         {
1594           struct target_waitstatus tmpstatus;
1595 #if 0
1596           all_registers_info ((char *)0, 0);
1597 #endif
1598           registers_changed ();
1599           target_resume (pid, 1, TARGET_SIGNAL_0);
1600
1601           /* We may have received a signal that we want to pass to
1602              the inferior; therefore, we must not clobber the waitstatus
1603              in W.  So we call wait ourselves, then continue the loop
1604              at the "have_waited" label.  */
1605           if (target_wait_hook)
1606             target_wait_hook (pid, &tmpstatus);
1607           else
1608             target_wait (pid, &tmpstatus);
1609
1610           goto have_waited;
1611         }
1612
1613 #ifdef HAVE_STEPPABLE_WATCHPOINT
1614       /* It may not be necessary to disable the watchpoint to stop over
1615          it.  For example, the PA can (with some kernel cooperation) 
1616          single step over a watchpoint without disabling the watchpoint.  */
1617       if (STOPPED_BY_WATCHPOINT (w))
1618         {
1619           resume (1, 0);
1620           continue;
1621         }
1622 #endif
1623
1624 #ifdef HAVE_NONSTEPPABLE_WATCHPOINT
1625       /* It is far more common to need to disable a watchpoint
1626          to step the inferior over it.  FIXME.  What else might
1627          a debug register or page protection watchpoint scheme need
1628          here?  */
1629       if (STOPPED_BY_WATCHPOINT (w))
1630         {
1631 /* At this point, we are stopped at an instruction which has attempted to write
1632    to a piece of memory under control of a watchpoint.  The instruction hasn't
1633    actually executed yet.  If we were to evaluate the watchpoint expression
1634    now, we would get the old value, and therefore no change would seem to have
1635    occurred.
1636
1637    In order to make watchpoints work `right', we really need to complete the
1638    memory write, and then evaluate the watchpoint expression.  The following
1639    code does that by removing the watchpoint (actually, all watchpoints and
1640    breakpoints), single-stepping the target, re-inserting watchpoints, and then
1641    falling through to let normal single-step processing handle proceed.  Since
1642    this includes evaluating watchpoints, things will come to a stop in the
1643    correct manner.  */
1644
1645           write_pc (stop_pc - DECR_PC_AFTER_BREAK);
1646
1647           remove_breakpoints ();
1648           registers_changed();
1649           target_resume (pid, 1, TARGET_SIGNAL_0); /* Single step */
1650
1651           if (target_wait_hook)
1652             target_wait_hook (pid, &w);
1653           else
1654             target_wait (pid, &w);
1655           insert_breakpoints ();
1656
1657           /* FIXME-maybe: is this cleaner than setting a flag?  Does it
1658              handle things like signals arriving and other things happening
1659              in combination correctly?  */
1660           stepped_after_stopped_by_watchpoint = 1;
1661           goto have_waited;
1662         }
1663 #endif
1664
1665 #ifdef HAVE_CONTINUABLE_WATCHPOINT
1666       /* It may be possible to simply continue after a watchpoint.  */
1667       STOPPED_BY_WATCHPOINT (w);
1668 #endif
1669
1670       stop_func_start = 0;
1671       stop_func_end = 0;
1672       stop_func_name = 0;
1673       /* Don't care about return value; stop_func_start and stop_func_name
1674          will both be 0 if it doesn't work.  */
1675       find_pc_partial_function (stop_pc, &stop_func_name, &stop_func_start,
1676                                 &stop_func_end);
1677       stop_func_start += FUNCTION_START_OFFSET;
1678       another_trap = 0;
1679       bpstat_clear (&stop_bpstat);
1680       stop_step = 0;
1681       stop_stack_dummy = 0;
1682       stop_print_frame = 1;
1683       random_signal = 0;
1684       stopped_by_random_signal = 0;
1685       breakpoints_failed = 0;
1686       
1687       /* Look at the cause of the stop, and decide what to do.
1688          The alternatives are:
1689          1) break; to really stop and return to the debugger,
1690          2) drop through to start up again
1691          (set another_trap to 1 to single step once)
1692          3) set random_signal to 1, and the decision between 1 and 2
1693          will be made according to the signal handling tables.  */
1694       
1695       /* First, distinguish signals caused by the debugger from signals
1696          that have to do with the program's own actions.
1697          Note that breakpoint insns may cause SIGTRAP or SIGILL
1698          or SIGEMT, depending on the operating system version.
1699          Here we detect when a SIGILL or SIGEMT is really a breakpoint
1700          and change it to SIGTRAP.  */
1701       
1702       if (stop_signal == TARGET_SIGNAL_TRAP
1703           || (breakpoints_inserted &&
1704               (stop_signal == TARGET_SIGNAL_ILL
1705                || stop_signal == TARGET_SIGNAL_EMT
1706             ))
1707           || stop_soon_quietly)
1708         {
1709           if (stop_signal == TARGET_SIGNAL_TRAP && stop_after_trap)
1710             {
1711               stop_print_frame = 0;
1712               break;
1713             }
1714           if (stop_soon_quietly)
1715             break;
1716
1717           /* Don't even think about breakpoints
1718              if just proceeded over a breakpoint.
1719
1720              However, if we are trying to proceed over a breakpoint
1721              and end up in sigtramp, then through_sigtramp_breakpoint
1722              will be set and we should check whether we've hit the
1723              step breakpoint.  */
1724           if (stop_signal == TARGET_SIGNAL_TRAP && trap_expected
1725               && through_sigtramp_breakpoint == NULL)
1726             bpstat_clear (&stop_bpstat);
1727           else
1728             {
1729               /* See if there is a breakpoint at the current PC.  */
1730               stop_bpstat = bpstat_stop_status
1731                 (&stop_pc,
1732                  (DECR_PC_AFTER_BREAK ?
1733                  /* Notice the case of stepping through a jump
1734                     that lands just after a breakpoint.
1735                     Don't confuse that with hitting the breakpoint.
1736                     What we check for is that 1) stepping is going on
1737                     and 2) the pc before the last insn does not match
1738                     the address of the breakpoint before the current pc
1739                     and 3) we didn't hit a breakpoint in a signal handler
1740                     without an intervening stop in sigtramp, which is
1741                     detected by a new stack pointer value below
1742                     any usual function calling stack adjustments.  */
1743                  (CURRENTLY_STEPPING ()
1744                   && prev_pc != stop_pc - DECR_PC_AFTER_BREAK
1745                   && !(step_range_end
1746                        && INNER_THAN (read_sp (), (step_sp - 16)))) :
1747                  0)
1748                  );
1749               /* Following in case break condition called a
1750                  function.  */
1751               stop_print_frame = 1;
1752             }
1753
1754           if (stop_signal == TARGET_SIGNAL_TRAP)
1755             random_signal
1756               = !(bpstat_explains_signal (stop_bpstat)
1757                   || trap_expected
1758 #ifndef CALL_DUMMY_BREAKPOINT_OFFSET
1759                   || PC_IN_CALL_DUMMY (stop_pc, read_sp (),
1760                                        FRAME_FP (get_current_frame ()))
1761 #endif /* No CALL_DUMMY_BREAKPOINT_OFFSET.  */
1762                   || (step_range_end && step_resume_breakpoint == NULL));
1763
1764           else
1765             {
1766               random_signal
1767                 = !(bpstat_explains_signal (stop_bpstat)
1768                     /* End of a stack dummy.  Some systems (e.g. Sony
1769                        news) give another signal besides SIGTRAP,
1770                        so check here as well as above.  */
1771 #ifndef CALL_DUMMY_BREAKPOINT_OFFSET
1772                     || PC_IN_CALL_DUMMY (stop_pc, read_sp (),
1773                                          FRAME_FP (get_current_frame ()))
1774 #endif /* No CALL_DUMMY_BREAKPOINT_OFFSET.  */
1775                     );
1776               if (!random_signal)
1777                 stop_signal = TARGET_SIGNAL_TRAP;
1778             }
1779         }
1780
1781       /* When we reach this point, we've pretty much decided
1782          that the reason for stopping must've been a random
1783          (unexpected) signal. */
1784
1785       else
1786         random_signal = 1;
1787      /* If a fork, vfork or exec event was seen, then there are two
1788          possible responses we can make:
1789
1790          1. If a catchpoint triggers for the event (random_signal == 0),
1791             then we must stop now and issue a prompt.  We will resume
1792             the inferior when the user tells us to.
1793          2. If no catchpoint triggers for the event (random_signal == 1),
1794             then we must resume the inferior now and keep checking.
1795
1796          In either case, we must take appropriate steps to "follow" the
1797          the fork/vfork/exec when the inferior is resumed.  For example,
1798          if follow-fork-mode is "child", then we must detach from the
1799          parent inferior and follow the new child inferior.
1800
1801          In either case, setting pending_follow causes the next resume()
1802          to take the appropriate following action. */
1803 process_event_stop_test:
1804       if (w.kind == TARGET_WAITKIND_FORKED)
1805         {
1806           if (random_signal)  /* I.e., no catchpoint triggered for this. */
1807             {
1808               trap_expected = 1;
1809               stop_signal = TARGET_SIGNAL_0;
1810               goto keep_going;
1811             }
1812         }
1813       else if (w.kind == TARGET_WAITKIND_VFORKED)
1814         {
1815           if (random_signal)  /* I.e., no catchpoint triggered for this. */
1816             {
1817               stop_signal = TARGET_SIGNAL_0;
1818               goto keep_going;
1819             }
1820         }
1821       else if (w.kind == TARGET_WAITKIND_EXECD)
1822         {
1823           pending_follow.kind = w.kind;
1824           if (random_signal)  /* I.e., no catchpoint triggered for this. */
1825             {
1826               trap_expected = 1;
1827               stop_signal = TARGET_SIGNAL_0;
1828               goto keep_going;
1829             }
1830         }
1831
1832       /* For the program's own signals, act according to
1833          the signal handling tables.  */
1834
1835       if (random_signal)
1836         {
1837           /* Signal not for debugging purposes.  */
1838           int printed = 0;
1839           
1840           stopped_by_random_signal = 1;
1841           
1842           if (signal_print[stop_signal])
1843             {
1844               printed = 1;
1845               target_terminal_ours_for_output ();
1846               annotate_signal ();
1847               printf_filtered ("\nProgram received signal ");
1848               annotate_signal_name ();
1849               printf_filtered ("%s", target_signal_to_name (stop_signal));
1850               annotate_signal_name_end ();
1851               printf_filtered (", ");
1852               annotate_signal_string ();
1853               printf_filtered ("%s", target_signal_to_string (stop_signal));
1854               annotate_signal_string_end ();
1855               printf_filtered (".\n");
1856               gdb_flush (gdb_stdout);
1857             }
1858           if (signal_stop[stop_signal])
1859             break;
1860           /* If not going to stop, give terminal back
1861              if we took it away.  */
1862           else if (printed)
1863             target_terminal_inferior ();
1864
1865           /* Clear the signal if it should not be passed.  */
1866           if (signal_program[stop_signal] == 0)
1867             stop_signal = TARGET_SIGNAL_0;
1868
1869           /* If we're in the middle of a "next" command, let the code for
1870              stepping over a function handle this. pai/1997-09-10
1871
1872              A previous comment here suggested it was possible to change
1873              this to jump to keep_going in all cases. */
1874           
1875           if (step_over_calls > 0)
1876             goto step_over_function;
1877           else
1878             goto check_sigtramp2;
1879         }
1880
1881       /* Handle cases caused by hitting a breakpoint.  */
1882       {
1883         CORE_ADDR jmp_buf_pc;
1884         struct bpstat_what what;
1885
1886         what = bpstat_what (stop_bpstat);
1887
1888         if (what.call_dummy)
1889           {
1890             stop_stack_dummy = 1;
1891 #ifdef HP_OS_BUG
1892             trap_expected_after_continue = 1;
1893 #endif
1894           }
1895
1896         switch (what.main_action)
1897           {
1898           case BPSTAT_WHAT_SET_LONGJMP_RESUME:
1899             /* If we hit the breakpoint at longjmp, disable it for the
1900                duration of this command.  Then, install a temporary
1901                breakpoint at the target of the jmp_buf. */
1902             disable_longjmp_breakpoint();
1903             remove_breakpoints ();
1904             breakpoints_inserted = 0;
1905             if (!GET_LONGJMP_TARGET(&jmp_buf_pc)) goto keep_going;
1906
1907             /* Need to blow away step-resume breakpoint, as it
1908                interferes with us */
1909             if (step_resume_breakpoint != NULL)
1910               {
1911                 delete_breakpoint (step_resume_breakpoint);
1912                 step_resume_breakpoint = NULL;
1913               }
1914             /* Not sure whether we need to blow this away too, but probably
1915                it is like the step-resume breakpoint.  */
1916             if (through_sigtramp_breakpoint != NULL)
1917               {
1918                 delete_breakpoint (through_sigtramp_breakpoint);
1919                 through_sigtramp_breakpoint = NULL;
1920               }
1921
1922 #if 0
1923             /* FIXME - Need to implement nested temporary breakpoints */
1924             if (step_over_calls > 0)
1925               set_longjmp_resume_breakpoint(jmp_buf_pc,
1926                                             get_current_frame());
1927             else
1928 #endif                          /* 0 */
1929               set_longjmp_resume_breakpoint(jmp_buf_pc, NULL);
1930             handling_longjmp = 1; /* FIXME */
1931             goto keep_going;
1932
1933           case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
1934           case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME_SINGLE:
1935             remove_breakpoints ();
1936             breakpoints_inserted = 0;
1937 #if 0
1938             /* FIXME - Need to implement nested temporary breakpoints */
1939             if (step_over_calls
1940                 && (INNER_THAN (FRAME_FP (get_current_frame ()),
1941                                 step_frame_address)))
1942               {
1943                 another_trap = 1;
1944                 goto keep_going;
1945               }
1946 #endif                          /* 0 */
1947             disable_longjmp_breakpoint();
1948             handling_longjmp = 0; /* FIXME */
1949             if (what.main_action == BPSTAT_WHAT_CLEAR_LONGJMP_RESUME)
1950               break;
1951             /* else fallthrough */
1952
1953           case BPSTAT_WHAT_SINGLE:
1954             if (breakpoints_inserted)
1955               {
1956                 thread_step_needed = 1;
1957                 remove_breakpoints ();
1958               }
1959             breakpoints_inserted = 0;
1960             another_trap = 1;
1961             /* Still need to check other stuff, at least the case
1962                where we are stepping and step out of the right range.  */
1963             break;
1964
1965           case BPSTAT_WHAT_STOP_NOISY:
1966             stop_print_frame = 1;
1967
1968             /* We are about to nuke the step_resume_breakpoint and
1969                through_sigtramp_breakpoint via the cleanup chain, so
1970                no need to worry about it here.  */
1971
1972             goto stop_stepping;
1973
1974           case BPSTAT_WHAT_STOP_SILENT:
1975             stop_print_frame = 0;
1976
1977             /* We are about to nuke the step_resume_breakpoint and
1978                through_sigtramp_breakpoint via the cleanup chain, so
1979                no need to worry about it here.  */
1980
1981             goto stop_stepping;
1982
1983           case BPSTAT_WHAT_STEP_RESUME:
1984             /* This proably demands a more elegant solution, but, yeah
1985                right...
1986
1987                This function's use of the simple variable
1988                step_resume_breakpoint doesn't seem to accomodate
1989                simultaneously active step-resume bp's, although the
1990                breakpoint list certainly can.
1991
1992                If we reach here and step_resume_breakpoint is already
1993                NULL, then apparently we have multiple active
1994                step-resume bp's.  We'll just delete the breakpoint we
1995                stopped at, and carry on.  */
1996             if (step_resume_breakpoint == NULL)
1997               {
1998                 step_resume_breakpoint =
1999                   bpstat_find_step_resume_breakpoint (stop_bpstat);
2000               }
2001             delete_breakpoint (step_resume_breakpoint);
2002             step_resume_breakpoint = NULL;
2003             break;
2004
2005           case BPSTAT_WHAT_THROUGH_SIGTRAMP:
2006             if (through_sigtramp_breakpoint)
2007               delete_breakpoint (through_sigtramp_breakpoint);
2008             through_sigtramp_breakpoint = NULL;
2009
2010             /* If were waiting for a trap, hitting the step_resume_break
2011                doesn't count as getting it.  */
2012             if (trap_expected)
2013               another_trap = 1;
2014             break;
2015
2016           case BPSTAT_WHAT_CHECK_SHLIBS:
2017           case BPSTAT_WHAT_CHECK_SHLIBS_RESUME_FROM_HOOK:
2018 #ifdef SOLIB_ADD
2019             {
2020               extern int auto_solib_add;
2021
2022               /* Remove breakpoints, we eventually want to step over the
2023                  shlib event breakpoint, and SOLIB_ADD might adjust
2024                  breakpoint addresses via breakpoint_re_set.  */
2025               if (breakpoints_inserted)
2026                 remove_breakpoints ();
2027               breakpoints_inserted = 0;
2028
2029               /* Check for any newly added shared libraries if we're
2030                  supposed to be adding them automatically.  */
2031               if (auto_solib_add)
2032                 {
2033                   /* Switch terminal for any messages produced by
2034                      breakpoint_re_set.  */
2035                   target_terminal_ours_for_output ();
2036                   SOLIB_ADD (NULL, 0, NULL);
2037                   target_terminal_inferior ();
2038                 }
2039
2040               /* Try to reenable shared library breakpoints, additional
2041                  code segments in shared libraries might be mapped in now. */
2042               re_enable_breakpoints_in_shlibs ();
2043
2044               /* If requested, stop when the dynamic linker notifies
2045                  gdb of events.  This allows the user to get control
2046                  and place breakpoints in initializer routines for
2047                  dynamically loaded objects (among other things).  */
2048               if (stop_on_solib_events)
2049                 {
2050                   stop_print_frame = 0;
2051                   goto stop_stepping;
2052                 }
2053
2054               /* If we stopped due to an explicit catchpoint, then the
2055                  (see above) call to SOLIB_ADD pulled in any symbols
2056                  from a newly-loaded library, if appropriate.
2057
2058                  We do want the inferior to stop, but not where it is
2059                  now, which is in the dynamic linker callback.  Rather,
2060                  we would like it stop in the user's program, just after
2061                  the call that caused this catchpoint to trigger.  That
2062                  gives the user a more useful vantage from which to
2063                  examine their program's state. */
2064               else if (what.main_action == BPSTAT_WHAT_CHECK_SHLIBS_RESUME_FROM_HOOK)
2065                 {
2066                   /* ??rehrauer: If I could figure out how to get the
2067                      right return PC from here, we could just set a temp
2068                      breakpoint and resume.  I'm not sure we can without
2069                      cracking open the dld's shared libraries and sniffing
2070                      their unwind tables and text/data ranges, and that's
2071                      not a terribly portable notion.
2072
2073                      Until that time, we must step the inferior out of the
2074                      dld callback, and also out of the dld itself (and any
2075                      code or stubs in libdld.sl, such as "shl_load" and
2076                      friends) until we reach non-dld code.  At that point,
2077                      we can stop stepping. */
2078                   bpstat_get_triggered_catchpoints (stop_bpstat,
2079                                                     &stepping_through_solib_catchpoints);
2080                   stepping_through_solib_after_catch = 1;
2081
2082                   /* Be sure to lift all breakpoints, so the inferior does
2083                      actually step past this point... */
2084                   another_trap = 1;
2085                   break;
2086                 }
2087               else
2088                 {
2089                   /* We want to step over this breakpoint, then keep going.  */
2090                   another_trap = 1;
2091                   break;
2092                 }
2093             }
2094 #endif
2095           break;
2096
2097           case BPSTAT_WHAT_LAST:
2098             /* Not a real code, but listed here to shut up gcc -Wall.  */
2099
2100           case BPSTAT_WHAT_KEEP_CHECKING:
2101             break;
2102           }
2103       }
2104
2105       /* We come here if we hit a breakpoint but should not
2106          stop for it.  Possibly we also were stepping
2107          and should stop for that.  So fall through and
2108          test for stepping.  But, if not stepping,
2109          do not stop.  */
2110
2111       /* Are we stepping to get the inferior out of the dynamic
2112          linker's hook (and possibly the dld itself) after catching
2113          a shlib event? */
2114       if (stepping_through_solib_after_catch)
2115         {
2116 #if defined(SOLIB_ADD)
2117           /* Have we reached our destination?  If not, keep going. */
2118           if (SOLIB_IN_DYNAMIC_LINKER (pid, stop_pc))
2119             {
2120               another_trap = 1;
2121               goto keep_going;
2122             }
2123 #endif
2124           /* Else, stop and report the catchpoint(s) whose triggering
2125              caused us to begin stepping. */
2126           stepping_through_solib_after_catch = 0;
2127           bpstat_clear (&stop_bpstat);
2128           stop_bpstat = bpstat_copy (stepping_through_solib_catchpoints);
2129           bpstat_clear (&stepping_through_solib_catchpoints);
2130           stop_print_frame = 1;
2131           goto stop_stepping;
2132         }
2133
2134 #ifndef CALL_DUMMY_BREAKPOINT_OFFSET
2135       /* This is the old way of detecting the end of the stack dummy.
2136          An architecture which defines CALL_DUMMY_BREAKPOINT_OFFSET gets
2137          handled above.  As soon as we can test it on all of them, all
2138          architectures should define it.  */
2139
2140       /* If this is the breakpoint at the end of a stack dummy,
2141          just stop silently, unless the user was doing an si/ni, in which
2142          case she'd better know what she's doing.  */
2143
2144       if (CALL_DUMMY_HAS_COMPLETED (stop_pc, read_sp (), 
2145                                     FRAME_FP (get_current_frame ()))
2146           && !step_range_end)
2147         {
2148           stop_print_frame = 0;
2149           stop_stack_dummy = 1;
2150 #ifdef HP_OS_BUG
2151           trap_expected_after_continue = 1;
2152 #endif
2153           break;
2154         }
2155 #endif /* No CALL_DUMMY_BREAKPOINT_OFFSET.  */
2156
2157       if (step_resume_breakpoint)
2158         /* Having a step-resume breakpoint overrides anything
2159            else having to do with stepping commands until
2160            that breakpoint is reached.  */
2161         /* I'm not sure whether this needs to be check_sigtramp2 or
2162            whether it could/should be keep_going.  */
2163         goto check_sigtramp2;
2164
2165       if (step_range_end == 0)
2166         /* Likewise if we aren't even stepping.  */
2167         /* I'm not sure whether this needs to be check_sigtramp2 or
2168            whether it could/should be keep_going.  */
2169         goto check_sigtramp2;
2170
2171       /* If stepping through a line, keep going if still within it.
2172
2173          Note that step_range_end is the address of the first instruction
2174          beyond the step range, and NOT the address of the last instruction
2175          within it! */
2176       if (stop_pc >= step_range_start
2177           && stop_pc < step_range_end
2178 #if 0
2179 /* I haven't a clue what might trigger this clause, and it seems wrong
2180    anyway, so I've disabled it until someone complains.  -Stu 10/24/95 */
2181
2182           /* The step range might include the start of the
2183              function, so if we are at the start of the
2184              step range and either the stack or frame pointers
2185              just changed, we've stepped outside */
2186           && !(stop_pc == step_range_start
2187                && FRAME_FP (get_current_frame ())
2188                && (INNER_THAN (read_sp (), step_sp)
2189                    || FRAME_FP (get_current_frame ()) != step_frame_address))
2190 #endif
2191 )
2192         {
2193           /* We might be doing a BPSTAT_WHAT_SINGLE and getting a signal.
2194              So definately need to check for sigtramp here.  */
2195           goto check_sigtramp2;
2196         }
2197
2198       /* We stepped out of the stepping range.  */
2199
2200       /* If we are stepping at the source level and entered the runtime
2201          loader dynamic symbol resolution code, we keep on single stepping
2202          until we exit the run time loader code and reach the callee's
2203          address.  */
2204       if (step_over_calls < 0 && IN_SOLIB_DYNSYM_RESOLVE_CODE (stop_pc))
2205         goto keep_going;
2206
2207       /* We can't update step_sp every time through the loop, because
2208          reading the stack pointer would slow down stepping too much.
2209          But we can update it every time we leave the step range.  */
2210       update_step_sp = 1;
2211
2212       /* Did we just take a signal?  */
2213       if (IN_SIGTRAMP (stop_pc, stop_func_name)
2214           && !IN_SIGTRAMP (prev_pc, prev_func_name)
2215           && INNER_THAN (read_sp (), step_sp))
2216         {
2217           /* We've just taken a signal; go until we are back to
2218              the point where we took it and one more.  */
2219
2220           /* Note: The test above succeeds not only when we stepped
2221              into a signal handler, but also when we step past the last
2222              statement of a signal handler and end up in the return stub
2223              of the signal handler trampoline.  To distinguish between
2224              these two cases, check that the frame is INNER_THAN the
2225              previous one below. pai/1997-09-11 */ 
2226
2227
2228           {
2229             CORE_ADDR current_frame = FRAME_FP (get_current_frame());
2230
2231             if (INNER_THAN (current_frame, step_frame_address))
2232               {
2233                 /* We have just taken a signal; go until we are back to
2234                    the point where we took it and one more.  */
2235
2236                 /* This code is needed at least in the following case:
2237                    The user types "next" and then a signal arrives (before
2238                    the "next" is done).  */
2239
2240                 /* Note that if we are stopped at a breakpoint, then we need
2241                    the step_resume breakpoint to override any breakpoints at
2242                    the same location, so that we will still step over the
2243                    breakpoint even though the signal happened.  */
2244                 struct symtab_and_line sr_sal;
2245
2246                 INIT_SAL(&sr_sal);
2247                 sr_sal.symtab = NULL;
2248                 sr_sal.line = 0;
2249                 sr_sal.pc = prev_pc;
2250                 /* We could probably be setting the frame to
2251                    step_frame_address; I don't think anyone thought to
2252                    try it.  */
2253                 step_resume_breakpoint =
2254                   set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
2255                 if (breakpoints_inserted)
2256                   insert_breakpoints ();
2257               }
2258             else
2259               {
2260                 /* We just stepped out of a signal handler and into
2261                    its calling trampoline.
2262
2263                    Normally, we'd jump to step_over_function from
2264                    here, but for some reason GDB can't unwind the
2265                    stack correctly to find the real PC for the point
2266                    user code where the signal trampoline will return
2267                    -- FRAME_SAVED_PC fails, at least on HP-UX 10.20.
2268                    But signal trampolines are pretty small stubs of
2269                    code, anyway, so it's OK instead to just
2270                    single-step out.  Note: assuming such trampolines
2271                    don't exhibit recursion on any platform... */
2272                 find_pc_partial_function (stop_pc, &stop_func_name, 
2273                                           &stop_func_start,
2274                                           &stop_func_end);
2275                 /* Readjust stepping range */ 
2276                 step_range_start = stop_func_start;
2277                 step_range_end = stop_func_end;
2278                 stepping_through_sigtramp = 1;
2279               }
2280           }
2281
2282
2283           /* If this is stepi or nexti, make sure that the stepping range
2284              gets us past that instruction.  */
2285           if (step_range_end == 1)
2286             /* FIXME: Does this run afoul of the code below which, if
2287                we step into the middle of a line, resets the stepping
2288                range?  */
2289             step_range_end = (step_range_start = prev_pc) + 1;
2290
2291           remove_breakpoints_on_following_step = 1;
2292           goto keep_going;
2293         }
2294
2295 #if 0
2296       /* I disabled this test because it was too complicated and slow.  
2297          The SKIP_PROLOGUE was especially slow, because it caused 
2298          unnecessary prologue examination on various architectures.  
2299          The code in the #else clause has been tested on the Sparc, 
2300          Mips, PA, and Power architectures, so it's pretty likely to 
2301          be correct.  -Stu 10/24/95 */
2302
2303       /* See if we left the step range due to a subroutine call that
2304          we should proceed to the end of.  */
2305
2306       if (stop_func_start)
2307         {
2308           struct symtab *s;
2309
2310           /* Do this after the IN_SIGTRAMP check; it might give
2311              an error.  */
2312           prologue_pc = stop_func_start;
2313
2314           /* Don't skip the prologue if this is assembly source */
2315           s = find_pc_symtab (stop_pc);
2316           if (s && s->language != language_asm)
2317             SKIP_PROLOGUE (prologue_pc);
2318         }
2319
2320       if (!(INNER_THAN (step_sp, read_sp ()))   /* don't mistake (sig)return 
2321                                                    as a call */
2322           && (/* Might be a non-recursive call.  If the symbols are missing
2323                  enough that stop_func_start == prev_func_start even though
2324                  they are really two functions, we will treat some calls as
2325                  jumps.  */
2326               stop_func_start != prev_func_start
2327
2328               /* Might be a recursive call if either we have a prologue
2329                  or the call instruction itself saves the PC on the stack.  */
2330               || prologue_pc != stop_func_start
2331               || read_sp () != step_sp)
2332           && (/* PC is completely out of bounds of any known objfiles.  Treat
2333                  like a subroutine call. */
2334               ! stop_func_start
2335
2336               /* If we do a call, we will be at the start of a function...  */
2337               || stop_pc == stop_func_start
2338
2339               /* ...except on the Alpha with -O (and also Irix 5 and
2340                  perhaps others), in which we might call the address
2341                  after the load of gp.  Since prologues don't contain
2342                  calls, we can't return to within one, and we don't
2343                  jump back into them, so this check is OK.  */
2344
2345               || stop_pc < prologue_pc
2346
2347               /* ...and if it is a leaf function, the prologue might
2348                  consist of gp loading only, so the call transfers to
2349                  the first instruction after the prologue.  */
2350               || (stop_pc == prologue_pc
2351
2352                   /* Distinguish this from the case where we jump back
2353                      to the first instruction after the prologue,
2354                      within a function.  */
2355                    && stop_func_start != prev_func_start)
2356
2357               /* If we end up in certain places, it means we did a subroutine
2358                  call.  I'm not completely sure this is necessary now that we
2359                  have the above checks with stop_func_start (and now that
2360                  find_pc_partial_function is pickier).  */
2361               || IN_SOLIB_CALL_TRAMPOLINE (stop_pc, stop_func_name)
2362
2363               /* If none of the above apply, it is a jump within a function,
2364                  or a return from a subroutine.  The other case is longjmp,
2365                  which can no longer happen here as long as the
2366                  handling_longjmp stuff is working.  */
2367               ))
2368 #else
2369         /* This test is a much more streamlined, (but hopefully correct)
2370            replacement for the code above.  It's been tested on the Sparc,
2371            Mips, PA, and Power architectures with good results.  */
2372
2373         if (stop_pc == stop_func_start /* Quick test */
2374             || (in_prologue (stop_pc, stop_func_start) &&
2375                 ! IN_SOLIB_RETURN_TRAMPOLINE (stop_pc, stop_func_name))
2376             || IN_SOLIB_CALL_TRAMPOLINE (stop_pc, stop_func_name)
2377             || stop_func_name == 0)
2378 #endif
2379
2380         {
2381           /* It's a subroutine call.  */
2382
2383           if (step_over_calls == 0)
2384             {
2385               /* I presume that step_over_calls is only 0 when we're
2386                  supposed to be stepping at the assembly language level
2387                  ("stepi").  Just stop.  */
2388               stop_step = 1;
2389               break;
2390             }
2391
2392           if (step_over_calls > 0 || IGNORE_HELPER_CALL (stop_pc))
2393             /* We're doing a "next".  */
2394             goto step_over_function;
2395
2396           /* If we are in a function call trampoline (a stub between
2397              the calling routine and the real function), locate the real
2398              function.  That's what tells us (a) whether we want to step
2399              into it at all, and (b) what prologue we want to run to
2400              the end of, if we do step into it.  */
2401           tmp = SKIP_TRAMPOLINE_CODE (stop_pc);
2402           if (tmp != 0)
2403             stop_func_start = tmp;
2404           else
2405             {
2406               tmp = DYNAMIC_TRAMPOLINE_NEXTPC (stop_pc);
2407               if (tmp)
2408                 {
2409                   struct symtab_and_line xxx;
2410                   /* Why isn't this s_a_l called "sr_sal", like all of the
2411                      other s_a_l's where this code is duplicated?  */
2412                   INIT_SAL (&xxx);      /* initialize to zeroes */
2413                   xxx.pc      = tmp;
2414                   xxx.section = find_pc_overlay (xxx.pc);
2415                   step_resume_breakpoint = 
2416                     set_momentary_breakpoint (xxx, NULL, bp_step_resume);
2417                   insert_breakpoints ();
2418                   goto keep_going;
2419                 }
2420             }
2421
2422           /* If we have line number information for the function we
2423              are thinking of stepping into, step into it.
2424
2425              If there are several symtabs at that PC (e.g. with include
2426              files), just want to know whether *any* of them have line
2427              numbers.  find_pc_line handles this.  */
2428           {
2429             struct symtab_and_line tmp_sal;
2430
2431             tmp_sal = find_pc_line (stop_func_start, 0);
2432             if (tmp_sal.line != 0)
2433               goto step_into_function;
2434           }
2435
2436 step_over_function:
2437           /* A subroutine call has happened.  */
2438           {
2439             /* Set a special breakpoint after the return */
2440             struct symtab_and_line sr_sal;
2441
2442             INIT_SAL(&sr_sal);
2443             sr_sal.symtab = NULL;
2444             sr_sal.line = 0;
2445
2446             /* If we came here after encountering a signal in the middle of
2447                a "next", use the stashed-away previous frame pc */
2448             sr_sal.pc
2449               = stopped_by_random_signal
2450               ? prev_pc
2451               : ADDR_BITS_REMOVE (SAVED_PC_AFTER_CALL (get_current_frame ()));
2452             
2453             step_resume_breakpoint =
2454               set_momentary_breakpoint (sr_sal,
2455                                         stopped_by_random_signal ? 
2456                                         NULL : get_current_frame (),
2457                                         bp_step_resume);
2458
2459             /* We've just entered a callee, and we wish to resume until
2460                it returns to the caller.  Setting a step_resume bp on
2461                the return PC will catch a return from the callee.
2462
2463                However, if the callee is recursing, we want to be
2464                careful not to catch returns of those recursive calls,
2465                but of THIS instance of the call.
2466
2467                To do this, we set the step_resume bp's frame to our
2468                current caller's frame (step_frame_address, which is
2469                set by the "next" or "until" command, before execution
2470                begins).
2471
2472                But ... don't do it if we're single-stepping out of a
2473                sigtramp, because the reason we're single-stepping is
2474                precisely because unwinding is a problem (HP-UX 10.20,
2475                e.g.) and the frame address is likely to be incorrect.
2476                No danger of sigtramp recursion.  */
2477
2478             if (stepping_through_sigtramp)
2479               {
2480                 step_resume_breakpoint->frame = (CORE_ADDR) NULL;
2481                 stepping_through_sigtramp = 0;
2482               }
2483             else if (!IN_SOLIB_DYNSYM_RESOLVE_CODE (sr_sal.pc))
2484               step_resume_breakpoint->frame = step_frame_address;
2485
2486             if (breakpoints_inserted)
2487               insert_breakpoints ();
2488           }
2489           goto keep_going;
2490
2491 step_into_function:
2492           /* Subroutine call with source code we should not step over.
2493              Do step to the first line of code in it.  */
2494           {
2495             struct symtab *s;
2496
2497             s = find_pc_symtab (stop_pc);
2498             if (s && s->language != language_asm)
2499               SKIP_PROLOGUE (stop_func_start);
2500           }
2501           sal = find_pc_line (stop_func_start, 0);
2502           /* Use the step_resume_break to step until
2503              the end of the prologue, even if that involves jumps
2504              (as it seems to on the vax under 4.2).  */
2505           /* If the prologue ends in the middle of a source line,
2506              continue to the end of that source line (if it is still
2507              within the function).  Otherwise, just go to end of prologue.  */
2508 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
2509           /* no, don't either.  It skips any code that's
2510              legitimately on the first line.  */
2511 #else
2512           if (sal.end && sal.pc != stop_func_start && sal.end < stop_func_end)
2513             stop_func_start = sal.end;
2514 #endif
2515
2516           if (stop_func_start == stop_pc)
2517             {
2518               /* We are already there: stop now.  */
2519               stop_step = 1;
2520               break;
2521             }
2522           else
2523             /* Put the step-breakpoint there and go until there. */
2524             {
2525               struct symtab_and_line sr_sal;
2526
2527               INIT_SAL (&sr_sal);       /* initialize to zeroes */
2528               sr_sal.pc      = stop_func_start;
2529               sr_sal.section = find_pc_overlay (stop_func_start);
2530               /* Do not specify what the fp should be when we stop
2531                  since on some machines the prologue
2532                  is where the new fp value is established.  */
2533               step_resume_breakpoint =
2534                 set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
2535               if (breakpoints_inserted)
2536                 insert_breakpoints ();
2537
2538               /* And make sure stepping stops right away then.  */
2539               step_range_end = step_range_start;
2540             }
2541           goto keep_going;
2542         }
2543
2544       /* We've wandered out of the step range.  */
2545
2546       sal = find_pc_line(stop_pc, 0);
2547       
2548       if (step_range_end == 1)
2549         {
2550           /* It is stepi or nexti.  We always want to stop stepping after
2551              one instruction.  */
2552           stop_step = 1;
2553           break;
2554         }
2555
2556       /* If we're in the return path from a shared library trampoline,
2557          we want to proceed through the trampoline when stepping.  */
2558       if (IN_SOLIB_RETURN_TRAMPOLINE(stop_pc, stop_func_name))
2559         {
2560           CORE_ADDR tmp;
2561
2562           /* Determine where this trampoline returns.  */
2563           tmp = SKIP_TRAMPOLINE_CODE (stop_pc);
2564
2565           /* Only proceed through if we know where it's going.  */
2566           if (tmp)
2567             {
2568               /* And put the step-breakpoint there and go until there. */
2569               struct symtab_and_line sr_sal;
2570
2571               INIT_SAL (&sr_sal);       /* initialize to zeroes */
2572               sr_sal.pc      = tmp;
2573               sr_sal.section = find_pc_overlay (sr_sal.pc);
2574               /* Do not specify what the fp should be when we stop
2575                  since on some machines the prologue
2576                  is where the new fp value is established.  */
2577               step_resume_breakpoint =
2578                 set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
2579               if (breakpoints_inserted)
2580                 insert_breakpoints ();
2581
2582               /* Restart without fiddling with the step ranges or
2583                  other state.  */
2584               goto keep_going;
2585             }
2586         }
2587          
2588       if (sal.line == 0)
2589         {
2590           /* We have no line number information.  That means to stop
2591              stepping (does this always happen right after one instruction,
2592              when we do "s" in a function with no line numbers,
2593              or can this happen as a result of a return or longjmp?).  */
2594           stop_step = 1;
2595           break;
2596         }
2597
2598       if ((stop_pc == sal.pc)
2599           && (current_line != sal.line || current_symtab != sal.symtab))
2600         {
2601           /* We are at the start of a different line.  So stop.  Note that
2602              we don't stop if we step into the middle of a different line.
2603              That is said to make things like for (;;) statements work
2604              better.  */
2605           stop_step = 1;
2606           break;
2607         }
2608
2609       /* We aren't done stepping.
2610
2611          Optimize by setting the stepping range to the line.
2612          (We might not be in the original line, but if we entered a
2613          new line in mid-statement, we continue stepping.  This makes 
2614          things like for(;;) statements work better.)  */
2615
2616       if (stop_func_end && sal.end >= stop_func_end)
2617         {
2618           /* If this is the last line of the function, don't keep stepping
2619              (it would probably step us out of the function).
2620              This is particularly necessary for a one-line function,
2621              in which after skipping the prologue we better stop even though
2622              we will be in mid-line.  */
2623           stop_step = 1;
2624           break;
2625         }
2626       step_range_start = sal.pc;
2627       step_range_end = sal.end;
2628       step_frame_address = FRAME_FP (get_current_frame ());
2629       current_line = sal.line;
2630       current_symtab = sal.symtab;
2631
2632       /* In the case where we just stepped out of a function into the middle
2633          of a line of the caller, continue stepping, but step_frame_address
2634          must be modified to current frame */
2635       {
2636         CORE_ADDR current_frame = FRAME_FP (get_current_frame());
2637         if (!(INNER_THAN (current_frame, step_frame_address)))
2638           step_frame_address = current_frame;
2639       }
2640       
2641
2642       goto keep_going;
2643
2644     check_sigtramp2:
2645       if (trap_expected
2646           && IN_SIGTRAMP (stop_pc, stop_func_name)
2647           && !IN_SIGTRAMP (prev_pc, prev_func_name)
2648           && INNER_THAN (read_sp (), step_sp))
2649         {
2650           /* What has happened here is that we have just stepped the inferior
2651              with a signal (because it is a signal which shouldn't make
2652              us stop), thus stepping into sigtramp.
2653
2654              So we need to set a step_resume_break_address breakpoint
2655              and continue until we hit it, and then step.  FIXME: This should
2656              be more enduring than a step_resume breakpoint; we should know
2657              that we will later need to keep going rather than re-hitting
2658              the breakpoint here (see testsuite/gdb.t06/signals.exp where
2659              it says "exceedingly difficult").  */
2660           struct symtab_and_line sr_sal;
2661
2662           INIT_SAL (&sr_sal);           /* initialize to zeroes */
2663           sr_sal.pc      = prev_pc;
2664           sr_sal.section = find_pc_overlay (sr_sal.pc);
2665           /* We perhaps could set the frame if we kept track of what
2666              the frame corresponding to prev_pc was.  But we don't,
2667              so don't.  */
2668           through_sigtramp_breakpoint =
2669             set_momentary_breakpoint (sr_sal, NULL, bp_through_sigtramp);
2670           if (breakpoints_inserted)
2671             insert_breakpoints ();
2672
2673           remove_breakpoints_on_following_step = 1;
2674           another_trap = 1;
2675         }
2676
2677     keep_going:
2678       /* Come to this label when you need to resume the inferior.
2679          It's really much cleaner to do a goto than a maze of if-else
2680          conditions.  */
2681
2682       /* ??rehrauer: ttrace on HP-UX theoretically allows one to debug
2683          a vforked child beetween its creation and subsequent exit or
2684          call to exec().  However, I had big problems in this rather
2685          creaky exec engine, getting that to work.  The fundamental
2686          problem is that I'm trying to debug two processes via an
2687          engine that only understands a single process with possibly
2688          multiple threads.
2689
2690          Hence, this spot is known to have problems when
2691          target_can_follow_vfork_prior_to_exec returns 1. */
2692
2693       /* Save the pc before execution, to compare with pc after stop.  */
2694       prev_pc = read_pc ();     /* Might have been DECR_AFTER_BREAK */
2695       prev_func_start = stop_func_start; /* Ok, since if DECR_PC_AFTER
2696                                           BREAK is defined, the
2697                                           original pc would not have
2698                                           been at the start of a
2699                                           function. */
2700       prev_func_name = stop_func_name;
2701
2702       if (update_step_sp)
2703         step_sp = read_sp ();
2704       update_step_sp = 0;
2705
2706       /* If we did not do break;, it means we should keep
2707          running the inferior and not return to debugger.  */
2708
2709       if (trap_expected && stop_signal != TARGET_SIGNAL_TRAP)
2710         {
2711           /* We took a signal (which we are supposed to pass through to
2712              the inferior, else we'd have done a break above) and we
2713              haven't yet gotten our trap.  Simply continue.  */
2714           resume (CURRENTLY_STEPPING (), stop_signal);
2715         }
2716       else
2717         {
2718           /* Either the trap was not expected, but we are continuing
2719              anyway (the user asked that this signal be passed to the
2720              child)
2721                -- or --
2722              The signal was SIGTRAP, e.g. it was our signal, but we
2723              decided we should resume from it.
2724
2725              We're going to run this baby now!
2726
2727              Insert breakpoints now, unless we are trying
2728              to one-proceed past a breakpoint.  */
2729           /* If we've just finished a special step resume and we don't
2730              want to hit a breakpoint, pull em out.  */
2731           if (step_resume_breakpoint == NULL
2732               && through_sigtramp_breakpoint == NULL
2733               && remove_breakpoints_on_following_step)
2734             {
2735               remove_breakpoints_on_following_step = 0;
2736               remove_breakpoints ();
2737               breakpoints_inserted = 0;
2738             }
2739           else if (!breakpoints_inserted &&
2740                    (through_sigtramp_breakpoint != NULL || !another_trap))
2741             {
2742               breakpoints_failed = insert_breakpoints ();
2743               if (breakpoints_failed)
2744                 break;
2745               breakpoints_inserted = 1;
2746             }
2747
2748           trap_expected = another_trap;
2749
2750           if (stop_signal == TARGET_SIGNAL_TRAP)
2751             stop_signal = TARGET_SIGNAL_0;
2752
2753 #ifdef SHIFT_INST_REGS
2754           /* I'm not sure when this following segment applies.  I do know, 
2755              now, that we shouldn't rewrite the regs when we were stopped 
2756              by a random signal from the inferior process.  */
2757           /* FIXME: Shouldn't this be based on the valid bit of the SXIP?
2758              (this is only used on the 88k).  */
2759
2760           if (!bpstat_explains_signal (stop_bpstat)
2761               && (stop_signal != TARGET_SIGNAL_CHLD) 
2762               && !stopped_by_random_signal)
2763             SHIFT_INST_REGS();
2764 #endif /* SHIFT_INST_REGS */
2765
2766           resume (CURRENTLY_STEPPING (), stop_signal);
2767         }
2768     }
2769
2770  stop_stepping:
2771   if (target_has_execution)
2772     {
2773       /* Are we stopping for a vfork event?  We only stop when we see
2774          the child's event.  However, we may not yet have seen the
2775          parent's event.  And, inferior_pid is still set to the parent's
2776          pid, until we resume again and follow either the parent or child.
2777
2778          To ensure that we can really touch inferior_pid (aka, the
2779          parent process) -- which calls to functions like read_pc
2780          implicitly do -- wait on the parent if necessary. */
2781       if ((pending_follow.kind == TARGET_WAITKIND_VFORKED)
2782           && ! pending_follow.fork_event.saw_parent_fork)
2783         {
2784           int  parent_pid;
2785
2786           do {
2787             if (target_wait_hook)
2788               parent_pid = target_wait_hook (-1, &w);
2789             else
2790               parent_pid = target_wait (-1, &w);
2791           } while (parent_pid != inferior_pid);
2792         }
2793
2794
2795       /* Assuming the inferior still exists, set these up for next
2796          time, just like we did above if we didn't break out of the
2797          loop.  */
2798       prev_pc = read_pc ();
2799       prev_func_start = stop_func_start;
2800       prev_func_name = stop_func_name;
2801     }
2802   do_cleanups (old_cleanups);
2803 }
2804
2805 /* This function returns TRUE if ep is an internal breakpoint
2806    set to catch generic shared library (aka dynamically-linked
2807    library) events.  (This is *NOT* the same as a catchpoint for a
2808    shlib event.  The latter is something a user can set; this is
2809    something gdb sets for its own use, and isn't ever shown to a
2810    user.) */
2811 static int
2812 is_internal_shlib_eventpoint (ep)
2813   struct breakpoint *  ep;
2814 {
2815   return
2816     (ep->type == bp_shlib_event)
2817     ;
2818 }
2819
2820 /* This function returns TRUE if bs indicates that the inferior
2821    stopped due to a shared library (aka dynamically-linked library)
2822    event. */
2823 static int
2824 stopped_for_internal_shlib_event (bs)
2825   bpstat  bs;
2826 {
2827   /* Note that multiple eventpoints may've caused the stop.  Any
2828      that are associated with shlib events will be accepted. */
2829   for (;bs != NULL; bs = bs->next)
2830     {
2831       if ((bs->breakpoint_at != NULL)
2832           && is_internal_shlib_eventpoint (bs->breakpoint_at))
2833         return 1;
2834     }
2835
2836   /* If we get here, then no candidate was found. */
2837   return 0;
2838 }
2839
2840 /* This function returns TRUE if bs indicates that the inferior
2841    stopped due to a shared library (aka dynamically-linked library)
2842    event caught by a catchpoint.
2843
2844    If TRUE, cp_p is set to point to the catchpoint.
2845
2846    Else, the value of cp_p is undefined. */
2847 static int
2848 stopped_for_shlib_catchpoint (bs, cp_p)
2849   bpstat  bs;
2850   struct breakpoint **  cp_p;
2851 {
2852   /* Note that multiple eventpoints may've caused the stop.  Any
2853      that are associated with shlib events will be accepted. */
2854   *cp_p = NULL;
2855
2856   for (;bs != NULL; bs = bs->next)
2857     {
2858       if ((bs->breakpoint_at != NULL)
2859           && ep_is_shlib_catchpoint (bs->breakpoint_at))
2860         {
2861           *cp_p = bs->breakpoint_at;
2862           return 1;
2863         }
2864     }
2865
2866   /* If we get here, then no candidate was found. */
2867   return 0;
2868 }
2869
2870 \f
2871 /* Here to return control to GDB when the inferior stops for real.
2872    Print appropriate messages, remove breakpoints, give terminal our modes.
2873
2874    STOP_PRINT_FRAME nonzero means print the executing frame
2875    (pc, function, args, file, line number and line text).
2876    BREAKPOINTS_FAILED nonzero means stop was due to error
2877    attempting to insert breakpoints.  */
2878
2879 void
2880 normal_stop ()
2881 {
2882
2883 #ifdef HPUXHPPA
2884   /* As with the notification of thread events, we want to delay
2885      notifying the user that we've switched thread context until
2886      the inferior actually stops.
2887
2888      (Note that there's no point in saying anything if the inferior
2889      has exited!) */
2890   if ((switched_from_inferior_pid != inferior_pid) &&
2891       target_has_execution)
2892     {
2893       target_terminal_ours_for_output ();
2894       printf_filtered ("[Switched to %s]\n", 
2895                        target_pid_or_tid_to_str (inferior_pid));
2896       switched_from_inferior_pid = inferior_pid;
2897     }
2898 #endif
2899
2900   /* Make sure that the current_frame's pc is correct.  This
2901      is a correction for setting up the frame info before doing
2902      DECR_PC_AFTER_BREAK */
2903   if (target_has_execution && get_current_frame())
2904     (get_current_frame ())->pc = read_pc ();
2905   
2906   if (breakpoints_failed)
2907     {
2908       target_terminal_ours_for_output ();
2909       print_sys_errmsg ("ptrace", breakpoints_failed);
2910       printf_filtered ("Stopped; cannot insert breakpoints.\n\
2911 The same program may be running in another process.\n");
2912     }
2913
2914   if (target_has_execution && breakpoints_inserted)
2915     {
2916     if (remove_breakpoints ())
2917       {
2918         target_terminal_ours_for_output ();
2919         printf_filtered ("Cannot remove breakpoints because ");
2920         printf_filtered ("program is no longer writable.\n");
2921         printf_filtered ("It might be running in another process.\n");
2922         printf_filtered ("Further execution is probably impossible.\n");
2923       }
2924     }
2925   breakpoints_inserted = 0;
2926
2927   /* Delete the breakpoint we stopped at, if it wants to be deleted.
2928      Delete any breakpoint that is to be deleted at the next stop.  */
2929
2930   breakpoint_auto_delete (stop_bpstat);
2931
2932   /* If an auto-display called a function and that got a signal,
2933      delete that auto-display to avoid an infinite recursion.  */
2934
2935   if (stopped_by_random_signal)
2936     disable_current_display ();
2937
2938   /* Don't print a message if in the middle of doing a "step n"
2939      operation for n > 1 */
2940   if (step_multi && stop_step)
2941     goto done;
2942
2943   target_terminal_ours ();
2944
2945   /* Did we stop because the user set the stop_on_solib_events
2946      variable?  (If so, we report this as a generic, "Stopped due
2947      to shlib event" message.) */
2948   if (stopped_for_internal_shlib_event (stop_bpstat))
2949     {
2950       printf_filtered ("Stopped due to shared library event\n");
2951     }
2952
2953   /* Look up the hook_stop and run it if it exists.  */
2954
2955   if (stop_command && stop_command->hook)
2956     {
2957       catch_errors (hook_stop_stub, (char *)stop_command->hook,
2958                     "Error while running hook_stop:\n", RETURN_MASK_ALL);
2959     }
2960
2961   if (!target_has_stack)
2962    {
2963
2964     goto done;
2965    }
2966
2967   /* Select innermost stack frame - i.e., current frame is frame 0,
2968      and current location is based on that.
2969      Don't do this on return from a stack dummy routine,
2970      or if the program has exited. */
2971
2972   if (!stop_stack_dummy)
2973     {
2974       select_frame (get_current_frame (), 0);
2975
2976       /* Print current location without a level number, if
2977          we have changed functions or hit a breakpoint.  
2978          Print source line if we have one.  
2979          bpstat_print() contains the logic deciding in detail
2980          what to print, based on the event(s) that just occurred. */
2981
2982       if (stop_print_frame)
2983         {
2984           int bpstat_ret;
2985           int source_flag;
2986
2987           bpstat_ret = bpstat_print (stop_bpstat);
2988           /* bpstat_print() returned one of:
2989              -1: Didn't print anything
2990               0: Printed preliminary "Breakpoint n, " message, desires
2991                  location tacked on
2992               1: Printed something, don't tack on location */
2993
2994           if (bpstat_ret == -1)
2995             if (   stop_step
2996                 && step_frame_address == FRAME_FP (get_current_frame ())
2997                 && step_start_function == find_pc_function (stop_pc))
2998               source_flag = -1; /* finished step, just print source line */
2999             else
3000               source_flag = 1; /* print location and source line */
3001           else if (bpstat_ret == 0) /* hit bpt, desire location */
3002             source_flag = 1; /* print location and source line */
3003           else /* bpstat_ret == 1, hit bpt, do not desire location */
3004             source_flag = -1; /* just print source line */
3005
3006           /* The behavior of this routine with respect to the source
3007              flag is:
3008              -1: Print only source line
3009              0: Print only location
3010              1: Print location and source line */
3011          show_and_print_stack_frame (selected_frame, -1, source_flag);
3012
3013           /* Display the auto-display expressions.  */
3014           do_displays ();
3015         }
3016     }
3017
3018   /* Save the function value return registers, if we care.
3019      We might be about to restore their previous contents.  */
3020   if (proceed_to_finish)
3021     read_register_bytes (0, stop_registers, REGISTER_BYTES);
3022
3023   if (stop_stack_dummy)
3024     {
3025       /* Pop the empty frame that contains the stack dummy.
3026          POP_FRAME ends with a setting of the current frame, so we
3027          can use that next. */
3028       POP_FRAME;
3029       /* Set stop_pc to what it was before we called the function.
3030          Can't rely on restore_inferior_status because that only gets
3031          called if we don't stop in the called function.  */
3032       stop_pc = read_pc();
3033       select_frame (get_current_frame (), 0);
3034     }
3035
3036
3037  TUIDO (((TuiOpaqueFuncPtr)tui_vCheckDataValues, selected_frame));
3038
3039  done:
3040   annotate_stopped ();
3041 }
3042
3043 static int
3044 hook_stop_stub (cmd)
3045      char *cmd;
3046 {
3047   execute_user_command ((struct cmd_list_element *)cmd, 0);
3048   return (0);
3049 }
3050 \f
3051 int signal_stop_state (signo)
3052      int signo;
3053 {
3054   return signal_stop[signo];
3055 }
3056
3057 int signal_print_state (signo)
3058      int signo;
3059 {
3060   return signal_print[signo];
3061 }
3062
3063 int signal_pass_state (signo)
3064      int signo;
3065 {
3066   return signal_program[signo];
3067 }
3068
3069 static void
3070 sig_print_header ()
3071 {
3072   printf_filtered ("\
3073 Signal        Stop\tPrint\tPass to program\tDescription\n");
3074 }
3075
3076 static void
3077 sig_print_info (oursig)
3078      enum target_signal oursig;
3079 {
3080   char *name = target_signal_to_name (oursig);
3081   int name_padding = 13 - strlen (name);
3082   if (name_padding <= 0)
3083     name_padding = 0;
3084
3085   printf_filtered ("%s", name);
3086   printf_filtered ("%*.*s ", name_padding, name_padding, 
3087                    "                 ");
3088   printf_filtered ("%s\t", signal_stop[oursig] ? "Yes" : "No");
3089   printf_filtered ("%s\t", signal_print[oursig] ? "Yes" : "No");
3090   printf_filtered ("%s\t\t", signal_program[oursig] ? "Yes" : "No");
3091   printf_filtered ("%s\n", target_signal_to_string (oursig));
3092 }
3093
3094 /* Specify how various signals in the inferior should be handled.  */
3095
3096 static void
3097 handle_command (args, from_tty)
3098      char *args;
3099      int from_tty;
3100 {
3101   char **argv;
3102   int digits, wordlen;
3103   int sigfirst, signum, siglast;
3104   enum target_signal oursig;
3105   int allsigs;
3106   int nsigs;
3107   unsigned char *sigs;
3108   struct cleanup *old_chain;
3109
3110   if (args == NULL)
3111     {
3112       error_no_arg ("signal to handle");
3113     }
3114
3115   /* Allocate and zero an array of flags for which signals to handle. */
3116
3117   nsigs = (int)TARGET_SIGNAL_LAST;
3118   sigs = (unsigned char *) alloca (nsigs);
3119   memset (sigs, 0, nsigs);
3120
3121   /* Break the command line up into args. */
3122
3123   argv = buildargv (args);
3124   if (argv == NULL)
3125     {
3126       nomem (0);
3127     }
3128   old_chain = make_cleanup ((make_cleanup_func) freeargv, (char *) argv);
3129
3130   /* Walk through the args, looking for signal oursigs, signal names, and
3131      actions.  Signal numbers and signal names may be interspersed with
3132      actions, with the actions being performed for all signals cumulatively
3133      specified.  Signal ranges can be specified as <LOW>-<HIGH>. */
3134
3135   while (*argv != NULL)
3136     {
3137       wordlen = strlen (*argv);
3138       for (digits = 0; isdigit ((*argv)[digits]); digits++) {;}
3139       allsigs = 0;
3140       sigfirst = siglast = -1;
3141
3142       if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
3143         {
3144           /* Apply action to all signals except those used by the
3145              debugger.  Silently skip those. */
3146           allsigs = 1;
3147           sigfirst = 0;
3148           siglast = nsigs - 1;
3149         }
3150       else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
3151         {
3152           SET_SIGS (nsigs, sigs, signal_stop);
3153           SET_SIGS (nsigs, sigs, signal_print);
3154         }
3155       else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
3156         {
3157           UNSET_SIGS (nsigs, sigs, signal_program);
3158         }
3159       else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
3160         {
3161           SET_SIGS (nsigs, sigs, signal_print);
3162         }
3163       else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
3164         {
3165           SET_SIGS (nsigs, sigs, signal_program);
3166         }
3167       else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
3168         {
3169           UNSET_SIGS (nsigs, sigs, signal_stop);
3170         }
3171       else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
3172         {
3173           SET_SIGS (nsigs, sigs, signal_program);
3174         }
3175       else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
3176         {
3177           UNSET_SIGS (nsigs, sigs, signal_print);
3178           UNSET_SIGS (nsigs, sigs, signal_stop);
3179         }
3180       else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
3181         {
3182           UNSET_SIGS (nsigs, sigs, signal_program);
3183         }
3184       else if (digits > 0)
3185         {
3186           /* It is numeric.  The numeric signal refers to our own
3187              internal signal numbering from target.h, not to host/target
3188              signal  number.  This is a feature; users really should be
3189              using symbolic names anyway, and the common ones like 
3190              SIGHUP, SIGINT, SIGALRM, etc. will work right anyway.  */
3191
3192           sigfirst = siglast = (int) 
3193             target_signal_from_command (atoi (*argv));
3194           if ((*argv)[digits] == '-')
3195             {
3196               siglast = (int)
3197                 target_signal_from_command (atoi ((*argv) + digits + 1));
3198             }
3199           if (sigfirst > siglast)
3200             {
3201               /* Bet he didn't figure we'd think of this case... */
3202               signum = sigfirst;
3203               sigfirst = siglast;
3204               siglast = signum;
3205             }
3206         }
3207       else
3208         {
3209           oursig = target_signal_from_name (*argv);
3210           if (oursig != TARGET_SIGNAL_UNKNOWN)
3211             {
3212               sigfirst = siglast = (int)oursig;
3213             }
3214           else
3215             {
3216               /* Not a number and not a recognized flag word => complain.  */
3217               error ("Unrecognized or ambiguous flag word: \"%s\".", *argv);
3218             }
3219         }
3220
3221       /* If any signal numbers or symbol names were found, set flags for
3222          which signals to apply actions to. */
3223
3224       for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
3225         {
3226           switch ((enum target_signal)signum)
3227             {
3228               case TARGET_SIGNAL_TRAP:
3229               case TARGET_SIGNAL_INT:
3230                 if (!allsigs && !sigs[signum])
3231                   {
3232                     if (query ("%s is used by the debugger.\n\
3233 Are you sure you want to change it? ",
3234                                target_signal_to_name
3235                                ((enum target_signal)signum)))
3236                       {
3237                         sigs[signum] = 1;
3238                       }
3239                     else
3240                       {
3241                         printf_unfiltered ("Not confirmed, unchanged.\n");
3242                         gdb_flush (gdb_stdout);
3243                       }
3244                   }
3245                 break;
3246               case TARGET_SIGNAL_0:
3247               case TARGET_SIGNAL_DEFAULT:
3248               case TARGET_SIGNAL_UNKNOWN:
3249                 /* Make sure that "all" doesn't print these.  */
3250                 break;
3251               default:
3252                 sigs[signum] = 1;
3253                 break;
3254             }
3255         }
3256
3257       argv++;
3258     }
3259
3260   target_notice_signals(inferior_pid);
3261
3262   if (from_tty)
3263     {
3264       /* Show the results.  */
3265       sig_print_header ();
3266       for (signum = 0; signum < nsigs; signum++)
3267         {
3268           if (sigs[signum])
3269             {
3270               sig_print_info (signum);
3271             }
3272         }
3273     }
3274
3275   do_cleanups (old_chain);
3276 }
3277
3278 static void
3279 xdb_handle_command (args, from_tty)
3280      char *args;
3281      int from_tty;
3282 {
3283   char **argv;
3284   struct cleanup *old_chain;
3285
3286   /* Break the command line up into args. */
3287
3288   argv = buildargv (args);
3289   if (argv == NULL)
3290     {
3291       nomem (0);
3292     }
3293   old_chain = make_cleanup ((make_cleanup_func) freeargv, (char *) argv);
3294   if (argv[1] != (char *)NULL)
3295     {
3296       char *argBuf;
3297       int  bufLen;
3298
3299       bufLen = strlen(argv[0]) + 20;
3300       argBuf = (char *)xmalloc(bufLen);
3301       if (argBuf)
3302         {
3303           int validFlag = 1;
3304           enum target_signal oursig;
3305
3306           oursig = target_signal_from_name (argv[0]);
3307           memset(argBuf, 0, bufLen);
3308           if (strcmp(argv[1], "Q") == 0)
3309             sprintf(argBuf, "%s %s", argv[0], "noprint");
3310           else
3311             {
3312               if (strcmp(argv[1], "s") == 0)
3313                 {
3314                   if (!signal_stop[oursig])
3315                     sprintf(argBuf, "%s %s", argv[0], "stop");
3316                   else
3317                     sprintf(argBuf, "%s %s", argv[0], "nostop");
3318                 }
3319               else if (strcmp(argv[1], "i") == 0)
3320                 {
3321                   if (!signal_program[oursig])
3322                     sprintf(argBuf, "%s %s", argv[0], "pass");
3323                   else
3324                     sprintf(argBuf, "%s %s", argv[0], "nopass");
3325                 }
3326               else if (strcmp(argv[1], "r") == 0)
3327                 {
3328                   if (!signal_print[oursig])
3329                     sprintf(argBuf, "%s %s", argv[0], "print");
3330                   else
3331                     sprintf(argBuf, "%s %s", argv[0], "noprint");
3332                 }
3333               else
3334                 validFlag = 0;
3335             }
3336           if (validFlag)
3337             handle_command(argBuf, from_tty);
3338           else
3339             printf_filtered("Invalid signal handling flag.\n");
3340           if (argBuf)
3341             free(argBuf);
3342         }
3343     }
3344   do_cleanups (old_chain);
3345 }
3346
3347 /* Print current contents of the tables set by the handle command.
3348    It is possible we should just be printing signals actually used
3349    by the current target (but for things to work right when switching
3350    targets, all signals should be in the signal tables).  */
3351
3352 static void
3353 signals_info (signum_exp, from_tty)
3354      char *signum_exp;
3355      int from_tty;
3356 {
3357   enum target_signal oursig;
3358   sig_print_header ();
3359
3360   if (signum_exp)
3361     {
3362       /* First see if this is a symbol name.  */
3363       oursig = target_signal_from_name (signum_exp);
3364       if (oursig == TARGET_SIGNAL_UNKNOWN)
3365         {
3366           /* No, try numeric.  */
3367           oursig =
3368             target_signal_from_command (parse_and_eval_address (signum_exp));
3369         }
3370       sig_print_info (oursig);
3371       return;
3372     }
3373
3374   printf_filtered ("\n");
3375   /* These ugly casts brought to you by the native VAX compiler.  */
3376   for (oursig = TARGET_SIGNAL_FIRST;
3377        (int)oursig < (int)TARGET_SIGNAL_LAST;
3378        oursig = (enum target_signal)((int)oursig + 1))
3379     {
3380       QUIT;
3381
3382       if (oursig != TARGET_SIGNAL_UNKNOWN
3383           && oursig != TARGET_SIGNAL_DEFAULT
3384           && oursig != TARGET_SIGNAL_0)
3385         sig_print_info (oursig);
3386     }
3387
3388   printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
3389 }
3390 \f
3391 /* Save all of the information associated with the inferior<==>gdb
3392    connection.  INF_STATUS is a pointer to a "struct inferior_status"
3393    (defined in inferior.h).  */
3394
3395 void
3396 save_inferior_status (inf_status, restore_stack_info)
3397      struct inferior_status *inf_status;
3398      int restore_stack_info;
3399 {
3400   inf_status->stop_signal = stop_signal;
3401   inf_status->stop_pc = stop_pc;
3402   inf_status->stop_step = stop_step;
3403   inf_status->stop_stack_dummy = stop_stack_dummy;
3404   inf_status->stopped_by_random_signal = stopped_by_random_signal;
3405   inf_status->trap_expected = trap_expected;
3406   inf_status->step_range_start = step_range_start;
3407   inf_status->step_range_end = step_range_end;
3408   inf_status->step_frame_address = step_frame_address;
3409   inf_status->step_over_calls = step_over_calls;
3410   inf_status->stop_after_trap = stop_after_trap;
3411   inf_status->stop_soon_quietly = stop_soon_quietly;
3412   /* Save original bpstat chain here; replace it with copy of chain. 
3413      If caller's caller is walking the chain, they'll be happier if we
3414      hand them back the original chain when restore_i_s is called.  */
3415   inf_status->stop_bpstat = stop_bpstat;
3416   stop_bpstat = bpstat_copy (stop_bpstat);
3417   inf_status->breakpoint_proceeded = breakpoint_proceeded;
3418   inf_status->restore_stack_info = restore_stack_info;
3419   inf_status->proceed_to_finish = proceed_to_finish;
3420   
3421   memcpy (inf_status->stop_registers, stop_registers, REGISTER_BYTES);
3422
3423   read_register_bytes (0, inf_status->registers, REGISTER_BYTES);
3424
3425   record_selected_frame (&(inf_status->selected_frame_address),
3426                          &(inf_status->selected_level));
3427   return;
3428 }
3429
3430 struct restore_selected_frame_args {
3431   CORE_ADDR frame_address;
3432   int level;
3433 };
3434
3435 static int restore_selected_frame PARAMS ((char *));
3436
3437 /* Restore the selected frame.  args is really a struct
3438    restore_selected_frame_args * (declared as char * for catch_errors)
3439    telling us what frame to restore.  Returns 1 for success, or 0 for
3440    failure.  An error message will have been printed on error.  */
3441
3442 static int
3443 restore_selected_frame (args)
3444      char *args;
3445 {
3446   struct restore_selected_frame_args *fr =
3447     (struct restore_selected_frame_args *) args;
3448   struct frame_info *frame;
3449   int level = fr->level;
3450
3451   frame = find_relative_frame (get_current_frame (), &level);
3452
3453   /* If inf_status->selected_frame_address is NULL, there was no
3454      previously selected frame.  */
3455   if (frame == NULL ||
3456   /*  FRAME_FP (frame) != fr->frame_address || */
3457       /* elz: deleted this check as a quick fix to the problem that
3458          for function called by hand gdb creates no internal frame
3459          structure and the real stack and gdb's idea of stack are
3460          different if nested calls by hands are made.  
3461
3462          mvs: this worries me.  */
3463       level != 0)
3464    {
3465       warning ("Unable to restore previously selected frame.\n");
3466       return 0;
3467     }
3468
3469   select_frame (frame, fr->level);
3470
3471   return(1);
3472 }
3473
3474 void
3475 restore_inferior_status (inf_status)
3476      struct inferior_status *inf_status;
3477 {
3478   stop_signal = inf_status->stop_signal;
3479   stop_pc = inf_status->stop_pc;
3480   stop_step = inf_status->stop_step;
3481   stop_stack_dummy = inf_status->stop_stack_dummy;
3482   stopped_by_random_signal = inf_status->stopped_by_random_signal;
3483   trap_expected = inf_status->trap_expected;
3484   step_range_start = inf_status->step_range_start;
3485   step_range_end = inf_status->step_range_end;
3486   step_frame_address = inf_status->step_frame_address;
3487   step_over_calls = inf_status->step_over_calls;
3488   stop_after_trap = inf_status->stop_after_trap;
3489   stop_soon_quietly = inf_status->stop_soon_quietly;
3490   bpstat_clear (&stop_bpstat);
3491   stop_bpstat = inf_status->stop_bpstat;
3492   breakpoint_proceeded = inf_status->breakpoint_proceeded;
3493   proceed_to_finish = inf_status->proceed_to_finish;
3494
3495   memcpy (stop_registers, inf_status->stop_registers, REGISTER_BYTES);
3496
3497   /* The inferior can be gone if the user types "print exit(0)"
3498      (and perhaps other times).  */
3499   if (target_has_execution)
3500     write_register_bytes (0, inf_status->registers, REGISTER_BYTES);
3501
3502   /* The inferior can be gone if the user types "print exit(0)"
3503      (and perhaps other times).  */
3504
3505   /* FIXME: If we are being called after stopping in a function which
3506      is called from gdb, we should not be trying to restore the
3507      selected frame; it just prints a spurious error message (The
3508      message is useful, however, in detecting bugs in gdb (like if gdb
3509      clobbers the stack)).  In fact, should we be restoring the
3510      inferior status at all in that case?  .  */
3511
3512   if (target_has_stack && inf_status->restore_stack_info)
3513     {
3514       struct restore_selected_frame_args fr;
3515       fr.level = inf_status->selected_level;
3516       fr.frame_address = inf_status->selected_frame_address;
3517       /* The point of catch_errors is that if the stack is clobbered,
3518          walking the stack might encounter a garbage pointer and error()
3519          trying to dereference it.  */
3520       if (catch_errors (restore_selected_frame, &fr,
3521                         "Unable to restore previously selected frame:\n",
3522                         RETURN_MASK_ERROR) == 0)
3523         /* Error in restoring the selected frame.  Select the innermost
3524            frame.  */
3525
3526
3527       select_frame (get_current_frame (), 0);
3528
3529     }
3530 }
3531
3532
3533 \f
3534 void
3535 set_follow_fork_mode_command (arg, from_tty, c)
3536   char *  arg;
3537   int  from_tty;
3538   struct cmd_list_element *  c;
3539 {
3540   if (! STREQ (arg, "parent") &&
3541       ! STREQ (arg, "child") &&
3542       ! STREQ (arg, "both") &&
3543       ! STREQ (arg, "ask"))
3544     error ("follow-fork-mode must be one of \"parent\", \"child\", \"both\" or \"ask\".");
3545
3546   if (follow_fork_mode_string != NULL)
3547     free (follow_fork_mode_string);
3548   follow_fork_mode_string = savestring (arg, strlen (arg));
3549 }
3550
3551
3552 \f
3553 void
3554 _initialize_infrun ()
3555 {
3556   register int i;
3557   register int numsigs;
3558   struct cmd_list_element *  c;
3559
3560   add_info ("signals", signals_info,
3561             "What debugger does when program gets various signals.\n\
3562 Specify a signal as argument to print info on that signal only.");
3563   add_info_alias ("handle", "signals", 0);
3564
3565   add_com ("handle", class_run, handle_command,
3566            concat ("Specify how to handle a signal.\n\
3567 Args are signals and actions to apply to those signals.\n\
3568 Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
3569 from 1-15 are allowed for compatibility with old versions of GDB.\n\
3570 Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
3571 The special arg \"all\" is recognized to mean all signals except those\n\
3572 used by the debugger, typically SIGTRAP and SIGINT.\n",
3573 "Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
3574 \"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
3575 Stop means reenter debugger if this signal happens (implies print).\n\
3576 Print means print a message if this signal happens.\n\
3577 Pass means let program see this signal; otherwise program doesn't know.\n\
3578 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
3579 Pass and Stop may be combined.", NULL));
3580   if (xdb_commands)
3581     {
3582       add_com("lz", class_info, signals_info, 
3583                   "What debugger does when program gets various signals.\n\
3584 Specify a signal as argument to print info on that signal only.");
3585       add_com("z", class_run, xdb_handle_command,
3586            concat ("Specify how to handle a signal.\n\
3587 Args are signals and actions to apply to those signals.\n\
3588 Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
3589 from 1-15 are allowed for compatibility with old versions of GDB.\n\
3590 Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
3591 The special arg \"all\" is recognized to mean all signals except those\n\
3592 used by the debugger, typically SIGTRAP and SIGINT.\n",
3593 "Recognized actions include \"s\" (toggles between stop and nostop), \n\
3594 \"r\" (toggles between print and noprint), \"i\" (toggles between pass and \
3595 nopass), \"Q\" (noprint)\n\
3596 Stop means reenter debugger if this signal happens (implies print).\n\
3597 Print means print a message if this signal happens.\n\
3598 Pass means let program see this signal; otherwise program doesn't know.\n\
3599 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
3600 Pass and Stop may be combined.", NULL));
3601     }
3602
3603   if (!dbx_commands)
3604     stop_command = add_cmd ("stop", class_obscure, not_just_help_class_command,
3605            "There is no `stop' command, but you can set a hook on `stop'.\n\
3606 This allows you to set a list of commands to be run each time execution\n\
3607 of the program stops.", &cmdlist);
3608
3609   numsigs = (int)TARGET_SIGNAL_LAST;
3610   signal_stop = (unsigned char *)    
3611     xmalloc (sizeof (signal_stop[0]) * numsigs);
3612   signal_print = (unsigned char *)
3613     xmalloc (sizeof (signal_print[0]) * numsigs);
3614   signal_program = (unsigned char *)
3615     xmalloc (sizeof (signal_program[0]) * numsigs);
3616   for (i = 0; i < numsigs; i++)
3617     {
3618       signal_stop[i] = 1;
3619       signal_print[i] = 1;
3620       signal_program[i] = 1;
3621     }
3622
3623   /* Signals caused by debugger's own actions
3624      should not be given to the program afterwards.  */
3625   signal_program[TARGET_SIGNAL_TRAP] = 0;
3626   signal_program[TARGET_SIGNAL_INT] = 0;
3627
3628   /* Signals that are not errors should not normally enter the debugger.  */
3629   signal_stop[TARGET_SIGNAL_ALRM] = 0;
3630   signal_print[TARGET_SIGNAL_ALRM] = 0;
3631   signal_stop[TARGET_SIGNAL_VTALRM] = 0;
3632   signal_print[TARGET_SIGNAL_VTALRM] = 0;
3633   signal_stop[TARGET_SIGNAL_PROF] = 0;
3634   signal_print[TARGET_SIGNAL_PROF] = 0;
3635   signal_stop[TARGET_SIGNAL_CHLD] = 0;
3636   signal_print[TARGET_SIGNAL_CHLD] = 0;
3637   signal_stop[TARGET_SIGNAL_IO] = 0;
3638   signal_print[TARGET_SIGNAL_IO] = 0;
3639   signal_stop[TARGET_SIGNAL_POLL] = 0;
3640   signal_print[TARGET_SIGNAL_POLL] = 0;
3641   signal_stop[TARGET_SIGNAL_URG] = 0;
3642   signal_print[TARGET_SIGNAL_URG] = 0;
3643
3644 #ifdef SOLIB_ADD
3645   add_show_from_set
3646     (add_set_cmd ("stop-on-solib-events", class_support, var_zinteger,
3647                   (char *) &stop_on_solib_events,
3648                   "Set stopping for shared library events.\n\
3649 If nonzero, gdb will give control to the user when the dynamic linker\n\
3650 notifies gdb of shared library events.  The most common event of interest\n\
3651 to the user would be loading/unloading of a new library.\n",
3652                   &setlist),
3653      &showlist);
3654 #endif
3655
3656   c = add_set_enum_cmd ("follow-fork-mode",
3657                         class_run,
3658                         follow_fork_mode_kind_names,
3659                         (char *) &follow_fork_mode_string,
3660 /* ??rehrauer:  The "both" option is broken, by what may be a 10.20
3661    kernel problem.  It's also not terribly useful without a GUI to
3662    help the user drive two debuggers.  So for now, I'm disabling
3663    the "both" option.  */
3664 /*                      "Set debugger response to a program call of fork \
3665 or vfork.\n\
3666 A fork or vfork creates a new process.  follow-fork-mode can be:\n\
3667   parent  - the original process is debugged after a fork\n\
3668   child   - the new process is debugged after a fork\n\
3669   both    - both the parent and child are debugged after a fork\n\
3670   ask     - the debugger will ask for one of the above choices\n\
3671 For \"both\", another copy of the debugger will be started to follow\n\
3672 the new child process.  The original debugger will continue to follow\n\
3673 the original parent process.  To distinguish their prompts, the\n\
3674 debugger copy's prompt will be changed.\n\
3675 For \"parent\" or \"child\", the unfollowed process will run free.\n\
3676 By default, the debugger will follow the parent process.",
3677 */
3678                         "Set debugger response to a program call of fork \
3679 or vfork.\n\
3680 A fork or vfork creates a new process.  follow-fork-mode can be:\n\
3681   parent  - the original process is debugged after a fork\n\
3682   child   - the new process is debugged after a fork\n\
3683   ask     - the debugger will ask for one of the above choices\n\
3684 For \"parent\" or \"child\", the unfollowed process will run free.\n\
3685 By default, the debugger will follow the parent process.",
3686                         &setlist);
3687 /*  c->function.sfunc = ;*/
3688   add_show_from_set (c, &showlist);
3689
3690   set_follow_fork_mode_command ("parent", 0, NULL);
3691 }