2001-06-01 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_breakpoint_current_contents,
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_breakpoint_current_contents,
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
2006 /* Load infrun state for the new thread. */
2007 load_infrun_state (ecs->ptid, &prev_pc,
2008 &prev_func_start, &prev_func_name,
2009 &trap_expected, &step_resume_breakpoint,
2010 &through_sigtramp_breakpoint,
2011 &step_range_start, &step_range_end,
2012 &step_frame_address, &ecs->handling_longjmp,
2013 &ecs->another_trap,
2014 &ecs->stepping_through_solib_after_catch,
2015 &ecs->stepping_through_solib_catchpoints,
2016 &ecs->stepping_through_sigtramp);
2017 }
2018
2019 inferior_ptid = ecs->ptid;
2020
2021 if (context_hook)
2022 context_hook (pid_to_thread_id (ecs->ptid));
2023
2024 flush_cached_frames ();
2025 }
2026
2027 if (SOFTWARE_SINGLE_STEP_P () && singlestep_breakpoints_inserted_p)
2028 {
2029 /* Pull the single step breakpoints out of the target. */
2030 SOFTWARE_SINGLE_STEP (0, 0);
2031 singlestep_breakpoints_inserted_p = 0;
2032 }
2033
2034 /* If PC is pointing at a nullified instruction, then step beyond
2035 it so that the user won't be confused when GDB appears to be ready
2036 to execute it. */
2037
2038 /* if (INSTRUCTION_NULLIFIED && currently_stepping (ecs)) */
2039 if (INSTRUCTION_NULLIFIED)
2040 {
2041 registers_changed ();
2042 target_resume (ecs->ptid, 1, TARGET_SIGNAL_0);
2043
2044 /* We may have received a signal that we want to pass to
2045 the inferior; therefore, we must not clobber the waitstatus
2046 in WS. */
2047
2048 ecs->infwait_state = infwait_nullified_state;
2049 ecs->waiton_ptid = ecs->ptid;
2050 ecs->wp = &(ecs->tmpstatus);
2051 prepare_to_wait (ecs);
2052 return;
2053 }
2054
2055 /* It may not be necessary to disable the watchpoint to stop over
2056 it. For example, the PA can (with some kernel cooperation)
2057 single step over a watchpoint without disabling the watchpoint. */
2058 if (HAVE_STEPPABLE_WATCHPOINT && STOPPED_BY_WATCHPOINT (ecs->ws))
2059 {
2060 resume (1, 0);
2061 prepare_to_wait (ecs);
2062 return;
2063 }
2064
2065 /* It is far more common to need to disable a watchpoint to step
2066 the inferior over it. FIXME. What else might a debug
2067 register or page protection watchpoint scheme need here? */
2068 if (HAVE_NONSTEPPABLE_WATCHPOINT && STOPPED_BY_WATCHPOINT (ecs->ws))
2069 {
2070 /* At this point, we are stopped at an instruction which has
2071 attempted to write to a piece of memory under control of
2072 a watchpoint. The instruction hasn't actually executed
2073 yet. If we were to evaluate the watchpoint expression
2074 now, we would get the old value, and therefore no change
2075 would seem to have occurred.
2076
2077 In order to make watchpoints work `right', we really need
2078 to complete the memory write, and then evaluate the
2079 watchpoint expression. The following code does that by
2080 removing the watchpoint (actually, all watchpoints and
2081 breakpoints), single-stepping the target, re-inserting
2082 watchpoints, and then falling through to let normal
2083 single-step processing handle proceed. Since this
2084 includes evaluating watchpoints, things will come to a
2085 stop in the correct manner. */
2086
2087 write_pc (stop_pc - DECR_PC_AFTER_BREAK);
2088
2089 remove_breakpoints ();
2090 registers_changed ();
2091 target_resume (ecs->ptid, 1, TARGET_SIGNAL_0); /* Single step */
2092
2093 ecs->waiton_ptid = ecs->ptid;
2094 ecs->wp = &(ecs->ws);
2095 ecs->infwait_state = infwait_nonstep_watch_state;
2096 prepare_to_wait (ecs);
2097 return;
2098 }
2099
2100 /* It may be possible to simply continue after a watchpoint. */
2101 if (HAVE_CONTINUABLE_WATCHPOINT)
2102 STOPPED_BY_WATCHPOINT (ecs->ws);
2103
2104 ecs->stop_func_start = 0;
2105 ecs->stop_func_end = 0;
2106 ecs->stop_func_name = 0;
2107 /* Don't care about return value; stop_func_start and stop_func_name
2108 will both be 0 if it doesn't work. */
2109 find_pc_partial_function (stop_pc, &ecs->stop_func_name,
2110 &ecs->stop_func_start, &ecs->stop_func_end);
2111 ecs->stop_func_start += FUNCTION_START_OFFSET;
2112 ecs->another_trap = 0;
2113 bpstat_clear (&stop_bpstat);
2114 stop_step = 0;
2115 stop_stack_dummy = 0;
2116 stop_print_frame = 1;
2117 ecs->random_signal = 0;
2118 stopped_by_random_signal = 0;
2119 breakpoints_failed = 0;
2120
2121 /* Look at the cause of the stop, and decide what to do.
2122 The alternatives are:
2123 1) break; to really stop and return to the debugger,
2124 2) drop through to start up again
2125 (set ecs->another_trap to 1 to single step once)
2126 3) set ecs->random_signal to 1, and the decision between 1 and 2
2127 will be made according to the signal handling tables. */
2128
2129 /* First, distinguish signals caused by the debugger from signals
2130 that have to do with the program's own actions.
2131 Note that breakpoint insns may cause SIGTRAP or SIGILL
2132 or SIGEMT, depending on the operating system version.
2133 Here we detect when a SIGILL or SIGEMT is really a breakpoint
2134 and change it to SIGTRAP. */
2135
2136 if (stop_signal == TARGET_SIGNAL_TRAP
2137 || (breakpoints_inserted &&
2138 (stop_signal == TARGET_SIGNAL_ILL
2139 || stop_signal == TARGET_SIGNAL_EMT
2140 ))
2141 || stop_soon_quietly)
2142 {
2143 if (stop_signal == TARGET_SIGNAL_TRAP && stop_after_trap)
2144 {
2145 stop_print_frame = 0;
2146 stop_stepping (ecs);
2147 return;
2148 }
2149 if (stop_soon_quietly)
2150 {
2151 stop_stepping (ecs);
2152 return;
2153 }
2154
2155 /* Don't even think about breakpoints
2156 if just proceeded over a breakpoint.
2157
2158 However, if we are trying to proceed over a breakpoint
2159 and end up in sigtramp, then through_sigtramp_breakpoint
2160 will be set and we should check whether we've hit the
2161 step breakpoint. */
2162 if (stop_signal == TARGET_SIGNAL_TRAP && trap_expected
2163 && through_sigtramp_breakpoint == NULL)
2164 bpstat_clear (&stop_bpstat);
2165 else
2166 {
2167 /* See if there is a breakpoint at the current PC. */
2168 stop_bpstat = bpstat_stop_status
2169 (&stop_pc,
2170 /* Pass TRUE if our reason for stopping is something other
2171 than hitting a breakpoint. We do this by checking that
2172 1) stepping is going on and 2) we didn't hit a breakpoint
2173 in a signal handler without an intervening stop in
2174 sigtramp, which is detected by a new stack pointer value
2175 below any usual function calling stack adjustments. */
2176 (currently_stepping (ecs)
2177 && !(step_range_end
2178 && INNER_THAN (read_sp (), (step_sp - 16))))
2179 );
2180 /* Following in case break condition called a
2181 function. */
2182 stop_print_frame = 1;
2183 }
2184
2185 if (stop_signal == TARGET_SIGNAL_TRAP)
2186 ecs->random_signal
2187 = !(bpstat_explains_signal (stop_bpstat)
2188 || trap_expected
2189 || (!CALL_DUMMY_BREAKPOINT_OFFSET_P
2190 && PC_IN_CALL_DUMMY (stop_pc, read_sp (),
2191 FRAME_FP (get_current_frame ())))
2192 || (step_range_end && step_resume_breakpoint == NULL));
2193
2194 else
2195 {
2196 ecs->random_signal
2197 = !(bpstat_explains_signal (stop_bpstat)
2198 /* End of a stack dummy. Some systems (e.g. Sony
2199 news) give another signal besides SIGTRAP, so
2200 check here as well as above. */
2201 || (!CALL_DUMMY_BREAKPOINT_OFFSET_P
2202 && PC_IN_CALL_DUMMY (stop_pc, read_sp (),
2203 FRAME_FP (get_current_frame ())))
2204 );
2205 if (!ecs->random_signal)
2206 stop_signal = TARGET_SIGNAL_TRAP;
2207 }
2208 }
2209
2210 /* When we reach this point, we've pretty much decided
2211 that the reason for stopping must've been a random
2212 (unexpected) signal. */
2213
2214 else
2215 ecs->random_signal = 1;
2216 /* If a fork, vfork or exec event was seen, then there are two
2217 possible responses we can make:
2218
2219 1. If a catchpoint triggers for the event (ecs->random_signal == 0),
2220 then we must stop now and issue a prompt. We will resume
2221 the inferior when the user tells us to.
2222 2. If no catchpoint triggers for the event (ecs->random_signal == 1),
2223 then we must resume the inferior now and keep checking.
2224
2225 In either case, we must take appropriate steps to "follow" the
2226 the fork/vfork/exec when the inferior is resumed. For example,
2227 if follow-fork-mode is "child", then we must detach from the
2228 parent inferior and follow the new child inferior.
2229
2230 In either case, setting pending_follow causes the next resume()
2231 to take the appropriate following action. */
2232 process_event_stop_test:
2233 if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
2234 {
2235 if (ecs->random_signal) /* I.e., no catchpoint triggered for this. */
2236 {
2237 trap_expected = 1;
2238 stop_signal = TARGET_SIGNAL_0;
2239 keep_going (ecs);
2240 return;
2241 }
2242 }
2243 else if (ecs->ws.kind == TARGET_WAITKIND_VFORKED)
2244 {
2245 if (ecs->random_signal) /* I.e., no catchpoint triggered for this. */
2246 {
2247 stop_signal = TARGET_SIGNAL_0;
2248 keep_going (ecs);
2249 return;
2250 }
2251 }
2252 else if (ecs->ws.kind == TARGET_WAITKIND_EXECD)
2253 {
2254 pending_follow.kind = ecs->ws.kind;
2255 if (ecs->random_signal) /* I.e., no catchpoint triggered for this. */
2256 {
2257 trap_expected = 1;
2258 stop_signal = TARGET_SIGNAL_0;
2259 keep_going (ecs);
2260 return;
2261 }
2262 }
2263
2264 /* For the program's own signals, act according to
2265 the signal handling tables. */
2266
2267 if (ecs->random_signal)
2268 {
2269 /* Signal not for debugging purposes. */
2270 int printed = 0;
2271
2272 stopped_by_random_signal = 1;
2273
2274 if (signal_print[stop_signal])
2275 {
2276 printed = 1;
2277 target_terminal_ours_for_output ();
2278 print_stop_reason (SIGNAL_RECEIVED, stop_signal);
2279 }
2280 if (signal_stop[stop_signal])
2281 {
2282 stop_stepping (ecs);
2283 return;
2284 }
2285 /* If not going to stop, give terminal back
2286 if we took it away. */
2287 else if (printed)
2288 target_terminal_inferior ();
2289
2290 /* Clear the signal if it should not be passed. */
2291 if (signal_program[stop_signal] == 0)
2292 stop_signal = TARGET_SIGNAL_0;
2293
2294 /* I'm not sure whether this needs to be check_sigtramp2 or
2295 whether it could/should be keep_going.
2296
2297 This used to jump to step_over_function if we are stepping,
2298 which is wrong.
2299
2300 Suppose the user does a `next' over a function call, and while
2301 that call is in progress, the inferior receives a signal for
2302 which GDB does not stop (i.e., signal_stop[SIG] is false). In
2303 that case, when we reach this point, there is already a
2304 step-resume breakpoint established, right where it should be:
2305 immediately after the function call the user is "next"-ing
2306 over. If we call step_over_function now, two bad things
2307 happen:
2308
2309 - we'll create a new breakpoint, at wherever the current
2310 frame's return address happens to be. That could be
2311 anywhere, depending on what function call happens to be on
2312 the top of the stack at that point. Point is, it's probably
2313 not where we need it.
2314
2315 - the existing step-resume breakpoint (which is at the correct
2316 address) will get orphaned: step_resume_breakpoint will point
2317 to the new breakpoint, and the old step-resume breakpoint
2318 will never be cleaned up.
2319
2320 The old behavior was meant to help HP-UX single-step out of
2321 sigtramps. It would place the new breakpoint at prev_pc, which
2322 was certainly wrong. I don't know the details there, so fixing
2323 this probably breaks that. As with anything else, it's up to
2324 the HP-UX maintainer to furnish a fix that doesn't break other
2325 platforms. --JimB, 20 May 1999 */
2326 check_sigtramp2 (ecs);
2327 keep_going (ecs);
2328 return;
2329 }
2330
2331 /* Handle cases caused by hitting a breakpoint. */
2332 {
2333 CORE_ADDR jmp_buf_pc;
2334 struct bpstat_what what;
2335
2336 what = bpstat_what (stop_bpstat);
2337
2338 if (what.call_dummy)
2339 {
2340 stop_stack_dummy = 1;
2341 #ifdef HP_OS_BUG
2342 trap_expected_after_continue = 1;
2343 #endif
2344 }
2345
2346 switch (what.main_action)
2347 {
2348 case BPSTAT_WHAT_SET_LONGJMP_RESUME:
2349 /* If we hit the breakpoint at longjmp, disable it for the
2350 duration of this command. Then, install a temporary
2351 breakpoint at the target of the jmp_buf. */
2352 disable_longjmp_breakpoint ();
2353 remove_breakpoints ();
2354 breakpoints_inserted = 0;
2355 if (!GET_LONGJMP_TARGET (&jmp_buf_pc))
2356 {
2357 keep_going (ecs);
2358 return;
2359 }
2360
2361 /* Need to blow away step-resume breakpoint, as it
2362 interferes with us */
2363 if (step_resume_breakpoint != NULL)
2364 {
2365 delete_breakpoint (step_resume_breakpoint);
2366 step_resume_breakpoint = NULL;
2367 }
2368 /* Not sure whether we need to blow this away too, but probably
2369 it is like the step-resume breakpoint. */
2370 if (through_sigtramp_breakpoint != NULL)
2371 {
2372 delete_breakpoint (through_sigtramp_breakpoint);
2373 through_sigtramp_breakpoint = NULL;
2374 }
2375
2376 #if 0
2377 /* FIXME - Need to implement nested temporary breakpoints */
2378 if (step_over_calls > 0)
2379 set_longjmp_resume_breakpoint (jmp_buf_pc,
2380 get_current_frame ());
2381 else
2382 #endif /* 0 */
2383 set_longjmp_resume_breakpoint (jmp_buf_pc, NULL);
2384 ecs->handling_longjmp = 1; /* FIXME */
2385 keep_going (ecs);
2386 return;
2387
2388 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
2389 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME_SINGLE:
2390 remove_breakpoints ();
2391 breakpoints_inserted = 0;
2392 #if 0
2393 /* FIXME - Need to implement nested temporary breakpoints */
2394 if (step_over_calls
2395 && (INNER_THAN (FRAME_FP (get_current_frame ()),
2396 step_frame_address)))
2397 {
2398 ecs->another_trap = 1;
2399 keep_going (ecs);
2400 return;
2401 }
2402 #endif /* 0 */
2403 disable_longjmp_breakpoint ();
2404 ecs->handling_longjmp = 0; /* FIXME */
2405 if (what.main_action == BPSTAT_WHAT_CLEAR_LONGJMP_RESUME)
2406 break;
2407 /* else fallthrough */
2408
2409 case BPSTAT_WHAT_SINGLE:
2410 if (breakpoints_inserted)
2411 {
2412 thread_step_needed = 1;
2413 remove_breakpoints ();
2414 }
2415 breakpoints_inserted = 0;
2416 ecs->another_trap = 1;
2417 /* Still need to check other stuff, at least the case
2418 where we are stepping and step out of the right range. */
2419 break;
2420
2421 case BPSTAT_WHAT_STOP_NOISY:
2422 stop_print_frame = 1;
2423
2424 /* We are about to nuke the step_resume_breakpoint and
2425 through_sigtramp_breakpoint via the cleanup chain, so
2426 no need to worry about it here. */
2427
2428 stop_stepping (ecs);
2429 return;
2430
2431 case BPSTAT_WHAT_STOP_SILENT:
2432 stop_print_frame = 0;
2433
2434 /* We are about to nuke the step_resume_breakpoint and
2435 through_sigtramp_breakpoint via the cleanup chain, so
2436 no need to worry about it here. */
2437
2438 stop_stepping (ecs);
2439 return;
2440
2441 case BPSTAT_WHAT_STEP_RESUME:
2442 /* This proably demands a more elegant solution, but, yeah
2443 right...
2444
2445 This function's use of the simple variable
2446 step_resume_breakpoint doesn't seem to accomodate
2447 simultaneously active step-resume bp's, although the
2448 breakpoint list certainly can.
2449
2450 If we reach here and step_resume_breakpoint is already
2451 NULL, then apparently we have multiple active
2452 step-resume bp's. We'll just delete the breakpoint we
2453 stopped at, and carry on.
2454
2455 Correction: what the code currently does is delete a
2456 step-resume bp, but it makes no effort to ensure that
2457 the one deleted is the one currently stopped at. MVS */
2458
2459 if (step_resume_breakpoint == NULL)
2460 {
2461 step_resume_breakpoint =
2462 bpstat_find_step_resume_breakpoint (stop_bpstat);
2463 }
2464 delete_breakpoint (step_resume_breakpoint);
2465 step_resume_breakpoint = NULL;
2466 break;
2467
2468 case BPSTAT_WHAT_THROUGH_SIGTRAMP:
2469 if (through_sigtramp_breakpoint)
2470 delete_breakpoint (through_sigtramp_breakpoint);
2471 through_sigtramp_breakpoint = NULL;
2472
2473 /* If were waiting for a trap, hitting the step_resume_break
2474 doesn't count as getting it. */
2475 if (trap_expected)
2476 ecs->another_trap = 1;
2477 break;
2478
2479 case BPSTAT_WHAT_CHECK_SHLIBS:
2480 case BPSTAT_WHAT_CHECK_SHLIBS_RESUME_FROM_HOOK:
2481 #ifdef SOLIB_ADD
2482 {
2483 /* Remove breakpoints, we eventually want to step over the
2484 shlib event breakpoint, and SOLIB_ADD might adjust
2485 breakpoint addresses via breakpoint_re_set. */
2486 if (breakpoints_inserted)
2487 remove_breakpoints ();
2488 breakpoints_inserted = 0;
2489
2490 /* Check for any newly added shared libraries if we're
2491 supposed to be adding them automatically. */
2492 if (auto_solib_add)
2493 {
2494 /* Switch terminal for any messages produced by
2495 breakpoint_re_set. */
2496 target_terminal_ours_for_output ();
2497 SOLIB_ADD (NULL, 0, NULL);
2498 target_terminal_inferior ();
2499 }
2500
2501 /* Try to reenable shared library breakpoints, additional
2502 code segments in shared libraries might be mapped in now. */
2503 re_enable_breakpoints_in_shlibs ();
2504
2505 /* If requested, stop when the dynamic linker notifies
2506 gdb of events. This allows the user to get control
2507 and place breakpoints in initializer routines for
2508 dynamically loaded objects (among other things). */
2509 if (stop_on_solib_events)
2510 {
2511 stop_stepping (ecs);
2512 return;
2513 }
2514
2515 /* If we stopped due to an explicit catchpoint, then the
2516 (see above) call to SOLIB_ADD pulled in any symbols
2517 from a newly-loaded library, if appropriate.
2518
2519 We do want the inferior to stop, but not where it is
2520 now, which is in the dynamic linker callback. Rather,
2521 we would like it stop in the user's program, just after
2522 the call that caused this catchpoint to trigger. That
2523 gives the user a more useful vantage from which to
2524 examine their program's state. */
2525 else if (what.main_action == BPSTAT_WHAT_CHECK_SHLIBS_RESUME_FROM_HOOK)
2526 {
2527 /* ??rehrauer: If I could figure out how to get the
2528 right return PC from here, we could just set a temp
2529 breakpoint and resume. I'm not sure we can without
2530 cracking open the dld's shared libraries and sniffing
2531 their unwind tables and text/data ranges, and that's
2532 not a terribly portable notion.
2533
2534 Until that time, we must step the inferior out of the
2535 dld callback, and also out of the dld itself (and any
2536 code or stubs in libdld.sl, such as "shl_load" and
2537 friends) until we reach non-dld code. At that point,
2538 we can stop stepping. */
2539 bpstat_get_triggered_catchpoints (stop_bpstat,
2540 &ecs->stepping_through_solib_catchpoints);
2541 ecs->stepping_through_solib_after_catch = 1;
2542
2543 /* Be sure to lift all breakpoints, so the inferior does
2544 actually step past this point... */
2545 ecs->another_trap = 1;
2546 break;
2547 }
2548 else
2549 {
2550 /* We want to step over this breakpoint, then keep going. */
2551 ecs->another_trap = 1;
2552 break;
2553 }
2554 }
2555 #endif
2556 break;
2557
2558 case BPSTAT_WHAT_LAST:
2559 /* Not a real code, but listed here to shut up gcc -Wall. */
2560
2561 case BPSTAT_WHAT_KEEP_CHECKING:
2562 break;
2563 }
2564 }
2565
2566 /* We come here if we hit a breakpoint but should not
2567 stop for it. Possibly we also were stepping
2568 and should stop for that. So fall through and
2569 test for stepping. But, if not stepping,
2570 do not stop. */
2571
2572 /* Are we stepping to get the inferior out of the dynamic
2573 linker's hook (and possibly the dld itself) after catching
2574 a shlib event? */
2575 if (ecs->stepping_through_solib_after_catch)
2576 {
2577 #if defined(SOLIB_ADD)
2578 /* Have we reached our destination? If not, keep going. */
2579 if (SOLIB_IN_DYNAMIC_LINKER (PIDGET (ecs->ptid), stop_pc))
2580 {
2581 ecs->another_trap = 1;
2582 keep_going (ecs);
2583 return;
2584 }
2585 #endif
2586 /* Else, stop and report the catchpoint(s) whose triggering
2587 caused us to begin stepping. */
2588 ecs->stepping_through_solib_after_catch = 0;
2589 bpstat_clear (&stop_bpstat);
2590 stop_bpstat = bpstat_copy (ecs->stepping_through_solib_catchpoints);
2591 bpstat_clear (&ecs->stepping_through_solib_catchpoints);
2592 stop_print_frame = 1;
2593 stop_stepping (ecs);
2594 return;
2595 }
2596
2597 if (!CALL_DUMMY_BREAKPOINT_OFFSET_P)
2598 {
2599 /* This is the old way of detecting the end of the stack dummy.
2600 An architecture which defines CALL_DUMMY_BREAKPOINT_OFFSET gets
2601 handled above. As soon as we can test it on all of them, all
2602 architectures should define it. */
2603
2604 /* If this is the breakpoint at the end of a stack dummy,
2605 just stop silently, unless the user was doing an si/ni, in which
2606 case she'd better know what she's doing. */
2607
2608 if (CALL_DUMMY_HAS_COMPLETED (stop_pc, read_sp (),
2609 FRAME_FP (get_current_frame ()))
2610 && !step_range_end)
2611 {
2612 stop_print_frame = 0;
2613 stop_stack_dummy = 1;
2614 #ifdef HP_OS_BUG
2615 trap_expected_after_continue = 1;
2616 #endif
2617 stop_stepping (ecs);
2618 return;
2619 }
2620 }
2621
2622 if (step_resume_breakpoint)
2623 {
2624 /* Having a step-resume breakpoint overrides anything
2625 else having to do with stepping commands until
2626 that breakpoint is reached. */
2627 /* I'm not sure whether this needs to be check_sigtramp2 or
2628 whether it could/should be keep_going. */
2629 check_sigtramp2 (ecs);
2630 keep_going (ecs);
2631 return;
2632 }
2633
2634 if (step_range_end == 0)
2635 {
2636 /* Likewise if we aren't even stepping. */
2637 /* I'm not sure whether this needs to be check_sigtramp2 or
2638 whether it could/should be keep_going. */
2639 check_sigtramp2 (ecs);
2640 keep_going (ecs);
2641 return;
2642 }
2643
2644 /* If stepping through a line, keep going if still within it.
2645
2646 Note that step_range_end is the address of the first instruction
2647 beyond the step range, and NOT the address of the last instruction
2648 within it! */
2649 if (stop_pc >= step_range_start
2650 && stop_pc < step_range_end)
2651 {
2652 /* We might be doing a BPSTAT_WHAT_SINGLE and getting a signal.
2653 So definately need to check for sigtramp here. */
2654 check_sigtramp2 (ecs);
2655 keep_going (ecs);
2656 return;
2657 }
2658
2659 /* We stepped out of the stepping range. */
2660
2661 /* If we are stepping at the source level and entered the runtime
2662 loader dynamic symbol resolution code, we keep on single stepping
2663 until we exit the run time loader code and reach the callee's
2664 address. */
2665 if (step_over_calls == STEP_OVER_UNDEBUGGABLE && IN_SOLIB_DYNSYM_RESOLVE_CODE (stop_pc))
2666 {
2667 CORE_ADDR pc_after_resolver = SKIP_SOLIB_RESOLVER (stop_pc);
2668
2669 if (pc_after_resolver)
2670 {
2671 /* Set up a step-resume breakpoint at the address
2672 indicated by SKIP_SOLIB_RESOLVER. */
2673 struct symtab_and_line sr_sal;
2674 INIT_SAL (&sr_sal);
2675 sr_sal.pc = pc_after_resolver;
2676
2677 check_for_old_step_resume_breakpoint ();
2678 step_resume_breakpoint =
2679 set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
2680 if (breakpoints_inserted)
2681 insert_breakpoints ();
2682 }
2683
2684 keep_going (ecs);
2685 return;
2686 }
2687
2688 /* We can't update step_sp every time through the loop, because
2689 reading the stack pointer would slow down stepping too much.
2690 But we can update it every time we leave the step range. */
2691 ecs->update_step_sp = 1;
2692
2693 /* Did we just take a signal? */
2694 if (IN_SIGTRAMP (stop_pc, ecs->stop_func_name)
2695 && !IN_SIGTRAMP (prev_pc, prev_func_name)
2696 && INNER_THAN (read_sp (), step_sp))
2697 {
2698 /* We've just taken a signal; go until we are back to
2699 the point where we took it and one more. */
2700
2701 /* Note: The test above succeeds not only when we stepped
2702 into a signal handler, but also when we step past the last
2703 statement of a signal handler and end up in the return stub
2704 of the signal handler trampoline. To distinguish between
2705 these two cases, check that the frame is INNER_THAN the
2706 previous one below. pai/1997-09-11 */
2707
2708
2709 {
2710 CORE_ADDR current_frame = FRAME_FP (get_current_frame ());
2711
2712 if (INNER_THAN (current_frame, step_frame_address))
2713 {
2714 /* We have just taken a signal; go until we are back to
2715 the point where we took it and one more. */
2716
2717 /* This code is needed at least in the following case:
2718 The user types "next" and then a signal arrives (before
2719 the "next" is done). */
2720
2721 /* Note that if we are stopped at a breakpoint, then we need
2722 the step_resume breakpoint to override any breakpoints at
2723 the same location, so that we will still step over the
2724 breakpoint even though the signal happened. */
2725 struct symtab_and_line sr_sal;
2726
2727 INIT_SAL (&sr_sal);
2728 sr_sal.symtab = NULL;
2729 sr_sal.line = 0;
2730 sr_sal.pc = prev_pc;
2731 /* We could probably be setting the frame to
2732 step_frame_address; I don't think anyone thought to
2733 try it. */
2734 check_for_old_step_resume_breakpoint ();
2735 step_resume_breakpoint =
2736 set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
2737 if (breakpoints_inserted)
2738 insert_breakpoints ();
2739 }
2740 else
2741 {
2742 /* We just stepped out of a signal handler and into
2743 its calling trampoline.
2744
2745 Normally, we'd call step_over_function from
2746 here, but for some reason GDB can't unwind the
2747 stack correctly to find the real PC for the point
2748 user code where the signal trampoline will return
2749 -- FRAME_SAVED_PC fails, at least on HP-UX 10.20.
2750 But signal trampolines are pretty small stubs of
2751 code, anyway, so it's OK instead to just
2752 single-step out. Note: assuming such trampolines
2753 don't exhibit recursion on any platform... */
2754 find_pc_partial_function (stop_pc, &ecs->stop_func_name,
2755 &ecs->stop_func_start,
2756 &ecs->stop_func_end);
2757 /* Readjust stepping range */
2758 step_range_start = ecs->stop_func_start;
2759 step_range_end = ecs->stop_func_end;
2760 ecs->stepping_through_sigtramp = 1;
2761 }
2762 }
2763
2764
2765 /* If this is stepi or nexti, make sure that the stepping range
2766 gets us past that instruction. */
2767 if (step_range_end == 1)
2768 /* FIXME: Does this run afoul of the code below which, if
2769 we step into the middle of a line, resets the stepping
2770 range? */
2771 step_range_end = (step_range_start = prev_pc) + 1;
2772
2773 ecs->remove_breakpoints_on_following_step = 1;
2774 keep_going (ecs);
2775 return;
2776 }
2777
2778 if (stop_pc == ecs->stop_func_start /* Quick test */
2779 || (in_prologue (stop_pc, ecs->stop_func_start) &&
2780 !IN_SOLIB_RETURN_TRAMPOLINE (stop_pc, ecs->stop_func_name))
2781 || IN_SOLIB_CALL_TRAMPOLINE (stop_pc, ecs->stop_func_name)
2782 || ecs->stop_func_name == 0)
2783 {
2784 /* It's a subroutine call. */
2785
2786 if (step_over_calls == STEP_OVER_NONE)
2787 {
2788 /* I presume that step_over_calls is only 0 when we're
2789 supposed to be stepping at the assembly language level
2790 ("stepi"). Just stop. */
2791 stop_step = 1;
2792 print_stop_reason (END_STEPPING_RANGE, 0);
2793 stop_stepping (ecs);
2794 return;
2795 }
2796
2797 if (step_over_calls == STEP_OVER_ALL || IGNORE_HELPER_CALL (stop_pc))
2798 {
2799 /* We're doing a "next". */
2800
2801 if (IN_SIGTRAMP (stop_pc, ecs->stop_func_name)
2802 && INNER_THAN (step_frame_address, read_sp()))
2803 /* We stepped out of a signal handler, and into its
2804 calling trampoline. This is misdetected as a
2805 subroutine call, but stepping over the signal
2806 trampoline isn't such a bad idea. In order to do
2807 that, we have to ignore the value in
2808 step_frame_address, since that doesn't represent the
2809 frame that'll reach when we return from the signal
2810 trampoline. Otherwise we'll probably continue to the
2811 end of the program. */
2812 step_frame_address = 0;
2813
2814 step_over_function (ecs);
2815 keep_going (ecs);
2816 return;
2817 }
2818
2819 /* If we are in a function call trampoline (a stub between
2820 the calling routine and the real function), locate the real
2821 function. That's what tells us (a) whether we want to step
2822 into it at all, and (b) what prologue we want to run to
2823 the end of, if we do step into it. */
2824 tmp = SKIP_TRAMPOLINE_CODE (stop_pc);
2825 if (tmp != 0)
2826 ecs->stop_func_start = tmp;
2827 else
2828 {
2829 tmp = DYNAMIC_TRAMPOLINE_NEXTPC (stop_pc);
2830 if (tmp)
2831 {
2832 struct symtab_and_line xxx;
2833 /* Why isn't this s_a_l called "sr_sal", like all of the
2834 other s_a_l's where this code is duplicated? */
2835 INIT_SAL (&xxx); /* initialize to zeroes */
2836 xxx.pc = tmp;
2837 xxx.section = find_pc_overlay (xxx.pc);
2838 check_for_old_step_resume_breakpoint ();
2839 step_resume_breakpoint =
2840 set_momentary_breakpoint (xxx, NULL, bp_step_resume);
2841 insert_breakpoints ();
2842 keep_going (ecs);
2843 return;
2844 }
2845 }
2846
2847 /* If we have line number information for the function we
2848 are thinking of stepping into, step into it.
2849
2850 If there are several symtabs at that PC (e.g. with include
2851 files), just want to know whether *any* of them have line
2852 numbers. find_pc_line handles this. */
2853 {
2854 struct symtab_and_line tmp_sal;
2855
2856 tmp_sal = find_pc_line (ecs->stop_func_start, 0);
2857 if (tmp_sal.line != 0)
2858 {
2859 step_into_function (ecs);
2860 return;
2861 }
2862 }
2863
2864 /* If we have no line number and the step-stop-if-no-debug
2865 is set, we stop the step so that the user has a chance to
2866 switch in assembly mode. */
2867 if (step_over_calls == STEP_OVER_UNDEBUGGABLE && step_stop_if_no_debug)
2868 {
2869 stop_step = 1;
2870 print_stop_reason (END_STEPPING_RANGE, 0);
2871 stop_stepping (ecs);
2872 return;
2873 }
2874
2875 step_over_function (ecs);
2876 keep_going (ecs);
2877 return;
2878
2879 }
2880
2881 /* We've wandered out of the step range. */
2882
2883 ecs->sal = find_pc_line (stop_pc, 0);
2884
2885 if (step_range_end == 1)
2886 {
2887 /* It is stepi or nexti. We always want to stop stepping after
2888 one instruction. */
2889 stop_step = 1;
2890 print_stop_reason (END_STEPPING_RANGE, 0);
2891 stop_stepping (ecs);
2892 return;
2893 }
2894
2895 /* If we're in the return path from a shared library trampoline,
2896 we want to proceed through the trampoline when stepping. */
2897 if (IN_SOLIB_RETURN_TRAMPOLINE (stop_pc, ecs->stop_func_name))
2898 {
2899 CORE_ADDR tmp;
2900
2901 /* Determine where this trampoline returns. */
2902 tmp = SKIP_TRAMPOLINE_CODE (stop_pc);
2903
2904 /* Only proceed through if we know where it's going. */
2905 if (tmp)
2906 {
2907 /* And put the step-breakpoint there and go until there. */
2908 struct symtab_and_line sr_sal;
2909
2910 INIT_SAL (&sr_sal); /* initialize to zeroes */
2911 sr_sal.pc = tmp;
2912 sr_sal.section = find_pc_overlay (sr_sal.pc);
2913 /* Do not specify what the fp should be when we stop
2914 since on some machines the prologue
2915 is where the new fp value is established. */
2916 check_for_old_step_resume_breakpoint ();
2917 step_resume_breakpoint =
2918 set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
2919 if (breakpoints_inserted)
2920 insert_breakpoints ();
2921
2922 /* Restart without fiddling with the step ranges or
2923 other state. */
2924 keep_going (ecs);
2925 return;
2926 }
2927 }
2928
2929 if (ecs->sal.line == 0)
2930 {
2931 /* We have no line number information. That means to stop
2932 stepping (does this always happen right after one instruction,
2933 when we do "s" in a function with no line numbers,
2934 or can this happen as a result of a return or longjmp?). */
2935 stop_step = 1;
2936 print_stop_reason (END_STEPPING_RANGE, 0);
2937 stop_stepping (ecs);
2938 return;
2939 }
2940
2941 if ((stop_pc == ecs->sal.pc)
2942 && (ecs->current_line != ecs->sal.line || ecs->current_symtab != ecs->sal.symtab))
2943 {
2944 /* We are at the start of a different line. So stop. Note that
2945 we don't stop if we step into the middle of a different line.
2946 That is said to make things like for (;;) statements work
2947 better. */
2948 stop_step = 1;
2949 print_stop_reason (END_STEPPING_RANGE, 0);
2950 stop_stepping (ecs);
2951 return;
2952 }
2953
2954 /* We aren't done stepping.
2955
2956 Optimize by setting the stepping range to the line.
2957 (We might not be in the original line, but if we entered a
2958 new line in mid-statement, we continue stepping. This makes
2959 things like for(;;) statements work better.) */
2960
2961 if (ecs->stop_func_end && ecs->sal.end >= ecs->stop_func_end)
2962 {
2963 /* If this is the last line of the function, don't keep stepping
2964 (it would probably step us out of the function).
2965 This is particularly necessary for a one-line function,
2966 in which after skipping the prologue we better stop even though
2967 we will be in mid-line. */
2968 stop_step = 1;
2969 print_stop_reason (END_STEPPING_RANGE, 0);
2970 stop_stepping (ecs);
2971 return;
2972 }
2973 step_range_start = ecs->sal.pc;
2974 step_range_end = ecs->sal.end;
2975 step_frame_address = FRAME_FP (get_current_frame ());
2976 ecs->current_line = ecs->sal.line;
2977 ecs->current_symtab = ecs->sal.symtab;
2978
2979 /* In the case where we just stepped out of a function into the middle
2980 of a line of the caller, continue stepping, but step_frame_address
2981 must be modified to current frame */
2982 {
2983 CORE_ADDR current_frame = FRAME_FP (get_current_frame ());
2984 if (!(INNER_THAN (current_frame, step_frame_address)))
2985 step_frame_address = current_frame;
2986 }
2987
2988 keep_going (ecs);
2989
2990 } /* extra brace, to preserve old indentation */
2991 }
2992
2993 /* Are we in the middle of stepping? */
2994
2995 static int
2996 currently_stepping (struct execution_control_state *ecs)
2997 {
2998 return ((through_sigtramp_breakpoint == NULL
2999 && !ecs->handling_longjmp
3000 && ((step_range_end && step_resume_breakpoint == NULL)
3001 || trap_expected))
3002 || ecs->stepping_through_solib_after_catch
3003 || bpstat_should_step ());
3004 }
3005
3006 static void
3007 check_sigtramp2 (struct execution_control_state *ecs)
3008 {
3009 if (trap_expected
3010 && IN_SIGTRAMP (stop_pc, ecs->stop_func_name)
3011 && !IN_SIGTRAMP (prev_pc, prev_func_name)
3012 && INNER_THAN (read_sp (), step_sp))
3013 {
3014 /* What has happened here is that we have just stepped the
3015 inferior with a signal (because it is a signal which
3016 shouldn't make us stop), thus stepping into sigtramp.
3017
3018 So we need to set a step_resume_break_address breakpoint and
3019 continue until we hit it, and then step. FIXME: This should
3020 be more enduring than a step_resume breakpoint; we should
3021 know that we will later need to keep going rather than
3022 re-hitting the breakpoint here (see the testsuite,
3023 gdb.base/signals.exp where it says "exceedingly difficult"). */
3024
3025 struct symtab_and_line sr_sal;
3026
3027 INIT_SAL (&sr_sal); /* initialize to zeroes */
3028 sr_sal.pc = prev_pc;
3029 sr_sal.section = find_pc_overlay (sr_sal.pc);
3030 /* We perhaps could set the frame if we kept track of what the
3031 frame corresponding to prev_pc was. But we don't, so don't. */
3032 through_sigtramp_breakpoint =
3033 set_momentary_breakpoint (sr_sal, NULL, bp_through_sigtramp);
3034 if (breakpoints_inserted)
3035 insert_breakpoints ();
3036
3037 ecs->remove_breakpoints_on_following_step = 1;
3038 ecs->another_trap = 1;
3039 }
3040 }
3041
3042 /* Subroutine call with source code we should not step over. Do step
3043 to the first line of code in it. */
3044
3045 static void
3046 step_into_function (struct execution_control_state *ecs)
3047 {
3048 struct symtab *s;
3049 struct symtab_and_line sr_sal;
3050
3051 s = find_pc_symtab (stop_pc);
3052 if (s && s->language != language_asm)
3053 ecs->stop_func_start = SKIP_PROLOGUE (ecs->stop_func_start);
3054
3055 ecs->sal = find_pc_line (ecs->stop_func_start, 0);
3056 /* Use the step_resume_break to step until the end of the prologue,
3057 even if that involves jumps (as it seems to on the vax under
3058 4.2). */
3059 /* If the prologue ends in the middle of a source line, continue to
3060 the end of that source line (if it is still within the function).
3061 Otherwise, just go to end of prologue. */
3062 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
3063 /* no, don't either. It skips any code that's legitimately on the
3064 first line. */
3065 #else
3066 if (ecs->sal.end
3067 && ecs->sal.pc != ecs->stop_func_start
3068 && ecs->sal.end < ecs->stop_func_end)
3069 ecs->stop_func_start = ecs->sal.end;
3070 #endif
3071
3072 if (ecs->stop_func_start == stop_pc)
3073 {
3074 /* We are already there: stop now. */
3075 stop_step = 1;
3076 print_stop_reason (END_STEPPING_RANGE, 0);
3077 stop_stepping (ecs);
3078 return;
3079 }
3080 else
3081 {
3082 /* Put the step-breakpoint there and go until there. */
3083 INIT_SAL (&sr_sal); /* initialize to zeroes */
3084 sr_sal.pc = ecs->stop_func_start;
3085 sr_sal.section = find_pc_overlay (ecs->stop_func_start);
3086 /* Do not specify what the fp should be when we stop since on
3087 some machines the prologue is where the new fp value is
3088 established. */
3089 check_for_old_step_resume_breakpoint ();
3090 step_resume_breakpoint =
3091 set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
3092 if (breakpoints_inserted)
3093 insert_breakpoints ();
3094
3095 /* And make sure stepping stops right away then. */
3096 step_range_end = step_range_start;
3097 }
3098 keep_going (ecs);
3099 }
3100
3101 /* We've just entered a callee, and we wish to resume until it returns
3102 to the caller. Setting a step_resume breakpoint on the return
3103 address will catch a return from the callee.
3104
3105 However, if the callee is recursing, we want to be careful not to
3106 catch returns of those recursive calls, but only of THIS instance
3107 of the call.
3108
3109 To do this, we set the step_resume bp's frame to our current
3110 caller's frame (step_frame_address, which is set by the "next" or
3111 "until" command, before execution begins). */
3112
3113 static void
3114 step_over_function (struct execution_control_state *ecs)
3115 {
3116 struct symtab_and_line sr_sal;
3117
3118 INIT_SAL (&sr_sal); /* initialize to zeros */
3119 sr_sal.pc = ADDR_BITS_REMOVE (SAVED_PC_AFTER_CALL (get_current_frame ()));
3120 sr_sal.section = find_pc_overlay (sr_sal.pc);
3121
3122 check_for_old_step_resume_breakpoint ();
3123 step_resume_breakpoint =
3124 set_momentary_breakpoint (sr_sal, get_current_frame (), bp_step_resume);
3125
3126 if (step_frame_address && !IN_SOLIB_DYNSYM_RESOLVE_CODE (sr_sal.pc))
3127 step_resume_breakpoint->frame = step_frame_address;
3128
3129 if (breakpoints_inserted)
3130 insert_breakpoints ();
3131 }
3132
3133 static void
3134 stop_stepping (struct execution_control_state *ecs)
3135 {
3136 if (target_has_execution)
3137 {
3138 /* Are we stopping for a vfork event? We only stop when we see
3139 the child's event. However, we may not yet have seen the
3140 parent's event. And, inferior_ptid is still set to the
3141 parent's pid, until we resume again and follow either the
3142 parent or child.
3143
3144 To ensure that we can really touch inferior_ptid (aka, the
3145 parent process) -- which calls to functions like read_pc
3146 implicitly do -- wait on the parent if necessary. */
3147 if ((pending_follow.kind == TARGET_WAITKIND_VFORKED)
3148 && !pending_follow.fork_event.saw_parent_fork)
3149 {
3150 ptid_t parent_ptid;
3151
3152 do
3153 {
3154 if (target_wait_hook)
3155 parent_ptid = target_wait_hook (pid_to_ptid (-1), &(ecs->ws));
3156 else
3157 parent_ptid = target_wait (pid_to_ptid (-1), &(ecs->ws));
3158 }
3159 while (! ptid_equal (parent_ptid, inferior_ptid));
3160 }
3161
3162 /* Assuming the inferior still exists, set these up for next
3163 time, just like we did above if we didn't break out of the
3164 loop. */
3165 prev_pc = read_pc ();
3166 prev_func_start = ecs->stop_func_start;
3167 prev_func_name = ecs->stop_func_name;
3168 }
3169
3170 /* Let callers know we don't want to wait for the inferior anymore. */
3171 ecs->wait_some_more = 0;
3172 }
3173
3174 /* This function handles various cases where we need to continue
3175 waiting for the inferior. */
3176 /* (Used to be the keep_going: label in the old wait_for_inferior) */
3177
3178 static void
3179 keep_going (struct execution_control_state *ecs)
3180 {
3181 /* ??rehrauer: ttrace on HP-UX theoretically allows one to debug a
3182 vforked child between its creation and subsequent exit or call to
3183 exec(). However, I had big problems in this rather creaky exec
3184 engine, getting that to work. The fundamental problem is that
3185 I'm trying to debug two processes via an engine that only
3186 understands a single process with possibly multiple threads.
3187
3188 Hence, this spot is known to have problems when
3189 target_can_follow_vfork_prior_to_exec returns 1. */
3190
3191 /* Save the pc before execution, to compare with pc after stop. */
3192 prev_pc = read_pc (); /* Might have been DECR_AFTER_BREAK */
3193 prev_func_start = ecs->stop_func_start; /* Ok, since if DECR_PC_AFTER
3194 BREAK is defined, the
3195 original pc would not have
3196 been at the start of a
3197 function. */
3198 prev_func_name = ecs->stop_func_name;
3199
3200 if (ecs->update_step_sp)
3201 step_sp = read_sp ();
3202 ecs->update_step_sp = 0;
3203
3204 /* If we did not do break;, it means we should keep running the
3205 inferior and not return to debugger. */
3206
3207 if (trap_expected && stop_signal != TARGET_SIGNAL_TRAP)
3208 {
3209 /* We took a signal (which we are supposed to pass through to
3210 the inferior, else we'd have done a break above) and we
3211 haven't yet gotten our trap. Simply continue. */
3212 resume (currently_stepping (ecs), stop_signal);
3213 }
3214 else
3215 {
3216 /* Either the trap was not expected, but we are continuing
3217 anyway (the user asked that this signal be passed to the
3218 child)
3219 -- or --
3220 The signal was SIGTRAP, e.g. it was our signal, but we
3221 decided we should resume from it.
3222
3223 We're going to run this baby now!
3224
3225 Insert breakpoints now, unless we are trying to one-proceed
3226 past a breakpoint. */
3227 /* If we've just finished a special step resume and we don't
3228 want to hit a breakpoint, pull em out. */
3229 if (step_resume_breakpoint == NULL
3230 && through_sigtramp_breakpoint == NULL
3231 && ecs->remove_breakpoints_on_following_step)
3232 {
3233 ecs->remove_breakpoints_on_following_step = 0;
3234 remove_breakpoints ();
3235 breakpoints_inserted = 0;
3236 }
3237 else if (!breakpoints_inserted &&
3238 (through_sigtramp_breakpoint != NULL || !ecs->another_trap))
3239 {
3240 breakpoints_failed = insert_breakpoints ();
3241 if (breakpoints_failed)
3242 {
3243 stop_stepping (ecs);
3244 return;
3245 }
3246 breakpoints_inserted = 1;
3247 }
3248
3249 trap_expected = ecs->another_trap;
3250
3251 /* Do not deliver SIGNAL_TRAP (except when the user explicitly
3252 specifies that such a signal should be delivered to the
3253 target program).
3254
3255 Typically, this would occure when a user is debugging a
3256 target monitor on a simulator: the target monitor sets a
3257 breakpoint; the simulator encounters this break-point and
3258 halts the simulation handing control to GDB; GDB, noteing
3259 that the break-point isn't valid, returns control back to the
3260 simulator; the simulator then delivers the hardware
3261 equivalent of a SIGNAL_TRAP to the program being debugged. */
3262
3263 if (stop_signal == TARGET_SIGNAL_TRAP
3264 && !signal_program[stop_signal])
3265 stop_signal = TARGET_SIGNAL_0;
3266
3267 #ifdef SHIFT_INST_REGS
3268 /* I'm not sure when this following segment applies. I do know,
3269 now, that we shouldn't rewrite the regs when we were stopped
3270 by a random signal from the inferior process. */
3271 /* FIXME: Shouldn't this be based on the valid bit of the SXIP?
3272 (this is only used on the 88k). */
3273
3274 if (!bpstat_explains_signal (stop_bpstat)
3275 && (stop_signal != TARGET_SIGNAL_CHLD)
3276 && !stopped_by_random_signal)
3277 SHIFT_INST_REGS ();
3278 #endif /* SHIFT_INST_REGS */
3279
3280 resume (currently_stepping (ecs), stop_signal);
3281 }
3282
3283 prepare_to_wait (ecs);
3284 }
3285
3286 /* This function normally comes after a resume, before
3287 handle_inferior_event exits. It takes care of any last bits of
3288 housekeeping, and sets the all-important wait_some_more flag. */
3289
3290 static void
3291 prepare_to_wait (struct execution_control_state *ecs)
3292 {
3293 if (ecs->infwait_state == infwait_normal_state)
3294 {
3295 overlay_cache_invalid = 1;
3296
3297 /* We have to invalidate the registers BEFORE calling
3298 target_wait because they can be loaded from the target while
3299 in target_wait. This makes remote debugging a bit more
3300 efficient for those targets that provide critical registers
3301 as part of their normal status mechanism. */
3302
3303 registers_changed ();
3304 ecs->waiton_ptid = pid_to_ptid (-1);
3305 ecs->wp = &(ecs->ws);
3306 }
3307 /* This is the old end of the while loop. Let everybody know we
3308 want to wait for the inferior some more and get called again
3309 soon. */
3310 ecs->wait_some_more = 1;
3311 }
3312
3313 /* Print why the inferior has stopped. We always print something when
3314 the inferior exits, or receives a signal. The rest of the cases are
3315 dealt with later on in normal_stop() and print_it_typical(). Ideally
3316 there should be a call to this function from handle_inferior_event()
3317 each time stop_stepping() is called.*/
3318 static void
3319 print_stop_reason (enum inferior_stop_reason stop_reason, int stop_info)
3320 {
3321 switch (stop_reason)
3322 {
3323 case STOP_UNKNOWN:
3324 /* We don't deal with these cases from handle_inferior_event()
3325 yet. */
3326 break;
3327 case END_STEPPING_RANGE:
3328 /* We are done with a step/next/si/ni command. */
3329 /* For now print nothing. */
3330 #ifdef UI_OUT
3331 /* Print a message only if not in the middle of doing a "step n"
3332 operation for n > 1 */
3333 if (!step_multi || !stop_step)
3334 if (interpreter_p && strcmp (interpreter_p, "mi") == 0)
3335 ui_out_field_string (uiout, "reason", "end-stepping-range");
3336 #endif
3337 break;
3338 case BREAKPOINT_HIT:
3339 /* We found a breakpoint. */
3340 /* For now print nothing. */
3341 break;
3342 case SIGNAL_EXITED:
3343 /* The inferior was terminated by a signal. */
3344 #ifdef UI_OUT
3345 annotate_signalled ();
3346 if (interpreter_p && strcmp (interpreter_p, "mi") == 0)
3347 ui_out_field_string (uiout, "reason", "exited-signalled");
3348 ui_out_text (uiout, "\nProgram terminated with signal ");
3349 annotate_signal_name ();
3350 ui_out_field_string (uiout, "signal-name", target_signal_to_name (stop_info));
3351 annotate_signal_name_end ();
3352 ui_out_text (uiout, ", ");
3353 annotate_signal_string ();
3354 ui_out_field_string (uiout, "signal-meaning", target_signal_to_string (stop_info));
3355 annotate_signal_string_end ();
3356 ui_out_text (uiout, ".\n");
3357 ui_out_text (uiout, "The program no longer exists.\n");
3358 #else
3359 annotate_signalled ();
3360 printf_filtered ("\nProgram terminated with signal ");
3361 annotate_signal_name ();
3362 printf_filtered ("%s", target_signal_to_name (stop_info));
3363 annotate_signal_name_end ();
3364 printf_filtered (", ");
3365 annotate_signal_string ();
3366 printf_filtered ("%s", target_signal_to_string (stop_info));
3367 annotate_signal_string_end ();
3368 printf_filtered (".\n");
3369
3370 printf_filtered ("The program no longer exists.\n");
3371 gdb_flush (gdb_stdout);
3372 #endif
3373 break;
3374 case EXITED:
3375 /* The inferior program is finished. */
3376 #ifdef UI_OUT
3377 annotate_exited (stop_info);
3378 if (stop_info)
3379 {
3380 if (interpreter_p && strcmp (interpreter_p, "mi") == 0)
3381 ui_out_field_string (uiout, "reason", "exited");
3382 ui_out_text (uiout, "\nProgram exited with code ");
3383 ui_out_field_fmt (uiout, "exit-code", "0%o", (unsigned int) stop_info);
3384 ui_out_text (uiout, ".\n");
3385 }
3386 else
3387 {
3388 if (interpreter_p && strcmp (interpreter_p, "mi") == 0)
3389 ui_out_field_string (uiout, "reason", "exited-normally");
3390 ui_out_text (uiout, "\nProgram exited normally.\n");
3391 }
3392 #else
3393 annotate_exited (stop_info);
3394 if (stop_info)
3395 printf_filtered ("\nProgram exited with code 0%o.\n",
3396 (unsigned int) stop_info);
3397 else
3398 printf_filtered ("\nProgram exited normally.\n");
3399 #endif
3400 break;
3401 case SIGNAL_RECEIVED:
3402 /* Signal received. The signal table tells us to print about
3403 it. */
3404 #ifdef UI_OUT
3405 annotate_signal ();
3406 ui_out_text (uiout, "\nProgram received signal ");
3407 annotate_signal_name ();
3408 ui_out_field_string (uiout, "signal-name", target_signal_to_name (stop_info));
3409 annotate_signal_name_end ();
3410 ui_out_text (uiout, ", ");
3411 annotate_signal_string ();
3412 ui_out_field_string (uiout, "signal-meaning", target_signal_to_string (stop_info));
3413 annotate_signal_string_end ();
3414 ui_out_text (uiout, ".\n");
3415 #else
3416 annotate_signal ();
3417 printf_filtered ("\nProgram received signal ");
3418 annotate_signal_name ();
3419 printf_filtered ("%s", target_signal_to_name (stop_info));
3420 annotate_signal_name_end ();
3421 printf_filtered (", ");
3422 annotate_signal_string ();
3423 printf_filtered ("%s", target_signal_to_string (stop_info));
3424 annotate_signal_string_end ();
3425 printf_filtered (".\n");
3426 gdb_flush (gdb_stdout);
3427 #endif
3428 break;
3429 default:
3430 internal_error (__FILE__, __LINE__,
3431 "print_stop_reason: unrecognized enum value");
3432 break;
3433 }
3434 }
3435 \f
3436
3437 /* Here to return control to GDB when the inferior stops for real.
3438 Print appropriate messages, remove breakpoints, give terminal our modes.
3439
3440 STOP_PRINT_FRAME nonzero means print the executing frame
3441 (pc, function, args, file, line number and line text).
3442 BREAKPOINTS_FAILED nonzero means stop was due to error
3443 attempting to insert breakpoints. */
3444
3445 void
3446 normal_stop (void)
3447 {
3448 /* As with the notification of thread events, we want to delay
3449 notifying the user that we've switched thread context until
3450 the inferior actually stops.
3451
3452 (Note that there's no point in saying anything if the inferior
3453 has exited!) */
3454 if (! ptid_equal (previous_inferior_ptid, inferior_ptid)
3455 && target_has_execution)
3456 {
3457 target_terminal_ours_for_output ();
3458 printf_filtered ("[Switching to %s]\n",
3459 target_pid_or_tid_to_str (inferior_ptid));
3460 previous_inferior_ptid = inferior_ptid;
3461 }
3462
3463 /* Make sure that the current_frame's pc is correct. This
3464 is a correction for setting up the frame info before doing
3465 DECR_PC_AFTER_BREAK */
3466 if (target_has_execution && get_current_frame ())
3467 (get_current_frame ())->pc = read_pc ();
3468
3469 if (breakpoints_failed)
3470 {
3471 target_terminal_ours_for_output ();
3472 print_sys_errmsg ("While inserting breakpoints", breakpoints_failed);
3473 printf_filtered ("Stopped; cannot insert breakpoints.\n\
3474 The same program may be running in another process,\n\
3475 or you may have requested too many hardware breakpoints\n\
3476 and/or watchpoints.\n");
3477 }
3478
3479 if (target_has_execution && breakpoints_inserted)
3480 {
3481 if (remove_breakpoints ())
3482 {
3483 target_terminal_ours_for_output ();
3484 printf_filtered ("Cannot remove breakpoints because ");
3485 printf_filtered ("program is no longer writable.\n");
3486 printf_filtered ("It might be running in another process.\n");
3487 printf_filtered ("Further execution is probably impossible.\n");
3488 }
3489 }
3490 breakpoints_inserted = 0;
3491
3492 /* Delete the breakpoint we stopped at, if it wants to be deleted.
3493 Delete any breakpoint that is to be deleted at the next stop. */
3494
3495 breakpoint_auto_delete (stop_bpstat);
3496
3497 /* If an auto-display called a function and that got a signal,
3498 delete that auto-display to avoid an infinite recursion. */
3499
3500 if (stopped_by_random_signal)
3501 disable_current_display ();
3502
3503 /* Don't print a message if in the middle of doing a "step n"
3504 operation for n > 1 */
3505 if (step_multi && stop_step)
3506 goto done;
3507
3508 target_terminal_ours ();
3509
3510 /* Look up the hook_stop and run it if it exists. */
3511
3512 if (stop_command && stop_command->hook_pre)
3513 {
3514 catch_errors (hook_stop_stub, stop_command->hook_pre,
3515 "Error while running hook_stop:\n", RETURN_MASK_ALL);
3516 }
3517
3518 if (!target_has_stack)
3519 {
3520
3521 goto done;
3522 }
3523
3524 /* Select innermost stack frame - i.e., current frame is frame 0,
3525 and current location is based on that.
3526 Don't do this on return from a stack dummy routine,
3527 or if the program has exited. */
3528
3529 if (!stop_stack_dummy)
3530 {
3531 select_frame (get_current_frame (), 0);
3532
3533 /* Print current location without a level number, if
3534 we have changed functions or hit a breakpoint.
3535 Print source line if we have one.
3536 bpstat_print() contains the logic deciding in detail
3537 what to print, based on the event(s) that just occurred. */
3538
3539 if (stop_print_frame
3540 && selected_frame)
3541 {
3542 int bpstat_ret;
3543 int source_flag;
3544 int do_frame_printing = 1;
3545
3546 bpstat_ret = bpstat_print (stop_bpstat);
3547 switch (bpstat_ret)
3548 {
3549 case PRINT_UNKNOWN:
3550 if (stop_step
3551 && step_frame_address == FRAME_FP (get_current_frame ())
3552 && step_start_function == find_pc_function (stop_pc))
3553 source_flag = SRC_LINE; /* finished step, just print source line */
3554 else
3555 source_flag = SRC_AND_LOC; /* print location and source line */
3556 break;
3557 case PRINT_SRC_AND_LOC:
3558 source_flag = SRC_AND_LOC; /* print location and source line */
3559 break;
3560 case PRINT_SRC_ONLY:
3561 source_flag = SRC_LINE;
3562 break;
3563 case PRINT_NOTHING:
3564 source_flag = SRC_LINE; /* something bogus */
3565 do_frame_printing = 0;
3566 break;
3567 default:
3568 internal_error (__FILE__, __LINE__,
3569 "Unknown value.");
3570 }
3571 #ifdef UI_OUT
3572 /* For mi, have the same behavior every time we stop:
3573 print everything but the source line. */
3574 if (interpreter_p && strcmp (interpreter_p, "mi") == 0)
3575 source_flag = LOC_AND_ADDRESS;
3576 #endif
3577
3578 #ifdef UI_OUT
3579 if (interpreter_p && strcmp (interpreter_p, "mi") == 0)
3580 ui_out_field_int (uiout, "thread-id",
3581 pid_to_thread_id (inferior_ptid));
3582 #endif
3583 /* The behavior of this routine with respect to the source
3584 flag is:
3585 SRC_LINE: Print only source line
3586 LOCATION: Print only location
3587 SRC_AND_LOC: Print location and source line */
3588 if (do_frame_printing)
3589 show_and_print_stack_frame (selected_frame, -1, source_flag);
3590
3591 /* Display the auto-display expressions. */
3592 do_displays ();
3593 }
3594 }
3595
3596 /* Save the function value return registers, if we care.
3597 We might be about to restore their previous contents. */
3598 if (proceed_to_finish)
3599 read_register_bytes (0, stop_registers, REGISTER_BYTES);
3600
3601 if (stop_stack_dummy)
3602 {
3603 /* Pop the empty frame that contains the stack dummy.
3604 POP_FRAME ends with a setting of the current frame, so we
3605 can use that next. */
3606 POP_FRAME;
3607 /* Set stop_pc to what it was before we called the function.
3608 Can't rely on restore_inferior_status because that only gets
3609 called if we don't stop in the called function. */
3610 stop_pc = read_pc ();
3611 select_frame (get_current_frame (), 0);
3612 }
3613
3614
3615 TUIDO (((TuiOpaqueFuncPtr) tui_vCheckDataValues, selected_frame));
3616
3617 done:
3618 annotate_stopped ();
3619 }
3620
3621 static int
3622 hook_stop_stub (void *cmd)
3623 {
3624 execute_user_command ((struct cmd_list_element *) cmd, 0);
3625 return (0);
3626 }
3627 \f
3628 int
3629 signal_stop_state (int signo)
3630 {
3631 return signal_stop[signo];
3632 }
3633
3634 int
3635 signal_print_state (int signo)
3636 {
3637 return signal_print[signo];
3638 }
3639
3640 int
3641 signal_pass_state (int signo)
3642 {
3643 return signal_program[signo];
3644 }
3645
3646 int signal_stop_update (signo, state)
3647 int signo;
3648 int state;
3649 {
3650 int ret = signal_stop[signo];
3651 signal_stop[signo] = state;
3652 return ret;
3653 }
3654
3655 int signal_print_update (signo, state)
3656 int signo;
3657 int state;
3658 {
3659 int ret = signal_print[signo];
3660 signal_print[signo] = state;
3661 return ret;
3662 }
3663
3664 int signal_pass_update (signo, state)
3665 int signo;
3666 int state;
3667 {
3668 int ret = signal_program[signo];
3669 signal_program[signo] = state;
3670 return ret;
3671 }
3672
3673 static void
3674 sig_print_header (void)
3675 {
3676 printf_filtered ("\
3677 Signal Stop\tPrint\tPass to program\tDescription\n");
3678 }
3679
3680 static void
3681 sig_print_info (enum target_signal oursig)
3682 {
3683 char *name = target_signal_to_name (oursig);
3684 int name_padding = 13 - strlen (name);
3685
3686 if (name_padding <= 0)
3687 name_padding = 0;
3688
3689 printf_filtered ("%s", name);
3690 printf_filtered ("%*.*s ", name_padding, name_padding,
3691 " ");
3692 printf_filtered ("%s\t", signal_stop[oursig] ? "Yes" : "No");
3693 printf_filtered ("%s\t", signal_print[oursig] ? "Yes" : "No");
3694 printf_filtered ("%s\t\t", signal_program[oursig] ? "Yes" : "No");
3695 printf_filtered ("%s\n", target_signal_to_string (oursig));
3696 }
3697
3698 /* Specify how various signals in the inferior should be handled. */
3699
3700 static void
3701 handle_command (char *args, int from_tty)
3702 {
3703 char **argv;
3704 int digits, wordlen;
3705 int sigfirst, signum, siglast;
3706 enum target_signal oursig;
3707 int allsigs;
3708 int nsigs;
3709 unsigned char *sigs;
3710 struct cleanup *old_chain;
3711
3712 if (args == NULL)
3713 {
3714 error_no_arg ("signal to handle");
3715 }
3716
3717 /* Allocate and zero an array of flags for which signals to handle. */
3718
3719 nsigs = (int) TARGET_SIGNAL_LAST;
3720 sigs = (unsigned char *) alloca (nsigs);
3721 memset (sigs, 0, nsigs);
3722
3723 /* Break the command line up into args. */
3724
3725 argv = buildargv (args);
3726 if (argv == NULL)
3727 {
3728 nomem (0);
3729 }
3730 old_chain = make_cleanup_freeargv (argv);
3731
3732 /* Walk through the args, looking for signal oursigs, signal names, and
3733 actions. Signal numbers and signal names may be interspersed with
3734 actions, with the actions being performed for all signals cumulatively
3735 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
3736
3737 while (*argv != NULL)
3738 {
3739 wordlen = strlen (*argv);
3740 for (digits = 0; isdigit ((*argv)[digits]); digits++)
3741 {;
3742 }
3743 allsigs = 0;
3744 sigfirst = siglast = -1;
3745
3746 if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
3747 {
3748 /* Apply action to all signals except those used by the
3749 debugger. Silently skip those. */
3750 allsigs = 1;
3751 sigfirst = 0;
3752 siglast = nsigs - 1;
3753 }
3754 else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
3755 {
3756 SET_SIGS (nsigs, sigs, signal_stop);
3757 SET_SIGS (nsigs, sigs, signal_print);
3758 }
3759 else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
3760 {
3761 UNSET_SIGS (nsigs, sigs, signal_program);
3762 }
3763 else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
3764 {
3765 SET_SIGS (nsigs, sigs, signal_print);
3766 }
3767 else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
3768 {
3769 SET_SIGS (nsigs, sigs, signal_program);
3770 }
3771 else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
3772 {
3773 UNSET_SIGS (nsigs, sigs, signal_stop);
3774 }
3775 else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
3776 {
3777 SET_SIGS (nsigs, sigs, signal_program);
3778 }
3779 else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
3780 {
3781 UNSET_SIGS (nsigs, sigs, signal_print);
3782 UNSET_SIGS (nsigs, sigs, signal_stop);
3783 }
3784 else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
3785 {
3786 UNSET_SIGS (nsigs, sigs, signal_program);
3787 }
3788 else if (digits > 0)
3789 {
3790 /* It is numeric. The numeric signal refers to our own
3791 internal signal numbering from target.h, not to host/target
3792 signal number. This is a feature; users really should be
3793 using symbolic names anyway, and the common ones like
3794 SIGHUP, SIGINT, SIGALRM, etc. will work right anyway. */
3795
3796 sigfirst = siglast = (int)
3797 target_signal_from_command (atoi (*argv));
3798 if ((*argv)[digits] == '-')
3799 {
3800 siglast = (int)
3801 target_signal_from_command (atoi ((*argv) + digits + 1));
3802 }
3803 if (sigfirst > siglast)
3804 {
3805 /* Bet he didn't figure we'd think of this case... */
3806 signum = sigfirst;
3807 sigfirst = siglast;
3808 siglast = signum;
3809 }
3810 }
3811 else
3812 {
3813 oursig = target_signal_from_name (*argv);
3814 if (oursig != TARGET_SIGNAL_UNKNOWN)
3815 {
3816 sigfirst = siglast = (int) oursig;
3817 }
3818 else
3819 {
3820 /* Not a number and not a recognized flag word => complain. */
3821 error ("Unrecognized or ambiguous flag word: \"%s\".", *argv);
3822 }
3823 }
3824
3825 /* If any signal numbers or symbol names were found, set flags for
3826 which signals to apply actions to. */
3827
3828 for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
3829 {
3830 switch ((enum target_signal) signum)
3831 {
3832 case TARGET_SIGNAL_TRAP:
3833 case TARGET_SIGNAL_INT:
3834 if (!allsigs && !sigs[signum])
3835 {
3836 if (query ("%s is used by the debugger.\n\
3837 Are you sure you want to change it? ",
3838 target_signal_to_name
3839 ((enum target_signal) signum)))
3840 {
3841 sigs[signum] = 1;
3842 }
3843 else
3844 {
3845 printf_unfiltered ("Not confirmed, unchanged.\n");
3846 gdb_flush (gdb_stdout);
3847 }
3848 }
3849 break;
3850 case TARGET_SIGNAL_0:
3851 case TARGET_SIGNAL_DEFAULT:
3852 case TARGET_SIGNAL_UNKNOWN:
3853 /* Make sure that "all" doesn't print these. */
3854 break;
3855 default:
3856 sigs[signum] = 1;
3857 break;
3858 }
3859 }
3860
3861 argv++;
3862 }
3863
3864 target_notice_signals (inferior_ptid);
3865
3866 if (from_tty)
3867 {
3868 /* Show the results. */
3869 sig_print_header ();
3870 for (signum = 0; signum < nsigs; signum++)
3871 {
3872 if (sigs[signum])
3873 {
3874 sig_print_info (signum);
3875 }
3876 }
3877 }
3878
3879 do_cleanups (old_chain);
3880 }
3881
3882 static void
3883 xdb_handle_command (char *args, int from_tty)
3884 {
3885 char **argv;
3886 struct cleanup *old_chain;
3887
3888 /* Break the command line up into args. */
3889
3890 argv = buildargv (args);
3891 if (argv == NULL)
3892 {
3893 nomem (0);
3894 }
3895 old_chain = make_cleanup_freeargv (argv);
3896 if (argv[1] != (char *) NULL)
3897 {
3898 char *argBuf;
3899 int bufLen;
3900
3901 bufLen = strlen (argv[0]) + 20;
3902 argBuf = (char *) xmalloc (bufLen);
3903 if (argBuf)
3904 {
3905 int validFlag = 1;
3906 enum target_signal oursig;
3907
3908 oursig = target_signal_from_name (argv[0]);
3909 memset (argBuf, 0, bufLen);
3910 if (strcmp (argv[1], "Q") == 0)
3911 sprintf (argBuf, "%s %s", argv[0], "noprint");
3912 else
3913 {
3914 if (strcmp (argv[1], "s") == 0)
3915 {
3916 if (!signal_stop[oursig])
3917 sprintf (argBuf, "%s %s", argv[0], "stop");
3918 else
3919 sprintf (argBuf, "%s %s", argv[0], "nostop");
3920 }
3921 else if (strcmp (argv[1], "i") == 0)
3922 {
3923 if (!signal_program[oursig])
3924 sprintf (argBuf, "%s %s", argv[0], "pass");
3925 else
3926 sprintf (argBuf, "%s %s", argv[0], "nopass");
3927 }
3928 else if (strcmp (argv[1], "r") == 0)
3929 {
3930 if (!signal_print[oursig])
3931 sprintf (argBuf, "%s %s", argv[0], "print");
3932 else
3933 sprintf (argBuf, "%s %s", argv[0], "noprint");
3934 }
3935 else
3936 validFlag = 0;
3937 }
3938 if (validFlag)
3939 handle_command (argBuf, from_tty);
3940 else
3941 printf_filtered ("Invalid signal handling flag.\n");
3942 if (argBuf)
3943 xfree (argBuf);
3944 }
3945 }
3946 do_cleanups (old_chain);
3947 }
3948
3949 /* Print current contents of the tables set by the handle command.
3950 It is possible we should just be printing signals actually used
3951 by the current target (but for things to work right when switching
3952 targets, all signals should be in the signal tables). */
3953
3954 static void
3955 signals_info (char *signum_exp, int from_tty)
3956 {
3957 enum target_signal oursig;
3958 sig_print_header ();
3959
3960 if (signum_exp)
3961 {
3962 /* First see if this is a symbol name. */
3963 oursig = target_signal_from_name (signum_exp);
3964 if (oursig == TARGET_SIGNAL_UNKNOWN)
3965 {
3966 /* No, try numeric. */
3967 oursig =
3968 target_signal_from_command (parse_and_eval_long (signum_exp));
3969 }
3970 sig_print_info (oursig);
3971 return;
3972 }
3973
3974 printf_filtered ("\n");
3975 /* These ugly casts brought to you by the native VAX compiler. */
3976 for (oursig = TARGET_SIGNAL_FIRST;
3977 (int) oursig < (int) TARGET_SIGNAL_LAST;
3978 oursig = (enum target_signal) ((int) oursig + 1))
3979 {
3980 QUIT;
3981
3982 if (oursig != TARGET_SIGNAL_UNKNOWN
3983 && oursig != TARGET_SIGNAL_DEFAULT
3984 && oursig != TARGET_SIGNAL_0)
3985 sig_print_info (oursig);
3986 }
3987
3988 printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
3989 }
3990 \f
3991 struct inferior_status
3992 {
3993 enum target_signal stop_signal;
3994 CORE_ADDR stop_pc;
3995 bpstat stop_bpstat;
3996 int stop_step;
3997 int stop_stack_dummy;
3998 int stopped_by_random_signal;
3999 int trap_expected;
4000 CORE_ADDR step_range_start;
4001 CORE_ADDR step_range_end;
4002 CORE_ADDR step_frame_address;
4003 enum step_over_calls_kind step_over_calls;
4004 CORE_ADDR step_resume_break_address;
4005 int stop_after_trap;
4006 int stop_soon_quietly;
4007 CORE_ADDR selected_frame_address;
4008 char *stop_registers;
4009
4010 /* These are here because if call_function_by_hand has written some
4011 registers and then decides to call error(), we better not have changed
4012 any registers. */
4013 char *registers;
4014
4015 int selected_level;
4016 int breakpoint_proceeded;
4017 int restore_stack_info;
4018 int proceed_to_finish;
4019 };
4020
4021 static struct inferior_status *
4022 xmalloc_inferior_status (void)
4023 {
4024 struct inferior_status *inf_status;
4025 inf_status = xmalloc (sizeof (struct inferior_status));
4026 inf_status->stop_registers = xmalloc (REGISTER_BYTES);
4027 inf_status->registers = xmalloc (REGISTER_BYTES);
4028 return inf_status;
4029 }
4030
4031 static void
4032 free_inferior_status (struct inferior_status *inf_status)
4033 {
4034 xfree (inf_status->registers);
4035 xfree (inf_status->stop_registers);
4036 xfree (inf_status);
4037 }
4038
4039 void
4040 write_inferior_status_register (struct inferior_status *inf_status, int regno,
4041 LONGEST val)
4042 {
4043 int size = REGISTER_RAW_SIZE (regno);
4044 void *buf = alloca (size);
4045 store_signed_integer (buf, size, val);
4046 memcpy (&inf_status->registers[REGISTER_BYTE (regno)], buf, size);
4047 }
4048
4049 /* Save all of the information associated with the inferior<==>gdb
4050 connection. INF_STATUS is a pointer to a "struct inferior_status"
4051 (defined in inferior.h). */
4052
4053 struct inferior_status *
4054 save_inferior_status (int restore_stack_info)
4055 {
4056 struct inferior_status *inf_status = xmalloc_inferior_status ();
4057
4058 inf_status->stop_signal = stop_signal;
4059 inf_status->stop_pc = stop_pc;
4060 inf_status->stop_step = stop_step;
4061 inf_status->stop_stack_dummy = stop_stack_dummy;
4062 inf_status->stopped_by_random_signal = stopped_by_random_signal;
4063 inf_status->trap_expected = trap_expected;
4064 inf_status->step_range_start = step_range_start;
4065 inf_status->step_range_end = step_range_end;
4066 inf_status->step_frame_address = step_frame_address;
4067 inf_status->step_over_calls = step_over_calls;
4068 inf_status->stop_after_trap = stop_after_trap;
4069 inf_status->stop_soon_quietly = stop_soon_quietly;
4070 /* Save original bpstat chain here; replace it with copy of chain.
4071 If caller's caller is walking the chain, they'll be happier if we
4072 hand them back the original chain when restore_inferior_status is
4073 called. */
4074 inf_status->stop_bpstat = stop_bpstat;
4075 stop_bpstat = bpstat_copy (stop_bpstat);
4076 inf_status->breakpoint_proceeded = breakpoint_proceeded;
4077 inf_status->restore_stack_info = restore_stack_info;
4078 inf_status->proceed_to_finish = proceed_to_finish;
4079
4080 memcpy (inf_status->stop_registers, stop_registers, REGISTER_BYTES);
4081
4082 read_register_bytes (0, inf_status->registers, REGISTER_BYTES);
4083
4084 record_selected_frame (&(inf_status->selected_frame_address),
4085 &(inf_status->selected_level));
4086 return inf_status;
4087 }
4088
4089 struct restore_selected_frame_args
4090 {
4091 CORE_ADDR frame_address;
4092 int level;
4093 };
4094
4095 static int
4096 restore_selected_frame (void *args)
4097 {
4098 struct restore_selected_frame_args *fr =
4099 (struct restore_selected_frame_args *) args;
4100 struct frame_info *frame;
4101 int level = fr->level;
4102
4103 frame = find_relative_frame (get_current_frame (), &level);
4104
4105 /* If inf_status->selected_frame_address is NULL, there was no
4106 previously selected frame. */
4107 if (frame == NULL ||
4108 /* FRAME_FP (frame) != fr->frame_address || */
4109 /* elz: deleted this check as a quick fix to the problem that
4110 for function called by hand gdb creates no internal frame
4111 structure and the real stack and gdb's idea of stack are
4112 different if nested calls by hands are made.
4113
4114 mvs: this worries me. */
4115 level != 0)
4116 {
4117 warning ("Unable to restore previously selected frame.\n");
4118 return 0;
4119 }
4120
4121 select_frame (frame, fr->level);
4122
4123 return (1);
4124 }
4125
4126 void
4127 restore_inferior_status (struct inferior_status *inf_status)
4128 {
4129 stop_signal = inf_status->stop_signal;
4130 stop_pc = inf_status->stop_pc;
4131 stop_step = inf_status->stop_step;
4132 stop_stack_dummy = inf_status->stop_stack_dummy;
4133 stopped_by_random_signal = inf_status->stopped_by_random_signal;
4134 trap_expected = inf_status->trap_expected;
4135 step_range_start = inf_status->step_range_start;
4136 step_range_end = inf_status->step_range_end;
4137 step_frame_address = inf_status->step_frame_address;
4138 step_over_calls = inf_status->step_over_calls;
4139 stop_after_trap = inf_status->stop_after_trap;
4140 stop_soon_quietly = inf_status->stop_soon_quietly;
4141 bpstat_clear (&stop_bpstat);
4142 stop_bpstat = inf_status->stop_bpstat;
4143 breakpoint_proceeded = inf_status->breakpoint_proceeded;
4144 proceed_to_finish = inf_status->proceed_to_finish;
4145
4146 /* FIXME: Is the restore of stop_registers always needed */
4147 memcpy (stop_registers, inf_status->stop_registers, REGISTER_BYTES);
4148
4149 /* The inferior can be gone if the user types "print exit(0)"
4150 (and perhaps other times). */
4151 if (target_has_execution)
4152 write_register_bytes (0, inf_status->registers, REGISTER_BYTES);
4153
4154 /* FIXME: If we are being called after stopping in a function which
4155 is called from gdb, we should not be trying to restore the
4156 selected frame; it just prints a spurious error message (The
4157 message is useful, however, in detecting bugs in gdb (like if gdb
4158 clobbers the stack)). In fact, should we be restoring the
4159 inferior status at all in that case? . */
4160
4161 if (target_has_stack && inf_status->restore_stack_info)
4162 {
4163 struct restore_selected_frame_args fr;
4164 fr.level = inf_status->selected_level;
4165 fr.frame_address = inf_status->selected_frame_address;
4166 /* The point of catch_errors is that if the stack is clobbered,
4167 walking the stack might encounter a garbage pointer and error()
4168 trying to dereference it. */
4169 if (catch_errors (restore_selected_frame, &fr,
4170 "Unable to restore previously selected frame:\n",
4171 RETURN_MASK_ERROR) == 0)
4172 /* Error in restoring the selected frame. Select the innermost
4173 frame. */
4174
4175
4176 select_frame (get_current_frame (), 0);
4177
4178 }
4179
4180 free_inferior_status (inf_status);
4181 }
4182
4183 static void
4184 do_restore_inferior_status_cleanup (void *sts)
4185 {
4186 restore_inferior_status (sts);
4187 }
4188
4189 struct cleanup *
4190 make_cleanup_restore_inferior_status (struct inferior_status *inf_status)
4191 {
4192 return make_cleanup (do_restore_inferior_status_cleanup, inf_status);
4193 }
4194
4195 void
4196 discard_inferior_status (struct inferior_status *inf_status)
4197 {
4198 /* See save_inferior_status for info on stop_bpstat. */
4199 bpstat_clear (&inf_status->stop_bpstat);
4200 free_inferior_status (inf_status);
4201 }
4202
4203 /* Oft used ptids */
4204 ptid_t null_ptid;
4205 ptid_t minus_one_ptid;
4206
4207 /* Create a ptid given the necessary PID, LWP, and TID components. */
4208
4209 ptid_t
4210 ptid_build (int pid, long lwp, long tid)
4211 {
4212 ptid_t ptid;
4213
4214 ptid.pid = pid;
4215 ptid.lwp = lwp;
4216 ptid.tid = tid;
4217 return ptid;
4218 }
4219
4220 /* Create a ptid from just a pid. */
4221
4222 ptid_t
4223 pid_to_ptid (int pid)
4224 {
4225 return ptid_build (pid, 0, 0);
4226 }
4227
4228 /* Fetch the pid (process id) component from a ptid. */
4229
4230 int
4231 ptid_get_pid (ptid_t ptid)
4232 {
4233 return ptid.pid;
4234 }
4235
4236 /* Fetch the lwp (lightweight process) component from a ptid. */
4237
4238 long
4239 ptid_get_lwp (ptid_t ptid)
4240 {
4241 return ptid.lwp;
4242 }
4243
4244 /* Fetch the tid (thread id) component from a ptid. */
4245
4246 long
4247 ptid_get_tid (ptid_t ptid)
4248 {
4249 return ptid.tid;
4250 }
4251
4252 /* ptid_equal() is used to test equality of two ptids. */
4253
4254 int
4255 ptid_equal (ptid_t ptid1, ptid_t ptid2)
4256 {
4257 return (ptid1.pid == ptid2.pid && ptid1.lwp == ptid2.lwp
4258 && ptid1.tid == ptid2.tid);
4259 }
4260
4261 /* restore_inferior_ptid() will be used by the cleanup machinery
4262 to restore the inferior_ptid value saved in a call to
4263 save_inferior_ptid(). */
4264
4265 static void
4266 restore_inferior_ptid (void *arg)
4267 {
4268 ptid_t *saved_ptid_ptr = arg;
4269 inferior_ptid = *saved_ptid_ptr;
4270 xfree (arg);
4271 }
4272
4273 /* Save the value of inferior_ptid so that it may be restored by a
4274 later call to do_cleanups(). Returns the struct cleanup pointer
4275 needed for later doing the cleanup. */
4276
4277 struct cleanup *
4278 save_inferior_ptid (void)
4279 {
4280 ptid_t *saved_ptid_ptr;
4281
4282 saved_ptid_ptr = xmalloc (sizeof (ptid_t));
4283 *saved_ptid_ptr = inferior_ptid;
4284 return make_cleanup (restore_inferior_ptid, saved_ptid_ptr);
4285 }
4286
4287 \f
4288 static void
4289 build_infrun (void)
4290 {
4291 stop_registers = xmalloc (REGISTER_BYTES);
4292 }
4293
4294 void
4295 _initialize_infrun (void)
4296 {
4297 register int i;
4298 register int numsigs;
4299 struct cmd_list_element *c;
4300
4301 build_infrun ();
4302
4303 register_gdbarch_swap (&stop_registers, sizeof (stop_registers), NULL);
4304 register_gdbarch_swap (NULL, 0, build_infrun);
4305
4306 add_info ("signals", signals_info,
4307 "What debugger does when program gets various signals.\n\
4308 Specify a signal as argument to print info on that signal only.");
4309 add_info_alias ("handle", "signals", 0);
4310
4311 add_com ("handle", class_run, handle_command,
4312 concat ("Specify how to handle a signal.\n\
4313 Args are signals and actions to apply to those signals.\n\
4314 Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
4315 from 1-15 are allowed for compatibility with old versions of GDB.\n\
4316 Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
4317 The special arg \"all\" is recognized to mean all signals except those\n\
4318 used by the debugger, typically SIGTRAP and SIGINT.\n",
4319 "Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
4320 \"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
4321 Stop means reenter debugger if this signal happens (implies print).\n\
4322 Print means print a message if this signal happens.\n\
4323 Pass means let program see this signal; otherwise program doesn't know.\n\
4324 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
4325 Pass and Stop may be combined.", NULL));
4326 if (xdb_commands)
4327 {
4328 add_com ("lz", class_info, signals_info,
4329 "What debugger does when program gets various signals.\n\
4330 Specify a signal as argument to print info on that signal only.");
4331 add_com ("z", class_run, xdb_handle_command,
4332 concat ("Specify how to handle a signal.\n\
4333 Args are signals and actions to apply to those signals.\n\
4334 Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
4335 from 1-15 are allowed for compatibility with old versions of GDB.\n\
4336 Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
4337 The special arg \"all\" is recognized to mean all signals except those\n\
4338 used by the debugger, typically SIGTRAP and SIGINT.\n",
4339 "Recognized actions include \"s\" (toggles between stop and nostop), \n\
4340 \"r\" (toggles between print and noprint), \"i\" (toggles between pass and \
4341 nopass), \"Q\" (noprint)\n\
4342 Stop means reenter debugger if this signal happens (implies print).\n\
4343 Print means print a message if this signal happens.\n\
4344 Pass means let program see this signal; otherwise program doesn't know.\n\
4345 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
4346 Pass and Stop may be combined.", NULL));
4347 }
4348
4349 if (!dbx_commands)
4350 stop_command = add_cmd ("stop", class_obscure, not_just_help_class_command,
4351 "There is no `stop' command, but you can set a hook on `stop'.\n\
4352 This allows you to set a list of commands to be run each time execution\n\
4353 of the program stops.", &cmdlist);
4354
4355 numsigs = (int) TARGET_SIGNAL_LAST;
4356 signal_stop = (unsigned char *)
4357 xmalloc (sizeof (signal_stop[0]) * numsigs);
4358 signal_print = (unsigned char *)
4359 xmalloc (sizeof (signal_print[0]) * numsigs);
4360 signal_program = (unsigned char *)
4361 xmalloc (sizeof (signal_program[0]) * numsigs);
4362 for (i = 0; i < numsigs; i++)
4363 {
4364 signal_stop[i] = 1;
4365 signal_print[i] = 1;
4366 signal_program[i] = 1;
4367 }
4368
4369 /* Signals caused by debugger's own actions
4370 should not be given to the program afterwards. */
4371 signal_program[TARGET_SIGNAL_TRAP] = 0;
4372 signal_program[TARGET_SIGNAL_INT] = 0;
4373
4374 /* Signals that are not errors should not normally enter the debugger. */
4375 signal_stop[TARGET_SIGNAL_ALRM] = 0;
4376 signal_print[TARGET_SIGNAL_ALRM] = 0;
4377 signal_stop[TARGET_SIGNAL_VTALRM] = 0;
4378 signal_print[TARGET_SIGNAL_VTALRM] = 0;
4379 signal_stop[TARGET_SIGNAL_PROF] = 0;
4380 signal_print[TARGET_SIGNAL_PROF] = 0;
4381 signal_stop[TARGET_SIGNAL_CHLD] = 0;
4382 signal_print[TARGET_SIGNAL_CHLD] = 0;
4383 signal_stop[TARGET_SIGNAL_IO] = 0;
4384 signal_print[TARGET_SIGNAL_IO] = 0;
4385 signal_stop[TARGET_SIGNAL_POLL] = 0;
4386 signal_print[TARGET_SIGNAL_POLL] = 0;
4387 signal_stop[TARGET_SIGNAL_URG] = 0;
4388 signal_print[TARGET_SIGNAL_URG] = 0;
4389 signal_stop[TARGET_SIGNAL_WINCH] = 0;
4390 signal_print[TARGET_SIGNAL_WINCH] = 0;
4391
4392 /* These signals are used internally by user-level thread
4393 implementations. (See signal(5) on Solaris.) Like the above
4394 signals, a healthy program receives and handles them as part of
4395 its normal operation. */
4396 signal_stop[TARGET_SIGNAL_LWP] = 0;
4397 signal_print[TARGET_SIGNAL_LWP] = 0;
4398 signal_stop[TARGET_SIGNAL_WAITING] = 0;
4399 signal_print[TARGET_SIGNAL_WAITING] = 0;
4400 signal_stop[TARGET_SIGNAL_CANCEL] = 0;
4401 signal_print[TARGET_SIGNAL_CANCEL] = 0;
4402
4403 #ifdef SOLIB_ADD
4404 add_show_from_set
4405 (add_set_cmd ("stop-on-solib-events", class_support, var_zinteger,
4406 (char *) &stop_on_solib_events,
4407 "Set stopping for shared library events.\n\
4408 If nonzero, gdb will give control to the user when the dynamic linker\n\
4409 notifies gdb of shared library events. The most common event of interest\n\
4410 to the user would be loading/unloading of a new library.\n",
4411 &setlist),
4412 &showlist);
4413 #endif
4414
4415 c = add_set_enum_cmd ("follow-fork-mode",
4416 class_run,
4417 follow_fork_mode_kind_names,
4418 &follow_fork_mode_string,
4419 /* ??rehrauer: The "both" option is broken, by what may be a 10.20
4420 kernel problem. It's also not terribly useful without a GUI to
4421 help the user drive two debuggers. So for now, I'm disabling
4422 the "both" option. */
4423 /* "Set debugger response to a program call of fork \
4424 or vfork.\n\
4425 A fork or vfork creates a new process. follow-fork-mode can be:\n\
4426 parent - the original process is debugged after a fork\n\
4427 child - the new process is debugged after a fork\n\
4428 both - both the parent and child are debugged after a fork\n\
4429 ask - the debugger will ask for one of the above choices\n\
4430 For \"both\", another copy of the debugger will be started to follow\n\
4431 the new child process. The original debugger will continue to follow\n\
4432 the original parent process. To distinguish their prompts, the\n\
4433 debugger copy's prompt will be changed.\n\
4434 For \"parent\" or \"child\", the unfollowed process will run free.\n\
4435 By default, the debugger will follow the parent process.",
4436 */
4437 "Set debugger response to a program call of fork \
4438 or vfork.\n\
4439 A fork or vfork creates a new process. follow-fork-mode can be:\n\
4440 parent - the original process is debugged after a fork\n\
4441 child - the new process is debugged after a fork\n\
4442 ask - the debugger will ask for one of the above choices\n\
4443 For \"parent\" or \"child\", the unfollowed process will run free.\n\
4444 By default, the debugger will follow the parent process.",
4445 &setlist);
4446 /* c->function.sfunc = ; */
4447 add_show_from_set (c, &showlist);
4448
4449 c = add_set_enum_cmd ("scheduler-locking", class_run,
4450 scheduler_enums, /* array of string names */
4451 &scheduler_mode, /* current mode */
4452 "Set mode for locking scheduler during execution.\n\
4453 off == no locking (threads may preempt at any time)\n\
4454 on == full locking (no thread except the current thread may run)\n\
4455 step == scheduler locked during every single-step operation.\n\
4456 In this mode, no other thread may run during a step command.\n\
4457 Other threads may run while stepping over a function call ('next').",
4458 &setlist);
4459
4460 c->function.sfunc = set_schedlock_func; /* traps on target vector */
4461 add_show_from_set (c, &showlist);
4462
4463 c = add_set_cmd ("step-mode", class_run,
4464 var_boolean, (char*) &step_stop_if_no_debug,
4465 "Set mode of the step operation. When set, doing a step over a\n\
4466 function without debug line information will stop at the first\n\
4467 instruction of that function. Otherwise, the function is skipped and\n\
4468 the step command stops at a different source line.",
4469 &setlist);
4470 add_show_from_set (c, &showlist);
4471
4472 /* ptid initializations */
4473 null_ptid = ptid_build (0, 0, 0);
4474 minus_one_ptid = ptid_build (-1, 0, 0);
4475 inferior_ptid = null_ptid;
4476 target_last_wait_ptid = minus_one_ptid;
4477 }
This page took 0.125505 seconds and 5 git commands to generate.