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