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