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