1 /* Start and stop the inferior process, for GDB.
2 Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
4 This file is part of GDB.
6 GDB is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GDB is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* Notes on the algorithm used in wait_for_inferior to determine if we
21 just did a subroutine call when stepping. We have the following
22 information at that point:
24 Current and previous (just before this step) pc.
25 Current and previous sp.
26 Current and previous start of current function.
28 If the start's of the functions don't match, then
30 a) We did a subroutine call.
32 In this case, the pc will be at the beginning of a function.
34 b) We did a subroutine return.
40 If we did a longjump, we were doing "nexti", since a next would
41 have attempted to skip over the assembly language routine in which
42 the longjmp is coded and would have simply been the equivalent of a
43 continue. I consider this ok behaivior. We'd like one of two
44 things to happen if we are doing a nexti through the longjmp()
45 routine: 1) It behaves as a stepi, or 2) It acts like a continue as
46 above. Given that this is a special case, and that anybody who
47 thinks that the concept of sub calls is meaningful in the context
48 of a longjmp, I'll take either one. Let's see what happens.
50 Acts like a subroutine return. I can handle that with no problem
53 -->So: If the current and previous beginnings of the current
54 function don't match, *and* the pc is at the start of a function,
55 we've done a subroutine call. If the pc is not at the start of a
56 function, we *didn't* do a subroutine call.
58 -->If the beginnings of the current and previous function do match,
61 a) We just did a recursive call.
63 In this case, we would be at the very beginning of a
64 function and 1) it will have a prologue (don't jump to
65 before prologue, or 2) (we assume here that it doesn't have
66 a prologue) there will have been a change in the stack
67 pointer over the last instruction. (Ie. it's got to put
68 the saved pc somewhere. The stack is the usual place. In
69 a recursive call a register is only an option if there's a
70 prologue to do something with it. This is even true on
71 register window machines; the prologue sets up the new
72 window. It might not be true on a register window machine
73 where the call instruction moved the register window
74 itself. Hmmm. One would hope that the stack pointer would
75 also change. If it doesn't, somebody send me a note, and
76 I'll work out a more general theory.
77 randy@wheaties.ai.mit.edu). This is true (albeit slipperly
78 so) on all machines I'm aware of:
80 m68k: Call changes stack pointer. Regular jumps don't.
82 sparc: Recursive calls must have frames and therefor,
85 vax: All calls have frames and hence change the
88 b) We did a return from a recursive call. I don't see that we
89 have either the ability or the need to distinguish this
90 from an ordinary jump. The stack frame will be printed
91 when and if the frame pointer changes; if we are in a
92 function without a frame pointer, it's the users own
95 c) We did a jump within a function. We assume that this is
96 true if we didn't do a recursive call.
98 d) We are in no-man's land ("I see no symbols here"). We
99 don't worry about this; it will make calls look like simple
100 jumps (and the stack frames will be printed when the frame
101 pointer moves), which is a reasonably non-violent response.
104 We skip this; it causes more problems than it's worth.
105 #ifdef SUN4_COMPILER_FEATURE
106 We do a special ifdef for the sun 4, forcing it to single step
107 into calls which don't have prologues. This means that we can't
108 nexti over leaf nodes, we can probably next over them (since they
109 won't have debugging symbols, usually), and we can next out of
110 functions returning structures (with a "call .stret4" at the end).
123 #include "inferior.h"
129 /* unistd.h is needed to #define X_OK */
133 #include <sys/file.h>
138 #include <sys/param.h>
139 #include <sys/ptrace.h>
140 #endif /* UMAX_PTRACE */
142 extern char *sys_siglist
[];
145 /* Tables of how to react to signals; the user sets them. */
147 static char signal_stop
[NSIG
];
148 static char signal_print
[NSIG
];
149 static char signal_program
[NSIG
];
151 /* Nonzero if breakpoints are now inserted in the inferior. */
153 static int breakpoints_inserted
;
155 /* Function inferior was in as of last step command. */
157 static struct symbol
*step_start_function
;
159 /* This is the sequence of bytes we insert for a breakpoint. */
161 static char break_insn
[] = BREAKPOINT
;
163 /* Nonzero => address for special breakpoint for resuming stepping. */
165 static CORE_ADDR step_resume_break_address
;
167 /* Original contents of the byte where the special breakpoint is. */
169 static char step_resume_break_shadow
[sizeof break_insn
];
171 /* Nonzero means the special breakpoint is a duplicate
172 so it has not itself been inserted. */
174 static int step_resume_break_duplicate
;
176 /* Nonzero if we are expecting a trace trap and should proceed from it.
177 2 means expecting 2 trace traps and should continue both times.
178 That occurs when we tell sh to exec the program: we will get
179 a trap after the exec of sh and a second when the program is exec'd. */
181 static int trap_expected
;
183 /* Nonzero if the next time we try to continue the inferior, it will
184 step one instruction and generate a spurious trace trap.
185 This is used to compensate for a bug in HP-UX. */
187 static int trap_expected_after_continue
;
189 /* Nonzero means expecting a trace trap
190 and should stop the inferior and return silently when it happens. */
194 /* Nonzero means expecting a trace trap due to attaching to a process. */
196 int stop_after_attach
;
198 /* Nonzero if pc has been changed by the debugger
199 since the inferior stopped. */
203 /* Nonzero if debugging a remote machine via a serial link or ethernet. */
205 int remote_debugging
;
207 /* Save register contents here when about to pop a stack dummy frame. */
209 char stop_registers
[REGISTER_BYTES
];
211 /* Nonzero if program stopped due to error trying to insert breakpoints. */
213 static int breakpoints_failed
;
215 /* Nonzero if inferior is in sh before our program got exec'd. */
217 static int running_in_shell
;
219 /* Nonzero after stop if current stack frame should be printed. */
221 static int stop_print_frame
;
223 #ifdef NO_SINGLE_STEP
224 extern int one_stepped
; /* From machine dependent code */
225 extern void single_step (); /* Same. */
226 #endif /* NO_SINGLE_STEP */
228 static void insert_step_breakpoint ();
229 static void remove_step_breakpoint ();
230 static void wait_for_inferior ();
231 static void normal_stop ();
234 /* Clear out all variables saying what to do when inferior is continued.
235 First do this, then set the ones you want, then call `proceed'. */
238 clear_proceed_status ()
241 step_range_start
= 0;
243 step_frame_address
= 0;
244 step_over_calls
= -1;
245 step_resume_break_address
= 0;
247 stop_after_attach
= 0;
249 /* Discard any remaining commands left by breakpoint we had stopped at. */
250 clear_breakpoint_commands ();
253 /* Basic routine for continuing the program in various fashions.
255 ADDR is the address to resume at, or -1 for resume where stopped.
256 SIGNAL is the signal to give it, or 0 for none,
257 or -1 for act according to how it stopped.
258 STEP is nonzero if should trap after one instruction.
259 -1 means return after that and print nothing.
260 You should probably set various step_... variables
261 before calling here, if you are stepping.
263 You should call clear_proceed_status before calling proceed. */
266 proceed (addr
, signal
, step
)
274 step_start_function
= find_pc_function (read_pc ());
280 /* If there is a breakpoint at the address we will resume at,
281 step one instruction before inserting breakpoints
282 so that we do not stop right away. */
284 if (!pc_changed
&& breakpoint_here_p (read_pc ()))
289 write_register (PC_REGNUM
, addr
);
291 write_register (NPC_REGNUM
, addr
+ 4);
295 if (trap_expected_after_continue
)
297 /* If (step == 0), a trap will be automatically generated after
298 the first instruction is executed. Force step one
299 instruction to clear this condition. This should not occur
300 if step is nonzero, but it is harmless in that case. */
302 trap_expected_after_continue
= 0;
306 /* We will get a trace trap after one instruction.
307 Continue it automatically and insert breakpoints then. */
311 int temp
= insert_breakpoints ();
314 print_sys_errmsg ("ptrace", temp
);
315 error ("Cannot insert breakpoints.\n\
316 The same program may be running in another process.");
318 breakpoints_inserted
= 1;
321 /* Install inferior's terminal modes. */
322 terminal_inferior ();
325 stop_signal
= signal
;
326 /* If this signal should not be seen by program,
327 give it zero. Used for debugging signals. */
328 else if (stop_signal
< NSIG
&& !signal_program
[stop_signal
])
331 /* Resume inferior. */
332 resume (oneproc
|| step
, stop_signal
);
334 /* Wait for it to stop (if not standalone)
335 and in any case decode why it stopped, and act accordingly. */
337 wait_for_inferior ();
341 /* Writing the inferior pc as a register calls this function
342 to inform infrun that the pc has been set in the debugger. */
352 /* Start an inferior process for the first time.
353 Actually it was started by the fork that created it,
354 but it will have stopped one instruction after execing sh.
355 Here we must get it up to actual execution of the real program. */
360 /* We will get a trace trap after one instruction.
361 Continue it automatically. Eventually (after shell does an exec)
362 it will get another trace trap. Then insert breakpoints and continue. */
364 #ifdef START_INFERIOR_TRAPS_EXPECTED
365 trap_expected
= START_INFERIOR_TRAPS_EXPECTED
;
370 running_in_shell
= 0; /* Set to 1 at first SIGTRAP, 0 at second. */
371 trap_expected_after_continue
= 0;
372 breakpoints_inserted
= 0;
373 mark_breakpoints_out ();
375 /* Set up the "saved terminal modes" of the inferior
376 based on what modes we are starting it with. */
377 terminal_init_inferior ();
379 /* Install inferior's terminal modes. */
380 terminal_inferior ();
382 if (remote_debugging
)
385 fetch_inferior_registers();
386 set_current_frame (create_new_frame (read_register (FP_REGNUM
),
388 stop_frame_address
= FRAME_FP (get_current_frame());
390 if (insert_breakpoints())
391 fatal("Can't insert breakpoints");
392 breakpoints_inserted
= 1;
397 wait_for_inferior ();
402 /* Start remote-debugging of a machine over a serial link. */
407 clear_proceed_status ();
408 running_in_shell
= 0;
411 breakpoints_inserted
= 0;
412 mark_breakpoints_out ();
413 wait_for_inferior ();
419 /* Attach to process PID, then initialize for debugging it
420 and wait for the trace-trap that results from attaching. */
429 mark_breakpoints_out ();
430 terminal_init_inferior ();
431 clear_proceed_status ();
432 stop_after_attach
= 1;
433 /*proceed (-1, 0, -2);*/
434 terminal_inferior ();
435 wait_for_inferior ();
438 #endif /* ATTACH_DETACH */
440 /* Wait for control to return from inferior to debugger.
441 If inferior gets a signal, we may decide to start it up again
442 instead of returning. That is why there is a loop in this function.
443 When this function actually returns it means the inferior
444 should be left stopped and GDB should read more commands. */
455 CORE_ADDR stop_sp
, prev_sp
;
456 CORE_ADDR prev_func_start
, stop_func_start
;
457 char *prev_func_name
, *stop_func_name
;
458 CORE_ADDR prologue_pc
;
459 int stop_step_resume_break
;
460 CORE_ADDR step_resume_break_sp
;
463 struct symtab_and_line sal
;
465 extern CORE_ADDR text_end
;
466 int remove_breakpoints_on_following_step
= 0;
468 prev_pc
= read_pc ();
469 (void) find_pc_partial_function (prev_pc
, &prev_func_name
,
471 prev_func_start
+= FUNCTION_START_OFFSET
;
472 prev_sp
= read_register (SP_REGNUM
);
476 /* Clean up saved state that will become invalid. */
478 flush_cached_frames ();
480 if (remote_debugging
)
485 if (pid
!= inferior_pid
)
489 /* See if the process still exists; clean up if it doesn't. */
492 terminal_ours_for_output ();
494 printf ("\nProgram exited with code 0%o.\n", WRETCODE (w
));
496 printf ("\nProgram exited normally.\n");
499 #ifdef NO_SINGLE_STEP
502 stop_print_frame
= 0;
505 else if (!WIFSTOPPED (w
))
508 stop_print_frame
= 0;
509 stop_signal
= WTERMSIG (w
);
510 terminal_ours_for_output ();
511 printf ("\nProgram terminated with signal %d, %s\n",
514 ? sys_siglist
[stop_signal
]
516 printf ("The inferior process no longer exists.\n");
518 #ifdef NO_SINGLE_STEP
524 #ifdef NO_SINGLE_STEP
526 single_step (0); /* This actually cleans up the ss */
527 #endif /* NO_SINGLE_STEP */
529 fetch_inferior_registers ();
530 stop_pc
= read_pc ();
531 set_current_frame ( create_new_frame (read_register (FP_REGNUM
),
534 stop_frame_address
= FRAME_FP (get_current_frame ());
535 stop_sp
= read_register (SP_REGNUM
);
537 /* Don't care about return value; stop_func_start will be 0
538 if it doesn't work. */
539 (void) find_pc_partial_function (stop_pc
, &stop_func_name
,
541 stop_func_start
+= FUNCTION_START_OFFSET
;
545 stop_stack_dummy
= 0;
546 stop_print_frame
= 1;
547 stop_step_resume_break
= 0;
549 stopped_by_random_signal
= 0;
550 breakpoints_failed
= 0;
552 /* Look at the cause of the stop, and decide what to do.
553 The alternatives are:
554 1) break; to really stop and return to the debugger,
555 2) drop through to start up again
556 (set another_trap to 1 to single step once)
557 3) set random_signal to 1, and the decision between 1 and 2
558 will be made according to the signal handling tables. */
560 stop_signal
= WSTOPSIG (w
);
562 /* First, distinguish signals caused by the debugger from signals
563 that have to do with the program's own actions.
564 Note that breakpoint insns may cause SIGTRAP or SIGILL
565 or SIGEMT, depending on the operating system version.
566 Here we detect when a SIGILL or SIGEMT is really a breakpoint
567 and change it to SIGTRAP. */
569 if (stop_signal
== SIGTRAP
570 || (breakpoints_inserted
&&
571 (stop_signal
== SIGILL
572 || stop_signal
== SIGEMT
))
573 || stop_after_attach
)
575 if (stop_signal
== SIGTRAP
&& stop_after_trap
)
577 stop_print_frame
= 0;
580 if (stop_after_attach
)
582 /* Don't even think about breakpoints
583 if still running the shell that will exec the program
584 or if just proceeded over a breakpoint. */
585 if (stop_signal
== SIGTRAP
&& trap_expected
)
589 /* See if there is a breakpoint at the current PC. */
590 #if DECR_PC_AFTER_BREAK
591 /* Notice the case of stepping through a jump
592 that leads just after a breakpoint.
593 Don't confuse that with hitting the breakpoint.
594 What we check for is that 1) stepping is going on
595 and 2) the pc before the last insn does not match
596 the address of the breakpoint before the current pc. */
597 if (!(prev_pc
!= stop_pc
- DECR_PC_AFTER_BREAK
598 && step_range_end
&& !step_resume_break_address
))
599 #endif /* DECR_PC_AFTER_BREAK not zero */
601 /* See if we stopped at the special breakpoint for
602 stepping over a subroutine call. */
603 if (stop_pc
- DECR_PC_AFTER_BREAK
604 == step_resume_break_address
)
606 stop_step_resume_break
= 1;
607 if (DECR_PC_AFTER_BREAK
)
609 stop_pc
-= DECR_PC_AFTER_BREAK
;
610 write_register (PC_REGNUM
, stop_pc
);
617 breakpoint_stop_status (stop_pc
, stop_frame_address
);
618 /* Following in case break condition called a
620 stop_print_frame
= 1;
621 if (stop_breakpoint
&& DECR_PC_AFTER_BREAK
)
623 stop_pc
-= DECR_PC_AFTER_BREAK
;
624 write_register (PC_REGNUM
, stop_pc
);
626 write_register (NPC_REGNUM
, stop_pc
+ 4);
634 if (stop_signal
== SIGTRAP
)
636 = !(stop_breakpoint
|| trap_expected
637 || stop_step_resume_break
638 #ifndef CANNOT_EXECUTE_STACK
639 || (stop_sp INNER_THAN stop_pc
640 && stop_pc INNER_THAN stop_frame_address
)
642 || stop_pc
== text_end
- 2
644 || (step_range_end
&& !step_resume_break_address
));
649 || stop_step_resume_break
651 || (stop_sp INNER_THAN stop_pc
652 && stop_pc INNER_THAN stop_frame_address
)
657 stop_signal
= SIGTRAP
;
663 /* For the program's own signals, act according to
664 the signal handling tables. */
667 && !(running_in_shell
&& stop_signal
== SIGSEGV
))
669 /* Signal not for debugging purposes. */
672 stopped_by_random_signal
= 1;
674 if (stop_signal
>= NSIG
675 || signal_print
[stop_signal
])
678 terminal_ours_for_output ();
679 printf ("\nProgram received signal %d, %s\n",
682 ? sys_siglist
[stop_signal
]
686 if (stop_signal
>= NSIG
687 || signal_stop
[stop_signal
])
689 /* If not going to stop, give terminal back
690 if we took it away. */
692 terminal_inferior ();
695 /* Handle cases caused by hitting a breakpoint. */
698 && (stop_breakpoint
|| stop_step_resume_break
))
700 /* Does a breakpoint want us to stop? */
701 if (stop_breakpoint
&& stop_breakpoint
!= -1
702 && stop_breakpoint
!= -0x1000001)
704 /* 0x1000000 is set in stop_breakpoint as returned by
705 breakpoint_stop_status to indicate a silent
707 if ((stop_breakpoint
> 0 ? stop_breakpoint
:
711 stop_print_frame
= 0;
712 if (stop_breakpoint
> 0)
713 stop_breakpoint
-= 0x1000000;
715 stop_breakpoint
+= 0x1000000;
719 /* But if we have hit the step-resumption breakpoint,
720 remove it. It has done its job getting us here.
721 The sp test is to make sure that we don't get hung
722 up in recursive calls in functions without frame
723 pointers. If the stack pointer isn't outside of
724 where the breakpoint was set (within a routine to be
725 stepped over), we're in the middle of a recursive
726 call. Not true for reg window machines (sparc)
727 because the must change frames to call things and
728 the stack pointer doesn't have to change if it
729 the bp was set in a routine without a frame (pc can
730 be stored in some other window).
732 The removal of the sp test is to allow calls to
733 alloca. Nasty things were happening. Oh, well,
734 gdb can only handle one level deep of lack of
736 if (stop_step_resume_break
737 && (step_frame_address
== 0
738 || (stop_frame_address
== step_frame_address
)))
740 remove_step_breakpoint ();
741 step_resume_break_address
= 0;
743 /* Otherwise, must remove breakpoints and single-step
744 to get us past the one we hit. */
747 remove_breakpoints ();
748 remove_step_breakpoint ();
749 breakpoints_inserted
= 0;
753 /* We come here if we hit a breakpoint but should not
754 stop for it. Possibly we also were stepping
755 and should stop for that. So fall through and
756 test for stepping. But, if not stepping,
760 /* If this is the breakpoint at the end of a stack dummy,
761 just stop silently. */
762 #ifndef CANNOT_EXECUTE_STACK
763 if (stop_sp INNER_THAN stop_pc
764 && stop_pc INNER_THAN stop_frame_address
)
766 if (stop_pc
== text_end
- 2)
769 stop_print_frame
= 0;
770 stop_stack_dummy
= 1;
772 trap_expected_after_continue
= 1;
777 if (step_resume_break_address
)
778 /* Having a step-resume breakpoint overrides anything
779 else having to do with stepping commands until
780 that breakpoint is reached. */
782 /* If stepping through a line, keep going if still within it. */
783 else if (!random_signal
785 && stop_pc
>= step_range_start
786 && stop_pc
< step_range_end
787 /* The step range might include the start of the
788 function, so if we are at the start of the
789 step range and either the stack or frame pointers
790 just changed, we've stepped outside */
791 && !(stop_pc
== step_range_start
792 && stop_frame_address
793 && (stop_sp INNER_THAN prev_sp
794 || stop_frame_address
!= step_frame_address
)))
796 /* Don't step through the return from a function
797 unless that is the first instruction stepped through. */
798 if (ABOUT_TO_RETURN (stop_pc
))
805 /* We stepped out of the stepping range. See if that was due
806 to a subroutine call that we should proceed to the end of. */
807 else if (!random_signal
&& step_range_end
)
811 prologue_pc
= stop_func_start
;
812 SKIP_PROLOGUE (prologue_pc
);
815 /* Did we just take a signal? */
816 if (stop_func_name
&& !strcmp ("_sigtramp", stop_func_name
)
817 && (!prev_func_name
|| strcmp ("_sigtramp", prev_func_name
)))
819 /* We've just taken a signal; go until we are back to
820 the point where we took it and one more. */
821 step_resume_break_address
= prev_pc
;
822 step_resume_break_duplicate
=
823 breakpoint_here_p (step_resume_break_address
);
824 step_resume_break_sp
= stop_sp
;
825 if (breakpoints_inserted
)
826 insert_step_breakpoint ();
827 /* Make sure that the stepping range gets us past
829 if (step_range_end
== 1)
830 step_range_end
= (step_range_start
= prev_pc
) + 1;
831 remove_breakpoints_on_following_step
= 1;
834 /* ==> See comments at top of file on this algorithm. <==*/
836 else if (stop_pc
== stop_func_start
837 && (stop_func_start
!= prev_func_start
838 || prologue_pc
!= stop_func_start
839 || stop_sp
!= prev_sp
))
841 /* It's a subroutine call */
842 if (step_over_calls
> 0
843 || (step_over_calls
&& find_pc_function (stop_pc
) == 0))
845 /* A subroutine call has happened. */
846 /* Set a special breakpoint after the return */
847 step_resume_break_address
=
848 SAVED_PC_AFTER_CALL (get_current_frame ());
849 step_resume_break_duplicate
850 = breakpoint_here_p (step_resume_break_address
);
851 step_resume_break_sp
= stop_sp
;
852 if (breakpoints_inserted
)
853 insert_step_breakpoint ();
855 /* Subroutine call with source code we should not step over.
856 Do step to the first line of code in it. */
857 else if (step_over_calls
)
859 SKIP_PROLOGUE (stop_func_start
);
860 sal
= find_pc_line (stop_func_start
, 0);
861 /* Use the step_resume_break to step until
862 the end of the prologue, even if that involves jumps
863 (as it seems to on the vax under 4.2). */
864 /* If the prologue ends in the middle of a source line,
865 continue to the end of that source line.
866 Otherwise, just go to end of prologue. */
867 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
868 /* no, don't either. It skips any code that's
869 legitimately on the first line. */
871 if (sal
.end
&& sal
.pc
!= stop_func_start
)
872 stop_func_start
= sal
.end
;
875 if (stop_func_start
== stop_pc
)
877 /* We are already there: stop now. */
882 /* Put the step-breakpoint there and go until there. */
884 step_resume_break_address
= stop_func_start
;
885 step_resume_break_sp
= stop_sp
;
887 step_resume_break_duplicate
888 = breakpoint_here_p (step_resume_break_address
);
889 if (breakpoints_inserted
)
890 insert_step_breakpoint ();
891 /* Do not specify what the fp should be when we stop
892 since on some machines the prologue
893 is where the new fp value is established. */
894 step_frame_address
= 0;
895 /* And make sure stepping stops right away then. */
896 step_range_end
= step_range_start
;
901 /* We get here only if step_over_calls is 0 and we
902 just stepped into a subroutine. I presume
903 that step_over_calls is only 0 when we're
904 supposed to be stepping at the assembly
910 /* No subroutince call; stop now. */
918 /* Save the pc before execution, to compare with pc after stop. */
919 prev_pc
= read_pc (); /* Might have been DECR_AFTER_BREAK */
920 prev_func_start
= stop_func_start
; /* Ok, since if DECR_PC_AFTER
921 BREAK is defined, the
922 original pc would not have
923 been at the start of a
925 prev_func_name
= stop_func_name
;
928 /* If we did not do break;, it means we should keep
929 running the inferior and not return to debugger. */
931 /* If trap_expected is 2, it means continue once more
932 and insert breakpoints at the next trap.
933 If trap_expected is 1 and the signal was SIGSEGV, it means
934 the shell is doing some memory allocation--just resume it
936 Otherwise insert breakpoints now, and possibly single step. */
938 if (trap_expected
> 1)
941 running_in_shell
= 1;
944 else if (running_in_shell
&& stop_signal
== SIGSEGV
)
948 else if (trap_expected
&& stop_signal
!= SIGTRAP
)
950 /* We took a signal which we are supposed to pass through to
951 the inferior and we haven't yet gotten our trap. Simply
953 resume ((step_range_end
&& !step_resume_break_address
)
959 /* Here, we are not awaiting another exec to get
960 the program we really want to debug.
961 Insert breakpoints now, unless we are trying
962 to one-proceed past a breakpoint. */
963 running_in_shell
= 0;
964 /* If we've just finished a special step resume and we don't
965 want to hit a breakpoint, pull em out. */
966 if (!step_resume_break_address
&&
967 remove_breakpoints_on_following_step
)
969 remove_breakpoints_on_following_step
= 0;
970 remove_breakpoints ();
971 breakpoints_inserted
= 0;
973 else if (!breakpoints_inserted
&& !another_trap
)
975 insert_step_breakpoint ();
976 breakpoints_failed
= insert_breakpoints ();
977 if (breakpoints_failed
)
979 breakpoints_inserted
= 1;
982 trap_expected
= another_trap
;
984 if (stop_signal
== SIGTRAP
)
987 resume ((step_range_end
&& !step_resume_break_address
)
994 /* Here to return control to GDB when the inferior stops for real.
995 Print appropriate messages, remove breakpoints, give terminal our modes.
997 RUNNING_IN_SHELL nonzero means the shell got a signal before
998 exec'ing the program we wanted to run.
999 STOP_PRINT_FRAME nonzero means print the executing frame
1000 (pc, function, args, file, line number and line text).
1001 BREAKPOINTS_FAILED nonzero means stop was due to error
1002 attempting to insert breakpoints. */
1007 /* Make sure that the current_frame's pc is correct. This
1008 is a correction for setting up the frame info before doing
1009 DECR_PC_AFTER_BREAK */
1011 (get_current_frame ())->pc
= read_pc ();
1013 if (breakpoints_failed
)
1015 terminal_ours_for_output ();
1016 print_sys_errmsg ("ptrace", breakpoints_failed
);
1017 printf ("Stopped; cannot insert breakpoints.\n\
1018 The same program may be running in another process.\n");
1022 remove_step_breakpoint ();
1024 if (inferior_pid
&& breakpoints_inserted
)
1025 if (remove_breakpoints ())
1027 terminal_ours_for_output ();
1028 printf ("Cannot remove breakpoints because program is no longer writable.\n\
1029 It must be running in another process.\n\
1030 Further execution is probably impossible.\n");
1033 breakpoints_inserted
= 0;
1035 /* Delete the breakpoint we stopped at, if it wants to be deleted.
1036 Delete any breakpoint that is to be deleted at the next stop. */
1038 breakpoint_auto_delete (stop_breakpoint
);
1040 /* If an auto-display called a function and that got a signal,
1041 delete that auto-display to avoid an infinite recursion. */
1043 if (stopped_by_random_signal
)
1044 disable_current_display ();
1046 if (step_multi
&& stop_step
)
1051 if (running_in_shell
)
1053 if (stop_signal
== SIGSEGV
)
1055 char *exec_file
= (char *) get_exec_file (1);
1057 if (access (exec_file
, X_OK
) != 0)
1058 printf ("The file \"%s\" is not executable.\n", exec_file
);
1061 You have just encountered a bug in \"sh\". GDB starts your program\n\
1062 by running \"sh\" with a command to exec your program.\n\
1063 This is so that \"sh\" will process wildcards and I/O redirection.\n\
1064 This time, \"sh\" crashed.\n\
1066 One known bug in \"sh\" bites when the environment takes up a lot of space.\n\
1067 Try \"info env\" to see the environment; then use \"delete env\" to kill\n\
1068 some variables whose values are large; then do \"run\" again.\n\
1070 If that works, you might want to put those \"delete env\" commands\n\
1071 into a \".gdbinit\" file in this directory so they will happen every time.\n");
1073 /* Don't confuse user with his program's symbols on sh's data. */
1074 stop_print_frame
= 0;
1077 if (inferior_pid
== 0)
1080 /* Select innermost stack frame except on return from a stack dummy routine,
1081 or if the program has exited. */
1082 if (!stop_stack_dummy
)
1084 select_frame (get_current_frame (), 0);
1086 if (stop_print_frame
)
1088 if (stop_breakpoint
> 0)
1089 printf ("\nBpt %d, ", stop_breakpoint
);
1090 print_sel_frame (stop_step
1091 && step_frame_address
== stop_frame_address
1092 && step_start_function
== find_pc_function (stop_pc
));
1093 /* Display the auto-display expressions. */
1098 /* Save the function value return registers
1099 We might be about to restore their previous contents. */
1100 read_register_bytes (0, stop_registers
, REGISTER_BYTES
);
1102 if (stop_stack_dummy
)
1104 /* Pop the empty frame that contains the stack dummy.
1105 POP_FRAME ends with a setting of the current frame, so we
1106 can use that next. */
1108 select_frame (get_current_frame (), 0);
1113 insert_step_breakpoint ()
1115 if (step_resume_break_address
&& !step_resume_break_duplicate
)
1117 read_memory (step_resume_break_address
,
1118 step_resume_break_shadow
, sizeof break_insn
);
1119 write_memory (step_resume_break_address
,
1120 break_insn
, sizeof break_insn
);
1125 remove_step_breakpoint ()
1127 if (step_resume_break_address
&& !step_resume_break_duplicate
)
1128 write_memory (step_resume_break_address
, step_resume_break_shadow
,
1132 /* Specify how various signals in the inferior should be handled. */
1135 handle_command (args
, from_tty
)
1139 register char *p
= args
;
1141 register int digits
, wordlen
;
1144 error_no_arg ("signal to handle");
1148 /* Find the end of the next word in the args. */
1149 for (wordlen
= 0; p
[wordlen
] && p
[wordlen
] != ' ' && p
[wordlen
] != '\t';
1151 for (digits
= 0; p
[digits
] >= '0' && p
[digits
] <= '9'; digits
++);
1153 /* If it is all digits, it is signal number to operate on. */
1154 if (digits
== wordlen
)
1157 if (signum
<= 0 || signum
>= NSIG
)
1160 error ("Invalid signal %s given as argument to \"handle\".", p
);
1162 if (signum
== SIGTRAP
|| signum
== SIGINT
)
1164 if (!query ("Signal %d is used by the debugger.\nAre you sure you want to change it? ", signum
))
1165 error ("Not confirmed.");
1168 else if (signum
== 0)
1169 error ("First argument is not a signal number.");
1171 /* Else, if already got a signal number, look for flag words
1172 saying what to do for it. */
1173 else if (!strncmp (p
, "stop", wordlen
))
1175 signal_stop
[signum
] = 1;
1176 signal_print
[signum
] = 1;
1178 else if (wordlen
>= 2 && !strncmp (p
, "print", wordlen
))
1179 signal_print
[signum
] = 1;
1180 else if (wordlen
>= 2 && !strncmp (p
, "pass", wordlen
))
1181 signal_program
[signum
] = 1;
1182 else if (!strncmp (p
, "ignore", wordlen
))
1183 signal_program
[signum
] = 0;
1184 else if (wordlen
>= 3 && !strncmp (p
, "nostop", wordlen
))
1185 signal_stop
[signum
] = 0;
1186 else if (wordlen
>= 4 && !strncmp (p
, "noprint", wordlen
))
1188 signal_print
[signum
] = 0;
1189 signal_stop
[signum
] = 0;
1191 else if (wordlen
>= 4 && !strncmp (p
, "nopass", wordlen
))
1192 signal_program
[signum
] = 0;
1193 else if (wordlen
>= 3 && !strncmp (p
, "noignore", wordlen
))
1194 signal_program
[signum
] = 1;
1195 /* Not a number and not a recognized flag word => complain. */
1199 error ("Unrecognized flag word: \"%s\".", p
);
1202 /* Find start of next word. */
1204 while (*p
== ' ' || *p
== '\t') p
++;
1209 /* Show the results. */
1210 printf ("Number\tStop\tPrint\tPass to program\tDescription\n");
1211 printf ("%d\t", signum
);
1212 printf ("%s\t", signal_stop
[signum
] ? "Yes" : "No");
1213 printf ("%s\t", signal_print
[signum
] ? "Yes" : "No");
1214 printf ("%s\t\t", signal_program
[signum
] ? "Yes" : "No");
1215 printf ("%s\n", sys_siglist
[signum
]);
1219 /* Print current contents of the tables set by the handle command. */
1222 signals_info (signum_exp
)
1226 printf_filtered ("Number\tStop\tPrint\tPass to program\tDescription\n");
1230 i
= parse_and_eval_address (signum_exp
);
1231 if (i
>= NSIG
|| i
< 0)
1232 error ("Signal number out of bounds.");
1233 printf_filtered ("%d\t", i
);
1234 printf_filtered ("%s\t", signal_stop
[i
] ? "Yes" : "No");
1235 printf_filtered ("%s\t", signal_print
[i
] ? "Yes" : "No");
1236 printf_filtered ("%s\t\t", signal_program
[i
] ? "Yes" : "No");
1237 printf_filtered ("%s\n", sys_siglist
[i
]);
1241 printf_filtered ("\n");
1242 for (i
= 0; i
< NSIG
; i
++)
1246 printf_filtered ("%d\t", i
);
1247 printf_filtered ("%s\t", signal_stop
[i
] ? "Yes" : "No");
1248 printf_filtered ("%s\t", signal_print
[i
] ? "Yes" : "No");
1249 printf_filtered ("%s\t\t", signal_program
[i
] ? "Yes" : "No");
1250 printf_filtered ("%s\n", sys_siglist
[i
]);
1253 printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
1256 /* Save all of the information associated with the inferior<==>gdb
1257 connection. INF_STATUS is a pointer to a "struct inferior_status"
1258 (defined in inferior.h). */
1260 struct command_line
*get_breakpoint_commands ();
1263 save_inferior_status (inf_status
, restore_stack_info
)
1264 struct inferior_status
*inf_status
;
1265 int restore_stack_info
;
1267 inf_status
->pc_changed
= pc_changed
;
1268 inf_status
->stop_signal
= stop_signal
;
1269 inf_status
->stop_pc
= stop_pc
;
1270 inf_status
->stop_frame_address
= stop_frame_address
;
1271 inf_status
->stop_breakpoint
= stop_breakpoint
;
1272 inf_status
->stop_step
= stop_step
;
1273 inf_status
->stop_stack_dummy
= stop_stack_dummy
;
1274 inf_status
->stopped_by_random_signal
= stopped_by_random_signal
;
1275 inf_status
->trap_expected
= trap_expected
;
1276 inf_status
->step_range_start
= step_range_start
;
1277 inf_status
->step_range_end
= step_range_end
;
1278 inf_status
->step_frame_address
= step_frame_address
;
1279 inf_status
->step_over_calls
= step_over_calls
;
1280 inf_status
->step_resume_break_address
= step_resume_break_address
;
1281 inf_status
->stop_after_trap
= stop_after_trap
;
1282 inf_status
->stop_after_attach
= stop_after_attach
;
1283 inf_status
->breakpoint_commands
= get_breakpoint_commands ();
1284 inf_status
->restore_stack_info
= restore_stack_info
;
1286 bcopy (stop_registers
, inf_status
->stop_registers
, REGISTER_BYTES
);
1288 record_selected_frame (&(inf_status
->selected_frame_address
),
1289 &(inf_status
->selected_level
));
1294 restore_inferior_status (inf_status
)
1295 struct inferior_status
*inf_status
;
1298 int level
= inf_status
->selected_level
;
1300 pc_changed
= inf_status
->pc_changed
;
1301 stop_signal
= inf_status
->stop_signal
;
1302 stop_pc
= inf_status
->stop_pc
;
1303 stop_frame_address
= inf_status
->stop_frame_address
;
1304 stop_breakpoint
= inf_status
->stop_breakpoint
;
1305 stop_step
= inf_status
->stop_step
;
1306 stop_stack_dummy
= inf_status
->stop_stack_dummy
;
1307 stopped_by_random_signal
= inf_status
->stopped_by_random_signal
;
1308 trap_expected
= inf_status
->trap_expected
;
1309 step_range_start
= inf_status
->step_range_start
;
1310 step_range_end
= inf_status
->step_range_end
;
1311 step_frame_address
= inf_status
->step_frame_address
;
1312 step_over_calls
= inf_status
->step_over_calls
;
1313 step_resume_break_address
= inf_status
->step_resume_break_address
;
1314 stop_after_trap
= inf_status
->stop_after_trap
;
1315 stop_after_attach
= inf_status
->stop_after_attach
;
1316 set_breakpoint_commands (inf_status
->breakpoint_commands
);
1318 bcopy (inf_status
->stop_registers
, stop_registers
, REGISTER_BYTES
);
1320 if (inf_status
->restore_stack_info
)
1322 fid
= find_relative_frame (get_current_frame (),
1325 if (FRAME_FP (fid
) != inf_status
->selected_frame_address
||
1328 fprintf (stderr
, "Unable to restore previously selected frame.\n");
1329 select_frame (get_current_frame (), 0);
1333 select_frame (fid
, inf_status
->selected_level
);
1340 _initialize_infrun ()
1344 add_info ("signals", signals_info
,
1345 "What debugger does when program gets various signals.\n\
1346 Specify a signal number as argument to print info on that signal only.");
1348 add_com ("handle", class_run
, handle_command
,
1349 "Specify how to handle a signal.\n\
1350 Args are signal number followed by flags.\n\
1351 Flags allowed are \"stop\", \"print\", \"pass\",\n\
1352 \"nostop\", \"noprint\" or \"nopass\".\n\
1353 Print means print a message if this signal happens.\n\
1354 Stop means reenter debugger if this signal happens (implies print).\n\
1355 Pass means let program see this signal; otherwise program doesn't know.\n\
1356 Pass and Stop may be combined.");
1358 for (i
= 0; i
< NSIG
; i
++)
1361 signal_print
[i
] = 1;
1362 signal_program
[i
] = 1;
1365 /* Signals caused by debugger's own actions
1366 should not be given to the program afterwards. */
1367 signal_program
[SIGTRAP
] = 0;
1368 signal_program
[SIGINT
] = 0;
1370 /* Signals that are not errors should not normally enter the debugger. */
1372 signal_stop
[SIGALRM
] = 0;
1373 signal_print
[SIGALRM
] = 0;
1374 #endif /* SIGALRM */
1376 signal_stop
[SIGVTALRM
] = 0;
1377 signal_print
[SIGVTALRM
] = 0;
1378 #endif /* SIGVTALRM */
1380 signal_stop
[SIGPROF
] = 0;
1381 signal_print
[SIGPROF
] = 0;
1382 #endif /* SIGPROF */
1384 signal_stop
[SIGCHLD
] = 0;
1385 signal_print
[SIGCHLD
] = 0;
1386 #endif /* SIGCHLD */
1388 signal_stop
[SIGCLD
] = 0;
1389 signal_print
[SIGCLD
] = 0;
1392 signal_stop
[SIGIO
] = 0;
1393 signal_print
[SIGIO
] = 0;
1396 signal_stop
[SIGURG
] = 0;
1397 signal_print
[SIGURG
] = 0;