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