1 /* Target-struct-independent code to start (run) and stop an inferior process.
2 Copyright 1986, 1987, 1988, 1989, 1991, 1992, 1993
3 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* Notes on the algorithm used in wait_for_inferior to determine if we
22 just did a subroutine call when stepping. We have the following
23 information at that point:
25 Current and previous (just before this step) pc.
26 Current and previous sp.
27 Current and previous start of current function.
29 If the starts of the functions don't match, then
31 a) We did a subroutine call.
33 In this case, the pc will be at the beginning of a function.
35 b) We did a subroutine return.
41 If we did a longjump, we were doing "nexti", since a next would
42 have attempted to skip over the assembly language routine in which
43 the longjmp is coded and would have simply been the equivalent of a
44 continue. I consider this ok behaivior. We'd like one of two
45 things to happen if we are doing a nexti through the longjmp()
46 routine: 1) It behaves as a stepi, or 2) It acts like a continue as
47 above. Given that this is a special case, and that anybody who
48 thinks that the concept of sub calls is meaningful in the context
49 of a longjmp, I'll take either one. Let's see what happens.
51 Acts like a subroutine return. I can handle that with no problem
54 -->So: If the current and previous beginnings of the current
55 function don't match, *and* the pc is at the start of a function,
56 we've done a subroutine call. If the pc is not at the start of a
57 function, we *didn't* do a subroutine call.
59 -->If the beginnings of the current and previous function do match,
62 a) We just did a recursive call.
64 In this case, we would be at the very beginning of a
65 function and 1) it will have a prologue (don't jump to
66 before prologue, or 2) (we assume here that it doesn't have
67 a prologue) there will have been a change in the stack
68 pointer over the last instruction. (Ie. it's got to put
69 the saved pc somewhere. The stack is the usual place. In
70 a recursive call a register is only an option if there's a
71 prologue to do something with it. This is even true on
72 register window machines; the prologue sets up the new
73 window. It might not be true on a register window machine
74 where the call instruction moved the register window
75 itself. Hmmm. One would hope that the stack pointer would
76 also change. If it doesn't, somebody send me a note, and
77 I'll work out a more general theory.
78 bug-gdb@prep.ai.mit.edu). This is true (albeit slipperly
79 so) on all machines I'm aware of:
81 m68k: Call changes stack pointer. Regular jumps don't.
83 sparc: Recursive calls must have frames and therefor,
86 vax: All calls have frames and hence change the
89 b) We did a return from a recursive call. I don't see that we
90 have either the ability or the need to distinguish this
91 from an ordinary jump. The stack frame will be printed
92 when and if the frame pointer changes; if we are in a
93 function without a frame pointer, it's the users own
96 c) We did a jump within a function. We assume that this is
97 true if we didn't do a recursive call.
99 d) We are in no-man's land ("I see no symbols here"). We
100 don't worry about this; it will make calls look like simple
101 jumps (and the stack frames will be printed when the frame
102 pointer moves), which is a reasonably non-violent response.
110 #include "inferior.h"
111 #include "breakpoint.h"
119 /* unistd.h is needed to #define X_OK */
123 #include <sys/file.h>
126 /* Prototypes for local functions */
129 signals_info PARAMS ((char *, int));
132 handle_command PARAMS ((char *, int));
135 sig_print_info PARAMS ((int));
138 sig_print_header PARAMS ((void));
141 resume_cleanups PARAMS ((int));
144 hook_stop_stub PARAMS ((char *));
146 /* GET_LONGJMP_TARGET returns the PC at which longjmp() will resume the
147 program. It needs to examine the jmp_buf argument and extract the PC
148 from it. The return value is non-zero on success, zero otherwise. */
149 #ifndef GET_LONGJMP_TARGET
150 #define GET_LONGJMP_TARGET(PC_ADDR) 0
154 /* Some machines have trampoline code that sits between function callers
155 and the actual functions themselves. If this machine doesn't have
156 such things, disable their processing. */
157 #ifndef SKIP_TRAMPOLINE_CODE
158 #define SKIP_TRAMPOLINE_CODE(pc) 0
161 /* For SVR4 shared libraries, each call goes through a small piece of
162 trampoline code in the ".init" section. IN_SOLIB_TRAMPOLINE evaluates
163 to nonzero if we are current stopped in one of these. */
164 #ifndef IN_SOLIB_TRAMPOLINE
165 #define IN_SOLIB_TRAMPOLINE(pc,name) 0
168 /* On some systems, the PC may be left pointing at an instruction that won't
169 actually be executed. This is usually indicated by a bit in the PSW. If
170 we find ourselves in such a state, then we step the target beyond the
171 nullified instruction before returning control to the user so as to avoid
174 #ifndef INSTRUCTION_NULLIFIED
175 #define INSTRUCTION_NULLIFIED 0
178 /* Tables of how to react to signals; the user sets them. */
180 static unsigned char *signal_stop;
181 static unsigned char *signal_print;
182 static unsigned char *signal_program;
184 #define SET_SIGS(nsigs,sigs,flags) \
186 int signum = (nsigs); \
187 while (signum-- > 0) \
188 if ((sigs)[signum]) \
189 (flags)[signum] = 1; \
192 #define UNSET_SIGS(nsigs,sigs,flags) \
194 int signum = (nsigs); \
195 while (signum-- > 0) \
196 if ((sigs)[signum]) \
197 (flags)[signum] = 0; \
201 /* Command list pointer for the "stop" placeholder. */
203 static struct cmd_list_element *stop_command;
205 /* Nonzero if breakpoints are now inserted in the inferior. */
207 static int breakpoints_inserted;
209 /* Function inferior was in as of last step command. */
211 static struct symbol *step_start_function;
213 /* Nonzero if we are expecting a trace trap and should proceed from it. */
215 static int trap_expected;
217 /* Nonzero if the next time we try to continue the inferior, it will
218 step one instruction and generate a spurious trace trap.
219 This is used to compensate for a bug in HP-UX. */
221 static int trap_expected_after_continue;
223 /* Nonzero means expecting a trace trap
224 and should stop the inferior and return silently when it happens. */
228 /* Nonzero means expecting a trap and caller will handle it themselves.
229 It is used after attach, due to attaching to a process;
230 when running in the shell before the child program has been exec'd;
231 and when running some kinds of remote stuff (FIXME?). */
233 int stop_soon_quietly;
235 /* Nonzero if pc has been changed by the debugger
236 since the inferior stopped. */
240 /* Nonzero if proceed is being used for a "finish" command or a similar
241 situation when stop_registers should be saved. */
243 int proceed_to_finish;
245 /* Save register contents here when about to pop a stack dummy frame,
246 if-and-only-if proceed_to_finish is set.
247 Thus this contains the return value from the called function (assuming
248 values are returned in a register). */
250 char stop_registers[REGISTER_BYTES];
252 /* Nonzero if program stopped due to error trying to insert breakpoints. */
254 static int breakpoints_failed;
256 /* Nonzero after stop if current stack frame should be printed. */
258 static int stop_print_frame;
260 #ifdef NO_SINGLE_STEP
261 extern int one_stepped; /* From machine dependent code */
262 extern void single_step (); /* Same. */
263 #endif /* NO_SINGLE_STEP */
266 /* Things to clean up if we QUIT out of resume (). */
269 resume_cleanups (arg)
275 /* Resume the inferior, but allow a QUIT. This is useful if the user
276 wants to interrupt some lengthy single-stepping operation
277 (for child processes, the SIGINT goes to the inferior, and so
278 we get a SIGINT random_signal, but for remote debugging and perhaps
279 other targets, that's not true).
281 STEP nonzero if we should step (zero to continue instead).
282 SIG is the signal to give the inferior (zero for none). */
288 struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
291 #ifdef NO_SINGLE_STEP
293 single_step(sig); /* Do it the hard way, w/temp breakpoints */
294 step = 0; /* ...and don't ask hardware to do it. */
298 /* Handle any optimized stores to the inferior NOW... */
299 #ifdef DO_DEFERRED_STORES
303 target_resume (step, sig);
304 discard_cleanups (old_cleanups);
308 /* Clear out all variables saying what to do when inferior is continued.
309 First do this, then set the ones you want, then call `proceed'. */
312 clear_proceed_status ()
315 step_range_start = 0;
317 step_frame_address = 0;
318 step_over_calls = -1;
320 stop_soon_quietly = 0;
321 proceed_to_finish = 0;
322 breakpoint_proceeded = 1; /* We're about to proceed... */
324 /* Discard any remaining commands or status from previous stop. */
325 bpstat_clear (&stop_bpstat);
328 /* Basic routine for continuing the program in various fashions.
330 ADDR is the address to resume at, or -1 for resume where stopped.
331 SIGGNAL is the signal to give it, or 0 for none,
332 or -1 for act according to how it stopped.
333 STEP is nonzero if should trap after one instruction.
334 -1 means return after that and print nothing.
335 You should probably set various step_... variables
336 before calling here, if you are stepping.
338 You should call clear_proceed_status before calling proceed. */
341 proceed (addr, siggnal, step)
349 step_start_function = find_pc_function (read_pc ());
353 if (addr == (CORE_ADDR)-1)
355 /* If there is a breakpoint at the address we will resume at,
356 step one instruction before inserting breakpoints
357 so that we do not stop right away. */
359 if (!pc_changed && breakpoint_here_p (read_pc ()))
365 if (trap_expected_after_continue)
367 /* If (step == 0), a trap will be automatically generated after
368 the first instruction is executed. Force step one
369 instruction to clear this condition. This should not occur
370 if step is nonzero, but it is harmless in that case. */
372 trap_expected_after_continue = 0;
376 /* We will get a trace trap after one instruction.
377 Continue it automatically and insert breakpoints then. */
381 int temp = insert_breakpoints ();
384 print_sys_errmsg ("ptrace", temp);
385 error ("Cannot insert breakpoints.\n\
386 The same program may be running in another process.");
388 breakpoints_inserted = 1;
391 /* Install inferior's terminal modes. */
392 target_terminal_inferior ();
395 stop_signal = siggnal;
396 /* If this signal should not be seen by program,
397 give it zero. Used for debugging signals. */
398 else if (stop_signal < NSIG && !signal_program[stop_signal])
401 /* Resume inferior. */
402 resume (oneproc || step || bpstat_should_step (), stop_signal);
404 /* Wait for it to stop (if not standalone)
405 and in any case decode why it stopped, and act accordingly. */
407 wait_for_inferior ();
411 /* Record the pc and sp of the program the last time it stopped.
412 These are just used internally by wait_for_inferior, but need
413 to be preserved over calls to it and cleared when the inferior
415 static CORE_ADDR prev_pc;
416 static CORE_ADDR prev_sp;
417 static CORE_ADDR prev_func_start;
418 static char *prev_func_name;
421 /* Start remote-debugging of a machine over a serial link. */
426 init_wait_for_inferior ();
427 clear_proceed_status ();
428 stop_soon_quietly = 1;
430 wait_for_inferior ();
434 /* Initialize static vars when a new inferior begins. */
437 init_wait_for_inferior ()
439 /* These are meaningless until the first time through wait_for_inferior. */
443 prev_func_name = NULL;
445 trap_expected_after_continue = 0;
446 breakpoints_inserted = 0;
447 mark_breakpoints_out ();
448 stop_signal = 0; /* Don't confuse first call to proceed(). */
452 delete_breakpoint_current_contents (arg)
455 struct breakpoint **breakpointp = (struct breakpoint **)arg;
456 if (*breakpointp != NULL)
457 delete_breakpoint (*breakpointp);
460 /* Wait for control to return from inferior to debugger.
461 If inferior gets a signal, we may decide to start it up again
462 instead of returning. That is why there is a loop in this function.
463 When this function actually returns it means the inferior
464 should be left stopped and GDB should read more commands. */
469 struct cleanup *old_cleanups;
474 CORE_ADDR stop_func_start;
475 char *stop_func_name;
476 CORE_ADDR prologue_pc, tmp;
477 struct symtab_and_line sal;
478 int remove_breakpoints_on_following_step = 0;
480 int handling_longjmp = 0; /* FIXME */
481 struct symtab *symtab;
482 struct breakpoint *step_resume_breakpoint = NULL;
484 old_cleanups = make_cleanup (delete_breakpoint_current_contents,
485 &step_resume_breakpoint);
486 sal = find_pc_line(prev_pc, 0);
487 current_line = sal.line;
491 /* Clean up saved state that will become invalid. */
493 flush_cached_frames ();
494 registers_changed ();
498 #ifdef SIGTRAP_STOP_AFTER_LOAD
500 /* Somebody called load(2), and it gave us a "trap signal after load".
501 Ignore it gracefully. */
503 SIGTRAP_STOP_AFTER_LOAD (w);
506 /* See if the process still exists; clean up if it doesn't. */
509 target_terminal_ours (); /* Must do this before mourn anyway */
511 printf_filtered ("\nProgram exited with code 0%o.\n",
512 (unsigned int)WEXITSTATUS (w));
515 printf_filtered ("\nProgram exited normally.\n");
517 target_mourn_inferior ();
518 #ifdef NO_SINGLE_STEP
521 stop_print_frame = 0;
524 else if (!WIFSTOPPED (w))
528 stop_print_frame = 0;
529 stop_signal = WTERMSIG (w);
530 target_terminal_ours (); /* Must do this before mourn anyway */
531 target_kill (); /* kill mourns as well */
532 #ifdef PRINT_RANDOM_SIGNAL
533 printf_filtered ("\nProgram terminated: ");
534 PRINT_RANDOM_SIGNAL (stop_signal);
536 printf_filtered ("\nProgram terminated with signal ");
537 signame = strsigno (stop_signal);
539 printf_filtered ("%d", stop_signal);
541 /* Do we need to print the number in addition to the name? */
542 printf_filtered ("%s (%d)", signame, stop_signal);
543 printf_filtered (", %s\n", safe_strsignal (stop_signal));
545 printf_filtered ("The program no longer exists.\n");
547 #ifdef NO_SINGLE_STEP
553 #ifdef NO_SINGLE_STEP
555 single_step (0); /* This actually cleans up the ss */
556 #endif /* NO_SINGLE_STEP */
558 /* If PC is pointing at a nullified instruction, then step beyond it so that
559 the user won't be confused when GDB appears to be ready to execute it. */
561 if (INSTRUCTION_NULLIFIED)
567 stop_pc = read_pc ();
568 set_current_frame ( create_new_frame (read_fp (),
571 stop_frame_address = FRAME_FP (get_current_frame ());
572 stop_sp = read_sp ();
575 /* Don't care about return value; stop_func_start and stop_func_name
576 will both be 0 if it doesn't work. */
577 find_pc_partial_function (stop_pc, &stop_func_name, &stop_func_start);
578 stop_func_start += FUNCTION_START_OFFSET;
580 bpstat_clear (&stop_bpstat);
582 stop_stack_dummy = 0;
583 stop_print_frame = 1;
585 stopped_by_random_signal = 0;
586 breakpoints_failed = 0;
588 /* Look at the cause of the stop, and decide what to do.
589 The alternatives are:
590 1) break; to really stop and return to the debugger,
591 2) drop through to start up again
592 (set another_trap to 1 to single step once)
593 3) set random_signal to 1, and the decision between 1 and 2
594 will be made according to the signal handling tables. */
596 stop_signal = WSTOPSIG (w);
598 /* First, distinguish signals caused by the debugger from signals
599 that have to do with the program's own actions.
600 Note that breakpoint insns may cause SIGTRAP or SIGILL
601 or SIGEMT, depending on the operating system version.
602 Here we detect when a SIGILL or SIGEMT is really a breakpoint
603 and change it to SIGTRAP. */
605 if (stop_signal == SIGTRAP
606 || (breakpoints_inserted &&
607 (stop_signal == SIGILL
609 || stop_signal == SIGEMT
612 || stop_soon_quietly)
614 if (stop_signal == SIGTRAP && stop_after_trap)
616 stop_print_frame = 0;
619 if (stop_soon_quietly)
622 /* Don't even think about breakpoints
623 if just proceeded over a breakpoint.
625 However, if we are trying to proceed over a breakpoint
626 and end up in sigtramp, then step_resume_breakpoint
627 will be set and we should check whether we've hit the
629 if (stop_signal == SIGTRAP && trap_expected
630 && step_resume_breakpoint == NULL)
631 bpstat_clear (&stop_bpstat);
634 /* See if there is a breakpoint at the current PC. */
635 #if DECR_PC_AFTER_BREAK
636 /* Notice the case of stepping through a jump
637 that lands just after a breakpoint.
638 Don't confuse that with hitting the breakpoint.
639 What we check for is that 1) stepping is going on
640 and 2) the pc before the last insn does not match
641 the address of the breakpoint before the current pc. */
642 if (prev_pc == stop_pc - DECR_PC_AFTER_BREAK
644 || step_resume_breakpoint != NULL
645 || handling_longjmp /* FIXME */)
646 #endif /* DECR_PC_AFTER_BREAK not zero */
649 bpstat_stop_status (&stop_pc, stop_frame_address);
650 /* Following in case break condition called a
652 stop_print_frame = 1;
656 if (stop_signal == SIGTRAP)
658 = !(bpstat_explains_signal (stop_bpstat)
660 || PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address)
661 || (step_range_end && step_resume_breakpoint == NULL));
665 = !(bpstat_explains_signal (stop_bpstat)
666 /* End of a stack dummy. Some systems (e.g. Sony
667 news) give another signal besides SIGTRAP,
668 so check here as well as above. */
669 || PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address)
672 stop_signal = SIGTRAP;
678 /* For the program's own signals, act according to
679 the signal handling tables. */
683 /* Signal not for debugging purposes. */
686 stopped_by_random_signal = 1;
688 if (stop_signal >= NSIG
689 || signal_print[stop_signal])
693 target_terminal_ours_for_output ();
694 #ifdef PRINT_RANDOM_SIGNAL
695 PRINT_RANDOM_SIGNAL (stop_signal);
697 printf_filtered ("\nProgram received signal ");
698 signame = strsigno (stop_signal);
700 printf_filtered ("%d", stop_signal);
702 /* Do we need to print the number as well as the name? */
703 printf_filtered ("%s (%d)", signame, stop_signal);
704 printf_filtered (", %s\n", safe_strsignal (stop_signal));
705 #endif /* PRINT_RANDOM_SIGNAL */
708 if (stop_signal >= NSIG
709 || signal_stop[stop_signal])
711 /* If not going to stop, give terminal back
712 if we took it away. */
714 target_terminal_inferior ();
716 /* Clear the signal if it should not be passed. */
717 if (signal_program[stop_signal] == 0)
720 /* I'm not sure whether this needs to be check_sigtramp2 or
721 whether it could/should be keep_going. */
722 goto check_sigtramp2;
725 /* Handle cases caused by hitting a breakpoint. */
727 CORE_ADDR jmp_buf_pc;
728 struct bpstat_what what;
730 what = bpstat_what (stop_bpstat);
732 switch (what.main_action)
734 case BPSTAT_WHAT_SET_LONGJMP_RESUME:
735 /* If we hit the breakpoint at longjmp, disable it for the
736 duration of this command. Then, install a temporary
737 breakpoint at the target of the jmp_buf. */
738 disable_longjmp_breakpoint();
739 remove_breakpoints ();
740 breakpoints_inserted = 0;
741 if (!GET_LONGJMP_TARGET(&jmp_buf_pc)) goto keep_going;
743 /* Need to blow away step-resume breakpoint, as it
744 interferes with us */
745 if (step_resume_breakpoint != NULL)
747 delete_breakpoint (step_resume_breakpoint);
748 step_resume_breakpoint = NULL;
749 what.step_resume = 0;
753 /* FIXME - Need to implement nested temporary breakpoints */
754 if (step_over_calls > 0)
755 set_longjmp_resume_breakpoint(jmp_buf_pc,
756 get_current_frame());
759 set_longjmp_resume_breakpoint(jmp_buf_pc, NULL);
760 handling_longjmp = 1; /* FIXME */
763 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
764 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME_SINGLE:
765 remove_breakpoints ();
766 breakpoints_inserted = 0;
768 /* FIXME - Need to implement nested temporary breakpoints */
770 && (stop_frame_address
771 INNER_THAN step_frame_address))
777 disable_longjmp_breakpoint();
778 handling_longjmp = 0; /* FIXME */
779 if (what.main_action == BPSTAT_WHAT_CLEAR_LONGJMP_RESUME)
781 /* else fallthrough */
783 case BPSTAT_WHAT_SINGLE:
784 if (breakpoints_inserted)
785 remove_breakpoints ();
786 breakpoints_inserted = 0;
788 /* Still need to check other stuff, at least the case
789 where we are stepping and step out of the right range. */
792 case BPSTAT_WHAT_STOP_NOISY:
793 stop_print_frame = 1;
794 /* We are about to nuke the step_resume_breakpoint via the
795 cleanup chain, so no need to worry about it here. */
798 case BPSTAT_WHAT_STOP_SILENT:
799 stop_print_frame = 0;
800 /* We are about to nuke the step_resume_breakpoint via the
801 cleanup chain, so no need to worry about it here. */
804 case BPSTAT_WHAT_KEEP_CHECKING:
808 if (what.step_resume)
810 delete_breakpoint (step_resume_breakpoint);
811 step_resume_breakpoint = NULL;
813 /* If were waiting for a trap, hitting the step_resume_break
814 doesn't count as getting it. */
820 /* We come here if we hit a breakpoint but should not
821 stop for it. Possibly we also were stepping
822 and should stop for that. So fall through and
823 test for stepping. But, if not stepping,
826 /* If this is the breakpoint at the end of a stack dummy,
827 just stop silently. */
828 if (PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address))
830 stop_print_frame = 0;
831 stop_stack_dummy = 1;
833 trap_expected_after_continue = 1;
838 if (step_resume_breakpoint)
839 /* Having a step-resume breakpoint overrides anything
840 else having to do with stepping commands until
841 that breakpoint is reached. */
842 /* I suspect this could/should be keep_going, because if the
843 check_sigtramp2 check succeeds, then it will put in another
844 step_resume_breakpoint, and we aren't (yet) prepared to nest
846 goto check_sigtramp2;
848 if (step_range_end == 0)
849 /* Likewise if we aren't even stepping. */
850 /* I'm not sure whether this needs to be check_sigtramp2 or
851 whether it could/should be keep_going. */
852 goto check_sigtramp2;
854 /* If stepping through a line, keep going if still within it. */
855 if (stop_pc >= step_range_start
856 && stop_pc < step_range_end
857 /* The step range might include the start of the
858 function, so if we are at the start of the
859 step range and either the stack or frame pointers
860 just changed, we've stepped outside */
861 && !(stop_pc == step_range_start
862 && stop_frame_address
863 && (stop_sp INNER_THAN prev_sp
864 || stop_frame_address != step_frame_address)))
866 /* We might be doing a BPSTAT_WHAT_SINGLE and getting a signal.
867 So definately need to check for sigtramp here. */
868 goto check_sigtramp2;
871 /* We stepped out of the stepping range. See if that was due
872 to a subroutine call that we should proceed to the end of. */
874 /* Did we just take a signal? */
875 if (IN_SIGTRAMP (stop_pc, stop_func_name)
876 && !IN_SIGTRAMP (prev_pc, prev_func_name))
878 /* This code is needed at least in the following case:
879 The user types "next" and then a signal arrives (before
880 the "next" is done). */
881 /* We've just taken a signal; go until we are back to
882 the point where we took it and one more. */
884 struct symtab_and_line sr_sal;
887 sr_sal.symtab = NULL;
889 step_resume_breakpoint =
890 set_momentary_breakpoint (sr_sal, get_current_frame (),
892 if (breakpoints_inserted)
893 insert_breakpoints ();
896 /* If this is stepi or nexti, make sure that the stepping range
897 gets us past that instruction. */
898 if (step_range_end == 1)
899 /* FIXME: Does this run afoul of the code below which, if
900 we step into the middle of a line, resets the stepping
902 step_range_end = (step_range_start = prev_pc) + 1;
904 remove_breakpoints_on_following_step = 1;
910 /* Do this after the IN_SIGTRAMP check; it might give
912 prologue_pc = stop_func_start;
913 SKIP_PROLOGUE (prologue_pc);
916 /* ==> See comments at top of file on this algorithm. <==*/
918 if ((stop_pc == stop_func_start
919 || IN_SOLIB_TRAMPOLINE (stop_pc, stop_func_name))
920 && (stop_func_start != prev_func_start
921 || prologue_pc != stop_func_start
922 || stop_sp != prev_sp))
924 /* It's a subroutine call. */
926 if (step_over_calls == 0)
928 /* I presume that step_over_calls is only 0 when we're
929 supposed to be stepping at the assembly language level
930 ("stepi"). Just stop. */
935 if (step_over_calls > 0)
936 /* We're doing a "next". */
937 goto step_over_function;
939 /* If we are in a function call trampoline (a stub between
940 the calling routine and the real function), locate the real
941 function. That's what tells us (a) whether we want to step
942 into it at all, and (b) what prologue we want to run to
943 the end of, if we do step into it. */
944 tmp = SKIP_TRAMPOLINE_CODE (stop_pc);
946 stop_func_start = tmp;
948 /* If we have line number information for the function we
949 are thinking of stepping into, step into it.
951 If there are several symtabs at that PC (e.g. with include
952 files), just want to know whether *any* of them have line
953 numbers. find_pc_line handles this. */
955 struct symtab_and_line tmp_sal;
957 tmp_sal = find_pc_line (stop_func_start, 0);
958 if (tmp_sal.line != 0)
959 goto step_into_function;
963 /* A subroutine call has happened. */
965 /* Set a special breakpoint after the return */
966 struct symtab_and_line sr_sal;
969 (SAVED_PC_AFTER_CALL (get_current_frame ()));
970 sr_sal.symtab = NULL;
972 step_resume_breakpoint =
973 set_momentary_breakpoint (sr_sal, get_current_frame (),
975 if (breakpoints_inserted)
976 insert_breakpoints ();
981 /* Subroutine call with source code we should not step over.
982 Do step to the first line of code in it. */
983 SKIP_PROLOGUE (stop_func_start);
984 sal = find_pc_line (stop_func_start, 0);
985 /* Use the step_resume_break to step until
986 the end of the prologue, even if that involves jumps
987 (as it seems to on the vax under 4.2). */
988 /* If the prologue ends in the middle of a source line,
989 continue to the end of that source line.
990 Otherwise, just go to end of prologue. */
991 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
992 /* no, don't either. It skips any code that's
993 legitimately on the first line. */
995 if (sal.end && sal.pc != stop_func_start)
996 stop_func_start = sal.end;
999 if (stop_func_start == stop_pc)
1001 /* We are already there: stop now. */
1006 /* Put the step-breakpoint there and go until there. */
1008 struct symtab_and_line sr_sal;
1010 sr_sal.pc = stop_func_start;
1011 sr_sal.symtab = NULL;
1013 /* Do not specify what the fp should be when we stop
1014 since on some machines the prologue
1015 is where the new fp value is established. */
1016 step_resume_breakpoint =
1017 set_momentary_breakpoint (sr_sal, (CORE_ADDR)0,
1019 if (breakpoints_inserted)
1020 insert_breakpoints ();
1022 /* And make sure stepping stops right away then. */
1023 step_range_end = step_range_start;
1028 /* We've wandered out of the step range (but haven't done a
1029 subroutine call or return). (Is that true? I think we get
1030 here if we did a return and maybe a longjmp). */
1032 sal = find_pc_line(stop_pc, 0);
1034 if (step_range_end == 1)
1036 /* It is stepi or nexti. We always want to stop stepping after
1044 /* We have no line number information. That means to stop
1045 stepping (does this always happen right after one instruction,
1046 when we do "s" in a function with no line numbers,
1047 or can this happen as a result of a return or longjmp?). */
1052 if (stop_pc == sal.pc && current_line != sal.line)
1054 /* We are at the start of a different line. So stop. Note that
1055 we don't stop if we step into the middle of a different line.
1056 That is said to make things like for (;;) statements work
1062 /* We aren't done stepping.
1064 Optimize by setting the stepping range to the line.
1065 (We might not be in the original line, but if we entered a
1066 new line in mid-statement, we continue stepping. This makes
1067 things like for(;;) statements work better.) */
1068 step_range_start = sal.pc;
1069 step_range_end = sal.end;
1074 && IN_SIGTRAMP (stop_pc, stop_func_name)
1075 && !IN_SIGTRAMP (prev_pc, prev_func_name))
1077 /* What has happened here is that we have just stepped the inferior
1078 with a signal (because it is a signal which shouldn't make
1079 us stop), thus stepping into sigtramp.
1081 So we need to set a step_resume_break_address breakpoint
1082 and continue until we hit it, and then step. FIXME: This should
1083 be more enduring than a step_resume breakpoint; we should know
1084 that we will later need to keep going rather than re-hitting
1085 the breakpoint here (see testsuite/gdb.t06/signals.exp where
1086 it says "exceedingly difficult"). */
1087 struct symtab_and_line sr_sal;
1089 sr_sal.pc = prev_pc;
1090 sr_sal.symtab = NULL;
1092 step_resume_breakpoint =
1093 set_momentary_breakpoint (sr_sal, get_current_frame (),
1095 if (breakpoints_inserted)
1096 insert_breakpoints ();
1098 remove_breakpoints_on_following_step = 1;
1103 /* Come to this label when you need to resume the inferior.
1104 It's really much cleaner to do a goto than a maze of if-else
1107 /* Save the pc before execution, to compare with pc after stop. */
1108 prev_pc = read_pc (); /* Might have been DECR_AFTER_BREAK */
1109 prev_func_start = stop_func_start; /* Ok, since if DECR_PC_AFTER
1110 BREAK is defined, the
1111 original pc would not have
1112 been at the start of a
1114 prev_func_name = stop_func_name;
1117 /* If we did not do break;, it means we should keep
1118 running the inferior and not return to debugger. */
1120 if (trap_expected && stop_signal != SIGTRAP)
1122 /* We took a signal (which we are supposed to pass through to
1123 the inferior, else we'd have done a break above) and we
1124 haven't yet gotten our trap. Simply continue. */
1125 resume ((step_range_end && step_resume_breakpoint == NULL)
1126 || (trap_expected && step_resume_breakpoint == NULL)
1127 || bpstat_should_step (),
1132 /* Either the trap was not expected, but we are continuing
1133 anyway (the user asked that this signal be passed to the
1136 The signal was SIGTRAP, e.g. it was our signal, but we
1137 decided we should resume from it.
1139 We're going to run this baby now!
1141 Insert breakpoints now, unless we are trying
1142 to one-proceed past a breakpoint. */
1143 /* If we've just finished a special step resume and we don't
1144 want to hit a breakpoint, pull em out. */
1145 if (step_resume_breakpoint == NULL &&
1146 remove_breakpoints_on_following_step)
1148 remove_breakpoints_on_following_step = 0;
1149 remove_breakpoints ();
1150 breakpoints_inserted = 0;
1152 else if (!breakpoints_inserted &&
1153 (step_resume_breakpoint != NULL || !another_trap))
1155 breakpoints_failed = insert_breakpoints ();
1156 if (breakpoints_failed)
1158 breakpoints_inserted = 1;
1161 trap_expected = another_trap;
1163 if (stop_signal == SIGTRAP)
1166 #ifdef SHIFT_INST_REGS
1167 /* I'm not sure when this following segment applies. I do know, now,
1168 that we shouldn't rewrite the regs when we were stopped by a
1169 random signal from the inferior process. */
1171 if (!bpstat_explains_signal (stop_bpstat)
1172 && (stop_signal != SIGCLD)
1173 && !stopped_by_random_signal)
1175 CORE_ADDR pc_contents = read_register (PC_REGNUM);
1176 CORE_ADDR npc_contents = read_register (NPC_REGNUM);
1177 if (pc_contents != npc_contents)
1179 write_register (NNPC_REGNUM, npc_contents);
1180 write_register (NPC_REGNUM, pc_contents);
1183 #endif /* SHIFT_INST_REGS */
1185 resume ((step_resume_breakpoint == NULL
1186 && !handling_longjmp
1189 || bpstat_should_step (),
1195 if (target_has_execution)
1197 /* Assuming the inferior still exists, set these up for next
1198 time, just like we did above if we didn't break out of the
1200 prev_pc = read_pc ();
1201 prev_func_start = stop_func_start;
1202 prev_func_name = stop_func_name;
1205 do_cleanups (old_cleanups);
1208 /* Here to return control to GDB when the inferior stops for real.
1209 Print appropriate messages, remove breakpoints, give terminal our modes.
1211 STOP_PRINT_FRAME nonzero means print the executing frame
1212 (pc, function, args, file, line number and line text).
1213 BREAKPOINTS_FAILED nonzero means stop was due to error
1214 attempting to insert breakpoints. */
1219 /* Make sure that the current_frame's pc is correct. This
1220 is a correction for setting up the frame info before doing
1221 DECR_PC_AFTER_BREAK */
1222 if (target_has_execution)
1223 (get_current_frame ())->pc = read_pc ();
1225 if (breakpoints_failed)
1227 target_terminal_ours_for_output ();
1228 print_sys_errmsg ("ptrace", breakpoints_failed);
1229 printf_filtered ("Stopped; cannot insert breakpoints.\n\
1230 The same program may be running in another process.\n");
1233 if (target_has_execution && breakpoints_inserted)
1234 if (remove_breakpoints ())
1236 target_terminal_ours_for_output ();
1237 printf_filtered ("Cannot remove breakpoints because program is no longer writable.\n\
1238 It might be running in another process.\n\
1239 Further execution is probably impossible.\n");
1242 breakpoints_inserted = 0;
1244 /* Delete the breakpoint we stopped at, if it wants to be deleted.
1245 Delete any breakpoint that is to be deleted at the next stop. */
1247 breakpoint_auto_delete (stop_bpstat);
1249 /* If an auto-display called a function and that got a signal,
1250 delete that auto-display to avoid an infinite recursion. */
1252 if (stopped_by_random_signal)
1253 disable_current_display ();
1255 if (step_multi && stop_step)
1258 target_terminal_ours ();
1260 /* Look up the hook_stop and run it if it exists. */
1262 if (stop_command->hook)
1264 catch_errors (hook_stop_stub, (char *)stop_command->hook,
1265 "Error while running hook_stop:\n", RETURN_MASK_ALL);
1268 if (!target_has_stack)
1271 /* Select innermost stack frame except on return from a stack dummy routine,
1272 or if the program has exited. Print it without a level number if
1273 we have changed functions or hit a breakpoint. Print source line
1275 if (!stop_stack_dummy)
1277 select_frame (get_current_frame (), 0);
1279 if (stop_print_frame)
1283 source_only = bpstat_print (stop_bpstat);
1284 source_only = source_only ||
1286 && step_frame_address == stop_frame_address
1287 && step_start_function == find_pc_function (stop_pc));
1289 print_stack_frame (selected_frame, -1, source_only? -1: 1);
1291 /* Display the auto-display expressions. */
1296 /* Save the function value return registers, if we care.
1297 We might be about to restore their previous contents. */
1298 if (proceed_to_finish)
1299 read_register_bytes (0, stop_registers, REGISTER_BYTES);
1301 if (stop_stack_dummy)
1303 /* Pop the empty frame that contains the stack dummy.
1304 POP_FRAME ends with a setting of the current frame, so we
1305 can use that next. */
1307 select_frame (get_current_frame (), 0);
1312 hook_stop_stub (cmd)
1315 execute_user_command ((struct cmd_list_element *)cmd, 0);
1319 int signal_stop_state (signo)
1322 return ((signo >= 0 && signo < NSIG) ? signal_stop[signo] : 0);
1325 int signal_print_state (signo)
1328 return ((signo >= 0 && signo < NSIG) ? signal_print[signo] : 0);
1331 int signal_pass_state (signo)
1334 return ((signo >= 0 && signo < NSIG) ? signal_program[signo] : 0);
1340 printf_filtered ("Signal\t\tStop\tPrint\tPass to program\tDescription\n");
1344 sig_print_info (number)
1349 if ((name = strsigno (number)) == NULL)
1350 printf_filtered ("%d\t\t", number);
1352 printf_filtered ("%s (%d)\t", name, number);
1353 printf_filtered ("%s\t", signal_stop[number] ? "Yes" : "No");
1354 printf_filtered ("%s\t", signal_print[number] ? "Yes" : "No");
1355 printf_filtered ("%s\t\t", signal_program[number] ? "Yes" : "No");
1356 printf_filtered ("%s\n", safe_strsignal (number));
1359 /* Specify how various signals in the inferior should be handled. */
1362 handle_command (args, from_tty)
1367 int digits, wordlen;
1368 int sigfirst, signum, siglast;
1371 unsigned char *sigs;
1372 struct cleanup *old_chain;
1376 error_no_arg ("signal to handle");
1379 /* Allocate and zero an array of flags for which signals to handle. */
1381 nsigs = signo_max () + 1;
1382 sigs = (unsigned char *) alloca (nsigs);
1383 memset (sigs, 0, nsigs);
1385 /* Break the command line up into args. */
1387 argv = buildargv (args);
1392 old_chain = make_cleanup (freeargv, (char *) argv);
1394 /* Walk through the args, looking for signal numbers, signal names, and
1395 actions. Signal numbers and signal names may be interspersed with
1396 actions, with the actions being performed for all signals cumulatively
1397 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
1399 while (*argv != NULL)
1401 wordlen = strlen (*argv);
1402 for (digits = 0; isdigit ((*argv)[digits]); digits++) {;}
1404 sigfirst = siglast = -1;
1406 if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
1408 /* Apply action to all signals except those used by the
1409 debugger. Silently skip those. */
1412 siglast = nsigs - 1;
1414 else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
1416 SET_SIGS (nsigs, sigs, signal_stop);
1417 SET_SIGS (nsigs, sigs, signal_print);
1419 else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
1421 UNSET_SIGS (nsigs, sigs, signal_program);
1423 else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
1425 SET_SIGS (nsigs, sigs, signal_print);
1427 else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
1429 SET_SIGS (nsigs, sigs, signal_program);
1431 else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
1433 UNSET_SIGS (nsigs, sigs, signal_stop);
1435 else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
1437 SET_SIGS (nsigs, sigs, signal_program);
1439 else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
1441 UNSET_SIGS (nsigs, sigs, signal_print);
1442 UNSET_SIGS (nsigs, sigs, signal_stop);
1444 else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
1446 UNSET_SIGS (nsigs, sigs, signal_program);
1448 else if (digits > 0)
1450 sigfirst = siglast = atoi (*argv);
1451 if ((*argv)[digits] == '-')
1453 siglast = atoi ((*argv) + digits + 1);
1455 if (sigfirst > siglast)
1457 /* Bet he didn't figure we'd think of this case... */
1462 if (sigfirst < 0 || sigfirst >= nsigs)
1464 error ("Signal %d not in range 0-%d", sigfirst, nsigs - 1);
1466 if (siglast < 0 || siglast >= nsigs)
1468 error ("Signal %d not in range 0-%d", siglast, nsigs - 1);
1471 else if ((signum = strtosigno (*argv)) != 0)
1473 sigfirst = siglast = signum;
1477 /* Not a number and not a recognized flag word => complain. */
1478 error ("Unrecognized or ambiguous flag word: \"%s\".", *argv);
1481 /* If any signal numbers or symbol names were found, set flags for
1482 which signals to apply actions to. */
1484 for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
1490 if (!allsigs && !sigs[signum])
1492 if (query ("%s is used by the debugger.\nAre you sure you want to change it? ", strsigno (signum)))
1498 printf ("Not confirmed, unchanged.\n");
1512 target_notice_signals();
1516 /* Show the results. */
1517 sig_print_header ();
1518 for (signum = 0; signum < nsigs; signum++)
1522 sig_print_info (signum);
1527 do_cleanups (old_chain);
1530 /* Print current contents of the tables set by the handle command. */
1533 signals_info (signum_exp, from_tty)
1538 sig_print_header ();
1542 /* First see if this is a symbol name. */
1543 i = strtosigno (signum_exp);
1546 /* Nope, maybe it's an address which evaluates to a signal
1548 i = parse_and_eval_address (signum_exp);
1549 if (i >= NSIG || i < 0)
1550 error ("Signal number out of bounds.");
1556 printf_filtered ("\n");
1557 for (i = 0; i < NSIG; i++)
1564 printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
1567 /* Save all of the information associated with the inferior<==>gdb
1568 connection. INF_STATUS is a pointer to a "struct inferior_status"
1569 (defined in inferior.h). */
1572 save_inferior_status (inf_status, restore_stack_info)
1573 struct inferior_status *inf_status;
1574 int restore_stack_info;
1576 inf_status->pc_changed = pc_changed;
1577 inf_status->stop_signal = stop_signal;
1578 inf_status->stop_pc = stop_pc;
1579 inf_status->stop_frame_address = stop_frame_address;
1580 inf_status->stop_step = stop_step;
1581 inf_status->stop_stack_dummy = stop_stack_dummy;
1582 inf_status->stopped_by_random_signal = stopped_by_random_signal;
1583 inf_status->trap_expected = trap_expected;
1584 inf_status->step_range_start = step_range_start;
1585 inf_status->step_range_end = step_range_end;
1586 inf_status->step_frame_address = step_frame_address;
1587 inf_status->step_over_calls = step_over_calls;
1588 inf_status->stop_after_trap = stop_after_trap;
1589 inf_status->stop_soon_quietly = stop_soon_quietly;
1590 /* Save original bpstat chain here; replace it with copy of chain.
1591 If caller's caller is walking the chain, they'll be happier if we
1592 hand them back the original chain when restore_i_s is called. */
1593 inf_status->stop_bpstat = stop_bpstat;
1594 stop_bpstat = bpstat_copy (stop_bpstat);
1595 inf_status->breakpoint_proceeded = breakpoint_proceeded;
1596 inf_status->restore_stack_info = restore_stack_info;
1597 inf_status->proceed_to_finish = proceed_to_finish;
1599 memcpy (inf_status->stop_registers, stop_registers, REGISTER_BYTES);
1601 record_selected_frame (&(inf_status->selected_frame_address),
1602 &(inf_status->selected_level));
1607 restore_inferior_status (inf_status)
1608 struct inferior_status *inf_status;
1611 int level = inf_status->selected_level;
1613 pc_changed = inf_status->pc_changed;
1614 stop_signal = inf_status->stop_signal;
1615 stop_pc = inf_status->stop_pc;
1616 stop_frame_address = inf_status->stop_frame_address;
1617 stop_step = inf_status->stop_step;
1618 stop_stack_dummy = inf_status->stop_stack_dummy;
1619 stopped_by_random_signal = inf_status->stopped_by_random_signal;
1620 trap_expected = inf_status->trap_expected;
1621 step_range_start = inf_status->step_range_start;
1622 step_range_end = inf_status->step_range_end;
1623 step_frame_address = inf_status->step_frame_address;
1624 step_over_calls = inf_status->step_over_calls;
1625 stop_after_trap = inf_status->stop_after_trap;
1626 stop_soon_quietly = inf_status->stop_soon_quietly;
1627 bpstat_clear (&stop_bpstat);
1628 stop_bpstat = inf_status->stop_bpstat;
1629 breakpoint_proceeded = inf_status->breakpoint_proceeded;
1630 proceed_to_finish = inf_status->proceed_to_finish;
1632 memcpy (stop_registers, inf_status->stop_registers, REGISTER_BYTES);
1634 /* The inferior can be gone if the user types "print exit(0)"
1635 (and perhaps other times). */
1636 if (target_has_stack && inf_status->restore_stack_info)
1638 fid = find_relative_frame (get_current_frame (),
1641 /* If inf_status->selected_frame_address is NULL, there was no
1642 previously selected frame. */
1644 FRAME_FP (fid) != inf_status->selected_frame_address ||
1648 /* I'm not sure this error message is a good idea. I have
1649 only seen it occur after "Can't continue previously
1650 requested operation" (we get called from do_cleanups), in
1651 which case it just adds insult to injury (one confusing
1652 error message after another. Besides which, does the
1653 user really care if we can't restore the previously
1655 fprintf (stderr, "Unable to restore previously selected frame.\n");
1657 select_frame (get_current_frame (), 0);
1661 select_frame (fid, inf_status->selected_level);
1667 _initialize_infrun ()
1670 register int numsigs;
1672 add_info ("signals", signals_info,
1673 "What debugger does when program gets various signals.\n\
1674 Specify a signal number as argument to print info on that signal only.");
1675 add_info_alias ("handle", "signals", 0);
1677 add_com ("handle", class_run, handle_command,
1678 "Specify how to handle a signal.\n\
1679 Args are signal numbers and actions to apply to those signals.\n\
1680 Signal numbers may be numeric (ex. 11) or symbolic (ex. SIGSEGV).\n\
1681 Numeric ranges may be specified with the form LOW-HIGH (ex. 14-21).\n\
1682 The special arg \"all\" is recognized to mean all signals except those\n\
1683 used by the debugger, typically SIGTRAP and SIGINT.\n\
1684 Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
1685 \"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
1686 Stop means reenter debugger if this signal happens (implies print).\n\
1687 Print means print a message if this signal happens.\n\
1688 Pass means let program see this signal; otherwise program doesn't know.\n\
1689 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
1690 Pass and Stop may be combined.");
1692 stop_command = add_cmd ("stop", class_obscure, not_just_help_class_command,
1693 "There is no `stop' command, but you can set a hook on `stop'.\n\
1694 This allows you to set a list of commands to be run each time execution\n\
1695 of the program stops.", &cmdlist);
1697 numsigs = signo_max () + 1;
1698 signal_stop = (unsigned char *)
1699 xmalloc (sizeof (signal_stop[0]) * numsigs);
1700 signal_print = (unsigned char *)
1701 xmalloc (sizeof (signal_print[0]) * numsigs);
1702 signal_program = (unsigned char *)
1703 xmalloc (sizeof (signal_program[0]) * numsigs);
1704 for (i = 0; i < numsigs; i++)
1707 signal_print[i] = 1;
1708 signal_program[i] = 1;
1711 /* Signals caused by debugger's own actions
1712 should not be given to the program afterwards. */
1713 signal_program[SIGTRAP] = 0;
1714 signal_program[SIGINT] = 0;
1716 /* Signals that are not errors should not normally enter the debugger. */
1718 signal_stop[SIGALRM] = 0;
1719 signal_print[SIGALRM] = 0;
1720 #endif /* SIGALRM */
1722 signal_stop[SIGVTALRM] = 0;
1723 signal_print[SIGVTALRM] = 0;
1724 #endif /* SIGVTALRM */
1726 signal_stop[SIGPROF] = 0;
1727 signal_print[SIGPROF] = 0;
1728 #endif /* SIGPROF */
1730 signal_stop[SIGCHLD] = 0;
1731 signal_print[SIGCHLD] = 0;
1732 #endif /* SIGCHLD */
1734 signal_stop[SIGCLD] = 0;
1735 signal_print[SIGCLD] = 0;
1738 signal_stop[SIGIO] = 0;
1739 signal_print[SIGIO] = 0;
1742 signal_stop[SIGURG] = 0;
1743 signal_print[SIGURG] = 0;