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).
124 #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 /* This code is needed at least in the following case:
846 The user types "next" and then a signal arrives (before
847 the "next" is done). */
848 /* We've just taken a signal; go until we are back to
849 the point where we took it and one more. */
850 step_resume_break_address
= prev_pc
;
851 step_resume_break_duplicate
=
852 breakpoint_here_p (step_resume_break_address
);
853 step_resume_break_sp
= stop_sp
;
854 if (breakpoints_inserted
)
855 insert_step_breakpoint ();
856 /* Make sure that the stepping range gets us past
858 if (step_range_end
== 1)
859 step_range_end
= (step_range_start
= prev_pc
) + 1;
860 remove_breakpoints_on_following_step
= 1;
863 /* ==> See comments at top of file on this algorithm. <==*/
865 else if (stop_pc
== stop_func_start
866 && (stop_func_start
!= prev_func_start
867 || prologue_pc
!= stop_func_start
868 || stop_sp
!= prev_sp
))
870 /* It's a subroutine call */
871 if (step_over_calls
> 0
872 || (step_over_calls
&& find_pc_function (stop_pc
) == 0))
874 /* A subroutine call has happened. */
875 /* Set a special breakpoint after the return */
876 step_resume_break_address
=
877 SAVED_PC_AFTER_CALL (get_current_frame ());
878 step_resume_break_duplicate
879 = breakpoint_here_p (step_resume_break_address
);
880 step_resume_break_sp
= stop_sp
;
881 if (breakpoints_inserted
)
882 insert_step_breakpoint ();
884 /* Subroutine call with source code we should not step over.
885 Do step to the first line of code in it. */
886 else if (step_over_calls
)
888 SKIP_PROLOGUE (stop_func_start
);
889 sal
= find_pc_line (stop_func_start
, 0);
890 /* Use the step_resume_break to step until
891 the end of the prologue, even if that involves jumps
892 (as it seems to on the vax under 4.2). */
893 /* If the prologue ends in the middle of a source line,
894 continue to the end of that source line.
895 Otherwise, just go to end of prologue. */
896 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
897 /* no, don't either. It skips any code that's
898 legitimately on the first line. */
900 if (sal
.end
&& sal
.pc
!= stop_func_start
)
901 stop_func_start
= sal
.end
;
904 if (stop_func_start
== stop_pc
)
906 /* We are already there: stop now. */
911 /* Put the step-breakpoint there and go until there. */
913 step_resume_break_address
= stop_func_start
;
914 step_resume_break_sp
= stop_sp
;
916 step_resume_break_duplicate
917 = breakpoint_here_p (step_resume_break_address
);
918 if (breakpoints_inserted
)
919 insert_step_breakpoint ();
920 /* Do not specify what the fp should be when we stop
921 since on some machines the prologue
922 is where the new fp value is established. */
923 step_frame_address
= 0;
924 /* And make sure stepping stops right away then. */
925 step_range_end
= step_range_start
;
930 /* We get here only if step_over_calls is 0 and we
931 just stepped into a subroutine. I presume
932 that step_over_calls is only 0 when we're
933 supposed to be stepping at the assembly
939 /* No subroutince call; stop now. */
947 /* Save the pc before execution, to compare with pc after stop. */
948 prev_pc
= read_pc (); /* Might have been DECR_AFTER_BREAK */
949 prev_func_start
= stop_func_start
; /* Ok, since if DECR_PC_AFTER
950 BREAK is defined, the
951 original pc would not have
952 been at the start of a
954 prev_func_name
= stop_func_name
;
957 /* If we did not do break;, it means we should keep
958 running the inferior and not return to debugger. */
960 /* If trap_expected is 2, it means continue once more
961 and insert breakpoints at the next trap.
962 If trap_expected is 1 and the signal was SIGSEGV, it means
963 the shell is doing some memory allocation--just resume it
965 Otherwise insert breakpoints now, and possibly single step. */
967 if (trap_expected
> 1)
970 running_in_shell
= 1;
973 else if (running_in_shell
&& stop_signal
== SIGSEGV
)
977 else if (trap_expected
&& stop_signal
!= SIGTRAP
)
979 /* We took a signal which we are supposed to pass through to
980 the inferior and we haven't yet gotten our trap. Simply
982 resume ((step_range_end
&& !step_resume_break_address
)
988 /* Here, we are not awaiting another exec to get
989 the program we really want to debug.
990 Insert breakpoints now, unless we are trying
991 to one-proceed past a breakpoint. */
992 running_in_shell
= 0;
993 /* If we've just finished a special step resume and we don't
994 want to hit a breakpoint, pull em out. */
995 if (!step_resume_break_address
&&
996 remove_breakpoints_on_following_step
)
998 remove_breakpoints_on_following_step
= 0;
999 remove_breakpoints ();
1000 breakpoints_inserted
= 0;
1002 else if (!breakpoints_inserted
&& !another_trap
)
1004 insert_step_breakpoint ();
1005 breakpoints_failed
= insert_breakpoints ();
1006 if (breakpoints_failed
)
1008 breakpoints_inserted
= 1;
1011 trap_expected
= another_trap
;
1013 if (stop_signal
== SIGTRAP
)
1016 resume ((step_range_end
&& !step_resume_break_address
)
1023 /* Here to return control to GDB when the inferior stops for real.
1024 Print appropriate messages, remove breakpoints, give terminal our modes.
1026 RUNNING_IN_SHELL nonzero means the shell got a signal before
1027 exec'ing the program we wanted to run.
1028 STOP_PRINT_FRAME nonzero means print the executing frame
1029 (pc, function, args, file, line number and line text).
1030 BREAKPOINTS_FAILED nonzero means stop was due to error
1031 attempting to insert breakpoints. */
1036 /* Make sure that the current_frame's pc is correct. This
1037 is a correction for setting up the frame info before doing
1038 DECR_PC_AFTER_BREAK */
1040 (get_current_frame ())->pc
= read_pc ();
1042 if (breakpoints_failed
)
1044 terminal_ours_for_output ();
1045 print_sys_errmsg ("ptrace", breakpoints_failed
);
1046 printf ("Stopped; cannot insert breakpoints.\n\
1047 The same program may be running in another process.\n");
1051 remove_step_breakpoint ();
1053 if (inferior_pid
&& breakpoints_inserted
)
1054 if (remove_breakpoints ())
1056 terminal_ours_for_output ();
1057 printf ("Cannot remove breakpoints because program is no longer writable.\n\
1058 It must be running in another process.\n\
1059 Further execution is probably impossible.\n");
1062 breakpoints_inserted
= 0;
1064 /* Delete the breakpoint we stopped at, if it wants to be deleted.
1065 Delete any breakpoint that is to be deleted at the next stop. */
1067 breakpoint_auto_delete (stop_breakpoint
);
1069 /* If an auto-display called a function and that got a signal,
1070 delete that auto-display to avoid an infinite recursion. */
1072 if (stopped_by_random_signal
)
1073 disable_current_display ();
1075 if (step_multi
&& stop_step
)
1080 if (running_in_shell
)
1082 if (stop_signal
== SIGSEGV
)
1084 char *exec_file
= (char *) get_exec_file (1);
1086 if (access (exec_file
, X_OK
) != 0)
1087 printf ("The file \"%s\" is not executable.\n", exec_file
);
1089 /* I don't think we should ever get here.
1090 wait_for_inferior now ignores SIGSEGV's which happen in
1091 the shell (since the Bourne shell (/bin/sh) has some
1092 rather, er, uh, *unorthodox* memory management
1093 involving catching SIGSEGV). */
1095 You have just encountered a bug in \"sh\". GDB starts your program\n\
1096 by running \"sh\" with a command to exec your program.\n\
1097 This is so that \"sh\" will process wildcards and I/O redirection.\n\
1098 This time, \"sh\" crashed.\n\
1100 One known bug in \"sh\" bites when the environment takes up a lot of space.\n\
1101 Try \"info env\" to see the environment; then use \"delete env\" to kill\n\
1102 some variables whose values are large; then do \"run\" again.\n\
1104 If that works, you might want to put those \"delete env\" commands\n\
1105 into a \".gdbinit\" file in this directory so they will happen every time.\n");
1107 /* Don't confuse user with his program's symbols on sh's data. */
1108 stop_print_frame
= 0;
1111 if (inferior_pid
== 0)
1114 /* Select innermost stack frame except on return from a stack dummy routine,
1115 or if the program has exited. */
1116 if (!stop_stack_dummy
)
1118 select_frame (get_current_frame (), 0);
1120 if (stop_print_frame
)
1122 if (stop_breakpoint
> 0)
1123 printf ("\nBpt %d, ", stop_breakpoint
);
1124 print_sel_frame (stop_step
1125 && step_frame_address
== stop_frame_address
1126 && step_start_function
== find_pc_function (stop_pc
));
1127 /* Display the auto-display expressions. */
1132 /* Save the function value return registers
1133 We might be about to restore their previous contents. */
1134 read_register_bytes (0, stop_registers
, REGISTER_BYTES
);
1136 if (stop_stack_dummy
)
1138 /* Pop the empty frame that contains the stack dummy.
1139 POP_FRAME ends with a setting of the current frame, so we
1140 can use that next. */
1142 select_frame (get_current_frame (), 0);
1147 insert_step_breakpoint ()
1149 if (step_resume_break_address
&& !step_resume_break_duplicate
)
1151 read_memory (step_resume_break_address
,
1152 step_resume_break_shadow
, sizeof break_insn
);
1153 write_memory (step_resume_break_address
,
1154 break_insn
, sizeof break_insn
);
1159 remove_step_breakpoint ()
1161 if (step_resume_break_address
&& !step_resume_break_duplicate
)
1162 write_memory (step_resume_break_address
, step_resume_break_shadow
,
1166 /* Specify how various signals in the inferior should be handled. */
1169 handle_command (args
, from_tty
)
1173 register char *p
= args
;
1175 register int digits
, wordlen
;
1178 error_no_arg ("signal to handle");
1182 /* Find the end of the next word in the args. */
1183 for (wordlen
= 0; p
[wordlen
] && p
[wordlen
] != ' ' && p
[wordlen
] != '\t';
1185 for (digits
= 0; p
[digits
] >= '0' && p
[digits
] <= '9'; digits
++);
1187 /* If it is all digits, it is signal number to operate on. */
1188 if (digits
== wordlen
)
1191 if (signum
<= 0 || signum
>= NSIG
)
1194 error ("Invalid signal %s given as argument to \"handle\".", p
);
1196 if (signum
== SIGTRAP
|| signum
== SIGINT
)
1198 if (!query ("Signal %d is used by the debugger.\nAre you sure you want to change it? ", signum
))
1199 error ("Not confirmed.");
1202 else if (signum
== 0)
1203 error ("First argument is not a signal number.");
1205 /* Else, if already got a signal number, look for flag words
1206 saying what to do for it. */
1207 else if (!strncmp (p
, "stop", wordlen
))
1209 signal_stop
[signum
] = 1;
1210 signal_print
[signum
] = 1;
1212 else if (wordlen
>= 2 && !strncmp (p
, "print", wordlen
))
1213 signal_print
[signum
] = 1;
1214 else if (wordlen
>= 2 && !strncmp (p
, "pass", wordlen
))
1215 signal_program
[signum
] = 1;
1216 else if (!strncmp (p
, "ignore", wordlen
))
1217 signal_program
[signum
] = 0;
1218 else if (wordlen
>= 3 && !strncmp (p
, "nostop", wordlen
))
1219 signal_stop
[signum
] = 0;
1220 else if (wordlen
>= 4 && !strncmp (p
, "noprint", wordlen
))
1222 signal_print
[signum
] = 0;
1223 signal_stop
[signum
] = 0;
1225 else if (wordlen
>= 4 && !strncmp (p
, "nopass", wordlen
))
1226 signal_program
[signum
] = 0;
1227 else if (wordlen
>= 3 && !strncmp (p
, "noignore", wordlen
))
1228 signal_program
[signum
] = 1;
1229 /* Not a number and not a recognized flag word => complain. */
1233 error ("Unrecognized flag word: \"%s\".", p
);
1236 /* Find start of next word. */
1238 while (*p
== ' ' || *p
== '\t') p
++;
1243 /* Show the results. */
1244 printf ("Number\tStop\tPrint\tPass to program\tDescription\n");
1245 printf ("%d\t", signum
);
1246 printf ("%s\t", signal_stop
[signum
] ? "Yes" : "No");
1247 printf ("%s\t", signal_print
[signum
] ? "Yes" : "No");
1248 printf ("%s\t\t", signal_program
[signum
] ? "Yes" : "No");
1249 printf ("%s\n", sys_siglist
[signum
]);
1253 /* Print current contents of the tables set by the handle command. */
1256 signals_info (signum_exp
)
1260 printf_filtered ("Number\tStop\tPrint\tPass to program\tDescription\n");
1264 i
= parse_and_eval_address (signum_exp
);
1265 if (i
>= NSIG
|| i
< 0)
1266 error ("Signal number out of bounds.");
1267 printf_filtered ("%d\t", i
);
1268 printf_filtered ("%s\t", signal_stop
[i
] ? "Yes" : "No");
1269 printf_filtered ("%s\t", signal_print
[i
] ? "Yes" : "No");
1270 printf_filtered ("%s\t\t", signal_program
[i
] ? "Yes" : "No");
1271 printf_filtered ("%s\n", sys_siglist
[i
]);
1275 printf_filtered ("\n");
1276 for (i
= 0; i
< NSIG
; i
++)
1280 printf_filtered ("%d\t", i
);
1281 printf_filtered ("%s\t", signal_stop
[i
] ? "Yes" : "No");
1282 printf_filtered ("%s\t", signal_print
[i
] ? "Yes" : "No");
1283 printf_filtered ("%s\t\t", signal_program
[i
] ? "Yes" : "No");
1284 printf_filtered ("%s\n", sys_siglist
[i
]);
1287 printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
1290 /* Save all of the information associated with the inferior<==>gdb
1291 connection. INF_STATUS is a pointer to a "struct inferior_status"
1292 (defined in inferior.h). */
1294 struct command_line
*get_breakpoint_commands ();
1297 save_inferior_status (inf_status
, restore_stack_info
)
1298 struct inferior_status
*inf_status
;
1299 int restore_stack_info
;
1301 inf_status
->pc_changed
= pc_changed
;
1302 inf_status
->stop_signal
= stop_signal
;
1303 inf_status
->stop_pc
= stop_pc
;
1304 inf_status
->stop_frame_address
= stop_frame_address
;
1305 inf_status
->stop_breakpoint
= stop_breakpoint
;
1306 inf_status
->stop_step
= stop_step
;
1307 inf_status
->stop_stack_dummy
= stop_stack_dummy
;
1308 inf_status
->stopped_by_random_signal
= stopped_by_random_signal
;
1309 inf_status
->trap_expected
= trap_expected
;
1310 inf_status
->step_range_start
= step_range_start
;
1311 inf_status
->step_range_end
= step_range_end
;
1312 inf_status
->step_frame_address
= step_frame_address
;
1313 inf_status
->step_over_calls
= step_over_calls
;
1314 inf_status
->step_resume_break_address
= step_resume_break_address
;
1315 inf_status
->stop_after_trap
= stop_after_trap
;
1316 inf_status
->stop_after_attach
= stop_after_attach
;
1317 inf_status
->breakpoint_commands
= get_breakpoint_commands ();
1318 inf_status
->restore_stack_info
= restore_stack_info
;
1320 bcopy (stop_registers
, inf_status
->stop_registers
, REGISTER_BYTES
);
1322 record_selected_frame (&(inf_status
->selected_frame_address
),
1323 &(inf_status
->selected_level
));
1328 restore_inferior_status (inf_status
)
1329 struct inferior_status
*inf_status
;
1332 int level
= inf_status
->selected_level
;
1334 pc_changed
= inf_status
->pc_changed
;
1335 stop_signal
= inf_status
->stop_signal
;
1336 stop_pc
= inf_status
->stop_pc
;
1337 stop_frame_address
= inf_status
->stop_frame_address
;
1338 stop_breakpoint
= inf_status
->stop_breakpoint
;
1339 stop_step
= inf_status
->stop_step
;
1340 stop_stack_dummy
= inf_status
->stop_stack_dummy
;
1341 stopped_by_random_signal
= inf_status
->stopped_by_random_signal
;
1342 trap_expected
= inf_status
->trap_expected
;
1343 step_range_start
= inf_status
->step_range_start
;
1344 step_range_end
= inf_status
->step_range_end
;
1345 step_frame_address
= inf_status
->step_frame_address
;
1346 step_over_calls
= inf_status
->step_over_calls
;
1347 step_resume_break_address
= inf_status
->step_resume_break_address
;
1348 stop_after_trap
= inf_status
->stop_after_trap
;
1349 stop_after_attach
= inf_status
->stop_after_attach
;
1350 set_breakpoint_commands (inf_status
->breakpoint_commands
);
1352 bcopy (inf_status
->stop_registers
, stop_registers
, REGISTER_BYTES
);
1354 /* The inferior can be gone if the user types "print exit(0)"
1355 (and perhaps other times). */
1356 if (have_inferior_p() && inf_status
->restore_stack_info
)
1358 fid
= find_relative_frame (get_current_frame (),
1362 FRAME_FP (fid
) != inf_status
->selected_frame_address
||
1365 /* I'm not sure this error message is a good idea. I have
1366 only seen it occur after "Can't continue previously
1367 requested operation" (we get called from do_cleanups), in
1368 which case it just adds insult to injury (one confusing
1369 error message after another. Besides which, does the
1370 user really care if we can't restore the previously
1372 fprintf (stderr
, "Unable to restore previously selected frame.\n");
1373 select_frame (get_current_frame (), 0);
1377 select_frame (fid
, inf_status
->selected_level
);
1384 _initialize_infrun ()
1388 add_info ("signals", signals_info
,
1389 "What debugger does when program gets various signals.\n\
1390 Specify a signal number as argument to print info on that signal only.");
1392 add_com ("handle", class_run
, handle_command
,
1393 "Specify how to handle a signal.\n\
1394 Args are signal number followed by flags.\n\
1395 Flags allowed are \"stop\", \"print\", \"pass\",\n\
1396 \"nostop\", \"noprint\" or \"nopass\".\n\
1397 Print means print a message if this signal happens.\n\
1398 Stop means reenter debugger if this signal happens (implies print).\n\
1399 Pass means let program see this signal; otherwise program doesn't know.\n\
1400 Pass and Stop may be combined.");
1402 for (i
= 0; i
< NSIG
; i
++)
1405 signal_print
[i
] = 1;
1406 signal_program
[i
] = 1;
1409 /* Signals caused by debugger's own actions
1410 should not be given to the program afterwards. */
1411 signal_program
[SIGTRAP
] = 0;
1412 signal_program
[SIGINT
] = 0;
1414 /* Signals that are not errors should not normally enter the debugger. */
1416 signal_stop
[SIGALRM
] = 0;
1417 signal_print
[SIGALRM
] = 0;
1418 #endif /* SIGALRM */
1420 signal_stop
[SIGVTALRM
] = 0;
1421 signal_print
[SIGVTALRM
] = 0;
1422 #endif /* SIGVTALRM */
1424 signal_stop
[SIGPROF
] = 0;
1425 signal_print
[SIGPROF
] = 0;
1426 #endif /* SIGPROF */
1428 signal_stop
[SIGCHLD
] = 0;
1429 signal_print
[SIGCHLD
] = 0;
1430 #endif /* SIGCHLD */
1432 signal_stop
[SIGCLD
] = 0;
1433 signal_print
[SIGCLD
] = 0;
1436 signal_stop
[SIGIO
] = 0;
1437 signal_print
[SIGIO
] = 0;
1440 signal_stop
[SIGURG
] = 0;
1441 signal_print
[SIGURG
] = 0;