import gdb-1999-12-06 snapshot
[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;
1758 }
c906108c 1759
c5aa993b
JM
1760 /* We may want to consider not doing a resume here in order to give
1761 the user a chance to play with the new thread. It might be good
1762 to make that a user-settable option. */
c906108c 1763
c5aa993b
JM
1764 /* At this point, all threads are stopped (happens automatically in
1765 either the OS or the native code). Therefore we need to continue
1766 all threads in order to make progress. */
1767 if (ecs->new_thread_event)
1768 {
1769 target_resume (-1, 0, TARGET_SIGNAL_0);
104c1213
JM
1770 prepare_to_wait (ecs);
1771 return;
c5aa993b 1772 }
c906108c 1773
c5aa993b 1774 stop_pc = read_pc_pid (ecs->pid);
c906108c 1775
c5aa993b
JM
1776 /* See if a thread hit a thread-specific breakpoint that was meant for
1777 another thread. If so, then step that thread past the breakpoint,
1778 and continue it. */
c906108c 1779
c5aa993b
JM
1780 if (stop_signal == TARGET_SIGNAL_TRAP)
1781 {
1782 if (SOFTWARE_SINGLE_STEP_P && singlestep_breakpoints_inserted_p)
1783 ecs->random_signal = 0;
1784 else if (breakpoints_inserted
1785 && breakpoint_here_p (stop_pc - DECR_PC_AFTER_BREAK))
1786 {
cd0fc7c3 1787 ecs->random_signal = 0;
c5aa993b
JM
1788 if (!breakpoint_thread_match (stop_pc - DECR_PC_AFTER_BREAK,
1789 ecs->pid))
1790 {
1791 int remove_status;
1792
1793 /* Saw a breakpoint, but it was hit by the wrong thread.
1794 Just continue. */
1795 write_pc_pid (stop_pc - DECR_PC_AFTER_BREAK, ecs->pid);
1796
1797 remove_status = remove_breakpoints ();
1798 /* Did we fail to remove breakpoints? If so, try
1799 to set the PC past the bp. (There's at least
1800 one situation in which we can fail to remove
1801 the bp's: On HP-UX's that use ttrace, we can't
1802 change the address space of a vforking child
1803 process until the child exits (well, okay, not
1804 then either :-) or execs. */
1805 if (remove_status != 0)
1806 {
1807 write_pc_pid (stop_pc - DECR_PC_AFTER_BREAK + 4, ecs->pid);
1808 }
1809 else
1810 { /* Single step */
1811 target_resume (ecs->pid, 1, TARGET_SIGNAL_0);
1812 /* FIXME: What if a signal arrives instead of the
1813 single-step happening? */
1814
1815 ecs->waiton_pid = ecs->pid;
1816 ecs->wp = &(ecs->ws);
1817 ecs->infwait_state = infwait_thread_hop_state;
104c1213
JM
1818 prepare_to_wait (ecs);
1819 return;
c5aa993b 1820 }
c906108c 1821
c5aa993b
JM
1822 /* We need to restart all the threads now,
1823 * unles we're running in scheduler-locked mode.
1824 * FIXME: shouldn't we look at currently_stepping ()?
1825 */
1826 if (scheduler_mode == schedlock_on)
1827 target_resume (ecs->pid, 0, TARGET_SIGNAL_0);
1828 else
1829 target_resume (-1, 0, TARGET_SIGNAL_0);
104c1213
JM
1830 prepare_to_wait (ecs);
1831 return;
c5aa993b
JM
1832 }
1833 else
1834 {
1835 /* This breakpoint matches--either it is the right
1836 thread or it's a generic breakpoint for all threads.
1837 Remember that we'll need to step just _this_ thread
1838 on any following user continuation! */
1839 thread_step_needed = 1;
1840 }
1841 }
1842 }
1843 else
1844 ecs->random_signal = 1;
1845
1846 /* See if something interesting happened to the non-current thread. If
1847 so, then switch to that thread, and eventually give control back to
1848 the user.
1849
1850 Note that if there's any kind of pending follow (i.e., of a fork,
1851 vfork or exec), we don't want to do this now. Rather, we'll let
1852 the next resume handle it. */
1853 if ((ecs->pid != inferior_pid) &&
1854 (pending_follow.kind == TARGET_WAITKIND_SPURIOUS))
1855 {
1856 int printed = 0;
c906108c 1857
c5aa993b
JM
1858 /* If it's a random signal for a non-current thread, notify user
1859 if he's expressed an interest. */
1860 if (ecs->random_signal
1861 && signal_print[stop_signal])
1862 {
c906108c
SS
1863/* ??rehrauer: I don't understand the rationale for this code. If the
1864 inferior will stop as a result of this signal, then the act of handling
1865 the stop ought to print a message that's couches the stoppage in user
1866 terms, e.g., "Stopped for breakpoint/watchpoint". If the inferior
1867 won't stop as a result of the signal -- i.e., if the signal is merely
1868 a side-effect of something GDB's doing "under the covers" for the
1869 user, such as stepping threads over a breakpoint they shouldn't stop
1870 for -- then the message seems to be a serious annoyance at best.
1871
1872 For now, remove the message altogether. */
1873#if 0
c5aa993b
JM
1874 printed = 1;
1875 target_terminal_ours_for_output ();
1876 printf_filtered ("\nProgram received signal %s, %s.\n",
1877 target_signal_to_name (stop_signal),
1878 target_signal_to_string (stop_signal));
1879 gdb_flush (gdb_stdout);
c906108c 1880#endif
c5aa993b 1881 }
c906108c 1882
c5aa993b
JM
1883 /* If it's not SIGTRAP and not a signal we want to stop for, then
1884 continue the thread. */
c906108c 1885
c5aa993b
JM
1886 if (stop_signal != TARGET_SIGNAL_TRAP
1887 && !signal_stop[stop_signal])
1888 {
1889 if (printed)
1890 target_terminal_inferior ();
c906108c 1891
c5aa993b
JM
1892 /* Clear the signal if it should not be passed. */
1893 if (signal_program[stop_signal] == 0)
1894 stop_signal = TARGET_SIGNAL_0;
c906108c 1895
c5aa993b 1896 target_resume (ecs->pid, 0, stop_signal);
104c1213
JM
1897 prepare_to_wait (ecs);
1898 return;
c5aa993b 1899 }
c906108c 1900
c5aa993b
JM
1901 /* It's a SIGTRAP or a signal we're interested in. Switch threads,
1902 and fall into the rest of wait_for_inferior(). */
1903
c2d11a7d
JM
1904 /* Caution: it may happen that the new thread (or the old one!)
1905 is not in the thread list. In this case we must not attempt
1906 to "switch context", or we run the risk that our context may
1907 be lost. This may happen as a result of the target module
1908 mishandling thread creation. */
1909
1910 if (in_thread_list (inferior_pid) && in_thread_list (ecs->pid))
1911 { /* Perform infrun state context switch: */
1912 /* Save infrun state for the old thread. */
1913 save_infrun_state (inferior_pid, prev_pc,
1914 prev_func_start, prev_func_name,
1915 trap_expected, step_resume_breakpoint,
1916 through_sigtramp_breakpoint,
1917 step_range_start, step_range_end,
1918 step_frame_address, ecs->handling_longjmp,
1919 ecs->another_trap,
1920 ecs->stepping_through_solib_after_catch,
1921 ecs->stepping_through_solib_catchpoints,
1922 ecs->stepping_through_sigtramp);
1923
1924 /* Load infrun state for the new thread. */
1925 load_infrun_state (ecs->pid, &prev_pc,
1926 &prev_func_start, &prev_func_name,
1927 &trap_expected, &step_resume_breakpoint,
1928 &through_sigtramp_breakpoint,
1929 &step_range_start, &step_range_end,
1930 &step_frame_address, &ecs->handling_longjmp,
1931 &ecs->another_trap,
1932 &ecs->stepping_through_solib_after_catch,
1933 &ecs->stepping_through_solib_catchpoints,
1934 &ecs->stepping_through_sigtramp);
1935 }
c5aa993b
JM
1936 if (may_switch_from_inferior_pid)
1937 switched_from_inferior_pid = inferior_pid;
1938
1939 inferior_pid = ecs->pid;
1940
c5aa993b
JM
1941 if (context_hook)
1942 context_hook (pid_to_thread_id (ecs->pid));
1943
1944 printf_filtered ("[Switching to %s]\n", target_pid_to_str (ecs->pid));
1945 flush_cached_frames ();
1946 }
c906108c 1947
c5aa993b
JM
1948 if (SOFTWARE_SINGLE_STEP_P && singlestep_breakpoints_inserted_p)
1949 {
1950 /* Pull the single step breakpoints out of the target. */
1951 SOFTWARE_SINGLE_STEP (0, 0);
1952 singlestep_breakpoints_inserted_p = 0;
1953 }
c906108c 1954
c5aa993b
JM
1955 /* If PC is pointing at a nullified instruction, then step beyond
1956 it so that the user won't be confused when GDB appears to be ready
1957 to execute it. */
c906108c 1958
c5aa993b
JM
1959 /* if (INSTRUCTION_NULLIFIED && currently_stepping (ecs)) */
1960 if (INSTRUCTION_NULLIFIED)
1961 {
1962 registers_changed ();
1963 target_resume (ecs->pid, 1, TARGET_SIGNAL_0);
c906108c 1964
c5aa993b
JM
1965 /* We may have received a signal that we want to pass to
1966 the inferior; therefore, we must not clobber the waitstatus
1967 in WS. */
c906108c 1968
c5aa993b
JM
1969 ecs->infwait_state = infwait_nullified_state;
1970 ecs->waiton_pid = ecs->pid;
1971 ecs->wp = &(ecs->tmpstatus);
104c1213
JM
1972 prepare_to_wait (ecs);
1973 return;
c5aa993b 1974 }
c906108c 1975
c5aa993b
JM
1976 /* It may not be necessary to disable the watchpoint to stop over
1977 it. For example, the PA can (with some kernel cooperation)
1978 single step over a watchpoint without disabling the watchpoint. */
1979 if (HAVE_STEPPABLE_WATCHPOINT && STOPPED_BY_WATCHPOINT (ecs->ws))
1980 {
1981 resume (1, 0);
104c1213
JM
1982 prepare_to_wait (ecs);
1983 return;
c5aa993b 1984 }
c906108c 1985
c5aa993b
JM
1986 /* It is far more common to need to disable a watchpoint to step
1987 the inferior over it. FIXME. What else might a debug
1988 register or page protection watchpoint scheme need here? */
1989 if (HAVE_NONSTEPPABLE_WATCHPOINT && STOPPED_BY_WATCHPOINT (ecs->ws))
1990 {
1991 /* At this point, we are stopped at an instruction which has
1992 attempted to write to a piece of memory under control of
1993 a watchpoint. The instruction hasn't actually executed
1994 yet. If we were to evaluate the watchpoint expression
1995 now, we would get the old value, and therefore no change
1996 would seem to have occurred.
1997
1998 In order to make watchpoints work `right', we really need
1999 to complete the memory write, and then evaluate the
2000 watchpoint expression. The following code does that by
2001 removing the watchpoint (actually, all watchpoints and
2002 breakpoints), single-stepping the target, re-inserting
2003 watchpoints, and then falling through to let normal
2004 single-step processing handle proceed. Since this
2005 includes evaluating watchpoints, things will come to a
2006 stop in the correct manner. */
2007
2008 write_pc (stop_pc - DECR_PC_AFTER_BREAK);
2009
2010 remove_breakpoints ();
2011 registers_changed ();
2012 target_resume (ecs->pid, 1, TARGET_SIGNAL_0); /* Single step */
2013
2014 ecs->waiton_pid = ecs->pid;
2015 ecs->wp = &(ecs->ws);
2016 ecs->infwait_state = infwait_nonstep_watch_state;
104c1213
JM
2017 prepare_to_wait (ecs);
2018 return;
c5aa993b 2019 }
c906108c 2020
c5aa993b
JM
2021 /* It may be possible to simply continue after a watchpoint. */
2022 if (HAVE_CONTINUABLE_WATCHPOINT)
2023 STOPPED_BY_WATCHPOINT (ecs->ws);
2024
2025 ecs->stop_func_start = 0;
2026 ecs->stop_func_end = 0;
2027 ecs->stop_func_name = 0;
2028 /* Don't care about return value; stop_func_start and stop_func_name
2029 will both be 0 if it doesn't work. */
2030 find_pc_partial_function (stop_pc, &ecs->stop_func_name,
2031 &ecs->stop_func_start, &ecs->stop_func_end);
2032 ecs->stop_func_start += FUNCTION_START_OFFSET;
2033 ecs->another_trap = 0;
2034 bpstat_clear (&stop_bpstat);
2035 stop_step = 0;
2036 stop_stack_dummy = 0;
2037 stop_print_frame = 1;
2038 ecs->random_signal = 0;
2039 stopped_by_random_signal = 0;
2040 breakpoints_failed = 0;
2041
2042 /* Look at the cause of the stop, and decide what to do.
2043 The alternatives are:
2044 1) break; to really stop and return to the debugger,
2045 2) drop through to start up again
2046 (set ecs->another_trap to 1 to single step once)
2047 3) set ecs->random_signal to 1, and the decision between 1 and 2
2048 will be made according to the signal handling tables. */
2049
2050 /* First, distinguish signals caused by the debugger from signals
2051 that have to do with the program's own actions.
2052 Note that breakpoint insns may cause SIGTRAP or SIGILL
2053 or SIGEMT, depending on the operating system version.
2054 Here we detect when a SIGILL or SIGEMT is really a breakpoint
2055 and change it to SIGTRAP. */
2056
2057 if (stop_signal == TARGET_SIGNAL_TRAP
2058 || (breakpoints_inserted &&
2059 (stop_signal == TARGET_SIGNAL_ILL
2060 || stop_signal == TARGET_SIGNAL_EMT
2061 ))
2062 || stop_soon_quietly)
2063 {
2064 if (stop_signal == TARGET_SIGNAL_TRAP && stop_after_trap)
2065 {
2066 stop_print_frame = 0;
104c1213
JM
2067 stop_stepping (ecs);
2068 return;
c5aa993b
JM
2069 }
2070 if (stop_soon_quietly)
104c1213
JM
2071 {
2072 stop_stepping (ecs);
2073 return;
2074 }
c906108c 2075
c5aa993b
JM
2076 /* Don't even think about breakpoints
2077 if just proceeded over a breakpoint.
c906108c 2078
c5aa993b
JM
2079 However, if we are trying to proceed over a breakpoint
2080 and end up in sigtramp, then through_sigtramp_breakpoint
2081 will be set and we should check whether we've hit the
2082 step breakpoint. */
2083 if (stop_signal == TARGET_SIGNAL_TRAP && trap_expected
2084 && through_sigtramp_breakpoint == NULL)
2085 bpstat_clear (&stop_bpstat);
2086 else
2087 {
2088 /* See if there is a breakpoint at the current PC. */
2089 stop_bpstat = bpstat_stop_status
2090 (&stop_pc,
6426a772
JM
2091 /* Pass TRUE if our reason for stopping is something other
2092 than hitting a breakpoint. We do this by checking that
2093 1) stepping is going on and 2) we didn't hit a breakpoint
2094 in a signal handler without an intervening stop in
2095 sigtramp, which is detected by a new stack pointer value
2096 below any usual function calling stack adjustments. */
c5aa993b 2097 (currently_stepping (ecs)
c5aa993b 2098 && !(step_range_end
6426a772 2099 && INNER_THAN (read_sp (), (step_sp - 16))))
c5aa993b
JM
2100 );
2101 /* Following in case break condition called a
2102 function. */
2103 stop_print_frame = 1;
2104 }
c906108c 2105
c5aa993b
JM
2106 if (stop_signal == TARGET_SIGNAL_TRAP)
2107 ecs->random_signal
2108 = !(bpstat_explains_signal (stop_bpstat)
2109 || trap_expected
2110 || (!CALL_DUMMY_BREAKPOINT_OFFSET_P
2111 && PC_IN_CALL_DUMMY (stop_pc, read_sp (),
2112 FRAME_FP (get_current_frame ())))
2113 || (step_range_end && step_resume_breakpoint == NULL));
2114
2115 else
2116 {
cd0fc7c3 2117 ecs->random_signal
c906108c 2118 = !(bpstat_explains_signal (stop_bpstat)
c5aa993b
JM
2119 /* End of a stack dummy. Some systems (e.g. Sony
2120 news) give another signal besides SIGTRAP, so
2121 check here as well as above. */
7a292a7a
SS
2122 || (!CALL_DUMMY_BREAKPOINT_OFFSET_P
2123 && PC_IN_CALL_DUMMY (stop_pc, read_sp (),
2124 FRAME_FP (get_current_frame ())))
c5aa993b
JM
2125 );
2126 if (!ecs->random_signal)
2127 stop_signal = TARGET_SIGNAL_TRAP;
2128 }
2129 }
c906108c 2130
c5aa993b
JM
2131 /* When we reach this point, we've pretty much decided
2132 that the reason for stopping must've been a random
2133 (unexpected) signal. */
2134
2135 else
2136 ecs->random_signal = 1;
2137 /* If a fork, vfork or exec event was seen, then there are two
2138 possible responses we can make:
2139
2140 1. If a catchpoint triggers for the event (ecs->random_signal == 0),
2141 then we must stop now and issue a prompt. We will resume
2142 the inferior when the user tells us to.
2143 2. If no catchpoint triggers for the event (ecs->random_signal == 1),
2144 then we must resume the inferior now and keep checking.
2145
2146 In either case, we must take appropriate steps to "follow" the
2147 the fork/vfork/exec when the inferior is resumed. For example,
2148 if follow-fork-mode is "child", then we must detach from the
2149 parent inferior and follow the new child inferior.
2150
2151 In either case, setting pending_follow causes the next resume()
2152 to take the appropriate following action. */
2153 process_event_stop_test:
2154 if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
2155 {
2156 if (ecs->random_signal) /* I.e., no catchpoint triggered for this. */
2157 {
2158 trap_expected = 1;
2159 stop_signal = TARGET_SIGNAL_0;
d4f3574e
SS
2160 keep_going (ecs);
2161 return;
c5aa993b
JM
2162 }
2163 }
2164 else if (ecs->ws.kind == TARGET_WAITKIND_VFORKED)
2165 {
2166 if (ecs->random_signal) /* I.e., no catchpoint triggered for this. */
2167 {
d4f3574e
SS
2168 stop_signal = TARGET_SIGNAL_0;
2169 keep_going (ecs);
2170 return;
c5aa993b
JM
2171 }
2172 }
2173 else if (ecs->ws.kind == TARGET_WAITKIND_EXECD)
2174 {
2175 pending_follow.kind = ecs->ws.kind;
2176 if (ecs->random_signal) /* I.e., no catchpoint triggered for this. */
2177 {
2178 trap_expected = 1;
2179 stop_signal = TARGET_SIGNAL_0;
d4f3574e
SS
2180 keep_going (ecs);
2181 return;
c5aa993b
JM
2182 }
2183 }
c906108c 2184
c5aa993b
JM
2185 /* For the program's own signals, act according to
2186 the signal handling tables. */
c906108c 2187
c5aa993b
JM
2188 if (ecs->random_signal)
2189 {
2190 /* Signal not for debugging purposes. */
2191 int printed = 0;
2192
2193 stopped_by_random_signal = 1;
2194
2195 if (signal_print[stop_signal])
2196 {
2197 printed = 1;
2198 target_terminal_ours_for_output ();
11cf8741 2199 print_stop_reason (SIGNAL_RECEIVED, stop_signal);
c5aa993b
JM
2200 }
2201 if (signal_stop[stop_signal])
104c1213
JM
2202 {
2203 stop_stepping (ecs);
2204 return;
2205 }
c5aa993b
JM
2206 /* If not going to stop, give terminal back
2207 if we took it away. */
2208 else if (printed)
2209 target_terminal_inferior ();
2210
2211 /* Clear the signal if it should not be passed. */
2212 if (signal_program[stop_signal] == 0)
2213 stop_signal = TARGET_SIGNAL_0;
2214
a0b3c4fd
JM
2215 /* I'm not sure whether this needs to be check_sigtramp2 or
2216 whether it could/should be keep_going.
2217
2218 This used to jump to step_over_function if we are stepping,
2219 which is wrong.
2220
2221 Suppose the user does a `next' over a function call, and while
2222 that call is in progress, the inferior receives a signal for
2223 which GDB does not stop (i.e., signal_stop[SIG] is false). In
2224 that case, when we reach this point, there is already a
2225 step-resume breakpoint established, right where it should be:
2226 immediately after the function call the user is "next"-ing
d4f3574e 2227 over. If we call step_over_function now, two bad things
a0b3c4fd
JM
2228 happen:
2229
2230 - we'll create a new breakpoint, at wherever the current
2231 frame's return address happens to be. That could be
2232 anywhere, depending on what function call happens to be on
2233 the top of the stack at that point. Point is, it's probably
2234 not where we need it.
2235
2236 - the existing step-resume breakpoint (which is at the correct
2237 address) will get orphaned: step_resume_breakpoint will point
2238 to the new breakpoint, and the old step-resume breakpoint
2239 will never be cleaned up.
2240
2241 The old behavior was meant to help HP-UX single-step out of
2242 sigtramps. It would place the new breakpoint at prev_pc, which
2243 was certainly wrong. I don't know the details there, so fixing
2244 this probably breaks that. As with anything else, it's up to
2245 the HP-UX maintainer to furnish a fix that doesn't break other
2246 platforms. --JimB, 20 May 1999 */
104c1213 2247 check_sigtramp2 (ecs);
c5aa993b
JM
2248 }
2249
2250 /* Handle cases caused by hitting a breakpoint. */
2251 {
2252 CORE_ADDR jmp_buf_pc;
2253 struct bpstat_what what;
2254
2255 what = bpstat_what (stop_bpstat);
2256
2257 if (what.call_dummy)
c906108c 2258 {
c5aa993b
JM
2259 stop_stack_dummy = 1;
2260#ifdef HP_OS_BUG
2261 trap_expected_after_continue = 1;
2262#endif
c906108c 2263 }
c5aa993b
JM
2264
2265 switch (what.main_action)
c906108c 2266 {
c5aa993b
JM
2267 case BPSTAT_WHAT_SET_LONGJMP_RESUME:
2268 /* If we hit the breakpoint at longjmp, disable it for the
2269 duration of this command. Then, install a temporary
2270 breakpoint at the target of the jmp_buf. */
2271 disable_longjmp_breakpoint ();
2272 remove_breakpoints ();
2273 breakpoints_inserted = 0;
2274 if (!GET_LONGJMP_TARGET (&jmp_buf_pc))
d4f3574e
SS
2275 {
2276 keep_going (ecs);
2277 return;
2278 }
c5aa993b
JM
2279
2280 /* Need to blow away step-resume breakpoint, as it
2281 interferes with us */
2282 if (step_resume_breakpoint != NULL)
c906108c 2283 {
c5aa993b
JM
2284 delete_breakpoint (step_resume_breakpoint);
2285 step_resume_breakpoint = NULL;
c906108c 2286 }
c5aa993b
JM
2287 /* Not sure whether we need to blow this away too, but probably
2288 it is like the step-resume breakpoint. */
2289 if (through_sigtramp_breakpoint != NULL)
c906108c 2290 {
c5aa993b
JM
2291 delete_breakpoint (through_sigtramp_breakpoint);
2292 through_sigtramp_breakpoint = NULL;
c906108c 2293 }
c906108c 2294
c5aa993b
JM
2295#if 0
2296 /* FIXME - Need to implement nested temporary breakpoints */
2297 if (step_over_calls > 0)
2298 set_longjmp_resume_breakpoint (jmp_buf_pc,
2299 get_current_frame ());
2300 else
2301#endif /* 0 */
2302 set_longjmp_resume_breakpoint (jmp_buf_pc, NULL);
2303 ecs->handling_longjmp = 1; /* FIXME */
d4f3574e
SS
2304 keep_going (ecs);
2305 return;
c906108c 2306
c5aa993b
JM
2307 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
2308 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME_SINGLE:
2309 remove_breakpoints ();
2310 breakpoints_inserted = 0;
2311#if 0
2312 /* FIXME - Need to implement nested temporary breakpoints */
2313 if (step_over_calls
2314 && (INNER_THAN (FRAME_FP (get_current_frame ()),
2315 step_frame_address)))
2316 {
2317 ecs->another_trap = 1;
d4f3574e
SS
2318 keep_going (ecs);
2319 return;
c5aa993b
JM
2320 }
2321#endif /* 0 */
2322 disable_longjmp_breakpoint ();
2323 ecs->handling_longjmp = 0; /* FIXME */
2324 if (what.main_action == BPSTAT_WHAT_CLEAR_LONGJMP_RESUME)
2325 break;
2326 /* else fallthrough */
c906108c 2327
c5aa993b
JM
2328 case BPSTAT_WHAT_SINGLE:
2329 if (breakpoints_inserted)
c906108c 2330 {
c5aa993b
JM
2331 thread_step_needed = 1;
2332 remove_breakpoints ();
c906108c 2333 }
c5aa993b
JM
2334 breakpoints_inserted = 0;
2335 ecs->another_trap = 1;
2336 /* Still need to check other stuff, at least the case
2337 where we are stepping and step out of the right range. */
2338 break;
c906108c 2339
c5aa993b
JM
2340 case BPSTAT_WHAT_STOP_NOISY:
2341 stop_print_frame = 1;
c906108c 2342
c5aa993b
JM
2343 /* We are about to nuke the step_resume_breakpoint and
2344 through_sigtramp_breakpoint via the cleanup chain, so
2345 no need to worry about it here. */
c906108c 2346
104c1213
JM
2347 stop_stepping (ecs);
2348 return;
c906108c 2349
c5aa993b
JM
2350 case BPSTAT_WHAT_STOP_SILENT:
2351 stop_print_frame = 0;
c906108c 2352
c5aa993b
JM
2353 /* We are about to nuke the step_resume_breakpoint and
2354 through_sigtramp_breakpoint via the cleanup chain, so
2355 no need to worry about it here. */
c906108c 2356
104c1213
JM
2357 stop_stepping (ecs);
2358 return;
c906108c 2359
c5aa993b
JM
2360 case BPSTAT_WHAT_STEP_RESUME:
2361 /* This proably demands a more elegant solution, but, yeah
2362 right...
2363
2364 This function's use of the simple variable
2365 step_resume_breakpoint doesn't seem to accomodate
2366 simultaneously active step-resume bp's, although the
2367 breakpoint list certainly can.
2368
2369 If we reach here and step_resume_breakpoint is already
2370 NULL, then apparently we have multiple active
2371 step-resume bp's. We'll just delete the breakpoint we
53a5351d
JM
2372 stopped at, and carry on.
2373
2374 Correction: what the code currently does is delete a
2375 step-resume bp, but it makes no effort to ensure that
2376 the one deleted is the one currently stopped at. MVS */
2377
c5aa993b
JM
2378 if (step_resume_breakpoint == NULL)
2379 {
2380 step_resume_breakpoint =
2381 bpstat_find_step_resume_breakpoint (stop_bpstat);
2382 }
2383 delete_breakpoint (step_resume_breakpoint);
2384 step_resume_breakpoint = NULL;
2385 break;
2386
2387 case BPSTAT_WHAT_THROUGH_SIGTRAMP:
2388 if (through_sigtramp_breakpoint)
2389 delete_breakpoint (through_sigtramp_breakpoint);
2390 through_sigtramp_breakpoint = NULL;
2391
2392 /* If were waiting for a trap, hitting the step_resume_break
2393 doesn't count as getting it. */
2394 if (trap_expected)
2395 ecs->another_trap = 1;
2396 break;
c906108c 2397
c5aa993b
JM
2398 case BPSTAT_WHAT_CHECK_SHLIBS:
2399 case BPSTAT_WHAT_CHECK_SHLIBS_RESUME_FROM_HOOK:
2400#ifdef SOLIB_ADD
c906108c 2401 {
c5aa993b
JM
2402 /* Remove breakpoints, we eventually want to step over the
2403 shlib event breakpoint, and SOLIB_ADD might adjust
2404 breakpoint addresses via breakpoint_re_set. */
2405 if (breakpoints_inserted)
2406 remove_breakpoints ();
c906108c 2407 breakpoints_inserted = 0;
c906108c 2408
c5aa993b
JM
2409 /* Check for any newly added shared libraries if we're
2410 supposed to be adding them automatically. */
2411 if (auto_solib_add)
c906108c 2412 {
c5aa993b
JM
2413 /* Switch terminal for any messages produced by
2414 breakpoint_re_set. */
2415 target_terminal_ours_for_output ();
2416 SOLIB_ADD (NULL, 0, NULL);
2417 target_terminal_inferior ();
c906108c 2418 }
c5aa993b
JM
2419
2420 /* Try to reenable shared library breakpoints, additional
2421 code segments in shared libraries might be mapped in now. */
2422 re_enable_breakpoints_in_shlibs ();
2423
2424 /* If requested, stop when the dynamic linker notifies
2425 gdb of events. This allows the user to get control
2426 and place breakpoints in initializer routines for
2427 dynamically loaded objects (among other things). */
2428 if (stop_on_solib_events)
c906108c 2429 {
104c1213
JM
2430 stop_stepping (ecs);
2431 return;
c906108c
SS
2432 }
2433
c5aa993b
JM
2434 /* If we stopped due to an explicit catchpoint, then the
2435 (see above) call to SOLIB_ADD pulled in any symbols
2436 from a newly-loaded library, if appropriate.
2437
2438 We do want the inferior to stop, but not where it is
2439 now, which is in the dynamic linker callback. Rather,
2440 we would like it stop in the user's program, just after
2441 the call that caused this catchpoint to trigger. That
2442 gives the user a more useful vantage from which to
2443 examine their program's state. */
2444 else if (what.main_action == BPSTAT_WHAT_CHECK_SHLIBS_RESUME_FROM_HOOK)
2445 {
2446 /* ??rehrauer: If I could figure out how to get the
2447 right return PC from here, we could just set a temp
2448 breakpoint and resume. I'm not sure we can without
2449 cracking open the dld's shared libraries and sniffing
2450 their unwind tables and text/data ranges, and that's
2451 not a terribly portable notion.
2452
2453 Until that time, we must step the inferior out of the
2454 dld callback, and also out of the dld itself (and any
2455 code or stubs in libdld.sl, such as "shl_load" and
2456 friends) until we reach non-dld code. At that point,
2457 we can stop stepping. */
2458 bpstat_get_triggered_catchpoints (stop_bpstat,
2459 &ecs->stepping_through_solib_catchpoints);
2460 ecs->stepping_through_solib_after_catch = 1;
2461
2462 /* Be sure to lift all breakpoints, so the inferior does
2463 actually step past this point... */
2464 ecs->another_trap = 1;
2465 break;
2466 }
2467 else
c906108c 2468 {
c5aa993b 2469 /* We want to step over this breakpoint, then keep going. */
cd0fc7c3 2470 ecs->another_trap = 1;
c5aa993b 2471 break;
c906108c 2472 }
c5aa993b
JM
2473 }
2474#endif
2475 break;
c906108c 2476
c5aa993b
JM
2477 case BPSTAT_WHAT_LAST:
2478 /* Not a real code, but listed here to shut up gcc -Wall. */
c906108c 2479
c5aa993b
JM
2480 case BPSTAT_WHAT_KEEP_CHECKING:
2481 break;
2482 }
2483 }
c906108c 2484
c5aa993b
JM
2485 /* We come here if we hit a breakpoint but should not
2486 stop for it. Possibly we also were stepping
2487 and should stop for that. So fall through and
2488 test for stepping. But, if not stepping,
2489 do not stop. */
c906108c 2490
c5aa993b
JM
2491 /* Are we stepping to get the inferior out of the dynamic
2492 linker's hook (and possibly the dld itself) after catching
2493 a shlib event? */
2494 if (ecs->stepping_through_solib_after_catch)
2495 {
2496#if defined(SOLIB_ADD)
2497 /* Have we reached our destination? If not, keep going. */
2498 if (SOLIB_IN_DYNAMIC_LINKER (ecs->pid, stop_pc))
2499 {
2500 ecs->another_trap = 1;
d4f3574e
SS
2501 keep_going (ecs);
2502 return;
c5aa993b
JM
2503 }
2504#endif
2505 /* Else, stop and report the catchpoint(s) whose triggering
2506 caused us to begin stepping. */
2507 ecs->stepping_through_solib_after_catch = 0;
2508 bpstat_clear (&stop_bpstat);
2509 stop_bpstat = bpstat_copy (ecs->stepping_through_solib_catchpoints);
2510 bpstat_clear (&ecs->stepping_through_solib_catchpoints);
2511 stop_print_frame = 1;
104c1213
JM
2512 stop_stepping (ecs);
2513 return;
c5aa993b 2514 }
c906108c 2515
c5aa993b
JM
2516 if (!CALL_DUMMY_BREAKPOINT_OFFSET_P)
2517 {
2518 /* This is the old way of detecting the end of the stack dummy.
2519 An architecture which defines CALL_DUMMY_BREAKPOINT_OFFSET gets
2520 handled above. As soon as we can test it on all of them, all
2521 architectures should define it. */
2522
2523 /* If this is the breakpoint at the end of a stack dummy,
2524 just stop silently, unless the user was doing an si/ni, in which
2525 case she'd better know what she's doing. */
2526
2527 if (CALL_DUMMY_HAS_COMPLETED (stop_pc, read_sp (),
2528 FRAME_FP (get_current_frame ()))
2529 && !step_range_end)
2530 {
c906108c 2531 stop_print_frame = 0;
c5aa993b
JM
2532 stop_stack_dummy = 1;
2533#ifdef HP_OS_BUG
2534 trap_expected_after_continue = 1;
2535#endif
104c1213
JM
2536 stop_stepping (ecs);
2537 return;
c5aa993b
JM
2538 }
2539 }
c906108c 2540
c5aa993b 2541 if (step_resume_breakpoint)
104c1213
JM
2542 {
2543 /* Having a step-resume breakpoint overrides anything
2544 else having to do with stepping commands until
2545 that breakpoint is reached. */
2546 /* I'm not sure whether this needs to be check_sigtramp2 or
2547 whether it could/should be keep_going. */
2548 check_sigtramp2 (ecs);
d4f3574e
SS
2549 keep_going (ecs);
2550 return;
104c1213
JM
2551 }
2552
c5aa993b 2553 if (step_range_end == 0)
104c1213
JM
2554 {
2555 /* Likewise if we aren't even stepping. */
2556 /* I'm not sure whether this needs to be check_sigtramp2 or
2557 whether it could/should be keep_going. */
2558 check_sigtramp2 (ecs);
d4f3574e
SS
2559 keep_going (ecs);
2560 return;
104c1213 2561 }
c5aa993b
JM
2562
2563 /* If stepping through a line, keep going if still within it.
2564
2565 Note that step_range_end is the address of the first instruction
2566 beyond the step range, and NOT the address of the last instruction
2567 within it! */
2568 if (stop_pc >= step_range_start
2569 && stop_pc < step_range_end)
2570 {
2571 /* We might be doing a BPSTAT_WHAT_SINGLE and getting a signal.
2572 So definately need to check for sigtramp here. */
104c1213 2573 check_sigtramp2 (ecs);
d4f3574e
SS
2574 keep_going (ecs);
2575 return;
c5aa993b 2576 }
c906108c 2577
c5aa993b 2578 /* We stepped out of the stepping range. */
c906108c 2579
c5aa993b
JM
2580 /* If we are stepping at the source level and entered the runtime
2581 loader dynamic symbol resolution code, we keep on single stepping
2582 until we exit the run time loader code and reach the callee's
2583 address. */
2584 if (step_over_calls < 0 && IN_SOLIB_DYNSYM_RESOLVE_CODE (stop_pc))
d4f3574e
SS
2585 {
2586 CORE_ADDR pc_after_resolver = SKIP_SOLIB_RESOLVER (stop_pc);
2587
2588 if (pc_after_resolver)
2589 {
2590 /* Set up a step-resume breakpoint at the address
2591 indicated by SKIP_SOLIB_RESOLVER. */
2592 struct symtab_and_line sr_sal;
2593 INIT_SAL (&sr_sal);
2594 sr_sal.pc = pc_after_resolver;
2595
2596 check_for_old_step_resume_breakpoint ();
2597 step_resume_breakpoint =
2598 set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
2599 if (breakpoints_inserted)
2600 insert_breakpoints ();
2601 }
2602
2603 keep_going (ecs);
2604 return;
2605 }
c906108c 2606
c5aa993b
JM
2607 /* We can't update step_sp every time through the loop, because
2608 reading the stack pointer would slow down stepping too much.
2609 But we can update it every time we leave the step range. */
2610 ecs->update_step_sp = 1;
c906108c 2611
c5aa993b
JM
2612 /* Did we just take a signal? */
2613 if (IN_SIGTRAMP (stop_pc, ecs->stop_func_name)
2614 && !IN_SIGTRAMP (prev_pc, prev_func_name)
2615 && INNER_THAN (read_sp (), step_sp))
2616 {
2617 /* We've just taken a signal; go until we are back to
2618 the point where we took it and one more. */
c906108c 2619
c5aa993b
JM
2620 /* Note: The test above succeeds not only when we stepped
2621 into a signal handler, but also when we step past the last
2622 statement of a signal handler and end up in the return stub
2623 of the signal handler trampoline. To distinguish between
2624 these two cases, check that the frame is INNER_THAN the
2625 previous one below. pai/1997-09-11 */
c906108c 2626
c906108c 2627
c5aa993b
JM
2628 {
2629 CORE_ADDR current_frame = FRAME_FP (get_current_frame ());
c906108c 2630
c5aa993b
JM
2631 if (INNER_THAN (current_frame, step_frame_address))
2632 {
2633 /* We have just taken a signal; go until we are back to
2634 the point where we took it and one more. */
c906108c 2635
c5aa993b
JM
2636 /* This code is needed at least in the following case:
2637 The user types "next" and then a signal arrives (before
2638 the "next" is done). */
c906108c 2639
c5aa993b
JM
2640 /* Note that if we are stopped at a breakpoint, then we need
2641 the step_resume breakpoint to override any breakpoints at
2642 the same location, so that we will still step over the
2643 breakpoint even though the signal happened. */
2644 struct symtab_and_line sr_sal;
c906108c 2645
c5aa993b
JM
2646 INIT_SAL (&sr_sal);
2647 sr_sal.symtab = NULL;
2648 sr_sal.line = 0;
2649 sr_sal.pc = prev_pc;
2650 /* We could probably be setting the frame to
2651 step_frame_address; I don't think anyone thought to
2652 try it. */
a0b3c4fd 2653 check_for_old_step_resume_breakpoint ();
c5aa993b
JM
2654 step_resume_breakpoint =
2655 set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
2656 if (breakpoints_inserted)
2657 insert_breakpoints ();
c906108c 2658 }
c5aa993b 2659 else
c906108c 2660 {
c5aa993b
JM
2661 /* We just stepped out of a signal handler and into
2662 its calling trampoline.
2663
d4f3574e 2664 Normally, we'd call step_over_function from
c5aa993b
JM
2665 here, but for some reason GDB can't unwind the
2666 stack correctly to find the real PC for the point
2667 user code where the signal trampoline will return
2668 -- FRAME_SAVED_PC fails, at least on HP-UX 10.20.
2669 But signal trampolines are pretty small stubs of
2670 code, anyway, so it's OK instead to just
2671 single-step out. Note: assuming such trampolines
2672 don't exhibit recursion on any platform... */
2673 find_pc_partial_function (stop_pc, &ecs->stop_func_name,
2674 &ecs->stop_func_start,
2675 &ecs->stop_func_end);
2676 /* Readjust stepping range */
2677 step_range_start = ecs->stop_func_start;
2678 step_range_end = ecs->stop_func_end;
2679 ecs->stepping_through_sigtramp = 1;
c906108c 2680 }
c906108c
SS
2681 }
2682
c906108c 2683
c5aa993b
JM
2684 /* If this is stepi or nexti, make sure that the stepping range
2685 gets us past that instruction. */
2686 if (step_range_end == 1)
2687 /* FIXME: Does this run afoul of the code below which, if
2688 we step into the middle of a line, resets the stepping
2689 range? */
2690 step_range_end = (step_range_start = prev_pc) + 1;
c906108c 2691
c5aa993b 2692 ecs->remove_breakpoints_on_following_step = 1;
d4f3574e
SS
2693 keep_going (ecs);
2694 return;
c5aa993b 2695 }
c906108c 2696
c5aa993b
JM
2697 if (stop_pc == ecs->stop_func_start /* Quick test */
2698 || (in_prologue (stop_pc, ecs->stop_func_start) &&
2699 !IN_SOLIB_RETURN_TRAMPOLINE (stop_pc, ecs->stop_func_name))
2700 || IN_SOLIB_CALL_TRAMPOLINE (stop_pc, ecs->stop_func_name)
2701 || ecs->stop_func_name == 0)
2702 {
2703 /* It's a subroutine call. */
c906108c 2704
c5aa993b 2705 if (step_over_calls == 0)
c906108c 2706 {
c5aa993b
JM
2707 /* I presume that step_over_calls is only 0 when we're
2708 supposed to be stepping at the assembly language level
2709 ("stepi"). Just stop. */
2710 stop_step = 1;
11cf8741 2711 print_stop_reason (END_STEPPING_RANGE, 0);
104c1213
JM
2712 stop_stepping (ecs);
2713 return;
c5aa993b 2714 }
c906108c 2715
c5aa993b 2716 if (step_over_calls > 0 || IGNORE_HELPER_CALL (stop_pc))
d4f3574e
SS
2717 {
2718 /* We're doing a "next". */
2719 step_over_function (ecs);
2720 keep_going (ecs);
2721 return;
2722 }
c5aa993b
JM
2723
2724 /* If we are in a function call trampoline (a stub between
2725 the calling routine and the real function), locate the real
2726 function. That's what tells us (a) whether we want to step
2727 into it at all, and (b) what prologue we want to run to
2728 the end of, if we do step into it. */
2729 tmp = SKIP_TRAMPOLINE_CODE (stop_pc);
2730 if (tmp != 0)
2731 ecs->stop_func_start = tmp;
2732 else
2733 {
2734 tmp = DYNAMIC_TRAMPOLINE_NEXTPC (stop_pc);
2735 if (tmp)
c906108c 2736 {
c5aa993b
JM
2737 struct symtab_and_line xxx;
2738 /* Why isn't this s_a_l called "sr_sal", like all of the
2739 other s_a_l's where this code is duplicated? */
2740 INIT_SAL (&xxx); /* initialize to zeroes */
2741 xxx.pc = tmp;
2742 xxx.section = find_pc_overlay (xxx.pc);
a0b3c4fd 2743 check_for_old_step_resume_breakpoint ();
c906108c 2744 step_resume_breakpoint =
c5aa993b
JM
2745 set_momentary_breakpoint (xxx, NULL, bp_step_resume);
2746 insert_breakpoints ();
d4f3574e
SS
2747 keep_going (ecs);
2748 return;
c906108c
SS
2749 }
2750 }
2751
c5aa993b
JM
2752 /* If we have line number information for the function we
2753 are thinking of stepping into, step into it.
c906108c 2754
c5aa993b
JM
2755 If there are several symtabs at that PC (e.g. with include
2756 files), just want to know whether *any* of them have line
2757 numbers. find_pc_line handles this. */
2758 {
2759 struct symtab_and_line tmp_sal;
c906108c 2760
c5aa993b
JM
2761 tmp_sal = find_pc_line (ecs->stop_func_start, 0);
2762 if (tmp_sal.line != 0)
c2c6d25f
JM
2763 {
2764 step_into_function (ecs);
2765 return;
2766 }
c906108c 2767 }
d4f3574e
SS
2768 step_over_function (ecs);
2769 keep_going (ecs);
2770 return;
c906108c 2771
c5aa993b 2772 }
c906108c 2773
c5aa993b 2774 /* We've wandered out of the step range. */
c906108c 2775
c5aa993b 2776 ecs->sal = find_pc_line (stop_pc, 0);
c906108c 2777
c5aa993b
JM
2778 if (step_range_end == 1)
2779 {
2780 /* It is stepi or nexti. We always want to stop stepping after
2781 one instruction. */
2782 stop_step = 1;
11cf8741 2783 print_stop_reason (END_STEPPING_RANGE, 0);
104c1213
JM
2784 stop_stepping (ecs);
2785 return;
c5aa993b 2786 }
c906108c 2787
c5aa993b
JM
2788 /* If we're in the return path from a shared library trampoline,
2789 we want to proceed through the trampoline when stepping. */
2790 if (IN_SOLIB_RETURN_TRAMPOLINE (stop_pc, ecs->stop_func_name))
2791 {
2792 CORE_ADDR tmp;
c906108c 2793
c5aa993b
JM
2794 /* Determine where this trampoline returns. */
2795 tmp = SKIP_TRAMPOLINE_CODE (stop_pc);
c906108c 2796
c5aa993b
JM
2797 /* Only proceed through if we know where it's going. */
2798 if (tmp)
2799 {
2800 /* And put the step-breakpoint there and go until there. */
2801 struct symtab_and_line sr_sal;
c906108c 2802
c5aa993b
JM
2803 INIT_SAL (&sr_sal); /* initialize to zeroes */
2804 sr_sal.pc = tmp;
2805 sr_sal.section = find_pc_overlay (sr_sal.pc);
2806 /* Do not specify what the fp should be when we stop
2807 since on some machines the prologue
2808 is where the new fp value is established. */
a0b3c4fd 2809 check_for_old_step_resume_breakpoint ();
c5aa993b
JM
2810 step_resume_breakpoint =
2811 set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
2812 if (breakpoints_inserted)
2813 insert_breakpoints ();
c906108c 2814
c5aa993b
JM
2815 /* Restart without fiddling with the step ranges or
2816 other state. */
d4f3574e
SS
2817 keep_going (ecs);
2818 return;
c5aa993b
JM
2819 }
2820 }
c906108c 2821
c5aa993b 2822 if (ecs->sal.line == 0)
c906108c 2823 {
c5aa993b
JM
2824 /* We have no line number information. That means to stop
2825 stepping (does this always happen right after one instruction,
2826 when we do "s" in a function with no line numbers,
2827 or can this happen as a result of a return or longjmp?). */
2828 stop_step = 1;
11cf8741 2829 print_stop_reason (END_STEPPING_RANGE, 0);
104c1213
JM
2830 stop_stepping (ecs);
2831 return;
c906108c
SS
2832 }
2833
c5aa993b
JM
2834 if ((stop_pc == ecs->sal.pc)
2835 && (ecs->current_line != ecs->sal.line || ecs->current_symtab != ecs->sal.symtab))
2836 {
2837 /* We are at the start of a different line. So stop. Note that
2838 we don't stop if we step into the middle of a different line.
2839 That is said to make things like for (;;) statements work
2840 better. */
2841 stop_step = 1;
11cf8741 2842 print_stop_reason (END_STEPPING_RANGE, 0);
104c1213
JM
2843 stop_stepping (ecs);
2844 return;
c5aa993b 2845 }
c906108c 2846
c5aa993b 2847 /* We aren't done stepping.
c906108c 2848
c5aa993b
JM
2849 Optimize by setting the stepping range to the line.
2850 (We might not be in the original line, but if we entered a
2851 new line in mid-statement, we continue stepping. This makes
2852 things like for(;;) statements work better.) */
c906108c 2853
c5aa993b
JM
2854 if (ecs->stop_func_end && ecs->sal.end >= ecs->stop_func_end)
2855 {
2856 /* If this is the last line of the function, don't keep stepping
2857 (it would probably step us out of the function).
2858 This is particularly necessary for a one-line function,
2859 in which after skipping the prologue we better stop even though
2860 we will be in mid-line. */
2861 stop_step = 1;
11cf8741 2862 print_stop_reason (END_STEPPING_RANGE, 0);
104c1213
JM
2863 stop_stepping (ecs);
2864 return;
c5aa993b
JM
2865 }
2866 step_range_start = ecs->sal.pc;
2867 step_range_end = ecs->sal.end;
2868 step_frame_address = FRAME_FP (get_current_frame ());
2869 ecs->current_line = ecs->sal.line;
2870 ecs->current_symtab = ecs->sal.symtab;
2871
2872 /* In the case where we just stepped out of a function into the middle
2873 of a line of the caller, continue stepping, but step_frame_address
2874 must be modified to current frame */
2875 {
2876 CORE_ADDR current_frame = FRAME_FP (get_current_frame ());
2877 if (!(INNER_THAN (current_frame, step_frame_address)))
2878 step_frame_address = current_frame;
2879 }
c906108c 2880
d4f3574e 2881 keep_going (ecs);
cd0fc7c3 2882
104c1213 2883 } /* extra brace, to preserve old indentation */
104c1213
JM
2884}
2885
2886/* Are we in the middle of stepping? */
2887
2888static int
2889currently_stepping (struct execution_control_state *ecs)
2890{
2891 return ((through_sigtramp_breakpoint == NULL
2892 && !ecs->handling_longjmp
2893 && ((step_range_end && step_resume_breakpoint == NULL)
2894 || trap_expected))
2895 || ecs->stepping_through_solib_after_catch
2896 || bpstat_should_step ());
2897}
c906108c 2898
104c1213
JM
2899static void
2900check_sigtramp2 (struct execution_control_state *ecs)
2901{
2902 if (trap_expected
2903 && IN_SIGTRAMP (stop_pc, ecs->stop_func_name)
2904 && !IN_SIGTRAMP (prev_pc, prev_func_name)
2905 && INNER_THAN (read_sp (), step_sp))
2906 {
2907 /* What has happened here is that we have just stepped the
2908 inferior with a signal (because it is a signal which
2909 shouldn't make us stop), thus stepping into sigtramp.
2910
2911 So we need to set a step_resume_break_address breakpoint and
2912 continue until we hit it, and then step. FIXME: This should
2913 be more enduring than a step_resume breakpoint; we should
2914 know that we will later need to keep going rather than
2915 re-hitting the breakpoint here (see the testsuite,
2916 gdb.base/signals.exp where it says "exceedingly difficult"). */
2917
2918 struct symtab_and_line sr_sal;
2919
2920 INIT_SAL (&sr_sal); /* initialize to zeroes */
2921 sr_sal.pc = prev_pc;
2922 sr_sal.section = find_pc_overlay (sr_sal.pc);
2923 /* We perhaps could set the frame if we kept track of what the
2924 frame corresponding to prev_pc was. But we don't, so don't. */
2925 through_sigtramp_breakpoint =
2926 set_momentary_breakpoint (sr_sal, NULL, bp_through_sigtramp);
2927 if (breakpoints_inserted)
2928 insert_breakpoints ();
cd0fc7c3 2929
104c1213
JM
2930 ecs->remove_breakpoints_on_following_step = 1;
2931 ecs->another_trap = 1;
2932 }
2933}
2934
c2c6d25f
JM
2935/* Subroutine call with source code we should not step over. Do step
2936 to the first line of code in it. */
2937
2938static void
2939step_into_function (struct execution_control_state *ecs)
2940{
2941 struct symtab *s;
2942 struct symtab_and_line sr_sal;
2943
2944 s = find_pc_symtab (stop_pc);
2945 if (s && s->language != language_asm)
2946 ecs->stop_func_start = SKIP_PROLOGUE (ecs->stop_func_start);
2947
2948 ecs->sal = find_pc_line (ecs->stop_func_start, 0);
2949 /* Use the step_resume_break to step until the end of the prologue,
2950 even if that involves jumps (as it seems to on the vax under
2951 4.2). */
2952 /* If the prologue ends in the middle of a source line, continue to
2953 the end of that source line (if it is still within the function).
2954 Otherwise, just go to end of prologue. */
2955#ifdef PROLOGUE_FIRSTLINE_OVERLAP
2956 /* no, don't either. It skips any code that's legitimately on the
2957 first line. */
2958#else
2959 if (ecs->sal.end
2960 && ecs->sal.pc != ecs->stop_func_start
2961 && ecs->sal.end < ecs->stop_func_end)
2962 ecs->stop_func_start = ecs->sal.end;
2963#endif
2964
2965 if (ecs->stop_func_start == stop_pc)
2966 {
2967 /* We are already there: stop now. */
2968 stop_step = 1;
11cf8741 2969 print_stop_reason (END_STEPPING_RANGE, 0);
c2c6d25f
JM
2970 stop_stepping (ecs);
2971 return;
2972 }
2973 else
2974 {
2975 /* Put the step-breakpoint there and go until there. */
2976 INIT_SAL (&sr_sal); /* initialize to zeroes */
2977 sr_sal.pc = ecs->stop_func_start;
2978 sr_sal.section = find_pc_overlay (ecs->stop_func_start);
2979 /* Do not specify what the fp should be when we stop since on
2980 some machines the prologue is where the new fp value is
2981 established. */
2982 check_for_old_step_resume_breakpoint ();
2983 step_resume_breakpoint =
2984 set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
2985 if (breakpoints_inserted)
2986 insert_breakpoints ();
2987
2988 /* And make sure stepping stops right away then. */
2989 step_range_end = step_range_start;
2990 }
2991 keep_going (ecs);
2992}
d4f3574e
SS
2993
2994/* We've just entered a callee, and we wish to resume until it returns
2995 to the caller. Setting a step_resume breakpoint on the return
2996 address will catch a return from the callee.
2997
2998 However, if the callee is recursing, we want to be careful not to
2999 catch returns of those recursive calls, but only of THIS instance
3000 of the call.
3001
3002 To do this, we set the step_resume bp's frame to our current
3003 caller's frame (step_frame_address, which is set by the "next" or
3004 "until" command, before execution begins). */
3005
3006static void
3007step_over_function (struct execution_control_state *ecs)
3008{
3009 struct symtab_and_line sr_sal;
3010
3011 INIT_SAL (&sr_sal); /* initialize to zeros */
3012 sr_sal.pc = ADDR_BITS_REMOVE (SAVED_PC_AFTER_CALL (get_current_frame ()));
3013 sr_sal.section = find_pc_overlay (sr_sal.pc);
3014
3015 check_for_old_step_resume_breakpoint ();
3016 step_resume_breakpoint =
3017 set_momentary_breakpoint (sr_sal, get_current_frame (), bp_step_resume);
3018
3019 if (!IN_SOLIB_DYNSYM_RESOLVE_CODE (sr_sal.pc))
3020 step_resume_breakpoint->frame = step_frame_address;
3021
3022 if (breakpoints_inserted)
3023 insert_breakpoints ();
3024}
3025
104c1213
JM
3026static void
3027stop_stepping (struct execution_control_state *ecs)
3028{
c906108c
SS
3029 if (target_has_execution)
3030 {
3031 /* Are we stopping for a vfork event? We only stop when we see
3032 the child's event. However, we may not yet have seen the
104c1213
JM
3033 parent's event. And, inferior_pid is still set to the
3034 parent's pid, until we resume again and follow either the
3035 parent or child.
c906108c
SS
3036
3037 To ensure that we can really touch inferior_pid (aka, the
3038 parent process) -- which calls to functions like read_pc
3039 implicitly do -- wait on the parent if necessary. */
3040 if ((pending_follow.kind == TARGET_WAITKIND_VFORKED)
3041 && !pending_follow.fork_event.saw_parent_fork)
3042 {
3043 int parent_pid;
3044
3045 do
3046 {
3047 if (target_wait_hook)
cd0fc7c3 3048 parent_pid = target_wait_hook (-1, &(ecs->ws));
c906108c 3049 else
cd0fc7c3 3050 parent_pid = target_wait (-1, &(ecs->ws));
c906108c
SS
3051 }
3052 while (parent_pid != inferior_pid);
3053 }
3054
c906108c 3055 /* Assuming the inferior still exists, set these up for next
c5aa993b
JM
3056 time, just like we did above if we didn't break out of the
3057 loop. */
c906108c 3058 prev_pc = read_pc ();
cd0fc7c3
SS
3059 prev_func_start = ecs->stop_func_start;
3060 prev_func_name = ecs->stop_func_name;
c906108c 3061 }
104c1213 3062
cd0fc7c3
SS
3063 /* Let callers know we don't want to wait for the inferior anymore. */
3064 ecs->wait_some_more = 0;
3065}
3066
d4f3574e
SS
3067/* This function handles various cases where we need to continue
3068 waiting for the inferior. */
3069/* (Used to be the keep_going: label in the old wait_for_inferior) */
3070
3071static void
3072keep_going (struct execution_control_state *ecs)
3073{
3074 /* ??rehrauer: ttrace on HP-UX theoretically allows one to debug a
3075 vforked child between its creation and subsequent exit or call to
3076 exec(). However, I had big problems in this rather creaky exec
3077 engine, getting that to work. The fundamental problem is that
3078 I'm trying to debug two processes via an engine that only
3079 understands a single process with possibly multiple threads.
3080
3081 Hence, this spot is known to have problems when
3082 target_can_follow_vfork_prior_to_exec returns 1. */
3083
3084 /* Save the pc before execution, to compare with pc after stop. */
3085 prev_pc = read_pc (); /* Might have been DECR_AFTER_BREAK */
3086 prev_func_start = ecs->stop_func_start; /* Ok, since if DECR_PC_AFTER
3087 BREAK is defined, the
3088 original pc would not have
3089 been at the start of a
3090 function. */
3091 prev_func_name = ecs->stop_func_name;
3092
3093 if (ecs->update_step_sp)
3094 step_sp = read_sp ();
3095 ecs->update_step_sp = 0;
3096
3097 /* If we did not do break;, it means we should keep running the
3098 inferior and not return to debugger. */
3099
3100 if (trap_expected && stop_signal != TARGET_SIGNAL_TRAP)
3101 {
3102 /* We took a signal (which we are supposed to pass through to
3103 the inferior, else we'd have done a break above) and we
3104 haven't yet gotten our trap. Simply continue. */
3105 resume (currently_stepping (ecs), stop_signal);
3106 }
3107 else
3108 {
3109 /* Either the trap was not expected, but we are continuing
3110 anyway (the user asked that this signal be passed to the
3111 child)
3112 -- or --
3113 The signal was SIGTRAP, e.g. it was our signal, but we
3114 decided we should resume from it.
3115
3116 We're going to run this baby now!
3117
3118 Insert breakpoints now, unless we are trying to one-proceed
3119 past a breakpoint. */
3120 /* If we've just finished a special step resume and we don't
3121 want to hit a breakpoint, pull em out. */
3122 if (step_resume_breakpoint == NULL
3123 && through_sigtramp_breakpoint == NULL
3124 && ecs->remove_breakpoints_on_following_step)
3125 {
3126 ecs->remove_breakpoints_on_following_step = 0;
3127 remove_breakpoints ();
3128 breakpoints_inserted = 0;
3129 }
3130 else if (!breakpoints_inserted &&
3131 (through_sigtramp_breakpoint != NULL || !ecs->another_trap))
3132 {
3133 breakpoints_failed = insert_breakpoints ();
3134 if (breakpoints_failed)
3135 {
3136 stop_stepping (ecs);
3137 return;
3138 }
3139 breakpoints_inserted = 1;
3140 }
3141
3142 trap_expected = ecs->another_trap;
3143
3144 /* Do not deliver SIGNAL_TRAP (except when the user explicitly
3145 specifies that such a signal should be delivered to the
3146 target program).
3147
3148 Typically, this would occure when a user is debugging a
3149 target monitor on a simulator: the target monitor sets a
3150 breakpoint; the simulator encounters this break-point and
3151 halts the simulation handing control to GDB; GDB, noteing
3152 that the break-point isn't valid, returns control back to the
3153 simulator; the simulator then delivers the hardware
3154 equivalent of a SIGNAL_TRAP to the program being debugged. */
3155
3156 if (stop_signal == TARGET_SIGNAL_TRAP
3157 && !signal_program[stop_signal])
3158 stop_signal = TARGET_SIGNAL_0;
3159
3160#ifdef SHIFT_INST_REGS
3161 /* I'm not sure when this following segment applies. I do know,
3162 now, that we shouldn't rewrite the regs when we were stopped
3163 by a random signal from the inferior process. */
3164 /* FIXME: Shouldn't this be based on the valid bit of the SXIP?
3165 (this is only used on the 88k). */
3166
3167 if (!bpstat_explains_signal (stop_bpstat)
3168 && (stop_signal != TARGET_SIGNAL_CHLD)
3169 && !stopped_by_random_signal)
3170 SHIFT_INST_REGS ();
3171#endif /* SHIFT_INST_REGS */
3172
3173 resume (currently_stepping (ecs), stop_signal);
3174 }
3175
3176 prepare_to_wait (ecs);
3177}
3178
104c1213
JM
3179/* This function normally comes after a resume, before
3180 handle_inferior_event exits. It takes care of any last bits of
3181 housekeeping, and sets the all-important wait_some_more flag. */
cd0fc7c3 3182
104c1213
JM
3183static void
3184prepare_to_wait (struct execution_control_state *ecs)
cd0fc7c3 3185{
104c1213
JM
3186 if (ecs->infwait_state == infwait_normal_state)
3187 {
3188 overlay_cache_invalid = 1;
3189
3190 /* We have to invalidate the registers BEFORE calling
3191 target_wait because they can be loaded from the target while
3192 in target_wait. This makes remote debugging a bit more
3193 efficient for those targets that provide critical registers
3194 as part of their normal status mechanism. */
3195
3196 registers_changed ();
3197 ecs->waiton_pid = -1;
3198 ecs->wp = &(ecs->ws);
3199 }
3200 /* This is the old end of the while loop. Let everybody know we
3201 want to wait for the inferior some more and get called again
3202 soon. */
3203 ecs->wait_some_more = 1;
c906108c 3204}
11cf8741
JM
3205
3206/* Print why the inferior has stopped. We always print something when
3207 the inferior exits, or receives a signal. The rest of the cases are
3208 dealt with later on in normal_stop() and print_it_typical(). Ideally
3209 there should be a call to this function from handle_inferior_event()
3210 each time stop_stepping() is called.*/
3211static void
3212print_stop_reason (enum inferior_stop_reason stop_reason, int stop_info)
3213{
3214 switch (stop_reason)
3215 {
3216 case STOP_UNKNOWN:
3217 /* We don't deal with these cases from handle_inferior_event()
3218 yet. */
3219 break;
3220 case END_STEPPING_RANGE:
3221 /* We are done with a step/next/si/ni command. */
3222 /* For now print nothing. */
3223 break;
3224 case BREAKPOINT_HIT:
3225 /* We found a breakpoint. */
3226 /* For now print nothing. */
3227 break;
3228 case SIGNAL_EXITED:
3229 /* The inferior was terminated by a signal. */
3230 annotate_signalled ();
3231 printf_filtered ("\nProgram terminated with signal ");
3232 annotate_signal_name ();
3233 printf_filtered ("%s", target_signal_to_name (stop_info));
3234 annotate_signal_name_end ();
3235 printf_filtered (", ");
3236 annotate_signal_string ();
3237 printf_filtered ("%s", target_signal_to_string (stop_info));
3238 annotate_signal_string_end ();
3239 printf_filtered (".\n");
3240
3241 printf_filtered ("The program no longer exists.\n");
3242 gdb_flush (gdb_stdout);
3243 break;
3244 case EXITED:
3245 /* The inferior program is finished. */
3246 annotate_exited (stop_info);
3247 if (stop_info)
3248 printf_filtered ("\nProgram exited with code 0%o.\n",
3249 (unsigned int) stop_info);
3250 else
3251 printf_filtered ("\nProgram exited normally.\n");
3252 break;
3253 case SIGNAL_RECEIVED:
3254 /* Signal received. The signal table tells us to print about
3255 it. */
3256 annotate_signal ();
3257 printf_filtered ("\nProgram received signal ");
3258 annotate_signal_name ();
3259 printf_filtered ("%s", target_signal_to_name (stop_info));
3260 annotate_signal_name_end ();
3261 printf_filtered (", ");
3262 annotate_signal_string ();
3263 printf_filtered ("%s", target_signal_to_string (stop_info));
3264 annotate_signal_string_end ();
3265 printf_filtered (".\n");
3266 gdb_flush (gdb_stdout);
3267 break;
3268 default:
3269 internal_error ("print_stop_reason: unrecognized enum value");
3270 break;
3271 }
3272}
c906108c 3273\f
43ff13b4 3274
c906108c
SS
3275/* Here to return control to GDB when the inferior stops for real.
3276 Print appropriate messages, remove breakpoints, give terminal our modes.
3277
3278 STOP_PRINT_FRAME nonzero means print the executing frame
3279 (pc, function, args, file, line number and line text).
3280 BREAKPOINTS_FAILED nonzero means stop was due to error
3281 attempting to insert breakpoints. */
3282
3283void
96baa820 3284normal_stop (void)
c906108c 3285{
c906108c
SS
3286 /* As with the notification of thread events, we want to delay
3287 notifying the user that we've switched thread context until
3288 the inferior actually stops.
3289
3290 (Note that there's no point in saying anything if the inferior
3291 has exited!) */
7a292a7a
SS
3292 if (may_switch_from_inferior_pid
3293 && (switched_from_inferior_pid != inferior_pid)
3294 && target_has_execution)
c906108c
SS
3295 {
3296 target_terminal_ours_for_output ();
3297 printf_filtered ("[Switched to %s]\n",
3298 target_pid_or_tid_to_str (inferior_pid));
3299 switched_from_inferior_pid = inferior_pid;
3300 }
c906108c
SS
3301
3302 /* Make sure that the current_frame's pc is correct. This
3303 is a correction for setting up the frame info before doing
3304 DECR_PC_AFTER_BREAK */
3305 if (target_has_execution && get_current_frame ())
3306 (get_current_frame ())->pc = read_pc ();
3307
3308 if (breakpoints_failed)
3309 {
3310 target_terminal_ours_for_output ();
3311 print_sys_errmsg ("ptrace", breakpoints_failed);
3312 printf_filtered ("Stopped; cannot insert breakpoints.\n\
3313The same program may be running in another process.\n");
3314 }
3315
3316 if (target_has_execution && breakpoints_inserted)
3317 {
3318 if (remove_breakpoints ())
3319 {
3320 target_terminal_ours_for_output ();
3321 printf_filtered ("Cannot remove breakpoints because ");
3322 printf_filtered ("program is no longer writable.\n");
3323 printf_filtered ("It might be running in another process.\n");
3324 printf_filtered ("Further execution is probably impossible.\n");
3325 }
3326 }
3327 breakpoints_inserted = 0;
3328
3329 /* Delete the breakpoint we stopped at, if it wants to be deleted.
3330 Delete any breakpoint that is to be deleted at the next stop. */
3331
3332 breakpoint_auto_delete (stop_bpstat);
3333
3334 /* If an auto-display called a function and that got a signal,
3335 delete that auto-display to avoid an infinite recursion. */
3336
3337 if (stopped_by_random_signal)
3338 disable_current_display ();
3339
3340 /* Don't print a message if in the middle of doing a "step n"
3341 operation for n > 1 */
3342 if (step_multi && stop_step)
3343 goto done;
3344
3345 target_terminal_ours ();
3346
c906108c
SS
3347 /* Look up the hook_stop and run it if it exists. */
3348
3349 if (stop_command && stop_command->hook)
3350 {
3351 catch_errors (hook_stop_stub, stop_command->hook,
3352 "Error while running hook_stop:\n", RETURN_MASK_ALL);
3353 }
3354
3355 if (!target_has_stack)
3356 {
3357
3358 goto done;
3359 }
3360
3361 /* Select innermost stack frame - i.e., current frame is frame 0,
3362 and current location is based on that.
3363 Don't do this on return from a stack dummy routine,
3364 or if the program has exited. */
3365
3366 if (!stop_stack_dummy)
3367 {
3368 select_frame (get_current_frame (), 0);
3369
3370 /* Print current location without a level number, if
c5aa993b
JM
3371 we have changed functions or hit a breakpoint.
3372 Print source line if we have one.
3373 bpstat_print() contains the logic deciding in detail
3374 what to print, based on the event(s) that just occurred. */
c906108c
SS
3375
3376 if (stop_print_frame)
3377 {
3378 int bpstat_ret;
3379 int source_flag;
917317f4 3380 int do_frame_printing = 1;
c906108c
SS
3381
3382 bpstat_ret = bpstat_print (stop_bpstat);
917317f4
JM
3383 switch (bpstat_ret)
3384 {
3385 case PRINT_UNKNOWN:
3386 if (stop_step
3387 && step_frame_address == FRAME_FP (get_current_frame ())
3388 && step_start_function == find_pc_function (stop_pc))
3389 source_flag = -1; /* finished step, just print source line */
3390 else
3391 source_flag = 1; /* print location and source line */
3392 break;
3393 case PRINT_SRC_AND_LOC:
3394 source_flag = 1; /* print location and source line */
3395 break;
3396 case PRINT_SRC_ONLY:
3397 source_flag = -1;
3398 break;
3399 case PRINT_NOTHING:
3400 do_frame_printing = 0;
3401 break;
3402 default:
3403 internal_error ("Unknown value.");
3404 }
c906108c
SS
3405
3406 /* The behavior of this routine with respect to the source
3407 flag is:
3408 -1: Print only source line
3409 0: Print only location
3410 1: Print location and source line */
917317f4
JM
3411 if (do_frame_printing)
3412 show_and_print_stack_frame (selected_frame, -1, source_flag);
c906108c
SS
3413
3414 /* Display the auto-display expressions. */
3415 do_displays ();
3416 }
3417 }
3418
3419 /* Save the function value return registers, if we care.
3420 We might be about to restore their previous contents. */
3421 if (proceed_to_finish)
3422 read_register_bytes (0, stop_registers, REGISTER_BYTES);
3423
3424 if (stop_stack_dummy)
3425 {
3426 /* Pop the empty frame that contains the stack dummy.
3427 POP_FRAME ends with a setting of the current frame, so we
c5aa993b 3428 can use that next. */
c906108c
SS
3429 POP_FRAME;
3430 /* Set stop_pc to what it was before we called the function.
c5aa993b
JM
3431 Can't rely on restore_inferior_status because that only gets
3432 called if we don't stop in the called function. */
c906108c
SS
3433 stop_pc = read_pc ();
3434 select_frame (get_current_frame (), 0);
3435 }
3436
3437
3438 TUIDO (((TuiOpaqueFuncPtr) tui_vCheckDataValues, selected_frame));
3439
3440done:
3441 annotate_stopped ();
3442}
3443
3444static int
96baa820 3445hook_stop_stub (void *cmd)
c906108c
SS
3446{
3447 execute_user_command ((struct cmd_list_element *) cmd, 0);
3448 return (0);
3449}
3450\f
c5aa993b 3451int
96baa820 3452signal_stop_state (int signo)
c906108c
SS
3453{
3454 return signal_stop[signo];
3455}
3456
c5aa993b 3457int
96baa820 3458signal_print_state (int signo)
c906108c
SS
3459{
3460 return signal_print[signo];
3461}
3462
c5aa993b 3463int
96baa820 3464signal_pass_state (int signo)
c906108c
SS
3465{
3466 return signal_program[signo];
3467}
3468
d4f3574e
SS
3469int signal_stop_update (signo, state)
3470 int signo;
3471 int state;
3472{
3473 int ret = signal_stop[signo];
3474 signal_stop[signo] = state;
3475 return ret;
3476}
3477
3478int signal_print_update (signo, state)
3479 int signo;
3480 int state;
3481{
3482 int ret = signal_print[signo];
3483 signal_print[signo] = state;
3484 return ret;
3485}
3486
3487int signal_pass_update (signo, state)
3488 int signo;
3489 int state;
3490{
3491 int ret = signal_program[signo];
3492 signal_program[signo] = state;
3493 return ret;
3494}
3495
c906108c 3496static void
96baa820 3497sig_print_header (void)
c906108c
SS
3498{
3499 printf_filtered ("\
3500Signal Stop\tPrint\tPass to program\tDescription\n");
3501}
3502
3503static void
96baa820 3504sig_print_info (enum target_signal oursig)
c906108c
SS
3505{
3506 char *name = target_signal_to_name (oursig);
3507 int name_padding = 13 - strlen (name);
96baa820 3508
c906108c
SS
3509 if (name_padding <= 0)
3510 name_padding = 0;
3511
3512 printf_filtered ("%s", name);
3513 printf_filtered ("%*.*s ", name_padding, name_padding,
3514 " ");
3515 printf_filtered ("%s\t", signal_stop[oursig] ? "Yes" : "No");
3516 printf_filtered ("%s\t", signal_print[oursig] ? "Yes" : "No");
3517 printf_filtered ("%s\t\t", signal_program[oursig] ? "Yes" : "No");
3518 printf_filtered ("%s\n", target_signal_to_string (oursig));
3519}
3520
3521/* Specify how various signals in the inferior should be handled. */
3522
3523static void
96baa820 3524handle_command (char *args, int from_tty)
c906108c
SS
3525{
3526 char **argv;
3527 int digits, wordlen;
3528 int sigfirst, signum, siglast;
3529 enum target_signal oursig;
3530 int allsigs;
3531 int nsigs;
3532 unsigned char *sigs;
3533 struct cleanup *old_chain;
3534
3535 if (args == NULL)
3536 {
3537 error_no_arg ("signal to handle");
3538 }
3539
3540 /* Allocate and zero an array of flags for which signals to handle. */
3541
3542 nsigs = (int) TARGET_SIGNAL_LAST;
3543 sigs = (unsigned char *) alloca (nsigs);
3544 memset (sigs, 0, nsigs);
3545
3546 /* Break the command line up into args. */
3547
3548 argv = buildargv (args);
3549 if (argv == NULL)
3550 {
3551 nomem (0);
3552 }
7a292a7a 3553 old_chain = make_cleanup_freeargv (argv);
c906108c
SS
3554
3555 /* Walk through the args, looking for signal oursigs, signal names, and
3556 actions. Signal numbers and signal names may be interspersed with
3557 actions, with the actions being performed for all signals cumulatively
3558 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
3559
3560 while (*argv != NULL)
3561 {
3562 wordlen = strlen (*argv);
3563 for (digits = 0; isdigit ((*argv)[digits]); digits++)
3564 {;
3565 }
3566 allsigs = 0;
3567 sigfirst = siglast = -1;
3568
3569 if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
3570 {
3571 /* Apply action to all signals except those used by the
3572 debugger. Silently skip those. */
3573 allsigs = 1;
3574 sigfirst = 0;
3575 siglast = nsigs - 1;
3576 }
3577 else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
3578 {
3579 SET_SIGS (nsigs, sigs, signal_stop);
3580 SET_SIGS (nsigs, sigs, signal_print);
3581 }
3582 else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
3583 {
3584 UNSET_SIGS (nsigs, sigs, signal_program);
3585 }
3586 else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
3587 {
3588 SET_SIGS (nsigs, sigs, signal_print);
3589 }
3590 else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
3591 {
3592 SET_SIGS (nsigs, sigs, signal_program);
3593 }
3594 else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
3595 {
3596 UNSET_SIGS (nsigs, sigs, signal_stop);
3597 }
3598 else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
3599 {
3600 SET_SIGS (nsigs, sigs, signal_program);
3601 }
3602 else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
3603 {
3604 UNSET_SIGS (nsigs, sigs, signal_print);
3605 UNSET_SIGS (nsigs, sigs, signal_stop);
3606 }
3607 else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
3608 {
3609 UNSET_SIGS (nsigs, sigs, signal_program);
3610 }
3611 else if (digits > 0)
3612 {
3613 /* It is numeric. The numeric signal refers to our own
3614 internal signal numbering from target.h, not to host/target
3615 signal number. This is a feature; users really should be
3616 using symbolic names anyway, and the common ones like
3617 SIGHUP, SIGINT, SIGALRM, etc. will work right anyway. */
3618
3619 sigfirst = siglast = (int)
3620 target_signal_from_command (atoi (*argv));
3621 if ((*argv)[digits] == '-')
3622 {
3623 siglast = (int)
3624 target_signal_from_command (atoi ((*argv) + digits + 1));
3625 }
3626 if (sigfirst > siglast)
3627 {
3628 /* Bet he didn't figure we'd think of this case... */
3629 signum = sigfirst;
3630 sigfirst = siglast;
3631 siglast = signum;
3632 }
3633 }
3634 else
3635 {
3636 oursig = target_signal_from_name (*argv);
3637 if (oursig != TARGET_SIGNAL_UNKNOWN)
3638 {
3639 sigfirst = siglast = (int) oursig;
3640 }
3641 else
3642 {
3643 /* Not a number and not a recognized flag word => complain. */
3644 error ("Unrecognized or ambiguous flag word: \"%s\".", *argv);
3645 }
3646 }
3647
3648 /* If any signal numbers or symbol names were found, set flags for
c5aa993b 3649 which signals to apply actions to. */
c906108c
SS
3650
3651 for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
3652 {
3653 switch ((enum target_signal) signum)
3654 {
3655 case TARGET_SIGNAL_TRAP:
3656 case TARGET_SIGNAL_INT:
3657 if (!allsigs && !sigs[signum])
3658 {
3659 if (query ("%s is used by the debugger.\n\
3660Are you sure you want to change it? ",
3661 target_signal_to_name
3662 ((enum target_signal) signum)))
3663 {
3664 sigs[signum] = 1;
3665 }
3666 else
3667 {
3668 printf_unfiltered ("Not confirmed, unchanged.\n");
3669 gdb_flush (gdb_stdout);
3670 }
3671 }
3672 break;
3673 case TARGET_SIGNAL_0:
3674 case TARGET_SIGNAL_DEFAULT:
3675 case TARGET_SIGNAL_UNKNOWN:
3676 /* Make sure that "all" doesn't print these. */
3677 break;
3678 default:
3679 sigs[signum] = 1;
3680 break;
3681 }
3682 }
3683
3684 argv++;
3685 }
3686
3687 target_notice_signals (inferior_pid);
3688
3689 if (from_tty)
3690 {
3691 /* Show the results. */
3692 sig_print_header ();
3693 for (signum = 0; signum < nsigs; signum++)
3694 {
3695 if (sigs[signum])
3696 {
3697 sig_print_info (signum);
3698 }
3699 }
3700 }
3701
3702 do_cleanups (old_chain);
3703}
3704
3705static void
96baa820 3706xdb_handle_command (char *args, int from_tty)
c906108c
SS
3707{
3708 char **argv;
3709 struct cleanup *old_chain;
3710
3711 /* Break the command line up into args. */
3712
3713 argv = buildargv (args);
3714 if (argv == NULL)
3715 {
3716 nomem (0);
3717 }
7a292a7a 3718 old_chain = make_cleanup_freeargv (argv);
c906108c
SS
3719 if (argv[1] != (char *) NULL)
3720 {
3721 char *argBuf;
3722 int bufLen;
3723
3724 bufLen = strlen (argv[0]) + 20;
3725 argBuf = (char *) xmalloc (bufLen);
3726 if (argBuf)
3727 {
3728 int validFlag = 1;
3729 enum target_signal oursig;
3730
3731 oursig = target_signal_from_name (argv[0]);
3732 memset (argBuf, 0, bufLen);
3733 if (strcmp (argv[1], "Q") == 0)
3734 sprintf (argBuf, "%s %s", argv[0], "noprint");
3735 else
3736 {
3737 if (strcmp (argv[1], "s") == 0)
3738 {
3739 if (!signal_stop[oursig])
3740 sprintf (argBuf, "%s %s", argv[0], "stop");
3741 else
3742 sprintf (argBuf, "%s %s", argv[0], "nostop");
3743 }
3744 else if (strcmp (argv[1], "i") == 0)
3745 {
3746 if (!signal_program[oursig])
3747 sprintf (argBuf, "%s %s", argv[0], "pass");
3748 else
3749 sprintf (argBuf, "%s %s", argv[0], "nopass");
3750 }
3751 else if (strcmp (argv[1], "r") == 0)
3752 {
3753 if (!signal_print[oursig])
3754 sprintf (argBuf, "%s %s", argv[0], "print");
3755 else
3756 sprintf (argBuf, "%s %s", argv[0], "noprint");
3757 }
3758 else
3759 validFlag = 0;
3760 }
3761 if (validFlag)
3762 handle_command (argBuf, from_tty);
3763 else
3764 printf_filtered ("Invalid signal handling flag.\n");
3765 if (argBuf)
3766 free (argBuf);
3767 }
3768 }
3769 do_cleanups (old_chain);
3770}
3771
3772/* Print current contents of the tables set by the handle command.
3773 It is possible we should just be printing signals actually used
3774 by the current target (but for things to work right when switching
3775 targets, all signals should be in the signal tables). */
3776
3777static void
96baa820 3778signals_info (char *signum_exp, int from_tty)
c906108c
SS
3779{
3780 enum target_signal oursig;
3781 sig_print_header ();
3782
3783 if (signum_exp)
3784 {
3785 /* First see if this is a symbol name. */
3786 oursig = target_signal_from_name (signum_exp);
3787 if (oursig == TARGET_SIGNAL_UNKNOWN)
3788 {
3789 /* No, try numeric. */
3790 oursig =
3791 target_signal_from_command (parse_and_eval_address (signum_exp));
3792 }
3793 sig_print_info (oursig);
3794 return;
3795 }
3796
3797 printf_filtered ("\n");
3798 /* These ugly casts brought to you by the native VAX compiler. */
3799 for (oursig = TARGET_SIGNAL_FIRST;
3800 (int) oursig < (int) TARGET_SIGNAL_LAST;
3801 oursig = (enum target_signal) ((int) oursig + 1))
3802 {
3803 QUIT;
3804
3805 if (oursig != TARGET_SIGNAL_UNKNOWN
3806 && oursig != TARGET_SIGNAL_DEFAULT
3807 && oursig != TARGET_SIGNAL_0)
3808 sig_print_info (oursig);
3809 }
3810
3811 printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
3812}
3813\f
7a292a7a
SS
3814struct inferior_status
3815{
3816 enum target_signal stop_signal;
3817 CORE_ADDR stop_pc;
3818 bpstat stop_bpstat;
3819 int stop_step;
3820 int stop_stack_dummy;
3821 int stopped_by_random_signal;
3822 int trap_expected;
3823 CORE_ADDR step_range_start;
3824 CORE_ADDR step_range_end;
3825 CORE_ADDR step_frame_address;
3826 int step_over_calls;
3827 CORE_ADDR step_resume_break_address;
3828 int stop_after_trap;
3829 int stop_soon_quietly;
3830 CORE_ADDR selected_frame_address;
3831 char *stop_registers;
3832
3833 /* These are here because if call_function_by_hand has written some
3834 registers and then decides to call error(), we better not have changed
3835 any registers. */
3836 char *registers;
3837
3838 int selected_level;
3839 int breakpoint_proceeded;
3840 int restore_stack_info;
3841 int proceed_to_finish;
3842};
3843
7a292a7a 3844static struct inferior_status *
96baa820 3845xmalloc_inferior_status (void)
7a292a7a
SS
3846{
3847 struct inferior_status *inf_status;
3848 inf_status = xmalloc (sizeof (struct inferior_status));
3849 inf_status->stop_registers = xmalloc (REGISTER_BYTES);
3850 inf_status->registers = xmalloc (REGISTER_BYTES);
3851 return inf_status;
3852}
3853
7a292a7a 3854static void
96baa820 3855free_inferior_status (struct inferior_status *inf_status)
7a292a7a
SS
3856{
3857 free (inf_status->registers);
3858 free (inf_status->stop_registers);
3859 free (inf_status);
3860}
3861
3862void
96baa820
JM
3863write_inferior_status_register (struct inferior_status *inf_status, int regno,
3864 LONGEST val)
7a292a7a 3865{
c5aa993b 3866 int size = REGISTER_RAW_SIZE (regno);
7a292a7a
SS
3867 void *buf = alloca (size);
3868 store_signed_integer (buf, size, val);
3869 memcpy (&inf_status->registers[REGISTER_BYTE (regno)], buf, size);
3870}
3871
c906108c
SS
3872/* Save all of the information associated with the inferior<==>gdb
3873 connection. INF_STATUS is a pointer to a "struct inferior_status"
3874 (defined in inferior.h). */
3875
7a292a7a 3876struct inferior_status *
96baa820 3877save_inferior_status (int restore_stack_info)
c906108c 3878{
7a292a7a
SS
3879 struct inferior_status *inf_status = xmalloc_inferior_status ();
3880
c906108c
SS
3881 inf_status->stop_signal = stop_signal;
3882 inf_status->stop_pc = stop_pc;
3883 inf_status->stop_step = stop_step;
3884 inf_status->stop_stack_dummy = stop_stack_dummy;
3885 inf_status->stopped_by_random_signal = stopped_by_random_signal;
3886 inf_status->trap_expected = trap_expected;
3887 inf_status->step_range_start = step_range_start;
3888 inf_status->step_range_end = step_range_end;
3889 inf_status->step_frame_address = step_frame_address;
3890 inf_status->step_over_calls = step_over_calls;
3891 inf_status->stop_after_trap = stop_after_trap;
3892 inf_status->stop_soon_quietly = stop_soon_quietly;
3893 /* Save original bpstat chain here; replace it with copy of chain.
3894 If caller's caller is walking the chain, they'll be happier if we
7a292a7a
SS
3895 hand them back the original chain when restore_inferior_status is
3896 called. */
c906108c
SS
3897 inf_status->stop_bpstat = stop_bpstat;
3898 stop_bpstat = bpstat_copy (stop_bpstat);
3899 inf_status->breakpoint_proceeded = breakpoint_proceeded;
3900 inf_status->restore_stack_info = restore_stack_info;
3901 inf_status->proceed_to_finish = proceed_to_finish;
c5aa993b 3902
c906108c
SS
3903 memcpy (inf_status->stop_registers, stop_registers, REGISTER_BYTES);
3904
3905 read_register_bytes (0, inf_status->registers, REGISTER_BYTES);
3906
3907 record_selected_frame (&(inf_status->selected_frame_address),
3908 &(inf_status->selected_level));
7a292a7a 3909 return inf_status;
c906108c
SS
3910}
3911
3912struct restore_selected_frame_args
3913{
3914 CORE_ADDR frame_address;
3915 int level;
3916};
3917
c906108c 3918static int
96baa820 3919restore_selected_frame (void *args)
c906108c
SS
3920{
3921 struct restore_selected_frame_args *fr =
3922 (struct restore_selected_frame_args *) args;
3923 struct frame_info *frame;
3924 int level = fr->level;
3925
3926 frame = find_relative_frame (get_current_frame (), &level);
3927
3928 /* If inf_status->selected_frame_address is NULL, there was no
3929 previously selected frame. */
3930 if (frame == NULL ||
3931 /* FRAME_FP (frame) != fr->frame_address || */
3932 /* elz: deleted this check as a quick fix to the problem that
c5aa993b
JM
3933 for function called by hand gdb creates no internal frame
3934 structure and the real stack and gdb's idea of stack are
3935 different if nested calls by hands are made.
c906108c 3936
c5aa993b 3937 mvs: this worries me. */
c906108c
SS
3938 level != 0)
3939 {
3940 warning ("Unable to restore previously selected frame.\n");
3941 return 0;
3942 }
3943
3944 select_frame (frame, fr->level);
3945
3946 return (1);
3947}
3948
3949void
96baa820 3950restore_inferior_status (struct inferior_status *inf_status)
c906108c
SS
3951{
3952 stop_signal = inf_status->stop_signal;
3953 stop_pc = inf_status->stop_pc;
3954 stop_step = inf_status->stop_step;
3955 stop_stack_dummy = inf_status->stop_stack_dummy;
3956 stopped_by_random_signal = inf_status->stopped_by_random_signal;
3957 trap_expected = inf_status->trap_expected;
3958 step_range_start = inf_status->step_range_start;
3959 step_range_end = inf_status->step_range_end;
3960 step_frame_address = inf_status->step_frame_address;
3961 step_over_calls = inf_status->step_over_calls;
3962 stop_after_trap = inf_status->stop_after_trap;
3963 stop_soon_quietly = inf_status->stop_soon_quietly;
3964 bpstat_clear (&stop_bpstat);
3965 stop_bpstat = inf_status->stop_bpstat;
3966 breakpoint_proceeded = inf_status->breakpoint_proceeded;
3967 proceed_to_finish = inf_status->proceed_to_finish;
3968
7a292a7a 3969 /* FIXME: Is the restore of stop_registers always needed */
c906108c
SS
3970 memcpy (stop_registers, inf_status->stop_registers, REGISTER_BYTES);
3971
3972 /* The inferior can be gone if the user types "print exit(0)"
3973 (and perhaps other times). */
3974 if (target_has_execution)
3975 write_register_bytes (0, inf_status->registers, REGISTER_BYTES);
3976
c906108c
SS
3977 /* FIXME: If we are being called after stopping in a function which
3978 is called from gdb, we should not be trying to restore the
3979 selected frame; it just prints a spurious error message (The
3980 message is useful, however, in detecting bugs in gdb (like if gdb
3981 clobbers the stack)). In fact, should we be restoring the
3982 inferior status at all in that case? . */
3983
3984 if (target_has_stack && inf_status->restore_stack_info)
3985 {
3986 struct restore_selected_frame_args fr;
3987 fr.level = inf_status->selected_level;
3988 fr.frame_address = inf_status->selected_frame_address;
3989 /* The point of catch_errors is that if the stack is clobbered,
c5aa993b
JM
3990 walking the stack might encounter a garbage pointer and error()
3991 trying to dereference it. */
c906108c
SS
3992 if (catch_errors (restore_selected_frame, &fr,
3993 "Unable to restore previously selected frame:\n",
3994 RETURN_MASK_ERROR) == 0)
3995 /* Error in restoring the selected frame. Select the innermost
3996 frame. */
3997
3998
3999 select_frame (get_current_frame (), 0);
4000
4001 }
c906108c 4002
7a292a7a
SS
4003 free_inferior_status (inf_status);
4004}
c906108c
SS
4005
4006void
96baa820 4007discard_inferior_status (struct inferior_status *inf_status)
7a292a7a
SS
4008{
4009 /* See save_inferior_status for info on stop_bpstat. */
4010 bpstat_clear (&inf_status->stop_bpstat);
4011 free_inferior_status (inf_status);
4012}
4013
4014static void
96baa820
JM
4015set_follow_fork_mode_command (char *arg, int from_tty,
4016 struct cmd_list_element *c)
c906108c
SS
4017{
4018 if (!STREQ (arg, "parent") &&
4019 !STREQ (arg, "child") &&
4020 !STREQ (arg, "both") &&
4021 !STREQ (arg, "ask"))
4022 error ("follow-fork-mode must be one of \"parent\", \"child\", \"both\" or \"ask\".");
4023
4024 if (follow_fork_mode_string != NULL)
4025 free (follow_fork_mode_string);
4026 follow_fork_mode_string = savestring (arg, strlen (arg));
4027}
c5aa993b 4028\f
7a292a7a 4029static void
96baa820 4030build_infrun (void)
7a292a7a
SS
4031{
4032 stop_registers = xmalloc (REGISTER_BYTES);
4033}
c906108c 4034
c906108c 4035void
96baa820 4036_initialize_infrun (void)
c906108c
SS
4037{
4038 register int i;
4039 register int numsigs;
4040 struct cmd_list_element *c;
4041
7a292a7a
SS
4042 build_infrun ();
4043
0f71a2f6
JM
4044 register_gdbarch_swap (&stop_registers, sizeof (stop_registers), NULL);
4045 register_gdbarch_swap (NULL, 0, build_infrun);
4046
c906108c
SS
4047 add_info ("signals", signals_info,
4048 "What debugger does when program gets various signals.\n\
4049Specify a signal as argument to print info on that signal only.");
4050 add_info_alias ("handle", "signals", 0);
4051
4052 add_com ("handle", class_run, handle_command,
4053 concat ("Specify how to handle a signal.\n\
4054Args are signals and actions to apply to those signals.\n\
4055Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
4056from 1-15 are allowed for compatibility with old versions of GDB.\n\
4057Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
4058The special arg \"all\" is recognized to mean all signals except those\n\
4059used by the debugger, typically SIGTRAP and SIGINT.\n",
4060 "Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
4061\"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
4062Stop means reenter debugger if this signal happens (implies print).\n\
4063Print means print a message if this signal happens.\n\
4064Pass means let program see this signal; otherwise program doesn't know.\n\
4065Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
4066Pass and Stop may be combined.", NULL));
4067 if (xdb_commands)
4068 {
4069 add_com ("lz", class_info, signals_info,
4070 "What debugger does when program gets various signals.\n\
4071Specify a signal as argument to print info on that signal only.");
4072 add_com ("z", class_run, xdb_handle_command,
4073 concat ("Specify how to handle a signal.\n\
4074Args are signals and actions to apply to those signals.\n\
4075Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
4076from 1-15 are allowed for compatibility with old versions of GDB.\n\
4077Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
4078The special arg \"all\" is recognized to mean all signals except those\n\
4079used by the debugger, typically SIGTRAP and SIGINT.\n",
4080 "Recognized actions include \"s\" (toggles between stop and nostop), \n\
4081\"r\" (toggles between print and noprint), \"i\" (toggles between pass and \
4082nopass), \"Q\" (noprint)\n\
4083Stop means reenter debugger if this signal happens (implies print).\n\
4084Print means print a message if this signal happens.\n\
4085Pass means let program see this signal; otherwise program doesn't know.\n\
4086Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
4087Pass and Stop may be combined.", NULL));
4088 }
4089
4090 if (!dbx_commands)
4091 stop_command = add_cmd ("stop", class_obscure, not_just_help_class_command,
4092 "There is no `stop' command, but you can set a hook on `stop'.\n\
4093This allows you to set a list of commands to be run each time execution\n\
4094of the program stops.", &cmdlist);
4095
4096 numsigs = (int) TARGET_SIGNAL_LAST;
4097 signal_stop = (unsigned char *)
4098 xmalloc (sizeof (signal_stop[0]) * numsigs);
4099 signal_print = (unsigned char *)
4100 xmalloc (sizeof (signal_print[0]) * numsigs);
4101 signal_program = (unsigned char *)
4102 xmalloc (sizeof (signal_program[0]) * numsigs);
4103 for (i = 0; i < numsigs; i++)
4104 {
4105 signal_stop[i] = 1;
4106 signal_print[i] = 1;
4107 signal_program[i] = 1;
4108 }
4109
4110 /* Signals caused by debugger's own actions
4111 should not be given to the program afterwards. */
4112 signal_program[TARGET_SIGNAL_TRAP] = 0;
4113 signal_program[TARGET_SIGNAL_INT] = 0;
4114
4115 /* Signals that are not errors should not normally enter the debugger. */
4116 signal_stop[TARGET_SIGNAL_ALRM] = 0;
4117 signal_print[TARGET_SIGNAL_ALRM] = 0;
4118 signal_stop[TARGET_SIGNAL_VTALRM] = 0;
4119 signal_print[TARGET_SIGNAL_VTALRM] = 0;
4120 signal_stop[TARGET_SIGNAL_PROF] = 0;
4121 signal_print[TARGET_SIGNAL_PROF] = 0;
4122 signal_stop[TARGET_SIGNAL_CHLD] = 0;
4123 signal_print[TARGET_SIGNAL_CHLD] = 0;
4124 signal_stop[TARGET_SIGNAL_IO] = 0;
4125 signal_print[TARGET_SIGNAL_IO] = 0;
4126 signal_stop[TARGET_SIGNAL_POLL] = 0;
4127 signal_print[TARGET_SIGNAL_POLL] = 0;
4128 signal_stop[TARGET_SIGNAL_URG] = 0;
4129 signal_print[TARGET_SIGNAL_URG] = 0;
4130 signal_stop[TARGET_SIGNAL_WINCH] = 0;
4131 signal_print[TARGET_SIGNAL_WINCH] = 0;
4132
cd0fc7c3
SS
4133 /* These signals are used internally by user-level thread
4134 implementations. (See signal(5) on Solaris.) Like the above
4135 signals, a healthy program receives and handles them as part of
4136 its normal operation. */
4137 signal_stop[TARGET_SIGNAL_LWP] = 0;
4138 signal_print[TARGET_SIGNAL_LWP] = 0;
4139 signal_stop[TARGET_SIGNAL_WAITING] = 0;
4140 signal_print[TARGET_SIGNAL_WAITING] = 0;
4141 signal_stop[TARGET_SIGNAL_CANCEL] = 0;
4142 signal_print[TARGET_SIGNAL_CANCEL] = 0;
4143
c906108c
SS
4144#ifdef SOLIB_ADD
4145 add_show_from_set
4146 (add_set_cmd ("stop-on-solib-events", class_support, var_zinteger,
4147 (char *) &stop_on_solib_events,
4148 "Set stopping for shared library events.\n\
4149If nonzero, gdb will give control to the user when the dynamic linker\n\
4150notifies gdb of shared library events. The most common event of interest\n\
4151to the user would be loading/unloading of a new library.\n",
4152 &setlist),
4153 &showlist);
4154#endif
4155
4156 c = add_set_enum_cmd ("follow-fork-mode",
4157 class_run,
4158 follow_fork_mode_kind_names,
4159 (char *) &follow_fork_mode_string,
4160/* ??rehrauer: The "both" option is broken, by what may be a 10.20
4161 kernel problem. It's also not terribly useful without a GUI to
4162 help the user drive two debuggers. So for now, I'm disabling
4163 the "both" option. */
c5aa993b
JM
4164/* "Set debugger response to a program call of fork \
4165 or vfork.\n\
4166 A fork or vfork creates a new process. follow-fork-mode can be:\n\
4167 parent - the original process is debugged after a fork\n\
4168 child - the new process is debugged after a fork\n\
4169 both - both the parent and child are debugged after a fork\n\
4170 ask - the debugger will ask for one of the above choices\n\
4171 For \"both\", another copy of the debugger will be started to follow\n\
4172 the new child process. The original debugger will continue to follow\n\
4173 the original parent process. To distinguish their prompts, the\n\
4174 debugger copy's prompt will be changed.\n\
4175 For \"parent\" or \"child\", the unfollowed process will run free.\n\
4176 By default, the debugger will follow the parent process.",
4177 */
c906108c
SS
4178 "Set debugger response to a program call of fork \
4179or vfork.\n\
4180A fork or vfork creates a new process. follow-fork-mode can be:\n\
4181 parent - the original process is debugged after a fork\n\
4182 child - the new process is debugged after a fork\n\
4183 ask - the debugger will ask for one of the above choices\n\
4184For \"parent\" or \"child\", the unfollowed process will run free.\n\
4185By default, the debugger will follow the parent process.",
4186 &setlist);
c5aa993b 4187/* c->function.sfunc = ; */
c906108c
SS
4188 add_show_from_set (c, &showlist);
4189
4190 set_follow_fork_mode_command ("parent", 0, NULL);
4191
4192 c = add_set_enum_cmd ("scheduler-locking", class_run,
4193 scheduler_enums, /* array of string names */
4194 (char *) &scheduler_mode, /* current mode */
4195 "Set mode for locking scheduler during execution.\n\
4196off == no locking (threads may preempt at any time)\n\
4197on == full locking (no thread except the current thread may run)\n\
4198step == scheduler locked during every single-step operation.\n\
4199 In this mode, no other thread may run during a step command.\n\
4200 Other threads may run while stepping over a function call ('next').",
4201 &setlist);
4202
4203 c->function.sfunc = set_schedlock_func; /* traps on target vector */
4204 add_show_from_set (c, &showlist);
4205}
This page took 0.221013 seconds and 4 git commands to generate.