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