1 /* Start (run) and stop the inferior process, for GDB.
2 Copyright (C) 1986, 1987, 1988, 1989, 1991 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 bug-gdb@prep.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).
125 #include "inferior.h"
126 #include "breakpoint.h"
131 #include "terminal.h" /* For #ifdef TIOCGPGRP and new_tty */
136 /* unistd.h is needed to #define X_OK */
140 #include <sys/file.h>
143 #ifdef SET_STACK_LIMIT_HUGE
144 #include <sys/time.h>
145 #include <sys/resource.h>
147 extern int original_stack_limit
;
148 #endif /* SET_STACK_LIMIT_HUGE */
150 extern char *getenv ();
152 extern struct target_ops child_ops
; /* In inftarg.c */
154 /* Copy of inferior_io_terminal when inferior was last started. */
156 extern char *inferior_thisrun_terminal
;
159 /* Sigtramp is a routine that the kernel calls (which then calls the
160 signal handler). On most machines it is a library routine that
161 is linked into the executable.
163 This macro, given a program counter value and the name of the
164 function in which that PC resides (which can be null if the
165 name is not known), returns nonzero if the PC and name show
166 that we are in sigtramp.
168 On most machines just see if the name is sigtramp (and if we have
169 no name, assume we are not in sigtramp). */
170 #if !defined (IN_SIGTRAMP)
171 #define IN_SIGTRAMP(pc, name) \
172 name && !strcmp ("_sigtramp", name)
175 /* Tables of how to react to signals; the user sets them. */
177 static char signal_stop
[NSIG
];
178 static char signal_print
[NSIG
];
179 static char signal_program
[NSIG
];
181 /* Nonzero if breakpoints are now inserted in the inferior. */
182 /* Nonstatic for initialization during xxx_create_inferior. FIXME. */
184 /*static*/ int breakpoints_inserted
;
186 /* Function inferior was in as of last step command. */
188 static struct symbol
*step_start_function
;
190 /* Nonzero => address for special breakpoint for resuming stepping. */
192 static CORE_ADDR step_resume_break_address
;
194 /* Pointer to orig contents of the byte where the special breakpoint is. */
196 static char step_resume_break_shadow
[BREAKPOINT_MAX
];
198 /* Nonzero means the special breakpoint is a duplicate
199 so it has not itself been inserted. */
201 static int step_resume_break_duplicate
;
203 /* Nonzero if we are expecting a trace trap and should proceed from it. */
205 static int trap_expected
;
207 /* Nonzero if the next time we try to continue the inferior, it will
208 step one instruction and generate a spurious trace trap.
209 This is used to compensate for a bug in HP-UX. */
211 static int trap_expected_after_continue
;
213 /* Nonzero means expecting a trace trap
214 and should stop the inferior and return silently when it happens. */
218 /* Nonzero means expecting a trap and caller will handle it themselves.
219 It is used after attach, due to attaching to a process;
220 when running in the shell before the child program has been exec'd;
221 and when running some kinds of remote stuff (FIXME?). */
223 int stop_soon_quietly
;
225 /* Nonzero if pc has been changed by the debugger
226 since the inferior stopped. */
230 /* Nonzero if proceed is being used for a "finish" command or a similar
231 situation when stop_registers should be saved. */
233 int proceed_to_finish
;
235 /* Save register contents here when about to pop a stack dummy frame,
236 if-and-only-if proceed_to_finish is set.
237 Thus this contains the return value from the called function (assuming
238 values are returned in a register). */
240 char stop_registers
[REGISTER_BYTES
];
242 /* Nonzero if program stopped due to error trying to insert breakpoints. */
244 static int breakpoints_failed
;
246 /* Nonzero after stop if current stack frame should be printed. */
248 static int stop_print_frame
;
250 #ifdef NO_SINGLE_STEP
251 extern int one_stepped
; /* From machine dependent code */
252 extern void single_step (); /* Same. */
253 #endif /* NO_SINGLE_STEP */
255 static void insert_step_breakpoint ();
256 static void remove_step_breakpoint ();
257 /*static*/ void wait_for_inferior ();
258 void init_wait_for_inferior ();
262 /* Things to clean up if we QUIT out of resume (). */
264 resume_cleanups (arg
)
270 /* Resume the inferior, but allow a QUIT. This is useful if the user
271 wants to interrupt some lengthy single-stepping operation
272 (for child processes, the SIGINT goes to the inferior, and so
273 we get a SIGINT random_signal, but for remote debugging and perhaps
274 other targets, that's not true).
276 STEP nonzero if we should step (zero to continue instead).
277 SIG is the signal to give the inferior (zero for none). */
283 struct cleanup
*old_cleanups
= make_cleanup (resume_cleanups
, 0);
285 target_resume (step
, sig
);
286 discard_cleanups (old_cleanups
);
290 /* Clear out all variables saying what to do when inferior is continued.
291 First do this, then set the ones you want, then call `proceed'. */
294 clear_proceed_status ()
297 step_range_start
= 0;
299 step_frame_address
= 0;
300 step_over_calls
= -1;
301 step_resume_break_address
= 0;
303 stop_soon_quietly
= 0;
304 proceed_to_finish
= 0;
305 breakpoint_proceeded
= 1; /* We're about to proceed... */
307 /* Discard any remaining commands or status from previous stop. */
308 bpstat_clear (&stop_bpstat
);
311 /* Basic routine for continuing the program in various fashions.
313 ADDR is the address to resume at, or -1 for resume where stopped.
314 SIGGNAL is the signal to give it, or 0 for none,
315 or -1 for act according to how it stopped.
316 STEP is nonzero if should trap after one instruction.
317 -1 means return after that and print nothing.
318 You should probably set various step_... variables
319 before calling here, if you are stepping.
321 You should call clear_proceed_status before calling proceed. */
324 proceed (addr
, siggnal
, step
)
332 step_start_function
= find_pc_function (read_pc ());
338 /* If there is a breakpoint at the address we will resume at,
339 step one instruction before inserting breakpoints
340 so that we do not stop right away. */
342 if (!pc_changed
&& breakpoint_here_p (read_pc ()))
347 write_register (PC_REGNUM
, addr
);
349 write_register (NPC_REGNUM
, addr
+ 4);
351 write_register (NNPC_REGNUM
, addr
+ 8);
356 if (trap_expected_after_continue
)
358 /* If (step == 0), a trap will be automatically generated after
359 the first instruction is executed. Force step one
360 instruction to clear this condition. This should not occur
361 if step is nonzero, but it is harmless in that case. */
363 trap_expected_after_continue
= 0;
367 /* We will get a trace trap after one instruction.
368 Continue it automatically and insert breakpoints then. */
372 int temp
= insert_breakpoints ();
375 print_sys_errmsg ("ptrace", temp
);
376 error ("Cannot insert breakpoints.\n\
377 The same program may be running in another process.");
379 breakpoints_inserted
= 1;
382 /* Install inferior's terminal modes. */
383 target_terminal_inferior ();
386 stop_signal
= siggnal
;
387 /* If this signal should not be seen by program,
388 give it zero. Used for debugging signals. */
389 else if (stop_signal
< NSIG
&& !signal_program
[stop_signal
])
392 /* Handle any optimized stores to the inferior NOW... */
393 #ifdef DO_DEFERRED_STORES
397 /* Resume inferior. */
398 resume (oneproc
|| step
|| bpstat_should_step (), stop_signal
);
400 /* Wait for it to stop (if not standalone)
401 and in any case decode why it stopped, and act accordingly. */
403 wait_for_inferior ();
408 /* This might be useful (not sure), but isn't currently used. See also
410 /* Writing the inferior pc as a register calls this function
411 to inform infrun that the pc has been set in the debugger. */
422 /* Record the pc and sp of the program the last time it stopped.
423 These are just used internally by wait_for_inferior, but need
424 to be preserved over calls to it and cleared when the inferior
426 static CORE_ADDR prev_pc
;
427 static CORE_ADDR prev_sp
;
428 static CORE_ADDR prev_func_start
;
429 static char *prev_func_name
;
432 /* Start an inferior Unix child process and sets inferior_pid to its pid.
433 EXEC_FILE is the file to run.
434 ALLARGS is a string containing the arguments to the program.
435 ENV is the environment vector to pass. Errors reported with error(). */
438 #define SHELL_FILE "/bin/sh"
442 child_create_inferior (exec_file
, allargs
, env
)
450 extern char *sys_errlist
[];
452 static char default_shell_file
[] = SHELL_FILE
;
455 /* Set debug_fork then attach to the child while it sleeps, to debug. */
456 static int debug_fork
= 0;
457 /* This is set to the result of setpgrp, which if vforked, will be visible
458 to you in the parent process. It's only used by humans for debugging. */
459 static int debug_setpgrp
= 657473;
461 /* The user might want tilde-expansion, and in general probably wants
462 the program to behave the same way as if run from
463 his/her favorite shell. So we let the shell run it for us.
464 FIXME, this should probably search the local environment (as
465 modified by the setenv command), not the env gdb inherited. */
466 shell_file
= getenv ("SHELL");
467 if (shell_file
== NULL
)
468 shell_file
= default_shell_file
;
470 len
= 5 + strlen (exec_file
) + 1 + strlen (allargs
) + 1 + /*slop*/ 10;
471 /* If desired, concat something onto the front of ALLARGS.
472 SHELL_COMMAND is the result. */
473 #ifdef SHELL_COMMAND_CONCAT
474 shell_command
= (char *) alloca (strlen (SHELL_COMMAND_CONCAT
) + len
);
475 strcpy (shell_command
, SHELL_COMMAND_CONCAT
);
477 shell_command
= (char *) alloca (len
);
478 shell_command
[0] = '\0';
480 strcat (shell_command
, "exec ");
481 strcat (shell_command
, exec_file
);
482 strcat (shell_command
, " ");
483 strcat (shell_command
, allargs
);
485 /* exec is said to fail if the executable is open. */
488 #if defined(USG) && !defined(HAVE_VFORK)
498 perror_with_name ("vfork");
506 /* Run inferior in a separate process group. */
507 debug_setpgrp
= setpgrp (getpid (), getpid ());
508 if (0 != debug_setpgrp
)
509 perror("setpgrp failed in child");
510 #endif /* TIOCGPGRP */
512 #ifdef SET_STACK_LIMIT_HUGE
513 /* Reset the stack limit back to what it was. */
517 getrlimit (RLIMIT_STACK
, &rlim
);
518 rlim
.rlim_cur
= original_stack_limit
;
519 setrlimit (RLIMIT_STACK
, &rlim
);
521 #endif /* SET_STACK_LIMIT_HUGE */
523 /* Tell the terminal handling subsystem what tty we plan to run on;
524 it will now switch to that one if non-null. */
526 new_tty (inferior_io_terminal
);
528 /* Changing the signal handlers for the inferior after
529 a vfork can also change them for the superior, so we don't mess
530 with signals here. See comments in
531 initialize_signals for how we get the right signal handlers
534 call_ptrace (0, 0, 0, 0); /* "Trace me, Dr. Memory!" */
535 execle (shell_file
, shell_file
, "-c", shell_command
, (char *)0, env
);
537 fprintf (stderr
, "Cannot exec %s: %s.\n", shell_file
,
538 errno
< sys_nerr
? sys_errlist
[errno
] : "unknown error");
543 /* Now that we have a child process, make it our target. */
544 push_target (&child_ops
);
546 #ifdef CREATE_INFERIOR_HOOK
547 CREATE_INFERIOR_HOOK (pid
);
550 /* The process was started by the fork that created it,
551 but it will have stopped one instruction after execing the shell.
552 Here we must get it up to actual execution of the real program. */
554 inferior_pid
= pid
; /* Needed for wait_for_inferior stuff below */
556 clear_proceed_status ();
558 #if defined (START_INFERIOR_HOOK)
559 START_INFERIOR_HOOK ();
562 /* We will get a trace trap after one instruction.
563 Continue it automatically. Eventually (after shell does an exec)
564 it will get another trace trap. Then insert breakpoints and continue. */
566 #ifdef START_INFERIOR_TRAPS_EXPECTED
567 pending_execs
= START_INFERIOR_TRAPS_EXPECTED
;
572 init_wait_for_inferior ();
574 /* Set up the "saved terminal modes" of the inferior
575 based on what modes we are starting it with. */
576 target_terminal_init ();
578 /* Install inferior's terminal modes. */
579 target_terminal_inferior ();
583 stop_soon_quietly
= 1; /* Make wait_for_inferior be quiet */
584 wait_for_inferior ();
585 if (stop_signal
!= SIGTRAP
)
587 /* Let shell child handle its own signals in its own way */
588 /* FIXME, what if child has exit()ed? Must exit loop somehow */
589 resume (0, stop_signal
);
593 /* We handle SIGTRAP, however; it means child did an exec. */
594 if (0 == --pending_execs
)
596 resume (0, 0); /* Just make it go on */
599 stop_soon_quietly
= 0;
601 /* Should this perhaps just be a "proceed" call? FIXME */
602 insert_step_breakpoint ();
603 breakpoints_failed
= insert_breakpoints ();
604 if (!breakpoints_failed
)
606 breakpoints_inserted
= 1;
607 target_terminal_inferior();
608 /* Start the child program going on its first instruction, single-
609 stepping if we need to. */
610 resume (bpstat_should_step (), 0);
611 wait_for_inferior ();
616 /* Start remote-debugging of a machine over a serial link. */
621 init_wait_for_inferior ();
622 clear_proceed_status ();
623 stop_soon_quietly
= 1;
625 wait_for_inferior ();
629 /* Initialize static vars when a new inferior begins. */
632 init_wait_for_inferior ()
634 /* These are meaningless until the first time through wait_for_inferior. */
638 prev_func_name
= NULL
;
640 trap_expected_after_continue
= 0;
641 breakpoints_inserted
= 0;
642 mark_breakpoints_out ();
643 stop_signal
= 0; /* Don't confuse first call to proceed(). */
647 /* Attach to process PID, then initialize for debugging it
648 and wait for the trace-trap that results from attaching. */
651 child_attach (args
, from_tty
)
661 error_no_arg ("process-id to attach");
663 #ifndef ATTACH_DETACH
664 error ("Can't attach to a process on this machine.");
668 if (target_has_execution
)
670 if (query ("A program is being debugged already. Kill it? "))
671 target_kill ((char *)0, from_tty
);
673 error ("Inferior not killed.");
676 exec_file
= (char *) get_exec_file (1);
680 printf ("Attaching program: %s pid %d\n",
687 push_target (&child_ops
);
689 mark_breakpoints_out ();
690 target_terminal_init ();
691 clear_proceed_status ();
692 stop_soon_quietly
= 1;
693 /*proceed (-1, 0, -2);*/
694 target_terminal_inferior ();
695 wait_for_inferior ();
697 #endif /* ATTACH_DETACH */
700 /* Wait for control to return from inferior to debugger.
701 If inferior gets a signal, we may decide to start it up again
702 instead of returning. That is why there is a loop in this function.
703 When this function actually returns it means the inferior
704 should be left stopped and GDB should read more commands. */
713 CORE_ADDR stop_func_start
;
714 char *stop_func_name
;
715 CORE_ADDR prologue_pc
;
716 int stop_step_resume_break
;
717 struct symtab_and_line sal
;
718 int remove_breakpoints_on_following_step
= 0;
721 /* This no longer works now that read_register is lazy;
722 it might try to ptrace when the process is not stopped. */
723 prev_pc
= read_pc ();
724 (void) find_pc_partial_function (prev_pc
, &prev_func_name
,
726 prev_func_start
+= FUNCTION_START_OFFSET
;
727 prev_sp
= read_register (SP_REGNUM
);
732 /* Clean up saved state that will become invalid. */
734 flush_cached_frames ();
735 registers_changed ();
739 /* See if the process still exists; clean up if it doesn't. */
742 target_terminal_ours (); /* Must do this before mourn anyway */
744 printf ("\nProgram exited with code 0%o.\n",
745 (unsigned int)WEXITSTATUS (w
));
748 printf ("\nProgram exited normally.\n");
750 target_mourn_inferior ();
751 #ifdef NO_SINGLE_STEP
754 stop_print_frame
= 0;
757 else if (!WIFSTOPPED (w
))
759 stop_print_frame
= 0;
760 stop_signal
= WTERMSIG (w
);
761 target_terminal_ours (); /* Must do this before mourn anyway */
762 target_kill ((char *)0, 0); /* kill mourns as well */
763 #ifdef PRINT_RANDOM_SIGNAL
764 printf ("\nProgram terminated: ");
765 PRINT_RANDOM_SIGNAL (stop_signal
);
767 printf ("\nProgram terminated with signal %d, %s\n",
770 ? sys_siglist
[stop_signal
]
773 printf ("The inferior process no longer exists.\n");
775 #ifdef NO_SINGLE_STEP
781 #ifdef NO_SINGLE_STEP
783 single_step (0); /* This actually cleans up the ss */
784 #endif /* NO_SINGLE_STEP */
786 stop_pc
= read_pc ();
787 set_current_frame ( create_new_frame (read_register (FP_REGNUM
),
790 stop_frame_address
= FRAME_FP (get_current_frame ());
791 stop_sp
= read_register (SP_REGNUM
);
794 /* Don't care about return value; stop_func_start and stop_func_name
795 will both be 0 if it doesn't work. */
796 (void) find_pc_partial_function (stop_pc
, &stop_func_name
,
798 stop_func_start
+= FUNCTION_START_OFFSET
;
800 bpstat_clear (&stop_bpstat
);
802 stop_stack_dummy
= 0;
803 stop_print_frame
= 1;
804 stop_step_resume_break
= 0;
806 stopped_by_random_signal
= 0;
807 breakpoints_failed
= 0;
809 /* Look at the cause of the stop, and decide what to do.
810 The alternatives are:
811 1) break; to really stop and return to the debugger,
812 2) drop through to start up again
813 (set another_trap to 1 to single step once)
814 3) set random_signal to 1, and the decision between 1 and 2
815 will be made according to the signal handling tables. */
817 stop_signal
= WSTOPSIG (w
);
819 /* First, distinguish signals caused by the debugger from signals
820 that have to do with the program's own actions.
821 Note that breakpoint insns may cause SIGTRAP or SIGILL
822 or SIGEMT, depending on the operating system version.
823 Here we detect when a SIGILL or SIGEMT is really a breakpoint
824 and change it to SIGTRAP. */
826 if (stop_signal
== SIGTRAP
827 || (breakpoints_inserted
&&
828 (stop_signal
== SIGILL
829 || stop_signal
== SIGEMT
))
830 || stop_soon_quietly
)
832 if (stop_signal
== SIGTRAP
&& stop_after_trap
)
834 stop_print_frame
= 0;
837 if (stop_soon_quietly
)
840 /* Don't even think about breakpoints
841 if just proceeded over a breakpoint.
843 However, if we are trying to proceed over a breakpoint
844 and end up in sigtramp, then step_resume_break_address
845 will be set and we should check whether we've hit the
847 if (stop_signal
== SIGTRAP
&& trap_expected
848 && step_resume_break_address
== NULL
)
849 bpstat_clear (&stop_bpstat
);
852 /* See if there is a breakpoint at the current PC. */
853 #if DECR_PC_AFTER_BREAK
854 /* Notice the case of stepping through a jump
855 that leads just after a breakpoint.
856 Don't confuse that with hitting the breakpoint.
857 What we check for is that 1) stepping is going on
858 and 2) the pc before the last insn does not match
859 the address of the breakpoint before the current pc. */
860 if (!(prev_pc
!= stop_pc
- DECR_PC_AFTER_BREAK
861 && step_range_end
&& !step_resume_break_address
))
862 #endif /* DECR_PC_AFTER_BREAK not zero */
864 /* See if we stopped at the special breakpoint for
865 stepping over a subroutine call. If both are zero,
866 this wasn't the reason for the stop. */
867 if (stop_pc
- DECR_PC_AFTER_BREAK
868 == step_resume_break_address
869 && step_resume_break_address
)
871 stop_step_resume_break
= 1;
872 if (DECR_PC_AFTER_BREAK
)
874 stop_pc
-= DECR_PC_AFTER_BREAK
;
875 write_register (PC_REGNUM
, stop_pc
);
882 bpstat_stop_status (&stop_pc
, stop_frame_address
);
883 /* Following in case break condition called a
885 stop_print_frame
= 1;
890 if (stop_signal
== SIGTRAP
)
892 = !(bpstat_explains_signal (stop_bpstat
)
894 || stop_step_resume_break
895 || PC_IN_CALL_DUMMY (stop_pc
, stop_sp
, stop_frame_address
)
896 || (step_range_end
&& !step_resume_break_address
));
900 = !(bpstat_explains_signal (stop_bpstat
)
901 || stop_step_resume_break
902 /* End of a stack dummy. Some systems (e.g. Sony
903 news) give another signal besides SIGTRAP,
904 so check here as well as above. */
905 || (stop_sp INNER_THAN stop_pc
906 && stop_pc INNER_THAN stop_frame_address
)
909 stop_signal
= SIGTRAP
;
915 /* For the program's own signals, act according to
916 the signal handling tables. */
920 /* Signal not for debugging purposes. */
923 stopped_by_random_signal
= 1;
925 if (stop_signal
>= NSIG
926 || signal_print
[stop_signal
])
929 target_terminal_ours_for_output ();
930 #ifdef PRINT_RANDOM_SIGNAL
931 PRINT_RANDOM_SIGNAL (stop_signal
);
933 printf ("\nProgram received signal %d, %s\n",
936 ? sys_siglist
[stop_signal
]
938 #endif /* PRINT_RANDOM_SIGNAL */
941 if (stop_signal
>= NSIG
942 || signal_stop
[stop_signal
])
944 /* If not going to stop, give terminal back
945 if we took it away. */
947 target_terminal_inferior ();
950 /* Handle cases caused by hitting a breakpoint. */
953 && (bpstat_explains_signal (stop_bpstat
) || stop_step_resume_break
))
955 /* Does a breakpoint want us to stop? */
956 if (bpstat_stop (stop_bpstat
))
958 stop_print_frame
= bpstat_should_print (stop_bpstat
);
961 /* But if we have hit the step-resumption breakpoint,
962 remove it. It has done its job getting us here.
963 The sp test is to make sure that we don't get hung
964 up in recursive calls in functions without frame
965 pointers. If the stack pointer isn't outside of
966 where the breakpoint was set (within a routine to be
967 stepped over), we're in the middle of a recursive
968 call. Not true for reg window machines (sparc)
969 because the must change frames to call things and
970 the stack pointer doesn't have to change if it
971 the bp was set in a routine without a frame (pc can
972 be stored in some other window).
974 The removal of the sp test is to allow calls to
975 alloca. Nasty things were happening. Oh, well,
976 gdb can only handle one level deep of lack of
978 if (stop_step_resume_break
979 && (step_frame_address
== 0
980 || (stop_frame_address
== step_frame_address
)))
982 remove_step_breakpoint ();
983 step_resume_break_address
= 0;
985 /* If were waiting for a trap, hitting the step_resume_break
986 doesn't count as getting it. */
990 /* Otherwise, must remove breakpoints and single-step
991 to get us past the one we hit. */
994 remove_breakpoints ();
995 remove_step_breakpoint ();
996 breakpoints_inserted
= 0;
1000 /* We come here if we hit a breakpoint but should not
1001 stop for it. Possibly we also were stepping
1002 and should stop for that. So fall through and
1003 test for stepping. But, if not stepping,
1007 /* If this is the breakpoint at the end of a stack dummy,
1008 just stop silently. */
1009 if (PC_IN_CALL_DUMMY (stop_pc
, stop_sp
, stop_frame_address
))
1011 stop_print_frame
= 0;
1012 stop_stack_dummy
= 1;
1014 trap_expected_after_continue
= 1;
1019 if (step_resume_break_address
)
1020 /* Having a step-resume breakpoint overrides anything
1021 else having to do with stepping commands until
1022 that breakpoint is reached. */
1024 /* If stepping through a line, keep going if still within it. */
1025 else if (!random_signal
1027 && stop_pc
>= step_range_start
1028 && stop_pc
< step_range_end
1029 /* The step range might include the start of the
1030 function, so if we are at the start of the
1031 step range and either the stack or frame pointers
1032 just changed, we've stepped outside */
1033 && !(stop_pc
== step_range_start
1034 && stop_frame_address
1035 && (stop_sp INNER_THAN prev_sp
1036 || stop_frame_address
!= step_frame_address
)))
1039 /* When "next"ing through a function,
1040 This causes an extra stop at the end.
1041 Is there any reason for this?
1042 It's confusing to the user. */
1043 /* Don't step through the return from a function
1044 unless that is the first instruction stepped through. */
1045 if (ABOUT_TO_RETURN (stop_pc
))
1053 /* We stepped out of the stepping range. See if that was due
1054 to a subroutine call that we should proceed to the end of. */
1055 else if (!random_signal
&& step_range_end
)
1057 if (stop_func_start
)
1059 prologue_pc
= stop_func_start
;
1060 SKIP_PROLOGUE (prologue_pc
);
1063 /* Did we just take a signal? */
1064 if (IN_SIGTRAMP (stop_pc
, stop_func_name
)
1065 && !IN_SIGTRAMP (prev_pc
, prev_func_name
))
1067 /* This code is needed at least in the following case:
1068 The user types "next" and then a signal arrives (before
1069 the "next" is done). */
1070 /* We've just taken a signal; go until we are back to
1071 the point where we took it and one more. */
1072 step_resume_break_address
= prev_pc
;
1073 step_resume_break_duplicate
=
1074 breakpoint_here_p (step_resume_break_address
);
1075 if (breakpoints_inserted
)
1076 insert_step_breakpoint ();
1077 /* Make sure that the stepping range gets us past
1078 that instruction. */
1079 if (step_range_end
== 1)
1080 step_range_end
= (step_range_start
= prev_pc
) + 1;
1081 remove_breakpoints_on_following_step
= 1;
1084 /* ==> See comments at top of file on this algorithm. <==*/
1086 else if (stop_pc
== stop_func_start
1087 && (stop_func_start
!= prev_func_start
1088 || prologue_pc
!= stop_func_start
1089 || stop_sp
!= prev_sp
))
1091 /* It's a subroutine call */
1092 if (step_over_calls
> 0
1093 || (step_over_calls
&& find_pc_function (stop_pc
) == 0))
1095 /* A subroutine call has happened. */
1096 /* Set a special breakpoint after the return */
1097 step_resume_break_address
=
1099 (SAVED_PC_AFTER_CALL (get_current_frame ()));
1100 step_resume_break_duplicate
1101 = breakpoint_here_p (step_resume_break_address
);
1102 if (breakpoints_inserted
)
1103 insert_step_breakpoint ();
1105 /* Subroutine call with source code we should not step over.
1106 Do step to the first line of code in it. */
1107 else if (step_over_calls
)
1109 SKIP_PROLOGUE (stop_func_start
);
1110 sal
= find_pc_line (stop_func_start
, 0);
1111 /* Use the step_resume_break to step until
1112 the end of the prologue, even if that involves jumps
1113 (as it seems to on the vax under 4.2). */
1114 /* If the prologue ends in the middle of a source line,
1115 continue to the end of that source line.
1116 Otherwise, just go to end of prologue. */
1117 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
1118 /* no, don't either. It skips any code that's
1119 legitimately on the first line. */
1121 if (sal
.end
&& sal
.pc
!= stop_func_start
)
1122 stop_func_start
= sal
.end
;
1125 if (stop_func_start
== stop_pc
)
1127 /* We are already there: stop now. */
1132 /* Put the step-breakpoint there and go until there. */
1134 step_resume_break_address
= stop_func_start
;
1136 step_resume_break_duplicate
1137 = breakpoint_here_p (step_resume_break_address
);
1138 if (breakpoints_inserted
)
1139 insert_step_breakpoint ();
1140 /* Do not specify what the fp should be when we stop
1141 since on some machines the prologue
1142 is where the new fp value is established. */
1143 step_frame_address
= 0;
1144 /* And make sure stepping stops right away then. */
1145 step_range_end
= step_range_start
;
1150 /* We get here only if step_over_calls is 0 and we
1151 just stepped into a subroutine. I presume
1152 that step_over_calls is only 0 when we're
1153 supposed to be stepping at the assembly
1159 /* No subroutince call; stop now. */
1167 else if (trap_expected
1168 && IN_SIGTRAMP (stop_pc
, stop_func_name
)
1169 && !IN_SIGTRAMP (prev_pc
, prev_func_name
))
1171 /* What has happened here is that we have just stepped the inferior
1172 with a signal (because it is a signal which shouldn't make
1173 us stop), thus stepping into sigtramp.
1175 So we need to set a step_resume_break_address breakpoint
1176 and continue until we hit it, and then step. */
1177 step_resume_break_address
= prev_pc
;
1178 /* Always 1, I think, but it's probably easier to have
1179 the step_resume_break as usual rather than trying to
1180 re-use the breakpoint which is already there. */
1181 step_resume_break_duplicate
=
1182 breakpoint_here_p (step_resume_break_address
);
1183 if (breakpoints_inserted
)
1184 insert_step_breakpoint ();
1185 remove_breakpoints_on_following_step
= 1;
1189 /* Save the pc before execution, to compare with pc after stop. */
1190 prev_pc
= read_pc (); /* Might have been DECR_AFTER_BREAK */
1191 prev_func_start
= stop_func_start
; /* Ok, since if DECR_PC_AFTER
1192 BREAK is defined, the
1193 original pc would not have
1194 been at the start of a
1196 prev_func_name
= stop_func_name
;
1199 /* If we did not do break;, it means we should keep
1200 running the inferior and not return to debugger. */
1202 if (trap_expected
&& stop_signal
!= SIGTRAP
)
1204 /* We took a signal (which we are supposed to pass through to
1205 the inferior, else we'd have done a break above) and we
1206 haven't yet gotten our trap. Simply continue. */
1207 resume ((step_range_end
&& !step_resume_break_address
)
1208 || (trap_expected
&& !step_resume_break_address
)
1209 || bpstat_should_step (),
1214 /* Either the trap was not expected, but we are continuing
1215 anyway (the user asked that this signal be passed to the
1218 The signal was SIGTRAP, e.g. it was our signal, but we
1219 decided we should resume from it.
1221 We're going to run this baby now!
1223 Insert breakpoints now, unless we are trying
1224 to one-proceed past a breakpoint. */
1225 /* If we've just finished a special step resume and we don't
1226 want to hit a breakpoint, pull em out. */
1227 if (!step_resume_break_address
&&
1228 remove_breakpoints_on_following_step
)
1230 remove_breakpoints_on_following_step
= 0;
1231 remove_breakpoints ();
1232 breakpoints_inserted
= 0;
1234 else if (!breakpoints_inserted
&&
1235 (step_resume_break_address
!= NULL
|| !another_trap
))
1237 insert_step_breakpoint ();
1238 breakpoints_failed
= insert_breakpoints ();
1239 if (breakpoints_failed
)
1241 breakpoints_inserted
= 1;
1244 trap_expected
= another_trap
;
1246 if (stop_signal
== SIGTRAP
)
1249 #ifdef SHIFT_INST_REGS
1250 /* I'm not sure when this following segment applies. I do know, now,
1251 that we shouldn't rewrite the regs when we were stopped by a
1252 random signal from the inferior process. */
1254 if (!stop_breakpoint
&& (stop_signal
!= SIGCLD
)
1255 && !stopped_by_random_signal
)
1257 CORE_ADDR pc_contents
= read_register (PC_REGNUM
);
1258 CORE_ADDR npc_contents
= read_register (NPC_REGNUM
);
1259 if (pc_contents
!= npc_contents
)
1261 write_register (NNPC_REGNUM
, npc_contents
);
1262 write_register (NPC_REGNUM
, pc_contents
);
1265 #endif /* SHIFT_INST_REGS */
1267 resume ((step_range_end
&& !step_resume_break_address
)
1268 || (trap_expected
&& !step_resume_break_address
)
1269 || bpstat_should_step (),
1273 if (target_has_execution
)
1275 /* Assuming the inferior still exists, set these up for next
1276 time, just like we did above if we didn't break out of the
1278 prev_pc
= read_pc ();
1279 prev_func_start
= stop_func_start
;
1280 prev_func_name
= stop_func_name
;
1285 /* Here to return control to GDB when the inferior stops for real.
1286 Print appropriate messages, remove breakpoints, give terminal our modes.
1288 STOP_PRINT_FRAME nonzero means print the executing frame
1289 (pc, function, args, file, line number and line text).
1290 BREAKPOINTS_FAILED nonzero means stop was due to error
1291 attempting to insert breakpoints. */
1296 /* Make sure that the current_frame's pc is correct. This
1297 is a correction for setting up the frame info before doing
1298 DECR_PC_AFTER_BREAK */
1299 if (target_has_execution
)
1300 (get_current_frame ())->pc
= read_pc ();
1302 if (breakpoints_failed
)
1304 target_terminal_ours_for_output ();
1305 print_sys_errmsg ("ptrace", breakpoints_failed
);
1306 printf ("Stopped; cannot insert breakpoints.\n\
1307 The same program may be running in another process.\n");
1310 if (target_has_execution
)
1311 remove_step_breakpoint ();
1313 if (target_has_execution
&& breakpoints_inserted
)
1314 if (remove_breakpoints ())
1316 target_terminal_ours_for_output ();
1317 printf ("Cannot remove breakpoints because program is no longer writable.\n\
1318 It might be running in another process.\n\
1319 Further execution is probably impossible.\n");
1322 breakpoints_inserted
= 0;
1324 /* Delete the breakpoint we stopped at, if it wants to be deleted.
1325 Delete any breakpoint that is to be deleted at the next stop. */
1327 breakpoint_auto_delete (stop_bpstat
);
1329 /* If an auto-display called a function and that got a signal,
1330 delete that auto-display to avoid an infinite recursion. */
1332 if (stopped_by_random_signal
)
1333 disable_current_display ();
1335 if (step_multi
&& stop_step
)
1338 target_terminal_ours ();
1340 if (!target_has_stack
)
1343 /* Select innermost stack frame except on return from a stack dummy routine,
1344 or if the program has exited. */
1345 if (!stop_stack_dummy
)
1347 select_frame (get_current_frame (), 0);
1349 if (stop_print_frame
)
1351 int source_only
= bpstat_print (stop_bpstat
);
1355 && step_frame_address
== stop_frame_address
1356 && step_start_function
== find_pc_function (stop_pc
)));
1358 /* Display the auto-display expressions. */
1363 /* Save the function value return registers, if we care.
1364 We might be about to restore their previous contents. */
1365 if (proceed_to_finish
)
1366 read_register_bytes (0, stop_registers
, REGISTER_BYTES
);
1368 if (stop_stack_dummy
)
1370 /* Pop the empty frame that contains the stack dummy.
1371 POP_FRAME ends with a setting of the current frame, so we
1372 can use that next. */
1374 select_frame (get_current_frame (), 0);
1379 insert_step_breakpoint ()
1381 if (step_resume_break_address
&& !step_resume_break_duplicate
)
1382 target_insert_breakpoint (step_resume_break_address
,
1383 step_resume_break_shadow
);
1387 remove_step_breakpoint ()
1389 if (step_resume_break_address
&& !step_resume_break_duplicate
)
1390 target_remove_breakpoint (step_resume_break_address
,
1391 step_resume_break_shadow
);
1397 printf_filtered ("Signal\t\tStop\tPrint\tPass to program\tDescription\n");
1401 sig_print_info (number
)
1404 char *abbrev
= sig_abbrev(number
);
1406 printf_filtered ("%d\t\t", number
);
1408 printf_filtered ("SIG%s (%d)\t", abbrev
, number
);
1409 printf_filtered ("%s\t", signal_stop
[number
] ? "Yes" : "No");
1410 printf_filtered ("%s\t", signal_print
[number
] ? "Yes" : "No");
1411 printf_filtered ("%s\t\t", signal_program
[number
] ? "Yes" : "No");
1412 printf_filtered ("%s\n", sys_siglist
[number
]);
1415 /* Specify how various signals in the inferior should be handled. */
1418 handle_command (args
, from_tty
)
1422 register char *p
= args
;
1424 register int digits
, wordlen
;
1428 error_no_arg ("signal to handle");
1432 /* Find the end of the next word in the args. */
1434 p
[wordlen
] && p
[wordlen
] != ' ' && p
[wordlen
] != '\t';
1436 /* Set nextarg to the start of the word after the one we just
1437 found, and null-terminate this one. */
1438 if (p
[wordlen
] == '\0')
1439 nextarg
= p
+ wordlen
;
1443 nextarg
= p
+ wordlen
+ 1;
1447 for (digits
= 0; p
[digits
] >= '0' && p
[digits
] <= '9'; digits
++);
1451 /* It is the first argument--must be the signal to operate on. */
1452 if (digits
== wordlen
)
1456 if (signum
<= 0 || signum
>= NSIG
)
1459 error ("Invalid signal %s given as argument to \"handle\".", p
);
1465 signum
= sig_number (p
);
1467 error ("No such signal \"%s\"", p
);
1470 if (signum
== SIGTRAP
|| signum
== SIGINT
)
1472 if (!query ("SIG%s is used by the debugger.\nAre you sure you want to change it? ", sig_abbrev (signum
)))
1473 error ("Not confirmed.");
1476 /* Else, if already got a signal number, look for flag words
1477 saying what to do for it. */
1478 else if (!strncmp (p
, "stop", wordlen
))
1480 signal_stop
[signum
] = 1;
1481 signal_print
[signum
] = 1;
1483 else if (wordlen
>= 2 && !strncmp (p
, "print", wordlen
))
1484 signal_print
[signum
] = 1;
1485 else if (wordlen
>= 2 && !strncmp (p
, "pass", wordlen
))
1486 signal_program
[signum
] = 1;
1487 else if (!strncmp (p
, "ignore", wordlen
))
1488 signal_program
[signum
] = 0;
1489 else if (wordlen
>= 3 && !strncmp (p
, "nostop", wordlen
))
1490 signal_stop
[signum
] = 0;
1491 else if (wordlen
>= 4 && !strncmp (p
, "noprint", wordlen
))
1493 signal_print
[signum
] = 0;
1494 signal_stop
[signum
] = 0;
1496 else if (wordlen
>= 4 && !strncmp (p
, "nopass", wordlen
))
1497 signal_program
[signum
] = 0;
1498 else if (wordlen
>= 3 && !strncmp (p
, "noignore", wordlen
))
1499 signal_program
[signum
] = 1;
1500 /* Not a number and not a recognized flag word => complain. */
1503 error ("Unrecognized flag word: \"%s\".", p
);
1506 /* Find start of next word. */
1508 while (*p
== ' ' || *p
== '\t') p
++;
1513 /* Show the results. */
1514 sig_print_header ();
1515 sig_print_info (signum
);
1519 /* Print current contents of the tables set by the handle command. */
1522 signals_info (signum_exp
)
1526 sig_print_header ();
1530 /* First see if this is a symbol name. */
1531 i
= sig_number (signum_exp
);
1534 /* Nope, maybe it's an address which evaluates to a signal
1536 i
= parse_and_eval_address (signum_exp
);
1537 if (i
>= NSIG
|| i
< 0)
1538 error ("Signal number out of bounds.");
1544 printf_filtered ("\n");
1545 for (i
= 0; i
< NSIG
; i
++)
1552 printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
1555 /* Save all of the information associated with the inferior<==>gdb
1556 connection. INF_STATUS is a pointer to a "struct inferior_status"
1557 (defined in inferior.h). */
1560 save_inferior_status (inf_status
, restore_stack_info
)
1561 struct inferior_status
*inf_status
;
1562 int restore_stack_info
;
1564 inf_status
->pc_changed
= pc_changed
;
1565 inf_status
->stop_signal
= stop_signal
;
1566 inf_status
->stop_pc
= stop_pc
;
1567 inf_status
->stop_frame_address
= stop_frame_address
;
1568 inf_status
->stop_step
= stop_step
;
1569 inf_status
->stop_stack_dummy
= stop_stack_dummy
;
1570 inf_status
->stopped_by_random_signal
= stopped_by_random_signal
;
1571 inf_status
->trap_expected
= trap_expected
;
1572 inf_status
->step_range_start
= step_range_start
;
1573 inf_status
->step_range_end
= step_range_end
;
1574 inf_status
->step_frame_address
= step_frame_address
;
1575 inf_status
->step_over_calls
= step_over_calls
;
1576 inf_status
->step_resume_break_address
= step_resume_break_address
;
1577 inf_status
->stop_after_trap
= stop_after_trap
;
1578 inf_status
->stop_soon_quietly
= stop_soon_quietly
;
1579 /* Save original bpstat chain here; replace it with copy of chain.
1580 If caller's caller is walking the chain, they'll be happier if we
1581 hand them back the original chain when restore_i_s is called. */
1582 inf_status
->stop_bpstat
= stop_bpstat
;
1583 stop_bpstat
= bpstat_copy (stop_bpstat
);
1584 inf_status
->breakpoint_proceeded
= breakpoint_proceeded
;
1585 inf_status
->restore_stack_info
= restore_stack_info
;
1586 inf_status
->proceed_to_finish
= proceed_to_finish
;
1588 bcopy (stop_registers
, inf_status
->stop_registers
, REGISTER_BYTES
);
1590 record_selected_frame (&(inf_status
->selected_frame_address
),
1591 &(inf_status
->selected_level
));
1596 restore_inferior_status (inf_status
)
1597 struct inferior_status
*inf_status
;
1600 int level
= inf_status
->selected_level
;
1602 pc_changed
= inf_status
->pc_changed
;
1603 stop_signal
= inf_status
->stop_signal
;
1604 stop_pc
= inf_status
->stop_pc
;
1605 stop_frame_address
= inf_status
->stop_frame_address
;
1606 stop_step
= inf_status
->stop_step
;
1607 stop_stack_dummy
= inf_status
->stop_stack_dummy
;
1608 stopped_by_random_signal
= inf_status
->stopped_by_random_signal
;
1609 trap_expected
= inf_status
->trap_expected
;
1610 step_range_start
= inf_status
->step_range_start
;
1611 step_range_end
= inf_status
->step_range_end
;
1612 step_frame_address
= inf_status
->step_frame_address
;
1613 step_over_calls
= inf_status
->step_over_calls
;
1614 step_resume_break_address
= inf_status
->step_resume_break_address
;
1615 stop_after_trap
= inf_status
->stop_after_trap
;
1616 stop_soon_quietly
= inf_status
->stop_soon_quietly
;
1617 bpstat_clear (&stop_bpstat
);
1618 stop_bpstat
= inf_status
->stop_bpstat
;
1619 breakpoint_proceeded
= inf_status
->breakpoint_proceeded
;
1620 proceed_to_finish
= inf_status
->proceed_to_finish
;
1622 bcopy (inf_status
->stop_registers
, stop_registers
, REGISTER_BYTES
);
1624 /* The inferior can be gone if the user types "print exit(0)"
1625 (and perhaps other times). */
1626 if (target_has_stack
&& inf_status
->restore_stack_info
)
1628 fid
= find_relative_frame (get_current_frame (),
1631 /* If inf_status->selected_frame_address is NULL, there was no
1632 previously selected frame. */
1634 FRAME_FP (fid
) != inf_status
->selected_frame_address
||
1638 /* I'm not sure this error message is a good idea. I have
1639 only seen it occur after "Can't continue previously
1640 requested operation" (we get called from do_cleanups), in
1641 which case it just adds insult to injury (one confusing
1642 error message after another. Besides which, does the
1643 user really care if we can't restore the previously
1645 fprintf (stderr
, "Unable to restore previously selected frame.\n");
1647 select_frame (get_current_frame (), 0);
1651 select_frame (fid
, inf_status
->selected_level
);
1657 _initialize_infrun ()
1661 add_info ("signals", signals_info
,
1662 "What debugger does when program gets various signals.\n\
1663 Specify a signal number as argument to print info on that signal only.");
1665 add_com ("handle", class_run
, handle_command
,
1666 "Specify how to handle a signal.\n\
1667 Args are signal number followed by flags.\n\
1668 Flags allowed are \"stop\", \"print\", \"pass\",\n\
1669 \"nostop\", \"noprint\" or \"nopass\".\n\
1670 Print means print a message if this signal happens.\n\
1671 Stop means reenter debugger if this signal happens (implies print).\n\
1672 Pass means let program see this signal; otherwise program doesn't know.\n\
1673 Pass and Stop may be combined.");
1675 for (i
= 0; i
< NSIG
; i
++)
1678 signal_print
[i
] = 1;
1679 signal_program
[i
] = 1;
1682 /* Signals caused by debugger's own actions
1683 should not be given to the program afterwards. */
1684 signal_program
[SIGTRAP
] = 0;
1685 signal_program
[SIGINT
] = 0;
1687 /* Signals that are not errors should not normally enter the debugger. */
1689 signal_stop
[SIGALRM
] = 0;
1690 signal_print
[SIGALRM
] = 0;
1691 #endif /* SIGALRM */
1693 signal_stop
[SIGVTALRM
] = 0;
1694 signal_print
[SIGVTALRM
] = 0;
1695 #endif /* SIGVTALRM */
1697 signal_stop
[SIGPROF
] = 0;
1698 signal_print
[SIGPROF
] = 0;
1699 #endif /* SIGPROF */
1701 signal_stop
[SIGCHLD
] = 0;
1702 signal_print
[SIGCHLD
] = 0;
1703 #endif /* SIGCHLD */
1705 signal_stop
[SIGCLD
] = 0;
1706 signal_print
[SIGCLD
] = 0;
1709 signal_stop
[SIGIO
] = 0;
1710 signal_print
[SIGIO
] = 0;
1713 signal_stop
[SIGURG
] = 0;
1714 signal_print
[SIGURG
] = 0;