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