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