* infrun.c (wait_for_inferior): Add treat_exec_as_sigtrap parameter
[external/binutils.git] / gdb / infrun.c
1 /* Target-struct-independent code to start (run) and stop an inferior
2    process.
3
4    Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
5    1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
6    2008 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
49 #include "gdb_assert.h"
50 #include "mi/mi-common.h"
51
52 /* Prototypes for local functions */
53
54 static void signals_info (char *, int);
55
56 static void handle_command (char *, int);
57
58 static void sig_print_info (enum target_signal);
59
60 static void sig_print_header (void);
61
62 static void resume_cleanups (void *);
63
64 static int hook_stop_stub (void *);
65
66 static int restore_selected_frame (void *);
67
68 static void build_infrun (void);
69
70 static int follow_fork (void);
71
72 static void set_schedlock_func (char *args, int from_tty,
73                                 struct cmd_list_element *c);
74
75 struct execution_control_state;
76
77 static int currently_stepping (struct execution_control_state *ecs);
78
79 static void xdb_handle_command (char *args, int from_tty);
80
81 static int prepare_to_proceed (int);
82
83 void _initialize_infrun (void);
84
85 int inferior_ignoring_leading_exec_events = 0;
86
87 /* When set, stop the 'step' command if we enter a function which has
88    no line number information.  The normal behavior is that we step
89    over such function.  */
90 int step_stop_if_no_debug = 0;
91 static void
92 show_step_stop_if_no_debug (struct ui_file *file, int from_tty,
93                             struct cmd_list_element *c, const char *value)
94 {
95   fprintf_filtered (file, _("Mode of the step operation is %s.\n"), value);
96 }
97
98 /* In asynchronous mode, but simulating synchronous execution. */
99
100 int sync_execution = 0;
101
102 /* wait_for_inferior and normal_stop use this to notify the user
103    when the inferior stopped in a different thread than it had been
104    running in.  */
105
106 static ptid_t previous_inferior_ptid;
107
108 /* This is true for configurations that may follow through execl() and
109    similar functions.  At present this is only true for HP-UX native.  */
110
111 #ifndef MAY_FOLLOW_EXEC
112 #define MAY_FOLLOW_EXEC (0)
113 #endif
114
115 static int may_follow_exec = MAY_FOLLOW_EXEC;
116
117 static int debug_infrun = 0;
118 static void
119 show_debug_infrun (struct ui_file *file, int from_tty,
120                    struct cmd_list_element *c, const char *value)
121 {
122   fprintf_filtered (file, _("Inferior debugging is %s.\n"), value);
123 }
124
125 /* If the program uses ELF-style shared libraries, then calls to
126    functions in shared libraries go through stubs, which live in a
127    table called the PLT (Procedure Linkage Table).  The first time the
128    function is called, the stub sends control to the dynamic linker,
129    which looks up the function's real address, patches the stub so
130    that future calls will go directly to the function, and then passes
131    control to the function.
132
133    If we are stepping at the source level, we don't want to see any of
134    this --- we just want to skip over the stub and the dynamic linker.
135    The simple approach is to single-step until control leaves the
136    dynamic linker.
137
138    However, on some systems (e.g., Red Hat's 5.2 distribution) the
139    dynamic linker calls functions in the shared C library, so you
140    can't tell from the PC alone whether the dynamic linker is still
141    running.  In this case, we use a step-resume breakpoint to get us
142    past the dynamic linker, as if we were using "next" to step over a
143    function call.
144
145    IN_SOLIB_DYNSYM_RESOLVE_CODE says whether we're in the dynamic
146    linker code or not.  Normally, this means we single-step.  However,
147    if SKIP_SOLIB_RESOLVER then returns non-zero, then its value is an
148    address where we can place a step-resume breakpoint to get past the
149    linker's symbol resolution function.
150
151    IN_SOLIB_DYNSYM_RESOLVE_CODE can generally be implemented in a
152    pretty portable way, by comparing the PC against the address ranges
153    of the dynamic linker's sections.
154
155    SKIP_SOLIB_RESOLVER is generally going to be system-specific, since
156    it depends on internal details of the dynamic linker.  It's usually
157    not too hard to figure out where to put a breakpoint, but it
158    certainly isn't portable.  SKIP_SOLIB_RESOLVER should do plenty of
159    sanity checking.  If it can't figure things out, returning zero and
160    getting the (possibly confusing) stepping behavior is better than
161    signalling an error, which will obscure the change in the
162    inferior's state.  */
163
164 /* This function returns TRUE if pc is the address of an instruction
165    that lies within the dynamic linker (such as the event hook, or the
166    dld itself).
167
168    This function must be used only when a dynamic linker event has
169    been caught, and the inferior is being stepped out of the hook, or
170    undefined results are guaranteed.  */
171
172 #ifndef SOLIB_IN_DYNAMIC_LINKER
173 #define SOLIB_IN_DYNAMIC_LINKER(pid,pc) 0
174 #endif
175
176
177 /* Convert the #defines into values.  This is temporary until wfi control
178    flow is completely sorted out.  */
179
180 #ifndef CANNOT_STEP_HW_WATCHPOINTS
181 #define CANNOT_STEP_HW_WATCHPOINTS 0
182 #else
183 #undef  CANNOT_STEP_HW_WATCHPOINTS
184 #define CANNOT_STEP_HW_WATCHPOINTS 1
185 #endif
186
187 /* Tables of how to react to signals; the user sets them.  */
188
189 static unsigned char *signal_stop;
190 static unsigned char *signal_print;
191 static unsigned char *signal_program;
192
193 #define SET_SIGS(nsigs,sigs,flags) \
194   do { \
195     int signum = (nsigs); \
196     while (signum-- > 0) \
197       if ((sigs)[signum]) \
198         (flags)[signum] = 1; \
199   } while (0)
200
201 #define UNSET_SIGS(nsigs,sigs,flags) \
202   do { \
203     int signum = (nsigs); \
204     while (signum-- > 0) \
205       if ((sigs)[signum]) \
206         (flags)[signum] = 0; \
207   } while (0)
208
209 /* Value to pass to target_resume() to cause all threads to resume */
210
211 #define RESUME_ALL (pid_to_ptid (-1))
212
213 /* Command list pointer for the "stop" placeholder.  */
214
215 static struct cmd_list_element *stop_command;
216
217 /* Function inferior was in as of last step command.  */
218
219 static struct symbol *step_start_function;
220
221 /* Nonzero if we are presently stepping over a breakpoint.
222
223    If we hit a breakpoint or watchpoint, and then continue,
224    we need to single step the current thread with breakpoints
225    disabled, to avoid hitting the same breakpoint or
226    watchpoint again.  And we should step just a single
227    thread and keep other threads stopped, so that 
228    other threads don't miss breakpoints while they are removed.
229
230    So, this variable simultaneously means that we need to single
231    step the current thread, keep other threads stopped, and that
232    breakpoints should be removed while we step.
233
234    This variable is set either:
235    - in proceed, when we resume inferior on user's explicit request
236    - in keep_going, if handle_inferior_event decides we need to
237    step over breakpoint.  
238
239    The variable is cleared in clear_proceed_status, called every
240    time before we call proceed.  The proceed calls wait_for_inferior,
241    which calls handle_inferior_event in a loop, and until 
242    wait_for_inferior exits, this variable is changed only by keep_going.  */
243
244 static int stepping_over_breakpoint;
245
246 /* Nonzero if we want to give control to the user when we're notified
247    of shared library events by the dynamic linker.  */
248 static int stop_on_solib_events;
249 static void
250 show_stop_on_solib_events (struct ui_file *file, int from_tty,
251                            struct cmd_list_element *c, const char *value)
252 {
253   fprintf_filtered (file, _("Stopping for shared library events is %s.\n"),
254                     value);
255 }
256
257 /* Nonzero means expecting a trace trap
258    and should stop the inferior and return silently when it happens.  */
259
260 int stop_after_trap;
261
262 /* Nonzero means expecting a trap and caller will handle it themselves.
263    It is used after attach, due to attaching to a process;
264    when running in the shell before the child program has been exec'd;
265    and when running some kinds of remote stuff (FIXME?).  */
266
267 enum stop_kind stop_soon;
268
269 /* Nonzero if proceed is being used for a "finish" command or a similar
270    situation when stop_registers should be saved.  */
271
272 int proceed_to_finish;
273
274 /* Save register contents here when about to pop a stack dummy frame,
275    if-and-only-if proceed_to_finish is set.
276    Thus this contains the return value from the called function (assuming
277    values are returned in a register).  */
278
279 struct regcache *stop_registers;
280
281 /* Nonzero after stop if current stack frame should be printed.  */
282
283 static int stop_print_frame;
284
285 static struct breakpoint *step_resume_breakpoint = NULL;
286
287 /* This is a cached copy of the pid/waitstatus of the last event
288    returned by target_wait()/deprecated_target_wait_hook().  This
289    information is returned by get_last_target_status().  */
290 static ptid_t target_last_wait_ptid;
291 static struct target_waitstatus target_last_waitstatus;
292
293 /* This is used to remember when a fork, vfork or exec event
294    was caught by a catchpoint, and thus the event is to be
295    followed at the next resume of the inferior, and not
296    immediately. */
297 static struct
298 {
299   enum target_waitkind kind;
300   struct
301   {
302     int parent_pid;
303     int child_pid;
304   }
305   fork_event;
306   char *execd_pathname;
307 }
308 pending_follow;
309
310 static const char follow_fork_mode_child[] = "child";
311 static const char follow_fork_mode_parent[] = "parent";
312
313 static const char *follow_fork_mode_kind_names[] = {
314   follow_fork_mode_child,
315   follow_fork_mode_parent,
316   NULL
317 };
318
319 static const char *follow_fork_mode_string = follow_fork_mode_parent;
320 static void
321 show_follow_fork_mode_string (struct ui_file *file, int from_tty,
322                               struct cmd_list_element *c, const char *value)
323 {
324   fprintf_filtered (file, _("\
325 Debugger response to a program call of fork or vfork is \"%s\".\n"),
326                     value);
327 }
328 \f
329
330 static int
331 follow_fork (void)
332 {
333   int follow_child = (follow_fork_mode_string == follow_fork_mode_child);
334
335   return target_follow_fork (follow_child);
336 }
337
338 void
339 follow_inferior_reset_breakpoints (void)
340 {
341   /* Was there a step_resume breakpoint?  (There was if the user
342      did a "next" at the fork() call.)  If so, explicitly reset its
343      thread number.
344
345      step_resumes are a form of bp that are made to be per-thread.
346      Since we created the step_resume bp when the parent process
347      was being debugged, and now are switching to the child process,
348      from the breakpoint package's viewpoint, that's a switch of
349      "threads".  We must update the bp's notion of which thread
350      it is for, or it'll be ignored when it triggers.  */
351
352   if (step_resume_breakpoint)
353     breakpoint_re_set_thread (step_resume_breakpoint);
354
355   /* Reinsert all breakpoints in the child.  The user may have set
356      breakpoints after catching the fork, in which case those
357      were never set in the child, but only in the parent.  This makes
358      sure the inserted breakpoints match the breakpoint list.  */
359
360   breakpoint_re_set ();
361   insert_breakpoints ();
362 }
363
364 /* EXECD_PATHNAME is assumed to be non-NULL. */
365
366 static void
367 follow_exec (int pid, char *execd_pathname)
368 {
369   int saved_pid = pid;
370   struct target_ops *tgt;
371
372   if (!may_follow_exec)
373     return;
374
375   /* This is an exec event that we actually wish to pay attention to.
376      Refresh our symbol table to the newly exec'd program, remove any
377      momentary bp's, etc.
378
379      If there are breakpoints, they aren't really inserted now,
380      since the exec() transformed our inferior into a fresh set
381      of instructions.
382
383      We want to preserve symbolic breakpoints on the list, since
384      we have hopes that they can be reset after the new a.out's
385      symbol table is read.
386
387      However, any "raw" breakpoints must be removed from the list
388      (e.g., the solib bp's), since their address is probably invalid
389      now.
390
391      And, we DON'T want to call delete_breakpoints() here, since
392      that may write the bp's "shadow contents" (the instruction
393      value that was overwritten witha TRAP instruction).  Since
394      we now have a new a.out, those shadow contents aren't valid. */
395   update_breakpoints_after_exec ();
396
397   /* If there was one, it's gone now.  We cannot truly step-to-next
398      statement through an exec(). */
399   step_resume_breakpoint = NULL;
400   step_range_start = 0;
401   step_range_end = 0;
402
403   /* What is this a.out's name? */
404   printf_unfiltered (_("Executing new program: %s\n"), execd_pathname);
405
406   /* We've followed the inferior through an exec.  Therefore, the
407      inferior has essentially been killed & reborn. */
408
409   /* First collect the run target in effect.  */
410   tgt = find_run_target ();
411   /* If we can't find one, things are in a very strange state...  */
412   if (tgt == NULL)
413     error (_("Could find run target to save before following exec"));
414
415   gdb_flush (gdb_stdout);
416   target_mourn_inferior ();
417   inferior_ptid = pid_to_ptid (saved_pid);
418   /* Because mourn_inferior resets inferior_ptid. */
419   push_target (tgt);
420
421   /* That a.out is now the one to use. */
422   exec_file_attach (execd_pathname, 0);
423
424   /* And also is where symbols can be found. */
425   symbol_file_add_main (execd_pathname, 0);
426
427   /* Reset the shared library package.  This ensures that we get
428      a shlib event when the child reaches "_start", at which point
429      the dld will have had a chance to initialize the child. */
430 #if defined(SOLIB_RESTART)
431   SOLIB_RESTART ();
432 #endif
433 #ifdef SOLIB_CREATE_INFERIOR_HOOK
434   SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid));
435 #else
436   solib_create_inferior_hook ();
437 #endif
438
439   /* Reinsert all breakpoints.  (Those which were symbolic have
440      been reset to the proper address in the new a.out, thanks
441      to symbol_file_command...) */
442   insert_breakpoints ();
443
444   /* The next resume of this inferior should bring it to the shlib
445      startup breakpoints.  (If the user had also set bp's on
446      "main" from the old (parent) process, then they'll auto-
447      matically get reset there in the new process.) */
448 }
449
450 /* Non-zero if we just simulating a single-step.  This is needed
451    because we cannot remove the breakpoints in the inferior process
452    until after the `wait' in `wait_for_inferior'.  */
453 static int singlestep_breakpoints_inserted_p = 0;
454
455 /* The thread we inserted single-step breakpoints for.  */
456 static ptid_t singlestep_ptid;
457
458 /* PC when we started this single-step.  */
459 static CORE_ADDR singlestep_pc;
460
461 /* If another thread hit the singlestep breakpoint, we save the original
462    thread here so that we can resume single-stepping it later.  */
463 static ptid_t saved_singlestep_ptid;
464 static int stepping_past_singlestep_breakpoint;
465
466 /* If not equal to null_ptid, this means that after stepping over breakpoint
467    is finished, we need to switch to deferred_step_ptid, and step it.
468
469    The use case is when one thread has hit a breakpoint, and then the user 
470    has switched to another thread and issued 'step'. We need to step over
471    breakpoint in the thread which hit the breakpoint, but then continue
472    stepping the thread user has selected.  */
473 static ptid_t deferred_step_ptid;
474 \f
475
476 /* Things to clean up if we QUIT out of resume ().  */
477 static void
478 resume_cleanups (void *ignore)
479 {
480   normal_stop ();
481 }
482
483 static const char schedlock_off[] = "off";
484 static const char schedlock_on[] = "on";
485 static const char schedlock_step[] = "step";
486 static const char *scheduler_enums[] = {
487   schedlock_off,
488   schedlock_on,
489   schedlock_step,
490   NULL
491 };
492 static const char *scheduler_mode = schedlock_off;
493 static void
494 show_scheduler_mode (struct ui_file *file, int from_tty,
495                      struct cmd_list_element *c, const char *value)
496 {
497   fprintf_filtered (file, _("\
498 Mode for locking scheduler during execution is \"%s\".\n"),
499                     value);
500 }
501
502 static void
503 set_schedlock_func (char *args, int from_tty, struct cmd_list_element *c)
504 {
505   if (!target_can_lock_scheduler)
506     {
507       scheduler_mode = schedlock_off;
508       error (_("Target '%s' cannot support this command."), target_shortname);
509     }
510 }
511
512
513 /* Resume the inferior, but allow a QUIT.  This is useful if the user
514    wants to interrupt some lengthy single-stepping operation
515    (for child processes, the SIGINT goes to the inferior, and so
516    we get a SIGINT random_signal, but for remote debugging and perhaps
517    other targets, that's not true).
518
519    STEP nonzero if we should step (zero to continue instead).
520    SIG is the signal to give the inferior (zero for none).  */
521 void
522 resume (int step, enum target_signal sig)
523 {
524   int should_resume = 1;
525   struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
526   QUIT;
527
528   if (debug_infrun)
529     fprintf_unfiltered (gdb_stdlog, "infrun: resume (step=%d, signal=%d)\n",
530                         step, sig);
531
532   /* FIXME: calling breakpoint_here_p (read_pc ()) three times! */
533
534
535   /* Some targets (e.g. Solaris x86) have a kernel bug when stepping
536      over an instruction that causes a page fault without triggering
537      a hardware watchpoint. The kernel properly notices that it shouldn't
538      stop, because the hardware watchpoint is not triggered, but it forgets
539      the step request and continues the program normally.
540      Work around the problem by removing hardware watchpoints if a step is
541      requested, GDB will check for a hardware watchpoint trigger after the
542      step anyway.  */
543   if (CANNOT_STEP_HW_WATCHPOINTS && step)
544     remove_hw_watchpoints ();
545
546
547   /* Normally, by the time we reach `resume', the breakpoints are either
548      removed or inserted, as appropriate.  The exception is if we're sitting
549      at a permanent breakpoint; we need to step over it, but permanent
550      breakpoints can't be removed.  So we have to test for it here.  */
551   if (breakpoint_here_p (read_pc ()) == permanent_breakpoint_here)
552     {
553       if (gdbarch_skip_permanent_breakpoint_p (current_gdbarch))
554         gdbarch_skip_permanent_breakpoint (current_gdbarch,
555                                            get_current_regcache ());
556       else
557         error (_("\
558 The program is stopped at a permanent breakpoint, but GDB does not know\n\
559 how to step past a permanent breakpoint on this architecture.  Try using\n\
560 a command like `return' or `jump' to continue execution."));
561     }
562
563   if (step && gdbarch_software_single_step_p (current_gdbarch))
564     {
565       /* Do it the hard way, w/temp breakpoints */
566       if (gdbarch_software_single_step (current_gdbarch, get_current_frame ()))
567         {
568           /* ...and don't ask hardware to do it.  */
569           step = 0;
570           /* and do not pull these breakpoints until after a `wait' in
571           `wait_for_inferior' */
572           singlestep_breakpoints_inserted_p = 1;
573           singlestep_ptid = inferior_ptid;
574           singlestep_pc = read_pc ();
575         }
576     }
577
578   /* If there were any forks/vforks/execs that were caught and are
579      now to be followed, then do so.  */
580   switch (pending_follow.kind)
581     {
582     case TARGET_WAITKIND_FORKED:
583     case TARGET_WAITKIND_VFORKED:
584       pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
585       if (follow_fork ())
586         should_resume = 0;
587       break;
588
589     case TARGET_WAITKIND_EXECD:
590       /* follow_exec is called as soon as the exec event is seen. */
591       pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
592       break;
593
594     default:
595       break;
596     }
597
598   /* Install inferior's terminal modes.  */
599   target_terminal_inferior ();
600
601   if (should_resume)
602     {
603       ptid_t resume_ptid;
604
605       resume_ptid = RESUME_ALL; /* Default */
606
607       /* If STEP is set, it's a request to use hardware stepping
608          facilities.  But in that case, we should never
609          use singlestep breakpoint.  */
610       gdb_assert (!(singlestep_breakpoints_inserted_p && step));
611
612       if (singlestep_breakpoints_inserted_p
613           && stepping_past_singlestep_breakpoint)
614         {
615           /* The situation here is as follows.  In thread T1 we wanted to
616              single-step.  Lacking hardware single-stepping we've
617              set breakpoint at the PC of the next instruction -- call it
618              P.  After resuming, we've hit that breakpoint in thread T2.
619              Now we've removed original breakpoint, inserted breakpoint
620              at P+1, and try to step to advance T2 past breakpoint.
621              We need to step only T2, as if T1 is allowed to freely run,
622              it can run past P, and if other threads are allowed to run,
623              they can hit breakpoint at P+1, and nested hits of single-step
624              breakpoints is not something we'd want -- that's complicated
625              to support, and has no value.  */
626           resume_ptid = inferior_ptid;
627         }
628
629       if ((step || singlestep_breakpoints_inserted_p)
630           && breakpoint_here_p (read_pc ())
631           && !breakpoint_inserted_here_p (read_pc ()))
632         {
633           /* We're stepping, have breakpoint at PC, and it's 
634              not inserted.  Most likely, proceed has noticed that
635              we have breakpoint and tries to single-step over it,
636              so that it's not hit.  In which case, we need to
637              single-step only this thread, and keep others stopped,
638              as they can miss this breakpoint if allowed to run.  
639
640              The current code either has all breakpoints inserted, 
641              or all removed, so if we let other threads run,
642              we can actually miss any breakpoint, not the one at PC.  */
643           resume_ptid = inferior_ptid;
644         }
645
646       if ((scheduler_mode == schedlock_on)
647           || (scheduler_mode == schedlock_step
648               && (step || singlestep_breakpoints_inserted_p)))
649         {
650           /* User-settable 'scheduler' mode requires solo thread resume. */
651           resume_ptid = inferior_ptid;
652         }
653
654       if (gdbarch_cannot_step_breakpoint (current_gdbarch))
655         {
656           /* Most targets can step a breakpoint instruction, thus
657              executing it normally.  But if this one cannot, just
658              continue and we will hit it anyway.  */
659           if (step && breakpoint_inserted_here_p (read_pc ()))
660             step = 0;
661         }
662       target_resume (resume_ptid, step, sig);
663     }
664
665   discard_cleanups (old_cleanups);
666 }
667 \f
668
669 /* Clear out all variables saying what to do when inferior is continued.
670    First do this, then set the ones you want, then call `proceed'.  */
671
672 void
673 clear_proceed_status (void)
674 {
675   stepping_over_breakpoint = 0;
676   step_range_start = 0;
677   step_range_end = 0;
678   step_frame_id = null_frame_id;
679   step_over_calls = STEP_OVER_UNDEBUGGABLE;
680   stop_after_trap = 0;
681   stop_soon = NO_STOP_QUIETLY;
682   proceed_to_finish = 0;
683   breakpoint_proceeded = 1;     /* We're about to proceed... */
684
685   if (stop_registers)
686     {
687       regcache_xfree (stop_registers);
688       stop_registers = NULL;
689     }
690
691   /* Discard any remaining commands or status from previous stop.  */
692   bpstat_clear (&stop_bpstat);
693 }
694
695 /* This should be suitable for any targets that support threads. */
696
697 static int
698 prepare_to_proceed (int step)
699 {
700   ptid_t wait_ptid;
701   struct target_waitstatus wait_status;
702
703   /* Get the last target status returned by target_wait().  */
704   get_last_target_status (&wait_ptid, &wait_status);
705
706   /* Make sure we were stopped at a breakpoint.  */
707   if (wait_status.kind != TARGET_WAITKIND_STOPPED
708       || wait_status.value.sig != TARGET_SIGNAL_TRAP)
709     {
710       return 0;
711     }
712
713   /* Switched over from WAIT_PID.  */
714   if (!ptid_equal (wait_ptid, minus_one_ptid)
715       && !ptid_equal (inferior_ptid, wait_ptid)
716       && breakpoint_here_p (read_pc_pid (wait_ptid)))
717     {
718       /* If stepping, remember current thread to switch back to.  */
719       if (step)
720         {
721           deferred_step_ptid = inferior_ptid;
722         }
723
724       /* Switch back to WAIT_PID thread.  */
725       switch_to_thread (wait_ptid);
726
727       /* We return 1 to indicate that there is a breakpoint here,
728          so we need to step over it before continuing to avoid
729          hitting it straight away. */
730       return 1;
731     }
732
733   return 0;
734 }
735
736 /* Record the pc of the program the last time it stopped.  This is
737    just used internally by wait_for_inferior, but need to be preserved
738    over calls to it and cleared when the inferior is started.  */
739 static CORE_ADDR prev_pc;
740
741 /* Basic routine for continuing the program in various fashions.
742
743    ADDR is the address to resume at, or -1 for resume where stopped.
744    SIGGNAL is the signal to give it, or 0 for none,
745    or -1 for act according to how it stopped.
746    STEP is nonzero if should trap after one instruction.
747    -1 means return after that and print nothing.
748    You should probably set various step_... variables
749    before calling here, if you are stepping.
750
751    You should call clear_proceed_status before calling proceed.  */
752
753 void
754 proceed (CORE_ADDR addr, enum target_signal siggnal, int step)
755 {
756   int oneproc = 0;
757
758   if (step > 0)
759     step_start_function = find_pc_function (read_pc ());
760   if (step < 0)
761     stop_after_trap = 1;
762
763   if (addr == (CORE_ADDR) -1)
764     {
765       if (read_pc () == stop_pc && breakpoint_here_p (read_pc ()))
766         /* There is a breakpoint at the address we will resume at,
767            step one instruction before inserting breakpoints so that
768            we do not stop right away (and report a second hit at this
769            breakpoint).  */
770         oneproc = 1;
771       else if (gdbarch_single_step_through_delay_p (current_gdbarch)
772               && gdbarch_single_step_through_delay (current_gdbarch,
773                                                     get_current_frame ()))
774         /* We stepped onto an instruction that needs to be stepped
775            again before re-inserting the breakpoint, do so.  */
776         oneproc = 1;
777     }
778   else
779     {
780       write_pc (addr);
781     }
782
783   if (debug_infrun)
784     fprintf_unfiltered (gdb_stdlog,
785                         "infrun: proceed (addr=0x%s, signal=%d, step=%d)\n",
786                         paddr_nz (addr), siggnal, step);
787
788   /* In a multi-threaded task we may select another thread
789      and then continue or step.
790
791      But if the old thread was stopped at a breakpoint, it
792      will immediately cause another breakpoint stop without
793      any execution (i.e. it will report a breakpoint hit
794      incorrectly).  So we must step over it first.
795
796      prepare_to_proceed checks the current thread against the thread
797      that reported the most recent event.  If a step-over is required
798      it returns TRUE and sets the current thread to the old thread. */
799   if (prepare_to_proceed (step))
800     oneproc = 1;
801
802   if (oneproc)
803     /* We will get a trace trap after one instruction.
804        Continue it automatically and insert breakpoints then.  */
805     stepping_over_breakpoint = 1;
806   else
807     insert_breakpoints ();
808
809   if (siggnal != TARGET_SIGNAL_DEFAULT)
810     stop_signal = siggnal;
811   /* If this signal should not be seen by program,
812      give it zero.  Used for debugging signals.  */
813   else if (!signal_program[stop_signal])
814     stop_signal = TARGET_SIGNAL_0;
815
816   annotate_starting ();
817
818   /* Make sure that output from GDB appears before output from the
819      inferior.  */
820   gdb_flush (gdb_stdout);
821
822   /* Refresh prev_pc value just prior to resuming.  This used to be
823      done in stop_stepping, however, setting prev_pc there did not handle
824      scenarios such as inferior function calls or returning from
825      a function via the return command.  In those cases, the prev_pc
826      value was not set properly for subsequent commands.  The prev_pc value 
827      is used to initialize the starting line number in the ecs.  With an 
828      invalid value, the gdb next command ends up stopping at the position
829      represented by the next line table entry past our start position.
830      On platforms that generate one line table entry per line, this
831      is not a problem.  However, on the ia64, the compiler generates
832      extraneous line table entries that do not increase the line number.
833      When we issue the gdb next command on the ia64 after an inferior call
834      or a return command, we often end up a few instructions forward, still 
835      within the original line we started.
836
837      An attempt was made to have init_execution_control_state () refresh
838      the prev_pc value before calculating the line number.  This approach
839      did not work because on platforms that use ptrace, the pc register
840      cannot be read unless the inferior is stopped.  At that point, we
841      are not guaranteed the inferior is stopped and so the read_pc ()
842      call can fail.  Setting the prev_pc value here ensures the value is 
843      updated correctly when the inferior is stopped.  */
844   prev_pc = read_pc ();
845
846   /* Resume inferior.  */
847   resume (oneproc || step || bpstat_should_step (), stop_signal);
848
849   /* Wait for it to stop (if not standalone)
850      and in any case decode why it stopped, and act accordingly.  */
851   /* Do this only if we are not using the event loop, or if the target
852      does not support asynchronous execution. */
853   if (!target_can_async_p ())
854     {
855       wait_for_inferior (0);
856       normal_stop ();
857     }
858 }
859 \f
860
861 /* Start remote-debugging of a machine over a serial link.  */
862
863 void
864 start_remote (int from_tty)
865 {
866   init_thread_list ();
867   init_wait_for_inferior ();
868   stop_soon = STOP_QUIETLY_REMOTE;
869   stepping_over_breakpoint = 0;
870
871   /* Always go on waiting for the target, regardless of the mode. */
872   /* FIXME: cagney/1999-09-23: At present it isn't possible to
873      indicate to wait_for_inferior that a target should timeout if
874      nothing is returned (instead of just blocking).  Because of this,
875      targets expecting an immediate response need to, internally, set
876      things up so that the target_wait() is forced to eventually
877      timeout. */
878   /* FIXME: cagney/1999-09-24: It isn't possible for target_open() to
879      differentiate to its caller what the state of the target is after
880      the initial open has been performed.  Here we're assuming that
881      the target has stopped.  It should be possible to eventually have
882      target_open() return to the caller an indication that the target
883      is currently running and GDB state should be set to the same as
884      for an async run. */
885   wait_for_inferior (0);
886
887   /* Now that the inferior has stopped, do any bookkeeping like
888      loading shared libraries.  We want to do this before normal_stop,
889      so that the displayed frame is up to date.  */
890   post_create_inferior (&current_target, from_tty);
891
892   normal_stop ();
893 }
894
895 /* Initialize static vars when a new inferior begins.  */
896
897 void
898 init_wait_for_inferior (void)
899 {
900   /* These are meaningless until the first time through wait_for_inferior.  */
901   prev_pc = 0;
902
903   breakpoint_init_inferior (inf_starting);
904
905   /* Don't confuse first call to proceed(). */
906   stop_signal = TARGET_SIGNAL_0;
907
908   /* The first resume is not following a fork/vfork/exec. */
909   pending_follow.kind = TARGET_WAITKIND_SPURIOUS;       /* I.e., none. */
910
911   clear_proceed_status ();
912
913   stepping_past_singlestep_breakpoint = 0;
914   deferred_step_ptid = null_ptid;
915
916   target_last_wait_ptid = minus_one_ptid;
917 }
918 \f
919 /* This enum encodes possible reasons for doing a target_wait, so that
920    wfi can call target_wait in one place.  (Ultimately the call will be
921    moved out of the infinite loop entirely.) */
922
923 enum infwait_states
924 {
925   infwait_normal_state,
926   infwait_thread_hop_state,
927   infwait_step_watch_state,
928   infwait_nonstep_watch_state
929 };
930
931 /* Why did the inferior stop? Used to print the appropriate messages
932    to the interface from within handle_inferior_event(). */
933 enum inferior_stop_reason
934 {
935   /* Step, next, nexti, stepi finished. */
936   END_STEPPING_RANGE,
937   /* Inferior terminated by signal. */
938   SIGNAL_EXITED,
939   /* Inferior exited. */
940   EXITED,
941   /* Inferior received signal, and user asked to be notified. */
942   SIGNAL_RECEIVED
943 };
944
945 /* This structure contains what used to be local variables in
946    wait_for_inferior.  Probably many of them can return to being
947    locals in handle_inferior_event.  */
948
949 struct execution_control_state
950 {
951   struct target_waitstatus ws;
952   struct target_waitstatus *wp;
953   /* Should we step over breakpoint next time keep_going 
954      is called?  */
955   int stepping_over_breakpoint;
956   int random_signal;
957   CORE_ADDR stop_func_start;
958   CORE_ADDR stop_func_end;
959   char *stop_func_name;
960   struct symtab_and_line sal;
961   int current_line;
962   struct symtab *current_symtab;
963   int handling_longjmp;         /* FIXME */
964   ptid_t ptid;
965   ptid_t saved_inferior_ptid;
966   int step_after_step_resume_breakpoint;
967   int stepping_through_solib_after_catch;
968   bpstat stepping_through_solib_catchpoints;
969   int new_thread_event;
970   struct target_waitstatus tmpstatus;
971   enum infwait_states infwait_state;
972   ptid_t waiton_ptid;
973   int wait_some_more;
974 };
975
976 void init_execution_control_state (struct execution_control_state *ecs);
977
978 void handle_inferior_event (struct execution_control_state *ecs);
979
980 static void step_into_function (struct execution_control_state *ecs);
981 static void insert_step_resume_breakpoint_at_frame (struct frame_info *step_frame);
982 static void insert_step_resume_breakpoint_at_caller (struct frame_info *);
983 static void insert_step_resume_breakpoint_at_sal (struct symtab_and_line sr_sal,
984                                                   struct frame_id sr_id);
985 static void stop_stepping (struct execution_control_state *ecs);
986 static void prepare_to_wait (struct execution_control_state *ecs);
987 static void keep_going (struct execution_control_state *ecs);
988 static void print_stop_reason (enum inferior_stop_reason stop_reason,
989                                int stop_info);
990
991 /* Wait for control to return from inferior to debugger.
992
993    If TREAT_EXEC_AS_SIGTRAP is non-zero, then handle EXEC signals
994    as if they were SIGTRAP signals.  This can be useful during
995    the startup sequence on some targets such as HP/UX, where
996    we receive an EXEC event instead of the expected SIGTRAP.
997
998    If inferior gets a signal, we may decide to start it up again
999    instead of returning.  That is why there is a loop in this function.
1000    When this function actually returns it means the inferior
1001    should be left stopped and GDB should read more commands.  */
1002
1003 void
1004 wait_for_inferior (int treat_exec_as_sigtrap)
1005 {
1006   struct cleanup *old_cleanups;
1007   struct execution_control_state ecss;
1008   struct execution_control_state *ecs;
1009
1010   if (debug_infrun)
1011     fprintf_unfiltered
1012       (gdb_stdlog, "infrun: wait_for_inferior (treat_exec_as_sigtrap=%d)\n",
1013        treat_exec_as_sigtrap);
1014
1015   old_cleanups = make_cleanup (delete_step_resume_breakpoint,
1016                                &step_resume_breakpoint);
1017
1018   /* wfi still stays in a loop, so it's OK just to take the address of
1019      a local to get the ecs pointer.  */
1020   ecs = &ecss;
1021
1022   /* Fill in with reasonable starting values.  */
1023   init_execution_control_state (ecs);
1024
1025   /* We'll update this if & when we switch to a new thread. */
1026   previous_inferior_ptid = inferior_ptid;
1027
1028   overlay_cache_invalid = 1;
1029
1030   /* We have to invalidate the registers BEFORE calling target_wait
1031      because they can be loaded from the target while in target_wait.
1032      This makes remote debugging a bit more efficient for those
1033      targets that provide critical registers as part of their normal
1034      status mechanism. */
1035
1036   registers_changed ();
1037
1038   while (1)
1039     {
1040       if (deprecated_target_wait_hook)
1041         ecs->ptid = deprecated_target_wait_hook (ecs->waiton_ptid, ecs->wp);
1042       else
1043         ecs->ptid = target_wait (ecs->waiton_ptid, ecs->wp);
1044
1045       if (treat_exec_as_sigtrap && ecs->ws.kind == TARGET_WAITKIND_EXECD)
1046         {
1047           xfree (ecs->ws.value.execd_pathname);
1048           ecs->ws.kind = TARGET_WAITKIND_STOPPED;
1049           ecs->ws.value.sig = TARGET_SIGNAL_TRAP;
1050         }
1051
1052       /* Now figure out what to do with the result of the result.  */
1053       handle_inferior_event (ecs);
1054
1055       if (!ecs->wait_some_more)
1056         break;
1057     }
1058   do_cleanups (old_cleanups);
1059 }
1060
1061 /* Asynchronous version of wait_for_inferior. It is called by the
1062    event loop whenever a change of state is detected on the file
1063    descriptor corresponding to the target. It can be called more than
1064    once to complete a single execution command. In such cases we need
1065    to keep the state in a global variable ASYNC_ECSS. If it is the
1066    last time that this function is called for a single execution
1067    command, then report to the user that the inferior has stopped, and
1068    do the necessary cleanups. */
1069
1070 struct execution_control_state async_ecss;
1071 struct execution_control_state *async_ecs;
1072
1073 void
1074 fetch_inferior_event (void *client_data)
1075 {
1076   static struct cleanup *old_cleanups;
1077
1078   async_ecs = &async_ecss;
1079
1080   if (!async_ecs->wait_some_more)
1081     {
1082       old_cleanups = make_exec_cleanup (delete_step_resume_breakpoint,
1083                                         &step_resume_breakpoint);
1084
1085       /* Fill in with reasonable starting values.  */
1086       init_execution_control_state (async_ecs);
1087
1088       /* We'll update this if & when we switch to a new thread. */
1089       previous_inferior_ptid = inferior_ptid;
1090
1091       overlay_cache_invalid = 1;
1092
1093       /* We have to invalidate the registers BEFORE calling target_wait
1094          because they can be loaded from the target while in target_wait.
1095          This makes remote debugging a bit more efficient for those
1096          targets that provide critical registers as part of their normal
1097          status mechanism. */
1098
1099       registers_changed ();
1100     }
1101
1102   if (deprecated_target_wait_hook)
1103     async_ecs->ptid =
1104       deprecated_target_wait_hook (async_ecs->waiton_ptid, async_ecs->wp);
1105   else
1106     async_ecs->ptid = target_wait (async_ecs->waiton_ptid, async_ecs->wp);
1107
1108   /* Now figure out what to do with the result of the result.  */
1109   handle_inferior_event (async_ecs);
1110
1111   if (!async_ecs->wait_some_more)
1112     {
1113       /* Do only the cleanups that have been added by this
1114          function. Let the continuations for the commands do the rest,
1115          if there are any. */
1116       do_exec_cleanups (old_cleanups);
1117       normal_stop ();
1118       if (step_multi && stop_step)
1119         inferior_event_handler (INF_EXEC_CONTINUE, NULL);
1120       else
1121         inferior_event_handler (INF_EXEC_COMPLETE, NULL);
1122     }
1123 }
1124
1125 /* Prepare an execution control state for looping through a
1126    wait_for_inferior-type loop.  */
1127
1128 void
1129 init_execution_control_state (struct execution_control_state *ecs)
1130 {
1131   ecs->stepping_over_breakpoint = 0;
1132   ecs->random_signal = 0;
1133   ecs->step_after_step_resume_breakpoint = 0;
1134   ecs->handling_longjmp = 0;    /* FIXME */
1135   ecs->stepping_through_solib_after_catch = 0;
1136   ecs->stepping_through_solib_catchpoints = NULL;
1137   ecs->sal = find_pc_line (prev_pc, 0);
1138   ecs->current_line = ecs->sal.line;
1139   ecs->current_symtab = ecs->sal.symtab;
1140   ecs->infwait_state = infwait_normal_state;
1141   ecs->waiton_ptid = pid_to_ptid (-1);
1142   ecs->wp = &(ecs->ws);
1143 }
1144
1145 /* Return the cached copy of the last pid/waitstatus returned by
1146    target_wait()/deprecated_target_wait_hook().  The data is actually
1147    cached by handle_inferior_event(), which gets called immediately
1148    after target_wait()/deprecated_target_wait_hook().  */
1149
1150 void
1151 get_last_target_status (ptid_t *ptidp, struct target_waitstatus *status)
1152 {
1153   *ptidp = target_last_wait_ptid;
1154   *status = target_last_waitstatus;
1155 }
1156
1157 void
1158 nullify_last_target_wait_ptid (void)
1159 {
1160   target_last_wait_ptid = minus_one_ptid;
1161 }
1162
1163 /* Switch thread contexts, maintaining "infrun state". */
1164
1165 static void
1166 context_switch (struct execution_control_state *ecs)
1167 {
1168   /* Caution: it may happen that the new thread (or the old one!)
1169      is not in the thread list.  In this case we must not attempt
1170      to "switch context", or we run the risk that our context may
1171      be lost.  This may happen as a result of the target module
1172      mishandling thread creation.  */
1173
1174   if (debug_infrun)
1175     {
1176       fprintf_unfiltered (gdb_stdlog, "infrun: Switching context from %s ",
1177                           target_pid_to_str (inferior_ptid));
1178       fprintf_unfiltered (gdb_stdlog, "to %s\n",
1179                           target_pid_to_str (ecs->ptid));
1180     }
1181
1182   if (in_thread_list (inferior_ptid) && in_thread_list (ecs->ptid))
1183     {                           /* Perform infrun state context switch: */
1184       /* Save infrun state for the old thread.  */
1185       save_infrun_state (inferior_ptid, prev_pc,
1186                          stepping_over_breakpoint, step_resume_breakpoint,
1187                          step_range_start,
1188                          step_range_end, &step_frame_id,
1189                          ecs->handling_longjmp, ecs->stepping_over_breakpoint,
1190                          ecs->stepping_through_solib_after_catch,
1191                          ecs->stepping_through_solib_catchpoints,
1192                          ecs->current_line, ecs->current_symtab);
1193
1194       /* Load infrun state for the new thread.  */
1195       load_infrun_state (ecs->ptid, &prev_pc,
1196                          &stepping_over_breakpoint, &step_resume_breakpoint,
1197                          &step_range_start,
1198                          &step_range_end, &step_frame_id,
1199                          &ecs->handling_longjmp, &ecs->stepping_over_breakpoint,
1200                          &ecs->stepping_through_solib_after_catch,
1201                          &ecs->stepping_through_solib_catchpoints,
1202                          &ecs->current_line, &ecs->current_symtab);
1203     }
1204
1205   switch_to_thread (ecs->ptid);
1206 }
1207
1208 static void
1209 adjust_pc_after_break (struct execution_control_state *ecs)
1210 {
1211   CORE_ADDR breakpoint_pc;
1212
1213   /* If this target does not decrement the PC after breakpoints, then
1214      we have nothing to do.  */
1215   if (gdbarch_decr_pc_after_break (current_gdbarch) == 0)
1216     return;
1217
1218   /* If we've hit a breakpoint, we'll normally be stopped with SIGTRAP.  If
1219      we aren't, just return.
1220
1221      We assume that waitkinds other than TARGET_WAITKIND_STOPPED are not
1222      affected by gdbarch_decr_pc_after_break.  Other waitkinds which are
1223      implemented by software breakpoints should be handled through the normal
1224      breakpoint layer.
1225
1226      NOTE drow/2004-01-31: On some targets, breakpoints may generate
1227      different signals (SIGILL or SIGEMT for instance), but it is less
1228      clear where the PC is pointing afterwards.  It may not match
1229      gdbarch_decr_pc_after_break.  I don't know any specific target that
1230      generates these signals at breakpoints (the code has been in GDB since at
1231      least 1992) so I can not guess how to handle them here.
1232
1233      In earlier versions of GDB, a target with 
1234      gdbarch_have_nonsteppable_watchpoint would have the PC after hitting a
1235      watchpoint affected by gdbarch_decr_pc_after_break.  I haven't found any
1236      target with both of these set in GDB history, and it seems unlikely to be
1237      correct, so gdbarch_have_nonsteppable_watchpoint is not checked here.  */
1238
1239   if (ecs->ws.kind != TARGET_WAITKIND_STOPPED)
1240     return;
1241
1242   if (ecs->ws.value.sig != TARGET_SIGNAL_TRAP)
1243     return;
1244
1245   /* Find the location where (if we've hit a breakpoint) the
1246      breakpoint would be.  */
1247   breakpoint_pc = read_pc_pid (ecs->ptid) - gdbarch_decr_pc_after_break
1248                                             (current_gdbarch);
1249
1250   /* Check whether there actually is a software breakpoint inserted
1251      at that location.  */
1252   if (software_breakpoint_inserted_here_p (breakpoint_pc))
1253     {
1254       /* When using hardware single-step, a SIGTRAP is reported for both
1255          a completed single-step and a software breakpoint.  Need to
1256          differentiate between the two, as the latter needs adjusting
1257          but the former does not.
1258
1259          The SIGTRAP can be due to a completed hardware single-step only if 
1260           - we didn't insert software single-step breakpoints
1261           - the thread to be examined is still the current thread
1262           - this thread is currently being stepped
1263
1264          If any of these events did not occur, we must have stopped due
1265          to hitting a software breakpoint, and have to back up to the
1266          breakpoint address.
1267
1268          As a special case, we could have hardware single-stepped a
1269          software breakpoint.  In this case (prev_pc == breakpoint_pc),
1270          we also need to back up to the breakpoint address.  */
1271
1272       if (singlestep_breakpoints_inserted_p
1273           || !ptid_equal (ecs->ptid, inferior_ptid)
1274           || !currently_stepping (ecs)
1275           || prev_pc == breakpoint_pc)
1276         write_pc_pid (breakpoint_pc, ecs->ptid);
1277     }
1278 }
1279
1280 /* Given an execution control state that has been freshly filled in
1281    by an event from the inferior, figure out what it means and take
1282    appropriate action.  */
1283
1284 void
1285 handle_inferior_event (struct execution_control_state *ecs)
1286 {
1287   int sw_single_step_trap_p = 0;
1288   int stopped_by_watchpoint;
1289   int stepped_after_stopped_by_watchpoint = 0;
1290
1291   /* Cache the last pid/waitstatus. */
1292   target_last_wait_ptid = ecs->ptid;
1293   target_last_waitstatus = *ecs->wp;
1294
1295   /* Always clear state belonging to the previous time we stopped.  */
1296   stop_stack_dummy = 0;
1297
1298   adjust_pc_after_break (ecs);
1299
1300   switch (ecs->infwait_state)
1301     {
1302     case infwait_thread_hop_state:
1303       if (debug_infrun)
1304         fprintf_unfiltered (gdb_stdlog, "infrun: infwait_thread_hop_state\n");
1305       /* Cancel the waiton_ptid. */
1306       ecs->waiton_ptid = pid_to_ptid (-1);
1307       break;
1308
1309     case infwait_normal_state:
1310       if (debug_infrun)
1311         fprintf_unfiltered (gdb_stdlog, "infrun: infwait_normal_state\n");
1312       break;
1313
1314     case infwait_step_watch_state:
1315       if (debug_infrun)
1316         fprintf_unfiltered (gdb_stdlog,
1317                             "infrun: infwait_step_watch_state\n");
1318
1319       stepped_after_stopped_by_watchpoint = 1;
1320       break;
1321
1322     case infwait_nonstep_watch_state:
1323       if (debug_infrun)
1324         fprintf_unfiltered (gdb_stdlog,
1325                             "infrun: infwait_nonstep_watch_state\n");
1326       insert_breakpoints ();
1327
1328       /* FIXME-maybe: is this cleaner than setting a flag?  Does it
1329          handle things like signals arriving and other things happening
1330          in combination correctly?  */
1331       stepped_after_stopped_by_watchpoint = 1;
1332       break;
1333
1334     default:
1335       internal_error (__FILE__, __LINE__, _("bad switch"));
1336     }
1337   ecs->infwait_state = infwait_normal_state;
1338
1339   reinit_frame_cache ();
1340
1341   /* If it's a new process, add it to the thread database */
1342
1343   ecs->new_thread_event = (!ptid_equal (ecs->ptid, inferior_ptid)
1344                            && !ptid_equal (ecs->ptid, minus_one_ptid)
1345                            && !in_thread_list (ecs->ptid));
1346
1347   if (ecs->ws.kind != TARGET_WAITKIND_EXITED
1348       && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED && ecs->new_thread_event)
1349     add_thread (ecs->ptid);
1350
1351   switch (ecs->ws.kind)
1352     {
1353     case TARGET_WAITKIND_LOADED:
1354       if (debug_infrun)
1355         fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_LOADED\n");
1356       /* Ignore gracefully during startup of the inferior, as it might
1357          be the shell which has just loaded some objects, otherwise
1358          add the symbols for the newly loaded objects.  Also ignore at
1359          the beginning of an attach or remote session; we will query
1360          the full list of libraries once the connection is
1361          established.  */
1362       if (stop_soon == NO_STOP_QUIETLY)
1363         {
1364           /* Remove breakpoints, SOLIB_ADD might adjust
1365              breakpoint addresses via breakpoint_re_set.  */
1366           remove_breakpoints ();
1367
1368           /* Check for any newly added shared libraries if we're
1369              supposed to be adding them automatically.  Switch
1370              terminal for any messages produced by
1371              breakpoint_re_set.  */
1372           target_terminal_ours_for_output ();
1373           /* NOTE: cagney/2003-11-25: Make certain that the target
1374              stack's section table is kept up-to-date.  Architectures,
1375              (e.g., PPC64), use the section table to perform
1376              operations such as address => section name and hence
1377              require the table to contain all sections (including
1378              those found in shared libraries).  */
1379           /* NOTE: cagney/2003-11-25: Pass current_target and not
1380              exec_ops to SOLIB_ADD.  This is because current GDB is
1381              only tooled to propagate section_table changes out from
1382              the "current_target" (see target_resize_to_sections), and
1383              not up from the exec stratum.  This, of course, isn't
1384              right.  "infrun.c" should only interact with the
1385              exec/process stratum, instead relying on the target stack
1386              to propagate relevant changes (stop, section table
1387              changed, ...) up to other layers.  */
1388 #ifdef SOLIB_ADD
1389           SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
1390 #else
1391           solib_add (NULL, 0, &current_target, auto_solib_add);
1392 #endif
1393           target_terminal_inferior ();
1394
1395           /* If requested, stop when the dynamic linker notifies
1396              gdb of events.  This allows the user to get control
1397              and place breakpoints in initializer routines for
1398              dynamically loaded objects (among other things).  */
1399           if (stop_on_solib_events)
1400             {
1401               stop_stepping (ecs);
1402               return;
1403             }
1404
1405           /* NOTE drow/2007-05-11: This might be a good place to check
1406              for "catch load".  */
1407
1408           /* Reinsert breakpoints and continue.  */
1409           insert_breakpoints ();
1410         }
1411
1412       /* If we are skipping through a shell, or through shared library
1413          loading that we aren't interested in, resume the program.  If
1414          we're running the program normally, also resume.  But stop if
1415          we're attaching or setting up a remote connection.  */
1416       if (stop_soon == STOP_QUIETLY || stop_soon == NO_STOP_QUIETLY)
1417         {
1418           resume (0, TARGET_SIGNAL_0);
1419           prepare_to_wait (ecs);
1420           return;
1421         }
1422
1423       break;
1424
1425     case TARGET_WAITKIND_SPURIOUS:
1426       if (debug_infrun)
1427         fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SPURIOUS\n");
1428       resume (0, TARGET_SIGNAL_0);
1429       prepare_to_wait (ecs);
1430       return;
1431
1432     case TARGET_WAITKIND_EXITED:
1433       if (debug_infrun)
1434         fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_EXITED\n");
1435       target_terminal_ours ();  /* Must do this before mourn anyway */
1436       print_stop_reason (EXITED, ecs->ws.value.integer);
1437
1438       /* Record the exit code in the convenience variable $_exitcode, so
1439          that the user can inspect this again later.  */
1440       set_internalvar (lookup_internalvar ("_exitcode"),
1441                        value_from_longest (builtin_type_int,
1442                                            (LONGEST) ecs->ws.value.integer));
1443       gdb_flush (gdb_stdout);
1444       target_mourn_inferior ();
1445       singlestep_breakpoints_inserted_p = 0;
1446       stop_print_frame = 0;
1447       stop_stepping (ecs);
1448       return;
1449
1450     case TARGET_WAITKIND_SIGNALLED:
1451       if (debug_infrun)
1452         fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SIGNALLED\n");
1453       stop_print_frame = 0;
1454       stop_signal = ecs->ws.value.sig;
1455       target_terminal_ours ();  /* Must do this before mourn anyway */
1456
1457       /* Note: By definition of TARGET_WAITKIND_SIGNALLED, we shouldn't
1458          reach here unless the inferior is dead.  However, for years
1459          target_kill() was called here, which hints that fatal signals aren't
1460          really fatal on some systems.  If that's true, then some changes
1461          may be needed. */
1462       target_mourn_inferior ();
1463
1464       print_stop_reason (SIGNAL_EXITED, stop_signal);
1465       singlestep_breakpoints_inserted_p = 0;
1466       stop_stepping (ecs);
1467       return;
1468
1469       /* The following are the only cases in which we keep going;
1470          the above cases end in a continue or goto. */
1471     case TARGET_WAITKIND_FORKED:
1472     case TARGET_WAITKIND_VFORKED:
1473       if (debug_infrun)
1474         fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_FORKED\n");
1475       stop_signal = TARGET_SIGNAL_TRAP;
1476       pending_follow.kind = ecs->ws.kind;
1477
1478       pending_follow.fork_event.parent_pid = PIDGET (ecs->ptid);
1479       pending_follow.fork_event.child_pid = ecs->ws.value.related_pid;
1480
1481       if (!ptid_equal (ecs->ptid, inferior_ptid))
1482         {
1483           context_switch (ecs);
1484           reinit_frame_cache ();
1485         }
1486
1487       stop_pc = read_pc ();
1488
1489       stop_bpstat = bpstat_stop_status (stop_pc, ecs->ptid);
1490
1491       ecs->random_signal = !bpstat_explains_signal (stop_bpstat);
1492
1493       /* If no catchpoint triggered for this, then keep going.  */
1494       if (ecs->random_signal)
1495         {
1496           stop_signal = TARGET_SIGNAL_0;
1497           keep_going (ecs);
1498           return;
1499         }
1500       goto process_event_stop_test;
1501
1502     case TARGET_WAITKIND_EXECD:
1503       if (debug_infrun)
1504         fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_EXECD\n");
1505       stop_signal = TARGET_SIGNAL_TRAP;
1506
1507       /* NOTE drow/2002-12-05: This code should be pushed down into the
1508          target_wait function.  Until then following vfork on HP/UX 10.20
1509          is probably broken by this.  Of course, it's broken anyway.  */
1510       /* Is this a target which reports multiple exec events per actual
1511          call to exec()?  (HP-UX using ptrace does, for example.)  If so,
1512          ignore all but the last one.  Just resume the exec'r, and wait
1513          for the next exec event. */
1514       if (inferior_ignoring_leading_exec_events)
1515         {
1516           inferior_ignoring_leading_exec_events--;
1517           target_resume (ecs->ptid, 0, TARGET_SIGNAL_0);
1518           prepare_to_wait (ecs);
1519           return;
1520         }
1521       inferior_ignoring_leading_exec_events =
1522         target_reported_exec_events_per_exec_call () - 1;
1523
1524       pending_follow.execd_pathname =
1525         savestring (ecs->ws.value.execd_pathname,
1526                     strlen (ecs->ws.value.execd_pathname));
1527
1528       /* This causes the eventpoints and symbol table to be reset.  Must
1529          do this now, before trying to determine whether to stop. */
1530       follow_exec (PIDGET (inferior_ptid), pending_follow.execd_pathname);
1531       xfree (pending_follow.execd_pathname);
1532
1533       stop_pc = read_pc_pid (ecs->ptid);
1534       ecs->saved_inferior_ptid = inferior_ptid;
1535       inferior_ptid = ecs->ptid;
1536
1537       stop_bpstat = bpstat_stop_status (stop_pc, ecs->ptid);
1538
1539       ecs->random_signal = !bpstat_explains_signal (stop_bpstat);
1540       inferior_ptid = ecs->saved_inferior_ptid;
1541
1542       if (!ptid_equal (ecs->ptid, inferior_ptid))
1543         {
1544           context_switch (ecs);
1545           reinit_frame_cache ();
1546         }
1547
1548       /* If no catchpoint triggered for this, then keep going.  */
1549       if (ecs->random_signal)
1550         {
1551           stop_signal = TARGET_SIGNAL_0;
1552           keep_going (ecs);
1553           return;
1554         }
1555       goto process_event_stop_test;
1556
1557       /* Be careful not to try to gather much state about a thread
1558          that's in a syscall.  It's frequently a losing proposition.  */
1559     case TARGET_WAITKIND_SYSCALL_ENTRY:
1560       if (debug_infrun)
1561         fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SYSCALL_ENTRY\n");
1562       resume (0, TARGET_SIGNAL_0);
1563       prepare_to_wait (ecs);
1564       return;
1565
1566       /* Before examining the threads further, step this thread to
1567          get it entirely out of the syscall.  (We get notice of the
1568          event when the thread is just on the verge of exiting a
1569          syscall.  Stepping one instruction seems to get it back
1570          into user code.)  */
1571     case TARGET_WAITKIND_SYSCALL_RETURN:
1572       if (debug_infrun)
1573         fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SYSCALL_RETURN\n");
1574       target_resume (ecs->ptid, 1, TARGET_SIGNAL_0);
1575       prepare_to_wait (ecs);
1576       return;
1577
1578     case TARGET_WAITKIND_STOPPED:
1579       if (debug_infrun)
1580         fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_STOPPED\n");
1581       stop_signal = ecs->ws.value.sig;
1582       break;
1583
1584       /* We had an event in the inferior, but we are not interested
1585          in handling it at this level. The lower layers have already
1586          done what needs to be done, if anything.
1587
1588          One of the possible circumstances for this is when the
1589          inferior produces output for the console. The inferior has
1590          not stopped, and we are ignoring the event.  Another possible
1591          circumstance is any event which the lower level knows will be
1592          reported multiple times without an intervening resume.  */
1593     case TARGET_WAITKIND_IGNORE:
1594       if (debug_infrun)
1595         fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_IGNORE\n");
1596       prepare_to_wait (ecs);
1597       return;
1598     }
1599
1600   /* We may want to consider not doing a resume here in order to give
1601      the user a chance to play with the new thread.  It might be good
1602      to make that a user-settable option.  */
1603
1604   /* At this point, all threads are stopped (happens automatically in
1605      either the OS or the native code).  Therefore we need to continue
1606      all threads in order to make progress.  */
1607   if (ecs->new_thread_event)
1608     {
1609       target_resume (RESUME_ALL, 0, TARGET_SIGNAL_0);
1610       prepare_to_wait (ecs);
1611       return;
1612     }
1613
1614   stop_pc = read_pc_pid (ecs->ptid);
1615
1616   if (debug_infrun)
1617     fprintf_unfiltered (gdb_stdlog, "infrun: stop_pc = 0x%s\n", paddr_nz (stop_pc));
1618
1619   if (stepping_past_singlestep_breakpoint)
1620     {
1621       gdb_assert (singlestep_breakpoints_inserted_p);
1622       gdb_assert (ptid_equal (singlestep_ptid, ecs->ptid));
1623       gdb_assert (!ptid_equal (singlestep_ptid, saved_singlestep_ptid));
1624
1625       stepping_past_singlestep_breakpoint = 0;
1626
1627       /* We've either finished single-stepping past the single-step
1628          breakpoint, or stopped for some other reason.  It would be nice if
1629          we could tell, but we can't reliably.  */
1630       if (stop_signal == TARGET_SIGNAL_TRAP)
1631         {
1632           if (debug_infrun)
1633             fprintf_unfiltered (gdb_stdlog, "infrun: stepping_past_singlestep_breakpoint\n");
1634           /* Pull the single step breakpoints out of the target.  */
1635           remove_single_step_breakpoints ();
1636           singlestep_breakpoints_inserted_p = 0;
1637
1638           ecs->random_signal = 0;
1639
1640           ecs->ptid = saved_singlestep_ptid;
1641           context_switch (ecs);
1642           if (deprecated_context_hook)
1643             deprecated_context_hook (pid_to_thread_id (ecs->ptid));
1644
1645           resume (1, TARGET_SIGNAL_0);
1646           prepare_to_wait (ecs);
1647           return;
1648         }
1649     }
1650
1651   stepping_past_singlestep_breakpoint = 0;
1652
1653   if (!ptid_equal (deferred_step_ptid, null_ptid))
1654     {
1655       /* If we stopped for some other reason than single-stepping, ignore
1656          the fact that we were supposed to switch back.  */
1657       if (stop_signal == TARGET_SIGNAL_TRAP)
1658         {
1659           if (debug_infrun)
1660             fprintf_unfiltered (gdb_stdlog,
1661                                 "infrun: handling deferred step\n");
1662
1663           /* Pull the single step breakpoints out of the target.  */
1664           if (singlestep_breakpoints_inserted_p)
1665             {
1666               remove_single_step_breakpoints ();
1667               singlestep_breakpoints_inserted_p = 0;
1668             }
1669
1670           /* Note: We do not call context_switch at this point, as the
1671              context is already set up for stepping the original thread.  */
1672           switch_to_thread (deferred_step_ptid);
1673           deferred_step_ptid = null_ptid;
1674           /* Suppress spurious "Switching to ..." message.  */
1675           previous_inferior_ptid = inferior_ptid;
1676
1677           resume (1, TARGET_SIGNAL_0);
1678           prepare_to_wait (ecs);
1679           return;
1680         }
1681
1682       deferred_step_ptid = null_ptid;
1683     }
1684
1685   /* See if a thread hit a thread-specific breakpoint that was meant for
1686      another thread.  If so, then step that thread past the breakpoint,
1687      and continue it.  */
1688
1689   if (stop_signal == TARGET_SIGNAL_TRAP)
1690     {
1691       int thread_hop_needed = 0;
1692
1693       /* Check if a regular breakpoint has been hit before checking
1694          for a potential single step breakpoint. Otherwise, GDB will
1695          not see this breakpoint hit when stepping onto breakpoints.  */
1696       if (regular_breakpoint_inserted_here_p (stop_pc))
1697         {
1698           ecs->random_signal = 0;
1699           if (!breakpoint_thread_match (stop_pc, ecs->ptid))
1700             thread_hop_needed = 1;
1701         }
1702       else if (singlestep_breakpoints_inserted_p)
1703         {
1704           /* We have not context switched yet, so this should be true
1705              no matter which thread hit the singlestep breakpoint.  */
1706           gdb_assert (ptid_equal (inferior_ptid, singlestep_ptid));
1707           if (debug_infrun)
1708             fprintf_unfiltered (gdb_stdlog, "infrun: software single step "
1709                                 "trap for %s\n",
1710                                 target_pid_to_str (ecs->ptid));
1711
1712           ecs->random_signal = 0;
1713           /* The call to in_thread_list is necessary because PTIDs sometimes
1714              change when we go from single-threaded to multi-threaded.  If
1715              the singlestep_ptid is still in the list, assume that it is
1716              really different from ecs->ptid.  */
1717           if (!ptid_equal (singlestep_ptid, ecs->ptid)
1718               && in_thread_list (singlestep_ptid))
1719             {
1720               /* If the PC of the thread we were trying to single-step
1721                  has changed, discard this event (which we were going
1722                  to ignore anyway), and pretend we saw that thread
1723                  trap.  This prevents us continuously moving the
1724                  single-step breakpoint forward, one instruction at a
1725                  time.  If the PC has changed, then the thread we were
1726                  trying to single-step has trapped or been signalled,
1727                  but the event has not been reported to GDB yet.
1728
1729                  There might be some cases where this loses signal
1730                  information, if a signal has arrived at exactly the
1731                  same time that the PC changed, but this is the best
1732                  we can do with the information available.  Perhaps we
1733                  should arrange to report all events for all threads
1734                  when they stop, or to re-poll the remote looking for
1735                  this particular thread (i.e. temporarily enable
1736                  schedlock).  */
1737              if (read_pc_pid (singlestep_ptid) != singlestep_pc)
1738                {
1739                  if (debug_infrun)
1740                    fprintf_unfiltered (gdb_stdlog, "infrun: unexpected thread,"
1741                                        " but expected thread advanced also\n");
1742
1743                  /* The current context still belongs to
1744                     singlestep_ptid.  Don't swap here, since that's
1745                     the context we want to use.  Just fudge our
1746                     state and continue.  */
1747                  ecs->ptid = singlestep_ptid;
1748                  stop_pc = read_pc_pid (ecs->ptid);
1749                }
1750              else
1751                {
1752                  if (debug_infrun)
1753                    fprintf_unfiltered (gdb_stdlog,
1754                                        "infrun: unexpected thread\n");
1755
1756                  thread_hop_needed = 1;
1757                  stepping_past_singlestep_breakpoint = 1;
1758                  saved_singlestep_ptid = singlestep_ptid;
1759                }
1760             }
1761         }
1762
1763       if (thread_hop_needed)
1764         {
1765           int remove_status;
1766
1767           if (debug_infrun)
1768             fprintf_unfiltered (gdb_stdlog, "infrun: thread_hop_needed\n");
1769
1770           /* Saw a breakpoint, but it was hit by the wrong thread.
1771              Just continue. */
1772
1773           if (singlestep_breakpoints_inserted_p)
1774             {
1775               /* Pull the single step breakpoints out of the target. */
1776               remove_single_step_breakpoints ();
1777               singlestep_breakpoints_inserted_p = 0;
1778             }
1779
1780           remove_status = remove_breakpoints ();
1781           /* Did we fail to remove breakpoints?  If so, try
1782              to set the PC past the bp.  (There's at least
1783              one situation in which we can fail to remove
1784              the bp's: On HP-UX's that use ttrace, we can't
1785              change the address space of a vforking child
1786              process until the child exits (well, okay, not
1787              then either :-) or execs. */
1788           if (remove_status != 0)
1789             error (_("Cannot step over breakpoint hit in wrong thread"));
1790           else
1791             {                   /* Single step */
1792               if (!ptid_equal (inferior_ptid, ecs->ptid))
1793                 context_switch (ecs);
1794               ecs->waiton_ptid = ecs->ptid;
1795               ecs->wp = &(ecs->ws);
1796               ecs->stepping_over_breakpoint = 1;
1797
1798               ecs->infwait_state = infwait_thread_hop_state;
1799               keep_going (ecs);
1800               registers_changed ();
1801               return;
1802             }
1803         }
1804       else if (singlestep_breakpoints_inserted_p)
1805         {
1806           sw_single_step_trap_p = 1;
1807           ecs->random_signal = 0;
1808         }
1809     }
1810   else
1811     ecs->random_signal = 1;
1812
1813   /* See if something interesting happened to the non-current thread.  If
1814      so, then switch to that thread.  */
1815   if (!ptid_equal (ecs->ptid, inferior_ptid))
1816     {
1817       if (debug_infrun)
1818         fprintf_unfiltered (gdb_stdlog, "infrun: context switch\n");
1819
1820       context_switch (ecs);
1821
1822       if (deprecated_context_hook)
1823         deprecated_context_hook (pid_to_thread_id (ecs->ptid));
1824     }
1825
1826   if (singlestep_breakpoints_inserted_p)
1827     {
1828       /* Pull the single step breakpoints out of the target. */
1829       remove_single_step_breakpoints ();
1830       singlestep_breakpoints_inserted_p = 0;
1831     }
1832
1833   if (stepped_after_stopped_by_watchpoint)
1834     stopped_by_watchpoint = 0;
1835   else
1836     stopped_by_watchpoint = watchpoints_triggered (&ecs->ws);
1837
1838   /* If necessary, step over this watchpoint.  We'll be back to display
1839      it in a moment.  */
1840   if (stopped_by_watchpoint
1841       && (HAVE_STEPPABLE_WATCHPOINT
1842           || gdbarch_have_nonsteppable_watchpoint (current_gdbarch)))
1843     {
1844       if (debug_infrun)
1845         fprintf_unfiltered (gdb_stdlog, "infrun: STOPPED_BY_WATCHPOINT\n");
1846
1847       /* At this point, we are stopped at an instruction which has
1848          attempted to write to a piece of memory under control of
1849          a watchpoint.  The instruction hasn't actually executed
1850          yet.  If we were to evaluate the watchpoint expression
1851          now, we would get the old value, and therefore no change
1852          would seem to have occurred.
1853
1854          In order to make watchpoints work `right', we really need
1855          to complete the memory write, and then evaluate the
1856          watchpoint expression.  We do this by single-stepping the
1857          target.
1858
1859          It may not be necessary to disable the watchpoint to stop over
1860          it.  For example, the PA can (with some kernel cooperation)
1861          single step over a watchpoint without disabling the watchpoint.
1862
1863          It is far more common to need to disable a watchpoint to step
1864          the inferior over it.  If we have non-steppable watchpoints,
1865          we must disable the current watchpoint; it's simplest to
1866          disable all watchpoints and breakpoints.  */
1867          
1868       if (!HAVE_STEPPABLE_WATCHPOINT)
1869         remove_breakpoints ();
1870       registers_changed ();
1871       target_resume (ecs->ptid, 1, TARGET_SIGNAL_0);    /* Single step */
1872       ecs->waiton_ptid = ecs->ptid;
1873       if (HAVE_STEPPABLE_WATCHPOINT)
1874         ecs->infwait_state = infwait_step_watch_state;
1875       else
1876         ecs->infwait_state = infwait_nonstep_watch_state;
1877       prepare_to_wait (ecs);
1878       return;
1879     }
1880
1881   ecs->stop_func_start = 0;
1882   ecs->stop_func_end = 0;
1883   ecs->stop_func_name = 0;
1884   /* Don't care about return value; stop_func_start and stop_func_name
1885      will both be 0 if it doesn't work.  */
1886   find_pc_partial_function (stop_pc, &ecs->stop_func_name,
1887                             &ecs->stop_func_start, &ecs->stop_func_end);
1888   ecs->stop_func_start
1889     += gdbarch_deprecated_function_start_offset (current_gdbarch);
1890   ecs->stepping_over_breakpoint = 0;
1891   bpstat_clear (&stop_bpstat);
1892   stop_step = 0;
1893   stop_print_frame = 1;
1894   ecs->random_signal = 0;
1895   stopped_by_random_signal = 0;
1896
1897   if (stop_signal == TARGET_SIGNAL_TRAP
1898       && stepping_over_breakpoint
1899       && gdbarch_single_step_through_delay_p (current_gdbarch)
1900       && currently_stepping (ecs))
1901     {
1902       /* We're trying to step off a breakpoint.  Turns out that we're
1903          also on an instruction that needs to be stepped multiple
1904          times before it's been fully executing. E.g., architectures
1905          with a delay slot.  It needs to be stepped twice, once for
1906          the instruction and once for the delay slot.  */
1907       int step_through_delay
1908         = gdbarch_single_step_through_delay (current_gdbarch,
1909                                              get_current_frame ());
1910       if (debug_infrun && step_through_delay)
1911         fprintf_unfiltered (gdb_stdlog, "infrun: step through delay\n");
1912       if (step_range_end == 0 && step_through_delay)
1913         {
1914           /* The user issued a continue when stopped at a breakpoint.
1915              Set up for another trap and get out of here.  */
1916          ecs->stepping_over_breakpoint = 1;
1917          keep_going (ecs);
1918          return;
1919         }
1920       else if (step_through_delay)
1921         {
1922           /* The user issued a step when stopped at a breakpoint.
1923              Maybe we should stop, maybe we should not - the delay
1924              slot *might* correspond to a line of source.  In any
1925              case, don't decide that here, just set 
1926              ecs->stepping_over_breakpoint, making sure we 
1927              single-step again before breakpoints are re-inserted.  */
1928           ecs->stepping_over_breakpoint = 1;
1929         }
1930     }
1931
1932   /* Look at the cause of the stop, and decide what to do.
1933      The alternatives are:
1934      1) break; to really stop and return to the debugger,
1935      2) drop through to start up again
1936      (set ecs->stepping_over_breakpoint to 1 to single step once)
1937      3) set ecs->random_signal to 1, and the decision between 1 and 2
1938      will be made according to the signal handling tables.  */
1939
1940   /* First, distinguish signals caused by the debugger from signals
1941      that have to do with the program's own actions.  Note that
1942      breakpoint insns may cause SIGTRAP or SIGILL or SIGEMT, depending
1943      on the operating system version.  Here we detect when a SIGILL or
1944      SIGEMT is really a breakpoint and change it to SIGTRAP.  We do
1945      something similar for SIGSEGV, since a SIGSEGV will be generated
1946      when we're trying to execute a breakpoint instruction on a
1947      non-executable stack.  This happens for call dummy breakpoints
1948      for architectures like SPARC that place call dummies on the
1949      stack.  */
1950
1951   if (stop_signal == TARGET_SIGNAL_TRAP
1952       || (breakpoint_inserted_here_p (stop_pc)
1953           && (stop_signal == TARGET_SIGNAL_ILL
1954               || stop_signal == TARGET_SIGNAL_SEGV
1955               || stop_signal == TARGET_SIGNAL_EMT))
1956       || stop_soon == STOP_QUIETLY || stop_soon == STOP_QUIETLY_NO_SIGSTOP
1957       || stop_soon == STOP_QUIETLY_REMOTE)
1958     {
1959       if (stop_signal == TARGET_SIGNAL_TRAP && stop_after_trap)
1960         {
1961           if (debug_infrun)
1962             fprintf_unfiltered (gdb_stdlog, "infrun: stopped\n");
1963           stop_print_frame = 0;
1964           stop_stepping (ecs);
1965           return;
1966         }
1967
1968       /* This is originated from start_remote(), start_inferior() and
1969          shared libraries hook functions.  */
1970       if (stop_soon == STOP_QUIETLY || stop_soon == STOP_QUIETLY_REMOTE)
1971         {
1972           if (debug_infrun)
1973             fprintf_unfiltered (gdb_stdlog, "infrun: quietly stopped\n");
1974           stop_stepping (ecs);
1975           return;
1976         }
1977
1978       /* This originates from attach_command().  We need to overwrite
1979          the stop_signal here, because some kernels don't ignore a
1980          SIGSTOP in a subsequent ptrace(PTRACE_SONT,SOGSTOP) call.
1981          See more comments in inferior.h.  */
1982       if (stop_soon == STOP_QUIETLY_NO_SIGSTOP)
1983         {
1984           stop_stepping (ecs);
1985           if (stop_signal == TARGET_SIGNAL_STOP)
1986             stop_signal = TARGET_SIGNAL_0;
1987           return;
1988         }
1989
1990       /* See if there is a breakpoint at the current PC.  */
1991       stop_bpstat = bpstat_stop_status (stop_pc, ecs->ptid);
1992       
1993       /* Following in case break condition called a
1994          function.  */
1995       stop_print_frame = 1;
1996
1997       /* NOTE: cagney/2003-03-29: These two checks for a random signal
1998          at one stage in the past included checks for an inferior
1999          function call's call dummy's return breakpoint.  The original
2000          comment, that went with the test, read:
2001
2002          ``End of a stack dummy.  Some systems (e.g. Sony news) give
2003          another signal besides SIGTRAP, so check here as well as
2004          above.''
2005
2006          If someone ever tries to get get call dummys on a
2007          non-executable stack to work (where the target would stop
2008          with something like a SIGSEGV), then those tests might need
2009          to be re-instated.  Given, however, that the tests were only
2010          enabled when momentary breakpoints were not being used, I
2011          suspect that it won't be the case.
2012
2013          NOTE: kettenis/2004-02-05: Indeed such checks don't seem to
2014          be necessary for call dummies on a non-executable stack on
2015          SPARC.  */
2016
2017       if (stop_signal == TARGET_SIGNAL_TRAP)
2018         ecs->random_signal
2019           = !(bpstat_explains_signal (stop_bpstat)
2020               || stepping_over_breakpoint
2021               || (step_range_end && step_resume_breakpoint == NULL));
2022       else
2023         {
2024           ecs->random_signal = !bpstat_explains_signal (stop_bpstat);
2025           if (!ecs->random_signal)
2026             stop_signal = TARGET_SIGNAL_TRAP;
2027         }
2028     }
2029
2030   /* When we reach this point, we've pretty much decided
2031      that the reason for stopping must've been a random
2032      (unexpected) signal. */
2033
2034   else
2035     ecs->random_signal = 1;
2036
2037 process_event_stop_test:
2038   /* For the program's own signals, act according to
2039      the signal handling tables.  */
2040
2041   if (ecs->random_signal)
2042     {
2043       /* Signal not for debugging purposes.  */
2044       int printed = 0;
2045
2046       if (debug_infrun)
2047          fprintf_unfiltered (gdb_stdlog, "infrun: random signal %d\n", stop_signal);
2048
2049       stopped_by_random_signal = 1;
2050
2051       if (signal_print[stop_signal])
2052         {
2053           printed = 1;
2054           target_terminal_ours_for_output ();
2055           print_stop_reason (SIGNAL_RECEIVED, stop_signal);
2056         }
2057       if (signal_stop[stop_signal])
2058         {
2059           stop_stepping (ecs);
2060           return;
2061         }
2062       /* If not going to stop, give terminal back
2063          if we took it away.  */
2064       else if (printed)
2065         target_terminal_inferior ();
2066
2067       /* Clear the signal if it should not be passed.  */
2068       if (signal_program[stop_signal] == 0)
2069         stop_signal = TARGET_SIGNAL_0;
2070
2071       if (prev_pc == read_pc ()
2072           && breakpoint_here_p (read_pc ())
2073           && !breakpoint_inserted_here_p (read_pc ())
2074           && step_resume_breakpoint == NULL)
2075         {
2076           /* We were just starting a new sequence, attempting to
2077              single-step off of a breakpoint and expecting a SIGTRAP.
2078              Intead this signal arrives.  This signal will take us out
2079              of the stepping range so GDB needs to remember to, when
2080              the signal handler returns, resume stepping off that
2081              breakpoint.  */
2082           /* To simplify things, "continue" is forced to use the same
2083              code paths as single-step - set a breakpoint at the
2084              signal return address and then, once hit, step off that
2085              breakpoint.  */
2086
2087           insert_step_resume_breakpoint_at_frame (get_current_frame ());
2088           ecs->step_after_step_resume_breakpoint = 1;
2089           keep_going (ecs);
2090           return;
2091         }
2092
2093       if (step_range_end != 0
2094           && stop_signal != TARGET_SIGNAL_0
2095           && stop_pc >= step_range_start && stop_pc < step_range_end
2096           && frame_id_eq (get_frame_id (get_current_frame ()),
2097                           step_frame_id)
2098           && step_resume_breakpoint == NULL)
2099         {
2100           /* The inferior is about to take a signal that will take it
2101              out of the single step range.  Set a breakpoint at the
2102              current PC (which is presumably where the signal handler
2103              will eventually return) and then allow the inferior to
2104              run free.
2105
2106              Note that this is only needed for a signal delivered
2107              while in the single-step range.  Nested signals aren't a
2108              problem as they eventually all return.  */
2109           insert_step_resume_breakpoint_at_frame (get_current_frame ());
2110           keep_going (ecs);
2111           return;
2112         }
2113
2114       /* Note: step_resume_breakpoint may be non-NULL.  This occures
2115          when either there's a nested signal, or when there's a
2116          pending signal enabled just as the signal handler returns
2117          (leaving the inferior at the step-resume-breakpoint without
2118          actually executing it).  Either way continue until the
2119          breakpoint is really hit.  */
2120       keep_going (ecs);
2121       return;
2122     }
2123
2124   /* Handle cases caused by hitting a breakpoint.  */
2125   {
2126     CORE_ADDR jmp_buf_pc;
2127     struct bpstat_what what;
2128
2129     what = bpstat_what (stop_bpstat);
2130
2131     if (what.call_dummy)
2132       {
2133         stop_stack_dummy = 1;
2134       }
2135
2136     switch (what.main_action)
2137       {
2138       case BPSTAT_WHAT_SET_LONGJMP_RESUME:
2139         /* If we hit the breakpoint at longjmp, disable it for the
2140            duration of this command.  Then, install a temporary
2141            breakpoint at the target of the jmp_buf. */
2142         if (debug_infrun)
2143           fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_SET_LONGJMP_RESUME\n");
2144         disable_longjmp_breakpoint ();
2145         if (!gdbarch_get_longjmp_target_p (current_gdbarch)
2146             || !gdbarch_get_longjmp_target (current_gdbarch,
2147                                             get_current_frame (), &jmp_buf_pc))
2148           {
2149             keep_going (ecs);
2150             return;
2151           }
2152
2153         /* Need to blow away step-resume breakpoint, as it
2154            interferes with us */
2155         if (step_resume_breakpoint != NULL)
2156           {
2157             delete_step_resume_breakpoint (&step_resume_breakpoint);
2158           }
2159
2160         set_longjmp_resume_breakpoint (jmp_buf_pc, null_frame_id);
2161         ecs->handling_longjmp = 1;      /* FIXME */
2162         keep_going (ecs);
2163         return;
2164
2165       case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
2166       case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME_SINGLE:
2167         if (debug_infrun)
2168           fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_CLEAR_LONGJMP_RESUME\n");
2169         disable_longjmp_breakpoint ();
2170         ecs->handling_longjmp = 0;      /* FIXME */
2171         if (what.main_action == BPSTAT_WHAT_CLEAR_LONGJMP_RESUME)
2172           break;
2173         /* else fallthrough */
2174
2175       case BPSTAT_WHAT_SINGLE:
2176         if (debug_infrun)
2177           fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_SINGLE\n");
2178         ecs->stepping_over_breakpoint = 1;
2179         /* Still need to check other stuff, at least the case
2180            where we are stepping and step out of the right range.  */
2181         break;
2182
2183       case BPSTAT_WHAT_STOP_NOISY:
2184         if (debug_infrun)
2185           fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_NOISY\n");
2186         stop_print_frame = 1;
2187
2188         /* We are about to nuke the step_resume_breakpointt via the
2189            cleanup chain, so no need to worry about it here.  */
2190
2191         stop_stepping (ecs);
2192         return;
2193
2194       case BPSTAT_WHAT_STOP_SILENT:
2195         if (debug_infrun)
2196           fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_SILENT\n");
2197         stop_print_frame = 0;
2198
2199         /* We are about to nuke the step_resume_breakpoin via the
2200            cleanup chain, so no need to worry about it here.  */
2201
2202         stop_stepping (ecs);
2203         return;
2204
2205       case BPSTAT_WHAT_STEP_RESUME:
2206         /* This proably demands a more elegant solution, but, yeah
2207            right...
2208
2209            This function's use of the simple variable
2210            step_resume_breakpoint doesn't seem to accomodate
2211            simultaneously active step-resume bp's, although the
2212            breakpoint list certainly can.
2213
2214            If we reach here and step_resume_breakpoint is already
2215            NULL, then apparently we have multiple active
2216            step-resume bp's.  We'll just delete the breakpoint we
2217            stopped at, and carry on.  
2218
2219            Correction: what the code currently does is delete a
2220            step-resume bp, but it makes no effort to ensure that
2221            the one deleted is the one currently stopped at.  MVS  */
2222
2223         if (debug_infrun)
2224           fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STEP_RESUME\n");
2225
2226         if (step_resume_breakpoint == NULL)
2227           {
2228             step_resume_breakpoint =
2229               bpstat_find_step_resume_breakpoint (stop_bpstat);
2230           }
2231         delete_step_resume_breakpoint (&step_resume_breakpoint);
2232         if (ecs->step_after_step_resume_breakpoint)
2233           {
2234             /* Back when the step-resume breakpoint was inserted, we
2235                were trying to single-step off a breakpoint.  Go back
2236                to doing that.  */
2237             ecs->step_after_step_resume_breakpoint = 0;
2238             ecs->stepping_over_breakpoint = 1;
2239             keep_going (ecs);
2240             return;
2241           }
2242         break;
2243
2244       case BPSTAT_WHAT_CHECK_SHLIBS:
2245       case BPSTAT_WHAT_CHECK_SHLIBS_RESUME_FROM_HOOK:
2246         {
2247           if (debug_infrun)
2248             fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_CHECK_SHLIBS\n");
2249           /* Remove breakpoints, we eventually want to step over the
2250              shlib event breakpoint, and SOLIB_ADD might adjust
2251              breakpoint addresses via breakpoint_re_set.  */
2252           remove_breakpoints ();
2253
2254           /* Check for any newly added shared libraries if we're
2255              supposed to be adding them automatically.  Switch
2256              terminal for any messages produced by
2257              breakpoint_re_set.  */
2258           target_terminal_ours_for_output ();
2259           /* NOTE: cagney/2003-11-25: Make certain that the target
2260              stack's section table is kept up-to-date.  Architectures,
2261              (e.g., PPC64), use the section table to perform
2262              operations such as address => section name and hence
2263              require the table to contain all sections (including
2264              those found in shared libraries).  */
2265           /* NOTE: cagney/2003-11-25: Pass current_target and not
2266              exec_ops to SOLIB_ADD.  This is because current GDB is
2267              only tooled to propagate section_table changes out from
2268              the "current_target" (see target_resize_to_sections), and
2269              not up from the exec stratum.  This, of course, isn't
2270              right.  "infrun.c" should only interact with the
2271              exec/process stratum, instead relying on the target stack
2272              to propagate relevant changes (stop, section table
2273              changed, ...) up to other layers.  */
2274 #ifdef SOLIB_ADD
2275           SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
2276 #else
2277           solib_add (NULL, 0, &current_target, auto_solib_add);
2278 #endif
2279           target_terminal_inferior ();
2280
2281           /* If requested, stop when the dynamic linker notifies
2282              gdb of events.  This allows the user to get control
2283              and place breakpoints in initializer routines for
2284              dynamically loaded objects (among other things).  */
2285           if (stop_on_solib_events || stop_stack_dummy)
2286             {
2287               stop_stepping (ecs);
2288               return;
2289             }
2290
2291           /* If we stopped due to an explicit catchpoint, then the
2292              (see above) call to SOLIB_ADD pulled in any symbols
2293              from a newly-loaded library, if appropriate.
2294
2295              We do want the inferior to stop, but not where it is
2296              now, which is in the dynamic linker callback.  Rather,
2297              we would like it stop in the user's program, just after
2298              the call that caused this catchpoint to trigger.  That
2299              gives the user a more useful vantage from which to
2300              examine their program's state. */
2301           else if (what.main_action
2302                    == BPSTAT_WHAT_CHECK_SHLIBS_RESUME_FROM_HOOK)
2303             {
2304               /* ??rehrauer: If I could figure out how to get the
2305                  right return PC from here, we could just set a temp
2306                  breakpoint and resume.  I'm not sure we can without
2307                  cracking open the dld's shared libraries and sniffing
2308                  their unwind tables and text/data ranges, and that's
2309                  not a terribly portable notion.
2310
2311                  Until that time, we must step the inferior out of the
2312                  dld callback, and also out of the dld itself (and any
2313                  code or stubs in libdld.sl, such as "shl_load" and
2314                  friends) until we reach non-dld code.  At that point,
2315                  we can stop stepping. */
2316               bpstat_get_triggered_catchpoints (stop_bpstat,
2317                                                 &ecs->
2318                                                 stepping_through_solib_catchpoints);
2319               ecs->stepping_through_solib_after_catch = 1;
2320
2321               /* Be sure to lift all breakpoints, so the inferior does
2322                  actually step past this point... */
2323               ecs->stepping_over_breakpoint = 1;
2324               break;
2325             }
2326           else
2327             {
2328               /* We want to step over this breakpoint, then keep going.  */
2329               ecs->stepping_over_breakpoint = 1;
2330               break;
2331             }
2332         }
2333         break;
2334
2335       case BPSTAT_WHAT_LAST:
2336         /* Not a real code, but listed here to shut up gcc -Wall.  */
2337
2338       case BPSTAT_WHAT_KEEP_CHECKING:
2339         break;
2340       }
2341   }
2342
2343   /* We come here if we hit a breakpoint but should not
2344      stop for it.  Possibly we also were stepping
2345      and should stop for that.  So fall through and
2346      test for stepping.  But, if not stepping,
2347      do not stop.  */
2348
2349   /* Are we stepping to get the inferior out of the dynamic linker's
2350      hook (and possibly the dld itself) after catching a shlib
2351      event?  */
2352   if (ecs->stepping_through_solib_after_catch)
2353     {
2354 #if defined(SOLIB_ADD)
2355       /* Have we reached our destination?  If not, keep going. */
2356       if (SOLIB_IN_DYNAMIC_LINKER (PIDGET (ecs->ptid), stop_pc))
2357         {
2358           if (debug_infrun)
2359             fprintf_unfiltered (gdb_stdlog, "infrun: stepping in dynamic linker\n");
2360           ecs->stepping_over_breakpoint = 1;
2361           keep_going (ecs);
2362           return;
2363         }
2364 #endif
2365       if (debug_infrun)
2366          fprintf_unfiltered (gdb_stdlog, "infrun: step past dynamic linker\n");
2367       /* Else, stop and report the catchpoint(s) whose triggering
2368          caused us to begin stepping. */
2369       ecs->stepping_through_solib_after_catch = 0;
2370       bpstat_clear (&stop_bpstat);
2371       stop_bpstat = bpstat_copy (ecs->stepping_through_solib_catchpoints);
2372       bpstat_clear (&ecs->stepping_through_solib_catchpoints);
2373       stop_print_frame = 1;
2374       stop_stepping (ecs);
2375       return;
2376     }
2377
2378   if (step_resume_breakpoint)
2379     {
2380       if (debug_infrun)
2381          fprintf_unfiltered (gdb_stdlog,
2382                              "infrun: step-resume breakpoint is inserted\n");
2383
2384       /* Having a step-resume breakpoint overrides anything
2385          else having to do with stepping commands until
2386          that breakpoint is reached.  */
2387       keep_going (ecs);
2388       return;
2389     }
2390
2391   if (step_range_end == 0)
2392     {
2393       if (debug_infrun)
2394          fprintf_unfiltered (gdb_stdlog, "infrun: no stepping, continue\n");
2395       /* Likewise if we aren't even stepping.  */
2396       keep_going (ecs);
2397       return;
2398     }
2399
2400   /* If stepping through a line, keep going if still within it.
2401
2402      Note that step_range_end is the address of the first instruction
2403      beyond the step range, and NOT the address of the last instruction
2404      within it! */
2405   if (stop_pc >= step_range_start && stop_pc < step_range_end)
2406     {
2407       if (debug_infrun)
2408          fprintf_unfiltered (gdb_stdlog, "infrun: stepping inside range [0x%s-0x%s]\n",
2409                             paddr_nz (step_range_start),
2410                             paddr_nz (step_range_end));
2411       keep_going (ecs);
2412       return;
2413     }
2414
2415   /* We stepped out of the stepping range.  */
2416
2417   /* If we are stepping at the source level and entered the runtime
2418      loader dynamic symbol resolution code, we keep on single stepping
2419      until we exit the run time loader code and reach the callee's
2420      address.  */
2421   if (step_over_calls == STEP_OVER_UNDEBUGGABLE
2422 #ifdef IN_SOLIB_DYNSYM_RESOLVE_CODE
2423       && IN_SOLIB_DYNSYM_RESOLVE_CODE (stop_pc)
2424 #else
2425       && in_solib_dynsym_resolve_code (stop_pc)
2426 #endif
2427       )
2428     {
2429       CORE_ADDR pc_after_resolver =
2430         gdbarch_skip_solib_resolver (current_gdbarch, stop_pc);
2431
2432       if (debug_infrun)
2433          fprintf_unfiltered (gdb_stdlog, "infrun: stepped into dynsym resolve code\n");
2434
2435       if (pc_after_resolver)
2436         {
2437           /* Set up a step-resume breakpoint at the address
2438              indicated by SKIP_SOLIB_RESOLVER.  */
2439           struct symtab_and_line sr_sal;
2440           init_sal (&sr_sal);
2441           sr_sal.pc = pc_after_resolver;
2442
2443           insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
2444         }
2445
2446       keep_going (ecs);
2447       return;
2448     }
2449
2450   if (step_range_end != 1
2451       && (step_over_calls == STEP_OVER_UNDEBUGGABLE
2452           || step_over_calls == STEP_OVER_ALL)
2453       && get_frame_type (get_current_frame ()) == SIGTRAMP_FRAME)
2454     {
2455       if (debug_infrun)
2456          fprintf_unfiltered (gdb_stdlog, "infrun: stepped into signal trampoline\n");
2457       /* The inferior, while doing a "step" or "next", has ended up in
2458          a signal trampoline (either by a signal being delivered or by
2459          the signal handler returning).  Just single-step until the
2460          inferior leaves the trampoline (either by calling the handler
2461          or returning).  */
2462       keep_going (ecs);
2463       return;
2464     }
2465
2466   /* Check for subroutine calls.  The check for the current frame
2467      equalling the step ID is not necessary - the check of the
2468      previous frame's ID is sufficient - but it is a common case and
2469      cheaper than checking the previous frame's ID.
2470
2471      NOTE: frame_id_eq will never report two invalid frame IDs as
2472      being equal, so to get into this block, both the current and
2473      previous frame must have valid frame IDs.  */
2474   if (!frame_id_eq (get_frame_id (get_current_frame ()), step_frame_id)
2475       && frame_id_eq (frame_unwind_id (get_current_frame ()), step_frame_id))
2476     {
2477       CORE_ADDR real_stop_pc;
2478
2479       if (debug_infrun)
2480          fprintf_unfiltered (gdb_stdlog, "infrun: stepped into subroutine\n");
2481
2482       if ((step_over_calls == STEP_OVER_NONE)
2483           || ((step_range_end == 1)
2484               && in_prologue (prev_pc, ecs->stop_func_start)))
2485         {
2486           /* I presume that step_over_calls is only 0 when we're
2487              supposed to be stepping at the assembly language level
2488              ("stepi").  Just stop.  */
2489           /* Also, maybe we just did a "nexti" inside a prolog, so we
2490              thought it was a subroutine call but it was not.  Stop as
2491              well.  FENN */
2492           stop_step = 1;
2493           print_stop_reason (END_STEPPING_RANGE, 0);
2494           stop_stepping (ecs);
2495           return;
2496         }
2497
2498       if (step_over_calls == STEP_OVER_ALL)
2499         {
2500           /* We're doing a "next", set a breakpoint at callee's return
2501              address (the address at which the caller will
2502              resume).  */
2503           insert_step_resume_breakpoint_at_caller (get_current_frame ());
2504           keep_going (ecs);
2505           return;
2506         }
2507
2508       /* If we are in a function call trampoline (a stub between the
2509          calling routine and the real function), locate the real
2510          function.  That's what tells us (a) whether we want to step
2511          into it at all, and (b) what prologue we want to run to the
2512          end of, if we do step into it.  */
2513       real_stop_pc = skip_language_trampoline (get_current_frame (), stop_pc);
2514       if (real_stop_pc == 0)
2515         real_stop_pc = gdbarch_skip_trampoline_code
2516                          (current_gdbarch, get_current_frame (), stop_pc);
2517       if (real_stop_pc != 0)
2518         ecs->stop_func_start = real_stop_pc;
2519
2520       if (
2521 #ifdef IN_SOLIB_DYNSYM_RESOLVE_CODE
2522           IN_SOLIB_DYNSYM_RESOLVE_CODE (ecs->stop_func_start)
2523 #else
2524           in_solib_dynsym_resolve_code (ecs->stop_func_start)
2525 #endif
2526 )
2527         {
2528           struct symtab_and_line sr_sal;
2529           init_sal (&sr_sal);
2530           sr_sal.pc = ecs->stop_func_start;
2531
2532           insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
2533           keep_going (ecs);
2534           return;
2535         }
2536
2537       /* If we have line number information for the function we are
2538          thinking of stepping into, step into it.
2539
2540          If there are several symtabs at that PC (e.g. with include
2541          files), just want to know whether *any* of them have line
2542          numbers.  find_pc_line handles this.  */
2543       {
2544         struct symtab_and_line tmp_sal;
2545
2546         tmp_sal = find_pc_line (ecs->stop_func_start, 0);
2547         if (tmp_sal.line != 0)
2548           {
2549             step_into_function (ecs);
2550             return;
2551           }
2552       }
2553
2554       /* If we have no line number and the step-stop-if-no-debug is
2555          set, we stop the step so that the user has a chance to switch
2556          in assembly mode.  */
2557       if (step_over_calls == STEP_OVER_UNDEBUGGABLE && step_stop_if_no_debug)
2558         {
2559           stop_step = 1;
2560           print_stop_reason (END_STEPPING_RANGE, 0);
2561           stop_stepping (ecs);
2562           return;
2563         }
2564
2565       /* Set a breakpoint at callee's return address (the address at
2566          which the caller will resume).  */
2567       insert_step_resume_breakpoint_at_caller (get_current_frame ());
2568       keep_going (ecs);
2569       return;
2570     }
2571
2572   /* If we're in the return path from a shared library trampoline,
2573      we want to proceed through the trampoline when stepping.  */
2574   if (gdbarch_in_solib_return_trampoline (current_gdbarch,
2575                                           stop_pc, ecs->stop_func_name))
2576     {
2577       /* Determine where this trampoline returns.  */
2578       CORE_ADDR real_stop_pc;
2579       real_stop_pc = gdbarch_skip_trampoline_code
2580                        (current_gdbarch, get_current_frame (), stop_pc);
2581
2582       if (debug_infrun)
2583          fprintf_unfiltered (gdb_stdlog, "infrun: stepped into solib return tramp\n");
2584
2585       /* Only proceed through if we know where it's going.  */
2586       if (real_stop_pc)
2587         {
2588           /* And put the step-breakpoint there and go until there. */
2589           struct symtab_and_line sr_sal;
2590
2591           init_sal (&sr_sal);   /* initialize to zeroes */
2592           sr_sal.pc = real_stop_pc;
2593           sr_sal.section = find_pc_overlay (sr_sal.pc);
2594
2595           /* Do not specify what the fp should be when we stop since
2596              on some machines the prologue is where the new fp value
2597              is established.  */
2598           insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
2599
2600           /* Restart without fiddling with the step ranges or
2601              other state.  */
2602           keep_going (ecs);
2603           return;
2604         }
2605     }
2606
2607   ecs->sal = find_pc_line (stop_pc, 0);
2608
2609   /* NOTE: tausq/2004-05-24: This if block used to be done before all
2610      the trampoline processing logic, however, there are some trampolines 
2611      that have no names, so we should do trampoline handling first.  */
2612   if (step_over_calls == STEP_OVER_UNDEBUGGABLE
2613       && ecs->stop_func_name == NULL
2614       && ecs->sal.line == 0)
2615     {
2616       if (debug_infrun)
2617          fprintf_unfiltered (gdb_stdlog, "infrun: stepped into undebuggable function\n");
2618
2619       /* The inferior just stepped into, or returned to, an
2620          undebuggable function (where there is no debugging information
2621          and no line number corresponding to the address where the
2622          inferior stopped).  Since we want to skip this kind of code,
2623          we keep going until the inferior returns from this
2624          function - unless the user has asked us not to (via
2625          set step-mode) or we no longer know how to get back
2626          to the call site.  */
2627       if (step_stop_if_no_debug
2628           || !frame_id_p (frame_unwind_id (get_current_frame ())))
2629         {
2630           /* If we have no line number and the step-stop-if-no-debug
2631              is set, we stop the step so that the user has a chance to
2632              switch in assembly mode.  */
2633           stop_step = 1;
2634           print_stop_reason (END_STEPPING_RANGE, 0);
2635           stop_stepping (ecs);
2636           return;
2637         }
2638       else
2639         {
2640           /* Set a breakpoint at callee's return address (the address
2641              at which the caller will resume).  */
2642           insert_step_resume_breakpoint_at_caller (get_current_frame ());
2643           keep_going (ecs);
2644           return;
2645         }
2646     }
2647
2648   if (step_range_end == 1)
2649     {
2650       /* It is stepi or nexti.  We always want to stop stepping after
2651          one instruction.  */
2652       if (debug_infrun)
2653          fprintf_unfiltered (gdb_stdlog, "infrun: stepi/nexti\n");
2654       stop_step = 1;
2655       print_stop_reason (END_STEPPING_RANGE, 0);
2656       stop_stepping (ecs);
2657       return;
2658     }
2659
2660   if (ecs->sal.line == 0)
2661     {
2662       /* We have no line number information.  That means to stop
2663          stepping (does this always happen right after one instruction,
2664          when we do "s" in a function with no line numbers,
2665          or can this happen as a result of a return or longjmp?).  */
2666       if (debug_infrun)
2667          fprintf_unfiltered (gdb_stdlog, "infrun: no line number info\n");
2668       stop_step = 1;
2669       print_stop_reason (END_STEPPING_RANGE, 0);
2670       stop_stepping (ecs);
2671       return;
2672     }
2673
2674   if ((stop_pc == ecs->sal.pc)
2675       && (ecs->current_line != ecs->sal.line
2676           || ecs->current_symtab != ecs->sal.symtab))
2677     {
2678       /* We are at the start of a different line.  So stop.  Note that
2679          we don't stop if we step into the middle of a different line.
2680          That is said to make things like for (;;) statements work
2681          better.  */
2682       if (debug_infrun)
2683          fprintf_unfiltered (gdb_stdlog, "infrun: stepped to a different line\n");
2684       stop_step = 1;
2685       print_stop_reason (END_STEPPING_RANGE, 0);
2686       stop_stepping (ecs);
2687       return;
2688     }
2689
2690   /* We aren't done stepping.
2691
2692      Optimize by setting the stepping range to the line.
2693      (We might not be in the original line, but if we entered a
2694      new line in mid-statement, we continue stepping.  This makes
2695      things like for(;;) statements work better.)  */
2696
2697   step_range_start = ecs->sal.pc;
2698   step_range_end = ecs->sal.end;
2699   step_frame_id = get_frame_id (get_current_frame ());
2700   ecs->current_line = ecs->sal.line;
2701   ecs->current_symtab = ecs->sal.symtab;
2702
2703   /* In the case where we just stepped out of a function into the
2704      middle of a line of the caller, continue stepping, but
2705      step_frame_id must be modified to current frame */
2706 #if 0
2707   /* NOTE: cagney/2003-10-16: I think this frame ID inner test is too
2708      generous.  It will trigger on things like a step into a frameless
2709      stackless leaf function.  I think the logic should instead look
2710      at the unwound frame ID has that should give a more robust
2711      indication of what happened.  */
2712   if (step - ID == current - ID)
2713     still stepping in same function;
2714   else if (step - ID == unwind (current - ID))
2715     stepped into a function;
2716   else
2717     stepped out of a function;
2718   /* Of course this assumes that the frame ID unwind code is robust
2719      and we're willing to introduce frame unwind logic into this
2720      function.  Fortunately, those days are nearly upon us.  */
2721 #endif
2722   {
2723     struct frame_info *frame = get_current_frame ();
2724     struct frame_id current_frame = get_frame_id (frame);
2725     if (!(frame_id_inner (get_frame_arch (frame), current_frame,
2726                           step_frame_id)))
2727       step_frame_id = current_frame;
2728   }
2729
2730   if (debug_infrun)
2731      fprintf_unfiltered (gdb_stdlog, "infrun: keep going\n");
2732   keep_going (ecs);
2733 }
2734
2735 /* Are we in the middle of stepping?  */
2736
2737 static int
2738 currently_stepping (struct execution_control_state *ecs)
2739 {
2740   return ((!ecs->handling_longjmp
2741            && ((step_range_end && step_resume_breakpoint == NULL)
2742                || stepping_over_breakpoint))
2743           || ecs->stepping_through_solib_after_catch
2744           || bpstat_should_step ());
2745 }
2746
2747 /* Subroutine call with source code we should not step over.  Do step
2748    to the first line of code in it.  */
2749
2750 static void
2751 step_into_function (struct execution_control_state *ecs)
2752 {
2753   struct symtab *s;
2754   struct symtab_and_line sr_sal;
2755
2756   s = find_pc_symtab (stop_pc);
2757   if (s && s->language != language_asm)
2758     ecs->stop_func_start = gdbarch_skip_prologue
2759                              (current_gdbarch, ecs->stop_func_start);
2760
2761   ecs->sal = find_pc_line (ecs->stop_func_start, 0);
2762   /* Use the step_resume_break to step until the end of the prologue,
2763      even if that involves jumps (as it seems to on the vax under
2764      4.2).  */
2765   /* If the prologue ends in the middle of a source line, continue to
2766      the end of that source line (if it is still within the function).
2767      Otherwise, just go to end of prologue.  */
2768   if (ecs->sal.end
2769       && ecs->sal.pc != ecs->stop_func_start
2770       && ecs->sal.end < ecs->stop_func_end)
2771     ecs->stop_func_start = ecs->sal.end;
2772
2773   /* Architectures which require breakpoint adjustment might not be able
2774      to place a breakpoint at the computed address.  If so, the test
2775      ``ecs->stop_func_start == stop_pc'' will never succeed.  Adjust
2776      ecs->stop_func_start to an address at which a breakpoint may be
2777      legitimately placed.
2778
2779      Note:  kevinb/2004-01-19:  On FR-V, if this adjustment is not
2780      made, GDB will enter an infinite loop when stepping through
2781      optimized code consisting of VLIW instructions which contain
2782      subinstructions corresponding to different source lines.  On
2783      FR-V, it's not permitted to place a breakpoint on any but the
2784      first subinstruction of a VLIW instruction.  When a breakpoint is
2785      set, GDB will adjust the breakpoint address to the beginning of
2786      the VLIW instruction.  Thus, we need to make the corresponding
2787      adjustment here when computing the stop address.  */
2788
2789   if (gdbarch_adjust_breakpoint_address_p (current_gdbarch))
2790     {
2791       ecs->stop_func_start
2792         = gdbarch_adjust_breakpoint_address (current_gdbarch,
2793                                              ecs->stop_func_start);
2794     }
2795
2796   if (ecs->stop_func_start == stop_pc)
2797     {
2798       /* We are already there: stop now.  */
2799       stop_step = 1;
2800       print_stop_reason (END_STEPPING_RANGE, 0);
2801       stop_stepping (ecs);
2802       return;
2803     }
2804   else
2805     {
2806       /* Put the step-breakpoint there and go until there.  */
2807       init_sal (&sr_sal);       /* initialize to zeroes */
2808       sr_sal.pc = ecs->stop_func_start;
2809       sr_sal.section = find_pc_overlay (ecs->stop_func_start);
2810
2811       /* Do not specify what the fp should be when we stop since on
2812          some machines the prologue is where the new fp value is
2813          established.  */
2814       insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
2815
2816       /* And make sure stepping stops right away then.  */
2817       step_range_end = step_range_start;
2818     }
2819   keep_going (ecs);
2820 }
2821
2822 /* Insert a "step-resume breakpoint" at SR_SAL with frame ID SR_ID.
2823    This is used to both functions and to skip over code.  */
2824
2825 static void
2826 insert_step_resume_breakpoint_at_sal (struct symtab_and_line sr_sal,
2827                                       struct frame_id sr_id)
2828 {
2829   /* There should never be more than one step-resume breakpoint per
2830      thread, so we should never be setting a new
2831      step_resume_breakpoint when one is already active.  */
2832   gdb_assert (step_resume_breakpoint == NULL);
2833
2834   if (debug_infrun)
2835     fprintf_unfiltered (gdb_stdlog,
2836                         "infrun: inserting step-resume breakpoint at 0x%s\n",
2837                         paddr_nz (sr_sal.pc));
2838
2839   step_resume_breakpoint = set_momentary_breakpoint (sr_sal, sr_id,
2840                                                      bp_step_resume);
2841 }
2842
2843 /* Insert a "step-resume breakpoint" at RETURN_FRAME.pc.  This is used
2844    to skip a potential signal handler.
2845
2846    This is called with the interrupted function's frame.  The signal
2847    handler, when it returns, will resume the interrupted function at
2848    RETURN_FRAME.pc.  */
2849
2850 static void
2851 insert_step_resume_breakpoint_at_frame (struct frame_info *return_frame)
2852 {
2853   struct symtab_and_line sr_sal;
2854
2855   gdb_assert (return_frame != NULL);
2856   init_sal (&sr_sal);           /* initialize to zeros */
2857
2858   sr_sal.pc = gdbarch_addr_bits_remove
2859                 (current_gdbarch, get_frame_pc (return_frame));
2860   sr_sal.section = find_pc_overlay (sr_sal.pc);
2861
2862   insert_step_resume_breakpoint_at_sal (sr_sal, get_frame_id (return_frame));
2863 }
2864
2865 /* Similar to insert_step_resume_breakpoint_at_frame, except
2866    but a breakpoint at the previous frame's PC.  This is used to
2867    skip a function after stepping into it (for "next" or if the called
2868    function has no debugging information).
2869
2870    The current function has almost always been reached by single
2871    stepping a call or return instruction.  NEXT_FRAME belongs to the
2872    current function, and the breakpoint will be set at the caller's
2873    resume address.
2874
2875    This is a separate function rather than reusing
2876    insert_step_resume_breakpoint_at_frame in order to avoid
2877    get_prev_frame, which may stop prematurely (see the implementation
2878    of frame_unwind_id for an example).  */
2879
2880 static void
2881 insert_step_resume_breakpoint_at_caller (struct frame_info *next_frame)
2882 {
2883   struct symtab_and_line sr_sal;
2884
2885   /* We shouldn't have gotten here if we don't know where the call site
2886      is.  */
2887   gdb_assert (frame_id_p (frame_unwind_id (next_frame)));
2888
2889   init_sal (&sr_sal);           /* initialize to zeros */
2890
2891   sr_sal.pc = gdbarch_addr_bits_remove
2892                 (current_gdbarch, frame_pc_unwind (next_frame));
2893   sr_sal.section = find_pc_overlay (sr_sal.pc);
2894
2895   insert_step_resume_breakpoint_at_sal (sr_sal, frame_unwind_id (next_frame));
2896 }
2897
2898 static void
2899 stop_stepping (struct execution_control_state *ecs)
2900 {
2901   if (debug_infrun)
2902     fprintf_unfiltered (gdb_stdlog, "infrun: stop_stepping\n");
2903
2904   /* Let callers know we don't want to wait for the inferior anymore.  */
2905   ecs->wait_some_more = 0;
2906 }
2907
2908 /* This function handles various cases where we need to continue
2909    waiting for the inferior.  */
2910 /* (Used to be the keep_going: label in the old wait_for_inferior) */
2911
2912 static void
2913 keep_going (struct execution_control_state *ecs)
2914 {
2915   /* Save the pc before execution, to compare with pc after stop.  */
2916   prev_pc = read_pc ();         /* Might have been DECR_AFTER_BREAK */
2917
2918   /* If we did not do break;, it means we should keep running the
2919      inferior and not return to debugger.  */
2920
2921   if (stepping_over_breakpoint && stop_signal != TARGET_SIGNAL_TRAP)
2922     {
2923       /* We took a signal (which we are supposed to pass through to
2924          the inferior, else we'd have done a break above) and we
2925          haven't yet gotten our trap.  Simply continue.  */
2926       resume (currently_stepping (ecs), stop_signal);
2927     }
2928   else
2929     {
2930       /* Either the trap was not expected, but we are continuing
2931          anyway (the user asked that this signal be passed to the
2932          child)
2933          -- or --
2934          The signal was SIGTRAP, e.g. it was our signal, but we
2935          decided we should resume from it.
2936
2937          We're going to run this baby now!  
2938
2939          Note that insert_breakpoints won't try to re-insert
2940          already inserted breakpoints.  Therefore, we don't
2941          care if breakpoints were already inserted, or not.  */
2942       
2943       if (ecs->stepping_over_breakpoint)
2944         {
2945           remove_breakpoints ();
2946         }
2947       else
2948         {
2949           struct gdb_exception e;
2950           /* Stop stepping when inserting breakpoints
2951              has failed.  */
2952           TRY_CATCH (e, RETURN_MASK_ERROR)
2953             {
2954               insert_breakpoints ();
2955             }
2956           if (e.reason < 0)
2957             {
2958               stop_stepping (ecs);
2959               return;
2960             }
2961         }
2962
2963       stepping_over_breakpoint = ecs->stepping_over_breakpoint;
2964
2965       /* Do not deliver SIGNAL_TRAP (except when the user explicitly
2966          specifies that such a signal should be delivered to the
2967          target program).
2968
2969          Typically, this would occure when a user is debugging a
2970          target monitor on a simulator: the target monitor sets a
2971          breakpoint; the simulator encounters this break-point and
2972          halts the simulation handing control to GDB; GDB, noteing
2973          that the break-point isn't valid, returns control back to the
2974          simulator; the simulator then delivers the hardware
2975          equivalent of a SIGNAL_TRAP to the program being debugged. */
2976
2977       if (stop_signal == TARGET_SIGNAL_TRAP && !signal_program[stop_signal])
2978         stop_signal = TARGET_SIGNAL_0;
2979
2980
2981       resume (currently_stepping (ecs), stop_signal);
2982     }
2983
2984   prepare_to_wait (ecs);
2985 }
2986
2987 /* This function normally comes after a resume, before
2988    handle_inferior_event exits.  It takes care of any last bits of
2989    housekeeping, and sets the all-important wait_some_more flag.  */
2990
2991 static void
2992 prepare_to_wait (struct execution_control_state *ecs)
2993 {
2994   if (debug_infrun)
2995     fprintf_unfiltered (gdb_stdlog, "infrun: prepare_to_wait\n");
2996   if (ecs->infwait_state == infwait_normal_state)
2997     {
2998       overlay_cache_invalid = 1;
2999
3000       /* We have to invalidate the registers BEFORE calling
3001          target_wait because they can be loaded from the target while
3002          in target_wait.  This makes remote debugging a bit more
3003          efficient for those targets that provide critical registers
3004          as part of their normal status mechanism. */
3005
3006       registers_changed ();
3007       ecs->waiton_ptid = pid_to_ptid (-1);
3008       ecs->wp = &(ecs->ws);
3009     }
3010   /* This is the old end of the while loop.  Let everybody know we
3011      want to wait for the inferior some more and get called again
3012      soon.  */
3013   ecs->wait_some_more = 1;
3014 }
3015
3016 /* Print why the inferior has stopped. We always print something when
3017    the inferior exits, or receives a signal. The rest of the cases are
3018    dealt with later on in normal_stop() and print_it_typical().  Ideally
3019    there should be a call to this function from handle_inferior_event()
3020    each time stop_stepping() is called.*/
3021 static void
3022 print_stop_reason (enum inferior_stop_reason stop_reason, int stop_info)
3023 {
3024   switch (stop_reason)
3025     {
3026     case END_STEPPING_RANGE:
3027       /* We are done with a step/next/si/ni command. */
3028       /* For now print nothing. */
3029       /* Print a message only if not in the middle of doing a "step n"
3030          operation for n > 1 */
3031       if (!step_multi || !stop_step)
3032         if (ui_out_is_mi_like_p (uiout))
3033           ui_out_field_string
3034             (uiout, "reason",
3035              async_reason_lookup (EXEC_ASYNC_END_STEPPING_RANGE));
3036       break;
3037     case SIGNAL_EXITED:
3038       /* The inferior was terminated by a signal. */
3039       annotate_signalled ();
3040       if (ui_out_is_mi_like_p (uiout))
3041         ui_out_field_string
3042           (uiout, "reason",
3043            async_reason_lookup (EXEC_ASYNC_EXITED_SIGNALLED));
3044       ui_out_text (uiout, "\nProgram terminated with signal ");
3045       annotate_signal_name ();
3046       ui_out_field_string (uiout, "signal-name",
3047                            target_signal_to_name (stop_info));
3048       annotate_signal_name_end ();
3049       ui_out_text (uiout, ", ");
3050       annotate_signal_string ();
3051       ui_out_field_string (uiout, "signal-meaning",
3052                            target_signal_to_string (stop_info));
3053       annotate_signal_string_end ();
3054       ui_out_text (uiout, ".\n");
3055       ui_out_text (uiout, "The program no longer exists.\n");
3056       break;
3057     case EXITED:
3058       /* The inferior program is finished. */
3059       annotate_exited (stop_info);
3060       if (stop_info)
3061         {
3062           if (ui_out_is_mi_like_p (uiout))
3063             ui_out_field_string (uiout, "reason", 
3064                                  async_reason_lookup (EXEC_ASYNC_EXITED));
3065           ui_out_text (uiout, "\nProgram exited with code ");
3066           ui_out_field_fmt (uiout, "exit-code", "0%o",
3067                             (unsigned int) stop_info);
3068           ui_out_text (uiout, ".\n");
3069         }
3070       else
3071         {
3072           if (ui_out_is_mi_like_p (uiout))
3073             ui_out_field_string
3074               (uiout, "reason",
3075                async_reason_lookup (EXEC_ASYNC_EXITED_NORMALLY));
3076           ui_out_text (uiout, "\nProgram exited normally.\n");
3077         }
3078       /* Support the --return-child-result option.  */
3079       return_child_result_value = stop_info;
3080       break;
3081     case SIGNAL_RECEIVED:
3082       /* Signal received. The signal table tells us to print about
3083          it. */
3084       annotate_signal ();
3085       ui_out_text (uiout, "\nProgram received signal ");
3086       annotate_signal_name ();
3087       if (ui_out_is_mi_like_p (uiout))
3088         ui_out_field_string
3089           (uiout, "reason", async_reason_lookup (EXEC_ASYNC_SIGNAL_RECEIVED));
3090       ui_out_field_string (uiout, "signal-name",
3091                            target_signal_to_name (stop_info));
3092       annotate_signal_name_end ();
3093       ui_out_text (uiout, ", ");
3094       annotate_signal_string ();
3095       ui_out_field_string (uiout, "signal-meaning",
3096                            target_signal_to_string (stop_info));
3097       annotate_signal_string_end ();
3098       ui_out_text (uiout, ".\n");
3099       break;
3100     default:
3101       internal_error (__FILE__, __LINE__,
3102                       _("print_stop_reason: unrecognized enum value"));
3103       break;
3104     }
3105 }
3106 \f
3107
3108 /* Here to return control to GDB when the inferior stops for real.
3109    Print appropriate messages, remove breakpoints, give terminal our modes.
3110
3111    STOP_PRINT_FRAME nonzero means print the executing frame
3112    (pc, function, args, file, line number and line text).
3113    BREAKPOINTS_FAILED nonzero means stop was due to error
3114    attempting to insert breakpoints.  */
3115
3116 void
3117 normal_stop (void)
3118 {
3119   struct target_waitstatus last;
3120   ptid_t last_ptid;
3121
3122   get_last_target_status (&last_ptid, &last);
3123
3124   /* As with the notification of thread events, we want to delay
3125      notifying the user that we've switched thread context until
3126      the inferior actually stops.
3127
3128      There's no point in saying anything if the inferior has exited.
3129      Note that SIGNALLED here means "exited with a signal", not
3130      "received a signal".  */
3131   if (!ptid_equal (previous_inferior_ptid, inferior_ptid)
3132       && target_has_execution
3133       && last.kind != TARGET_WAITKIND_SIGNALLED
3134       && last.kind != TARGET_WAITKIND_EXITED)
3135     {
3136       target_terminal_ours_for_output ();
3137       printf_filtered (_("[Switching to %s]\n"),
3138                        target_pid_to_str (inferior_ptid));
3139       previous_inferior_ptid = inferior_ptid;
3140     }
3141
3142   /* NOTE drow/2004-01-17: Is this still necessary?  */
3143   /* Make sure that the current_frame's pc is correct.  This
3144      is a correction for setting up the frame info before doing
3145      gdbarch_decr_pc_after_break */
3146   if (target_has_execution)
3147     /* FIXME: cagney/2002-12-06: Has the PC changed?  Thanks to
3148        gdbarch_decr_pc_after_break, the program counter can change.  Ask the
3149        frame code to check for this and sort out any resultant mess.
3150        gdbarch_decr_pc_after_break needs to just go away.  */
3151     deprecated_update_frame_pc_hack (get_current_frame (), read_pc ());
3152
3153   if (target_has_execution)
3154     {
3155       if (remove_breakpoints ())
3156         {
3157           target_terminal_ours_for_output ();
3158           printf_filtered (_("\
3159 Cannot remove breakpoints because program is no longer writable.\n\
3160 It might be running in another process.\n\
3161 Further execution is probably impossible.\n"));
3162         }
3163     }
3164
3165   /* Delete the breakpoint we stopped at, if it wants to be deleted.
3166      Delete any breakpoint that is to be deleted at the next stop.  */
3167
3168   breakpoint_auto_delete (stop_bpstat);
3169
3170   /* If an auto-display called a function and that got a signal,
3171      delete that auto-display to avoid an infinite recursion.  */
3172
3173   if (stopped_by_random_signal)
3174     disable_current_display ();
3175
3176   /* Don't print a message if in the middle of doing a "step n"
3177      operation for n > 1 */
3178   if (step_multi && stop_step)
3179     goto done;
3180
3181   target_terminal_ours ();
3182
3183   /* Set the current source location.  This will also happen if we
3184      display the frame below, but the current SAL will be incorrect
3185      during a user hook-stop function.  */
3186   if (target_has_stack && !stop_stack_dummy)
3187     set_current_sal_from_frame (get_current_frame (), 1);
3188
3189   /* Look up the hook_stop and run it (CLI internally handles problem
3190      of stop_command's pre-hook not existing).  */
3191   if (stop_command)
3192     catch_errors (hook_stop_stub, stop_command,
3193                   "Error while running hook_stop:\n", RETURN_MASK_ALL);
3194
3195   if (!target_has_stack)
3196     {
3197
3198       goto done;
3199     }
3200
3201   /* Select innermost stack frame - i.e., current frame is frame 0,
3202      and current location is based on that.
3203      Don't do this on return from a stack dummy routine,
3204      or if the program has exited. */
3205
3206   if (!stop_stack_dummy)
3207     {
3208       select_frame (get_current_frame ());
3209
3210       /* Print current location without a level number, if
3211          we have changed functions or hit a breakpoint.
3212          Print source line if we have one.
3213          bpstat_print() contains the logic deciding in detail
3214          what to print, based on the event(s) that just occurred. */
3215
3216       if (stop_print_frame)
3217         {
3218           int bpstat_ret;
3219           int source_flag;
3220           int do_frame_printing = 1;
3221
3222           bpstat_ret = bpstat_print (stop_bpstat);
3223           switch (bpstat_ret)
3224             {
3225             case PRINT_UNKNOWN:
3226               /* If we had hit a shared library event breakpoint,
3227                  bpstat_print would print out this message.  If we hit
3228                  an OS-level shared library event, do the same
3229                  thing.  */
3230               if (last.kind == TARGET_WAITKIND_LOADED)
3231                 {
3232                   printf_filtered (_("Stopped due to shared library event\n"));
3233                   source_flag = SRC_LINE;       /* something bogus */
3234                   do_frame_printing = 0;
3235                   break;
3236                 }
3237
3238               /* FIXME: cagney/2002-12-01: Given that a frame ID does
3239                  (or should) carry around the function and does (or
3240                  should) use that when doing a frame comparison.  */
3241               if (stop_step
3242                   && frame_id_eq (step_frame_id,
3243                                   get_frame_id (get_current_frame ()))
3244                   && step_start_function == find_pc_function (stop_pc))
3245                 source_flag = SRC_LINE; /* finished step, just print source line */
3246               else
3247                 source_flag = SRC_AND_LOC;      /* print location and source line */
3248               break;
3249             case PRINT_SRC_AND_LOC:
3250               source_flag = SRC_AND_LOC;        /* print location and source line */
3251               break;
3252             case PRINT_SRC_ONLY:
3253               source_flag = SRC_LINE;
3254               break;
3255             case PRINT_NOTHING:
3256               source_flag = SRC_LINE;   /* something bogus */
3257               do_frame_printing = 0;
3258               break;
3259             default:
3260               internal_error (__FILE__, __LINE__, _("Unknown value."));
3261             }
3262
3263           if (ui_out_is_mi_like_p (uiout))
3264             ui_out_field_int (uiout, "thread-id",
3265                               pid_to_thread_id (inferior_ptid));
3266           /* The behavior of this routine with respect to the source
3267              flag is:
3268              SRC_LINE: Print only source line
3269              LOCATION: Print only location
3270              SRC_AND_LOC: Print location and source line */
3271           if (do_frame_printing)
3272             print_stack_frame (get_selected_frame (NULL), 0, source_flag);
3273
3274           /* Display the auto-display expressions.  */
3275           do_displays ();
3276         }
3277     }
3278
3279   /* Save the function value return registers, if we care.
3280      We might be about to restore their previous contents.  */
3281   if (proceed_to_finish)
3282     {
3283       /* This should not be necessary.  */
3284       if (stop_registers)
3285         regcache_xfree (stop_registers);
3286
3287       /* NB: The copy goes through to the target picking up the value of
3288          all the registers.  */
3289       stop_registers = regcache_dup (get_current_regcache ());
3290     }
3291
3292   if (stop_stack_dummy)
3293     {
3294       /* Pop the empty frame that contains the stack dummy.  POP_FRAME
3295          ends with a setting of the current frame, so we can use that
3296          next. */
3297       frame_pop (get_current_frame ());
3298       /* Set stop_pc to what it was before we called the function.
3299          Can't rely on restore_inferior_status because that only gets
3300          called if we don't stop in the called function.  */
3301       stop_pc = read_pc ();
3302       select_frame (get_current_frame ());
3303     }
3304
3305 done:
3306   annotate_stopped ();
3307   observer_notify_normal_stop (stop_bpstat);
3308 }
3309
3310 static int
3311 hook_stop_stub (void *cmd)
3312 {
3313   execute_cmd_pre_hook ((struct cmd_list_element *) cmd);
3314   return (0);
3315 }
3316 \f
3317 int
3318 signal_stop_state (int signo)
3319 {
3320   return signal_stop[signo];
3321 }
3322
3323 int
3324 signal_print_state (int signo)
3325 {
3326   return signal_print[signo];
3327 }
3328
3329 int
3330 signal_pass_state (int signo)
3331 {
3332   return signal_program[signo];
3333 }
3334
3335 int
3336 signal_stop_update (int signo, int state)
3337 {
3338   int ret = signal_stop[signo];
3339   signal_stop[signo] = state;
3340   return ret;
3341 }
3342
3343 int
3344 signal_print_update (int signo, int state)
3345 {
3346   int ret = signal_print[signo];
3347   signal_print[signo] = state;
3348   return ret;
3349 }
3350
3351 int
3352 signal_pass_update (int signo, int state)
3353 {
3354   int ret = signal_program[signo];
3355   signal_program[signo] = state;
3356   return ret;
3357 }
3358
3359 static void
3360 sig_print_header (void)
3361 {
3362   printf_filtered (_("\
3363 Signal        Stop\tPrint\tPass to program\tDescription\n"));
3364 }
3365
3366 static void
3367 sig_print_info (enum target_signal oursig)
3368 {
3369   char *name = target_signal_to_name (oursig);
3370   int name_padding = 13 - strlen (name);
3371
3372   if (name_padding <= 0)
3373     name_padding = 0;
3374
3375   printf_filtered ("%s", name);
3376   printf_filtered ("%*.*s ", name_padding, name_padding, "                 ");
3377   printf_filtered ("%s\t", signal_stop[oursig] ? "Yes" : "No");
3378   printf_filtered ("%s\t", signal_print[oursig] ? "Yes" : "No");
3379   printf_filtered ("%s\t\t", signal_program[oursig] ? "Yes" : "No");
3380   printf_filtered ("%s\n", target_signal_to_string (oursig));
3381 }
3382
3383 /* Specify how various signals in the inferior should be handled.  */
3384
3385 static void
3386 handle_command (char *args, int from_tty)
3387 {
3388   char **argv;
3389   int digits, wordlen;
3390   int sigfirst, signum, siglast;
3391   enum target_signal oursig;
3392   int allsigs;
3393   int nsigs;
3394   unsigned char *sigs;
3395   struct cleanup *old_chain;
3396
3397   if (args == NULL)
3398     {
3399       error_no_arg (_("signal to handle"));
3400     }
3401
3402   /* Allocate and zero an array of flags for which signals to handle. */
3403
3404   nsigs = (int) TARGET_SIGNAL_LAST;
3405   sigs = (unsigned char *) alloca (nsigs);
3406   memset (sigs, 0, nsigs);
3407
3408   /* Break the command line up into args. */
3409
3410   argv = buildargv (args);
3411   if (argv == NULL)
3412     {
3413       nomem (0);
3414     }
3415   old_chain = make_cleanup_freeargv (argv);
3416
3417   /* Walk through the args, looking for signal oursigs, signal names, and
3418      actions.  Signal numbers and signal names may be interspersed with
3419      actions, with the actions being performed for all signals cumulatively
3420      specified.  Signal ranges can be specified as <LOW>-<HIGH>. */
3421
3422   while (*argv != NULL)
3423     {
3424       wordlen = strlen (*argv);
3425       for (digits = 0; isdigit ((*argv)[digits]); digits++)
3426         {;
3427         }
3428       allsigs = 0;
3429       sigfirst = siglast = -1;
3430
3431       if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
3432         {
3433           /* Apply action to all signals except those used by the
3434              debugger.  Silently skip those. */
3435           allsigs = 1;
3436           sigfirst = 0;
3437           siglast = nsigs - 1;
3438         }
3439       else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
3440         {
3441           SET_SIGS (nsigs, sigs, signal_stop);
3442           SET_SIGS (nsigs, sigs, signal_print);
3443         }
3444       else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
3445         {
3446           UNSET_SIGS (nsigs, sigs, signal_program);
3447         }
3448       else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
3449         {
3450           SET_SIGS (nsigs, sigs, signal_print);
3451         }
3452       else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
3453         {
3454           SET_SIGS (nsigs, sigs, signal_program);
3455         }
3456       else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
3457         {
3458           UNSET_SIGS (nsigs, sigs, signal_stop);
3459         }
3460       else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
3461         {
3462           SET_SIGS (nsigs, sigs, signal_program);
3463         }
3464       else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
3465         {
3466           UNSET_SIGS (nsigs, sigs, signal_print);
3467           UNSET_SIGS (nsigs, sigs, signal_stop);
3468         }
3469       else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
3470         {
3471           UNSET_SIGS (nsigs, sigs, signal_program);
3472         }
3473       else if (digits > 0)
3474         {
3475           /* It is numeric.  The numeric signal refers to our own
3476              internal signal numbering from target.h, not to host/target
3477              signal  number.  This is a feature; users really should be
3478              using symbolic names anyway, and the common ones like
3479              SIGHUP, SIGINT, SIGALRM, etc. will work right anyway.  */
3480
3481           sigfirst = siglast = (int)
3482             target_signal_from_command (atoi (*argv));
3483           if ((*argv)[digits] == '-')
3484             {
3485               siglast = (int)
3486                 target_signal_from_command (atoi ((*argv) + digits + 1));
3487             }
3488           if (sigfirst > siglast)
3489             {
3490               /* Bet he didn't figure we'd think of this case... */
3491               signum = sigfirst;
3492               sigfirst = siglast;
3493               siglast = signum;
3494             }
3495         }
3496       else
3497         {
3498           oursig = target_signal_from_name (*argv);
3499           if (oursig != TARGET_SIGNAL_UNKNOWN)
3500             {
3501               sigfirst = siglast = (int) oursig;
3502             }
3503           else
3504             {
3505               /* Not a number and not a recognized flag word => complain.  */
3506               error (_("Unrecognized or ambiguous flag word: \"%s\"."), *argv);
3507             }
3508         }
3509
3510       /* If any signal numbers or symbol names were found, set flags for
3511          which signals to apply actions to. */
3512
3513       for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
3514         {
3515           switch ((enum target_signal) signum)
3516             {
3517             case TARGET_SIGNAL_TRAP:
3518             case TARGET_SIGNAL_INT:
3519               if (!allsigs && !sigs[signum])
3520                 {
3521                   if (query ("%s is used by the debugger.\n\
3522 Are you sure you want to change it? ", target_signal_to_name ((enum target_signal) signum)))
3523                     {
3524                       sigs[signum] = 1;
3525                     }
3526                   else
3527                     {
3528                       printf_unfiltered (_("Not confirmed, unchanged.\n"));
3529                       gdb_flush (gdb_stdout);
3530                     }
3531                 }
3532               break;
3533             case TARGET_SIGNAL_0:
3534             case TARGET_SIGNAL_DEFAULT:
3535             case TARGET_SIGNAL_UNKNOWN:
3536               /* Make sure that "all" doesn't print these.  */
3537               break;
3538             default:
3539               sigs[signum] = 1;
3540               break;
3541             }
3542         }
3543
3544       argv++;
3545     }
3546
3547   target_notice_signals (inferior_ptid);
3548
3549   if (from_tty)
3550     {
3551       /* Show the results.  */
3552       sig_print_header ();
3553       for (signum = 0; signum < nsigs; signum++)
3554         {
3555           if (sigs[signum])
3556             {
3557               sig_print_info (signum);
3558             }
3559         }
3560     }
3561
3562   do_cleanups (old_chain);
3563 }
3564
3565 static void
3566 xdb_handle_command (char *args, int from_tty)
3567 {
3568   char **argv;
3569   struct cleanup *old_chain;
3570
3571   /* Break the command line up into args. */
3572
3573   argv = buildargv (args);
3574   if (argv == NULL)
3575     {
3576       nomem (0);
3577     }
3578   old_chain = make_cleanup_freeargv (argv);
3579   if (argv[1] != (char *) NULL)
3580     {
3581       char *argBuf;
3582       int bufLen;
3583
3584       bufLen = strlen (argv[0]) + 20;
3585       argBuf = (char *) xmalloc (bufLen);
3586       if (argBuf)
3587         {
3588           int validFlag = 1;
3589           enum target_signal oursig;
3590
3591           oursig = target_signal_from_name (argv[0]);
3592           memset (argBuf, 0, bufLen);
3593           if (strcmp (argv[1], "Q") == 0)
3594             sprintf (argBuf, "%s %s", argv[0], "noprint");
3595           else
3596             {
3597               if (strcmp (argv[1], "s") == 0)
3598                 {
3599                   if (!signal_stop[oursig])
3600                     sprintf (argBuf, "%s %s", argv[0], "stop");
3601                   else
3602                     sprintf (argBuf, "%s %s", argv[0], "nostop");
3603                 }
3604               else if (strcmp (argv[1], "i") == 0)
3605                 {
3606                   if (!signal_program[oursig])
3607                     sprintf (argBuf, "%s %s", argv[0], "pass");
3608                   else
3609                     sprintf (argBuf, "%s %s", argv[0], "nopass");
3610                 }
3611               else if (strcmp (argv[1], "r") == 0)
3612                 {
3613                   if (!signal_print[oursig])
3614                     sprintf (argBuf, "%s %s", argv[0], "print");
3615                   else
3616                     sprintf (argBuf, "%s %s", argv[0], "noprint");
3617                 }
3618               else
3619                 validFlag = 0;
3620             }
3621           if (validFlag)
3622             handle_command (argBuf, from_tty);
3623           else
3624             printf_filtered (_("Invalid signal handling flag.\n"));
3625           if (argBuf)
3626             xfree (argBuf);
3627         }
3628     }
3629   do_cleanups (old_chain);
3630 }
3631
3632 /* Print current contents of the tables set by the handle command.
3633    It is possible we should just be printing signals actually used
3634    by the current target (but for things to work right when switching
3635    targets, all signals should be in the signal tables).  */
3636
3637 static void
3638 signals_info (char *signum_exp, int from_tty)
3639 {
3640   enum target_signal oursig;
3641   sig_print_header ();
3642
3643   if (signum_exp)
3644     {
3645       /* First see if this is a symbol name.  */
3646       oursig = target_signal_from_name (signum_exp);
3647       if (oursig == TARGET_SIGNAL_UNKNOWN)
3648         {
3649           /* No, try numeric.  */
3650           oursig =
3651             target_signal_from_command (parse_and_eval_long (signum_exp));
3652         }
3653       sig_print_info (oursig);
3654       return;
3655     }
3656
3657   printf_filtered ("\n");
3658   /* These ugly casts brought to you by the native VAX compiler.  */
3659   for (oursig = TARGET_SIGNAL_FIRST;
3660        (int) oursig < (int) TARGET_SIGNAL_LAST;
3661        oursig = (enum target_signal) ((int) oursig + 1))
3662     {
3663       QUIT;
3664
3665       if (oursig != TARGET_SIGNAL_UNKNOWN
3666           && oursig != TARGET_SIGNAL_DEFAULT && oursig != TARGET_SIGNAL_0)
3667         sig_print_info (oursig);
3668     }
3669
3670   printf_filtered (_("\nUse the \"handle\" command to change these tables.\n"));
3671 }
3672 \f
3673 struct inferior_status
3674 {
3675   enum target_signal stop_signal;
3676   CORE_ADDR stop_pc;
3677   bpstat stop_bpstat;
3678   int stop_step;
3679   int stop_stack_dummy;
3680   int stopped_by_random_signal;
3681   int stepping_over_breakpoint;
3682   CORE_ADDR step_range_start;
3683   CORE_ADDR step_range_end;
3684   struct frame_id step_frame_id;
3685   enum step_over_calls_kind step_over_calls;
3686   CORE_ADDR step_resume_break_address;
3687   int stop_after_trap;
3688   int stop_soon;
3689
3690   /* These are here because if call_function_by_hand has written some
3691      registers and then decides to call error(), we better not have changed
3692      any registers.  */
3693   struct regcache *registers;
3694
3695   /* A frame unique identifier.  */
3696   struct frame_id selected_frame_id;
3697
3698   int breakpoint_proceeded;
3699   int restore_stack_info;
3700   int proceed_to_finish;
3701 };
3702
3703 void
3704 write_inferior_status_register (struct inferior_status *inf_status, int regno,
3705                                 LONGEST val)
3706 {
3707   int size = register_size (current_gdbarch, regno);
3708   void *buf = alloca (size);
3709   store_signed_integer (buf, size, val);
3710   regcache_raw_write (inf_status->registers, regno, buf);
3711 }
3712
3713 /* Save all of the information associated with the inferior<==>gdb
3714    connection.  INF_STATUS is a pointer to a "struct inferior_status"
3715    (defined in inferior.h).  */
3716
3717 struct inferior_status *
3718 save_inferior_status (int restore_stack_info)
3719 {
3720   struct inferior_status *inf_status = XMALLOC (struct inferior_status);
3721
3722   inf_status->stop_signal = stop_signal;
3723   inf_status->stop_pc = stop_pc;
3724   inf_status->stop_step = stop_step;
3725   inf_status->stop_stack_dummy = stop_stack_dummy;
3726   inf_status->stopped_by_random_signal = stopped_by_random_signal;
3727   inf_status->stepping_over_breakpoint = stepping_over_breakpoint;
3728   inf_status->step_range_start = step_range_start;
3729   inf_status->step_range_end = step_range_end;
3730   inf_status->step_frame_id = step_frame_id;
3731   inf_status->step_over_calls = step_over_calls;
3732   inf_status->stop_after_trap = stop_after_trap;
3733   inf_status->stop_soon = stop_soon;
3734   /* Save original bpstat chain here; replace it with copy of chain.
3735      If caller's caller is walking the chain, they'll be happier if we
3736      hand them back the original chain when restore_inferior_status is
3737      called.  */
3738   inf_status->stop_bpstat = stop_bpstat;
3739   stop_bpstat = bpstat_copy (stop_bpstat);
3740   inf_status->breakpoint_proceeded = breakpoint_proceeded;
3741   inf_status->restore_stack_info = restore_stack_info;
3742   inf_status->proceed_to_finish = proceed_to_finish;
3743
3744   inf_status->registers = regcache_dup (get_current_regcache ());
3745
3746   inf_status->selected_frame_id = get_frame_id (get_selected_frame (NULL));
3747   return inf_status;
3748 }
3749
3750 static int
3751 restore_selected_frame (void *args)
3752 {
3753   struct frame_id *fid = (struct frame_id *) args;
3754   struct frame_info *frame;
3755
3756   frame = frame_find_by_id (*fid);
3757
3758   /* If inf_status->selected_frame_id is NULL, there was no previously
3759      selected frame.  */
3760   if (frame == NULL)
3761     {
3762       warning (_("Unable to restore previously selected frame."));
3763       return 0;
3764     }
3765
3766   select_frame (frame);
3767
3768   return (1);
3769 }
3770
3771 void
3772 restore_inferior_status (struct inferior_status *inf_status)
3773 {
3774   stop_signal = inf_status->stop_signal;
3775   stop_pc = inf_status->stop_pc;
3776   stop_step = inf_status->stop_step;
3777   stop_stack_dummy = inf_status->stop_stack_dummy;
3778   stopped_by_random_signal = inf_status->stopped_by_random_signal;
3779   stepping_over_breakpoint = inf_status->stepping_over_breakpoint;
3780   step_range_start = inf_status->step_range_start;
3781   step_range_end = inf_status->step_range_end;
3782   step_frame_id = inf_status->step_frame_id;
3783   step_over_calls = inf_status->step_over_calls;
3784   stop_after_trap = inf_status->stop_after_trap;
3785   stop_soon = inf_status->stop_soon;
3786   bpstat_clear (&stop_bpstat);
3787   stop_bpstat = inf_status->stop_bpstat;
3788   breakpoint_proceeded = inf_status->breakpoint_proceeded;
3789   proceed_to_finish = inf_status->proceed_to_finish;
3790
3791   /* The inferior can be gone if the user types "print exit(0)"
3792      (and perhaps other times).  */
3793   if (target_has_execution)
3794     /* NB: The register write goes through to the target.  */
3795     regcache_cpy (get_current_regcache (), inf_status->registers);
3796   regcache_xfree (inf_status->registers);
3797
3798   /* FIXME: If we are being called after stopping in a function which
3799      is called from gdb, we should not be trying to restore the
3800      selected frame; it just prints a spurious error message (The
3801      message is useful, however, in detecting bugs in gdb (like if gdb
3802      clobbers the stack)).  In fact, should we be restoring the
3803      inferior status at all in that case?  .  */
3804
3805   if (target_has_stack && inf_status->restore_stack_info)
3806     {
3807       /* The point of catch_errors is that if the stack is clobbered,
3808          walking the stack might encounter a garbage pointer and
3809          error() trying to dereference it.  */
3810       if (catch_errors
3811           (restore_selected_frame, &inf_status->selected_frame_id,
3812            "Unable to restore previously selected frame:\n",
3813            RETURN_MASK_ERROR) == 0)
3814         /* Error in restoring the selected frame.  Select the innermost
3815            frame.  */
3816         select_frame (get_current_frame ());
3817
3818     }
3819
3820   xfree (inf_status);
3821 }
3822
3823 static void
3824 do_restore_inferior_status_cleanup (void *sts)
3825 {
3826   restore_inferior_status (sts);
3827 }
3828
3829 struct cleanup *
3830 make_cleanup_restore_inferior_status (struct inferior_status *inf_status)
3831 {
3832   return make_cleanup (do_restore_inferior_status_cleanup, inf_status);
3833 }
3834
3835 void
3836 discard_inferior_status (struct inferior_status *inf_status)
3837 {
3838   /* See save_inferior_status for info on stop_bpstat. */
3839   bpstat_clear (&inf_status->stop_bpstat);
3840   regcache_xfree (inf_status->registers);
3841   xfree (inf_status);
3842 }
3843
3844 int
3845 inferior_has_forked (int pid, int *child_pid)
3846 {
3847   struct target_waitstatus last;
3848   ptid_t last_ptid;
3849
3850   get_last_target_status (&last_ptid, &last);
3851
3852   if (last.kind != TARGET_WAITKIND_FORKED)
3853     return 0;
3854
3855   if (ptid_get_pid (last_ptid) != pid)
3856     return 0;
3857
3858   *child_pid = last.value.related_pid;
3859   return 1;
3860 }
3861
3862 int
3863 inferior_has_vforked (int pid, int *child_pid)
3864 {
3865   struct target_waitstatus last;
3866   ptid_t last_ptid;
3867
3868   get_last_target_status (&last_ptid, &last);
3869
3870   if (last.kind != TARGET_WAITKIND_VFORKED)
3871     return 0;
3872
3873   if (ptid_get_pid (last_ptid) != pid)
3874     return 0;
3875
3876   *child_pid = last.value.related_pid;
3877   return 1;
3878 }
3879
3880 int
3881 inferior_has_execd (int pid, char **execd_pathname)
3882 {
3883   struct target_waitstatus last;
3884   ptid_t last_ptid;
3885
3886   get_last_target_status (&last_ptid, &last);
3887
3888   if (last.kind != TARGET_WAITKIND_EXECD)
3889     return 0;
3890
3891   if (ptid_get_pid (last_ptid) != pid)
3892     return 0;
3893
3894   *execd_pathname = xstrdup (last.value.execd_pathname);
3895   return 1;
3896 }
3897
3898 /* Oft used ptids */
3899 ptid_t null_ptid;
3900 ptid_t minus_one_ptid;
3901
3902 /* Create a ptid given the necessary PID, LWP, and TID components.  */
3903
3904 ptid_t
3905 ptid_build (int pid, long lwp, long tid)
3906 {
3907   ptid_t ptid;
3908
3909   ptid.pid = pid;
3910   ptid.lwp = lwp;
3911   ptid.tid = tid;
3912   return ptid;
3913 }
3914
3915 /* Create a ptid from just a pid.  */
3916
3917 ptid_t
3918 pid_to_ptid (int pid)
3919 {
3920   return ptid_build (pid, 0, 0);
3921 }
3922
3923 /* Fetch the pid (process id) component from a ptid.  */
3924
3925 int
3926 ptid_get_pid (ptid_t ptid)
3927 {
3928   return ptid.pid;
3929 }
3930
3931 /* Fetch the lwp (lightweight process) component from a ptid.  */
3932
3933 long
3934 ptid_get_lwp (ptid_t ptid)
3935 {
3936   return ptid.lwp;
3937 }
3938
3939 /* Fetch the tid (thread id) component from a ptid.  */
3940
3941 long
3942 ptid_get_tid (ptid_t ptid)
3943 {
3944   return ptid.tid;
3945 }
3946
3947 /* ptid_equal() is used to test equality of two ptids.  */
3948
3949 int
3950 ptid_equal (ptid_t ptid1, ptid_t ptid2)
3951 {
3952   return (ptid1.pid == ptid2.pid && ptid1.lwp == ptid2.lwp
3953           && ptid1.tid == ptid2.tid);
3954 }
3955
3956 /* restore_inferior_ptid() will be used by the cleanup machinery
3957    to restore the inferior_ptid value saved in a call to
3958    save_inferior_ptid().  */
3959
3960 static void
3961 restore_inferior_ptid (void *arg)
3962 {
3963   ptid_t *saved_ptid_ptr = arg;
3964   inferior_ptid = *saved_ptid_ptr;
3965   xfree (arg);
3966 }
3967
3968 /* Save the value of inferior_ptid so that it may be restored by a
3969    later call to do_cleanups().  Returns the struct cleanup pointer
3970    needed for later doing the cleanup.  */
3971
3972 struct cleanup *
3973 save_inferior_ptid (void)
3974 {
3975   ptid_t *saved_ptid_ptr;
3976
3977   saved_ptid_ptr = xmalloc (sizeof (ptid_t));
3978   *saved_ptid_ptr = inferior_ptid;
3979   return make_cleanup (restore_inferior_ptid, saved_ptid_ptr);
3980 }
3981 \f
3982
3983 void
3984 _initialize_infrun (void)
3985 {
3986   int i;
3987   int numsigs;
3988   struct cmd_list_element *c;
3989
3990   add_info ("signals", signals_info, _("\
3991 What debugger does when program gets various signals.\n\
3992 Specify a signal as argument to print info on that signal only."));
3993   add_info_alias ("handle", "signals", 0);
3994
3995   add_com ("handle", class_run, handle_command, _("\
3996 Specify how to handle a signal.\n\
3997 Args are signals and actions to apply to those signals.\n\
3998 Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
3999 from 1-15 are allowed for compatibility with old versions of GDB.\n\
4000 Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
4001 The special arg \"all\" is recognized to mean all signals except those\n\
4002 used by the debugger, typically SIGTRAP and SIGINT.\n\
4003 Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
4004 \"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
4005 Stop means reenter debugger if this signal happens (implies print).\n\
4006 Print means print a message if this signal happens.\n\
4007 Pass means let program see this signal; otherwise program doesn't know.\n\
4008 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
4009 Pass and Stop may be combined."));
4010   if (xdb_commands)
4011     {
4012       add_com ("lz", class_info, signals_info, _("\
4013 What debugger does when program gets various signals.\n\
4014 Specify a signal as argument to print info on that signal only."));
4015       add_com ("z", class_run, xdb_handle_command, _("\
4016 Specify how to handle a signal.\n\
4017 Args are signals and actions to apply to those signals.\n\
4018 Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
4019 from 1-15 are allowed for compatibility with old versions of GDB.\n\
4020 Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
4021 The special arg \"all\" is recognized to mean all signals except those\n\
4022 used by the debugger, typically SIGTRAP and SIGINT.\n\
4023 Recognized actions include \"s\" (toggles between stop and nostop), \n\
4024 \"r\" (toggles between print and noprint), \"i\" (toggles between pass and \
4025 nopass), \"Q\" (noprint)\n\
4026 Stop means reenter debugger if this signal happens (implies print).\n\
4027 Print means print a message if this signal happens.\n\
4028 Pass means let program see this signal; otherwise program doesn't know.\n\
4029 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
4030 Pass and Stop may be combined."));
4031     }
4032
4033   if (!dbx_commands)
4034     stop_command = add_cmd ("stop", class_obscure,
4035                             not_just_help_class_command, _("\
4036 There is no `stop' command, but you can set a hook on `stop'.\n\
4037 This allows you to set a list of commands to be run each time execution\n\
4038 of the program stops."), &cmdlist);
4039
4040   add_setshow_zinteger_cmd ("infrun", class_maintenance, &debug_infrun, _("\
4041 Set inferior debugging."), _("\
4042 Show inferior debugging."), _("\
4043 When non-zero, inferior specific debugging is enabled."),
4044                             NULL,
4045                             show_debug_infrun,
4046                             &setdebuglist, &showdebuglist);
4047
4048   numsigs = (int) TARGET_SIGNAL_LAST;
4049   signal_stop = (unsigned char *) xmalloc (sizeof (signal_stop[0]) * numsigs);
4050   signal_print = (unsigned char *)
4051     xmalloc (sizeof (signal_print[0]) * numsigs);
4052   signal_program = (unsigned char *)
4053     xmalloc (sizeof (signal_program[0]) * numsigs);
4054   for (i = 0; i < numsigs; i++)
4055     {
4056       signal_stop[i] = 1;
4057       signal_print[i] = 1;
4058       signal_program[i] = 1;
4059     }
4060
4061   /* Signals caused by debugger's own actions
4062      should not be given to the program afterwards.  */
4063   signal_program[TARGET_SIGNAL_TRAP] = 0;
4064   signal_program[TARGET_SIGNAL_INT] = 0;
4065
4066   /* Signals that are not errors should not normally enter the debugger.  */
4067   signal_stop[TARGET_SIGNAL_ALRM] = 0;
4068   signal_print[TARGET_SIGNAL_ALRM] = 0;
4069   signal_stop[TARGET_SIGNAL_VTALRM] = 0;
4070   signal_print[TARGET_SIGNAL_VTALRM] = 0;
4071   signal_stop[TARGET_SIGNAL_PROF] = 0;
4072   signal_print[TARGET_SIGNAL_PROF] = 0;
4073   signal_stop[TARGET_SIGNAL_CHLD] = 0;
4074   signal_print[TARGET_SIGNAL_CHLD] = 0;
4075   signal_stop[TARGET_SIGNAL_IO] = 0;
4076   signal_print[TARGET_SIGNAL_IO] = 0;
4077   signal_stop[TARGET_SIGNAL_POLL] = 0;
4078   signal_print[TARGET_SIGNAL_POLL] = 0;
4079   signal_stop[TARGET_SIGNAL_URG] = 0;
4080   signal_print[TARGET_SIGNAL_URG] = 0;
4081   signal_stop[TARGET_SIGNAL_WINCH] = 0;
4082   signal_print[TARGET_SIGNAL_WINCH] = 0;
4083
4084   /* These signals are used internally by user-level thread
4085      implementations.  (See signal(5) on Solaris.)  Like the above
4086      signals, a healthy program receives and handles them as part of
4087      its normal operation.  */
4088   signal_stop[TARGET_SIGNAL_LWP] = 0;
4089   signal_print[TARGET_SIGNAL_LWP] = 0;
4090   signal_stop[TARGET_SIGNAL_WAITING] = 0;
4091   signal_print[TARGET_SIGNAL_WAITING] = 0;
4092   signal_stop[TARGET_SIGNAL_CANCEL] = 0;
4093   signal_print[TARGET_SIGNAL_CANCEL] = 0;
4094
4095   add_setshow_zinteger_cmd ("stop-on-solib-events", class_support,
4096                             &stop_on_solib_events, _("\
4097 Set stopping for shared library events."), _("\
4098 Show stopping for shared library events."), _("\
4099 If nonzero, gdb will give control to the user when the dynamic linker\n\
4100 notifies gdb of shared library events.  The most common event of interest\n\
4101 to the user would be loading/unloading of a new library."),
4102                             NULL,
4103                             show_stop_on_solib_events,
4104                             &setlist, &showlist);
4105
4106   add_setshow_enum_cmd ("follow-fork-mode", class_run,
4107                         follow_fork_mode_kind_names,
4108                         &follow_fork_mode_string, _("\
4109 Set debugger response to a program call of fork or vfork."), _("\
4110 Show debugger response to a program call of fork or vfork."), _("\
4111 A fork or vfork creates a new process.  follow-fork-mode can be:\n\
4112   parent  - the original process is debugged after a fork\n\
4113   child   - the new process is debugged after a fork\n\
4114 The unfollowed process will continue to run.\n\
4115 By default, the debugger will follow the parent process."),
4116                         NULL,
4117                         show_follow_fork_mode_string,
4118                         &setlist, &showlist);
4119
4120   add_setshow_enum_cmd ("scheduler-locking", class_run, 
4121                         scheduler_enums, &scheduler_mode, _("\
4122 Set mode for locking scheduler during execution."), _("\
4123 Show mode for locking scheduler during execution."), _("\
4124 off  == no locking (threads may preempt at any time)\n\
4125 on   == full locking (no thread except the current thread may run)\n\
4126 step == scheduler locked during every single-step operation.\n\
4127         In this mode, no other thread may run during a step command.\n\
4128         Other threads may run while stepping over a function call ('next')."), 
4129                         set_schedlock_func,     /* traps on target vector */
4130                         show_scheduler_mode,
4131                         &setlist, &showlist);
4132
4133   add_setshow_boolean_cmd ("step-mode", class_run, &step_stop_if_no_debug, _("\
4134 Set mode of the step operation."), _("\
4135 Show mode of the step operation."), _("\
4136 When set, doing a step over a function without debug line information\n\
4137 will stop at the first instruction of that function. Otherwise, the\n\
4138 function is skipped and the step command stops at a different source line."),
4139                            NULL,
4140                            show_step_stop_if_no_debug,
4141                            &setlist, &showlist);
4142
4143   /* ptid initializations */
4144   null_ptid = ptid_build (0, 0, 0);
4145   minus_one_ptid = ptid_build (-1, 0, 0);
4146   inferior_ptid = null_ptid;
4147   target_last_wait_ptid = minus_one_ptid;
4148 }