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