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