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