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