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