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