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