OSDN Git Service

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