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