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