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