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