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