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