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