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