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