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