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