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