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