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