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