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