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