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