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