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