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