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 /* Required by <sys/user.h>. */
143 #include <sys/types.h>
144 /* Required by <sys/user.h>, at least on system V. */
146 /* Needed by IN_SIGTRAMP on some machines (e.g. vax). */
147 #include <sys/param.h>
148 /* Needed by IN_SIGTRAMP on some machines (e.g. vax). */
149 #include <sys/user.h>
151 extern char *sys_siglist
[];
154 /* Sigtramp is a routine that the kernel calls (which then calls the
155 signal handler). On most machines it is a library routine that
156 is linked into the executable.
158 This macro, given a program counter value and the name of the
159 function in which that PC resides (which can be null if the
160 name is not known), returns nonzero if the PC and name show
161 that we are in sigtramp.
163 On most machines just see if the name is sigtramp (and if we have
164 no name, assume we are not in sigtramp). */
165 #if !defined (IN_SIGTRAMP)
166 #define IN_SIGTRAMP(pc, name) \
167 name && !strcmp ("_sigtramp", name)
170 /* Tables of how to react to signals; the user sets them. */
172 static char signal_stop
[NSIG
];
173 static char signal_print
[NSIG
];
174 static char signal_program
[NSIG
];
176 /* Nonzero if breakpoints are now inserted in the inferior. */
178 static int breakpoints_inserted
;
180 /* Function inferior was in as of last step command. */
182 static struct symbol
*step_start_function
;
184 /* This is the sequence of bytes we insert for a breakpoint. */
186 static char break_insn
[] = BREAKPOINT
;
188 /* Nonzero => address for special breakpoint for resuming stepping. */
190 static CORE_ADDR step_resume_break_address
;
192 /* Original contents of the byte where the special breakpoint is. */
194 static char step_resume_break_shadow
[sizeof break_insn
];
196 /* Nonzero means the special breakpoint is a duplicate
197 so it has not itself been inserted. */
199 static int step_resume_break_duplicate
;
201 /* Nonzero if we are expecting a trace trap and should proceed from it.
202 2 means expecting 2 trace traps and should continue both times.
203 That occurs when we tell sh to exec the program: we will get
204 a trap after the exec of sh and a second when the program is exec'd. */
206 static int trap_expected
;
208 /* Nonzero if the next time we try to continue the inferior, it will
209 step one instruction and generate a spurious trace trap.
210 This is used to compensate for a bug in HP-UX. */
212 static int trap_expected_after_continue
;
214 /* Nonzero means expecting a trace trap
215 and should stop the inferior and return silently when it happens. */
219 /* Nonzero means expecting a trace trap due to attaching to a process. */
221 int stop_after_attach
;
223 /* Nonzero if pc has been changed by the debugger
224 since the inferior stopped. */
228 /* Nonzero if debugging a remote machine via a serial link or ethernet. */
230 int remote_debugging
;
232 /* Save register contents here when about to pop a stack dummy frame. */
234 char stop_registers
[REGISTER_BYTES
];
236 /* Nonzero if program stopped due to error trying to insert breakpoints. */
238 static int breakpoints_failed
;
240 /* Nonzero if inferior is in sh before our program got exec'd. */
242 static int running_in_shell
;
244 /* Nonzero after stop if current stack frame should be printed. */
246 static int stop_print_frame
;
248 #ifdef NO_SINGLE_STEP
249 extern int one_stepped
; /* From machine dependent code */
250 extern void single_step (); /* Same. */
251 #endif /* NO_SINGLE_STEP */
253 static void insert_step_breakpoint ();
254 static void remove_step_breakpoint ();
255 static void wait_for_inferior ();
256 static void normal_stop ();
259 /* Clear out all variables saying what to do when inferior is continued.
260 First do this, then set the ones you want, then call `proceed'. */
263 clear_proceed_status ()
266 step_range_start
= 0;
268 step_frame_address
= 0;
269 step_over_calls
= -1;
270 step_resume_break_address
= 0;
272 stop_after_attach
= 0;
274 /* Discard any remaining commands left by breakpoint we had stopped at. */
275 clear_breakpoint_commands ();
278 /* Basic routine for continuing the program in various fashions.
280 ADDR is the address to resume at, or -1 for resume where stopped.
281 SIGNAL is the signal to give it, or 0 for none,
282 or -1 for act according to how it stopped.
283 STEP is nonzero if should trap after one instruction.
284 -1 means return after that and print nothing.
285 You should probably set various step_... variables
286 before calling here, if you are stepping.
288 You should call clear_proceed_status before calling proceed. */
291 proceed (addr
, signal
, step
)
299 step_start_function
= find_pc_function (read_pc ());
305 /* If there is a breakpoint at the address we will resume at,
306 step one instruction before inserting breakpoints
307 so that we do not stop right away. */
309 if (!pc_changed
&& breakpoint_here_p (read_pc ()))
314 write_register (PC_REGNUM
, addr
);
316 write_register (NPC_REGNUM
, addr
+ 4);
320 if (trap_expected_after_continue
)
322 /* If (step == 0), a trap will be automatically generated after
323 the first instruction is executed. Force step one
324 instruction to clear this condition. This should not occur
325 if step is nonzero, but it is harmless in that case. */
327 trap_expected_after_continue
= 0;
331 /* We will get a trace trap after one instruction.
332 Continue it automatically and insert breakpoints then. */
336 int temp
= insert_breakpoints ();
339 print_sys_errmsg ("ptrace", temp
);
340 error ("Cannot insert breakpoints.\n\
341 The same program may be running in another process.");
343 breakpoints_inserted
= 1;
346 /* Install inferior's terminal modes. */
347 terminal_inferior ();
350 stop_signal
= signal
;
351 /* If this signal should not be seen by program,
352 give it zero. Used for debugging signals. */
353 else if (stop_signal
< NSIG
&& !signal_program
[stop_signal
])
356 /* Resume inferior. */
357 resume (oneproc
|| step
, stop_signal
);
359 /* Wait for it to stop (if not standalone)
360 and in any case decode why it stopped, and act accordingly. */
362 wait_for_inferior ();
366 /* Writing the inferior pc as a register calls this function
367 to inform infrun that the pc has been set in the debugger. */
377 /* Start an inferior process for the first time.
378 Actually it was started by the fork that created it,
379 but it will have stopped one instruction after execing sh.
380 Here we must get it up to actual execution of the real program. */
385 /* We will get a trace trap after one instruction.
386 Continue it automatically. Eventually (after shell does an exec)
387 it will get another trace trap. Then insert breakpoints and continue. */
389 #ifdef START_INFERIOR_TRAPS_EXPECTED
390 trap_expected
= START_INFERIOR_TRAPS_EXPECTED
;
395 running_in_shell
= 0; /* Set to 1 at first SIGTRAP, 0 at second. */
396 trap_expected_after_continue
= 0;
397 breakpoints_inserted
= 0;
398 mark_breakpoints_out ();
400 /* Set up the "saved terminal modes" of the inferior
401 based on what modes we are starting it with. */
402 terminal_init_inferior ();
404 /* Install inferior's terminal modes. */
405 terminal_inferior ();
407 if (remote_debugging
)
410 fetch_inferior_registers();
411 set_current_frame (create_new_frame (read_register (FP_REGNUM
),
413 stop_frame_address
= FRAME_FP (get_current_frame());
415 if (insert_breakpoints())
416 fatal("Can't insert breakpoints");
417 breakpoints_inserted
= 1;
422 wait_for_inferior ();
427 /* Start remote-debugging of a machine over a serial link. */
432 clear_proceed_status ();
433 running_in_shell
= 0;
436 breakpoints_inserted
= 0;
437 mark_breakpoints_out ();
438 wait_for_inferior ();
444 /* Attach to process PID, then initialize for debugging it
445 and wait for the trace-trap that results from attaching. */
454 mark_breakpoints_out ();
455 terminal_init_inferior ();
456 clear_proceed_status ();
457 stop_after_attach
= 1;
458 /*proceed (-1, 0, -2);*/
459 terminal_inferior ();
460 wait_for_inferior ();
463 #endif /* ATTACH_DETACH */
465 /* Wait for control to return from inferior to debugger.
466 If inferior gets a signal, we may decide to start it up again
467 instead of returning. That is why there is a loop in this function.
468 When this function actually returns it means the inferior
469 should be left stopped and GDB should read more commands. */
480 CORE_ADDR stop_sp
, prev_sp
;
481 CORE_ADDR prev_func_start
, stop_func_start
;
482 char *prev_func_name
, *stop_func_name
;
483 CORE_ADDR prologue_pc
;
484 int stop_step_resume_break
;
485 CORE_ADDR step_resume_break_sp
;
488 struct symtab_and_line sal
;
490 extern CORE_ADDR text_end
;
491 int remove_breakpoints_on_following_step
= 0;
493 prev_pc
= read_pc ();
494 (void) find_pc_partial_function (prev_pc
, &prev_func_name
,
496 prev_func_start
+= FUNCTION_START_OFFSET
;
497 prev_sp
= read_register (SP_REGNUM
);
501 /* Clean up saved state that will become invalid. */
503 flush_cached_frames ();
505 if (remote_debugging
)
510 if (pid
!= inferior_pid
)
514 /* See if the process still exists; clean up if it doesn't. */
517 terminal_ours_for_output ();
519 printf ("\nProgram exited with code 0%o.\n", WRETCODE (w
));
521 printf ("\nProgram exited normally.\n");
524 #ifdef NO_SINGLE_STEP
527 stop_print_frame
= 0;
530 else if (!WIFSTOPPED (w
))
533 stop_print_frame
= 0;
534 stop_signal
= WTERMSIG (w
);
535 terminal_ours_for_output ();
536 printf ("\nProgram terminated with signal %d, %s\n",
539 ? sys_siglist
[stop_signal
]
541 printf ("The inferior process no longer exists.\n");
543 #ifdef NO_SINGLE_STEP
549 #ifdef NO_SINGLE_STEP
551 single_step (0); /* This actually cleans up the ss */
552 #endif /* NO_SINGLE_STEP */
554 fetch_inferior_registers ();
555 stop_pc
= read_pc ();
556 set_current_frame ( create_new_frame (read_register (FP_REGNUM
),
559 stop_frame_address
= FRAME_FP (get_current_frame ());
560 stop_sp
= read_register (SP_REGNUM
);
563 /* Don't care about return value; stop_func_start and stop_func_name
564 will both be 0 if it doesn't work. */
565 (void) find_pc_partial_function (stop_pc
, &stop_func_name
,
567 stop_func_start
+= FUNCTION_START_OFFSET
;
571 stop_stack_dummy
= 0;
572 stop_print_frame
= 1;
573 stop_step_resume_break
= 0;
575 stopped_by_random_signal
= 0;
576 breakpoints_failed
= 0;
578 /* Look at the cause of the stop, and decide what to do.
579 The alternatives are:
580 1) break; to really stop and return to the debugger,
581 2) drop through to start up again
582 (set another_trap to 1 to single step once)
583 3) set random_signal to 1, and the decision between 1 and 2
584 will be made according to the signal handling tables. */
586 stop_signal
= WSTOPSIG (w
);
588 /* First, distinguish signals caused by the debugger from signals
589 that have to do with the program's own actions.
590 Note that breakpoint insns may cause SIGTRAP or SIGILL
591 or SIGEMT, depending on the operating system version.
592 Here we detect when a SIGILL or SIGEMT is really a breakpoint
593 and change it to SIGTRAP. */
595 if (stop_signal
== SIGTRAP
596 || (breakpoints_inserted
&&
597 (stop_signal
== SIGILL
598 || stop_signal
== SIGEMT
))
599 || stop_after_attach
)
601 if (stop_signal
== SIGTRAP
&& stop_after_trap
)
603 stop_print_frame
= 0;
606 if (stop_after_attach
)
608 /* Don't even think about breakpoints
609 if still running the shell that will exec the program
610 or if just proceeded over a breakpoint. */
611 if (stop_signal
== SIGTRAP
&& trap_expected
)
615 /* See if there is a breakpoint at the current PC. */
616 #if DECR_PC_AFTER_BREAK
617 /* Notice the case of stepping through a jump
618 that leads just after a breakpoint.
619 Don't confuse that with hitting the breakpoint.
620 What we check for is that 1) stepping is going on
621 and 2) the pc before the last insn does not match
622 the address of the breakpoint before the current pc. */
623 if (!(prev_pc
!= stop_pc
- DECR_PC_AFTER_BREAK
624 && step_range_end
&& !step_resume_break_address
))
625 #endif /* DECR_PC_AFTER_BREAK not zero */
627 /* See if we stopped at the special breakpoint for
628 stepping over a subroutine call. */
629 if (stop_pc
- DECR_PC_AFTER_BREAK
630 == step_resume_break_address
)
632 stop_step_resume_break
= 1;
633 if (DECR_PC_AFTER_BREAK
)
635 stop_pc
-= DECR_PC_AFTER_BREAK
;
636 write_register (PC_REGNUM
, stop_pc
);
643 breakpoint_stop_status (stop_pc
, stop_frame_address
);
644 /* Following in case break condition called a
646 stop_print_frame
= 1;
647 if (stop_breakpoint
&& DECR_PC_AFTER_BREAK
)
649 stop_pc
-= DECR_PC_AFTER_BREAK
;
650 write_register (PC_REGNUM
, stop_pc
);
652 write_register (NPC_REGNUM
, stop_pc
+ 4);
660 if (stop_signal
== SIGTRAP
)
662 = !(stop_breakpoint
|| trap_expected
663 || stop_step_resume_break
664 #ifndef CANNOT_EXECUTE_STACK
665 || (stop_sp INNER_THAN stop_pc
666 && stop_pc INNER_THAN stop_frame_address
)
668 || stop_pc
== text_end
- 2
670 || (step_range_end
&& !step_resume_break_address
));
675 || stop_step_resume_break
677 || (stop_sp INNER_THAN stop_pc
678 && stop_pc INNER_THAN stop_frame_address
)
683 stop_signal
= SIGTRAP
;
689 /* For the program's own signals, act according to
690 the signal handling tables. */
693 && !(running_in_shell
&& stop_signal
== SIGSEGV
))
695 /* Signal not for debugging purposes. */
698 stopped_by_random_signal
= 1;
700 if (stop_signal
>= NSIG
701 || signal_print
[stop_signal
])
704 terminal_ours_for_output ();
705 printf ("\nProgram received signal %d, %s\n",
708 ? sys_siglist
[stop_signal
]
712 if (stop_signal
>= NSIG
713 || signal_stop
[stop_signal
])
715 /* If not going to stop, give terminal back
716 if we took it away. */
718 terminal_inferior ();
721 /* Handle cases caused by hitting a breakpoint. */
724 && (stop_breakpoint
|| stop_step_resume_break
))
726 /* Does a breakpoint want us to stop? */
727 if (stop_breakpoint
&& stop_breakpoint
!= -1
728 && stop_breakpoint
!= -0x1000001)
730 /* 0x1000000 is set in stop_breakpoint as returned by
731 breakpoint_stop_status to indicate a silent
733 if ((stop_breakpoint
> 0 ? stop_breakpoint
:
737 stop_print_frame
= 0;
738 if (stop_breakpoint
> 0)
739 stop_breakpoint
-= 0x1000000;
741 stop_breakpoint
+= 0x1000000;
745 /* But if we have hit the step-resumption breakpoint,
746 remove it. It has done its job getting us here.
747 The sp test is to make sure that we don't get hung
748 up in recursive calls in functions without frame
749 pointers. If the stack pointer isn't outside of
750 where the breakpoint was set (within a routine to be
751 stepped over), we're in the middle of a recursive
752 call. Not true for reg window machines (sparc)
753 because the must change frames to call things and
754 the stack pointer doesn't have to change if it
755 the bp was set in a routine without a frame (pc can
756 be stored in some other window).
758 The removal of the sp test is to allow calls to
759 alloca. Nasty things were happening. Oh, well,
760 gdb can only handle one level deep of lack of
762 if (stop_step_resume_break
763 && (step_frame_address
== 0
764 || (stop_frame_address
== step_frame_address
)))
766 remove_step_breakpoint ();
767 step_resume_break_address
= 0;
769 /* Otherwise, must remove breakpoints and single-step
770 to get us past the one we hit. */
773 remove_breakpoints ();
774 remove_step_breakpoint ();
775 breakpoints_inserted
= 0;
779 /* We come here if we hit a breakpoint but should not
780 stop for it. Possibly we also were stepping
781 and should stop for that. So fall through and
782 test for stepping. But, if not stepping,
786 /* If this is the breakpoint at the end of a stack dummy,
787 just stop silently. */
788 #ifndef CANNOT_EXECUTE_STACK
789 if (stop_sp INNER_THAN stop_pc
790 && stop_pc INNER_THAN stop_frame_address
)
792 if (stop_pc
== text_end
- 2)
795 stop_print_frame
= 0;
796 stop_stack_dummy
= 1;
798 trap_expected_after_continue
= 1;
803 if (step_resume_break_address
)
804 /* Having a step-resume breakpoint overrides anything
805 else having to do with stepping commands until
806 that breakpoint is reached. */
808 /* If stepping through a line, keep going if still within it. */
809 else if (!random_signal
811 && stop_pc
>= step_range_start
812 && stop_pc
< step_range_end
813 /* The step range might include the start of the
814 function, so if we are at the start of the
815 step range and either the stack or frame pointers
816 just changed, we've stepped outside */
817 && !(stop_pc
== step_range_start
818 && stop_frame_address
819 && (stop_sp INNER_THAN prev_sp
820 || stop_frame_address
!= step_frame_address
)))
822 /* Don't step through the return from a function
823 unless that is the first instruction stepped through. */
824 if (ABOUT_TO_RETURN (stop_pc
))
831 /* We stepped out of the stepping range. See if that was due
832 to a subroutine call that we should proceed to the end of. */
833 else if (!random_signal
&& step_range_end
)
837 prologue_pc
= stop_func_start
;
838 SKIP_PROLOGUE (prologue_pc
);
841 /* Did we just take a signal? */
842 if (IN_SIGTRAMP (stop_pc
, stop_func_name
)
843 && !IN_SIGTRAMP (prev_pc
, prev_func_name
))
845 /* We've just taken a signal; go until we are back to
846 the point where we took it and one more. */
847 step_resume_break_address
= prev_pc
;
848 step_resume_break_duplicate
=
849 breakpoint_here_p (step_resume_break_address
);
850 step_resume_break_sp
= stop_sp
;
851 if (breakpoints_inserted
)
852 insert_step_breakpoint ();
853 /* Make sure that the stepping range gets us past
855 if (step_range_end
== 1)
856 step_range_end
= (step_range_start
= prev_pc
) + 1;
857 remove_breakpoints_on_following_step
= 1;
860 /* ==> See comments at top of file on this algorithm. <==*/
862 else if (stop_pc
== stop_func_start
863 && (stop_func_start
!= prev_func_start
864 || prologue_pc
!= stop_func_start
865 || stop_sp
!= prev_sp
))
867 /* It's a subroutine call */
868 if (step_over_calls
> 0
869 || (step_over_calls
&& find_pc_function (stop_pc
) == 0))
871 /* A subroutine call has happened. */
872 /* Set a special breakpoint after the return */
873 step_resume_break_address
=
874 SAVED_PC_AFTER_CALL (get_current_frame ());
875 step_resume_break_duplicate
876 = breakpoint_here_p (step_resume_break_address
);
877 step_resume_break_sp
= stop_sp
;
878 if (breakpoints_inserted
)
879 insert_step_breakpoint ();
881 /* Subroutine call with source code we should not step over.
882 Do step to the first line of code in it. */
883 else if (step_over_calls
)
885 SKIP_PROLOGUE (stop_func_start
);
886 sal
= find_pc_line (stop_func_start
, 0);
887 /* Use the step_resume_break to step until
888 the end of the prologue, even if that involves jumps
889 (as it seems to on the vax under 4.2). */
890 /* If the prologue ends in the middle of a source line,
891 continue to the end of that source line.
892 Otherwise, just go to end of prologue. */
893 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
894 /* no, don't either. It skips any code that's
895 legitimately on the first line. */
897 if (sal
.end
&& sal
.pc
!= stop_func_start
)
898 stop_func_start
= sal
.end
;
901 if (stop_func_start
== stop_pc
)
903 /* We are already there: stop now. */
908 /* Put the step-breakpoint there and go until there. */
910 step_resume_break_address
= stop_func_start
;
911 step_resume_break_sp
= stop_sp
;
913 step_resume_break_duplicate
914 = breakpoint_here_p (step_resume_break_address
);
915 if (breakpoints_inserted
)
916 insert_step_breakpoint ();
917 /* Do not specify what the fp should be when we stop
918 since on some machines the prologue
919 is where the new fp value is established. */
920 step_frame_address
= 0;
921 /* And make sure stepping stops right away then. */
922 step_range_end
= step_range_start
;
927 /* We get here only if step_over_calls is 0 and we
928 just stepped into a subroutine. I presume
929 that step_over_calls is only 0 when we're
930 supposed to be stepping at the assembly
936 /* No subroutince call; stop now. */
944 /* Save the pc before execution, to compare with pc after stop. */
945 prev_pc
= read_pc (); /* Might have been DECR_AFTER_BREAK */
946 prev_func_start
= stop_func_start
; /* Ok, since if DECR_PC_AFTER
947 BREAK is defined, the
948 original pc would not have
949 been at the start of a
951 prev_func_name
= stop_func_name
;
954 /* If we did not do break;, it means we should keep
955 running the inferior and not return to debugger. */
957 /* If trap_expected is 2, it means continue once more
958 and insert breakpoints at the next trap.
959 If trap_expected is 1 and the signal was SIGSEGV, it means
960 the shell is doing some memory allocation--just resume it
962 Otherwise insert breakpoints now, and possibly single step. */
964 if (trap_expected
> 1)
967 running_in_shell
= 1;
970 else if (running_in_shell
&& stop_signal
== SIGSEGV
)
974 else if (trap_expected
&& stop_signal
!= SIGTRAP
)
976 /* We took a signal which we are supposed to pass through to
977 the inferior and we haven't yet gotten our trap. Simply
979 resume ((step_range_end
&& !step_resume_break_address
)
985 /* Here, we are not awaiting another exec to get
986 the program we really want to debug.
987 Insert breakpoints now, unless we are trying
988 to one-proceed past a breakpoint. */
989 running_in_shell
= 0;
990 /* If we've just finished a special step resume and we don't
991 want to hit a breakpoint, pull em out. */
992 if (!step_resume_break_address
&&
993 remove_breakpoints_on_following_step
)
995 remove_breakpoints_on_following_step
= 0;
996 remove_breakpoints ();
997 breakpoints_inserted
= 0;
999 else if (!breakpoints_inserted
&& !another_trap
)
1001 insert_step_breakpoint ();
1002 breakpoints_failed
= insert_breakpoints ();
1003 if (breakpoints_failed
)
1005 breakpoints_inserted
= 1;
1008 trap_expected
= another_trap
;
1010 if (stop_signal
== SIGTRAP
)
1013 resume ((step_range_end
&& !step_resume_break_address
)
1020 /* Here to return control to GDB when the inferior stops for real.
1021 Print appropriate messages, remove breakpoints, give terminal our modes.
1023 RUNNING_IN_SHELL nonzero means the shell got a signal before
1024 exec'ing the program we wanted to run.
1025 STOP_PRINT_FRAME nonzero means print the executing frame
1026 (pc, function, args, file, line number and line text).
1027 BREAKPOINTS_FAILED nonzero means stop was due to error
1028 attempting to insert breakpoints. */
1033 /* Make sure that the current_frame's pc is correct. This
1034 is a correction for setting up the frame info before doing
1035 DECR_PC_AFTER_BREAK */
1037 (get_current_frame ())->pc
= read_pc ();
1039 if (breakpoints_failed
)
1041 terminal_ours_for_output ();
1042 print_sys_errmsg ("ptrace", breakpoints_failed
);
1043 printf ("Stopped; cannot insert breakpoints.\n\
1044 The same program may be running in another process.\n");
1048 remove_step_breakpoint ();
1050 if (inferior_pid
&& breakpoints_inserted
)
1051 if (remove_breakpoints ())
1053 terminal_ours_for_output ();
1054 printf ("Cannot remove breakpoints because program is no longer writable.\n\
1055 It must be running in another process.\n\
1056 Further execution is probably impossible.\n");
1059 breakpoints_inserted
= 0;
1061 /* Delete the breakpoint we stopped at, if it wants to be deleted.
1062 Delete any breakpoint that is to be deleted at the next stop. */
1064 breakpoint_auto_delete (stop_breakpoint
);
1066 /* If an auto-display called a function and that got a signal,
1067 delete that auto-display to avoid an infinite recursion. */
1069 if (stopped_by_random_signal
)
1070 disable_current_display ();
1072 if (step_multi
&& stop_step
)
1077 if (running_in_shell
)
1079 if (stop_signal
== SIGSEGV
)
1081 char *exec_file
= (char *) get_exec_file (1);
1083 if (access (exec_file
, X_OK
) != 0)
1084 printf ("The file \"%s\" is not executable.\n", exec_file
);
1087 You have just encountered a bug in \"sh\". GDB starts your program\n\
1088 by running \"sh\" with a command to exec your program.\n\
1089 This is so that \"sh\" will process wildcards and I/O redirection.\n\
1090 This time, \"sh\" crashed.\n\
1092 One known bug in \"sh\" bites when the environment takes up a lot of space.\n\
1093 Try \"info env\" to see the environment; then use \"delete env\" to kill\n\
1094 some variables whose values are large; then do \"run\" again.\n\
1096 If that works, you might want to put those \"delete env\" commands\n\
1097 into a \".gdbinit\" file in this directory so they will happen every time.\n");
1099 /* Don't confuse user with his program's symbols on sh's data. */
1100 stop_print_frame
= 0;
1103 if (inferior_pid
== 0)
1106 /* Select innermost stack frame except on return from a stack dummy routine,
1107 or if the program has exited. */
1108 if (!stop_stack_dummy
)
1110 select_frame (get_current_frame (), 0);
1112 if (stop_print_frame
)
1114 if (stop_breakpoint
> 0)
1115 printf ("\nBpt %d, ", stop_breakpoint
);
1116 print_sel_frame (stop_step
1117 && step_frame_address
== stop_frame_address
1118 && step_start_function
== find_pc_function (stop_pc
));
1119 /* Display the auto-display expressions. */
1124 /* Save the function value return registers
1125 We might be about to restore their previous contents. */
1126 read_register_bytes (0, stop_registers
, REGISTER_BYTES
);
1128 if (stop_stack_dummy
)
1130 /* Pop the empty frame that contains the stack dummy.
1131 POP_FRAME ends with a setting of the current frame, so we
1132 can use that next. */
1134 select_frame (get_current_frame (), 0);
1139 insert_step_breakpoint ()
1141 if (step_resume_break_address
&& !step_resume_break_duplicate
)
1143 read_memory (step_resume_break_address
,
1144 step_resume_break_shadow
, sizeof break_insn
);
1145 write_memory (step_resume_break_address
,
1146 break_insn
, sizeof break_insn
);
1151 remove_step_breakpoint ()
1153 if (step_resume_break_address
&& !step_resume_break_duplicate
)
1154 write_memory (step_resume_break_address
, step_resume_break_shadow
,
1158 /* Specify how various signals in the inferior should be handled. */
1161 handle_command (args
, from_tty
)
1165 register char *p
= args
;
1167 register int digits
, wordlen
;
1170 error_no_arg ("signal to handle");
1174 /* Find the end of the next word in the args. */
1175 for (wordlen
= 0; p
[wordlen
] && p
[wordlen
] != ' ' && p
[wordlen
] != '\t';
1177 for (digits
= 0; p
[digits
] >= '0' && p
[digits
] <= '9'; digits
++);
1179 /* If it is all digits, it is signal number to operate on. */
1180 if (digits
== wordlen
)
1183 if (signum
<= 0 || signum
>= NSIG
)
1186 error ("Invalid signal %s given as argument to \"handle\".", p
);
1188 if (signum
== SIGTRAP
|| signum
== SIGINT
)
1190 if (!query ("Signal %d is used by the debugger.\nAre you sure you want to change it? ", signum
))
1191 error ("Not confirmed.");
1194 else if (signum
== 0)
1195 error ("First argument is not a signal number.");
1197 /* Else, if already got a signal number, look for flag words
1198 saying what to do for it. */
1199 else if (!strncmp (p
, "stop", wordlen
))
1201 signal_stop
[signum
] = 1;
1202 signal_print
[signum
] = 1;
1204 else if (wordlen
>= 2 && !strncmp (p
, "print", wordlen
))
1205 signal_print
[signum
] = 1;
1206 else if (wordlen
>= 2 && !strncmp (p
, "pass", wordlen
))
1207 signal_program
[signum
] = 1;
1208 else if (!strncmp (p
, "ignore", wordlen
))
1209 signal_program
[signum
] = 0;
1210 else if (wordlen
>= 3 && !strncmp (p
, "nostop", wordlen
))
1211 signal_stop
[signum
] = 0;
1212 else if (wordlen
>= 4 && !strncmp (p
, "noprint", wordlen
))
1214 signal_print
[signum
] = 0;
1215 signal_stop
[signum
] = 0;
1217 else if (wordlen
>= 4 && !strncmp (p
, "nopass", wordlen
))
1218 signal_program
[signum
] = 0;
1219 else if (wordlen
>= 3 && !strncmp (p
, "noignore", wordlen
))
1220 signal_program
[signum
] = 1;
1221 /* Not a number and not a recognized flag word => complain. */
1225 error ("Unrecognized flag word: \"%s\".", p
);
1228 /* Find start of next word. */
1230 while (*p
== ' ' || *p
== '\t') p
++;
1235 /* Show the results. */
1236 printf ("Number\tStop\tPrint\tPass to program\tDescription\n");
1237 printf ("%d\t", signum
);
1238 printf ("%s\t", signal_stop
[signum
] ? "Yes" : "No");
1239 printf ("%s\t", signal_print
[signum
] ? "Yes" : "No");
1240 printf ("%s\t\t", signal_program
[signum
] ? "Yes" : "No");
1241 printf ("%s\n", sys_siglist
[signum
]);
1245 /* Print current contents of the tables set by the handle command. */
1248 signals_info (signum_exp
)
1252 printf_filtered ("Number\tStop\tPrint\tPass to program\tDescription\n");
1256 i
= parse_and_eval_address (signum_exp
);
1257 if (i
>= NSIG
|| i
< 0)
1258 error ("Signal number out of bounds.");
1259 printf_filtered ("%d\t", i
);
1260 printf_filtered ("%s\t", signal_stop
[i
] ? "Yes" : "No");
1261 printf_filtered ("%s\t", signal_print
[i
] ? "Yes" : "No");
1262 printf_filtered ("%s\t\t", signal_program
[i
] ? "Yes" : "No");
1263 printf_filtered ("%s\n", sys_siglist
[i
]);
1267 printf_filtered ("\n");
1268 for (i
= 0; i
< NSIG
; i
++)
1272 printf_filtered ("%d\t", i
);
1273 printf_filtered ("%s\t", signal_stop
[i
] ? "Yes" : "No");
1274 printf_filtered ("%s\t", signal_print
[i
] ? "Yes" : "No");
1275 printf_filtered ("%s\t\t", signal_program
[i
] ? "Yes" : "No");
1276 printf_filtered ("%s\n", sys_siglist
[i
]);
1279 printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
1282 /* Save all of the information associated with the inferior<==>gdb
1283 connection. INF_STATUS is a pointer to a "struct inferior_status"
1284 (defined in inferior.h). */
1286 struct command_line
*get_breakpoint_commands ();
1289 save_inferior_status (inf_status
, restore_stack_info
)
1290 struct inferior_status
*inf_status
;
1291 int restore_stack_info
;
1293 inf_status
->pc_changed
= pc_changed
;
1294 inf_status
->stop_signal
= stop_signal
;
1295 inf_status
->stop_pc
= stop_pc
;
1296 inf_status
->stop_frame_address
= stop_frame_address
;
1297 inf_status
->stop_breakpoint
= stop_breakpoint
;
1298 inf_status
->stop_step
= stop_step
;
1299 inf_status
->stop_stack_dummy
= stop_stack_dummy
;
1300 inf_status
->stopped_by_random_signal
= stopped_by_random_signal
;
1301 inf_status
->trap_expected
= trap_expected
;
1302 inf_status
->step_range_start
= step_range_start
;
1303 inf_status
->step_range_end
= step_range_end
;
1304 inf_status
->step_frame_address
= step_frame_address
;
1305 inf_status
->step_over_calls
= step_over_calls
;
1306 inf_status
->step_resume_break_address
= step_resume_break_address
;
1307 inf_status
->stop_after_trap
= stop_after_trap
;
1308 inf_status
->stop_after_attach
= stop_after_attach
;
1309 inf_status
->breakpoint_commands
= get_breakpoint_commands ();
1310 inf_status
->restore_stack_info
= restore_stack_info
;
1312 bcopy (stop_registers
, inf_status
->stop_registers
, REGISTER_BYTES
);
1314 record_selected_frame (&(inf_status
->selected_frame_address
),
1315 &(inf_status
->selected_level
));
1320 restore_inferior_status (inf_status
)
1321 struct inferior_status
*inf_status
;
1324 int level
= inf_status
->selected_level
;
1326 pc_changed
= inf_status
->pc_changed
;
1327 stop_signal
= inf_status
->stop_signal
;
1328 stop_pc
= inf_status
->stop_pc
;
1329 stop_frame_address
= inf_status
->stop_frame_address
;
1330 stop_breakpoint
= inf_status
->stop_breakpoint
;
1331 stop_step
= inf_status
->stop_step
;
1332 stop_stack_dummy
= inf_status
->stop_stack_dummy
;
1333 stopped_by_random_signal
= inf_status
->stopped_by_random_signal
;
1334 trap_expected
= inf_status
->trap_expected
;
1335 step_range_start
= inf_status
->step_range_start
;
1336 step_range_end
= inf_status
->step_range_end
;
1337 step_frame_address
= inf_status
->step_frame_address
;
1338 step_over_calls
= inf_status
->step_over_calls
;
1339 step_resume_break_address
= inf_status
->step_resume_break_address
;
1340 stop_after_trap
= inf_status
->stop_after_trap
;
1341 stop_after_attach
= inf_status
->stop_after_attach
;
1342 set_breakpoint_commands (inf_status
->breakpoint_commands
);
1344 bcopy (inf_status
->stop_registers
, stop_registers
, REGISTER_BYTES
);
1346 if (inf_status
->restore_stack_info
)
1348 fid
= find_relative_frame (get_current_frame (),
1351 if (FRAME_FP (fid
) != inf_status
->selected_frame_address
||
1354 fprintf (stderr
, "Unable to restore previously selected frame.\n");
1355 select_frame (get_current_frame (), 0);
1359 select_frame (fid
, inf_status
->selected_level
);
1366 _initialize_infrun ()
1370 add_info ("signals", signals_info
,
1371 "What debugger does when program gets various signals.\n\
1372 Specify a signal number as argument to print info on that signal only.");
1374 add_com ("handle", class_run
, handle_command
,
1375 "Specify how to handle a signal.\n\
1376 Args are signal number followed by flags.\n\
1377 Flags allowed are \"stop\", \"print\", \"pass\",\n\
1378 \"nostop\", \"noprint\" or \"nopass\".\n\
1379 Print means print a message if this signal happens.\n\
1380 Stop means reenter debugger if this signal happens (implies print).\n\
1381 Pass means let program see this signal; otherwise program doesn't know.\n\
1382 Pass and Stop may be combined.");
1384 for (i
= 0; i
< NSIG
; i
++)
1387 signal_print
[i
] = 1;
1388 signal_program
[i
] = 1;
1391 /* Signals caused by debugger's own actions
1392 should not be given to the program afterwards. */
1393 signal_program
[SIGTRAP
] = 0;
1394 signal_program
[SIGINT
] = 0;
1396 /* Signals that are not errors should not normally enter the debugger. */
1398 signal_stop
[SIGALRM
] = 0;
1399 signal_print
[SIGALRM
] = 0;
1400 #endif /* SIGALRM */
1402 signal_stop
[SIGVTALRM
] = 0;
1403 signal_print
[SIGVTALRM
] = 0;
1404 #endif /* SIGVTALRM */
1406 signal_stop
[SIGPROF
] = 0;
1407 signal_print
[SIGPROF
] = 0;
1408 #endif /* SIGPROF */
1410 signal_stop
[SIGCHLD
] = 0;
1411 signal_print
[SIGCHLD
] = 0;
1412 #endif /* SIGCHLD */
1414 signal_stop
[SIGCLD
] = 0;
1415 signal_print
[SIGCLD
] = 0;
1418 signal_stop
[SIGIO
] = 0;
1419 signal_print
[SIGIO
] = 0;
1422 signal_stop
[SIGURG
] = 0;
1423 signal_print
[SIGURG
] = 0;