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