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