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