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