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