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