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