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