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