2011-09-02 Pedro Alves <pedro@codesourcery.com>
[platform/upstream/binutils.git] / gdb / infrun.c
1 /* Target-struct-independent code to start (run) and stop an inferior
2    process.
3
4    Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
5    1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
6    2008, 2009, 2010, 2011 Free Software 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 3 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, see <http://www.gnu.org/licenses/>.  */
22
23 #include "defs.h"
24 #include "gdb_string.h"
25 #include <ctype.h>
26 #include "symtab.h"
27 #include "frame.h"
28 #include "inferior.h"
29 #include "exceptions.h"
30 #include "breakpoint.h"
31 #include "gdb_wait.h"
32 #include "gdbcore.h"
33 #include "gdbcmd.h"
34 #include "cli/cli-script.h"
35 #include "target.h"
36 #include "gdbthread.h"
37 #include "annotate.h"
38 #include "symfile.h"
39 #include "top.h"
40 #include <signal.h>
41 #include "inf-loop.h"
42 #include "regcache.h"
43 #include "value.h"
44 #include "observer.h"
45 #include "language.h"
46 #include "solib.h"
47 #include "main.h"
48 #include "dictionary.h"
49 #include "block.h"
50 #include "gdb_assert.h"
51 #include "mi/mi-common.h"
52 #include "event-top.h"
53 #include "record.h"
54 #include "inline-frame.h"
55 #include "jit.h"
56 #include "tracepoint.h"
57 #include "continuations.h"
58 #include "interps.h"
59
60 /* Prototypes for local functions */
61
62 static void signals_info (char *, int);
63
64 static void handle_command (char *, int);
65
66 static void sig_print_info (enum target_signal);
67
68 static void sig_print_header (void);
69
70 static void resume_cleanups (void *);
71
72 static int hook_stop_stub (void *);
73
74 static int restore_selected_frame (void *);
75
76 static int follow_fork (void);
77
78 static void set_schedlock_func (char *args, int from_tty,
79                                 struct cmd_list_element *c);
80
81 static int currently_stepping (struct thread_info *tp);
82
83 static int currently_stepping_or_nexting_callback (struct thread_info *tp,
84                                                    void *data);
85
86 static void xdb_handle_command (char *args, int from_tty);
87
88 static int prepare_to_proceed (int);
89
90 static void print_exited_reason (int exitstatus);
91
92 static void print_signal_exited_reason (enum target_signal siggnal);
93
94 static void print_no_history_reason (void);
95
96 static void print_signal_received_reason (enum target_signal siggnal);
97
98 static void print_end_stepping_range_reason (void);
99
100 void _initialize_infrun (void);
101
102 void nullify_last_target_wait_ptid (void);
103
104 static void insert_hp_step_resume_breakpoint_at_frame (struct frame_info *);
105
106 static void insert_step_resume_breakpoint_at_caller (struct frame_info *);
107
108 static void insert_longjmp_resume_breakpoint (struct gdbarch *, CORE_ADDR);
109
110 /* When set, stop the 'step' command if we enter a function which has
111    no line number information.  The normal behavior is that we step
112    over such function.  */
113 int step_stop_if_no_debug = 0;
114 static void
115 show_step_stop_if_no_debug (struct ui_file *file, int from_tty,
116                             struct cmd_list_element *c, const char *value)
117 {
118   fprintf_filtered (file, _("Mode of the step operation is %s.\n"), value);
119 }
120
121 /* In asynchronous mode, but simulating synchronous execution.  */
122
123 int sync_execution = 0;
124
125 /* wait_for_inferior and normal_stop use this to notify the user
126    when the inferior stopped in a different thread than it had been
127    running in.  */
128
129 static ptid_t previous_inferior_ptid;
130
131 /* Default behavior is to detach newly forked processes (legacy).  */
132 int detach_fork = 1;
133
134 int debug_displaced = 0;
135 static void
136 show_debug_displaced (struct ui_file *file, int from_tty,
137                       struct cmd_list_element *c, const char *value)
138 {
139   fprintf_filtered (file, _("Displace stepping debugging is %s.\n"), value);
140 }
141
142 int debug_infrun = 0;
143 static void
144 show_debug_infrun (struct ui_file *file, int from_tty,
145                    struct cmd_list_element *c, const char *value)
146 {
147   fprintf_filtered (file, _("Inferior debugging is %s.\n"), value);
148 }
149
150 /* If the program uses ELF-style shared libraries, then calls to
151    functions in shared libraries go through stubs, which live in a
152    table called the PLT (Procedure Linkage Table).  The first time the
153    function is called, the stub sends control to the dynamic linker,
154    which looks up the function's real address, patches the stub so
155    that future calls will go directly to the function, and then passes
156    control to the function.
157
158    If we are stepping at the source level, we don't want to see any of
159    this --- we just want to skip over the stub and the dynamic linker.
160    The simple approach is to single-step until control leaves the
161    dynamic linker.
162
163    However, on some systems (e.g., Red Hat's 5.2 distribution) the
164    dynamic linker calls functions in the shared C library, so you
165    can't tell from the PC alone whether the dynamic linker is still
166    running.  In this case, we use a step-resume breakpoint to get us
167    past the dynamic linker, as if we were using "next" to step over a
168    function call.
169
170    in_solib_dynsym_resolve_code() says whether we're in the dynamic
171    linker code or not.  Normally, this means we single-step.  However,
172    if SKIP_SOLIB_RESOLVER then returns non-zero, then its value is an
173    address where we can place a step-resume breakpoint to get past the
174    linker's symbol resolution function.
175
176    in_solib_dynsym_resolve_code() can generally be implemented in a
177    pretty portable way, by comparing the PC against the address ranges
178    of the dynamic linker's sections.
179
180    SKIP_SOLIB_RESOLVER is generally going to be system-specific, since
181    it depends on internal details of the dynamic linker.  It's usually
182    not too hard to figure out where to put a breakpoint, but it
183    certainly isn't portable.  SKIP_SOLIB_RESOLVER should do plenty of
184    sanity checking.  If it can't figure things out, returning zero and
185    getting the (possibly confusing) stepping behavior is better than
186    signalling an error, which will obscure the change in the
187    inferior's state.  */
188
189 /* This function returns TRUE if pc is the address of an instruction
190    that lies within the dynamic linker (such as the event hook, or the
191    dld itself).
192
193    This function must be used only when a dynamic linker event has
194    been caught, and the inferior is being stepped out of the hook, or
195    undefined results are guaranteed.  */
196
197 #ifndef SOLIB_IN_DYNAMIC_LINKER
198 #define SOLIB_IN_DYNAMIC_LINKER(pid,pc) 0
199 #endif
200
201 /* "Observer mode" is somewhat like a more extreme version of
202    non-stop, in which all GDB operations that might affect the
203    target's execution have been disabled.  */
204
205 static int non_stop_1 = 0;
206
207 int observer_mode = 0;
208 static int observer_mode_1 = 0;
209
210 static void
211 set_observer_mode (char *args, int from_tty,
212                    struct cmd_list_element *c)
213 {
214   extern int pagination_enabled;
215
216   if (target_has_execution)
217     {
218       observer_mode_1 = observer_mode;
219       error (_("Cannot change this setting while the inferior is running."));
220     }
221
222   observer_mode = observer_mode_1;
223
224   may_write_registers = !observer_mode;
225   may_write_memory = !observer_mode;
226   may_insert_breakpoints = !observer_mode;
227   may_insert_tracepoints = !observer_mode;
228   /* We can insert fast tracepoints in or out of observer mode,
229      but enable them if we're going into this mode.  */
230   if (observer_mode)
231     may_insert_fast_tracepoints = 1;
232   may_stop = !observer_mode;
233   update_target_permissions ();
234
235   /* Going *into* observer mode we must force non-stop, then
236      going out we leave it that way.  */
237   if (observer_mode)
238     {
239       target_async_permitted = 1;
240       pagination_enabled = 0;
241       non_stop = non_stop_1 = 1;
242     }
243
244   if (from_tty)
245     printf_filtered (_("Observer mode is now %s.\n"),
246                      (observer_mode ? "on" : "off"));
247 }
248
249 static void
250 show_observer_mode (struct ui_file *file, int from_tty,
251                     struct cmd_list_element *c, const char *value)
252 {
253   fprintf_filtered (file, _("Observer mode is %s.\n"), value);
254 }
255
256 /* This updates the value of observer mode based on changes in
257    permissions.  Note that we are deliberately ignoring the values of
258    may-write-registers and may-write-memory, since the user may have
259    reason to enable these during a session, for instance to turn on a
260    debugging-related global.  */
261
262 void
263 update_observer_mode (void)
264 {
265   int newval;
266
267   newval = (!may_insert_breakpoints
268             && !may_insert_tracepoints
269             && may_insert_fast_tracepoints
270             && !may_stop
271             && non_stop);
272
273   /* Let the user know if things change.  */
274   if (newval != observer_mode)
275     printf_filtered (_("Observer mode is now %s.\n"),
276                      (newval ? "on" : "off"));
277
278   observer_mode = observer_mode_1 = newval;
279 }
280
281 /* Tables of how to react to signals; the user sets them.  */
282
283 static unsigned char *signal_stop;
284 static unsigned char *signal_print;
285 static unsigned char *signal_program;
286
287 /* Table of signals that the target may silently handle.
288    This is automatically determined from the flags above,
289    and simply cached here.  */
290 static unsigned char *signal_pass;
291
292 #define SET_SIGS(nsigs,sigs,flags) \
293   do { \
294     int signum = (nsigs); \
295     while (signum-- > 0) \
296       if ((sigs)[signum]) \
297         (flags)[signum] = 1; \
298   } while (0)
299
300 #define UNSET_SIGS(nsigs,sigs,flags) \
301   do { \
302     int signum = (nsigs); \
303     while (signum-- > 0) \
304       if ((sigs)[signum]) \
305         (flags)[signum] = 0; \
306   } while (0)
307
308 /* Value to pass to target_resume() to cause all threads to resume.  */
309
310 #define RESUME_ALL minus_one_ptid
311
312 /* Command list pointer for the "stop" placeholder.  */
313
314 static struct cmd_list_element *stop_command;
315
316 /* Function inferior was in as of last step command.  */
317
318 static struct symbol *step_start_function;
319
320 /* Nonzero if we want to give control to the user when we're notified
321    of shared library events by the dynamic linker.  */
322 int stop_on_solib_events;
323 static void
324 show_stop_on_solib_events (struct ui_file *file, int from_tty,
325                            struct cmd_list_element *c, const char *value)
326 {
327   fprintf_filtered (file, _("Stopping for shared library events is %s.\n"),
328                     value);
329 }
330
331 /* Nonzero means expecting a trace trap
332    and should stop the inferior and return silently when it happens.  */
333
334 int stop_after_trap;
335
336 /* Save register contents here when executing a "finish" command or are
337    about to pop a stack dummy frame, if-and-only-if proceed_to_finish is set.
338    Thus this contains the return value from the called function (assuming
339    values are returned in a register).  */
340
341 struct regcache *stop_registers;
342
343 /* Nonzero after stop if current stack frame should be printed.  */
344
345 static int stop_print_frame;
346
347 /* This is a cached copy of the pid/waitstatus of the last event
348    returned by target_wait()/deprecated_target_wait_hook().  This
349    information is returned by get_last_target_status().  */
350 static ptid_t target_last_wait_ptid;
351 static struct target_waitstatus target_last_waitstatus;
352
353 static void context_switch (ptid_t ptid);
354
355 void init_thread_stepping_state (struct thread_info *tss);
356
357 void init_infwait_state (void);
358
359 static const char follow_fork_mode_child[] = "child";
360 static const char follow_fork_mode_parent[] = "parent";
361
362 static const char *follow_fork_mode_kind_names[] = {
363   follow_fork_mode_child,
364   follow_fork_mode_parent,
365   NULL
366 };
367
368 static const char *follow_fork_mode_string = follow_fork_mode_parent;
369 static void
370 show_follow_fork_mode_string (struct ui_file *file, int from_tty,
371                               struct cmd_list_element *c, const char *value)
372 {
373   fprintf_filtered (file,
374                     _("Debugger response to a program "
375                       "call of fork or vfork is \"%s\".\n"),
376                     value);
377 }
378 \f
379
380 /* Tell the target to follow the fork we're stopped at.  Returns true
381    if the inferior should be resumed; false, if the target for some
382    reason decided it's best not to resume.  */
383
384 static int
385 follow_fork (void)
386 {
387   int follow_child = (follow_fork_mode_string == follow_fork_mode_child);
388   int should_resume = 1;
389   struct thread_info *tp;
390
391   /* Copy user stepping state to the new inferior thread.  FIXME: the
392      followed fork child thread should have a copy of most of the
393      parent thread structure's run control related fields, not just these.
394      Initialized to avoid "may be used uninitialized" warnings from gcc.  */
395   struct breakpoint *step_resume_breakpoint = NULL;
396   struct breakpoint *exception_resume_breakpoint = NULL;
397   CORE_ADDR step_range_start = 0;
398   CORE_ADDR step_range_end = 0;
399   struct frame_id step_frame_id = { 0 };
400
401   if (!non_stop)
402     {
403       ptid_t wait_ptid;
404       struct target_waitstatus wait_status;
405
406       /* Get the last target status returned by target_wait().  */
407       get_last_target_status (&wait_ptid, &wait_status);
408
409       /* If not stopped at a fork event, then there's nothing else to
410          do.  */
411       if (wait_status.kind != TARGET_WAITKIND_FORKED
412           && wait_status.kind != TARGET_WAITKIND_VFORKED)
413         return 1;
414
415       /* Check if we switched over from WAIT_PTID, since the event was
416          reported.  */
417       if (!ptid_equal (wait_ptid, minus_one_ptid)
418           && !ptid_equal (inferior_ptid, wait_ptid))
419         {
420           /* We did.  Switch back to WAIT_PTID thread, to tell the
421              target to follow it (in either direction).  We'll
422              afterwards refuse to resume, and inform the user what
423              happened.  */
424           switch_to_thread (wait_ptid);
425           should_resume = 0;
426         }
427     }
428
429   tp = inferior_thread ();
430
431   /* If there were any forks/vforks that were caught and are now to be
432      followed, then do so now.  */
433   switch (tp->pending_follow.kind)
434     {
435     case TARGET_WAITKIND_FORKED:
436     case TARGET_WAITKIND_VFORKED:
437       {
438         ptid_t parent, child;
439
440         /* If the user did a next/step, etc, over a fork call,
441            preserve the stepping state in the fork child.  */
442         if (follow_child && should_resume)
443           {
444             step_resume_breakpoint = clone_momentary_breakpoint
445                                          (tp->control.step_resume_breakpoint);
446             step_range_start = tp->control.step_range_start;
447             step_range_end = tp->control.step_range_end;
448             step_frame_id = tp->control.step_frame_id;
449             exception_resume_breakpoint
450               = clone_momentary_breakpoint (tp->control.exception_resume_breakpoint);
451
452             /* For now, delete the parent's sr breakpoint, otherwise,
453                parent/child sr breakpoints are considered duplicates,
454                and the child version will not be installed.  Remove
455                this when the breakpoints module becomes aware of
456                inferiors and address spaces.  */
457             delete_step_resume_breakpoint (tp);
458             tp->control.step_range_start = 0;
459             tp->control.step_range_end = 0;
460             tp->control.step_frame_id = null_frame_id;
461             delete_exception_resume_breakpoint (tp);
462           }
463
464         parent = inferior_ptid;
465         child = tp->pending_follow.value.related_pid;
466
467         /* Tell the target to do whatever is necessary to follow
468            either parent or child.  */
469         if (target_follow_fork (follow_child))
470           {
471             /* Target refused to follow, or there's some other reason
472                we shouldn't resume.  */
473             should_resume = 0;
474           }
475         else
476           {
477             /* This pending follow fork event is now handled, one way
478                or another.  The previous selected thread may be gone
479                from the lists by now, but if it is still around, need
480                to clear the pending follow request.  */
481             tp = find_thread_ptid (parent);
482             if (tp)
483               tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
484
485             /* This makes sure we don't try to apply the "Switched
486                over from WAIT_PID" logic above.  */
487             nullify_last_target_wait_ptid ();
488
489             /* If we followed the child, switch to it...  */
490             if (follow_child)
491               {
492                 switch_to_thread (child);
493
494                 /* ... and preserve the stepping state, in case the
495                    user was stepping over the fork call.  */
496                 if (should_resume)
497                   {
498                     tp = inferior_thread ();
499                     tp->control.step_resume_breakpoint
500                       = step_resume_breakpoint;
501                     tp->control.step_range_start = step_range_start;
502                     tp->control.step_range_end = step_range_end;
503                     tp->control.step_frame_id = step_frame_id;
504                     tp->control.exception_resume_breakpoint
505                       = exception_resume_breakpoint;
506                   }
507                 else
508                   {
509                     /* If we get here, it was because we're trying to
510                        resume from a fork catchpoint, but, the user
511                        has switched threads away from the thread that
512                        forked.  In that case, the resume command
513                        issued is most likely not applicable to the
514                        child, so just warn, and refuse to resume.  */
515                     warning (_("Not resuming: switched threads "
516                                "before following fork child.\n"));
517                   }
518
519                 /* Reset breakpoints in the child as appropriate.  */
520                 follow_inferior_reset_breakpoints ();
521               }
522             else
523               switch_to_thread (parent);
524           }
525       }
526       break;
527     case TARGET_WAITKIND_SPURIOUS:
528       /* Nothing to follow.  */
529       break;
530     default:
531       internal_error (__FILE__, __LINE__,
532                       "Unexpected pending_follow.kind %d\n",
533                       tp->pending_follow.kind);
534       break;
535     }
536
537   return should_resume;
538 }
539
540 void
541 follow_inferior_reset_breakpoints (void)
542 {
543   struct thread_info *tp = inferior_thread ();
544
545   /* Was there a step_resume breakpoint?  (There was if the user
546      did a "next" at the fork() call.)  If so, explicitly reset its
547      thread number.
548
549      step_resumes are a form of bp that are made to be per-thread.
550      Since we created the step_resume bp when the parent process
551      was being debugged, and now are switching to the child process,
552      from the breakpoint package's viewpoint, that's a switch of
553      "threads".  We must update the bp's notion of which thread
554      it is for, or it'll be ignored when it triggers.  */
555
556   if (tp->control.step_resume_breakpoint)
557     breakpoint_re_set_thread (tp->control.step_resume_breakpoint);
558
559   if (tp->control.exception_resume_breakpoint)
560     breakpoint_re_set_thread (tp->control.exception_resume_breakpoint);
561
562   /* Reinsert all breakpoints in the child.  The user may have set
563      breakpoints after catching the fork, in which case those
564      were never set in the child, but only in the parent.  This makes
565      sure the inserted breakpoints match the breakpoint list.  */
566
567   breakpoint_re_set ();
568   insert_breakpoints ();
569 }
570
571 /* The child has exited or execed: resume threads of the parent the
572    user wanted to be executing.  */
573
574 static int
575 proceed_after_vfork_done (struct thread_info *thread,
576                           void *arg)
577 {
578   int pid = * (int *) arg;
579
580   if (ptid_get_pid (thread->ptid) == pid
581       && is_running (thread->ptid)
582       && !is_executing (thread->ptid)
583       && !thread->stop_requested
584       && thread->suspend.stop_signal == TARGET_SIGNAL_0)
585     {
586       if (debug_infrun)
587         fprintf_unfiltered (gdb_stdlog,
588                             "infrun: resuming vfork parent thread %s\n",
589                             target_pid_to_str (thread->ptid));
590
591       switch_to_thread (thread->ptid);
592       clear_proceed_status ();
593       proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
594     }
595
596   return 0;
597 }
598
599 /* Called whenever we notice an exec or exit event, to handle
600    detaching or resuming a vfork parent.  */
601
602 static void
603 handle_vfork_child_exec_or_exit (int exec)
604 {
605   struct inferior *inf = current_inferior ();
606
607   if (inf->vfork_parent)
608     {
609       int resume_parent = -1;
610
611       /* This exec or exit marks the end of the shared memory region
612          between the parent and the child.  If the user wanted to
613          detach from the parent, now is the time.  */
614
615       if (inf->vfork_parent->pending_detach)
616         {
617           struct thread_info *tp;
618           struct cleanup *old_chain;
619           struct program_space *pspace;
620           struct address_space *aspace;
621
622           /* follow-fork child, detach-on-fork on.  */
623
624           old_chain = make_cleanup_restore_current_thread ();
625
626           /* We're letting loose of the parent.  */
627           tp = any_live_thread_of_process (inf->vfork_parent->pid);
628           switch_to_thread (tp->ptid);
629
630           /* We're about to detach from the parent, which implicitly
631              removes breakpoints from its address space.  There's a
632              catch here: we want to reuse the spaces for the child,
633              but, parent/child are still sharing the pspace at this
634              point, although the exec in reality makes the kernel give
635              the child a fresh set of new pages.  The problem here is
636              that the breakpoints module being unaware of this, would
637              likely chose the child process to write to the parent
638              address space.  Swapping the child temporarily away from
639              the spaces has the desired effect.  Yes, this is "sort
640              of" a hack.  */
641
642           pspace = inf->pspace;
643           aspace = inf->aspace;
644           inf->aspace = NULL;
645           inf->pspace = NULL;
646
647           if (debug_infrun || info_verbose)
648             {
649               target_terminal_ours ();
650
651               if (exec)
652                 fprintf_filtered (gdb_stdlog,
653                                   "Detaching vfork parent process "
654                                   "%d after child exec.\n",
655                                   inf->vfork_parent->pid);
656               else
657                 fprintf_filtered (gdb_stdlog,
658                                   "Detaching vfork parent process "
659                                   "%d after child exit.\n",
660                                   inf->vfork_parent->pid);
661             }
662
663           target_detach (NULL, 0);
664
665           /* Put it back.  */
666           inf->pspace = pspace;
667           inf->aspace = aspace;
668
669           do_cleanups (old_chain);
670         }
671       else if (exec)
672         {
673           /* We're staying attached to the parent, so, really give the
674              child a new address space.  */
675           inf->pspace = add_program_space (maybe_new_address_space ());
676           inf->aspace = inf->pspace->aspace;
677           inf->removable = 1;
678           set_current_program_space (inf->pspace);
679
680           resume_parent = inf->vfork_parent->pid;
681
682           /* Break the bonds.  */
683           inf->vfork_parent->vfork_child = NULL;
684         }
685       else
686         {
687           struct cleanup *old_chain;
688           struct program_space *pspace;
689
690           /* If this is a vfork child exiting, then the pspace and
691              aspaces were shared with the parent.  Since we're
692              reporting the process exit, we'll be mourning all that is
693              found in the address space, and switching to null_ptid,
694              preparing to start a new inferior.  But, since we don't
695              want to clobber the parent's address/program spaces, we
696              go ahead and create a new one for this exiting
697              inferior.  */
698
699           /* Switch to null_ptid, so that clone_program_space doesn't want
700              to read the selected frame of a dead process.  */
701           old_chain = save_inferior_ptid ();
702           inferior_ptid = null_ptid;
703
704           /* This inferior is dead, so avoid giving the breakpoints
705              module the option to write through to it (cloning a
706              program space resets breakpoints).  */
707           inf->aspace = NULL;
708           inf->pspace = NULL;
709           pspace = add_program_space (maybe_new_address_space ());
710           set_current_program_space (pspace);
711           inf->removable = 1;
712           clone_program_space (pspace, inf->vfork_parent->pspace);
713           inf->pspace = pspace;
714           inf->aspace = pspace->aspace;
715
716           /* Put back inferior_ptid.  We'll continue mourning this
717              inferior.  */
718           do_cleanups (old_chain);
719
720           resume_parent = inf->vfork_parent->pid;
721           /* Break the bonds.  */
722           inf->vfork_parent->vfork_child = NULL;
723         }
724
725       inf->vfork_parent = NULL;
726
727       gdb_assert (current_program_space == inf->pspace);
728
729       if (non_stop && resume_parent != -1)
730         {
731           /* If the user wanted the parent to be running, let it go
732              free now.  */
733           struct cleanup *old_chain = make_cleanup_restore_current_thread ();
734
735           if (debug_infrun)
736             fprintf_unfiltered (gdb_stdlog,
737                                 "infrun: resuming vfork parent process %d\n",
738                                 resume_parent);
739
740           iterate_over_threads (proceed_after_vfork_done, &resume_parent);
741
742           do_cleanups (old_chain);
743         }
744     }
745 }
746
747 /* Enum strings for "set|show displaced-stepping".  */
748
749 static const char follow_exec_mode_new[] = "new";
750 static const char follow_exec_mode_same[] = "same";
751 static const char *follow_exec_mode_names[] =
752 {
753   follow_exec_mode_new,
754   follow_exec_mode_same,
755   NULL,
756 };
757
758 static const char *follow_exec_mode_string = follow_exec_mode_same;
759 static void
760 show_follow_exec_mode_string (struct ui_file *file, int from_tty,
761                               struct cmd_list_element *c, const char *value)
762 {
763   fprintf_filtered (file, _("Follow exec mode is \"%s\".\n"),  value);
764 }
765
766 /* EXECD_PATHNAME is assumed to be non-NULL.  */
767
768 static void
769 follow_exec (ptid_t pid, char *execd_pathname)
770 {
771   struct thread_info *th = inferior_thread ();
772   struct inferior *inf = current_inferior ();
773
774   /* This is an exec event that we actually wish to pay attention to.
775      Refresh our symbol table to the newly exec'd program, remove any
776      momentary bp's, etc.
777
778      If there are breakpoints, they aren't really inserted now,
779      since the exec() transformed our inferior into a fresh set
780      of instructions.
781
782      We want to preserve symbolic breakpoints on the list, since
783      we have hopes that they can be reset after the new a.out's
784      symbol table is read.
785
786      However, any "raw" breakpoints must be removed from the list
787      (e.g., the solib bp's), since their address is probably invalid
788      now.
789
790      And, we DON'T want to call delete_breakpoints() here, since
791      that may write the bp's "shadow contents" (the instruction
792      value that was overwritten witha TRAP instruction).  Since
793      we now have a new a.out, those shadow contents aren't valid.  */
794
795   mark_breakpoints_out ();
796
797   update_breakpoints_after_exec ();
798
799   /* If there was one, it's gone now.  We cannot truly step-to-next
800      statement through an exec().  */
801   th->control.step_resume_breakpoint = NULL;
802   th->control.exception_resume_breakpoint = NULL;
803   th->control.step_range_start = 0;
804   th->control.step_range_end = 0;
805
806   /* The target reports the exec event to the main thread, even if
807      some other thread does the exec, and even if the main thread was
808      already stopped --- if debugging in non-stop mode, it's possible
809      the user had the main thread held stopped in the previous image
810      --- release it now.  This is the same behavior as step-over-exec
811      with scheduler-locking on in all-stop mode.  */
812   th->stop_requested = 0;
813
814   /* What is this a.out's name?  */
815   printf_unfiltered (_("%s is executing new program: %s\n"),
816                      target_pid_to_str (inferior_ptid),
817                      execd_pathname);
818
819   /* We've followed the inferior through an exec.  Therefore, the
820      inferior has essentially been killed & reborn.  */
821
822   gdb_flush (gdb_stdout);
823
824   breakpoint_init_inferior (inf_execd);
825
826   if (gdb_sysroot && *gdb_sysroot)
827     {
828       char *name = alloca (strlen (gdb_sysroot)
829                             + strlen (execd_pathname)
830                             + 1);
831
832       strcpy (name, gdb_sysroot);
833       strcat (name, execd_pathname);
834       execd_pathname = name;
835     }
836
837   /* Reset the shared library package.  This ensures that we get a
838      shlib event when the child reaches "_start", at which point the
839      dld will have had a chance to initialize the child.  */
840   /* Also, loading a symbol file below may trigger symbol lookups, and
841      we don't want those to be satisfied by the libraries of the
842      previous incarnation of this process.  */
843   no_shared_libraries (NULL, 0);
844
845   if (follow_exec_mode_string == follow_exec_mode_new)
846     {
847       struct program_space *pspace;
848
849       /* The user wants to keep the old inferior and program spaces
850          around.  Create a new fresh one, and switch to it.  */
851
852       inf = add_inferior (current_inferior ()->pid);
853       pspace = add_program_space (maybe_new_address_space ());
854       inf->pspace = pspace;
855       inf->aspace = pspace->aspace;
856
857       exit_inferior_num_silent (current_inferior ()->num);
858
859       set_current_inferior (inf);
860       set_current_program_space (pspace);
861     }
862
863   gdb_assert (current_program_space == inf->pspace);
864
865   /* That a.out is now the one to use.  */
866   exec_file_attach (execd_pathname, 0);
867
868   /* SYMFILE_DEFER_BP_RESET is used as the proper displacement for PIE
869      (Position Independent Executable) main symbol file will get applied by
870      solib_create_inferior_hook below.  breakpoint_re_set would fail to insert
871      the breakpoints with the zero displacement.  */
872
873   symbol_file_add (execd_pathname, SYMFILE_MAINLINE | SYMFILE_DEFER_BP_RESET,
874                    NULL, 0);
875
876   set_initial_language ();
877
878 #ifdef SOLIB_CREATE_INFERIOR_HOOK
879   SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid));
880 #else
881   solib_create_inferior_hook (0);
882 #endif
883
884   jit_inferior_created_hook ();
885
886   breakpoint_re_set ();
887
888   /* Reinsert all breakpoints.  (Those which were symbolic have
889      been reset to the proper address in the new a.out, thanks
890      to symbol_file_command...).  */
891   insert_breakpoints ();
892
893   /* The next resume of this inferior should bring it to the shlib
894      startup breakpoints.  (If the user had also set bp's on
895      "main" from the old (parent) process, then they'll auto-
896      matically get reset there in the new process.).  */
897 }
898
899 /* Non-zero if we just simulating a single-step.  This is needed
900    because we cannot remove the breakpoints in the inferior process
901    until after the `wait' in `wait_for_inferior'.  */
902 static int singlestep_breakpoints_inserted_p = 0;
903
904 /* The thread we inserted single-step breakpoints for.  */
905 static ptid_t singlestep_ptid;
906
907 /* PC when we started this single-step.  */
908 static CORE_ADDR singlestep_pc;
909
910 /* If another thread hit the singlestep breakpoint, we save the original
911    thread here so that we can resume single-stepping it later.  */
912 static ptid_t saved_singlestep_ptid;
913 static int stepping_past_singlestep_breakpoint;
914
915 /* If not equal to null_ptid, this means that after stepping over breakpoint
916    is finished, we need to switch to deferred_step_ptid, and step it.
917
918    The use case is when one thread has hit a breakpoint, and then the user 
919    has switched to another thread and issued 'step'.  We need to step over
920    breakpoint in the thread which hit the breakpoint, but then continue
921    stepping the thread user has selected.  */
922 static ptid_t deferred_step_ptid;
923 \f
924 /* Displaced stepping.  */
925
926 /* In non-stop debugging mode, we must take special care to manage
927    breakpoints properly; in particular, the traditional strategy for
928    stepping a thread past a breakpoint it has hit is unsuitable.
929    'Displaced stepping' is a tactic for stepping one thread past a
930    breakpoint it has hit while ensuring that other threads running
931    concurrently will hit the breakpoint as they should.
932
933    The traditional way to step a thread T off a breakpoint in a
934    multi-threaded program in all-stop mode is as follows:
935
936    a0) Initially, all threads are stopped, and breakpoints are not
937        inserted.
938    a1) We single-step T, leaving breakpoints uninserted.
939    a2) We insert breakpoints, and resume all threads.
940
941    In non-stop debugging, however, this strategy is unsuitable: we
942    don't want to have to stop all threads in the system in order to
943    continue or step T past a breakpoint.  Instead, we use displaced
944    stepping:
945
946    n0) Initially, T is stopped, other threads are running, and
947        breakpoints are inserted.
948    n1) We copy the instruction "under" the breakpoint to a separate
949        location, outside the main code stream, making any adjustments
950        to the instruction, register, and memory state as directed by
951        T's architecture.
952    n2) We single-step T over the instruction at its new location.
953    n3) We adjust the resulting register and memory state as directed
954        by T's architecture.  This includes resetting T's PC to point
955        back into the main instruction stream.
956    n4) We resume T.
957
958    This approach depends on the following gdbarch methods:
959
960    - gdbarch_max_insn_length and gdbarch_displaced_step_location
961      indicate where to copy the instruction, and how much space must
962      be reserved there.  We use these in step n1.
963
964    - gdbarch_displaced_step_copy_insn copies a instruction to a new
965      address, and makes any necessary adjustments to the instruction,
966      register contents, and memory.  We use this in step n1.
967
968    - gdbarch_displaced_step_fixup adjusts registers and memory after
969      we have successfuly single-stepped the instruction, to yield the
970      same effect the instruction would have had if we had executed it
971      at its original address.  We use this in step n3.
972
973    - gdbarch_displaced_step_free_closure provides cleanup.
974
975    The gdbarch_displaced_step_copy_insn and
976    gdbarch_displaced_step_fixup functions must be written so that
977    copying an instruction with gdbarch_displaced_step_copy_insn,
978    single-stepping across the copied instruction, and then applying
979    gdbarch_displaced_insn_fixup should have the same effects on the
980    thread's memory and registers as stepping the instruction in place
981    would have.  Exactly which responsibilities fall to the copy and
982    which fall to the fixup is up to the author of those functions.
983
984    See the comments in gdbarch.sh for details.
985
986    Note that displaced stepping and software single-step cannot
987    currently be used in combination, although with some care I think
988    they could be made to.  Software single-step works by placing
989    breakpoints on all possible subsequent instructions; if the
990    displaced instruction is a PC-relative jump, those breakpoints
991    could fall in very strange places --- on pages that aren't
992    executable, or at addresses that are not proper instruction
993    boundaries.  (We do generally let other threads run while we wait
994    to hit the software single-step breakpoint, and they might
995    encounter such a corrupted instruction.)  One way to work around
996    this would be to have gdbarch_displaced_step_copy_insn fully
997    simulate the effect of PC-relative instructions (and return NULL)
998    on architectures that use software single-stepping.
999
1000    In non-stop mode, we can have independent and simultaneous step
1001    requests, so more than one thread may need to simultaneously step
1002    over a breakpoint.  The current implementation assumes there is
1003    only one scratch space per process.  In this case, we have to
1004    serialize access to the scratch space.  If thread A wants to step
1005    over a breakpoint, but we are currently waiting for some other
1006    thread to complete a displaced step, we leave thread A stopped and
1007    place it in the displaced_step_request_queue.  Whenever a displaced
1008    step finishes, we pick the next thread in the queue and start a new
1009    displaced step operation on it.  See displaced_step_prepare and
1010    displaced_step_fixup for details.  */
1011
1012 struct displaced_step_request
1013 {
1014   ptid_t ptid;
1015   struct displaced_step_request *next;
1016 };
1017
1018 /* Per-inferior displaced stepping state.  */
1019 struct displaced_step_inferior_state
1020 {
1021   /* Pointer to next in linked list.  */
1022   struct displaced_step_inferior_state *next;
1023
1024   /* The process this displaced step state refers to.  */
1025   int pid;
1026
1027   /* A queue of pending displaced stepping requests.  One entry per
1028      thread that needs to do a displaced step.  */
1029   struct displaced_step_request *step_request_queue;
1030
1031   /* If this is not null_ptid, this is the thread carrying out a
1032      displaced single-step in process PID.  This thread's state will
1033      require fixing up once it has completed its step.  */
1034   ptid_t step_ptid;
1035
1036   /* The architecture the thread had when we stepped it.  */
1037   struct gdbarch *step_gdbarch;
1038
1039   /* The closure provided gdbarch_displaced_step_copy_insn, to be used
1040      for post-step cleanup.  */
1041   struct displaced_step_closure *step_closure;
1042
1043   /* The address of the original instruction, and the copy we
1044      made.  */
1045   CORE_ADDR step_original, step_copy;
1046
1047   /* Saved contents of copy area.  */
1048   gdb_byte *step_saved_copy;
1049 };
1050
1051 /* The list of states of processes involved in displaced stepping
1052    presently.  */
1053 static struct displaced_step_inferior_state *displaced_step_inferior_states;
1054
1055 /* Get the displaced stepping state of process PID.  */
1056
1057 static struct displaced_step_inferior_state *
1058 get_displaced_stepping_state (int pid)
1059 {
1060   struct displaced_step_inferior_state *state;
1061
1062   for (state = displaced_step_inferior_states;
1063        state != NULL;
1064        state = state->next)
1065     if (state->pid == pid)
1066       return state;
1067
1068   return NULL;
1069 }
1070
1071 /* Add a new displaced stepping state for process PID to the displaced
1072    stepping state list, or return a pointer to an already existing
1073    entry, if it already exists.  Never returns NULL.  */
1074
1075 static struct displaced_step_inferior_state *
1076 add_displaced_stepping_state (int pid)
1077 {
1078   struct displaced_step_inferior_state *state;
1079
1080   for (state = displaced_step_inferior_states;
1081        state != NULL;
1082        state = state->next)
1083     if (state->pid == pid)
1084       return state;
1085
1086   state = xcalloc (1, sizeof (*state));
1087   state->pid = pid;
1088   state->next = displaced_step_inferior_states;
1089   displaced_step_inferior_states = state;
1090
1091   return state;
1092 }
1093
1094 /* If inferior is in displaced stepping, and ADDR equals to starting address
1095    of copy area, return corresponding displaced_step_closure.  Otherwise,
1096    return NULL.  */
1097
1098 struct displaced_step_closure*
1099 get_displaced_step_closure_by_addr (CORE_ADDR addr)
1100 {
1101   struct displaced_step_inferior_state *displaced
1102     = get_displaced_stepping_state (ptid_get_pid (inferior_ptid));
1103
1104   /* If checking the mode of displaced instruction in copy area.  */
1105   if (displaced && !ptid_equal (displaced->step_ptid, null_ptid)
1106      && (displaced->step_copy == addr))
1107     return displaced->step_closure;
1108
1109   return NULL;
1110 }
1111
1112 /* Remove the displaced stepping state of process PID.  */
1113
1114 static void
1115 remove_displaced_stepping_state (int pid)
1116 {
1117   struct displaced_step_inferior_state *it, **prev_next_p;
1118
1119   gdb_assert (pid != 0);
1120
1121   it = displaced_step_inferior_states;
1122   prev_next_p = &displaced_step_inferior_states;
1123   while (it)
1124     {
1125       if (it->pid == pid)
1126         {
1127           *prev_next_p = it->next;
1128           xfree (it);
1129           return;
1130         }
1131
1132       prev_next_p = &it->next;
1133       it = *prev_next_p;
1134     }
1135 }
1136
1137 static void
1138 infrun_inferior_exit (struct inferior *inf)
1139 {
1140   remove_displaced_stepping_state (inf->pid);
1141 }
1142
1143 /* Enum strings for "set|show displaced-stepping".  */
1144
1145 static const char can_use_displaced_stepping_auto[] = "auto";
1146 static const char can_use_displaced_stepping_on[] = "on";
1147 static const char can_use_displaced_stepping_off[] = "off";
1148 static const char *can_use_displaced_stepping_enum[] =
1149 {
1150   can_use_displaced_stepping_auto,
1151   can_use_displaced_stepping_on,
1152   can_use_displaced_stepping_off,
1153   NULL,
1154 };
1155
1156 /* If ON, and the architecture supports it, GDB will use displaced
1157    stepping to step over breakpoints.  If OFF, or if the architecture
1158    doesn't support it, GDB will instead use the traditional
1159    hold-and-step approach.  If AUTO (which is the default), GDB will
1160    decide which technique to use to step over breakpoints depending on
1161    which of all-stop or non-stop mode is active --- displaced stepping
1162    in non-stop mode; hold-and-step in all-stop mode.  */
1163
1164 static const char *can_use_displaced_stepping =
1165   can_use_displaced_stepping_auto;
1166
1167 static void
1168 show_can_use_displaced_stepping (struct ui_file *file, int from_tty,
1169                                  struct cmd_list_element *c,
1170                                  const char *value)
1171 {
1172   if (can_use_displaced_stepping == can_use_displaced_stepping_auto)
1173     fprintf_filtered (file,
1174                       _("Debugger's willingness to use displaced stepping "
1175                         "to step over breakpoints is %s (currently %s).\n"),
1176                       value, non_stop ? "on" : "off");
1177   else
1178     fprintf_filtered (file,
1179                       _("Debugger's willingness to use displaced stepping "
1180                         "to step over breakpoints is %s.\n"), value);
1181 }
1182
1183 /* Return non-zero if displaced stepping can/should be used to step
1184    over breakpoints.  */
1185
1186 static int
1187 use_displaced_stepping (struct gdbarch *gdbarch)
1188 {
1189   return (((can_use_displaced_stepping == can_use_displaced_stepping_auto
1190             && non_stop)
1191            || can_use_displaced_stepping == can_use_displaced_stepping_on)
1192           && gdbarch_displaced_step_copy_insn_p (gdbarch)
1193           && !RECORD_IS_USED);
1194 }
1195
1196 /* Clean out any stray displaced stepping state.  */
1197 static void
1198 displaced_step_clear (struct displaced_step_inferior_state *displaced)
1199 {
1200   /* Indicate that there is no cleanup pending.  */
1201   displaced->step_ptid = null_ptid;
1202
1203   if (displaced->step_closure)
1204     {
1205       gdbarch_displaced_step_free_closure (displaced->step_gdbarch,
1206                                            displaced->step_closure);
1207       displaced->step_closure = NULL;
1208     }
1209 }
1210
1211 static void
1212 displaced_step_clear_cleanup (void *arg)
1213 {
1214   struct displaced_step_inferior_state *state = arg;
1215
1216   displaced_step_clear (state);
1217 }
1218
1219 /* Dump LEN bytes at BUF in hex to FILE, followed by a newline.  */
1220 void
1221 displaced_step_dump_bytes (struct ui_file *file,
1222                            const gdb_byte *buf,
1223                            size_t len)
1224 {
1225   int i;
1226
1227   for (i = 0; i < len; i++)
1228     fprintf_unfiltered (file, "%02x ", buf[i]);
1229   fputs_unfiltered ("\n", file);
1230 }
1231
1232 /* Prepare to single-step, using displaced stepping.
1233
1234    Note that we cannot use displaced stepping when we have a signal to
1235    deliver.  If we have a signal to deliver and an instruction to step
1236    over, then after the step, there will be no indication from the
1237    target whether the thread entered a signal handler or ignored the
1238    signal and stepped over the instruction successfully --- both cases
1239    result in a simple SIGTRAP.  In the first case we mustn't do a
1240    fixup, and in the second case we must --- but we can't tell which.
1241    Comments in the code for 'random signals' in handle_inferior_event
1242    explain how we handle this case instead.
1243
1244    Returns 1 if preparing was successful -- this thread is going to be
1245    stepped now; or 0 if displaced stepping this thread got queued.  */
1246 static int
1247 displaced_step_prepare (ptid_t ptid)
1248 {
1249   struct cleanup *old_cleanups, *ignore_cleanups;
1250   struct regcache *regcache = get_thread_regcache (ptid);
1251   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1252   CORE_ADDR original, copy;
1253   ULONGEST len;
1254   struct displaced_step_closure *closure;
1255   struct displaced_step_inferior_state *displaced;
1256
1257   /* We should never reach this function if the architecture does not
1258      support displaced stepping.  */
1259   gdb_assert (gdbarch_displaced_step_copy_insn_p (gdbarch));
1260
1261   /* We have to displaced step one thread at a time, as we only have
1262      access to a single scratch space per inferior.  */
1263
1264   displaced = add_displaced_stepping_state (ptid_get_pid (ptid));
1265
1266   if (!ptid_equal (displaced->step_ptid, null_ptid))
1267     {
1268       /* Already waiting for a displaced step to finish.  Defer this
1269          request and place in queue.  */
1270       struct displaced_step_request *req, *new_req;
1271
1272       if (debug_displaced)
1273         fprintf_unfiltered (gdb_stdlog,
1274                             "displaced: defering step of %s\n",
1275                             target_pid_to_str (ptid));
1276
1277       new_req = xmalloc (sizeof (*new_req));
1278       new_req->ptid = ptid;
1279       new_req->next = NULL;
1280
1281       if (displaced->step_request_queue)
1282         {
1283           for (req = displaced->step_request_queue;
1284                req && req->next;
1285                req = req->next)
1286             ;
1287           req->next = new_req;
1288         }
1289       else
1290         displaced->step_request_queue = new_req;
1291
1292       return 0;
1293     }
1294   else
1295     {
1296       if (debug_displaced)
1297         fprintf_unfiltered (gdb_stdlog,
1298                             "displaced: stepping %s now\n",
1299                             target_pid_to_str (ptid));
1300     }
1301
1302   displaced_step_clear (displaced);
1303
1304   old_cleanups = save_inferior_ptid ();
1305   inferior_ptid = ptid;
1306
1307   original = regcache_read_pc (regcache);
1308
1309   copy = gdbarch_displaced_step_location (gdbarch);
1310   len = gdbarch_max_insn_length (gdbarch);
1311
1312   /* Save the original contents of the copy area.  */
1313   displaced->step_saved_copy = xmalloc (len);
1314   ignore_cleanups = make_cleanup (free_current_contents,
1315                                   &displaced->step_saved_copy);
1316   read_memory (copy, displaced->step_saved_copy, len);
1317   if (debug_displaced)
1318     {
1319       fprintf_unfiltered (gdb_stdlog, "displaced: saved %s: ",
1320                           paddress (gdbarch, copy));
1321       displaced_step_dump_bytes (gdb_stdlog,
1322                                  displaced->step_saved_copy,
1323                                  len);
1324     };
1325
1326   closure = gdbarch_displaced_step_copy_insn (gdbarch,
1327                                               original, copy, regcache);
1328
1329   /* We don't support the fully-simulated case at present.  */
1330   gdb_assert (closure);
1331
1332   /* Save the information we need to fix things up if the step
1333      succeeds.  */
1334   displaced->step_ptid = ptid;
1335   displaced->step_gdbarch = gdbarch;
1336   displaced->step_closure = closure;
1337   displaced->step_original = original;
1338   displaced->step_copy = copy;
1339
1340   make_cleanup (displaced_step_clear_cleanup, displaced);
1341
1342   /* Resume execution at the copy.  */
1343   regcache_write_pc (regcache, copy);
1344
1345   discard_cleanups (ignore_cleanups);
1346
1347   do_cleanups (old_cleanups);
1348
1349   if (debug_displaced)
1350     fprintf_unfiltered (gdb_stdlog, "displaced: displaced pc to %s\n",
1351                         paddress (gdbarch, copy));
1352
1353   return 1;
1354 }
1355
1356 static void
1357 write_memory_ptid (ptid_t ptid, CORE_ADDR memaddr,
1358                    const gdb_byte *myaddr, int len)
1359 {
1360   struct cleanup *ptid_cleanup = save_inferior_ptid ();
1361
1362   inferior_ptid = ptid;
1363   write_memory (memaddr, myaddr, len);
1364   do_cleanups (ptid_cleanup);
1365 }
1366
1367 static void
1368 displaced_step_fixup (ptid_t event_ptid, enum target_signal signal)
1369 {
1370   struct cleanup *old_cleanups;
1371   struct displaced_step_inferior_state *displaced
1372     = get_displaced_stepping_state (ptid_get_pid (event_ptid));
1373
1374   /* Was any thread of this process doing a displaced step?  */
1375   if (displaced == NULL)
1376     return;
1377
1378   /* Was this event for the pid we displaced?  */
1379   if (ptid_equal (displaced->step_ptid, null_ptid)
1380       || ! ptid_equal (displaced->step_ptid, event_ptid))
1381     return;
1382
1383   old_cleanups = make_cleanup (displaced_step_clear_cleanup, displaced);
1384
1385   /* Restore the contents of the copy area.  */
1386   {
1387     ULONGEST len = gdbarch_max_insn_length (displaced->step_gdbarch);
1388
1389     write_memory_ptid (displaced->step_ptid, displaced->step_copy,
1390                        displaced->step_saved_copy, len);
1391     if (debug_displaced)
1392       fprintf_unfiltered (gdb_stdlog, "displaced: restored %s\n",
1393                           paddress (displaced->step_gdbarch,
1394                                     displaced->step_copy));
1395   }
1396
1397   /* Did the instruction complete successfully?  */
1398   if (signal == TARGET_SIGNAL_TRAP)
1399     {
1400       /* Fix up the resulting state.  */
1401       gdbarch_displaced_step_fixup (displaced->step_gdbarch,
1402                                     displaced->step_closure,
1403                                     displaced->step_original,
1404                                     displaced->step_copy,
1405                                     get_thread_regcache (displaced->step_ptid));
1406     }
1407   else
1408     {
1409       /* Since the instruction didn't complete, all we can do is
1410          relocate the PC.  */
1411       struct regcache *regcache = get_thread_regcache (event_ptid);
1412       CORE_ADDR pc = regcache_read_pc (regcache);
1413
1414       pc = displaced->step_original + (pc - displaced->step_copy);
1415       regcache_write_pc (regcache, pc);
1416     }
1417
1418   do_cleanups (old_cleanups);
1419
1420   displaced->step_ptid = null_ptid;
1421
1422   /* Are there any pending displaced stepping requests?  If so, run
1423      one now.  Leave the state object around, since we're likely to
1424      need it again soon.  */
1425   while (displaced->step_request_queue)
1426     {
1427       struct displaced_step_request *head;
1428       ptid_t ptid;
1429       struct regcache *regcache;
1430       struct gdbarch *gdbarch;
1431       CORE_ADDR actual_pc;
1432       struct address_space *aspace;
1433
1434       head = displaced->step_request_queue;
1435       ptid = head->ptid;
1436       displaced->step_request_queue = head->next;
1437       xfree (head);
1438
1439       context_switch (ptid);
1440
1441       regcache = get_thread_regcache (ptid);
1442       actual_pc = regcache_read_pc (regcache);
1443       aspace = get_regcache_aspace (regcache);
1444
1445       if (breakpoint_here_p (aspace, actual_pc))
1446         {
1447           if (debug_displaced)
1448             fprintf_unfiltered (gdb_stdlog,
1449                                 "displaced: stepping queued %s now\n",
1450                                 target_pid_to_str (ptid));
1451
1452           displaced_step_prepare (ptid);
1453
1454           gdbarch = get_regcache_arch (regcache);
1455
1456           if (debug_displaced)
1457             {
1458               CORE_ADDR actual_pc = regcache_read_pc (regcache);
1459               gdb_byte buf[4];
1460
1461               fprintf_unfiltered (gdb_stdlog, "displaced: run %s: ",
1462                                   paddress (gdbarch, actual_pc));
1463               read_memory (actual_pc, buf, sizeof (buf));
1464               displaced_step_dump_bytes (gdb_stdlog, buf, sizeof (buf));
1465             }
1466
1467           if (gdbarch_displaced_step_hw_singlestep (gdbarch,
1468                                                     displaced->step_closure))
1469             target_resume (ptid, 1, TARGET_SIGNAL_0);
1470           else
1471             target_resume (ptid, 0, TARGET_SIGNAL_0);
1472
1473           /* Done, we're stepping a thread.  */
1474           break;
1475         }
1476       else
1477         {
1478           int step;
1479           struct thread_info *tp = inferior_thread ();
1480
1481           /* The breakpoint we were sitting under has since been
1482              removed.  */
1483           tp->control.trap_expected = 0;
1484
1485           /* Go back to what we were trying to do.  */
1486           step = currently_stepping (tp);
1487
1488           if (debug_displaced)
1489             fprintf_unfiltered (gdb_stdlog,
1490                                 "breakpoint is gone %s: step(%d)\n",
1491                                 target_pid_to_str (tp->ptid), step);
1492
1493           target_resume (ptid, step, TARGET_SIGNAL_0);
1494           tp->suspend.stop_signal = TARGET_SIGNAL_0;
1495
1496           /* This request was discarded.  See if there's any other
1497              thread waiting for its turn.  */
1498         }
1499     }
1500 }
1501
1502 /* Update global variables holding ptids to hold NEW_PTID if they were
1503    holding OLD_PTID.  */
1504 static void
1505 infrun_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
1506 {
1507   struct displaced_step_request *it;
1508   struct displaced_step_inferior_state *displaced;
1509
1510   if (ptid_equal (inferior_ptid, old_ptid))
1511     inferior_ptid = new_ptid;
1512
1513   if (ptid_equal (singlestep_ptid, old_ptid))
1514     singlestep_ptid = new_ptid;
1515
1516   if (ptid_equal (deferred_step_ptid, old_ptid))
1517     deferred_step_ptid = new_ptid;
1518
1519   for (displaced = displaced_step_inferior_states;
1520        displaced;
1521        displaced = displaced->next)
1522     {
1523       if (ptid_equal (displaced->step_ptid, old_ptid))
1524         displaced->step_ptid = new_ptid;
1525
1526       for (it = displaced->step_request_queue; it; it = it->next)
1527         if (ptid_equal (it->ptid, old_ptid))
1528           it->ptid = new_ptid;
1529     }
1530 }
1531
1532 \f
1533 /* Resuming.  */
1534
1535 /* Things to clean up if we QUIT out of resume ().  */
1536 static void
1537 resume_cleanups (void *ignore)
1538 {
1539   normal_stop ();
1540 }
1541
1542 static const char schedlock_off[] = "off";
1543 static const char schedlock_on[] = "on";
1544 static const char schedlock_step[] = "step";
1545 static const char *scheduler_enums[] = {
1546   schedlock_off,
1547   schedlock_on,
1548   schedlock_step,
1549   NULL
1550 };
1551 static const char *scheduler_mode = schedlock_off;
1552 static void
1553 show_scheduler_mode (struct ui_file *file, int from_tty,
1554                      struct cmd_list_element *c, const char *value)
1555 {
1556   fprintf_filtered (file,
1557                     _("Mode for locking scheduler "
1558                       "during execution is \"%s\".\n"),
1559                     value);
1560 }
1561
1562 static void
1563 set_schedlock_func (char *args, int from_tty, struct cmd_list_element *c)
1564 {
1565   if (!target_can_lock_scheduler)
1566     {
1567       scheduler_mode = schedlock_off;
1568       error (_("Target '%s' cannot support this command."), target_shortname);
1569     }
1570 }
1571
1572 /* True if execution commands resume all threads of all processes by
1573    default; otherwise, resume only threads of the current inferior
1574    process.  */
1575 int sched_multi = 0;
1576
1577 /* Try to setup for software single stepping over the specified location.
1578    Return 1 if target_resume() should use hardware single step.
1579
1580    GDBARCH the current gdbarch.
1581    PC the location to step over.  */
1582
1583 static int
1584 maybe_software_singlestep (struct gdbarch *gdbarch, CORE_ADDR pc)
1585 {
1586   int hw_step = 1;
1587
1588   if (execution_direction == EXEC_FORWARD
1589       && gdbarch_software_single_step_p (gdbarch)
1590       && gdbarch_software_single_step (gdbarch, get_current_frame ()))
1591     {
1592       hw_step = 0;
1593       /* Do not pull these breakpoints until after a `wait' in
1594          `wait_for_inferior'.  */
1595       singlestep_breakpoints_inserted_p = 1;
1596       singlestep_ptid = inferior_ptid;
1597       singlestep_pc = pc;
1598     }
1599   return hw_step;
1600 }
1601
1602 /* Return a ptid representing the set of threads that we will proceed,
1603    in the perspective of the user/frontend.  We may actually resume
1604    fewer threads at first, e.g., if a thread is stopped at a
1605    breakpoint that needs stepping-off, but that should not be visible
1606    to the user/frontend, and neither should the frontend/user be
1607    allowed to proceed any of the threads that happen to be stopped for
1608    internal run control handling, if a previous command wanted them
1609    resumed.  */
1610
1611 ptid_t
1612 user_visible_resume_ptid (int step)
1613 {
1614   /* By default, resume all threads of all processes.  */
1615   ptid_t resume_ptid = RESUME_ALL;
1616
1617   /* Maybe resume only all threads of the current process.  */
1618   if (!sched_multi && target_supports_multi_process ())
1619     {
1620       resume_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
1621     }
1622
1623   /* Maybe resume a single thread after all.  */
1624   if (non_stop)
1625     {
1626       /* With non-stop mode on, threads are always handled
1627          individually.  */
1628       resume_ptid = inferior_ptid;
1629     }
1630   else if ((scheduler_mode == schedlock_on)
1631            || (scheduler_mode == schedlock_step
1632                && (step || singlestep_breakpoints_inserted_p)))
1633     {
1634       /* User-settable 'scheduler' mode requires solo thread resume.  */
1635       resume_ptid = inferior_ptid;
1636     }
1637
1638   return resume_ptid;
1639 }
1640
1641 /* Resume the inferior, but allow a QUIT.  This is useful if the user
1642    wants to interrupt some lengthy single-stepping operation
1643    (for child processes, the SIGINT goes to the inferior, and so
1644    we get a SIGINT random_signal, but for remote debugging and perhaps
1645    other targets, that's not true).
1646
1647    STEP nonzero if we should step (zero to continue instead).
1648    SIG is the signal to give the inferior (zero for none).  */
1649 void
1650 resume (int step, enum target_signal sig)
1651 {
1652   int should_resume = 1;
1653   struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
1654   struct regcache *regcache = get_current_regcache ();
1655   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1656   struct thread_info *tp = inferior_thread ();
1657   CORE_ADDR pc = regcache_read_pc (regcache);
1658   struct address_space *aspace = get_regcache_aspace (regcache);
1659
1660   QUIT;
1661
1662   if (current_inferior ()->waiting_for_vfork_done)
1663     {
1664       /* Don't try to single-step a vfork parent that is waiting for
1665          the child to get out of the shared memory region (by exec'ing
1666          or exiting).  This is particularly important on software
1667          single-step archs, as the child process would trip on the
1668          software single step breakpoint inserted for the parent
1669          process.  Since the parent will not actually execute any
1670          instruction until the child is out of the shared region (such
1671          are vfork's semantics), it is safe to simply continue it.
1672          Eventually, we'll see a TARGET_WAITKIND_VFORK_DONE event for
1673          the parent, and tell it to `keep_going', which automatically
1674          re-sets it stepping.  */
1675       if (debug_infrun)
1676         fprintf_unfiltered (gdb_stdlog,
1677                             "infrun: resume : clear step\n");
1678       step = 0;
1679     }
1680
1681   if (debug_infrun)
1682     fprintf_unfiltered (gdb_stdlog,
1683                         "infrun: resume (step=%d, signal=%d), "
1684                         "trap_expected=%d, current thread [%s] at %s\n",
1685                         step, sig, tp->control.trap_expected,
1686                         target_pid_to_str (inferior_ptid),
1687                         paddress (gdbarch, pc));
1688
1689   /* Normally, by the time we reach `resume', the breakpoints are either
1690      removed or inserted, as appropriate.  The exception is if we're sitting
1691      at a permanent breakpoint; we need to step over it, but permanent
1692      breakpoints can't be removed.  So we have to test for it here.  */
1693   if (breakpoint_here_p (aspace, pc) == permanent_breakpoint_here)
1694     {
1695       if (gdbarch_skip_permanent_breakpoint_p (gdbarch))
1696         gdbarch_skip_permanent_breakpoint (gdbarch, regcache);
1697       else
1698         error (_("\
1699 The program is stopped at a permanent breakpoint, but GDB does not know\n\
1700 how to step past a permanent breakpoint on this architecture.  Try using\n\
1701 a command like `return' or `jump' to continue execution."));
1702     }
1703
1704   /* If enabled, step over breakpoints by executing a copy of the
1705      instruction at a different address.
1706
1707      We can't use displaced stepping when we have a signal to deliver;
1708      the comments for displaced_step_prepare explain why.  The
1709      comments in the handle_inferior event for dealing with 'random
1710      signals' explain what we do instead.
1711
1712      We can't use displaced stepping when we are waiting for vfork_done
1713      event, displaced stepping breaks the vfork child similarly as single
1714      step software breakpoint.  */
1715   if (use_displaced_stepping (gdbarch)
1716       && (tp->control.trap_expected
1717           || (step && gdbarch_software_single_step_p (gdbarch)))
1718       && sig == TARGET_SIGNAL_0
1719       && !current_inferior ()->waiting_for_vfork_done)
1720     {
1721       struct displaced_step_inferior_state *displaced;
1722
1723       if (!displaced_step_prepare (inferior_ptid))
1724         {
1725           /* Got placed in displaced stepping queue.  Will be resumed
1726              later when all the currently queued displaced stepping
1727              requests finish.  The thread is not executing at this point,
1728              and the call to set_executing will be made later.  But we
1729              need to call set_running here, since from frontend point of view,
1730              the thread is running.  */
1731           set_running (inferior_ptid, 1);
1732           discard_cleanups (old_cleanups);
1733           return;
1734         }
1735
1736       displaced = get_displaced_stepping_state (ptid_get_pid (inferior_ptid));
1737       step = gdbarch_displaced_step_hw_singlestep (gdbarch,
1738                                                    displaced->step_closure);
1739     }
1740
1741   /* Do we need to do it the hard way, w/temp breakpoints?  */
1742   else if (step)
1743     step = maybe_software_singlestep (gdbarch, pc);
1744
1745   /* Currently, our software single-step implementation leads to different
1746      results than hardware single-stepping in one situation: when stepping
1747      into delivering a signal which has an associated signal handler,
1748      hardware single-step will stop at the first instruction of the handler,
1749      while software single-step will simply skip execution of the handler.
1750
1751      For now, this difference in behavior is accepted since there is no
1752      easy way to actually implement single-stepping into a signal handler
1753      without kernel support.
1754
1755      However, there is one scenario where this difference leads to follow-on
1756      problems: if we're stepping off a breakpoint by removing all breakpoints
1757      and then single-stepping.  In this case, the software single-step
1758      behavior means that even if there is a *breakpoint* in the signal
1759      handler, GDB still would not stop.
1760
1761      Fortunately, we can at least fix this particular issue.  We detect
1762      here the case where we are about to deliver a signal while software
1763      single-stepping with breakpoints removed.  In this situation, we
1764      revert the decisions to remove all breakpoints and insert single-
1765      step breakpoints, and instead we install a step-resume breakpoint
1766      at the current address, deliver the signal without stepping, and
1767      once we arrive back at the step-resume breakpoint, actually step
1768      over the breakpoint we originally wanted to step over.  */
1769   if (singlestep_breakpoints_inserted_p
1770       && tp->control.trap_expected && sig != TARGET_SIGNAL_0)
1771     {
1772       /* If we have nested signals or a pending signal is delivered
1773          immediately after a handler returns, might might already have
1774          a step-resume breakpoint set on the earlier handler.  We cannot
1775          set another step-resume breakpoint; just continue on until the
1776          original breakpoint is hit.  */
1777       if (tp->control.step_resume_breakpoint == NULL)
1778         {
1779           insert_hp_step_resume_breakpoint_at_frame (get_current_frame ());
1780           tp->step_after_step_resume_breakpoint = 1;
1781         }
1782
1783       remove_single_step_breakpoints ();
1784       singlestep_breakpoints_inserted_p = 0;
1785
1786       insert_breakpoints ();
1787       tp->control.trap_expected = 0;
1788     }
1789
1790   if (should_resume)
1791     {
1792       ptid_t resume_ptid;
1793
1794       /* If STEP is set, it's a request to use hardware stepping
1795          facilities.  But in that case, we should never
1796          use singlestep breakpoint.  */
1797       gdb_assert (!(singlestep_breakpoints_inserted_p && step));
1798
1799       /* Decide the set of threads to ask the target to resume.  Start
1800          by assuming everything will be resumed, than narrow the set
1801          by applying increasingly restricting conditions.  */
1802       resume_ptid = user_visible_resume_ptid (step);
1803
1804       /* Maybe resume a single thread after all.  */
1805       if (singlestep_breakpoints_inserted_p
1806           && stepping_past_singlestep_breakpoint)
1807         {
1808           /* The situation here is as follows.  In thread T1 we wanted to
1809              single-step.  Lacking hardware single-stepping we've
1810              set breakpoint at the PC of the next instruction -- call it
1811              P.  After resuming, we've hit that breakpoint in thread T2.
1812              Now we've removed original breakpoint, inserted breakpoint
1813              at P+1, and try to step to advance T2 past breakpoint.
1814              We need to step only T2, as if T1 is allowed to freely run,
1815              it can run past P, and if other threads are allowed to run,
1816              they can hit breakpoint at P+1, and nested hits of single-step
1817              breakpoints is not something we'd want -- that's complicated
1818              to support, and has no value.  */
1819           resume_ptid = inferior_ptid;
1820         }
1821       else if ((step || singlestep_breakpoints_inserted_p)
1822                && tp->control.trap_expected)
1823         {
1824           /* We're allowing a thread to run past a breakpoint it has
1825              hit, by single-stepping the thread with the breakpoint
1826              removed.  In which case, we need to single-step only this
1827              thread, and keep others stopped, as they can miss this
1828              breakpoint if allowed to run.
1829
1830              The current code actually removes all breakpoints when
1831              doing this, not just the one being stepped over, so if we
1832              let other threads run, we can actually miss any
1833              breakpoint, not just the one at PC.  */
1834           resume_ptid = inferior_ptid;
1835         }
1836
1837       if (gdbarch_cannot_step_breakpoint (gdbarch))
1838         {
1839           /* Most targets can step a breakpoint instruction, thus
1840              executing it normally.  But if this one cannot, just
1841              continue and we will hit it anyway.  */
1842           if (step && breakpoint_inserted_here_p (aspace, pc))
1843             step = 0;
1844         }
1845
1846       if (debug_displaced
1847           && use_displaced_stepping (gdbarch)
1848           && tp->control.trap_expected)
1849         {
1850           struct regcache *resume_regcache = get_thread_regcache (resume_ptid);
1851           struct gdbarch *resume_gdbarch = get_regcache_arch (resume_regcache);
1852           CORE_ADDR actual_pc = regcache_read_pc (resume_regcache);
1853           gdb_byte buf[4];
1854
1855           fprintf_unfiltered (gdb_stdlog, "displaced: run %s: ",
1856                               paddress (resume_gdbarch, actual_pc));
1857           read_memory (actual_pc, buf, sizeof (buf));
1858           displaced_step_dump_bytes (gdb_stdlog, buf, sizeof (buf));
1859         }
1860
1861       /* Install inferior's terminal modes.  */
1862       target_terminal_inferior ();
1863
1864       /* Avoid confusing the next resume, if the next stop/resume
1865          happens to apply to another thread.  */
1866       tp->suspend.stop_signal = TARGET_SIGNAL_0;
1867
1868       /* Advise target which signals may be handled silently.  If we have
1869          removed breakpoints because we are stepping over one (which can
1870          happen only if we are not using displaced stepping), we need to
1871          receive all signals to avoid accidentally skipping a breakpoint
1872          during execution of a signal handler.  */
1873       if ((step || singlestep_breakpoints_inserted_p)
1874           && tp->control.trap_expected
1875           && !use_displaced_stepping (gdbarch))
1876         target_pass_signals (0, NULL);
1877       else
1878         target_pass_signals ((int) TARGET_SIGNAL_LAST, signal_pass);
1879
1880       target_resume (resume_ptid, step, sig);
1881     }
1882
1883   discard_cleanups (old_cleanups);
1884 }
1885 \f
1886 /* Proceeding.  */
1887
1888 /* Clear out all variables saying what to do when inferior is continued.
1889    First do this, then set the ones you want, then call `proceed'.  */
1890
1891 static void
1892 clear_proceed_status_thread (struct thread_info *tp)
1893 {
1894   if (debug_infrun)
1895     fprintf_unfiltered (gdb_stdlog,
1896                         "infrun: clear_proceed_status_thread (%s)\n",
1897                         target_pid_to_str (tp->ptid));
1898
1899   tp->control.trap_expected = 0;
1900   tp->control.step_range_start = 0;
1901   tp->control.step_range_end = 0;
1902   tp->control.step_frame_id = null_frame_id;
1903   tp->control.step_stack_frame_id = null_frame_id;
1904   tp->control.step_over_calls = STEP_OVER_UNDEBUGGABLE;
1905   tp->stop_requested = 0;
1906
1907   tp->control.stop_step = 0;
1908
1909   tp->control.proceed_to_finish = 0;
1910
1911   /* Discard any remaining commands or status from previous stop.  */
1912   bpstat_clear (&tp->control.stop_bpstat);
1913 }
1914
1915 static int
1916 clear_proceed_status_callback (struct thread_info *tp, void *data)
1917 {
1918   if (is_exited (tp->ptid))
1919     return 0;
1920
1921   clear_proceed_status_thread (tp);
1922   return 0;
1923 }
1924
1925 void
1926 clear_proceed_status (void)
1927 {
1928   if (!non_stop)
1929     {
1930       /* In all-stop mode, delete the per-thread status of all
1931          threads, even if inferior_ptid is null_ptid, there may be
1932          threads on the list.  E.g., we may be launching a new
1933          process, while selecting the executable.  */
1934       iterate_over_threads (clear_proceed_status_callback, NULL);
1935     }
1936
1937   if (!ptid_equal (inferior_ptid, null_ptid))
1938     {
1939       struct inferior *inferior;
1940
1941       if (non_stop)
1942         {
1943           /* If in non-stop mode, only delete the per-thread status of
1944              the current thread.  */
1945           clear_proceed_status_thread (inferior_thread ());
1946         }
1947
1948       inferior = current_inferior ();
1949       inferior->control.stop_soon = NO_STOP_QUIETLY;
1950     }
1951
1952   stop_after_trap = 0;
1953
1954   observer_notify_about_to_proceed ();
1955
1956   if (stop_registers)
1957     {
1958       regcache_xfree (stop_registers);
1959       stop_registers = NULL;
1960     }
1961 }
1962
1963 /* Check the current thread against the thread that reported the most recent
1964    event.  If a step-over is required return TRUE and set the current thread
1965    to the old thread.  Otherwise return FALSE.
1966
1967    This should be suitable for any targets that support threads.  */
1968
1969 static int
1970 prepare_to_proceed (int step)
1971 {
1972   ptid_t wait_ptid;
1973   struct target_waitstatus wait_status;
1974   int schedlock_enabled;
1975
1976   /* With non-stop mode on, threads are always handled individually.  */
1977   gdb_assert (! non_stop);
1978
1979   /* Get the last target status returned by target_wait().  */
1980   get_last_target_status (&wait_ptid, &wait_status);
1981
1982   /* Make sure we were stopped at a breakpoint.  */
1983   if (wait_status.kind != TARGET_WAITKIND_STOPPED
1984       || (wait_status.value.sig != TARGET_SIGNAL_TRAP
1985           && wait_status.value.sig != TARGET_SIGNAL_ILL
1986           && wait_status.value.sig != TARGET_SIGNAL_SEGV
1987           && wait_status.value.sig != TARGET_SIGNAL_EMT))
1988     {
1989       return 0;
1990     }
1991
1992   schedlock_enabled = (scheduler_mode == schedlock_on
1993                        || (scheduler_mode == schedlock_step
1994                            && step));
1995
1996   /* Don't switch over to WAIT_PTID if scheduler locking is on.  */
1997   if (schedlock_enabled)
1998     return 0;
1999
2000   /* Don't switch over if we're about to resume some other process
2001      other than WAIT_PTID's, and schedule-multiple is off.  */
2002   if (!sched_multi
2003       && ptid_get_pid (wait_ptid) != ptid_get_pid (inferior_ptid))
2004     return 0;
2005
2006   /* Switched over from WAIT_PID.  */
2007   if (!ptid_equal (wait_ptid, minus_one_ptid)
2008       && !ptid_equal (inferior_ptid, wait_ptid))
2009     {
2010       struct regcache *regcache = get_thread_regcache (wait_ptid);
2011
2012       if (breakpoint_here_p (get_regcache_aspace (regcache),
2013                              regcache_read_pc (regcache)))
2014         {
2015           /* If stepping, remember current thread to switch back to.  */
2016           if (step)
2017             deferred_step_ptid = inferior_ptid;
2018
2019           /* Switch back to WAIT_PID thread.  */
2020           switch_to_thread (wait_ptid);
2021
2022           if (debug_infrun)
2023             fprintf_unfiltered (gdb_stdlog,
2024                                 "infrun: prepare_to_proceed (step=%d), "
2025                                 "switched to [%s]\n",
2026                                 step, target_pid_to_str (inferior_ptid));
2027
2028           /* We return 1 to indicate that there is a breakpoint here,
2029              so we need to step over it before continuing to avoid
2030              hitting it straight away.  */
2031           return 1;
2032         }
2033     }
2034
2035   return 0;
2036 }
2037
2038 /* Basic routine for continuing the program in various fashions.
2039
2040    ADDR is the address to resume at, or -1 for resume where stopped.
2041    SIGGNAL is the signal to give it, or 0 for none,
2042    or -1 for act according to how it stopped.
2043    STEP is nonzero if should trap after one instruction.
2044    -1 means return after that and print nothing.
2045    You should probably set various step_... variables
2046    before calling here, if you are stepping.
2047
2048    You should call clear_proceed_status before calling proceed.  */
2049
2050 void
2051 proceed (CORE_ADDR addr, enum target_signal siggnal, int step)
2052 {
2053   struct regcache *regcache;
2054   struct gdbarch *gdbarch;
2055   struct thread_info *tp;
2056   CORE_ADDR pc;
2057   struct address_space *aspace;
2058   int oneproc = 0;
2059
2060   /* If we're stopped at a fork/vfork, follow the branch set by the
2061      "set follow-fork-mode" command; otherwise, we'll just proceed
2062      resuming the current thread.  */
2063   if (!follow_fork ())
2064     {
2065       /* The target for some reason decided not to resume.  */
2066       normal_stop ();
2067       if (target_can_async_p ())
2068         inferior_event_handler (INF_EXEC_COMPLETE, NULL);
2069       return;
2070     }
2071
2072   /* We'll update this if & when we switch to a new thread.  */
2073   previous_inferior_ptid = inferior_ptid;
2074
2075   regcache = get_current_regcache ();
2076   gdbarch = get_regcache_arch (regcache);
2077   aspace = get_regcache_aspace (regcache);
2078   pc = regcache_read_pc (regcache);
2079
2080   if (step > 0)
2081     step_start_function = find_pc_function (pc);
2082   if (step < 0)
2083     stop_after_trap = 1;
2084
2085   if (addr == (CORE_ADDR) -1)
2086     {
2087       if (pc == stop_pc && breakpoint_here_p (aspace, pc)
2088           && execution_direction != EXEC_REVERSE)
2089         /* There is a breakpoint at the address we will resume at,
2090            step one instruction before inserting breakpoints so that
2091            we do not stop right away (and report a second hit at this
2092            breakpoint).
2093
2094            Note, we don't do this in reverse, because we won't
2095            actually be executing the breakpoint insn anyway.
2096            We'll be (un-)executing the previous instruction.  */
2097
2098         oneproc = 1;
2099       else if (gdbarch_single_step_through_delay_p (gdbarch)
2100                && gdbarch_single_step_through_delay (gdbarch,
2101                                                      get_current_frame ()))
2102         /* We stepped onto an instruction that needs to be stepped
2103            again before re-inserting the breakpoint, do so.  */
2104         oneproc = 1;
2105     }
2106   else
2107     {
2108       regcache_write_pc (regcache, addr);
2109     }
2110
2111   if (debug_infrun)
2112     fprintf_unfiltered (gdb_stdlog,
2113                         "infrun: proceed (addr=%s, signal=%d, step=%d)\n",
2114                         paddress (gdbarch, addr), siggnal, step);
2115
2116   if (non_stop)
2117     /* In non-stop, each thread is handled individually.  The context
2118        must already be set to the right thread here.  */
2119     ;
2120   else
2121     {
2122       /* In a multi-threaded task we may select another thread and
2123          then continue or step.
2124
2125          But if the old thread was stopped at a breakpoint, it will
2126          immediately cause another breakpoint stop without any
2127          execution (i.e. it will report a breakpoint hit incorrectly).
2128          So we must step over it first.
2129
2130          prepare_to_proceed checks the current thread against the
2131          thread that reported the most recent event.  If a step-over
2132          is required it returns TRUE and sets the current thread to
2133          the old thread.  */
2134       if (prepare_to_proceed (step))
2135         oneproc = 1;
2136     }
2137
2138   /* prepare_to_proceed may change the current thread.  */
2139   tp = inferior_thread ();
2140
2141   if (oneproc)
2142     {
2143       tp->control.trap_expected = 1;
2144       /* If displaced stepping is enabled, we can step over the
2145          breakpoint without hitting it, so leave all breakpoints
2146          inserted.  Otherwise we need to disable all breakpoints, step
2147          one instruction, and then re-add them when that step is
2148          finished.  */
2149       if (!use_displaced_stepping (gdbarch))
2150         remove_breakpoints ();
2151     }
2152
2153   /* We can insert breakpoints if we're not trying to step over one,
2154      or if we are stepping over one but we're using displaced stepping
2155      to do so.  */
2156   if (! tp->control.trap_expected || use_displaced_stepping (gdbarch))
2157     insert_breakpoints ();
2158
2159   if (!non_stop)
2160     {
2161       /* Pass the last stop signal to the thread we're resuming,
2162          irrespective of whether the current thread is the thread that
2163          got the last event or not.  This was historically GDB's
2164          behaviour before keeping a stop_signal per thread.  */
2165
2166       struct thread_info *last_thread;
2167       ptid_t last_ptid;
2168       struct target_waitstatus last_status;
2169
2170       get_last_target_status (&last_ptid, &last_status);
2171       if (!ptid_equal (inferior_ptid, last_ptid)
2172           && !ptid_equal (last_ptid, null_ptid)
2173           && !ptid_equal (last_ptid, minus_one_ptid))
2174         {
2175           last_thread = find_thread_ptid (last_ptid);
2176           if (last_thread)
2177             {
2178               tp->suspend.stop_signal = last_thread->suspend.stop_signal;
2179               last_thread->suspend.stop_signal = TARGET_SIGNAL_0;
2180             }
2181         }
2182     }
2183
2184   if (siggnal != TARGET_SIGNAL_DEFAULT)
2185     tp->suspend.stop_signal = siggnal;
2186   /* If this signal should not be seen by program,
2187      give it zero.  Used for debugging signals.  */
2188   else if (!signal_program[tp->suspend.stop_signal])
2189     tp->suspend.stop_signal = TARGET_SIGNAL_0;
2190
2191   annotate_starting ();
2192
2193   /* Make sure that output from GDB appears before output from the
2194      inferior.  */
2195   gdb_flush (gdb_stdout);
2196
2197   /* Refresh prev_pc value just prior to resuming.  This used to be
2198      done in stop_stepping, however, setting prev_pc there did not handle
2199      scenarios such as inferior function calls or returning from
2200      a function via the return command.  In those cases, the prev_pc
2201      value was not set properly for subsequent commands.  The prev_pc value 
2202      is used to initialize the starting line number in the ecs.  With an 
2203      invalid value, the gdb next command ends up stopping at the position
2204      represented by the next line table entry past our start position.
2205      On platforms that generate one line table entry per line, this
2206      is not a problem.  However, on the ia64, the compiler generates
2207      extraneous line table entries that do not increase the line number.
2208      When we issue the gdb next command on the ia64 after an inferior call
2209      or a return command, we often end up a few instructions forward, still 
2210      within the original line we started.
2211
2212      An attempt was made to refresh the prev_pc at the same time the
2213      execution_control_state is initialized (for instance, just before
2214      waiting for an inferior event).  But this approach did not work
2215      because of platforms that use ptrace, where the pc register cannot
2216      be read unless the inferior is stopped.  At that point, we are not
2217      guaranteed the inferior is stopped and so the regcache_read_pc() call
2218      can fail.  Setting the prev_pc value here ensures the value is updated
2219      correctly when the inferior is stopped.  */
2220   tp->prev_pc = regcache_read_pc (get_current_regcache ());
2221
2222   /* Fill in with reasonable starting values.  */
2223   init_thread_stepping_state (tp);
2224
2225   /* Reset to normal state.  */
2226   init_infwait_state ();
2227
2228   /* Resume inferior.  */
2229   resume (oneproc || step || bpstat_should_step (), tp->suspend.stop_signal);
2230
2231   /* Wait for it to stop (if not standalone)
2232      and in any case decode why it stopped, and act accordingly.  */
2233   /* Do this only if we are not using the event loop, or if the target
2234      does not support asynchronous execution.  */
2235   if (!target_can_async_p ())
2236     {
2237       wait_for_inferior ();
2238       normal_stop ();
2239     }
2240 }
2241 \f
2242
2243 /* Start remote-debugging of a machine over a serial link.  */
2244
2245 void
2246 start_remote (int from_tty)
2247 {
2248   struct inferior *inferior;
2249
2250   inferior = current_inferior ();
2251   inferior->control.stop_soon = STOP_QUIETLY_REMOTE;
2252
2253   /* Always go on waiting for the target, regardless of the mode.  */
2254   /* FIXME: cagney/1999-09-23: At present it isn't possible to
2255      indicate to wait_for_inferior that a target should timeout if
2256      nothing is returned (instead of just blocking).  Because of this,
2257      targets expecting an immediate response need to, internally, set
2258      things up so that the target_wait() is forced to eventually
2259      timeout.  */
2260   /* FIXME: cagney/1999-09-24: It isn't possible for target_open() to
2261      differentiate to its caller what the state of the target is after
2262      the initial open has been performed.  Here we're assuming that
2263      the target has stopped.  It should be possible to eventually have
2264      target_open() return to the caller an indication that the target
2265      is currently running and GDB state should be set to the same as
2266      for an async run.  */
2267   wait_for_inferior ();
2268
2269   /* Now that the inferior has stopped, do any bookkeeping like
2270      loading shared libraries.  We want to do this before normal_stop,
2271      so that the displayed frame is up to date.  */
2272   post_create_inferior (&current_target, from_tty);
2273
2274   normal_stop ();
2275 }
2276
2277 /* Initialize static vars when a new inferior begins.  */
2278
2279 void
2280 init_wait_for_inferior (void)
2281 {
2282   /* These are meaningless until the first time through wait_for_inferior.  */
2283
2284   breakpoint_init_inferior (inf_starting);
2285
2286   clear_proceed_status ();
2287
2288   stepping_past_singlestep_breakpoint = 0;
2289   deferred_step_ptid = null_ptid;
2290
2291   target_last_wait_ptid = minus_one_ptid;
2292
2293   previous_inferior_ptid = inferior_ptid;
2294   init_infwait_state ();
2295
2296   /* Discard any skipped inlined frames.  */
2297   clear_inline_frame_state (minus_one_ptid);
2298 }
2299
2300 \f
2301 /* This enum encodes possible reasons for doing a target_wait, so that
2302    wfi can call target_wait in one place.  (Ultimately the call will be
2303    moved out of the infinite loop entirely.) */
2304
2305 enum infwait_states
2306 {
2307   infwait_normal_state,
2308   infwait_thread_hop_state,
2309   infwait_step_watch_state,
2310   infwait_nonstep_watch_state
2311 };
2312
2313 /* The PTID we'll do a target_wait on.*/
2314 ptid_t waiton_ptid;
2315
2316 /* Current inferior wait state.  */
2317 enum infwait_states infwait_state;
2318
2319 /* Data to be passed around while handling an event.  This data is
2320    discarded between events.  */
2321 struct execution_control_state
2322 {
2323   ptid_t ptid;
2324   /* The thread that got the event, if this was a thread event; NULL
2325      otherwise.  */
2326   struct thread_info *event_thread;
2327
2328   struct target_waitstatus ws;
2329   int random_signal;
2330   int stop_func_filled_in;
2331   CORE_ADDR stop_func_start;
2332   CORE_ADDR stop_func_end;
2333   char *stop_func_name;
2334   int new_thread_event;
2335   int wait_some_more;
2336 };
2337
2338 static void handle_inferior_event (struct execution_control_state *ecs);
2339
2340 static void handle_step_into_function (struct gdbarch *gdbarch,
2341                                        struct execution_control_state *ecs);
2342 static void handle_step_into_function_backward (struct gdbarch *gdbarch,
2343                                                 struct execution_control_state *ecs);
2344 static void check_exception_resume (struct execution_control_state *,
2345                                     struct frame_info *, struct symbol *);
2346
2347 static void stop_stepping (struct execution_control_state *ecs);
2348 static void prepare_to_wait (struct execution_control_state *ecs);
2349 static void keep_going (struct execution_control_state *ecs);
2350
2351 /* Callback for iterate over threads.  If the thread is stopped, but
2352    the user/frontend doesn't know about that yet, go through
2353    normal_stop, as if the thread had just stopped now.  ARG points at
2354    a ptid.  If PTID is MINUS_ONE_PTID, applies to all threads.  If
2355    ptid_is_pid(PTID) is true, applies to all threads of the process
2356    pointed at by PTID.  Otherwise, apply only to the thread pointed by
2357    PTID.  */
2358
2359 static int
2360 infrun_thread_stop_requested_callback (struct thread_info *info, void *arg)
2361 {
2362   ptid_t ptid = * (ptid_t *) arg;
2363
2364   if ((ptid_equal (info->ptid, ptid)
2365        || ptid_equal (minus_one_ptid, ptid)
2366        || (ptid_is_pid (ptid)
2367            && ptid_get_pid (ptid) == ptid_get_pid (info->ptid)))
2368       && is_running (info->ptid)
2369       && !is_executing (info->ptid))
2370     {
2371       struct cleanup *old_chain;
2372       struct execution_control_state ecss;
2373       struct execution_control_state *ecs = &ecss;
2374
2375       memset (ecs, 0, sizeof (*ecs));
2376
2377       old_chain = make_cleanup_restore_current_thread ();
2378
2379       switch_to_thread (info->ptid);
2380
2381       /* Go through handle_inferior_event/normal_stop, so we always
2382          have consistent output as if the stop event had been
2383          reported.  */
2384       ecs->ptid = info->ptid;
2385       ecs->event_thread = find_thread_ptid (info->ptid);
2386       ecs->ws.kind = TARGET_WAITKIND_STOPPED;
2387       ecs->ws.value.sig = TARGET_SIGNAL_0;
2388
2389       handle_inferior_event (ecs);
2390
2391       if (!ecs->wait_some_more)
2392         {
2393           struct thread_info *tp;
2394
2395           normal_stop ();
2396
2397           /* Finish off the continuations.  */
2398           tp = inferior_thread ();
2399           do_all_intermediate_continuations_thread (tp, 1);
2400           do_all_continuations_thread (tp, 1);
2401         }
2402
2403       do_cleanups (old_chain);
2404     }
2405
2406   return 0;
2407 }
2408
2409 /* This function is attached as a "thread_stop_requested" observer.
2410    Cleanup local state that assumed the PTID was to be resumed, and
2411    report the stop to the frontend.  */
2412
2413 static void
2414 infrun_thread_stop_requested (ptid_t ptid)
2415 {
2416   struct displaced_step_inferior_state *displaced;
2417
2418   /* PTID was requested to stop.  Remove it from the displaced
2419      stepping queue, so we don't try to resume it automatically.  */
2420
2421   for (displaced = displaced_step_inferior_states;
2422        displaced;
2423        displaced = displaced->next)
2424     {
2425       struct displaced_step_request *it, **prev_next_p;
2426
2427       it = displaced->step_request_queue;
2428       prev_next_p = &displaced->step_request_queue;
2429       while (it)
2430         {
2431           if (ptid_match (it->ptid, ptid))
2432             {
2433               *prev_next_p = it->next;
2434               it->next = NULL;
2435               xfree (it);
2436             }
2437           else
2438             {
2439               prev_next_p = &it->next;
2440             }
2441
2442           it = *prev_next_p;
2443         }
2444     }
2445
2446   iterate_over_threads (infrun_thread_stop_requested_callback, &ptid);
2447 }
2448
2449 static void
2450 infrun_thread_thread_exit (struct thread_info *tp, int silent)
2451 {
2452   if (ptid_equal (target_last_wait_ptid, tp->ptid))
2453     nullify_last_target_wait_ptid ();
2454 }
2455
2456 /* Callback for iterate_over_threads.  */
2457
2458 static int
2459 delete_step_resume_breakpoint_callback (struct thread_info *info, void *data)
2460 {
2461   if (is_exited (info->ptid))
2462     return 0;
2463
2464   delete_step_resume_breakpoint (info);
2465   delete_exception_resume_breakpoint (info);
2466   return 0;
2467 }
2468
2469 /* In all-stop, delete the step resume breakpoint of any thread that
2470    had one.  In non-stop, delete the step resume breakpoint of the
2471    thread that just stopped.  */
2472
2473 static void
2474 delete_step_thread_step_resume_breakpoint (void)
2475 {
2476   if (!target_has_execution
2477       || ptid_equal (inferior_ptid, null_ptid))
2478     /* If the inferior has exited, we have already deleted the step
2479        resume breakpoints out of GDB's lists.  */
2480     return;
2481
2482   if (non_stop)
2483     {
2484       /* If in non-stop mode, only delete the step-resume or
2485          longjmp-resume breakpoint of the thread that just stopped
2486          stepping.  */
2487       struct thread_info *tp = inferior_thread ();
2488
2489       delete_step_resume_breakpoint (tp);
2490       delete_exception_resume_breakpoint (tp);
2491     }
2492   else
2493     /* In all-stop mode, delete all step-resume and longjmp-resume
2494        breakpoints of any thread that had them.  */
2495     iterate_over_threads (delete_step_resume_breakpoint_callback, NULL);
2496 }
2497
2498 /* A cleanup wrapper.  */
2499
2500 static void
2501 delete_step_thread_step_resume_breakpoint_cleanup (void *arg)
2502 {
2503   delete_step_thread_step_resume_breakpoint ();
2504 }
2505
2506 /* Pretty print the results of target_wait, for debugging purposes.  */
2507
2508 static void
2509 print_target_wait_results (ptid_t waiton_ptid, ptid_t result_ptid,
2510                            const struct target_waitstatus *ws)
2511 {
2512   char *status_string = target_waitstatus_to_string (ws);
2513   struct ui_file *tmp_stream = mem_fileopen ();
2514   char *text;
2515
2516   /* The text is split over several lines because it was getting too long.
2517      Call fprintf_unfiltered (gdb_stdlog) once so that the text is still
2518      output as a unit; we want only one timestamp printed if debug_timestamp
2519      is set.  */
2520
2521   fprintf_unfiltered (tmp_stream,
2522                       "infrun: target_wait (%d", PIDGET (waiton_ptid));
2523   if (PIDGET (waiton_ptid) != -1)
2524     fprintf_unfiltered (tmp_stream,
2525                         " [%s]", target_pid_to_str (waiton_ptid));
2526   fprintf_unfiltered (tmp_stream, ", status) =\n");
2527   fprintf_unfiltered (tmp_stream,
2528                       "infrun:   %d [%s],\n",
2529                       PIDGET (result_ptid), target_pid_to_str (result_ptid));
2530   fprintf_unfiltered (tmp_stream,
2531                       "infrun:   %s\n",
2532                       status_string);
2533
2534   text = ui_file_xstrdup (tmp_stream, NULL);
2535
2536   /* This uses %s in part to handle %'s in the text, but also to avoid
2537      a gcc error: the format attribute requires a string literal.  */
2538   fprintf_unfiltered (gdb_stdlog, "%s", text);
2539
2540   xfree (status_string);
2541   xfree (text);
2542   ui_file_delete (tmp_stream);
2543 }
2544
2545 /* Prepare and stabilize the inferior for detaching it.  E.g.,
2546    detaching while a thread is displaced stepping is a recipe for
2547    crashing it, as nothing would readjust the PC out of the scratch
2548    pad.  */
2549
2550 void
2551 prepare_for_detach (void)
2552 {
2553   struct inferior *inf = current_inferior ();
2554   ptid_t pid_ptid = pid_to_ptid (inf->pid);
2555   struct cleanup *old_chain_1;
2556   struct displaced_step_inferior_state *displaced;
2557
2558   displaced = get_displaced_stepping_state (inf->pid);
2559
2560   /* Is any thread of this process displaced stepping?  If not,
2561      there's nothing else to do.  */
2562   if (displaced == NULL || ptid_equal (displaced->step_ptid, null_ptid))
2563     return;
2564
2565   if (debug_infrun)
2566     fprintf_unfiltered (gdb_stdlog,
2567                         "displaced-stepping in-process while detaching");
2568
2569   old_chain_1 = make_cleanup_restore_integer (&inf->detaching);
2570   inf->detaching = 1;
2571
2572   while (!ptid_equal (displaced->step_ptid, null_ptid))
2573     {
2574       struct cleanup *old_chain_2;
2575       struct execution_control_state ecss;
2576       struct execution_control_state *ecs;
2577
2578       ecs = &ecss;
2579       memset (ecs, 0, sizeof (*ecs));
2580
2581       overlay_cache_invalid = 1;
2582
2583       /* We have to invalidate the registers BEFORE calling
2584          target_wait because they can be loaded from the target while
2585          in target_wait.  This makes remote debugging a bit more
2586          efficient for those targets that provide critical registers
2587          as part of their normal status mechanism.  */
2588
2589       registers_changed ();
2590
2591       if (deprecated_target_wait_hook)
2592         ecs->ptid = deprecated_target_wait_hook (pid_ptid, &ecs->ws, 0);
2593       else
2594         ecs->ptid = target_wait (pid_ptid, &ecs->ws, 0);
2595
2596       if (debug_infrun)
2597         print_target_wait_results (pid_ptid, ecs->ptid, &ecs->ws);
2598
2599       /* If an error happens while handling the event, propagate GDB's
2600          knowledge of the executing state to the frontend/user running
2601          state.  */
2602       old_chain_2 = make_cleanup (finish_thread_state_cleanup,
2603                                   &minus_one_ptid);
2604
2605       /* In non-stop mode, each thread is handled individually.
2606          Switch early, so the global state is set correctly for this
2607          thread.  */
2608       if (non_stop
2609           && ecs->ws.kind != TARGET_WAITKIND_EXITED
2610           && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED)
2611         context_switch (ecs->ptid);
2612
2613       /* Now figure out what to do with the result of the result.  */
2614       handle_inferior_event (ecs);
2615
2616       /* No error, don't finish the state yet.  */
2617       discard_cleanups (old_chain_2);
2618
2619       /* Breakpoints and watchpoints are not installed on the target
2620          at this point, and signals are passed directly to the
2621          inferior, so this must mean the process is gone.  */
2622       if (!ecs->wait_some_more)
2623         {
2624           discard_cleanups (old_chain_1);
2625           error (_("Program exited while detaching"));
2626         }
2627     }
2628
2629   discard_cleanups (old_chain_1);
2630 }
2631
2632 /* Wait for control to return from inferior to debugger.
2633
2634    If inferior gets a signal, we may decide to start it up again
2635    instead of returning.  That is why there is a loop in this function.
2636    When this function actually returns it means the inferior
2637    should be left stopped and GDB should read more commands.  */
2638
2639 void
2640 wait_for_inferior (void)
2641 {
2642   struct cleanup *old_cleanups;
2643   struct execution_control_state ecss;
2644   struct execution_control_state *ecs;
2645
2646   if (debug_infrun)
2647     fprintf_unfiltered
2648       (gdb_stdlog, "infrun: wait_for_inferior ()\n");
2649
2650   old_cleanups =
2651     make_cleanup (delete_step_thread_step_resume_breakpoint_cleanup, NULL);
2652
2653   ecs = &ecss;
2654   memset (ecs, 0, sizeof (*ecs));
2655
2656   while (1)
2657     {
2658       struct cleanup *old_chain;
2659
2660       /* We have to invalidate the registers BEFORE calling target_wait
2661          because they can be loaded from the target while in target_wait.
2662          This makes remote debugging a bit more efficient for those
2663          targets that provide critical registers as part of their normal
2664          status mechanism.  */
2665
2666       overlay_cache_invalid = 1;
2667       registers_changed ();
2668
2669       if (deprecated_target_wait_hook)
2670         ecs->ptid = deprecated_target_wait_hook (waiton_ptid, &ecs->ws, 0);
2671       else
2672         ecs->ptid = target_wait (waiton_ptid, &ecs->ws, 0);
2673
2674       if (debug_infrun)
2675         print_target_wait_results (waiton_ptid, ecs->ptid, &ecs->ws);
2676
2677       /* If an error happens while handling the event, propagate GDB's
2678          knowledge of the executing state to the frontend/user running
2679          state.  */
2680       old_chain = make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
2681
2682       if (ecs->ws.kind == TARGET_WAITKIND_SYSCALL_ENTRY
2683           || ecs->ws.kind == TARGET_WAITKIND_SYSCALL_RETURN)
2684         ecs->ws.value.syscall_number = UNKNOWN_SYSCALL;
2685
2686       /* Now figure out what to do with the result of the result.  */
2687       handle_inferior_event (ecs);
2688
2689       /* No error, don't finish the state yet.  */
2690       discard_cleanups (old_chain);
2691
2692       if (!ecs->wait_some_more)
2693         break;
2694     }
2695
2696   do_cleanups (old_cleanups);
2697 }
2698
2699 /* Asynchronous version of wait_for_inferior.  It is called by the
2700    event loop whenever a change of state is detected on the file
2701    descriptor corresponding to the target.  It can be called more than
2702    once to complete a single execution command.  In such cases we need
2703    to keep the state in a global variable ECSS.  If it is the last time
2704    that this function is called for a single execution command, then
2705    report to the user that the inferior has stopped, and do the
2706    necessary cleanups.  */
2707
2708 void
2709 fetch_inferior_event (void *client_data)
2710 {
2711   struct execution_control_state ecss;
2712   struct execution_control_state *ecs = &ecss;
2713   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
2714   struct cleanup *ts_old_chain;
2715   int was_sync = sync_execution;
2716
2717   memset (ecs, 0, sizeof (*ecs));
2718
2719   /* We're handling a live event, so make sure we're doing live
2720      debugging.  If we're looking at traceframes while the target is
2721      running, we're going to need to get back to that mode after
2722      handling the event.  */
2723   if (non_stop)
2724     {
2725       make_cleanup_restore_current_traceframe ();
2726       set_current_traceframe (-1);
2727     }
2728
2729   if (non_stop)
2730     /* In non-stop mode, the user/frontend should not notice a thread
2731        switch due to internal events.  Make sure we reverse to the
2732        user selected thread and frame after handling the event and
2733        running any breakpoint commands.  */
2734     make_cleanup_restore_current_thread ();
2735
2736   /* We have to invalidate the registers BEFORE calling target_wait
2737      because they can be loaded from the target while in target_wait.
2738      This makes remote debugging a bit more efficient for those
2739      targets that provide critical registers as part of their normal
2740      status mechanism.  */
2741
2742   overlay_cache_invalid = 1;
2743
2744   /* But don't do it if the current thread is already stopped (hence
2745      this is either a delayed event that will result in
2746      TARGET_WAITKIND_IGNORE, or it's an event for another thread (and
2747      we always clear the register and frame caches when the user
2748      switches threads anyway).  If we didn't do this, a spurious
2749      delayed event in all-stop mode would make the user lose the
2750      selected frame.  */
2751   if (non_stop || is_executing (inferior_ptid))
2752     registers_changed ();
2753
2754   make_cleanup_restore_integer (&execution_direction);
2755   execution_direction = target_execution_direction ();
2756
2757   if (deprecated_target_wait_hook)
2758     ecs->ptid =
2759       deprecated_target_wait_hook (waiton_ptid, &ecs->ws, TARGET_WNOHANG);
2760   else
2761     ecs->ptid = target_wait (waiton_ptid, &ecs->ws, TARGET_WNOHANG);
2762
2763   if (debug_infrun)
2764     print_target_wait_results (waiton_ptid, ecs->ptid, &ecs->ws);
2765
2766   if (non_stop
2767       && ecs->ws.kind != TARGET_WAITKIND_IGNORE
2768       && ecs->ws.kind != TARGET_WAITKIND_EXITED
2769       && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED)
2770     /* In non-stop mode, each thread is handled individually.  Switch
2771        early, so the global state is set correctly for this
2772        thread.  */
2773     context_switch (ecs->ptid);
2774
2775   /* If an error happens while handling the event, propagate GDB's
2776      knowledge of the executing state to the frontend/user running
2777      state.  */
2778   if (!non_stop)
2779     ts_old_chain = make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
2780   else
2781     ts_old_chain = make_cleanup (finish_thread_state_cleanup, &ecs->ptid);
2782
2783   /* Get executed before make_cleanup_restore_current_thread above to apply
2784      still for the thread which has thrown the exception.  */
2785   make_bpstat_clear_actions_cleanup ();
2786
2787   /* Now figure out what to do with the result of the result.  */
2788   handle_inferior_event (ecs);
2789
2790   if (!ecs->wait_some_more)
2791     {
2792       struct inferior *inf = find_inferior_pid (ptid_get_pid (ecs->ptid));
2793
2794       delete_step_thread_step_resume_breakpoint ();
2795
2796       /* We may not find an inferior if this was a process exit.  */
2797       if (inf == NULL || inf->control.stop_soon == NO_STOP_QUIETLY)
2798         normal_stop ();
2799
2800       if (target_has_execution
2801           && ecs->ws.kind != TARGET_WAITKIND_EXITED
2802           && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED
2803           && ecs->event_thread->step_multi
2804           && ecs->event_thread->control.stop_step)
2805         inferior_event_handler (INF_EXEC_CONTINUE, NULL);
2806       else
2807         inferior_event_handler (INF_EXEC_COMPLETE, NULL);
2808     }
2809
2810   /* No error, don't finish the thread states yet.  */
2811   discard_cleanups (ts_old_chain);
2812
2813   /* Revert thread and frame.  */
2814   do_cleanups (old_chain);
2815
2816   /* If the inferior was in sync execution mode, and now isn't,
2817      restore the prompt.  */
2818   if (interpreter_async && was_sync && !sync_execution)
2819     display_gdb_prompt (0);
2820 }
2821
2822 /* Record the frame and location we're currently stepping through.  */
2823 void
2824 set_step_info (struct frame_info *frame, struct symtab_and_line sal)
2825 {
2826   struct thread_info *tp = inferior_thread ();
2827
2828   tp->control.step_frame_id = get_frame_id (frame);
2829   tp->control.step_stack_frame_id = get_stack_frame_id (frame);
2830
2831   tp->current_symtab = sal.symtab;
2832   tp->current_line = sal.line;
2833 }
2834
2835 /* Clear context switchable stepping state.  */
2836
2837 void
2838 init_thread_stepping_state (struct thread_info *tss)
2839 {
2840   tss->stepping_over_breakpoint = 0;
2841   tss->step_after_step_resume_breakpoint = 0;
2842   tss->stepping_through_solib_after_catch = 0;
2843   tss->stepping_through_solib_catchpoints = NULL;
2844 }
2845
2846 /* Return the cached copy of the last pid/waitstatus returned by
2847    target_wait()/deprecated_target_wait_hook().  The data is actually
2848    cached by handle_inferior_event(), which gets called immediately
2849    after target_wait()/deprecated_target_wait_hook().  */
2850
2851 void
2852 get_last_target_status (ptid_t *ptidp, struct target_waitstatus *status)
2853 {
2854   *ptidp = target_last_wait_ptid;
2855   *status = target_last_waitstatus;
2856 }
2857
2858 void
2859 nullify_last_target_wait_ptid (void)
2860 {
2861   target_last_wait_ptid = minus_one_ptid;
2862 }
2863
2864 /* Switch thread contexts.  */
2865
2866 static void
2867 context_switch (ptid_t ptid)
2868 {
2869   if (debug_infrun)
2870     {
2871       fprintf_unfiltered (gdb_stdlog, "infrun: Switching context from %s ",
2872                           target_pid_to_str (inferior_ptid));
2873       fprintf_unfiltered (gdb_stdlog, "to %s\n",
2874                           target_pid_to_str (ptid));
2875     }
2876
2877   switch_to_thread (ptid);
2878 }
2879
2880 static void
2881 adjust_pc_after_break (struct execution_control_state *ecs)
2882 {
2883   struct regcache *regcache;
2884   struct gdbarch *gdbarch;
2885   struct address_space *aspace;
2886   CORE_ADDR breakpoint_pc;
2887
2888   /* If we've hit a breakpoint, we'll normally be stopped with SIGTRAP.  If
2889      we aren't, just return.
2890
2891      We assume that waitkinds other than TARGET_WAITKIND_STOPPED are not
2892      affected by gdbarch_decr_pc_after_break.  Other waitkinds which are
2893      implemented by software breakpoints should be handled through the normal
2894      breakpoint layer.
2895
2896      NOTE drow/2004-01-31: On some targets, breakpoints may generate
2897      different signals (SIGILL or SIGEMT for instance), but it is less
2898      clear where the PC is pointing afterwards.  It may not match
2899      gdbarch_decr_pc_after_break.  I don't know any specific target that
2900      generates these signals at breakpoints (the code has been in GDB since at
2901      least 1992) so I can not guess how to handle them here.
2902
2903      In earlier versions of GDB, a target with 
2904      gdbarch_have_nonsteppable_watchpoint would have the PC after hitting a
2905      watchpoint affected by gdbarch_decr_pc_after_break.  I haven't found any
2906      target with both of these set in GDB history, and it seems unlikely to be
2907      correct, so gdbarch_have_nonsteppable_watchpoint is not checked here.  */
2908
2909   if (ecs->ws.kind != TARGET_WAITKIND_STOPPED)
2910     return;
2911
2912   if (ecs->ws.value.sig != TARGET_SIGNAL_TRAP)
2913     return;
2914
2915   /* In reverse execution, when a breakpoint is hit, the instruction
2916      under it has already been de-executed.  The reported PC always
2917      points at the breakpoint address, so adjusting it further would
2918      be wrong.  E.g., consider this case on a decr_pc_after_break == 1
2919      architecture:
2920
2921        B1         0x08000000 :   INSN1
2922        B2         0x08000001 :   INSN2
2923                   0x08000002 :   INSN3
2924             PC -> 0x08000003 :   INSN4
2925
2926      Say you're stopped at 0x08000003 as above.  Reverse continuing
2927      from that point should hit B2 as below.  Reading the PC when the
2928      SIGTRAP is reported should read 0x08000001 and INSN2 should have
2929      been de-executed already.
2930
2931        B1         0x08000000 :   INSN1
2932        B2   PC -> 0x08000001 :   INSN2
2933                   0x08000002 :   INSN3
2934                   0x08000003 :   INSN4
2935
2936      We can't apply the same logic as for forward execution, because
2937      we would wrongly adjust the PC to 0x08000000, since there's a
2938      breakpoint at PC - 1.  We'd then report a hit on B1, although
2939      INSN1 hadn't been de-executed yet.  Doing nothing is the correct
2940      behaviour.  */
2941   if (execution_direction == EXEC_REVERSE)
2942     return;
2943
2944   /* If this target does not decrement the PC after breakpoints, then
2945      we have nothing to do.  */
2946   regcache = get_thread_regcache (ecs->ptid);
2947   gdbarch = get_regcache_arch (regcache);
2948   if (gdbarch_decr_pc_after_break (gdbarch) == 0)
2949     return;
2950
2951   aspace = get_regcache_aspace (regcache);
2952
2953   /* Find the location where (if we've hit a breakpoint) the
2954      breakpoint would be.  */
2955   breakpoint_pc = regcache_read_pc (regcache)
2956                   - gdbarch_decr_pc_after_break (gdbarch);
2957
2958   /* Check whether there actually is a software breakpoint inserted at
2959      that location.
2960
2961      If in non-stop mode, a race condition is possible where we've
2962      removed a breakpoint, but stop events for that breakpoint were
2963      already queued and arrive later.  To suppress those spurious
2964      SIGTRAPs, we keep a list of such breakpoint locations for a bit,
2965      and retire them after a number of stop events are reported.  */
2966   if (software_breakpoint_inserted_here_p (aspace, breakpoint_pc)
2967       || (non_stop && moribund_breakpoint_here_p (aspace, breakpoint_pc)))
2968     {
2969       struct cleanup *old_cleanups = NULL;
2970
2971       if (RECORD_IS_USED)
2972         old_cleanups = record_gdb_operation_disable_set ();
2973
2974       /* When using hardware single-step, a SIGTRAP is reported for both
2975          a completed single-step and a software breakpoint.  Need to
2976          differentiate between the two, as the latter needs adjusting
2977          but the former does not.
2978
2979          The SIGTRAP can be due to a completed hardware single-step only if 
2980           - we didn't insert software single-step breakpoints
2981           - the thread to be examined is still the current thread
2982           - this thread is currently being stepped
2983
2984          If any of these events did not occur, we must have stopped due
2985          to hitting a software breakpoint, and have to back up to the
2986          breakpoint address.
2987
2988          As a special case, we could have hardware single-stepped a
2989          software breakpoint.  In this case (prev_pc == breakpoint_pc),
2990          we also need to back up to the breakpoint address.  */
2991
2992       if (singlestep_breakpoints_inserted_p
2993           || !ptid_equal (ecs->ptid, inferior_ptid)
2994           || !currently_stepping (ecs->event_thread)
2995           || ecs->event_thread->prev_pc == breakpoint_pc)
2996         regcache_write_pc (regcache, breakpoint_pc);
2997
2998       if (RECORD_IS_USED)
2999         do_cleanups (old_cleanups);
3000     }
3001 }
3002
3003 void
3004 init_infwait_state (void)
3005 {
3006   waiton_ptid = pid_to_ptid (-1);
3007   infwait_state = infwait_normal_state;
3008 }
3009
3010 void
3011 error_is_running (void)
3012 {
3013   error (_("Cannot execute this command while "
3014            "the selected thread is running."));
3015 }
3016
3017 void
3018 ensure_not_running (void)
3019 {
3020   if (is_running (inferior_ptid))
3021     error_is_running ();
3022 }
3023
3024 static int
3025 stepped_in_from (struct frame_info *frame, struct frame_id step_frame_id)
3026 {
3027   for (frame = get_prev_frame (frame);
3028        frame != NULL;
3029        frame = get_prev_frame (frame))
3030     {
3031       if (frame_id_eq (get_frame_id (frame), step_frame_id))
3032         return 1;
3033       if (get_frame_type (frame) != INLINE_FRAME)
3034         break;
3035     }
3036
3037   return 0;
3038 }
3039
3040 /* Auxiliary function that handles syscall entry/return events.
3041    It returns 1 if the inferior should keep going (and GDB
3042    should ignore the event), or 0 if the event deserves to be
3043    processed.  */
3044
3045 static int
3046 handle_syscall_event (struct execution_control_state *ecs)
3047 {
3048   struct regcache *regcache;
3049   struct gdbarch *gdbarch;
3050   int syscall_number;
3051
3052   if (!ptid_equal (ecs->ptid, inferior_ptid))
3053     context_switch (ecs->ptid);
3054
3055   regcache = get_thread_regcache (ecs->ptid);
3056   gdbarch = get_regcache_arch (regcache);
3057   syscall_number = gdbarch_get_syscall_number (gdbarch, ecs->ptid);
3058   stop_pc = regcache_read_pc (regcache);
3059
3060   target_last_waitstatus.value.syscall_number = syscall_number;
3061
3062   if (catch_syscall_enabled () > 0
3063       && catching_syscall_number (syscall_number) > 0)
3064     {
3065       if (debug_infrun)
3066         fprintf_unfiltered (gdb_stdlog, "infrun: syscall number = '%d'\n",
3067                             syscall_number);
3068
3069       ecs->event_thread->control.stop_bpstat
3070         = bpstat_stop_status (get_regcache_aspace (regcache),
3071                               stop_pc, ecs->ptid);
3072       ecs->random_signal
3073         = !bpstat_explains_signal (ecs->event_thread->control.stop_bpstat);
3074
3075       if (!ecs->random_signal)
3076         {
3077           /* Catchpoint hit.  */
3078           ecs->event_thread->suspend.stop_signal = TARGET_SIGNAL_TRAP;
3079           return 0;
3080         }
3081     }
3082
3083   /* If no catchpoint triggered for this, then keep going.  */
3084   ecs->event_thread->suspend.stop_signal = TARGET_SIGNAL_0;
3085   keep_going (ecs);
3086   return 1;
3087 }
3088
3089 /* Clear the supplied execution_control_state's stop_func_* fields.  */
3090
3091 static void
3092 clear_stop_func (struct execution_control_state *ecs)
3093 {
3094   ecs->stop_func_filled_in = 0;
3095   ecs->stop_func_start = 0;
3096   ecs->stop_func_end = 0;
3097   ecs->stop_func_name = NULL;
3098 }
3099
3100 /* Lazily fill in the execution_control_state's stop_func_* fields.  */
3101
3102 static void
3103 fill_in_stop_func (struct gdbarch *gdbarch,
3104                    struct execution_control_state *ecs)
3105 {
3106   if (!ecs->stop_func_filled_in)
3107     {
3108       /* Don't care about return value; stop_func_start and stop_func_name
3109          will both be 0 if it doesn't work.  */
3110       find_pc_partial_function (stop_pc, &ecs->stop_func_name,
3111                                 &ecs->stop_func_start, &ecs->stop_func_end);
3112       ecs->stop_func_start
3113         += gdbarch_deprecated_function_start_offset (gdbarch);
3114
3115       ecs->stop_func_filled_in = 1;
3116     }
3117 }
3118
3119 /* Given an execution control state that has been freshly filled in
3120    by an event from the inferior, figure out what it means and take
3121    appropriate action.  */
3122
3123 static void
3124 handle_inferior_event (struct execution_control_state *ecs)
3125 {
3126   struct frame_info *frame;
3127   struct gdbarch *gdbarch;
3128   int stopped_by_watchpoint;
3129   int stepped_after_stopped_by_watchpoint = 0;
3130   struct symtab_and_line stop_pc_sal;
3131   enum stop_kind stop_soon;
3132
3133   if (ecs->ws.kind == TARGET_WAITKIND_IGNORE)
3134     {
3135       /* We had an event in the inferior, but we are not interested in
3136          handling it at this level.  The lower layers have already
3137          done what needs to be done, if anything.
3138
3139          One of the possible circumstances for this is when the
3140          inferior produces output for the console.  The inferior has
3141          not stopped, and we are ignoring the event.  Another possible
3142          circumstance is any event which the lower level knows will be
3143          reported multiple times without an intervening resume.  */
3144       if (debug_infrun)
3145         fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_IGNORE\n");
3146       prepare_to_wait (ecs);
3147       return;
3148     }
3149
3150   if (ecs->ws.kind != TARGET_WAITKIND_EXITED
3151       && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED)
3152     {
3153       struct inferior *inf = find_inferior_pid (ptid_get_pid (ecs->ptid));
3154
3155       gdb_assert (inf);
3156       stop_soon = inf->control.stop_soon;
3157     }
3158   else
3159     stop_soon = NO_STOP_QUIETLY;
3160
3161   /* Cache the last pid/waitstatus.  */
3162   target_last_wait_ptid = ecs->ptid;
3163   target_last_waitstatus = ecs->ws;
3164
3165   /* Always clear state belonging to the previous time we stopped.  */
3166   stop_stack_dummy = STOP_NONE;
3167
3168   /* If it's a new process, add it to the thread database.  */
3169
3170   ecs->new_thread_event = (!ptid_equal (ecs->ptid, inferior_ptid)
3171                            && !ptid_equal (ecs->ptid, minus_one_ptid)
3172                            && !in_thread_list (ecs->ptid));
3173
3174   if (ecs->ws.kind != TARGET_WAITKIND_EXITED
3175       && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED && ecs->new_thread_event)
3176     add_thread (ecs->ptid);
3177
3178   ecs->event_thread = find_thread_ptid (ecs->ptid);
3179
3180   /* Dependent on valid ECS->EVENT_THREAD.  */
3181   adjust_pc_after_break (ecs);
3182
3183   /* Dependent on the current PC value modified by adjust_pc_after_break.  */
3184   reinit_frame_cache ();
3185
3186   breakpoint_retire_moribund ();
3187
3188   /* First, distinguish signals caused by the debugger from signals
3189      that have to do with the program's own actions.  Note that
3190      breakpoint insns may cause SIGTRAP or SIGILL or SIGEMT, depending
3191      on the operating system version.  Here we detect when a SIGILL or
3192      SIGEMT is really a breakpoint and change it to SIGTRAP.  We do
3193      something similar for SIGSEGV, since a SIGSEGV will be generated
3194      when we're trying to execute a breakpoint instruction on a
3195      non-executable stack.  This happens for call dummy breakpoints
3196      for architectures like SPARC that place call dummies on the
3197      stack.  */
3198   if (ecs->ws.kind == TARGET_WAITKIND_STOPPED
3199       && (ecs->ws.value.sig == TARGET_SIGNAL_ILL
3200           || ecs->ws.value.sig == TARGET_SIGNAL_SEGV
3201           || ecs->ws.value.sig == TARGET_SIGNAL_EMT))
3202     {
3203       struct regcache *regcache = get_thread_regcache (ecs->ptid);
3204
3205       if (breakpoint_inserted_here_p (get_regcache_aspace (regcache),
3206                                       regcache_read_pc (regcache)))
3207         {
3208           if (debug_infrun)
3209             fprintf_unfiltered (gdb_stdlog,
3210                                 "infrun: Treating signal as SIGTRAP\n");
3211           ecs->ws.value.sig = TARGET_SIGNAL_TRAP;
3212         }
3213     }
3214
3215   /* Mark the non-executing threads accordingly.  In all-stop, all
3216      threads of all processes are stopped when we get any event
3217      reported.  In non-stop mode, only the event thread stops.  If
3218      we're handling a process exit in non-stop mode, there's nothing
3219      to do, as threads of the dead process are gone, and threads of
3220      any other process were left running.  */
3221   if (!non_stop)
3222     set_executing (minus_one_ptid, 0);
3223   else if (ecs->ws.kind != TARGET_WAITKIND_SIGNALLED
3224            && ecs->ws.kind != TARGET_WAITKIND_EXITED)
3225     set_executing (inferior_ptid, 0);
3226
3227   switch (infwait_state)
3228     {
3229     case infwait_thread_hop_state:
3230       if (debug_infrun)
3231         fprintf_unfiltered (gdb_stdlog, "infrun: infwait_thread_hop_state\n");
3232       break;
3233
3234     case infwait_normal_state:
3235       if (debug_infrun)
3236         fprintf_unfiltered (gdb_stdlog, "infrun: infwait_normal_state\n");
3237       break;
3238
3239     case infwait_step_watch_state:
3240       if (debug_infrun)
3241         fprintf_unfiltered (gdb_stdlog,
3242                             "infrun: infwait_step_watch_state\n");
3243
3244       stepped_after_stopped_by_watchpoint = 1;
3245       break;
3246
3247     case infwait_nonstep_watch_state:
3248       if (debug_infrun)
3249         fprintf_unfiltered (gdb_stdlog,
3250                             "infrun: infwait_nonstep_watch_state\n");
3251       insert_breakpoints ();
3252
3253       /* FIXME-maybe: is this cleaner than setting a flag?  Does it
3254          handle things like signals arriving and other things happening
3255          in combination correctly?  */
3256       stepped_after_stopped_by_watchpoint = 1;
3257       break;
3258
3259     default:
3260       internal_error (__FILE__, __LINE__, _("bad switch"));
3261     }
3262
3263   infwait_state = infwait_normal_state;
3264   waiton_ptid = pid_to_ptid (-1);
3265
3266   switch (ecs->ws.kind)
3267     {
3268     case TARGET_WAITKIND_LOADED:
3269       if (debug_infrun)
3270         fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_LOADED\n");
3271       /* Ignore gracefully during startup of the inferior, as it might
3272          be the shell which has just loaded some objects, otherwise
3273          add the symbols for the newly loaded objects.  Also ignore at
3274          the beginning of an attach or remote session; we will query
3275          the full list of libraries once the connection is
3276          established.  */
3277       if (stop_soon == NO_STOP_QUIETLY)
3278         {
3279           /* Check for any newly added shared libraries if we're
3280              supposed to be adding them automatically.  Switch
3281              terminal for any messages produced by
3282              breakpoint_re_set.  */
3283           target_terminal_ours_for_output ();
3284           /* NOTE: cagney/2003-11-25: Make certain that the target
3285              stack's section table is kept up-to-date.  Architectures,
3286              (e.g., PPC64), use the section table to perform
3287              operations such as address => section name and hence
3288              require the table to contain all sections (including
3289              those found in shared libraries).  */
3290 #ifdef SOLIB_ADD
3291           SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
3292 #else
3293           solib_add (NULL, 0, &current_target, auto_solib_add);
3294 #endif
3295           target_terminal_inferior ();
3296
3297           /* If requested, stop when the dynamic linker notifies
3298              gdb of events.  This allows the user to get control
3299              and place breakpoints in initializer routines for
3300              dynamically loaded objects (among other things).  */
3301           if (stop_on_solib_events)
3302             {
3303               /* Make sure we print "Stopped due to solib-event" in
3304                  normal_stop.  */
3305               stop_print_frame = 1;
3306
3307               stop_stepping (ecs);
3308               return;
3309             }
3310
3311           /* NOTE drow/2007-05-11: This might be a good place to check
3312              for "catch load".  */
3313         }
3314
3315       /* If we are skipping through a shell, or through shared library
3316          loading that we aren't interested in, resume the program.  If
3317          we're running the program normally, also resume.  But stop if
3318          we're attaching or setting up a remote connection.  */
3319       if (stop_soon == STOP_QUIETLY || stop_soon == NO_STOP_QUIETLY)
3320         {
3321           /* Loading of shared libraries might have changed breakpoint
3322              addresses.  Make sure new breakpoints are inserted.  */
3323           if (stop_soon == NO_STOP_QUIETLY
3324               && !breakpoints_always_inserted_mode ())
3325             insert_breakpoints ();
3326           resume (0, TARGET_SIGNAL_0);
3327           prepare_to_wait (ecs);
3328           return;
3329         }
3330
3331       break;
3332
3333     case TARGET_WAITKIND_SPURIOUS:
3334       if (debug_infrun)
3335         fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SPURIOUS\n");
3336       resume (0, TARGET_SIGNAL_0);
3337       prepare_to_wait (ecs);
3338       return;
3339
3340     case TARGET_WAITKIND_EXITED:
3341       if (debug_infrun)
3342         fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_EXITED\n");
3343       inferior_ptid = ecs->ptid;
3344       set_current_inferior (find_inferior_pid (ptid_get_pid (ecs->ptid)));
3345       set_current_program_space (current_inferior ()->pspace);
3346       handle_vfork_child_exec_or_exit (0);
3347       target_terminal_ours ();  /* Must do this before mourn anyway.  */
3348       print_exited_reason (ecs->ws.value.integer);
3349
3350       /* Record the exit code in the convenience variable $_exitcode, so
3351          that the user can inspect this again later.  */
3352       set_internalvar_integer (lookup_internalvar ("_exitcode"),
3353                                (LONGEST) ecs->ws.value.integer);
3354
3355       /* Also record this in the inferior itself.  */
3356       current_inferior ()->has_exit_code = 1;
3357       current_inferior ()->exit_code = (LONGEST) ecs->ws.value.integer;
3358
3359       gdb_flush (gdb_stdout);
3360       target_mourn_inferior ();
3361       singlestep_breakpoints_inserted_p = 0;
3362       cancel_single_step_breakpoints ();
3363       stop_print_frame = 0;
3364       stop_stepping (ecs);
3365       return;
3366
3367     case TARGET_WAITKIND_SIGNALLED:
3368       if (debug_infrun)
3369         fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SIGNALLED\n");
3370       inferior_ptid = ecs->ptid;
3371       set_current_inferior (find_inferior_pid (ptid_get_pid (ecs->ptid)));
3372       set_current_program_space (current_inferior ()->pspace);
3373       handle_vfork_child_exec_or_exit (0);
3374       stop_print_frame = 0;
3375       target_terminal_ours ();  /* Must do this before mourn anyway.  */
3376
3377       /* Note: By definition of TARGET_WAITKIND_SIGNALLED, we shouldn't
3378          reach here unless the inferior is dead.  However, for years
3379          target_kill() was called here, which hints that fatal signals aren't
3380          really fatal on some systems.  If that's true, then some changes
3381          may be needed.  */
3382       target_mourn_inferior ();
3383
3384       print_signal_exited_reason (ecs->ws.value.sig);
3385       singlestep_breakpoints_inserted_p = 0;
3386       cancel_single_step_breakpoints ();
3387       stop_stepping (ecs);
3388       return;
3389
3390       /* The following are the only cases in which we keep going;
3391          the above cases end in a continue or goto.  */
3392     case TARGET_WAITKIND_FORKED:
3393     case TARGET_WAITKIND_VFORKED:
3394       if (debug_infrun)
3395         fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_FORKED\n");
3396
3397       if (!ptid_equal (ecs->ptid, inferior_ptid))
3398         {
3399           context_switch (ecs->ptid);
3400           reinit_frame_cache ();
3401         }
3402
3403       /* Immediately detach breakpoints from the child before there's
3404          any chance of letting the user delete breakpoints from the
3405          breakpoint lists.  If we don't do this early, it's easy to
3406          leave left over traps in the child, vis: "break foo; catch
3407          fork; c; <fork>; del; c; <child calls foo>".  We only follow
3408          the fork on the last `continue', and by that time the
3409          breakpoint at "foo" is long gone from the breakpoint table.
3410          If we vforked, then we don't need to unpatch here, since both
3411          parent and child are sharing the same memory pages; we'll
3412          need to unpatch at follow/detach time instead to be certain
3413          that new breakpoints added between catchpoint hit time and
3414          vfork follow are detached.  */
3415       if (ecs->ws.kind != TARGET_WAITKIND_VFORKED)
3416         {
3417           int child_pid = ptid_get_pid (ecs->ws.value.related_pid);
3418
3419           /* This won't actually modify the breakpoint list, but will
3420              physically remove the breakpoints from the child.  */
3421           detach_breakpoints (child_pid);
3422         }
3423
3424       if (singlestep_breakpoints_inserted_p)
3425         {
3426           /* Pull the single step breakpoints out of the target.  */
3427           remove_single_step_breakpoints ();
3428           singlestep_breakpoints_inserted_p = 0;
3429         }
3430
3431       /* In case the event is caught by a catchpoint, remember that
3432          the event is to be followed at the next resume of the thread,
3433          and not immediately.  */
3434       ecs->event_thread->pending_follow = ecs->ws;
3435
3436       stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
3437
3438       ecs->event_thread->control.stop_bpstat
3439         = bpstat_stop_status (get_regcache_aspace (get_current_regcache ()),
3440                               stop_pc, ecs->ptid);
3441
3442       /* Note that we're interested in knowing the bpstat actually
3443          causes a stop, not just if it may explain the signal.
3444          Software watchpoints, for example, always appear in the
3445          bpstat.  */
3446       ecs->random_signal
3447         = !bpstat_causes_stop (ecs->event_thread->control.stop_bpstat);
3448
3449       /* If no catchpoint triggered for this, then keep going.  */
3450       if (ecs->random_signal)
3451         {
3452           ptid_t parent;
3453           ptid_t child;
3454           int should_resume;
3455           int follow_child
3456             = (follow_fork_mode_string == follow_fork_mode_child);
3457
3458           ecs->event_thread->suspend.stop_signal = TARGET_SIGNAL_0;
3459
3460           should_resume = follow_fork ();
3461
3462           parent = ecs->ptid;
3463           child = ecs->ws.value.related_pid;
3464
3465           /* In non-stop mode, also resume the other branch.  */
3466           if (non_stop && !detach_fork)
3467             {
3468               if (follow_child)
3469                 switch_to_thread (parent);
3470               else
3471                 switch_to_thread (child);
3472
3473               ecs->event_thread = inferior_thread ();
3474               ecs->ptid = inferior_ptid;
3475               keep_going (ecs);
3476             }
3477
3478           if (follow_child)
3479             switch_to_thread (child);
3480           else
3481             switch_to_thread (parent);
3482
3483           ecs->event_thread = inferior_thread ();
3484           ecs->ptid = inferior_ptid;
3485
3486           if (should_resume)
3487             keep_going (ecs);
3488           else
3489             stop_stepping (ecs);
3490           return;
3491         }
3492       ecs->event_thread->suspend.stop_signal = TARGET_SIGNAL_TRAP;
3493       goto process_event_stop_test;
3494
3495     case TARGET_WAITKIND_VFORK_DONE:
3496       /* Done with the shared memory region.  Re-insert breakpoints in
3497          the parent, and keep going.  */
3498
3499       if (debug_infrun)
3500         fprintf_unfiltered (gdb_stdlog,
3501                             "infrun: TARGET_WAITKIND_VFORK_DONE\n");
3502
3503       if (!ptid_equal (ecs->ptid, inferior_ptid))
3504         context_switch (ecs->ptid);
3505
3506       current_inferior ()->waiting_for_vfork_done = 0;
3507       current_inferior ()->pspace->breakpoints_not_allowed = 0;
3508       /* This also takes care of reinserting breakpoints in the
3509          previously locked inferior.  */
3510       keep_going (ecs);
3511       return;
3512
3513     case TARGET_WAITKIND_EXECD:
3514       if (debug_infrun)
3515         fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_EXECD\n");
3516
3517       if (!ptid_equal (ecs->ptid, inferior_ptid))
3518         {
3519           context_switch (ecs->ptid);
3520           reinit_frame_cache ();
3521         }
3522
3523       singlestep_breakpoints_inserted_p = 0;
3524       cancel_single_step_breakpoints ();
3525
3526       stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
3527
3528       /* Do whatever is necessary to the parent branch of the vfork.  */
3529       handle_vfork_child_exec_or_exit (1);
3530
3531       /* This causes the eventpoints and symbol table to be reset.
3532          Must do this now, before trying to determine whether to
3533          stop.  */
3534       follow_exec (inferior_ptid, ecs->ws.value.execd_pathname);
3535
3536       ecs->event_thread->control.stop_bpstat
3537         = bpstat_stop_status (get_regcache_aspace (get_current_regcache ()),
3538                               stop_pc, ecs->ptid);
3539       ecs->random_signal
3540         = !bpstat_explains_signal (ecs->event_thread->control.stop_bpstat);
3541
3542       /* Note that this may be referenced from inside
3543          bpstat_stop_status above, through inferior_has_execd.  */
3544       xfree (ecs->ws.value.execd_pathname);
3545       ecs->ws.value.execd_pathname = NULL;
3546
3547       /* If no catchpoint triggered for this, then keep going.  */
3548       if (ecs->random_signal)
3549         {
3550           ecs->event_thread->suspend.stop_signal = TARGET_SIGNAL_0;
3551           keep_going (ecs);
3552           return;
3553         }
3554       ecs->event_thread->suspend.stop_signal = TARGET_SIGNAL_TRAP;
3555       goto process_event_stop_test;
3556
3557       /* Be careful not to try to gather much state about a thread
3558          that's in a syscall.  It's frequently a losing proposition.  */
3559     case TARGET_WAITKIND_SYSCALL_ENTRY:
3560       if (debug_infrun)
3561         fprintf_unfiltered (gdb_stdlog,
3562                             "infrun: TARGET_WAITKIND_SYSCALL_ENTRY\n");
3563       /* Getting the current syscall number.  */
3564       if (handle_syscall_event (ecs) != 0)
3565         return;
3566       goto process_event_stop_test;
3567
3568       /* Before examining the threads further, step this thread to
3569          get it entirely out of the syscall.  (We get notice of the
3570          event when the thread is just on the verge of exiting a
3571          syscall.  Stepping one instruction seems to get it back
3572          into user code.)  */
3573     case TARGET_WAITKIND_SYSCALL_RETURN:
3574       if (debug_infrun)
3575         fprintf_unfiltered (gdb_stdlog,
3576                             "infrun: TARGET_WAITKIND_SYSCALL_RETURN\n");
3577       if (handle_syscall_event (ecs) != 0)
3578         return;
3579       goto process_event_stop_test;
3580
3581     case TARGET_WAITKIND_STOPPED:
3582       if (debug_infrun)
3583         fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_STOPPED\n");
3584       ecs->event_thread->suspend.stop_signal = ecs->ws.value.sig;
3585       break;
3586
3587     case TARGET_WAITKIND_NO_HISTORY:
3588       /* Reverse execution: target ran out of history info.  */
3589       stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
3590       print_no_history_reason ();
3591       stop_stepping (ecs);
3592       return;
3593     }
3594
3595   if (ecs->new_thread_event)
3596     {
3597       if (non_stop)
3598         /* Non-stop assumes that the target handles adding new threads
3599            to the thread list.  */
3600         internal_error (__FILE__, __LINE__,
3601                         "targets should add new threads to the thread "
3602                         "list themselves in non-stop mode.");
3603
3604       /* We may want to consider not doing a resume here in order to
3605          give the user a chance to play with the new thread.  It might
3606          be good to make that a user-settable option.  */
3607
3608       /* At this point, all threads are stopped (happens automatically
3609          in either the OS or the native code).  Therefore we need to
3610          continue all threads in order to make progress.  */
3611
3612       if (!ptid_equal (ecs->ptid, inferior_ptid))
3613         context_switch (ecs->ptid);
3614       target_resume (RESUME_ALL, 0, TARGET_SIGNAL_0);
3615       prepare_to_wait (ecs);
3616       return;
3617     }
3618
3619   if (ecs->ws.kind == TARGET_WAITKIND_STOPPED)
3620     {
3621       /* Do we need to clean up the state of a thread that has
3622          completed a displaced single-step?  (Doing so usually affects
3623          the PC, so do it here, before we set stop_pc.)  */
3624       displaced_step_fixup (ecs->ptid,
3625                             ecs->event_thread->suspend.stop_signal);
3626
3627       /* If we either finished a single-step or hit a breakpoint, but
3628          the user wanted this thread to be stopped, pretend we got a
3629          SIG0 (generic unsignaled stop).  */
3630
3631       if (ecs->event_thread->stop_requested
3632           && ecs->event_thread->suspend.stop_signal == TARGET_SIGNAL_TRAP)
3633         ecs->event_thread->suspend.stop_signal = TARGET_SIGNAL_0;
3634     }
3635
3636   stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
3637
3638   if (debug_infrun)
3639     {
3640       struct regcache *regcache = get_thread_regcache (ecs->ptid);
3641       struct gdbarch *gdbarch = get_regcache_arch (regcache);
3642       struct cleanup *old_chain = save_inferior_ptid ();
3643
3644       inferior_ptid = ecs->ptid;
3645
3646       fprintf_unfiltered (gdb_stdlog, "infrun: stop_pc = %s\n",
3647                           paddress (gdbarch, stop_pc));
3648       if (target_stopped_by_watchpoint ())
3649         {
3650           CORE_ADDR addr;
3651
3652           fprintf_unfiltered (gdb_stdlog, "infrun: stopped by watchpoint\n");
3653
3654           if (target_stopped_data_address (&current_target, &addr))
3655             fprintf_unfiltered (gdb_stdlog,
3656                                 "infrun: stopped data address = %s\n",
3657                                 paddress (gdbarch, addr));
3658           else
3659             fprintf_unfiltered (gdb_stdlog,
3660                                 "infrun: (no data address available)\n");
3661         }
3662
3663       do_cleanups (old_chain);
3664     }
3665
3666   if (stepping_past_singlestep_breakpoint)
3667     {
3668       gdb_assert (singlestep_breakpoints_inserted_p);
3669       gdb_assert (ptid_equal (singlestep_ptid, ecs->ptid));
3670       gdb_assert (!ptid_equal (singlestep_ptid, saved_singlestep_ptid));
3671
3672       stepping_past_singlestep_breakpoint = 0;
3673
3674       /* We've either finished single-stepping past the single-step
3675          breakpoint, or stopped for some other reason.  It would be nice if
3676          we could tell, but we can't reliably.  */
3677       if (ecs->event_thread->suspend.stop_signal == TARGET_SIGNAL_TRAP)
3678         {
3679           if (debug_infrun)
3680             fprintf_unfiltered (gdb_stdlog,
3681                                 "infrun: stepping_past_"
3682                                 "singlestep_breakpoint\n");
3683           /* Pull the single step breakpoints out of the target.  */
3684           remove_single_step_breakpoints ();
3685           singlestep_breakpoints_inserted_p = 0;
3686
3687           ecs->random_signal = 0;
3688           ecs->event_thread->control.trap_expected = 0;
3689
3690           context_switch (saved_singlestep_ptid);
3691           if (deprecated_context_hook)
3692             deprecated_context_hook (pid_to_thread_id (ecs->ptid));
3693
3694           resume (1, TARGET_SIGNAL_0);
3695           prepare_to_wait (ecs);
3696           return;
3697         }
3698     }
3699
3700   if (!ptid_equal (deferred_step_ptid, null_ptid))
3701     {
3702       /* In non-stop mode, there's never a deferred_step_ptid set.  */
3703       gdb_assert (!non_stop);
3704
3705       /* If we stopped for some other reason than single-stepping, ignore
3706          the fact that we were supposed to switch back.  */
3707       if (ecs->event_thread->suspend.stop_signal == TARGET_SIGNAL_TRAP)
3708         {
3709           if (debug_infrun)
3710             fprintf_unfiltered (gdb_stdlog,
3711                                 "infrun: handling deferred step\n");
3712
3713           /* Pull the single step breakpoints out of the target.  */
3714           if (singlestep_breakpoints_inserted_p)
3715             {
3716               remove_single_step_breakpoints ();
3717               singlestep_breakpoints_inserted_p = 0;
3718             }
3719
3720           ecs->event_thread->control.trap_expected = 0;
3721
3722           /* Note: We do not call context_switch at this point, as the
3723              context is already set up for stepping the original thread.  */
3724           switch_to_thread (deferred_step_ptid);
3725           deferred_step_ptid = null_ptid;
3726           /* Suppress spurious "Switching to ..." message.  */
3727           previous_inferior_ptid = inferior_ptid;
3728
3729           resume (1, TARGET_SIGNAL_0);
3730           prepare_to_wait (ecs);
3731           return;
3732         }
3733
3734       deferred_step_ptid = null_ptid;
3735     }
3736
3737   /* See if a thread hit a thread-specific breakpoint that was meant for
3738      another thread.  If so, then step that thread past the breakpoint,
3739      and continue it.  */
3740
3741   if (ecs->event_thread->suspend.stop_signal == TARGET_SIGNAL_TRAP)
3742     {
3743       int thread_hop_needed = 0;
3744       struct address_space *aspace = 
3745         get_regcache_aspace (get_thread_regcache (ecs->ptid));
3746
3747       /* Check if a regular breakpoint has been hit before checking
3748          for a potential single step breakpoint.  Otherwise, GDB will
3749          not see this breakpoint hit when stepping onto breakpoints.  */
3750       if (regular_breakpoint_inserted_here_p (aspace, stop_pc))
3751         {
3752           ecs->random_signal = 0;
3753           if (!breakpoint_thread_match (aspace, stop_pc, ecs->ptid))
3754             thread_hop_needed = 1;
3755         }
3756       else if (singlestep_breakpoints_inserted_p)
3757         {
3758           /* We have not context switched yet, so this should be true
3759              no matter which thread hit the singlestep breakpoint.  */
3760           gdb_assert (ptid_equal (inferior_ptid, singlestep_ptid));
3761           if (debug_infrun)
3762             fprintf_unfiltered (gdb_stdlog, "infrun: software single step "
3763                                 "trap for %s\n",
3764                                 target_pid_to_str (ecs->ptid));
3765
3766           ecs->random_signal = 0;
3767           /* The call to in_thread_list is necessary because PTIDs sometimes
3768              change when we go from single-threaded to multi-threaded.  If
3769              the singlestep_ptid is still in the list, assume that it is
3770              really different from ecs->ptid.  */
3771           if (!ptid_equal (singlestep_ptid, ecs->ptid)
3772               && in_thread_list (singlestep_ptid))
3773             {
3774               /* If the PC of the thread we were trying to single-step
3775                  has changed, discard this event (which we were going
3776                  to ignore anyway), and pretend we saw that thread
3777                  trap.  This prevents us continuously moving the
3778                  single-step breakpoint forward, one instruction at a
3779                  time.  If the PC has changed, then the thread we were
3780                  trying to single-step has trapped or been signalled,
3781                  but the event has not been reported to GDB yet.
3782
3783                  There might be some cases where this loses signal
3784                  information, if a signal has arrived at exactly the
3785                  same time that the PC changed, but this is the best
3786                  we can do with the information available.  Perhaps we
3787                  should arrange to report all events for all threads
3788                  when they stop, or to re-poll the remote looking for
3789                  this particular thread (i.e. temporarily enable
3790                  schedlock).  */
3791
3792              CORE_ADDR new_singlestep_pc
3793                = regcache_read_pc (get_thread_regcache (singlestep_ptid));
3794
3795              if (new_singlestep_pc != singlestep_pc)
3796                {
3797                  enum target_signal stop_signal;
3798
3799                  if (debug_infrun)
3800                    fprintf_unfiltered (gdb_stdlog, "infrun: unexpected thread,"
3801                                        " but expected thread advanced also\n");
3802
3803                  /* The current context still belongs to
3804                     singlestep_ptid.  Don't swap here, since that's
3805                     the context we want to use.  Just fudge our
3806                     state and continue.  */
3807                  stop_signal = ecs->event_thread->suspend.stop_signal;
3808                  ecs->event_thread->suspend.stop_signal = TARGET_SIGNAL_0;
3809                  ecs->ptid = singlestep_ptid;
3810                  ecs->event_thread = find_thread_ptid (ecs->ptid);
3811                  ecs->event_thread->suspend.stop_signal = stop_signal;
3812                  stop_pc = new_singlestep_pc;
3813                }
3814              else
3815                {
3816                  if (debug_infrun)
3817                    fprintf_unfiltered (gdb_stdlog,
3818                                        "infrun: unexpected thread\n");
3819
3820                  thread_hop_needed = 1;
3821                  stepping_past_singlestep_breakpoint = 1;
3822                  saved_singlestep_ptid = singlestep_ptid;
3823                }
3824             }
3825         }
3826
3827       if (thread_hop_needed)
3828         {
3829           struct regcache *thread_regcache;
3830           int remove_status = 0;
3831
3832           if (debug_infrun)
3833             fprintf_unfiltered (gdb_stdlog, "infrun: thread_hop_needed\n");
3834
3835           /* Switch context before touching inferior memory, the
3836              previous thread may have exited.  */
3837           if (!ptid_equal (inferior_ptid, ecs->ptid))
3838             context_switch (ecs->ptid);
3839
3840           /* Saw a breakpoint, but it was hit by the wrong thread.
3841              Just continue.  */
3842
3843           if (singlestep_breakpoints_inserted_p)
3844             {
3845               /* Pull the single step breakpoints out of the target.  */
3846               remove_single_step_breakpoints ();
3847               singlestep_breakpoints_inserted_p = 0;
3848             }
3849
3850           /* If the arch can displace step, don't remove the
3851              breakpoints.  */
3852           thread_regcache = get_thread_regcache (ecs->ptid);
3853           if (!use_displaced_stepping (get_regcache_arch (thread_regcache)))
3854             remove_status = remove_breakpoints ();
3855
3856           /* Did we fail to remove breakpoints?  If so, try
3857              to set the PC past the bp.  (There's at least
3858              one situation in which we can fail to remove
3859              the bp's: On HP-UX's that use ttrace, we can't
3860              change the address space of a vforking child
3861              process until the child exits (well, okay, not
3862              then either :-) or execs.  */
3863           if (remove_status != 0)
3864             error (_("Cannot step over breakpoint hit in wrong thread"));
3865           else
3866             {                   /* Single step */
3867               if (!non_stop)
3868                 {
3869                   /* Only need to require the next event from this
3870                      thread in all-stop mode.  */
3871                   waiton_ptid = ecs->ptid;
3872                   infwait_state = infwait_thread_hop_state;
3873                 }
3874
3875               ecs->event_thread->stepping_over_breakpoint = 1;
3876               keep_going (ecs);
3877               return;
3878             }
3879         }
3880       else if (singlestep_breakpoints_inserted_p)
3881         {
3882           ecs->random_signal = 0;
3883         }
3884     }
3885   else
3886     ecs->random_signal = 1;
3887
3888   /* See if something interesting happened to the non-current thread.  If
3889      so, then switch to that thread.  */
3890   if (!ptid_equal (ecs->ptid, inferior_ptid))
3891     {
3892       if (debug_infrun)
3893         fprintf_unfiltered (gdb_stdlog, "infrun: context switch\n");
3894
3895       context_switch (ecs->ptid);
3896
3897       if (deprecated_context_hook)
3898         deprecated_context_hook (pid_to_thread_id (ecs->ptid));
3899     }
3900
3901   /* At this point, get hold of the now-current thread's frame.  */
3902   frame = get_current_frame ();
3903   gdbarch = get_frame_arch (frame);
3904
3905   if (singlestep_breakpoints_inserted_p)
3906     {
3907       /* Pull the single step breakpoints out of the target.  */
3908       remove_single_step_breakpoints ();
3909       singlestep_breakpoints_inserted_p = 0;
3910     }
3911
3912   if (stepped_after_stopped_by_watchpoint)
3913     stopped_by_watchpoint = 0;
3914   else
3915     stopped_by_watchpoint = watchpoints_triggered (&ecs->ws);
3916
3917   /* If necessary, step over this watchpoint.  We'll be back to display
3918      it in a moment.  */
3919   if (stopped_by_watchpoint
3920       && (target_have_steppable_watchpoint
3921           || gdbarch_have_nonsteppable_watchpoint (gdbarch)))
3922     {
3923       /* At this point, we are stopped at an instruction which has
3924          attempted to write to a piece of memory under control of
3925          a watchpoint.  The instruction hasn't actually executed
3926          yet.  If we were to evaluate the watchpoint expression
3927          now, we would get the old value, and therefore no change
3928          would seem to have occurred.
3929
3930          In order to make watchpoints work `right', we really need
3931          to complete the memory write, and then evaluate the
3932          watchpoint expression.  We do this by single-stepping the
3933          target.
3934
3935          It may not be necessary to disable the watchpoint to stop over
3936          it.  For example, the PA can (with some kernel cooperation)
3937          single step over a watchpoint without disabling the watchpoint.
3938
3939          It is far more common to need to disable a watchpoint to step
3940          the inferior over it.  If we have non-steppable watchpoints,
3941          we must disable the current watchpoint; it's simplest to
3942          disable all watchpoints and breakpoints.  */
3943       int hw_step = 1;
3944
3945       if (!target_have_steppable_watchpoint)
3946         {
3947           remove_breakpoints ();
3948           /* See comment in resume why we need to stop bypassing signals
3949              while breakpoints have been removed.  */
3950           target_pass_signals (0, NULL);
3951         }
3952         /* Single step */
3953       hw_step = maybe_software_singlestep (gdbarch, stop_pc);
3954       target_resume (ecs->ptid, hw_step, TARGET_SIGNAL_0);
3955       waiton_ptid = ecs->ptid;
3956       if (target_have_steppable_watchpoint)
3957         infwait_state = infwait_step_watch_state;
3958       else
3959         infwait_state = infwait_nonstep_watch_state;
3960       prepare_to_wait (ecs);
3961       return;
3962     }
3963
3964   clear_stop_func (ecs);
3965   ecs->event_thread->stepping_over_breakpoint = 0;
3966   bpstat_clear (&ecs->event_thread->control.stop_bpstat);
3967   ecs->event_thread->control.stop_step = 0;
3968   stop_print_frame = 1;
3969   ecs->random_signal = 0;
3970   stopped_by_random_signal = 0;
3971
3972   /* Hide inlined functions starting here, unless we just performed stepi or
3973      nexti.  After stepi and nexti, always show the innermost frame (not any
3974      inline function call sites).  */
3975   if (ecs->event_thread->control.step_range_end != 1)
3976     skip_inline_frames (ecs->ptid);
3977
3978   if (ecs->event_thread->suspend.stop_signal == TARGET_SIGNAL_TRAP
3979       && ecs->event_thread->control.trap_expected
3980       && gdbarch_single_step_through_delay_p (gdbarch)
3981       && currently_stepping (ecs->event_thread))
3982     {
3983       /* We're trying to step off a breakpoint.  Turns out that we're
3984          also on an instruction that needs to be stepped multiple
3985          times before it's been fully executing.  E.g., architectures
3986          with a delay slot.  It needs to be stepped twice, once for
3987          the instruction and once for the delay slot.  */
3988       int step_through_delay
3989         = gdbarch_single_step_through_delay (gdbarch, frame);
3990
3991       if (debug_infrun && step_through_delay)
3992         fprintf_unfiltered (gdb_stdlog, "infrun: step through delay\n");
3993       if (ecs->event_thread->control.step_range_end == 0
3994           && step_through_delay)
3995         {
3996           /* The user issued a continue when stopped at a breakpoint.
3997              Set up for another trap and get out of here.  */
3998          ecs->event_thread->stepping_over_breakpoint = 1;
3999          keep_going (ecs);
4000          return;
4001         }
4002       else if (step_through_delay)
4003         {
4004           /* The user issued a step when stopped at a breakpoint.
4005              Maybe we should stop, maybe we should not - the delay
4006              slot *might* correspond to a line of source.  In any
4007              case, don't decide that here, just set 
4008              ecs->stepping_over_breakpoint, making sure we 
4009              single-step again before breakpoints are re-inserted.  */
4010           ecs->event_thread->stepping_over_breakpoint = 1;
4011         }
4012     }
4013
4014   /* Look at the cause of the stop, and decide what to do.
4015      The alternatives are:
4016      1) stop_stepping and return; to really stop and return to the debugger,
4017      2) keep_going and return to start up again
4018      (set ecs->event_thread->stepping_over_breakpoint to 1 to single step once)
4019      3) set ecs->random_signal to 1, and the decision between 1 and 2
4020      will be made according to the signal handling tables.  */
4021
4022   if (ecs->event_thread->suspend.stop_signal == TARGET_SIGNAL_TRAP
4023       || stop_soon == STOP_QUIETLY || stop_soon == STOP_QUIETLY_NO_SIGSTOP
4024       || stop_soon == STOP_QUIETLY_REMOTE)
4025     {
4026       if (ecs->event_thread->suspend.stop_signal == TARGET_SIGNAL_TRAP
4027           && stop_after_trap)
4028         {
4029           if (debug_infrun)
4030             fprintf_unfiltered (gdb_stdlog, "infrun: stopped\n");
4031           stop_print_frame = 0;
4032           stop_stepping (ecs);
4033           return;
4034         }
4035
4036       /* This is originated from start_remote(), start_inferior() and
4037          shared libraries hook functions.  */
4038       if (stop_soon == STOP_QUIETLY || stop_soon == STOP_QUIETLY_REMOTE)
4039         {
4040           if (debug_infrun)
4041             fprintf_unfiltered (gdb_stdlog, "infrun: quietly stopped\n");
4042           stop_stepping (ecs);
4043           return;
4044         }
4045
4046       /* This originates from attach_command().  We need to overwrite
4047          the stop_signal here, because some kernels don't ignore a
4048          SIGSTOP in a subsequent ptrace(PTRACE_CONT,SIGSTOP) call.
4049          See more comments in inferior.h.  On the other hand, if we
4050          get a non-SIGSTOP, report it to the user - assume the backend
4051          will handle the SIGSTOP if it should show up later.
4052
4053          Also consider that the attach is complete when we see a
4054          SIGTRAP.  Some systems (e.g. Windows), and stubs supporting
4055          target extended-remote report it instead of a SIGSTOP
4056          (e.g. gdbserver).  We already rely on SIGTRAP being our
4057          signal, so this is no exception.
4058
4059          Also consider that the attach is complete when we see a
4060          TARGET_SIGNAL_0.  In non-stop mode, GDB will explicitly tell
4061          the target to stop all threads of the inferior, in case the
4062          low level attach operation doesn't stop them implicitly.  If
4063          they weren't stopped implicitly, then the stub will report a
4064          TARGET_SIGNAL_0, meaning: stopped for no particular reason
4065          other than GDB's request.  */
4066       if (stop_soon == STOP_QUIETLY_NO_SIGSTOP
4067           && (ecs->event_thread->suspend.stop_signal == TARGET_SIGNAL_STOP
4068               || ecs->event_thread->suspend.stop_signal == TARGET_SIGNAL_TRAP
4069               || ecs->event_thread->suspend.stop_signal == TARGET_SIGNAL_0))
4070         {
4071           stop_stepping (ecs);
4072           ecs->event_thread->suspend.stop_signal = TARGET_SIGNAL_0;
4073           return;
4074         }
4075
4076       /* See if there is a breakpoint at the current PC.  */
4077       ecs->event_thread->control.stop_bpstat
4078         = bpstat_stop_status (get_regcache_aspace (get_current_regcache ()),
4079                               stop_pc, ecs->ptid);
4080
4081       /* Following in case break condition called a
4082          function.  */
4083       stop_print_frame = 1;
4084
4085       /* This is where we handle "moribund" watchpoints.  Unlike
4086          software breakpoints traps, hardware watchpoint traps are
4087          always distinguishable from random traps.  If no high-level
4088          watchpoint is associated with the reported stop data address
4089          anymore, then the bpstat does not explain the signal ---
4090          simply make sure to ignore it if `stopped_by_watchpoint' is
4091          set.  */
4092
4093       if (debug_infrun
4094           && ecs->event_thread->suspend.stop_signal == TARGET_SIGNAL_TRAP
4095           && !bpstat_explains_signal (ecs->event_thread->control.stop_bpstat)
4096           && stopped_by_watchpoint)
4097         fprintf_unfiltered (gdb_stdlog,
4098                             "infrun: no user watchpoint explains "
4099                             "watchpoint SIGTRAP, ignoring\n");
4100
4101       /* NOTE: cagney/2003-03-29: These two checks for a random signal
4102          at one stage in the past included checks for an inferior
4103          function call's call dummy's return breakpoint.  The original
4104          comment, that went with the test, read:
4105
4106          ``End of a stack dummy.  Some systems (e.g. Sony news) give
4107          another signal besides SIGTRAP, so check here as well as
4108          above.''
4109
4110          If someone ever tries to get call dummys on a
4111          non-executable stack to work (where the target would stop
4112          with something like a SIGSEGV), then those tests might need
4113          to be re-instated.  Given, however, that the tests were only
4114          enabled when momentary breakpoints were not being used, I
4115          suspect that it won't be the case.
4116
4117          NOTE: kettenis/2004-02-05: Indeed such checks don't seem to
4118          be necessary for call dummies on a non-executable stack on
4119          SPARC.  */
4120
4121       if (ecs->event_thread->suspend.stop_signal == TARGET_SIGNAL_TRAP)
4122         ecs->random_signal
4123           = !(bpstat_explains_signal (ecs->event_thread->control.stop_bpstat)
4124               || stopped_by_watchpoint
4125               || ecs->event_thread->control.trap_expected
4126               || (ecs->event_thread->control.step_range_end
4127                   && (ecs->event_thread->control.step_resume_breakpoint
4128                       == NULL)));
4129       else
4130         {
4131           ecs->random_signal = !bpstat_explains_signal
4132                                      (ecs->event_thread->control.stop_bpstat);
4133           if (!ecs->random_signal)
4134             ecs->event_thread->suspend.stop_signal = TARGET_SIGNAL_TRAP;
4135         }
4136     }
4137
4138   /* When we reach this point, we've pretty much decided
4139      that the reason for stopping must've been a random
4140      (unexpected) signal.  */
4141
4142   else
4143     ecs->random_signal = 1;
4144
4145 process_event_stop_test:
4146
4147   /* Re-fetch current thread's frame in case we did a
4148      "goto process_event_stop_test" above.  */
4149   frame = get_current_frame ();
4150   gdbarch = get_frame_arch (frame);
4151
4152   /* For the program's own signals, act according to
4153      the signal handling tables.  */
4154
4155   if (ecs->random_signal)
4156     {
4157       /* Signal not for debugging purposes.  */
4158       int printed = 0;
4159       struct inferior *inf = find_inferior_pid (ptid_get_pid (ecs->ptid));
4160
4161       if (debug_infrun)
4162          fprintf_unfiltered (gdb_stdlog, "infrun: random signal %d\n",
4163                              ecs->event_thread->suspend.stop_signal);
4164
4165       stopped_by_random_signal = 1;
4166
4167       if (signal_print[ecs->event_thread->suspend.stop_signal])
4168         {
4169           printed = 1;
4170           target_terminal_ours_for_output ();
4171           print_signal_received_reason
4172                                      (ecs->event_thread->suspend.stop_signal);
4173         }
4174       /* Always stop on signals if we're either just gaining control
4175          of the program, or the user explicitly requested this thread
4176          to remain stopped.  */
4177       if (stop_soon != NO_STOP_QUIETLY
4178           || ecs->event_thread->stop_requested
4179           || (!inf->detaching
4180               && signal_stop_state (ecs->event_thread->suspend.stop_signal)))
4181         {
4182           stop_stepping (ecs);
4183           return;
4184         }
4185       /* If not going to stop, give terminal back
4186          if we took it away.  */
4187       else if (printed)
4188         target_terminal_inferior ();
4189
4190       /* Clear the signal if it should not be passed.  */
4191       if (signal_program[ecs->event_thread->suspend.stop_signal] == 0)
4192         ecs->event_thread->suspend.stop_signal = TARGET_SIGNAL_0;
4193
4194       if (ecs->event_thread->prev_pc == stop_pc
4195           && ecs->event_thread->control.trap_expected
4196           && ecs->event_thread->control.step_resume_breakpoint == NULL)
4197         {
4198           /* We were just starting a new sequence, attempting to
4199              single-step off of a breakpoint and expecting a SIGTRAP.
4200              Instead this signal arrives.  This signal will take us out
4201              of the stepping range so GDB needs to remember to, when
4202              the signal handler returns, resume stepping off that
4203              breakpoint.  */
4204           /* To simplify things, "continue" is forced to use the same
4205              code paths as single-step - set a breakpoint at the
4206              signal return address and then, once hit, step off that
4207              breakpoint.  */
4208           if (debug_infrun)
4209             fprintf_unfiltered (gdb_stdlog,
4210                                 "infrun: signal arrived while stepping over "
4211                                 "breakpoint\n");
4212
4213           insert_hp_step_resume_breakpoint_at_frame (frame);
4214           ecs->event_thread->step_after_step_resume_breakpoint = 1;
4215           /* Reset trap_expected to ensure breakpoints are re-inserted.  */
4216           ecs->event_thread->control.trap_expected = 0;
4217           keep_going (ecs);
4218           return;
4219         }
4220
4221       if (ecs->event_thread->control.step_range_end != 0
4222           && ecs->event_thread->suspend.stop_signal != TARGET_SIGNAL_0
4223           && (ecs->event_thread->control.step_range_start <= stop_pc
4224               && stop_pc < ecs->event_thread->control.step_range_end)
4225           && frame_id_eq (get_stack_frame_id (frame),
4226                           ecs->event_thread->control.step_stack_frame_id)
4227           && ecs->event_thread->control.step_resume_breakpoint == NULL)
4228         {
4229           /* The inferior is about to take a signal that will take it
4230              out of the single step range.  Set a breakpoint at the
4231              current PC (which is presumably where the signal handler
4232              will eventually return) and then allow the inferior to
4233              run free.
4234
4235              Note that this is only needed for a signal delivered
4236              while in the single-step range.  Nested signals aren't a
4237              problem as they eventually all return.  */
4238           if (debug_infrun)
4239             fprintf_unfiltered (gdb_stdlog,
4240                                 "infrun: signal may take us out of "
4241                                 "single-step range\n");
4242
4243           insert_hp_step_resume_breakpoint_at_frame (frame);
4244           /* Reset trap_expected to ensure breakpoints are re-inserted.  */
4245           ecs->event_thread->control.trap_expected = 0;
4246           keep_going (ecs);
4247           return;
4248         }
4249
4250       /* Note: step_resume_breakpoint may be non-NULL.  This occures
4251          when either there's a nested signal, or when there's a
4252          pending signal enabled just as the signal handler returns
4253          (leaving the inferior at the step-resume-breakpoint without
4254          actually executing it).  Either way continue until the
4255          breakpoint is really hit.  */
4256       keep_going (ecs);
4257       return;
4258     }
4259
4260   /* Handle cases caused by hitting a breakpoint.  */
4261   {
4262     CORE_ADDR jmp_buf_pc;
4263     struct bpstat_what what;
4264
4265     what = bpstat_what (ecs->event_thread->control.stop_bpstat);
4266
4267     if (what.call_dummy)
4268       {
4269         stop_stack_dummy = what.call_dummy;
4270       }
4271
4272     /* If we hit an internal event that triggers symbol changes, the
4273        current frame will be invalidated within bpstat_what (e.g., if
4274        we hit an internal solib event).  Re-fetch it.  */
4275     frame = get_current_frame ();
4276     gdbarch = get_frame_arch (frame);
4277
4278     switch (what.main_action)
4279       {
4280       case BPSTAT_WHAT_SET_LONGJMP_RESUME:
4281         /* If we hit the breakpoint at longjmp while stepping, we
4282            install a momentary breakpoint at the target of the
4283            jmp_buf.  */
4284
4285         if (debug_infrun)
4286           fprintf_unfiltered (gdb_stdlog,
4287                               "infrun: BPSTAT_WHAT_SET_LONGJMP_RESUME\n");
4288
4289         ecs->event_thread->stepping_over_breakpoint = 1;
4290
4291         if (what.is_longjmp)
4292           {
4293             if (!gdbarch_get_longjmp_target_p (gdbarch)
4294                 || !gdbarch_get_longjmp_target (gdbarch,
4295                                                 frame, &jmp_buf_pc))
4296               {
4297                 if (debug_infrun)
4298                   fprintf_unfiltered (gdb_stdlog,
4299                                       "infrun: BPSTAT_WHAT_SET_LONGJMP_RESUME "
4300                                       "(!gdbarch_get_longjmp_target)\n");
4301                 keep_going (ecs);
4302                 return;
4303               }
4304
4305             /* We're going to replace the current step-resume breakpoint
4306                with a longjmp-resume breakpoint.  */
4307             delete_step_resume_breakpoint (ecs->event_thread);
4308
4309             /* Insert a breakpoint at resume address.  */
4310             insert_longjmp_resume_breakpoint (gdbarch, jmp_buf_pc);
4311           }
4312         else
4313           {
4314             struct symbol *func = get_frame_function (frame);
4315
4316             if (func)
4317               check_exception_resume (ecs, frame, func);
4318           }
4319         keep_going (ecs);
4320         return;
4321
4322       case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
4323         if (debug_infrun)
4324           fprintf_unfiltered (gdb_stdlog,
4325                               "infrun: BPSTAT_WHAT_CLEAR_LONGJMP_RESUME\n");
4326
4327         if (what.is_longjmp)
4328           {
4329             gdb_assert (ecs->event_thread->control.step_resume_breakpoint
4330                         != NULL);
4331             delete_step_resume_breakpoint (ecs->event_thread);
4332           }
4333         else
4334           {
4335             /* There are several cases to consider.
4336
4337                1. The initiating frame no longer exists.  In this case
4338                we must stop, because the exception has gone too far.
4339
4340                2. The initiating frame exists, and is the same as the
4341                current frame.  We stop, because the exception has been
4342                caught.
4343
4344                3. The initiating frame exists and is different from
4345                the current frame.  This means the exception has been
4346                caught beneath the initiating frame, so keep going.  */
4347             struct frame_info *init_frame
4348               = frame_find_by_id (ecs->event_thread->initiating_frame);
4349
4350             gdb_assert (ecs->event_thread->control.exception_resume_breakpoint
4351                         != NULL);
4352             delete_exception_resume_breakpoint (ecs->event_thread);
4353
4354             if (init_frame)
4355               {
4356                 struct frame_id current_id
4357                   = get_frame_id (get_current_frame ());
4358                 if (frame_id_eq (current_id,
4359                                  ecs->event_thread->initiating_frame))
4360                   {
4361                     /* Case 2.  Fall through.  */
4362                   }
4363                 else
4364                   {
4365                     /* Case 3.  */
4366                     keep_going (ecs);
4367                     return;
4368                   }
4369               }
4370
4371             /* For Cases 1 and 2, remove the step-resume breakpoint,
4372                if it exists.  */
4373             delete_step_resume_breakpoint (ecs->event_thread);
4374           }
4375
4376         ecs->event_thread->control.stop_step = 1;
4377         print_end_stepping_range_reason ();
4378         stop_stepping (ecs);
4379         return;
4380
4381       case BPSTAT_WHAT_SINGLE:
4382         if (debug_infrun)
4383           fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_SINGLE\n");
4384         ecs->event_thread->stepping_over_breakpoint = 1;
4385         /* Still need to check other stuff, at least the case
4386            where we are stepping and step out of the right range.  */
4387         break;
4388
4389       case BPSTAT_WHAT_STEP_RESUME:
4390         if (debug_infrun)
4391           fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STEP_RESUME\n");
4392
4393         delete_step_resume_breakpoint (ecs->event_thread);
4394         if (ecs->event_thread->control.proceed_to_finish
4395             && execution_direction == EXEC_REVERSE)
4396           {
4397             struct thread_info *tp = ecs->event_thread;
4398
4399             /* We are finishing a function in reverse, and just hit
4400                the step-resume breakpoint at the start address of the
4401                function, and we're almost there -- just need to back
4402                up by one more single-step, which should take us back
4403                to the function call.  */
4404             tp->control.step_range_start = tp->control.step_range_end = 1;
4405             keep_going (ecs);
4406             return;
4407           }
4408         fill_in_stop_func (gdbarch, ecs);
4409         if (stop_pc == ecs->stop_func_start
4410             && execution_direction == EXEC_REVERSE)
4411           {
4412             /* We are stepping over a function call in reverse, and
4413                just hit the step-resume breakpoint at the start
4414                address of the function.  Go back to single-stepping,
4415                which should take us back to the function call.  */
4416             ecs->event_thread->stepping_over_breakpoint = 1;
4417             keep_going (ecs);
4418             return;
4419           }
4420         break;
4421
4422       case BPSTAT_WHAT_STOP_NOISY:
4423         if (debug_infrun)
4424           fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_NOISY\n");
4425         stop_print_frame = 1;
4426
4427         /* We are about to nuke the step_resume_breakpointt via the
4428            cleanup chain, so no need to worry about it here.  */
4429
4430         stop_stepping (ecs);
4431         return;
4432
4433       case BPSTAT_WHAT_STOP_SILENT:
4434         if (debug_infrun)
4435           fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_SILENT\n");
4436         stop_print_frame = 0;
4437
4438         /* We are about to nuke the step_resume_breakpoin via the
4439            cleanup chain, so no need to worry about it here.  */
4440
4441         stop_stepping (ecs);
4442         return;
4443
4444       case BPSTAT_WHAT_HP_STEP_RESUME:
4445         if (debug_infrun)
4446           fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_HP_STEP_RESUME\n");
4447
4448         delete_step_resume_breakpoint (ecs->event_thread);
4449         if (ecs->event_thread->step_after_step_resume_breakpoint)
4450           {
4451             /* Back when the step-resume breakpoint was inserted, we
4452                were trying to single-step off a breakpoint.  Go back
4453                to doing that.  */
4454             ecs->event_thread->step_after_step_resume_breakpoint = 0;
4455             ecs->event_thread->stepping_over_breakpoint = 1;
4456             keep_going (ecs);
4457             return;
4458           }
4459         break;
4460
4461       case BPSTAT_WHAT_KEEP_CHECKING:
4462         break;
4463       }
4464   }
4465
4466   /* We come here if we hit a breakpoint but should not
4467      stop for it.  Possibly we also were stepping
4468      and should stop for that.  So fall through and
4469      test for stepping.  But, if not stepping,
4470      do not stop.  */
4471
4472   /* In all-stop mode, if we're currently stepping but have stopped in
4473      some other thread, we need to switch back to the stepped thread.  */
4474   if (!non_stop)
4475     {
4476       struct thread_info *tp;
4477
4478       tp = iterate_over_threads (currently_stepping_or_nexting_callback,
4479                                  ecs->event_thread);
4480       if (tp)
4481         {
4482           /* However, if the current thread is blocked on some internal
4483              breakpoint, and we simply need to step over that breakpoint
4484              to get it going again, do that first.  */
4485           if ((ecs->event_thread->control.trap_expected
4486                && ecs->event_thread->suspend.stop_signal != TARGET_SIGNAL_TRAP)
4487               || ecs->event_thread->stepping_over_breakpoint)
4488             {
4489               keep_going (ecs);
4490               return;
4491             }
4492
4493           /* If the stepping thread exited, then don't try to switch
4494              back and resume it, which could fail in several different
4495              ways depending on the target.  Instead, just keep going.
4496
4497              We can find a stepping dead thread in the thread list in
4498              two cases:
4499
4500              - The target supports thread exit events, and when the
4501              target tries to delete the thread from the thread list,
4502              inferior_ptid pointed at the exiting thread.  In such
4503              case, calling delete_thread does not really remove the
4504              thread from the list; instead, the thread is left listed,
4505              with 'exited' state.
4506
4507              - The target's debug interface does not support thread
4508              exit events, and so we have no idea whatsoever if the
4509              previously stepping thread is still alive.  For that
4510              reason, we need to synchronously query the target
4511              now.  */
4512           if (is_exited (tp->ptid)
4513               || !target_thread_alive (tp->ptid))
4514             {
4515               if (debug_infrun)
4516                 fprintf_unfiltered (gdb_stdlog,
4517                                     "infrun: not switching back to "
4518                                     "stepped thread, it has vanished\n");
4519
4520               delete_thread (tp->ptid);
4521               keep_going (ecs);
4522               return;
4523             }
4524
4525           /* Otherwise, we no longer expect a trap in the current thread.
4526              Clear the trap_expected flag before switching back -- this is
4527              what keep_going would do as well, if we called it.  */
4528           ecs->event_thread->control.trap_expected = 0;
4529
4530           if (debug_infrun)
4531             fprintf_unfiltered (gdb_stdlog,
4532                                 "infrun: switching back to stepped thread\n");
4533
4534           ecs->event_thread = tp;
4535           ecs->ptid = tp->ptid;
4536           context_switch (ecs->ptid);
4537           keep_going (ecs);
4538           return;
4539         }
4540     }
4541
4542   /* Are we stepping to get the inferior out of the dynamic linker's
4543      hook (and possibly the dld itself) after catching a shlib
4544      event?  */
4545   if (ecs->event_thread->stepping_through_solib_after_catch)
4546     {
4547 #if defined(SOLIB_ADD)
4548       /* Have we reached our destination?  If not, keep going.  */
4549       if (SOLIB_IN_DYNAMIC_LINKER (PIDGET (ecs->ptid), stop_pc))
4550         {
4551           if (debug_infrun)
4552             fprintf_unfiltered (gdb_stdlog,
4553                                 "infrun: stepping in dynamic linker\n");
4554           ecs->event_thread->stepping_over_breakpoint = 1;
4555           keep_going (ecs);
4556           return;
4557         }
4558 #endif
4559       if (debug_infrun)
4560          fprintf_unfiltered (gdb_stdlog, "infrun: step past dynamic linker\n");
4561       /* Else, stop and report the catchpoint(s) whose triggering
4562          caused us to begin stepping.  */
4563       ecs->event_thread->stepping_through_solib_after_catch = 0;
4564       bpstat_clear (&ecs->event_thread->control.stop_bpstat);
4565       ecs->event_thread->control.stop_bpstat
4566         = bpstat_copy (ecs->event_thread->stepping_through_solib_catchpoints);
4567       bpstat_clear (&ecs->event_thread->stepping_through_solib_catchpoints);
4568       stop_print_frame = 1;
4569       stop_stepping (ecs);
4570       return;
4571     }
4572
4573   if (ecs->event_thread->control.step_resume_breakpoint)
4574     {
4575       if (debug_infrun)
4576          fprintf_unfiltered (gdb_stdlog,
4577                              "infrun: step-resume breakpoint is inserted\n");
4578
4579       /* Having a step-resume breakpoint overrides anything
4580          else having to do with stepping commands until
4581          that breakpoint is reached.  */
4582       keep_going (ecs);
4583       return;
4584     }
4585
4586   if (ecs->event_thread->control.step_range_end == 0)
4587     {
4588       if (debug_infrun)
4589          fprintf_unfiltered (gdb_stdlog, "infrun: no stepping, continue\n");
4590       /* Likewise if we aren't even stepping.  */
4591       keep_going (ecs);
4592       return;
4593     }
4594
4595   /* Re-fetch current thread's frame in case the code above caused
4596      the frame cache to be re-initialized, making our FRAME variable
4597      a dangling pointer.  */
4598   frame = get_current_frame ();
4599   gdbarch = get_frame_arch (frame);
4600   fill_in_stop_func (gdbarch, ecs);
4601
4602   /* If stepping through a line, keep going if still within it.
4603
4604      Note that step_range_end is the address of the first instruction
4605      beyond the step range, and NOT the address of the last instruction
4606      within it!
4607
4608      Note also that during reverse execution, we may be stepping
4609      through a function epilogue and therefore must detect when
4610      the current-frame changes in the middle of a line.  */
4611
4612   if (stop_pc >= ecs->event_thread->control.step_range_start
4613       && stop_pc < ecs->event_thread->control.step_range_end
4614       && (execution_direction != EXEC_REVERSE
4615           || frame_id_eq (get_frame_id (frame),
4616                           ecs->event_thread->control.step_frame_id)))
4617     {
4618       if (debug_infrun)
4619         fprintf_unfiltered
4620           (gdb_stdlog, "infrun: stepping inside range [%s-%s]\n",
4621            paddress (gdbarch, ecs->event_thread->control.step_range_start),
4622            paddress (gdbarch, ecs->event_thread->control.step_range_end));
4623
4624       /* When stepping backward, stop at beginning of line range
4625          (unless it's the function entry point, in which case
4626          keep going back to the call point).  */
4627       if (stop_pc == ecs->event_thread->control.step_range_start
4628           && stop_pc != ecs->stop_func_start
4629           && execution_direction == EXEC_REVERSE)
4630         {
4631           ecs->event_thread->control.stop_step = 1;
4632           print_end_stepping_range_reason ();
4633           stop_stepping (ecs);
4634         }
4635       else
4636         keep_going (ecs);
4637
4638       return;
4639     }
4640
4641   /* We stepped out of the stepping range.  */
4642
4643   /* If we are stepping at the source level and entered the runtime
4644      loader dynamic symbol resolution code...
4645
4646      EXEC_FORWARD: we keep on single stepping until we exit the run
4647      time loader code and reach the callee's address.
4648
4649      EXEC_REVERSE: we've already executed the callee (backward), and
4650      the runtime loader code is handled just like any other
4651      undebuggable function call.  Now we need only keep stepping
4652      backward through the trampoline code, and that's handled further
4653      down, so there is nothing for us to do here.  */
4654
4655   if (execution_direction != EXEC_REVERSE
4656       && ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
4657       && in_solib_dynsym_resolve_code (stop_pc))
4658     {
4659       CORE_ADDR pc_after_resolver =
4660         gdbarch_skip_solib_resolver (gdbarch, stop_pc);
4661
4662       if (debug_infrun)
4663          fprintf_unfiltered (gdb_stdlog,
4664                              "infrun: stepped into dynsym resolve code\n");
4665
4666       if (pc_after_resolver)
4667         {
4668           /* Set up a step-resume breakpoint at the address
4669              indicated by SKIP_SOLIB_RESOLVER.  */
4670           struct symtab_and_line sr_sal;
4671
4672           init_sal (&sr_sal);
4673           sr_sal.pc = pc_after_resolver;
4674           sr_sal.pspace = get_frame_program_space (frame);
4675
4676           insert_step_resume_breakpoint_at_sal (gdbarch,
4677                                                 sr_sal, null_frame_id);
4678         }
4679
4680       keep_going (ecs);
4681       return;
4682     }
4683
4684   if (ecs->event_thread->control.step_range_end != 1
4685       && (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
4686           || ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
4687       && get_frame_type (frame) == SIGTRAMP_FRAME)
4688     {
4689       if (debug_infrun)
4690          fprintf_unfiltered (gdb_stdlog,
4691                              "infrun: stepped into signal trampoline\n");
4692       /* The inferior, while doing a "step" or "next", has ended up in
4693          a signal trampoline (either by a signal being delivered or by
4694          the signal handler returning).  Just single-step until the
4695          inferior leaves the trampoline (either by calling the handler
4696          or returning).  */
4697       keep_going (ecs);
4698       return;
4699     }
4700
4701   /* Check for subroutine calls.  The check for the current frame
4702      equalling the step ID is not necessary - the check of the
4703      previous frame's ID is sufficient - but it is a common case and
4704      cheaper than checking the previous frame's ID.
4705
4706      NOTE: frame_id_eq will never report two invalid frame IDs as
4707      being equal, so to get into this block, both the current and
4708      previous frame must have valid frame IDs.  */
4709   /* The outer_frame_id check is a heuristic to detect stepping
4710      through startup code.  If we step over an instruction which
4711      sets the stack pointer from an invalid value to a valid value,
4712      we may detect that as a subroutine call from the mythical
4713      "outermost" function.  This could be fixed by marking
4714      outermost frames as !stack_p,code_p,special_p.  Then the
4715      initial outermost frame, before sp was valid, would
4716      have code_addr == &_start.  See the comment in frame_id_eq
4717      for more.  */
4718   if (!frame_id_eq (get_stack_frame_id (frame),
4719                     ecs->event_thread->control.step_stack_frame_id)
4720       && (frame_id_eq (frame_unwind_caller_id (get_current_frame ()),
4721                        ecs->event_thread->control.step_stack_frame_id)
4722           && (!frame_id_eq (ecs->event_thread->control.step_stack_frame_id,
4723                             outer_frame_id)
4724               || step_start_function != find_pc_function (stop_pc))))
4725     {
4726       CORE_ADDR real_stop_pc;
4727
4728       if (debug_infrun)
4729          fprintf_unfiltered (gdb_stdlog, "infrun: stepped into subroutine\n");
4730
4731       if ((ecs->event_thread->control.step_over_calls == STEP_OVER_NONE)
4732           || ((ecs->event_thread->control.step_range_end == 1)
4733               && in_prologue (gdbarch, ecs->event_thread->prev_pc,
4734                               ecs->stop_func_start)))
4735         {
4736           /* I presume that step_over_calls is only 0 when we're
4737              supposed to be stepping at the assembly language level
4738              ("stepi").  Just stop.  */
4739           /* Also, maybe we just did a "nexti" inside a prolog, so we
4740              thought it was a subroutine call but it was not.  Stop as
4741              well.  FENN */
4742           /* And this works the same backward as frontward.  MVS */
4743           ecs->event_thread->control.stop_step = 1;
4744           print_end_stepping_range_reason ();
4745           stop_stepping (ecs);
4746           return;
4747         }
4748
4749       /* Reverse stepping through solib trampolines.  */
4750
4751       if (execution_direction == EXEC_REVERSE
4752           && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE
4753           && (gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc)
4754               || (ecs->stop_func_start == 0
4755                   && in_solib_dynsym_resolve_code (stop_pc))))
4756         {
4757           /* Any solib trampoline code can be handled in reverse
4758              by simply continuing to single-step.  We have already
4759              executed the solib function (backwards), and a few 
4760              steps will take us back through the trampoline to the
4761              caller.  */
4762           keep_going (ecs);
4763           return;
4764         }
4765
4766       if (ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
4767         {
4768           /* We're doing a "next".
4769
4770              Normal (forward) execution: set a breakpoint at the
4771              callee's return address (the address at which the caller
4772              will resume).
4773
4774              Reverse (backward) execution.  set the step-resume
4775              breakpoint at the start of the function that we just
4776              stepped into (backwards), and continue to there.  When we
4777              get there, we'll need to single-step back to the caller.  */
4778
4779           if (execution_direction == EXEC_REVERSE)
4780             {
4781               struct symtab_and_line sr_sal;
4782
4783               /* Normal function call return (static or dynamic).  */
4784               init_sal (&sr_sal);
4785               sr_sal.pc = ecs->stop_func_start;
4786               sr_sal.pspace = get_frame_program_space (frame);
4787               insert_step_resume_breakpoint_at_sal (gdbarch,
4788                                                     sr_sal, null_frame_id);
4789             }
4790           else
4791             insert_step_resume_breakpoint_at_caller (frame);
4792
4793           keep_going (ecs);
4794           return;
4795         }
4796
4797       /* If we are in a function call trampoline (a stub between the
4798          calling routine and the real function), locate the real
4799          function.  That's what tells us (a) whether we want to step
4800          into it at all, and (b) what prologue we want to run to the
4801          end of, if we do step into it.  */
4802       real_stop_pc = skip_language_trampoline (frame, stop_pc);
4803       if (real_stop_pc == 0)
4804         real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
4805       if (real_stop_pc != 0)
4806         ecs->stop_func_start = real_stop_pc;
4807
4808       if (real_stop_pc != 0 && in_solib_dynsym_resolve_code (real_stop_pc))
4809         {
4810           struct symtab_and_line sr_sal;
4811
4812           init_sal (&sr_sal);
4813           sr_sal.pc = ecs->stop_func_start;
4814           sr_sal.pspace = get_frame_program_space (frame);
4815
4816           insert_step_resume_breakpoint_at_sal (gdbarch,
4817                                                 sr_sal, null_frame_id);
4818           keep_going (ecs);
4819           return;
4820         }
4821
4822       /* If we have line number information for the function we are
4823          thinking of stepping into, step into it.
4824
4825          If there are several symtabs at that PC (e.g. with include
4826          files), just want to know whether *any* of them have line
4827          numbers.  find_pc_line handles this.  */
4828       {
4829         struct symtab_and_line tmp_sal;
4830
4831         tmp_sal = find_pc_line (ecs->stop_func_start, 0);
4832         if (tmp_sal.line != 0)
4833           {
4834             if (execution_direction == EXEC_REVERSE)
4835               handle_step_into_function_backward (gdbarch, ecs);
4836             else
4837               handle_step_into_function (gdbarch, ecs);
4838             return;
4839           }
4840       }
4841
4842       /* If we have no line number and the step-stop-if-no-debug is
4843          set, we stop the step so that the user has a chance to switch
4844          in assembly mode.  */
4845       if (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
4846           && step_stop_if_no_debug)
4847         {
4848           ecs->event_thread->control.stop_step = 1;
4849           print_end_stepping_range_reason ();
4850           stop_stepping (ecs);
4851           return;
4852         }
4853
4854       if (execution_direction == EXEC_REVERSE)
4855         {
4856           /* Set a breakpoint at callee's start address.
4857              From there we can step once and be back in the caller.  */
4858           struct symtab_and_line sr_sal;
4859
4860           init_sal (&sr_sal);
4861           sr_sal.pc = ecs->stop_func_start;
4862           sr_sal.pspace = get_frame_program_space (frame);
4863           insert_step_resume_breakpoint_at_sal (gdbarch,
4864                                                 sr_sal, null_frame_id);
4865         }
4866       else
4867         /* Set a breakpoint at callee's return address (the address
4868            at which the caller will resume).  */
4869         insert_step_resume_breakpoint_at_caller (frame);
4870
4871       keep_going (ecs);
4872       return;
4873     }
4874
4875   /* Reverse stepping through solib trampolines.  */
4876
4877   if (execution_direction == EXEC_REVERSE
4878       && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE)
4879     {
4880       if (gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc)
4881           || (ecs->stop_func_start == 0
4882               && in_solib_dynsym_resolve_code (stop_pc)))
4883         {
4884           /* Any solib trampoline code can be handled in reverse
4885              by simply continuing to single-step.  We have already
4886              executed the solib function (backwards), and a few 
4887              steps will take us back through the trampoline to the
4888              caller.  */
4889           keep_going (ecs);
4890           return;
4891         }
4892       else if (in_solib_dynsym_resolve_code (stop_pc))
4893         {
4894           /* Stepped backward into the solib dynsym resolver.
4895              Set a breakpoint at its start and continue, then
4896              one more step will take us out.  */
4897           struct symtab_and_line sr_sal;
4898
4899           init_sal (&sr_sal);
4900           sr_sal.pc = ecs->stop_func_start;
4901           sr_sal.pspace = get_frame_program_space (frame);
4902           insert_step_resume_breakpoint_at_sal (gdbarch, 
4903                                                 sr_sal, null_frame_id);
4904           keep_going (ecs);
4905           return;
4906         }
4907     }
4908
4909   /* If we're in the return path from a shared library trampoline,
4910      we want to proceed through the trampoline when stepping.  */
4911   if (gdbarch_in_solib_return_trampoline (gdbarch,
4912                                           stop_pc, ecs->stop_func_name))
4913     {
4914       /* Determine where this trampoline returns.  */
4915       CORE_ADDR real_stop_pc;
4916
4917       real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
4918
4919       if (debug_infrun)
4920          fprintf_unfiltered (gdb_stdlog,
4921                              "infrun: stepped into solib return tramp\n");
4922
4923       /* Only proceed through if we know where it's going.  */
4924       if (real_stop_pc)
4925         {
4926           /* And put the step-breakpoint there and go until there.  */
4927           struct symtab_and_line sr_sal;
4928
4929           init_sal (&sr_sal);   /* initialize to zeroes */
4930           sr_sal.pc = real_stop_pc;
4931           sr_sal.section = find_pc_overlay (sr_sal.pc);
4932           sr_sal.pspace = get_frame_program_space (frame);
4933
4934           /* Do not specify what the fp should be when we stop since
4935              on some machines the prologue is where the new fp value
4936              is established.  */
4937           insert_step_resume_breakpoint_at_sal (gdbarch,
4938                                                 sr_sal, null_frame_id);
4939
4940           /* Restart without fiddling with the step ranges or
4941              other state.  */
4942           keep_going (ecs);
4943           return;
4944         }
4945     }
4946
4947   stop_pc_sal = find_pc_line (stop_pc, 0);
4948
4949   /* NOTE: tausq/2004-05-24: This if block used to be done before all
4950      the trampoline processing logic, however, there are some trampolines 
4951      that have no names, so we should do trampoline handling first.  */
4952   if (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
4953       && ecs->stop_func_name == NULL
4954       && stop_pc_sal.line == 0)
4955     {
4956       if (debug_infrun)
4957          fprintf_unfiltered (gdb_stdlog,
4958                              "infrun: stepped into undebuggable function\n");
4959
4960       /* The inferior just stepped into, or returned to, an
4961          undebuggable function (where there is no debugging information
4962          and no line number corresponding to the address where the
4963          inferior stopped).  Since we want to skip this kind of code,
4964          we keep going until the inferior returns from this
4965          function - unless the user has asked us not to (via
4966          set step-mode) or we no longer know how to get back
4967          to the call site.  */
4968       if (step_stop_if_no_debug
4969           || !frame_id_p (frame_unwind_caller_id (frame)))
4970         {
4971           /* If we have no line number and the step-stop-if-no-debug
4972              is set, we stop the step so that the user has a chance to
4973              switch in assembly mode.  */
4974           ecs->event_thread->control.stop_step = 1;
4975           print_end_stepping_range_reason ();
4976           stop_stepping (ecs);
4977           return;
4978         }
4979       else
4980         {
4981           /* Set a breakpoint at callee's return address (the address
4982              at which the caller will resume).  */
4983           insert_step_resume_breakpoint_at_caller (frame);
4984           keep_going (ecs);
4985           return;
4986         }
4987     }
4988
4989   if (ecs->event_thread->control.step_range_end == 1)
4990     {
4991       /* It is stepi or nexti.  We always want to stop stepping after
4992          one instruction.  */
4993       if (debug_infrun)
4994          fprintf_unfiltered (gdb_stdlog, "infrun: stepi/nexti\n");
4995       ecs->event_thread->control.stop_step = 1;
4996       print_end_stepping_range_reason ();
4997       stop_stepping (ecs);
4998       return;
4999     }
5000
5001   if (stop_pc_sal.line == 0)
5002     {
5003       /* We have no line number information.  That means to stop
5004          stepping (does this always happen right after one instruction,
5005          when we do "s" in a function with no line numbers,
5006          or can this happen as a result of a return or longjmp?).  */
5007       if (debug_infrun)
5008          fprintf_unfiltered (gdb_stdlog, "infrun: no line number info\n");
5009       ecs->event_thread->control.stop_step = 1;
5010       print_end_stepping_range_reason ();
5011       stop_stepping (ecs);
5012       return;
5013     }
5014
5015   /* Look for "calls" to inlined functions, part one.  If the inline
5016      frame machinery detected some skipped call sites, we have entered
5017      a new inline function.  */
5018
5019   if (frame_id_eq (get_frame_id (get_current_frame ()),
5020                    ecs->event_thread->control.step_frame_id)
5021       && inline_skipped_frames (ecs->ptid))
5022     {
5023       struct symtab_and_line call_sal;
5024
5025       if (debug_infrun)
5026         fprintf_unfiltered (gdb_stdlog,
5027                             "infrun: stepped into inlined function\n");
5028
5029       find_frame_sal (get_current_frame (), &call_sal);
5030
5031       if (ecs->event_thread->control.step_over_calls != STEP_OVER_ALL)
5032         {
5033           /* For "step", we're going to stop.  But if the call site
5034              for this inlined function is on the same source line as
5035              we were previously stepping, go down into the function
5036              first.  Otherwise stop at the call site.  */
5037
5038           if (call_sal.line == ecs->event_thread->current_line
5039               && call_sal.symtab == ecs->event_thread->current_symtab)
5040             step_into_inline_frame (ecs->ptid);
5041
5042           ecs->event_thread->control.stop_step = 1;
5043           print_end_stepping_range_reason ();
5044           stop_stepping (ecs);
5045           return;
5046         }
5047       else
5048         {
5049           /* For "next", we should stop at the call site if it is on a
5050              different source line.  Otherwise continue through the
5051              inlined function.  */
5052           if (call_sal.line == ecs->event_thread->current_line
5053               && call_sal.symtab == ecs->event_thread->current_symtab)
5054             keep_going (ecs);
5055           else
5056             {
5057               ecs->event_thread->control.stop_step = 1;
5058               print_end_stepping_range_reason ();
5059               stop_stepping (ecs);
5060             }
5061           return;
5062         }
5063     }
5064
5065   /* Look for "calls" to inlined functions, part two.  If we are still
5066      in the same real function we were stepping through, but we have
5067      to go further up to find the exact frame ID, we are stepping
5068      through a more inlined call beyond its call site.  */
5069
5070   if (get_frame_type (get_current_frame ()) == INLINE_FRAME
5071       && !frame_id_eq (get_frame_id (get_current_frame ()),
5072                        ecs->event_thread->control.step_frame_id)
5073       && stepped_in_from (get_current_frame (),
5074                           ecs->event_thread->control.step_frame_id))
5075     {
5076       if (debug_infrun)
5077         fprintf_unfiltered (gdb_stdlog,
5078                             "infrun: stepping through inlined function\n");
5079
5080       if (ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
5081         keep_going (ecs);
5082       else
5083         {
5084           ecs->event_thread->control.stop_step = 1;
5085           print_end_stepping_range_reason ();
5086           stop_stepping (ecs);
5087         }
5088       return;
5089     }
5090
5091   if ((stop_pc == stop_pc_sal.pc)
5092       && (ecs->event_thread->current_line != stop_pc_sal.line
5093           || ecs->event_thread->current_symtab != stop_pc_sal.symtab))
5094     {
5095       /* We are at the start of a different line.  So stop.  Note that
5096          we don't stop if we step into the middle of a different line.
5097          That is said to make things like for (;;) statements work
5098          better.  */
5099       if (debug_infrun)
5100          fprintf_unfiltered (gdb_stdlog,
5101                              "infrun: stepped to a different line\n");
5102       ecs->event_thread->control.stop_step = 1;
5103       print_end_stepping_range_reason ();
5104       stop_stepping (ecs);
5105       return;
5106     }
5107
5108   /* We aren't done stepping.
5109
5110      Optimize by setting the stepping range to the line.
5111      (We might not be in the original line, but if we entered a
5112      new line in mid-statement, we continue stepping.  This makes
5113      things like for(;;) statements work better.)  */
5114
5115   ecs->event_thread->control.step_range_start = stop_pc_sal.pc;
5116   ecs->event_thread->control.step_range_end = stop_pc_sal.end;
5117   set_step_info (frame, stop_pc_sal);
5118
5119   if (debug_infrun)
5120      fprintf_unfiltered (gdb_stdlog, "infrun: keep going\n");
5121   keep_going (ecs);
5122 }
5123
5124 /* Is thread TP in the middle of single-stepping?  */
5125
5126 static int
5127 currently_stepping (struct thread_info *tp)
5128 {
5129   return ((tp->control.step_range_end
5130            && tp->control.step_resume_breakpoint == NULL)
5131           || tp->control.trap_expected
5132           || tp->stepping_through_solib_after_catch
5133           || bpstat_should_step ());
5134 }
5135
5136 /* Returns true if any thread *but* the one passed in "data" is in the
5137    middle of stepping or of handling a "next".  */
5138
5139 static int
5140 currently_stepping_or_nexting_callback (struct thread_info *tp, void *data)
5141 {
5142   if (tp == data)
5143     return 0;
5144
5145   return (tp->control.step_range_end
5146           || tp->control.trap_expected
5147           || tp->stepping_through_solib_after_catch);
5148 }
5149
5150 /* Inferior has stepped into a subroutine call with source code that
5151    we should not step over.  Do step to the first line of code in
5152    it.  */
5153
5154 static void
5155 handle_step_into_function (struct gdbarch *gdbarch,
5156                            struct execution_control_state *ecs)
5157 {
5158   struct symtab *s;
5159   struct symtab_and_line stop_func_sal, sr_sal;
5160
5161   fill_in_stop_func (gdbarch, ecs);
5162
5163   s = find_pc_symtab (stop_pc);
5164   if (s && s->language != language_asm)
5165     ecs->stop_func_start = gdbarch_skip_prologue (gdbarch,
5166                                                   ecs->stop_func_start);
5167
5168   stop_func_sal = find_pc_line (ecs->stop_func_start, 0);
5169   /* Use the step_resume_break to step until the end of the prologue,
5170      even if that involves jumps (as it seems to on the vax under
5171      4.2).  */
5172   /* If the prologue ends in the middle of a source line, continue to
5173      the end of that source line (if it is still within the function).
5174      Otherwise, just go to end of prologue.  */
5175   if (stop_func_sal.end
5176       && stop_func_sal.pc != ecs->stop_func_start
5177       && stop_func_sal.end < ecs->stop_func_end)
5178     ecs->stop_func_start = stop_func_sal.end;
5179
5180   /* Architectures which require breakpoint adjustment might not be able
5181      to place a breakpoint at the computed address.  If so, the test
5182      ``ecs->stop_func_start == stop_pc'' will never succeed.  Adjust
5183      ecs->stop_func_start to an address at which a breakpoint may be
5184      legitimately placed.
5185
5186      Note:  kevinb/2004-01-19:  On FR-V, if this adjustment is not
5187      made, GDB will enter an infinite loop when stepping through
5188      optimized code consisting of VLIW instructions which contain
5189      subinstructions corresponding to different source lines.  On
5190      FR-V, it's not permitted to place a breakpoint on any but the
5191      first subinstruction of a VLIW instruction.  When a breakpoint is
5192      set, GDB will adjust the breakpoint address to the beginning of
5193      the VLIW instruction.  Thus, we need to make the corresponding
5194      adjustment here when computing the stop address.  */
5195
5196   if (gdbarch_adjust_breakpoint_address_p (gdbarch))
5197     {
5198       ecs->stop_func_start
5199         = gdbarch_adjust_breakpoint_address (gdbarch,
5200                                              ecs->stop_func_start);
5201     }
5202
5203   if (ecs->stop_func_start == stop_pc)
5204     {
5205       /* We are already there: stop now.  */
5206       ecs->event_thread->control.stop_step = 1;
5207       print_end_stepping_range_reason ();
5208       stop_stepping (ecs);
5209       return;
5210     }
5211   else
5212     {
5213       /* Put the step-breakpoint there and go until there.  */
5214       init_sal (&sr_sal);       /* initialize to zeroes */
5215       sr_sal.pc = ecs->stop_func_start;
5216       sr_sal.section = find_pc_overlay (ecs->stop_func_start);
5217       sr_sal.pspace = get_frame_program_space (get_current_frame ());
5218
5219       /* Do not specify what the fp should be when we stop since on
5220          some machines the prologue is where the new fp value is
5221          established.  */
5222       insert_step_resume_breakpoint_at_sal (gdbarch, sr_sal, null_frame_id);
5223
5224       /* And make sure stepping stops right away then.  */
5225       ecs->event_thread->control.step_range_end
5226         = ecs->event_thread->control.step_range_start;
5227     }
5228   keep_going (ecs);
5229 }
5230
5231 /* Inferior has stepped backward into a subroutine call with source
5232    code that we should not step over.  Do step to the beginning of the
5233    last line of code in it.  */
5234
5235 static void
5236 handle_step_into_function_backward (struct gdbarch *gdbarch,
5237                                     struct execution_control_state *ecs)
5238 {
5239   struct symtab *s;
5240   struct symtab_and_line stop_func_sal;
5241
5242   fill_in_stop_func (gdbarch, ecs);
5243
5244   s = find_pc_symtab (stop_pc);
5245   if (s && s->language != language_asm)
5246     ecs->stop_func_start = gdbarch_skip_prologue (gdbarch,
5247                                                   ecs->stop_func_start);
5248
5249   stop_func_sal = find_pc_line (stop_pc, 0);
5250
5251   /* OK, we're just going to keep stepping here.  */
5252   if (stop_func_sal.pc == stop_pc)
5253     {
5254       /* We're there already.  Just stop stepping now.  */
5255       ecs->event_thread->control.stop_step = 1;
5256       print_end_stepping_range_reason ();
5257       stop_stepping (ecs);
5258     }
5259   else
5260     {
5261       /* Else just reset the step range and keep going.
5262          No step-resume breakpoint, they don't work for
5263          epilogues, which can have multiple entry paths.  */
5264       ecs->event_thread->control.step_range_start = stop_func_sal.pc;
5265       ecs->event_thread->control.step_range_end = stop_func_sal.end;
5266       keep_going (ecs);
5267     }
5268   return;
5269 }
5270
5271 /* Insert a "step-resume breakpoint" at SR_SAL with frame ID SR_ID.
5272    This is used to both functions and to skip over code.  */
5273
5274 static void
5275 insert_step_resume_breakpoint_at_sal_1 (struct gdbarch *gdbarch,
5276                                         struct symtab_and_line sr_sal,
5277                                         struct frame_id sr_id,
5278                                         enum bptype sr_type)
5279 {
5280   /* There should never be more than one step-resume or longjmp-resume
5281      breakpoint per thread, so we should never be setting a new
5282      step_resume_breakpoint when one is already active.  */
5283   gdb_assert (inferior_thread ()->control.step_resume_breakpoint == NULL);
5284   gdb_assert (sr_type == bp_step_resume || sr_type == bp_hp_step_resume);
5285
5286   if (debug_infrun)
5287     fprintf_unfiltered (gdb_stdlog,
5288                         "infrun: inserting step-resume breakpoint at %s\n",
5289                         paddress (gdbarch, sr_sal.pc));
5290
5291   inferior_thread ()->control.step_resume_breakpoint
5292     = set_momentary_breakpoint (gdbarch, sr_sal, sr_id, sr_type);
5293 }
5294
5295 void
5296 insert_step_resume_breakpoint_at_sal (struct gdbarch *gdbarch,
5297                                       struct symtab_and_line sr_sal,
5298                                       struct frame_id sr_id)
5299 {
5300   insert_step_resume_breakpoint_at_sal_1 (gdbarch,
5301                                           sr_sal, sr_id,
5302                                           bp_step_resume);
5303 }
5304
5305 /* Insert a "high-priority step-resume breakpoint" at RETURN_FRAME.pc.
5306    This is used to skip a potential signal handler.
5307
5308    This is called with the interrupted function's frame.  The signal
5309    handler, when it returns, will resume the interrupted function at
5310    RETURN_FRAME.pc.  */
5311
5312 static void
5313 insert_hp_step_resume_breakpoint_at_frame (struct frame_info *return_frame)
5314 {
5315   struct symtab_and_line sr_sal;
5316   struct gdbarch *gdbarch;
5317
5318   gdb_assert (return_frame != NULL);
5319   init_sal (&sr_sal);           /* initialize to zeros */
5320
5321   gdbarch = get_frame_arch (return_frame);
5322   sr_sal.pc = gdbarch_addr_bits_remove (gdbarch, get_frame_pc (return_frame));
5323   sr_sal.section = find_pc_overlay (sr_sal.pc);
5324   sr_sal.pspace = get_frame_program_space (return_frame);
5325
5326   insert_step_resume_breakpoint_at_sal_1 (gdbarch, sr_sal,
5327                                           get_stack_frame_id (return_frame),
5328                                           bp_hp_step_resume);
5329 }
5330
5331 /* Insert a "step-resume breakpoint" at the previous frame's PC.  This
5332    is used to skip a function after stepping into it (for "next" or if
5333    the called function has no debugging information).
5334
5335    The current function has almost always been reached by single
5336    stepping a call or return instruction.  NEXT_FRAME belongs to the
5337    current function, and the breakpoint will be set at the caller's
5338    resume address.
5339
5340    This is a separate function rather than reusing
5341    insert_hp_step_resume_breakpoint_at_frame in order to avoid
5342    get_prev_frame, which may stop prematurely (see the implementation
5343    of frame_unwind_caller_id for an example).  */
5344
5345 static void
5346 insert_step_resume_breakpoint_at_caller (struct frame_info *next_frame)
5347 {
5348   struct symtab_and_line sr_sal;
5349   struct gdbarch *gdbarch;
5350
5351   /* We shouldn't have gotten here if we don't know where the call site
5352      is.  */
5353   gdb_assert (frame_id_p (frame_unwind_caller_id (next_frame)));
5354
5355   init_sal (&sr_sal);           /* initialize to zeros */
5356
5357   gdbarch = frame_unwind_caller_arch (next_frame);
5358   sr_sal.pc = gdbarch_addr_bits_remove (gdbarch,
5359                                         frame_unwind_caller_pc (next_frame));
5360   sr_sal.section = find_pc_overlay (sr_sal.pc);
5361   sr_sal.pspace = frame_unwind_program_space (next_frame);
5362
5363   insert_step_resume_breakpoint_at_sal (gdbarch, sr_sal,
5364                                         frame_unwind_caller_id (next_frame));
5365 }
5366
5367 /* Insert a "longjmp-resume" breakpoint at PC.  This is used to set a
5368    new breakpoint at the target of a jmp_buf.  The handling of
5369    longjmp-resume uses the same mechanisms used for handling
5370    "step-resume" breakpoints.  */
5371
5372 static void
5373 insert_longjmp_resume_breakpoint (struct gdbarch *gdbarch, CORE_ADDR pc)
5374 {
5375   /* There should never be more than one step-resume or longjmp-resume
5376      breakpoint per thread, so we should never be setting a new
5377      longjmp_resume_breakpoint when one is already active.  */
5378   gdb_assert (inferior_thread ()->control.step_resume_breakpoint == NULL);
5379
5380   if (debug_infrun)
5381     fprintf_unfiltered (gdb_stdlog,
5382                         "infrun: inserting longjmp-resume breakpoint at %s\n",
5383                         paddress (gdbarch, pc));
5384
5385   inferior_thread ()->control.step_resume_breakpoint =
5386     set_momentary_breakpoint_at_pc (gdbarch, pc, bp_longjmp_resume);
5387 }
5388
5389 /* Insert an exception resume breakpoint.  TP is the thread throwing
5390    the exception.  The block B is the block of the unwinder debug hook
5391    function.  FRAME is the frame corresponding to the call to this
5392    function.  SYM is the symbol of the function argument holding the
5393    target PC of the exception.  */
5394
5395 static void
5396 insert_exception_resume_breakpoint (struct thread_info *tp,
5397                                     struct block *b,
5398                                     struct frame_info *frame,
5399                                     struct symbol *sym)
5400 {
5401   struct gdb_exception e;
5402
5403   /* We want to ignore errors here.  */
5404   TRY_CATCH (e, RETURN_MASK_ERROR)
5405     {
5406       struct symbol *vsym;
5407       struct value *value;
5408       CORE_ADDR handler;
5409       struct breakpoint *bp;
5410
5411       vsym = lookup_symbol (SYMBOL_LINKAGE_NAME (sym), b, VAR_DOMAIN, NULL);
5412       value = read_var_value (vsym, frame);
5413       /* If the value was optimized out, revert to the old behavior.  */
5414       if (! value_optimized_out (value))
5415         {
5416           handler = value_as_address (value);
5417
5418           if (debug_infrun)
5419             fprintf_unfiltered (gdb_stdlog,
5420                                 "infrun: exception resume at %lx\n",
5421                                 (unsigned long) handler);
5422
5423           bp = set_momentary_breakpoint_at_pc (get_frame_arch (frame),
5424                                                handler, bp_exception_resume);
5425           bp->thread = tp->num;
5426           inferior_thread ()->control.exception_resume_breakpoint = bp;
5427         }
5428     }
5429 }
5430
5431 /* This is called when an exception has been intercepted.  Check to
5432    see whether the exception's destination is of interest, and if so,
5433    set an exception resume breakpoint there.  */
5434
5435 static void
5436 check_exception_resume (struct execution_control_state *ecs,
5437                         struct frame_info *frame, struct symbol *func)
5438 {
5439   struct gdb_exception e;
5440
5441   TRY_CATCH (e, RETURN_MASK_ERROR)
5442     {
5443       struct block *b;
5444       struct dict_iterator iter;
5445       struct symbol *sym;
5446       int argno = 0;
5447
5448       /* The exception breakpoint is a thread-specific breakpoint on
5449          the unwinder's debug hook, declared as:
5450          
5451          void _Unwind_DebugHook (void *cfa, void *handler);
5452          
5453          The CFA argument indicates the frame to which control is
5454          about to be transferred.  HANDLER is the destination PC.
5455          
5456          We ignore the CFA and set a temporary breakpoint at HANDLER.
5457          This is not extremely efficient but it avoids issues in gdb
5458          with computing the DWARF CFA, and it also works even in weird
5459          cases such as throwing an exception from inside a signal
5460          handler.  */
5461
5462       b = SYMBOL_BLOCK_VALUE (func);
5463       ALL_BLOCK_SYMBOLS (b, iter, sym)
5464         {
5465           if (!SYMBOL_IS_ARGUMENT (sym))
5466             continue;
5467
5468           if (argno == 0)
5469             ++argno;
5470           else
5471             {
5472               insert_exception_resume_breakpoint (ecs->event_thread,
5473                                                   b, frame, sym);
5474               break;
5475             }
5476         }
5477     }
5478 }
5479
5480 static void
5481 stop_stepping (struct execution_control_state *ecs)
5482 {
5483   if (debug_infrun)
5484     fprintf_unfiltered (gdb_stdlog, "infrun: stop_stepping\n");
5485
5486   /* Let callers know we don't want to wait for the inferior anymore.  */
5487   ecs->wait_some_more = 0;
5488 }
5489
5490 /* This function handles various cases where we need to continue
5491    waiting for the inferior.  */
5492 /* (Used to be the keep_going: label in the old wait_for_inferior).  */
5493
5494 static void
5495 keep_going (struct execution_control_state *ecs)
5496 {
5497   /* Make sure normal_stop is called if we get a QUIT handled before
5498      reaching resume.  */
5499   struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
5500
5501   /* Save the pc before execution, to compare with pc after stop.  */
5502   ecs->event_thread->prev_pc
5503     = regcache_read_pc (get_thread_regcache (ecs->ptid));
5504
5505   /* If we did not do break;, it means we should keep running the
5506      inferior and not return to debugger.  */
5507
5508   if (ecs->event_thread->control.trap_expected
5509       && ecs->event_thread->suspend.stop_signal != TARGET_SIGNAL_TRAP)
5510     {
5511       /* We took a signal (which we are supposed to pass through to
5512          the inferior, else we'd not get here) and we haven't yet
5513          gotten our trap.  Simply continue.  */
5514
5515       discard_cleanups (old_cleanups);
5516       resume (currently_stepping (ecs->event_thread),
5517               ecs->event_thread->suspend.stop_signal);
5518     }
5519   else
5520     {
5521       /* Either the trap was not expected, but we are continuing
5522          anyway (the user asked that this signal be passed to the
5523          child)
5524          -- or --
5525          The signal was SIGTRAP, e.g. it was our signal, but we
5526          decided we should resume from it.
5527
5528          We're going to run this baby now!  
5529
5530          Note that insert_breakpoints won't try to re-insert
5531          already inserted breakpoints.  Therefore, we don't
5532          care if breakpoints were already inserted, or not.  */
5533       
5534       if (ecs->event_thread->stepping_over_breakpoint)
5535         {
5536           struct regcache *thread_regcache = get_thread_regcache (ecs->ptid);
5537
5538           if (!use_displaced_stepping (get_regcache_arch (thread_regcache)))
5539             /* Since we can't do a displaced step, we have to remove
5540                the breakpoint while we step it.  To keep things
5541                simple, we remove them all.  */
5542             remove_breakpoints ();
5543         }
5544       else
5545         {
5546           struct gdb_exception e;
5547
5548           /* Stop stepping when inserting breakpoints
5549              has failed.  */
5550           TRY_CATCH (e, RETURN_MASK_ERROR)
5551             {
5552               insert_breakpoints ();
5553             }
5554           if (e.reason < 0)
5555             {
5556               exception_print (gdb_stderr, e);
5557               stop_stepping (ecs);
5558               return;
5559             }
5560         }
5561
5562       ecs->event_thread->control.trap_expected
5563         = ecs->event_thread->stepping_over_breakpoint;
5564
5565       /* Do not deliver SIGNAL_TRAP (except when the user explicitly
5566          specifies that such a signal should be delivered to the
5567          target program).
5568
5569          Typically, this would occure when a user is debugging a
5570          target monitor on a simulator: the target monitor sets a
5571          breakpoint; the simulator encounters this break-point and
5572          halts the simulation handing control to GDB; GDB, noteing
5573          that the break-point isn't valid, returns control back to the
5574          simulator; the simulator then delivers the hardware
5575          equivalent of a SIGNAL_TRAP to the program being debugged.  */
5576
5577       if (ecs->event_thread->suspend.stop_signal == TARGET_SIGNAL_TRAP
5578           && !signal_program[ecs->event_thread->suspend.stop_signal])
5579         ecs->event_thread->suspend.stop_signal = TARGET_SIGNAL_0;
5580
5581       discard_cleanups (old_cleanups);
5582       resume (currently_stepping (ecs->event_thread),
5583               ecs->event_thread->suspend.stop_signal);
5584     }
5585
5586   prepare_to_wait (ecs);
5587 }
5588
5589 /* This function normally comes after a resume, before
5590    handle_inferior_event exits.  It takes care of any last bits of
5591    housekeeping, and sets the all-important wait_some_more flag.  */
5592
5593 static void
5594 prepare_to_wait (struct execution_control_state *ecs)
5595 {
5596   if (debug_infrun)
5597     fprintf_unfiltered (gdb_stdlog, "infrun: prepare_to_wait\n");
5598
5599   /* This is the old end of the while loop.  Let everybody know we
5600      want to wait for the inferior some more and get called again
5601      soon.  */
5602   ecs->wait_some_more = 1;
5603 }
5604
5605 /* Several print_*_reason functions to print why the inferior has stopped.
5606    We always print something when the inferior exits, or receives a signal.
5607    The rest of the cases are dealt with later on in normal_stop and
5608    print_it_typical.  Ideally there should be a call to one of these
5609    print_*_reason functions functions from handle_inferior_event each time
5610    stop_stepping is called.  */
5611
5612 /* Print why the inferior has stopped.  
5613    We are done with a step/next/si/ni command, print why the inferior has
5614    stopped.  For now print nothing.  Print a message only if not in the middle
5615    of doing a "step n" operation for n > 1.  */
5616
5617 static void
5618 print_end_stepping_range_reason (void)
5619 {
5620   if ((!inferior_thread ()->step_multi
5621        || !inferior_thread ()->control.stop_step)
5622       && ui_out_is_mi_like_p (current_uiout))
5623     ui_out_field_string (current_uiout, "reason",
5624                          async_reason_lookup (EXEC_ASYNC_END_STEPPING_RANGE));
5625 }
5626
5627 /* The inferior was terminated by a signal, print why it stopped.  */
5628
5629 static void
5630 print_signal_exited_reason (enum target_signal siggnal)
5631 {
5632   struct ui_out *uiout = current_uiout;
5633
5634   annotate_signalled ();
5635   if (ui_out_is_mi_like_p (uiout))
5636     ui_out_field_string
5637       (uiout, "reason", async_reason_lookup (EXEC_ASYNC_EXITED_SIGNALLED));
5638   ui_out_text (uiout, "\nProgram terminated with signal ");
5639   annotate_signal_name ();
5640   ui_out_field_string (uiout, "signal-name",
5641                        target_signal_to_name (siggnal));
5642   annotate_signal_name_end ();
5643   ui_out_text (uiout, ", ");
5644   annotate_signal_string ();
5645   ui_out_field_string (uiout, "signal-meaning",
5646                        target_signal_to_string (siggnal));
5647   annotate_signal_string_end ();
5648   ui_out_text (uiout, ".\n");
5649   ui_out_text (uiout, "The program no longer exists.\n");
5650 }
5651
5652 /* The inferior program is finished, print why it stopped.  */
5653
5654 static void
5655 print_exited_reason (int exitstatus)
5656 {
5657   struct inferior *inf = current_inferior ();
5658   const char *pidstr = target_pid_to_str (pid_to_ptid (inf->pid));
5659   struct ui_out *uiout = current_uiout;
5660
5661   annotate_exited (exitstatus);
5662   if (exitstatus)
5663     {
5664       if (ui_out_is_mi_like_p (uiout))
5665         ui_out_field_string (uiout, "reason", 
5666                              async_reason_lookup (EXEC_ASYNC_EXITED));
5667       ui_out_text (uiout, "[Inferior ");
5668       ui_out_text (uiout, plongest (inf->num));
5669       ui_out_text (uiout, " (");
5670       ui_out_text (uiout, pidstr);
5671       ui_out_text (uiout, ") exited with code ");
5672       ui_out_field_fmt (uiout, "exit-code", "0%o", (unsigned int) exitstatus);
5673       ui_out_text (uiout, "]\n");
5674     }
5675   else
5676     {
5677       if (ui_out_is_mi_like_p (uiout))
5678         ui_out_field_string
5679           (uiout, "reason", async_reason_lookup (EXEC_ASYNC_EXITED_NORMALLY));
5680       ui_out_text (uiout, "[Inferior ");
5681       ui_out_text (uiout, plongest (inf->num));
5682       ui_out_text (uiout, " (");
5683       ui_out_text (uiout, pidstr);
5684       ui_out_text (uiout, ") exited normally]\n");
5685     }
5686   /* Support the --return-child-result option.  */
5687   return_child_result_value = exitstatus;
5688 }
5689
5690 /* Signal received, print why the inferior has stopped.  The signal table
5691    tells us to print about it.  */
5692
5693 static void
5694 print_signal_received_reason (enum target_signal siggnal)
5695 {
5696   struct ui_out *uiout = current_uiout;
5697
5698   annotate_signal ();
5699
5700   if (siggnal == TARGET_SIGNAL_0 && !ui_out_is_mi_like_p (uiout))
5701     {
5702       struct thread_info *t = inferior_thread ();
5703
5704       ui_out_text (uiout, "\n[");
5705       ui_out_field_string (uiout, "thread-name",
5706                            target_pid_to_str (t->ptid));
5707       ui_out_field_fmt (uiout, "thread-id", "] #%d", t->num);
5708       ui_out_text (uiout, " stopped");
5709     }
5710   else
5711     {
5712       ui_out_text (uiout, "\nProgram received signal ");
5713       annotate_signal_name ();
5714       if (ui_out_is_mi_like_p (uiout))
5715         ui_out_field_string
5716           (uiout, "reason", async_reason_lookup (EXEC_ASYNC_SIGNAL_RECEIVED));
5717       ui_out_field_string (uiout, "signal-name",
5718                            target_signal_to_name (siggnal));
5719       annotate_signal_name_end ();
5720       ui_out_text (uiout, ", ");
5721       annotate_signal_string ();
5722       ui_out_field_string (uiout, "signal-meaning",
5723                            target_signal_to_string (siggnal));
5724       annotate_signal_string_end ();
5725     }
5726   ui_out_text (uiout, ".\n");
5727 }
5728
5729 /* Reverse execution: target ran out of history info, print why the inferior
5730    has stopped.  */
5731
5732 static void
5733 print_no_history_reason (void)
5734 {
5735   ui_out_text (current_uiout, "\nNo more reverse-execution history.\n");
5736 }
5737
5738 /* Here to return control to GDB when the inferior stops for real.
5739    Print appropriate messages, remove breakpoints, give terminal our modes.
5740
5741    STOP_PRINT_FRAME nonzero means print the executing frame
5742    (pc, function, args, file, line number and line text).
5743    BREAKPOINTS_FAILED nonzero means stop was due to error
5744    attempting to insert breakpoints.  */
5745
5746 void
5747 normal_stop (void)
5748 {
5749   struct target_waitstatus last;
5750   ptid_t last_ptid;
5751   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
5752
5753   get_last_target_status (&last_ptid, &last);
5754
5755   /* If an exception is thrown from this point on, make sure to
5756      propagate GDB's knowledge of the executing state to the
5757      frontend/user running state.  A QUIT is an easy exception to see
5758      here, so do this before any filtered output.  */
5759   if (!non_stop)
5760     make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
5761   else if (last.kind != TARGET_WAITKIND_SIGNALLED
5762            && last.kind != TARGET_WAITKIND_EXITED)
5763     make_cleanup (finish_thread_state_cleanup, &inferior_ptid);
5764
5765   /* In non-stop mode, we don't want GDB to switch threads behind the
5766      user's back, to avoid races where the user is typing a command to
5767      apply to thread x, but GDB switches to thread y before the user
5768      finishes entering the command.  */
5769
5770   /* As with the notification of thread events, we want to delay
5771      notifying the user that we've switched thread context until
5772      the inferior actually stops.
5773
5774      There's no point in saying anything if the inferior has exited.
5775      Note that SIGNALLED here means "exited with a signal", not
5776      "received a signal".  */
5777   if (!non_stop
5778       && !ptid_equal (previous_inferior_ptid, inferior_ptid)
5779       && target_has_execution
5780       && last.kind != TARGET_WAITKIND_SIGNALLED
5781       && last.kind != TARGET_WAITKIND_EXITED)
5782     {
5783       target_terminal_ours_for_output ();
5784       printf_filtered (_("[Switching to %s]\n"),
5785                        target_pid_to_str (inferior_ptid));
5786       annotate_thread_changed ();
5787       previous_inferior_ptid = inferior_ptid;
5788     }
5789
5790   if (!breakpoints_always_inserted_mode () && target_has_execution)
5791     {
5792       if (remove_breakpoints ())
5793         {
5794           target_terminal_ours_for_output ();
5795           printf_filtered (_("Cannot remove breakpoints because "
5796                              "program is no longer writable.\nFurther "
5797                              "execution is probably impossible.\n"));
5798         }
5799     }
5800
5801   /* If an auto-display called a function and that got a signal,
5802      delete that auto-display to avoid an infinite recursion.  */
5803
5804   if (stopped_by_random_signal)
5805     disable_current_display ();
5806
5807   /* Don't print a message if in the middle of doing a "step n"
5808      operation for n > 1 */
5809   if (target_has_execution
5810       && last.kind != TARGET_WAITKIND_SIGNALLED
5811       && last.kind != TARGET_WAITKIND_EXITED
5812       && inferior_thread ()->step_multi
5813       && inferior_thread ()->control.stop_step)
5814     goto done;
5815
5816   target_terminal_ours ();
5817
5818   /* Set the current source location.  This will also happen if we
5819      display the frame below, but the current SAL will be incorrect
5820      during a user hook-stop function.  */
5821   if (has_stack_frames () && !stop_stack_dummy)
5822     set_current_sal_from_frame (get_current_frame (), 1);
5823
5824   /* Let the user/frontend see the threads as stopped.  */
5825   do_cleanups (old_chain);
5826
5827   /* Look up the hook_stop and run it (CLI internally handles problem
5828      of stop_command's pre-hook not existing).  */
5829   if (stop_command)
5830     catch_errors (hook_stop_stub, stop_command,
5831                   "Error while running hook_stop:\n", RETURN_MASK_ALL);
5832
5833   if (!has_stack_frames ())
5834     goto done;
5835
5836   if (last.kind == TARGET_WAITKIND_SIGNALLED
5837       || last.kind == TARGET_WAITKIND_EXITED)
5838     goto done;
5839
5840   /* Select innermost stack frame - i.e., current frame is frame 0,
5841      and current location is based on that.
5842      Don't do this on return from a stack dummy routine,
5843      or if the program has exited.  */
5844
5845   if (!stop_stack_dummy)
5846     {
5847       select_frame (get_current_frame ());
5848
5849       /* Print current location without a level number, if
5850          we have changed functions or hit a breakpoint.
5851          Print source line if we have one.
5852          bpstat_print() contains the logic deciding in detail
5853          what to print, based on the event(s) that just occurred.  */
5854
5855       /* If --batch-silent is enabled then there's no need to print the current
5856          source location, and to try risks causing an error message about
5857          missing source files.  */
5858       if (stop_print_frame && !batch_silent)
5859         {
5860           int bpstat_ret;
5861           int source_flag;
5862           int do_frame_printing = 1;
5863           struct thread_info *tp = inferior_thread ();
5864
5865           bpstat_ret = bpstat_print (tp->control.stop_bpstat);
5866           switch (bpstat_ret)
5867             {
5868             case PRINT_UNKNOWN:
5869               /* If we had hit a shared library event breakpoint,
5870                  bpstat_print would print out this message.  If we hit
5871                  an OS-level shared library event, do the same
5872                  thing.  */
5873               if (last.kind == TARGET_WAITKIND_LOADED)
5874                 {
5875                   printf_filtered (_("Stopped due to shared library event\n"));
5876                   source_flag = SRC_LINE;       /* something bogus */
5877                   do_frame_printing = 0;
5878                   break;
5879                 }
5880
5881               /* FIXME: cagney/2002-12-01: Given that a frame ID does
5882                  (or should) carry around the function and does (or
5883                  should) use that when doing a frame comparison.  */
5884               if (tp->control.stop_step
5885                   && frame_id_eq (tp->control.step_frame_id,
5886                                   get_frame_id (get_current_frame ()))
5887                   && step_start_function == find_pc_function (stop_pc))
5888                 source_flag = SRC_LINE;         /* Finished step, just
5889                                                    print source line.  */
5890               else
5891                 source_flag = SRC_AND_LOC;      /* Print location and
5892                                                    source line.  */
5893               break;
5894             case PRINT_SRC_AND_LOC:
5895               source_flag = SRC_AND_LOC;        /* Print location and
5896                                                    source line.  */
5897               break;
5898             case PRINT_SRC_ONLY:
5899               source_flag = SRC_LINE;
5900               break;
5901             case PRINT_NOTHING:
5902               source_flag = SRC_LINE;   /* something bogus */
5903               do_frame_printing = 0;
5904               break;
5905             default:
5906               internal_error (__FILE__, __LINE__, _("Unknown value."));
5907             }
5908
5909           /* The behavior of this routine with respect to the source
5910              flag is:
5911              SRC_LINE: Print only source line
5912              LOCATION: Print only location
5913              SRC_AND_LOC: Print location and source line.  */
5914           if (do_frame_printing)
5915             print_stack_frame (get_selected_frame (NULL), 0, source_flag);
5916
5917           /* Display the auto-display expressions.  */
5918           do_displays ();
5919         }
5920     }
5921
5922   /* Save the function value return registers, if we care.
5923      We might be about to restore their previous contents.  */
5924   if (inferior_thread ()->control.proceed_to_finish
5925       && execution_direction != EXEC_REVERSE)
5926     {
5927       /* This should not be necessary.  */
5928       if (stop_registers)
5929         regcache_xfree (stop_registers);
5930
5931       /* NB: The copy goes through to the target picking up the value of
5932          all the registers.  */
5933       stop_registers = regcache_dup (get_current_regcache ());
5934     }
5935
5936   if (stop_stack_dummy == STOP_STACK_DUMMY)
5937     {
5938       /* Pop the empty frame that contains the stack dummy.
5939          This also restores inferior state prior to the call
5940          (struct infcall_suspend_state).  */
5941       struct frame_info *frame = get_current_frame ();
5942
5943       gdb_assert (get_frame_type (frame) == DUMMY_FRAME);
5944       frame_pop (frame);
5945       /* frame_pop() calls reinit_frame_cache as the last thing it
5946          does which means there's currently no selected frame.  We
5947          don't need to re-establish a selected frame if the dummy call
5948          returns normally, that will be done by
5949          restore_infcall_control_state.  However, we do have to handle
5950          the case where the dummy call is returning after being
5951          stopped (e.g. the dummy call previously hit a breakpoint).
5952          We can't know which case we have so just always re-establish
5953          a selected frame here.  */
5954       select_frame (get_current_frame ());
5955     }
5956
5957 done:
5958   annotate_stopped ();
5959
5960   /* Suppress the stop observer if we're in the middle of:
5961
5962      - a step n (n > 1), as there still more steps to be done.
5963
5964      - a "finish" command, as the observer will be called in
5965        finish_command_continuation, so it can include the inferior
5966        function's return value.
5967
5968      - calling an inferior function, as we pretend we inferior didn't
5969        run at all.  The return value of the call is handled by the
5970        expression evaluator, through call_function_by_hand.  */
5971
5972   if (!target_has_execution
5973       || last.kind == TARGET_WAITKIND_SIGNALLED
5974       || last.kind == TARGET_WAITKIND_EXITED
5975       || (!inferior_thread ()->step_multi
5976           && !(inferior_thread ()->control.stop_bpstat
5977                && inferior_thread ()->control.proceed_to_finish)
5978           && !inferior_thread ()->control.in_infcall))
5979     {
5980       if (!ptid_equal (inferior_ptid, null_ptid))
5981         observer_notify_normal_stop (inferior_thread ()->control.stop_bpstat,
5982                                      stop_print_frame);
5983       else
5984         observer_notify_normal_stop (NULL, stop_print_frame);
5985     }
5986
5987   if (target_has_execution)
5988     {
5989       if (last.kind != TARGET_WAITKIND_SIGNALLED
5990           && last.kind != TARGET_WAITKIND_EXITED)
5991         /* Delete the breakpoint we stopped at, if it wants to be deleted.
5992            Delete any breakpoint that is to be deleted at the next stop.  */
5993         breakpoint_auto_delete (inferior_thread ()->control.stop_bpstat);
5994     }
5995
5996   /* Try to get rid of automatically added inferiors that are no
5997      longer needed.  Keeping those around slows down things linearly.
5998      Note that this never removes the current inferior.  */
5999   prune_inferiors ();
6000 }
6001
6002 static int
6003 hook_stop_stub (void *cmd)
6004 {
6005   execute_cmd_pre_hook ((struct cmd_list_element *) cmd);
6006   return (0);
6007 }
6008 \f
6009 int
6010 signal_stop_state (int signo)
6011 {
6012   return signal_stop[signo];
6013 }
6014
6015 int
6016 signal_print_state (int signo)
6017 {
6018   return signal_print[signo];
6019 }
6020
6021 int
6022 signal_pass_state (int signo)
6023 {
6024   return signal_program[signo];
6025 }
6026
6027 static void
6028 signal_cache_update (int signo)
6029 {
6030   if (signo == -1)
6031     {
6032       for (signo = 0; signo < (int) TARGET_SIGNAL_LAST; signo++)
6033         signal_cache_update (signo);
6034
6035       return;
6036     }
6037
6038   signal_pass[signo] = (signal_stop[signo] == 0
6039                         && signal_print[signo] == 0
6040                         && signal_program[signo] == 1);
6041 }
6042
6043 int
6044 signal_stop_update (int signo, int state)
6045 {
6046   int ret = signal_stop[signo];
6047
6048   signal_stop[signo] = state;
6049   signal_cache_update (signo);
6050   return ret;
6051 }
6052
6053 int
6054 signal_print_update (int signo, int state)
6055 {
6056   int ret = signal_print[signo];
6057
6058   signal_print[signo] = state;
6059   signal_cache_update (signo);
6060   return ret;
6061 }
6062
6063 int
6064 signal_pass_update (int signo, int state)
6065 {
6066   int ret = signal_program[signo];
6067
6068   signal_program[signo] = state;
6069   signal_cache_update (signo);
6070   return ret;
6071 }
6072
6073 static void
6074 sig_print_header (void)
6075 {
6076   printf_filtered (_("Signal        Stop\tPrint\tPass "
6077                      "to program\tDescription\n"));
6078 }
6079
6080 static void
6081 sig_print_info (enum target_signal oursig)
6082 {
6083   const char *name = target_signal_to_name (oursig);
6084   int name_padding = 13 - strlen (name);
6085
6086   if (name_padding <= 0)
6087     name_padding = 0;
6088
6089   printf_filtered ("%s", name);
6090   printf_filtered ("%*.*s ", name_padding, name_padding, "                 ");
6091   printf_filtered ("%s\t", signal_stop[oursig] ? "Yes" : "No");
6092   printf_filtered ("%s\t", signal_print[oursig] ? "Yes" : "No");
6093   printf_filtered ("%s\t\t", signal_program[oursig] ? "Yes" : "No");
6094   printf_filtered ("%s\n", target_signal_to_string (oursig));
6095 }
6096
6097 /* Specify how various signals in the inferior should be handled.  */
6098
6099 static void
6100 handle_command (char *args, int from_tty)
6101 {
6102   char **argv;
6103   int digits, wordlen;
6104   int sigfirst, signum, siglast;
6105   enum target_signal oursig;
6106   int allsigs;
6107   int nsigs;
6108   unsigned char *sigs;
6109   struct cleanup *old_chain;
6110
6111   if (args == NULL)
6112     {
6113       error_no_arg (_("signal to handle"));
6114     }
6115
6116   /* Allocate and zero an array of flags for which signals to handle.  */
6117
6118   nsigs = (int) TARGET_SIGNAL_LAST;
6119   sigs = (unsigned char *) alloca (nsigs);
6120   memset (sigs, 0, nsigs);
6121
6122   /* Break the command line up into args.  */
6123
6124   argv = gdb_buildargv (args);
6125   old_chain = make_cleanup_freeargv (argv);
6126
6127   /* Walk through the args, looking for signal oursigs, signal names, and
6128      actions.  Signal numbers and signal names may be interspersed with
6129      actions, with the actions being performed for all signals cumulatively
6130      specified.  Signal ranges can be specified as <LOW>-<HIGH>.  */
6131
6132   while (*argv != NULL)
6133     {
6134       wordlen = strlen (*argv);
6135       for (digits = 0; isdigit ((*argv)[digits]); digits++)
6136         {;
6137         }
6138       allsigs = 0;
6139       sigfirst = siglast = -1;
6140
6141       if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
6142         {
6143           /* Apply action to all signals except those used by the
6144              debugger.  Silently skip those.  */
6145           allsigs = 1;
6146           sigfirst = 0;
6147           siglast = nsigs - 1;
6148         }
6149       else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
6150         {
6151           SET_SIGS (nsigs, sigs, signal_stop);
6152           SET_SIGS (nsigs, sigs, signal_print);
6153         }
6154       else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
6155         {
6156           UNSET_SIGS (nsigs, sigs, signal_program);
6157         }
6158       else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
6159         {
6160           SET_SIGS (nsigs, sigs, signal_print);
6161         }
6162       else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
6163         {
6164           SET_SIGS (nsigs, sigs, signal_program);
6165         }
6166       else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
6167         {
6168           UNSET_SIGS (nsigs, sigs, signal_stop);
6169         }
6170       else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
6171         {
6172           SET_SIGS (nsigs, sigs, signal_program);
6173         }
6174       else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
6175         {
6176           UNSET_SIGS (nsigs, sigs, signal_print);
6177           UNSET_SIGS (nsigs, sigs, signal_stop);
6178         }
6179       else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
6180         {
6181           UNSET_SIGS (nsigs, sigs, signal_program);
6182         }
6183       else if (digits > 0)
6184         {
6185           /* It is numeric.  The numeric signal refers to our own
6186              internal signal numbering from target.h, not to host/target
6187              signal  number.  This is a feature; users really should be
6188              using symbolic names anyway, and the common ones like
6189              SIGHUP, SIGINT, SIGALRM, etc. will work right anyway.  */
6190
6191           sigfirst = siglast = (int)
6192             target_signal_from_command (atoi (*argv));
6193           if ((*argv)[digits] == '-')
6194             {
6195               siglast = (int)
6196                 target_signal_from_command (atoi ((*argv) + digits + 1));
6197             }
6198           if (sigfirst > siglast)
6199             {
6200               /* Bet he didn't figure we'd think of this case...  */
6201               signum = sigfirst;
6202               sigfirst = siglast;
6203               siglast = signum;
6204             }
6205         }
6206       else
6207         {
6208           oursig = target_signal_from_name (*argv);
6209           if (oursig != TARGET_SIGNAL_UNKNOWN)
6210             {
6211               sigfirst = siglast = (int) oursig;
6212             }
6213           else
6214             {
6215               /* Not a number and not a recognized flag word => complain.  */
6216               error (_("Unrecognized or ambiguous flag word: \"%s\"."), *argv);
6217             }
6218         }
6219
6220       /* If any signal numbers or symbol names were found, set flags for
6221          which signals to apply actions to.  */
6222
6223       for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
6224         {
6225           switch ((enum target_signal) signum)
6226             {
6227             case TARGET_SIGNAL_TRAP:
6228             case TARGET_SIGNAL_INT:
6229               if (!allsigs && !sigs[signum])
6230                 {
6231                   if (query (_("%s is used by the debugger.\n\
6232 Are you sure you want to change it? "),
6233                              target_signal_to_name ((enum target_signal) signum)))
6234                     {
6235                       sigs[signum] = 1;
6236                     }
6237                   else
6238                     {
6239                       printf_unfiltered (_("Not confirmed, unchanged.\n"));
6240                       gdb_flush (gdb_stdout);
6241                     }
6242                 }
6243               break;
6244             case TARGET_SIGNAL_0:
6245             case TARGET_SIGNAL_DEFAULT:
6246             case TARGET_SIGNAL_UNKNOWN:
6247               /* Make sure that "all" doesn't print these.  */
6248               break;
6249             default:
6250               sigs[signum] = 1;
6251               break;
6252             }
6253         }
6254
6255       argv++;
6256     }
6257
6258   for (signum = 0; signum < nsigs; signum++)
6259     if (sigs[signum])
6260       {
6261         signal_cache_update (-1);
6262         target_pass_signals ((int) TARGET_SIGNAL_LAST, signal_pass);
6263
6264         if (from_tty)
6265           {
6266             /* Show the results.  */
6267             sig_print_header ();
6268             for (; signum < nsigs; signum++)
6269               if (sigs[signum])
6270                 sig_print_info (signum);
6271           }
6272
6273         break;
6274       }
6275
6276   do_cleanups (old_chain);
6277 }
6278
6279 static void
6280 xdb_handle_command (char *args, int from_tty)
6281 {
6282   char **argv;
6283   struct cleanup *old_chain;
6284
6285   if (args == NULL)
6286     error_no_arg (_("xdb command"));
6287
6288   /* Break the command line up into args.  */
6289
6290   argv = gdb_buildargv (args);
6291   old_chain = make_cleanup_freeargv (argv);
6292   if (argv[1] != (char *) NULL)
6293     {
6294       char *argBuf;
6295       int bufLen;
6296
6297       bufLen = strlen (argv[0]) + 20;
6298       argBuf = (char *) xmalloc (bufLen);
6299       if (argBuf)
6300         {
6301           int validFlag = 1;
6302           enum target_signal oursig;
6303
6304           oursig = target_signal_from_name (argv[0]);
6305           memset (argBuf, 0, bufLen);
6306           if (strcmp (argv[1], "Q") == 0)
6307             sprintf (argBuf, "%s %s", argv[0], "noprint");
6308           else
6309             {
6310               if (strcmp (argv[1], "s") == 0)
6311                 {
6312                   if (!signal_stop[oursig])
6313                     sprintf (argBuf, "%s %s", argv[0], "stop");
6314                   else
6315                     sprintf (argBuf, "%s %s", argv[0], "nostop");
6316                 }
6317               else if (strcmp (argv[1], "i") == 0)
6318                 {
6319                   if (!signal_program[oursig])
6320                     sprintf (argBuf, "%s %s", argv[0], "pass");
6321                   else
6322                     sprintf (argBuf, "%s %s", argv[0], "nopass");
6323                 }
6324               else if (strcmp (argv[1], "r") == 0)
6325                 {
6326                   if (!signal_print[oursig])
6327                     sprintf (argBuf, "%s %s", argv[0], "print");
6328                   else
6329                     sprintf (argBuf, "%s %s", argv[0], "noprint");
6330                 }
6331               else
6332                 validFlag = 0;
6333             }
6334           if (validFlag)
6335             handle_command (argBuf, from_tty);
6336           else
6337             printf_filtered (_("Invalid signal handling flag.\n"));
6338           if (argBuf)
6339             xfree (argBuf);
6340         }
6341     }
6342   do_cleanups (old_chain);
6343 }
6344
6345 /* Print current contents of the tables set by the handle command.
6346    It is possible we should just be printing signals actually used
6347    by the current target (but for things to work right when switching
6348    targets, all signals should be in the signal tables).  */
6349
6350 static void
6351 signals_info (char *signum_exp, int from_tty)
6352 {
6353   enum target_signal oursig;
6354
6355   sig_print_header ();
6356
6357   if (signum_exp)
6358     {
6359       /* First see if this is a symbol name.  */
6360       oursig = target_signal_from_name (signum_exp);
6361       if (oursig == TARGET_SIGNAL_UNKNOWN)
6362         {
6363           /* No, try numeric.  */
6364           oursig =
6365             target_signal_from_command (parse_and_eval_long (signum_exp));
6366         }
6367       sig_print_info (oursig);
6368       return;
6369     }
6370
6371   printf_filtered ("\n");
6372   /* These ugly casts brought to you by the native VAX compiler.  */
6373   for (oursig = TARGET_SIGNAL_FIRST;
6374        (int) oursig < (int) TARGET_SIGNAL_LAST;
6375        oursig = (enum target_signal) ((int) oursig + 1))
6376     {
6377       QUIT;
6378
6379       if (oursig != TARGET_SIGNAL_UNKNOWN
6380           && oursig != TARGET_SIGNAL_DEFAULT && oursig != TARGET_SIGNAL_0)
6381         sig_print_info (oursig);
6382     }
6383
6384   printf_filtered (_("\nUse the \"handle\" command "
6385                      "to change these tables.\n"));
6386 }
6387
6388 /* Check if it makes sense to read $_siginfo from the current thread
6389    at this point.  If not, throw an error.  */
6390
6391 static void
6392 validate_siginfo_access (void)
6393 {
6394   /* No current inferior, no siginfo.  */
6395   if (ptid_equal (inferior_ptid, null_ptid))
6396     error (_("No thread selected."));
6397
6398   /* Don't try to read from a dead thread.  */
6399   if (is_exited (inferior_ptid))
6400     error (_("The current thread has terminated"));
6401
6402   /* ... or from a spinning thread.  */
6403   if (is_running (inferior_ptid))
6404     error (_("Selected thread is running."));
6405 }
6406
6407 /* The $_siginfo convenience variable is a bit special.  We don't know
6408    for sure the type of the value until we actually have a chance to
6409    fetch the data.  The type can change depending on gdbarch, so it is
6410    also dependent on which thread you have selected.
6411
6412      1. making $_siginfo be an internalvar that creates a new value on
6413      access.
6414
6415      2. making the value of $_siginfo be an lval_computed value.  */
6416
6417 /* This function implements the lval_computed support for reading a
6418    $_siginfo value.  */
6419
6420 static void
6421 siginfo_value_read (struct value *v)
6422 {
6423   LONGEST transferred;
6424
6425   validate_siginfo_access ();
6426
6427   transferred =
6428     target_read (&current_target, TARGET_OBJECT_SIGNAL_INFO,
6429                  NULL,
6430                  value_contents_all_raw (v),
6431                  value_offset (v),
6432                  TYPE_LENGTH (value_type (v)));
6433
6434   if (transferred != TYPE_LENGTH (value_type (v)))
6435     error (_("Unable to read siginfo"));
6436 }
6437
6438 /* This function implements the lval_computed support for writing a
6439    $_siginfo value.  */
6440
6441 static void
6442 siginfo_value_write (struct value *v, struct value *fromval)
6443 {
6444   LONGEST transferred;
6445
6446   validate_siginfo_access ();
6447
6448   transferred = target_write (&current_target,
6449                               TARGET_OBJECT_SIGNAL_INFO,
6450                               NULL,
6451                               value_contents_all_raw (fromval),
6452                               value_offset (v),
6453                               TYPE_LENGTH (value_type (fromval)));
6454
6455   if (transferred != TYPE_LENGTH (value_type (fromval)))
6456     error (_("Unable to write siginfo"));
6457 }
6458
6459 static const struct lval_funcs siginfo_value_funcs =
6460   {
6461     siginfo_value_read,
6462     siginfo_value_write
6463   };
6464
6465 /* Return a new value with the correct type for the siginfo object of
6466    the current thread using architecture GDBARCH.  Return a void value
6467    if there's no object available.  */
6468
6469 static struct value *
6470 siginfo_make_value (struct gdbarch *gdbarch, struct internalvar *var)
6471 {
6472   if (target_has_stack
6473       && !ptid_equal (inferior_ptid, null_ptid)
6474       && gdbarch_get_siginfo_type_p (gdbarch))
6475     {
6476       struct type *type = gdbarch_get_siginfo_type (gdbarch);
6477
6478       return allocate_computed_value (type, &siginfo_value_funcs, NULL);
6479     }
6480
6481   return allocate_value (builtin_type (gdbarch)->builtin_void);
6482 }
6483
6484 \f
6485 /* infcall_suspend_state contains state about the program itself like its
6486    registers and any signal it received when it last stopped.
6487    This state must be restored regardless of how the inferior function call
6488    ends (either successfully, or after it hits a breakpoint or signal)
6489    if the program is to properly continue where it left off.  */
6490
6491 struct infcall_suspend_state
6492 {
6493   struct thread_suspend_state thread_suspend;
6494   struct inferior_suspend_state inferior_suspend;
6495
6496   /* Other fields:  */
6497   CORE_ADDR stop_pc;
6498   struct regcache *registers;
6499
6500   /* Format of SIGINFO_DATA or NULL if it is not present.  */
6501   struct gdbarch *siginfo_gdbarch;
6502
6503   /* The inferior format depends on SIGINFO_GDBARCH and it has a length of
6504      TYPE_LENGTH (gdbarch_get_siginfo_type ()).  For different gdbarch the
6505      content would be invalid.  */
6506   gdb_byte *siginfo_data;
6507 };
6508
6509 struct infcall_suspend_state *
6510 save_infcall_suspend_state (void)
6511 {
6512   struct infcall_suspend_state *inf_state;
6513   struct thread_info *tp = inferior_thread ();
6514   struct inferior *inf = current_inferior ();
6515   struct regcache *regcache = get_current_regcache ();
6516   struct gdbarch *gdbarch = get_regcache_arch (regcache);
6517   gdb_byte *siginfo_data = NULL;
6518
6519   if (gdbarch_get_siginfo_type_p (gdbarch))
6520     {
6521       struct type *type = gdbarch_get_siginfo_type (gdbarch);
6522       size_t len = TYPE_LENGTH (type);
6523       struct cleanup *back_to;
6524
6525       siginfo_data = xmalloc (len);
6526       back_to = make_cleanup (xfree, siginfo_data);
6527
6528       if (target_read (&current_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
6529                        siginfo_data, 0, len) == len)
6530         discard_cleanups (back_to);
6531       else
6532         {
6533           /* Errors ignored.  */
6534           do_cleanups (back_to);
6535           siginfo_data = NULL;
6536         }
6537     }
6538
6539   inf_state = XZALLOC (struct infcall_suspend_state);
6540
6541   if (siginfo_data)
6542     {
6543       inf_state->siginfo_gdbarch = gdbarch;
6544       inf_state->siginfo_data = siginfo_data;
6545     }
6546
6547   inf_state->thread_suspend = tp->suspend;
6548   inf_state->inferior_suspend = inf->suspend;
6549
6550   /* run_inferior_call will not use the signal due to its `proceed' call with
6551      TARGET_SIGNAL_0 anyway.  */
6552   tp->suspend.stop_signal = TARGET_SIGNAL_0;
6553
6554   inf_state->stop_pc = stop_pc;
6555
6556   inf_state->registers = regcache_dup (regcache);
6557
6558   return inf_state;
6559 }
6560
6561 /* Restore inferior session state to INF_STATE.  */
6562
6563 void
6564 restore_infcall_suspend_state (struct infcall_suspend_state *inf_state)
6565 {
6566   struct thread_info *tp = inferior_thread ();
6567   struct inferior *inf = current_inferior ();
6568   struct regcache *regcache = get_current_regcache ();
6569   struct gdbarch *gdbarch = get_regcache_arch (regcache);
6570
6571   tp->suspend = inf_state->thread_suspend;
6572   inf->suspend = inf_state->inferior_suspend;
6573
6574   stop_pc = inf_state->stop_pc;
6575
6576   if (inf_state->siginfo_gdbarch == gdbarch)
6577     {
6578       struct type *type = gdbarch_get_siginfo_type (gdbarch);
6579       size_t len = TYPE_LENGTH (type);
6580
6581       /* Errors ignored.  */
6582       target_write (&current_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
6583                     inf_state->siginfo_data, 0, len);
6584     }
6585
6586   /* The inferior can be gone if the user types "print exit(0)"
6587      (and perhaps other times).  */
6588   if (target_has_execution)
6589     /* NB: The register write goes through to the target.  */
6590     regcache_cpy (regcache, inf_state->registers);
6591
6592   discard_infcall_suspend_state (inf_state);
6593 }
6594
6595 static void
6596 do_restore_infcall_suspend_state_cleanup (void *state)
6597 {
6598   restore_infcall_suspend_state (state);
6599 }
6600
6601 struct cleanup *
6602 make_cleanup_restore_infcall_suspend_state
6603   (struct infcall_suspend_state *inf_state)
6604 {
6605   return make_cleanup (do_restore_infcall_suspend_state_cleanup, inf_state);
6606 }
6607
6608 void
6609 discard_infcall_suspend_state (struct infcall_suspend_state *inf_state)
6610 {
6611   regcache_xfree (inf_state->registers);
6612   xfree (inf_state->siginfo_data);
6613   xfree (inf_state);
6614 }
6615
6616 struct regcache *
6617 get_infcall_suspend_state_regcache (struct infcall_suspend_state *inf_state)
6618 {
6619   return inf_state->registers;
6620 }
6621
6622 /* infcall_control_state contains state regarding gdb's control of the
6623    inferior itself like stepping control.  It also contains session state like
6624    the user's currently selected frame.  */
6625
6626 struct infcall_control_state
6627 {
6628   struct thread_control_state thread_control;
6629   struct inferior_control_state inferior_control;
6630
6631   /* Other fields:  */
6632   enum stop_stack_kind stop_stack_dummy;
6633   int stopped_by_random_signal;
6634   int stop_after_trap;
6635
6636   /* ID if the selected frame when the inferior function call was made.  */
6637   struct frame_id selected_frame_id;
6638 };
6639
6640 /* Save all of the information associated with the inferior<==>gdb
6641    connection.  */
6642
6643 struct infcall_control_state *
6644 save_infcall_control_state (void)
6645 {
6646   struct infcall_control_state *inf_status = xmalloc (sizeof (*inf_status));
6647   struct thread_info *tp = inferior_thread ();
6648   struct inferior *inf = current_inferior ();
6649
6650   inf_status->thread_control = tp->control;
6651   inf_status->inferior_control = inf->control;
6652
6653   tp->control.step_resume_breakpoint = NULL;
6654   tp->control.exception_resume_breakpoint = NULL;
6655
6656   /* Save original bpstat chain to INF_STATUS; replace it in TP with copy of
6657      chain.  If caller's caller is walking the chain, they'll be happier if we
6658      hand them back the original chain when restore_infcall_control_state is
6659      called.  */
6660   tp->control.stop_bpstat = bpstat_copy (tp->control.stop_bpstat);
6661
6662   /* Other fields:  */
6663   inf_status->stop_stack_dummy = stop_stack_dummy;
6664   inf_status->stopped_by_random_signal = stopped_by_random_signal;
6665   inf_status->stop_after_trap = stop_after_trap;
6666
6667   inf_status->selected_frame_id = get_frame_id (get_selected_frame (NULL));
6668
6669   return inf_status;
6670 }
6671
6672 static int
6673 restore_selected_frame (void *args)
6674 {
6675   struct frame_id *fid = (struct frame_id *) args;
6676   struct frame_info *frame;
6677
6678   frame = frame_find_by_id (*fid);
6679
6680   /* If inf_status->selected_frame_id is NULL, there was no previously
6681      selected frame.  */
6682   if (frame == NULL)
6683     {
6684       warning (_("Unable to restore previously selected frame."));
6685       return 0;
6686     }
6687
6688   select_frame (frame);
6689
6690   return (1);
6691 }
6692
6693 /* Restore inferior session state to INF_STATUS.  */
6694
6695 void
6696 restore_infcall_control_state (struct infcall_control_state *inf_status)
6697 {
6698   struct thread_info *tp = inferior_thread ();
6699   struct inferior *inf = current_inferior ();
6700
6701   if (tp->control.step_resume_breakpoint)
6702     tp->control.step_resume_breakpoint->disposition = disp_del_at_next_stop;
6703
6704   if (tp->control.exception_resume_breakpoint)
6705     tp->control.exception_resume_breakpoint->disposition
6706       = disp_del_at_next_stop;
6707
6708   /* Handle the bpstat_copy of the chain.  */
6709   bpstat_clear (&tp->control.stop_bpstat);
6710
6711   tp->control = inf_status->thread_control;
6712   inf->control = inf_status->inferior_control;
6713
6714   /* Other fields:  */
6715   stop_stack_dummy = inf_status->stop_stack_dummy;
6716   stopped_by_random_signal = inf_status->stopped_by_random_signal;
6717   stop_after_trap = inf_status->stop_after_trap;
6718
6719   if (target_has_stack)
6720     {
6721       /* The point of catch_errors is that if the stack is clobbered,
6722          walking the stack might encounter a garbage pointer and
6723          error() trying to dereference it.  */
6724       if (catch_errors
6725           (restore_selected_frame, &inf_status->selected_frame_id,
6726            "Unable to restore previously selected frame:\n",
6727            RETURN_MASK_ERROR) == 0)
6728         /* Error in restoring the selected frame.  Select the innermost
6729            frame.  */
6730         select_frame (get_current_frame ());
6731     }
6732
6733   xfree (inf_status);
6734 }
6735
6736 static void
6737 do_restore_infcall_control_state_cleanup (void *sts)
6738 {
6739   restore_infcall_control_state (sts);
6740 }
6741
6742 struct cleanup *
6743 make_cleanup_restore_infcall_control_state
6744   (struct infcall_control_state *inf_status)
6745 {
6746   return make_cleanup (do_restore_infcall_control_state_cleanup, inf_status);
6747 }
6748
6749 void
6750 discard_infcall_control_state (struct infcall_control_state *inf_status)
6751 {
6752   if (inf_status->thread_control.step_resume_breakpoint)
6753     inf_status->thread_control.step_resume_breakpoint->disposition
6754       = disp_del_at_next_stop;
6755
6756   if (inf_status->thread_control.exception_resume_breakpoint)
6757     inf_status->thread_control.exception_resume_breakpoint->disposition
6758       = disp_del_at_next_stop;
6759
6760   /* See save_infcall_control_state for info on stop_bpstat.  */
6761   bpstat_clear (&inf_status->thread_control.stop_bpstat);
6762
6763   xfree (inf_status);
6764 }
6765 \f
6766 int
6767 inferior_has_forked (ptid_t pid, ptid_t *child_pid)
6768 {
6769   struct target_waitstatus last;
6770   ptid_t last_ptid;
6771
6772   get_last_target_status (&last_ptid, &last);
6773
6774   if (last.kind != TARGET_WAITKIND_FORKED)
6775     return 0;
6776
6777   if (!ptid_equal (last_ptid, pid))
6778     return 0;
6779
6780   *child_pid = last.value.related_pid;
6781   return 1;
6782 }
6783
6784 int
6785 inferior_has_vforked (ptid_t pid, ptid_t *child_pid)
6786 {
6787   struct target_waitstatus last;
6788   ptid_t last_ptid;
6789
6790   get_last_target_status (&last_ptid, &last);
6791
6792   if (last.kind != TARGET_WAITKIND_VFORKED)
6793     return 0;
6794
6795   if (!ptid_equal (last_ptid, pid))
6796     return 0;
6797
6798   *child_pid = last.value.related_pid;
6799   return 1;
6800 }
6801
6802 int
6803 inferior_has_execd (ptid_t pid, char **execd_pathname)
6804 {
6805   struct target_waitstatus last;
6806   ptid_t last_ptid;
6807
6808   get_last_target_status (&last_ptid, &last);
6809
6810   if (last.kind != TARGET_WAITKIND_EXECD)
6811     return 0;
6812
6813   if (!ptid_equal (last_ptid, pid))
6814     return 0;
6815
6816   *execd_pathname = xstrdup (last.value.execd_pathname);
6817   return 1;
6818 }
6819
6820 int
6821 inferior_has_called_syscall (ptid_t pid, int *syscall_number)
6822 {
6823   struct target_waitstatus last;
6824   ptid_t last_ptid;
6825
6826   get_last_target_status (&last_ptid, &last);
6827
6828   if (last.kind != TARGET_WAITKIND_SYSCALL_ENTRY &&
6829       last.kind != TARGET_WAITKIND_SYSCALL_RETURN)
6830     return 0;
6831
6832   if (!ptid_equal (last_ptid, pid))
6833     return 0;
6834
6835   *syscall_number = last.value.syscall_number;
6836   return 1;
6837 }
6838
6839 int
6840 ptid_match (ptid_t ptid, ptid_t filter)
6841 {
6842   if (ptid_equal (filter, minus_one_ptid))
6843     return 1;
6844   if (ptid_is_pid (filter)
6845       && ptid_get_pid (ptid) == ptid_get_pid (filter))
6846     return 1;
6847   else if (ptid_equal (ptid, filter))
6848     return 1;
6849
6850   return 0;
6851 }
6852
6853 /* restore_inferior_ptid() will be used by the cleanup machinery
6854    to restore the inferior_ptid value saved in a call to
6855    save_inferior_ptid().  */
6856
6857 static void
6858 restore_inferior_ptid (void *arg)
6859 {
6860   ptid_t *saved_ptid_ptr = arg;
6861
6862   inferior_ptid = *saved_ptid_ptr;
6863   xfree (arg);
6864 }
6865
6866 /* Save the value of inferior_ptid so that it may be restored by a
6867    later call to do_cleanups().  Returns the struct cleanup pointer
6868    needed for later doing the cleanup.  */
6869
6870 struct cleanup *
6871 save_inferior_ptid (void)
6872 {
6873   ptid_t *saved_ptid_ptr;
6874
6875   saved_ptid_ptr = xmalloc (sizeof (ptid_t));
6876   *saved_ptid_ptr = inferior_ptid;
6877   return make_cleanup (restore_inferior_ptid, saved_ptid_ptr);
6878 }
6879 \f
6880
6881 /* User interface for reverse debugging:
6882    Set exec-direction / show exec-direction commands
6883    (returns error unless target implements to_set_exec_direction method).  */
6884
6885 int execution_direction = EXEC_FORWARD;
6886 static const char exec_forward[] = "forward";
6887 static const char exec_reverse[] = "reverse";
6888 static const char *exec_direction = exec_forward;
6889 static const char *exec_direction_names[] = {
6890   exec_forward,
6891   exec_reverse,
6892   NULL
6893 };
6894
6895 static void
6896 set_exec_direction_func (char *args, int from_tty,
6897                          struct cmd_list_element *cmd)
6898 {
6899   if (target_can_execute_reverse)
6900     {
6901       if (!strcmp (exec_direction, exec_forward))
6902         execution_direction = EXEC_FORWARD;
6903       else if (!strcmp (exec_direction, exec_reverse))
6904         execution_direction = EXEC_REVERSE;
6905     }
6906   else
6907     {
6908       exec_direction = exec_forward;
6909       error (_("Target does not support this operation."));
6910     }
6911 }
6912
6913 static void
6914 show_exec_direction_func (struct ui_file *out, int from_tty,
6915                           struct cmd_list_element *cmd, const char *value)
6916 {
6917   switch (execution_direction) {
6918   case EXEC_FORWARD:
6919     fprintf_filtered (out, _("Forward.\n"));
6920     break;
6921   case EXEC_REVERSE:
6922     fprintf_filtered (out, _("Reverse.\n"));
6923     break;
6924   default:
6925     internal_error (__FILE__, __LINE__,
6926                     _("bogus execution_direction value: %d"),
6927                     (int) execution_direction);
6928   }
6929 }
6930
6931 /* User interface for non-stop mode.  */
6932
6933 int non_stop = 0;
6934
6935 static void
6936 set_non_stop (char *args, int from_tty,
6937               struct cmd_list_element *c)
6938 {
6939   if (target_has_execution)
6940     {
6941       non_stop_1 = non_stop;
6942       error (_("Cannot change this setting while the inferior is running."));
6943     }
6944
6945   non_stop = non_stop_1;
6946 }
6947
6948 static void
6949 show_non_stop (struct ui_file *file, int from_tty,
6950                struct cmd_list_element *c, const char *value)
6951 {
6952   fprintf_filtered (file,
6953                     _("Controlling the inferior in non-stop mode is %s.\n"),
6954                     value);
6955 }
6956
6957 static void
6958 show_schedule_multiple (struct ui_file *file, int from_tty,
6959                         struct cmd_list_element *c, const char *value)
6960 {
6961   fprintf_filtered (file, _("Resuming the execution of threads "
6962                             "of all processes is %s.\n"), value);
6963 }
6964
6965 void
6966 _initialize_infrun (void)
6967 {
6968   int i;
6969   int numsigs;
6970
6971   add_info ("signals", signals_info, _("\
6972 What debugger does when program gets various signals.\n\
6973 Specify a signal as argument to print info on that signal only."));
6974   add_info_alias ("handle", "signals", 0);
6975
6976   add_com ("handle", class_run, handle_command, _("\
6977 Specify how to handle a signal.\n\
6978 Args are signals and actions to apply to those signals.\n\
6979 Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
6980 from 1-15 are allowed for compatibility with old versions of GDB.\n\
6981 Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
6982 The special arg \"all\" is recognized to mean all signals except those\n\
6983 used by the debugger, typically SIGTRAP and SIGINT.\n\
6984 Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
6985 \"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
6986 Stop means reenter debugger if this signal happens (implies print).\n\
6987 Print means print a message if this signal happens.\n\
6988 Pass means let program see this signal; otherwise program doesn't know.\n\
6989 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
6990 Pass and Stop may be combined."));
6991   if (xdb_commands)
6992     {
6993       add_com ("lz", class_info, signals_info, _("\
6994 What debugger does when program gets various signals.\n\
6995 Specify a signal as argument to print info on that signal only."));
6996       add_com ("z", class_run, xdb_handle_command, _("\
6997 Specify how to handle a signal.\n\
6998 Args are signals and actions to apply to those signals.\n\
6999 Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
7000 from 1-15 are allowed for compatibility with old versions of GDB.\n\
7001 Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
7002 The special arg \"all\" is recognized to mean all signals except those\n\
7003 used by the debugger, typically SIGTRAP and SIGINT.\n\
7004 Recognized actions include \"s\" (toggles between stop and nostop),\n\
7005 \"r\" (toggles between print and noprint), \"i\" (toggles between pass and \
7006 nopass), \"Q\" (noprint)\n\
7007 Stop means reenter debugger if this signal happens (implies print).\n\
7008 Print means print a message if this signal happens.\n\
7009 Pass means let program see this signal; otherwise program doesn't know.\n\
7010 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
7011 Pass and Stop may be combined."));
7012     }
7013
7014   if (!dbx_commands)
7015     stop_command = add_cmd ("stop", class_obscure,
7016                             not_just_help_class_command, _("\
7017 There is no `stop' command, but you can set a hook on `stop'.\n\
7018 This allows you to set a list of commands to be run each time execution\n\
7019 of the program stops."), &cmdlist);
7020
7021   add_setshow_zinteger_cmd ("infrun", class_maintenance, &debug_infrun, _("\
7022 Set inferior debugging."), _("\
7023 Show inferior debugging."), _("\
7024 When non-zero, inferior specific debugging is enabled."),
7025                             NULL,
7026                             show_debug_infrun,
7027                             &setdebuglist, &showdebuglist);
7028
7029   add_setshow_boolean_cmd ("displaced", class_maintenance,
7030                            &debug_displaced, _("\
7031 Set displaced stepping debugging."), _("\
7032 Show displaced stepping debugging."), _("\
7033 When non-zero, displaced stepping specific debugging is enabled."),
7034                             NULL,
7035                             show_debug_displaced,
7036                             &setdebuglist, &showdebuglist);
7037
7038   add_setshow_boolean_cmd ("non-stop", no_class,
7039                            &non_stop_1, _("\
7040 Set whether gdb controls the inferior in non-stop mode."), _("\
7041 Show whether gdb controls the inferior in non-stop mode."), _("\
7042 When debugging a multi-threaded program and this setting is\n\
7043 off (the default, also called all-stop mode), when one thread stops\n\
7044 (for a breakpoint, watchpoint, exception, or similar events), GDB stops\n\
7045 all other threads in the program while you interact with the thread of\n\
7046 interest.  When you continue or step a thread, you can allow the other\n\
7047 threads to run, or have them remain stopped, but while you inspect any\n\
7048 thread's state, all threads stop.\n\
7049 \n\
7050 In non-stop mode, when one thread stops, other threads can continue\n\
7051 to run freely.  You'll be able to step each thread independently,\n\
7052 leave it stopped or free to run as needed."),
7053                            set_non_stop,
7054                            show_non_stop,
7055                            &setlist,
7056                            &showlist);
7057
7058   numsigs = (int) TARGET_SIGNAL_LAST;
7059   signal_stop = (unsigned char *) xmalloc (sizeof (signal_stop[0]) * numsigs);
7060   signal_print = (unsigned char *)
7061     xmalloc (sizeof (signal_print[0]) * numsigs);
7062   signal_program = (unsigned char *)
7063     xmalloc (sizeof (signal_program[0]) * numsigs);
7064   signal_pass = (unsigned char *)
7065     xmalloc (sizeof (signal_program[0]) * numsigs);
7066   for (i = 0; i < numsigs; i++)
7067     {
7068       signal_stop[i] = 1;
7069       signal_print[i] = 1;
7070       signal_program[i] = 1;
7071     }
7072
7073   /* Signals caused by debugger's own actions
7074      should not be given to the program afterwards.  */
7075   signal_program[TARGET_SIGNAL_TRAP] = 0;
7076   signal_program[TARGET_SIGNAL_INT] = 0;
7077
7078   /* Signals that are not errors should not normally enter the debugger.  */
7079   signal_stop[TARGET_SIGNAL_ALRM] = 0;
7080   signal_print[TARGET_SIGNAL_ALRM] = 0;
7081   signal_stop[TARGET_SIGNAL_VTALRM] = 0;
7082   signal_print[TARGET_SIGNAL_VTALRM] = 0;
7083   signal_stop[TARGET_SIGNAL_PROF] = 0;
7084   signal_print[TARGET_SIGNAL_PROF] = 0;
7085   signal_stop[TARGET_SIGNAL_CHLD] = 0;
7086   signal_print[TARGET_SIGNAL_CHLD] = 0;
7087   signal_stop[TARGET_SIGNAL_IO] = 0;
7088   signal_print[TARGET_SIGNAL_IO] = 0;
7089   signal_stop[TARGET_SIGNAL_POLL] = 0;
7090   signal_print[TARGET_SIGNAL_POLL] = 0;
7091   signal_stop[TARGET_SIGNAL_URG] = 0;
7092   signal_print[TARGET_SIGNAL_URG] = 0;
7093   signal_stop[TARGET_SIGNAL_WINCH] = 0;
7094   signal_print[TARGET_SIGNAL_WINCH] = 0;
7095   signal_stop[TARGET_SIGNAL_PRIO] = 0;
7096   signal_print[TARGET_SIGNAL_PRIO] = 0;
7097
7098   /* These signals are used internally by user-level thread
7099      implementations.  (See signal(5) on Solaris.)  Like the above
7100      signals, a healthy program receives and handles them as part of
7101      its normal operation.  */
7102   signal_stop[TARGET_SIGNAL_LWP] = 0;
7103   signal_print[TARGET_SIGNAL_LWP] = 0;
7104   signal_stop[TARGET_SIGNAL_WAITING] = 0;
7105   signal_print[TARGET_SIGNAL_WAITING] = 0;
7106   signal_stop[TARGET_SIGNAL_CANCEL] = 0;
7107   signal_print[TARGET_SIGNAL_CANCEL] = 0;
7108
7109   /* Update cached state.  */
7110   signal_cache_update (-1);
7111
7112   add_setshow_zinteger_cmd ("stop-on-solib-events", class_support,
7113                             &stop_on_solib_events, _("\
7114 Set stopping for shared library events."), _("\
7115 Show stopping for shared library events."), _("\
7116 If nonzero, gdb will give control to the user when the dynamic linker\n\
7117 notifies gdb of shared library events.  The most common event of interest\n\
7118 to the user would be loading/unloading of a new library."),
7119                             NULL,
7120                             show_stop_on_solib_events,
7121                             &setlist, &showlist);
7122
7123   add_setshow_enum_cmd ("follow-fork-mode", class_run,
7124                         follow_fork_mode_kind_names,
7125                         &follow_fork_mode_string, _("\
7126 Set debugger response to a program call of fork or vfork."), _("\
7127 Show debugger response to a program call of fork or vfork."), _("\
7128 A fork or vfork creates a new process.  follow-fork-mode can be:\n\
7129   parent  - the original process is debugged after a fork\n\
7130   child   - the new process is debugged after a fork\n\
7131 The unfollowed process will continue to run.\n\
7132 By default, the debugger will follow the parent process."),
7133                         NULL,
7134                         show_follow_fork_mode_string,
7135                         &setlist, &showlist);
7136
7137   add_setshow_enum_cmd ("follow-exec-mode", class_run,
7138                         follow_exec_mode_names,
7139                         &follow_exec_mode_string, _("\
7140 Set debugger response to a program call of exec."), _("\
7141 Show debugger response to a program call of exec."), _("\
7142 An exec call replaces the program image of a process.\n\
7143 \n\
7144 follow-exec-mode can be:\n\
7145 \n\
7146   new - the debugger creates a new inferior and rebinds the process\n\
7147 to this new inferior.  The program the process was running before\n\
7148 the exec call can be restarted afterwards by restarting the original\n\
7149 inferior.\n\
7150 \n\
7151   same - the debugger keeps the process bound to the same inferior.\n\
7152 The new executable image replaces the previous executable loaded in\n\
7153 the inferior.  Restarting the inferior after the exec call restarts\n\
7154 the executable the process was running after the exec call.\n\
7155 \n\
7156 By default, the debugger will use the same inferior."),
7157                         NULL,
7158                         show_follow_exec_mode_string,
7159                         &setlist, &showlist);
7160
7161   add_setshow_enum_cmd ("scheduler-locking", class_run, 
7162                         scheduler_enums, &scheduler_mode, _("\
7163 Set mode for locking scheduler during execution."), _("\
7164 Show mode for locking scheduler during execution."), _("\
7165 off  == no locking (threads may preempt at any time)\n\
7166 on   == full locking (no thread except the current thread may run)\n\
7167 step == scheduler locked during every single-step operation.\n\
7168         In this mode, no other thread may run during a step command.\n\
7169         Other threads may run while stepping over a function call ('next')."), 
7170                         set_schedlock_func,     /* traps on target vector */
7171                         show_scheduler_mode,
7172                         &setlist, &showlist);
7173
7174   add_setshow_boolean_cmd ("schedule-multiple", class_run, &sched_multi, _("\
7175 Set mode for resuming threads of all processes."), _("\
7176 Show mode for resuming threads of all processes."), _("\
7177 When on, execution commands (such as 'continue' or 'next') resume all\n\
7178 threads of all processes.  When off (which is the default), execution\n\
7179 commands only resume the threads of the current process.  The set of\n\
7180 threads that are resumed is further refined by the scheduler-locking\n\
7181 mode (see help set scheduler-locking)."),
7182                            NULL,
7183                            show_schedule_multiple,
7184                            &setlist, &showlist);
7185
7186   add_setshow_boolean_cmd ("step-mode", class_run, &step_stop_if_no_debug, _("\
7187 Set mode of the step operation."), _("\
7188 Show mode of the step operation."), _("\
7189 When set, doing a step over a function without debug line information\n\
7190 will stop at the first instruction of that function. Otherwise, the\n\
7191 function is skipped and the step command stops at a different source line."),
7192                            NULL,
7193                            show_step_stop_if_no_debug,
7194                            &setlist, &showlist);
7195
7196   add_setshow_enum_cmd ("displaced-stepping", class_run,
7197                         can_use_displaced_stepping_enum,
7198                         &can_use_displaced_stepping, _("\
7199 Set debugger's willingness to use displaced stepping."), _("\
7200 Show debugger's willingness to use displaced stepping."), _("\
7201 If on, gdb will use displaced stepping to step over breakpoints if it is\n\
7202 supported by the target architecture.  If off, gdb will not use displaced\n\
7203 stepping to step over breakpoints, even if such is supported by the target\n\
7204 architecture.  If auto (which is the default), gdb will use displaced stepping\n\
7205 if the target architecture supports it and non-stop mode is active, but will not\n\
7206 use it in all-stop mode (see help set non-stop)."),
7207                         NULL,
7208                         show_can_use_displaced_stepping,
7209                         &setlist, &showlist);
7210
7211   add_setshow_enum_cmd ("exec-direction", class_run, exec_direction_names,
7212                         &exec_direction, _("Set direction of execution.\n\
7213 Options are 'forward' or 'reverse'."),
7214                         _("Show direction of execution (forward/reverse)."),
7215                         _("Tells gdb whether to execute forward or backward."),
7216                         set_exec_direction_func, show_exec_direction_func,
7217                         &setlist, &showlist);
7218
7219   /* Set/show detach-on-fork: user-settable mode.  */
7220
7221   add_setshow_boolean_cmd ("detach-on-fork", class_run, &detach_fork, _("\
7222 Set whether gdb will detach the child of a fork."), _("\
7223 Show whether gdb will detach the child of a fork."), _("\
7224 Tells gdb whether to detach the child of a fork."),
7225                            NULL, NULL, &setlist, &showlist);
7226
7227   /* ptid initializations */
7228   inferior_ptid = null_ptid;
7229   target_last_wait_ptid = minus_one_ptid;
7230
7231   observer_attach_thread_ptid_changed (infrun_thread_ptid_changed);
7232   observer_attach_thread_stop_requested (infrun_thread_stop_requested);
7233   observer_attach_thread_exit (infrun_thread_thread_exit);
7234   observer_attach_inferior_exit (infrun_inferior_exit);
7235
7236   /* Explicitly create without lookup, since that tries to create a
7237      value with a void typed value, and when we get here, gdbarch
7238      isn't initialized yet.  At this point, we're quite sure there
7239      isn't another convenience variable of the same name.  */
7240   create_internalvar_type_lazy ("_siginfo", siginfo_make_value);
7241
7242   add_setshow_boolean_cmd ("observer", no_class,
7243                            &observer_mode_1, _("\
7244 Set whether gdb controls the inferior in observer mode."), _("\
7245 Show whether gdb controls the inferior in observer mode."), _("\
7246 In observer mode, GDB can get data from the inferior, but not\n\
7247 affect its execution.  Registers and memory may not be changed,\n\
7248 breakpoints may not be set, and the program cannot be interrupted\n\
7249 or signalled."),
7250                            set_observer_mode,
7251                            show_observer_mode,
7252                            &setlist,
7253                            &showlist);
7254 }