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