Tue Mar 3 15:11:52 1992 Michael Tiemann (tiemann@cygnus.com)
[binutils-gdb.git] / gdb / infrun.c
1 /* Start (run) and stop the inferior process, for GDB.
2 Copyright 1986, 1987, 1988, 1989, 1991, 1992 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program 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 2 of the License, or
9 (at your option) any later version.
10
11 This program 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.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
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:
23
24 Current and previous (just before this step) pc.
25 Current and previous sp.
26 Current and previous start of current function.
27
28 If the starts of the functions don't match, then
29
30 a) We did a subroutine call.
31
32 In this case, the pc will be at the beginning of a function.
33
34 b) We did a subroutine return.
35
36 Otherwise.
37
38 c) We did a longjmp.
39
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.
49
50 Acts like a subroutine return. I can handle that with no problem
51 at all.
52
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.
57
58 -->If the beginnings of the current and previous function do match,
59 either:
60
61 a) We just did a recursive call.
62
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:
79
80 m68k: Call changes stack pointer. Regular jumps don't.
81
82 sparc: Recursive calls must have frames and therefor,
83 prologues.
84
85 vax: All calls have frames and hence change the
86 stack pointer.
87
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
93 lookout.
94
95 c) We did a jump within a function. We assume that this is
96 true if we didn't do a recursive call.
97
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.
102 */
103
104 #include "defs.h"
105 #include <string.h>
106 #include "symtab.h"
107 #include "frame.h"
108 #include "inferior.h"
109 #include "breakpoint.h"
110 #include "wait.h"
111 #include "gdbcore.h"
112 #include "signame.h"
113 #include "command.h"
114 #include "terminal.h" /* For #ifdef TIOCGPGRP and new_tty */
115 #include "target.h"
116
117 #include <signal.h>
118
119 /* unistd.h is needed to #define X_OK */
120 #ifdef USG
121 #include <unistd.h>
122 #else
123 #include <sys/file.h>
124 #endif
125
126 #ifdef SET_STACK_LIMIT_HUGE
127 #include <sys/time.h>
128 #include <sys/resource.h>
129
130 extern int original_stack_limit;
131 #endif /* SET_STACK_LIMIT_HUGE */
132
133 /* Prototypes for local functions */
134
135 static void
136 signals_info PARAMS ((char *));
137
138 static void
139 handle_command PARAMS ((char *, int));
140
141 static void
142 sig_print_info PARAMS ((int));
143
144 static void
145 sig_print_header PARAMS ((void));
146
147 static void
148 remove_step_breakpoint PARAMS ((void));
149
150 static void
151 insert_step_breakpoint PARAMS ((void));
152
153 static void
154 resume PARAMS ((int, int));
155
156 static void
157 resume_cleanups PARAMS ((int));
158
159 extern char **environ;
160
161 extern struct target_ops child_ops; /* In inftarg.c */
162
163 /* Sigtramp is a routine that the kernel calls (which then calls the
164 signal handler). On most machines it is a library routine that
165 is linked into the executable.
166
167 This macro, given a program counter value and the name of the
168 function in which that PC resides (which can be null if the
169 name is not known), returns nonzero if the PC and name show
170 that we are in sigtramp.
171
172 On most machines just see if the name is sigtramp (and if we have
173 no name, assume we are not in sigtramp). */
174 #if !defined (IN_SIGTRAMP)
175 #define IN_SIGTRAMP(pc, name) \
176 (name && !strcmp ("_sigtramp", name))
177 #endif
178
179 /* GET_LONGJMP_TARGET returns the PC at which longjmp() will resume the
180 program. It needs to examine the jmp_buf argument and extract the PC
181 from it. The return value is non-zero on success, zero otherwise. */
182 #ifndef GET_LONGJMP_TARGET
183 #define GET_LONGJMP_TARGET(PC_ADDR) 0
184 #endif
185
186
187 /* Some machines have trampoline code that sits between function callers
188 and the actual functions themselves. If this machine doesn't have
189 such things, disable their processing. */
190 #ifndef SKIP_TRAMPOLINE_CODE
191 #define SKIP_TRAMPOLINE_CODE(pc) 0
192 #endif
193
194
195 #ifdef TDESC
196 #include "tdesc.h"
197 int safe_to_init_tdesc_context = 0;
198 extern dc_dcontext_t current_context;
199 #endif
200
201 /* Tables of how to react to signals; the user sets them. */
202
203 static char signal_stop[NSIG];
204 static char signal_print[NSIG];
205 static char signal_program[NSIG];
206
207 /* Nonzero if breakpoints are now inserted in the inferior. */
208 /* Nonstatic for initialization during xxx_create_inferior. FIXME. */
209
210 /*static*/ int breakpoints_inserted;
211
212 /* Function inferior was in as of last step command. */
213
214 static struct symbol *step_start_function;
215
216 /* Nonzero => address for special breakpoint for resuming stepping. */
217
218 static CORE_ADDR step_resume_break_address;
219
220 /* Pointer to orig contents of the byte where the special breakpoint is. */
221
222 static char step_resume_break_shadow[BREAKPOINT_MAX];
223
224 /* Nonzero means the special breakpoint is a duplicate
225 so it has not itself been inserted. */
226
227 static int step_resume_break_duplicate;
228
229 /* Nonzero if we are expecting a trace trap and should proceed from it. */
230
231 static int trap_expected;
232
233 /* Nonzero if the next time we try to continue the inferior, it will
234 step one instruction and generate a spurious trace trap.
235 This is used to compensate for a bug in HP-UX. */
236
237 static int trap_expected_after_continue;
238
239 /* Nonzero means expecting a trace trap
240 and should stop the inferior and return silently when it happens. */
241
242 int stop_after_trap;
243
244 /* Nonzero means expecting a trap and caller will handle it themselves.
245 It is used after attach, due to attaching to a process;
246 when running in the shell before the child program has been exec'd;
247 and when running some kinds of remote stuff (FIXME?). */
248
249 int stop_soon_quietly;
250
251 /* Nonzero if pc has been changed by the debugger
252 since the inferior stopped. */
253
254 int pc_changed;
255
256 /* Nonzero if proceed is being used for a "finish" command or a similar
257 situation when stop_registers should be saved. */
258
259 int proceed_to_finish;
260
261 /* Save register contents here when about to pop a stack dummy frame,
262 if-and-only-if proceed_to_finish is set.
263 Thus this contains the return value from the called function (assuming
264 values are returned in a register). */
265
266 char stop_registers[REGISTER_BYTES];
267
268 /* Nonzero if program stopped due to error trying to insert breakpoints. */
269
270 static int breakpoints_failed;
271
272 /* Nonzero after stop if current stack frame should be printed. */
273
274 static int stop_print_frame;
275
276 #ifdef NO_SINGLE_STEP
277 extern int one_stepped; /* From machine dependent code */
278 extern void single_step (); /* Same. */
279 #endif /* NO_SINGLE_STEP */
280
281 \f
282 /* Things to clean up if we QUIT out of resume (). */
283 /* ARGSUSED */
284 static void
285 resume_cleanups (arg)
286 int arg;
287 {
288 normal_stop ();
289 }
290
291 /* Resume the inferior, but allow a QUIT. This is useful if the user
292 wants to interrupt some lengthy single-stepping operation
293 (for child processes, the SIGINT goes to the inferior, and so
294 we get a SIGINT random_signal, but for remote debugging and perhaps
295 other targets, that's not true).
296
297 STEP nonzero if we should step (zero to continue instead).
298 SIG is the signal to give the inferior (zero for none). */
299 static void
300 resume (step, sig)
301 int step;
302 int sig;
303 {
304 struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
305 QUIT;
306
307 #ifdef NO_SINGLE_STEP
308 if (step) {
309 single_step(sig); /* Do it the hard way, w/temp breakpoints */
310 step = 0; /* ...and don't ask hardware to do it. */
311 }
312 #endif
313
314 /* Handle any optimized stores to the inferior NOW... */
315 #ifdef DO_DEFERRED_STORES
316 DO_DEFERRED_STORES;
317 #endif
318
319 target_resume (step, sig);
320 discard_cleanups (old_cleanups);
321 }
322
323 \f
324 /* Clear out all variables saying what to do when inferior is continued.
325 First do this, then set the ones you want, then call `proceed'. */
326
327 void
328 clear_proceed_status ()
329 {
330 trap_expected = 0;
331 step_range_start = 0;
332 step_range_end = 0;
333 step_frame_address = 0;
334 step_over_calls = -1;
335 step_resume_break_address = 0;
336 stop_after_trap = 0;
337 stop_soon_quietly = 0;
338 proceed_to_finish = 0;
339 breakpoint_proceeded = 1; /* We're about to proceed... */
340
341 /* Discard any remaining commands or status from previous stop. */
342 bpstat_clear (&stop_bpstat);
343 }
344
345 /* Basic routine for continuing the program in various fashions.
346
347 ADDR is the address to resume at, or -1 for resume where stopped.
348 SIGGNAL is the signal to give it, or 0 for none,
349 or -1 for act according to how it stopped.
350 STEP is nonzero if should trap after one instruction.
351 -1 means return after that and print nothing.
352 You should probably set various step_... variables
353 before calling here, if you are stepping.
354
355 You should call clear_proceed_status before calling proceed. */
356
357 void
358 proceed (addr, siggnal, step)
359 CORE_ADDR addr;
360 int siggnal;
361 int step;
362 {
363 int oneproc = 0;
364
365 if (step > 0)
366 step_start_function = find_pc_function (read_pc ());
367 if (step < 0)
368 stop_after_trap = 1;
369
370 if (addr == (CORE_ADDR)-1)
371 {
372 /* If there is a breakpoint at the address we will resume at,
373 step one instruction before inserting breakpoints
374 so that we do not stop right away. */
375
376 if (!pc_changed && breakpoint_here_p (read_pc ()))
377 oneproc = 1;
378 }
379 else
380 {
381 write_register (PC_REGNUM, addr);
382 #ifdef NPC_REGNUM
383 write_register (NPC_REGNUM, addr + 4);
384 #ifdef NNPC_REGNUM
385 write_register (NNPC_REGNUM, addr + 8);
386 #endif
387 #endif
388 }
389
390 if (trap_expected_after_continue)
391 {
392 /* If (step == 0), a trap will be automatically generated after
393 the first instruction is executed. Force step one
394 instruction to clear this condition. This should not occur
395 if step is nonzero, but it is harmless in that case. */
396 oneproc = 1;
397 trap_expected_after_continue = 0;
398 }
399
400 if (oneproc)
401 /* We will get a trace trap after one instruction.
402 Continue it automatically and insert breakpoints then. */
403 trap_expected = 1;
404 else
405 {
406 int temp = insert_breakpoints ();
407 if (temp)
408 {
409 print_sys_errmsg ("ptrace", temp);
410 error ("Cannot insert breakpoints.\n\
411 The same program may be running in another process.");
412 }
413 breakpoints_inserted = 1;
414 }
415
416 /* Install inferior's terminal modes. */
417 target_terminal_inferior ();
418
419 if (siggnal >= 0)
420 stop_signal = siggnal;
421 /* If this signal should not be seen by program,
422 give it zero. Used for debugging signals. */
423 else if (stop_signal < NSIG && !signal_program[stop_signal])
424 stop_signal= 0;
425
426 /* Resume inferior. */
427 resume (oneproc || step || bpstat_should_step (), stop_signal);
428
429 /* Wait for it to stop (if not standalone)
430 and in any case decode why it stopped, and act accordingly. */
431
432 wait_for_inferior ();
433 normal_stop ();
434 }
435
436 /* Record the pc and sp of the program the last time it stopped.
437 These are just used internally by wait_for_inferior, but need
438 to be preserved over calls to it and cleared when the inferior
439 is started. */
440 static CORE_ADDR prev_pc;
441 static CORE_ADDR prev_sp;
442 static CORE_ADDR prev_func_start;
443 static char *prev_func_name;
444
445 \f
446 /* Start an inferior Unix child process and sets inferior_pid to its pid.
447 EXEC_FILE is the file to run.
448 ALLARGS is a string containing the arguments to the program.
449 ENV is the environment vector to pass. Errors reported with error(). */
450
451 #ifndef SHELL_FILE
452 #define SHELL_FILE "/bin/sh"
453 #endif
454
455 void
456 child_create_inferior (exec_file, allargs, env)
457 char *exec_file;
458 char *allargs;
459 char **env;
460 {
461 int pid;
462 char *shell_command;
463 extern int sys_nerr;
464 extern char *sys_errlist[];
465 char *shell_file;
466 static char default_shell_file[] = SHELL_FILE;
467 int len;
468 int pending_execs;
469 /* Set debug_fork then attach to the child while it sleeps, to debug. */
470 static int debug_fork = 0;
471 /* This is set to the result of setpgrp, which if vforked, will be visible
472 to you in the parent process. It's only used by humans for debugging. */
473 static int debug_setpgrp = 657473;
474 char **save_our_env;
475
476 /* The user might want tilde-expansion, and in general probably wants
477 the program to behave the same way as if run from
478 his/her favorite shell. So we let the shell run it for us.
479 FIXME, this should probably search the local environment (as
480 modified by the setenv command), not the env gdb inherited. */
481 shell_file = getenv ("SHELL");
482 if (shell_file == NULL)
483 shell_file = default_shell_file;
484
485 len = 5 + strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop*/ 10;
486 /* If desired, concat something onto the front of ALLARGS.
487 SHELL_COMMAND is the result. */
488 #ifdef SHELL_COMMAND_CONCAT
489 shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + len);
490 strcpy (shell_command, SHELL_COMMAND_CONCAT);
491 #else
492 shell_command = (char *) alloca (len);
493 shell_command[0] = '\0';
494 #endif
495 strcat (shell_command, "exec ");
496 strcat (shell_command, exec_file);
497 strcat (shell_command, " ");
498 strcat (shell_command, allargs);
499
500 /* exec is said to fail if the executable is open. */
501 close_exec_file ();
502
503 /* Retain a copy of our environment variables, since the child will
504 replace the value of environ and if we're vforked, we have to
505 restore it. */
506 save_our_env = environ;
507
508 /* Tell the terminal handling subsystem what tty we plan to run on;
509 it will just record the information for later. */
510
511 new_tty_prefork (inferior_io_terminal);
512
513 /* It is generally good practice to flush any possible pending stdio
514 output prior to doing a fork, to avoid the possibility of both the
515 parent and child flushing the same data after the fork. */
516
517 fflush (stdout);
518 fflush (stderr);
519
520 #if defined(USG) && !defined(HAVE_VFORK)
521 pid = fork ();
522 #else
523 if (debug_fork)
524 pid = fork ();
525 else
526 pid = vfork ();
527 #endif
528
529 if (pid < 0)
530 perror_with_name ("vfork");
531
532 if (pid == 0)
533 {
534 if (debug_fork)
535 sleep (debug_fork);
536
537 #ifdef TIOCGPGRP
538 /* Run inferior in a separate process group. */
539 #ifdef NEED_POSIX_SETPGID
540 debug_setpgrp = setpgid (0, 0);
541 #else
542 #if defined(USG) && !defined(SETPGRP_ARGS)
543 debug_setpgrp = setpgrp ();
544 #else
545 debug_setpgrp = setpgrp (getpid (), getpid ());
546 #endif /* USG */
547 #endif /* NEED_POSIX_SETPGID */
548 if (debug_setpgrp == -1)
549 perror("setpgrp failed in child");
550 #endif /* TIOCGPGRP */
551
552 #ifdef SET_STACK_LIMIT_HUGE
553 /* Reset the stack limit back to what it was. */
554 {
555 struct rlimit rlim;
556
557 getrlimit (RLIMIT_STACK, &rlim);
558 rlim.rlim_cur = original_stack_limit;
559 setrlimit (RLIMIT_STACK, &rlim);
560 }
561 #endif /* SET_STACK_LIMIT_HUGE */
562
563 /* Ask the tty subsystem to switch to the one we specified earlier
564 (or to share the current terminal, if none was specified). */
565
566 new_tty ();
567
568 /* Changing the signal handlers for the inferior after
569 a vfork can also change them for the superior, so we don't mess
570 with signals here. See comments in
571 initialize_signals for how we get the right signal handlers
572 for the inferior. */
573
574 #ifdef USE_PROC_FS
575 proc_set_exec_trap (); /* Use SVR4 /proc interface */
576 #else
577 call_ptrace (0, 0, 0, 0); /* "Trace me, Dr. Memory!" */
578 #endif
579
580 /* There is no execlpe call, so we have to set the environment
581 for our child in the global variable. If we've vforked, this
582 clobbers the parent, but environ is restored a few lines down
583 in the parent. By the way, yes we do need to look down the
584 path to find $SHELL. Rich Pixley says so, and I agree. */
585 environ = env;
586 execlp (shell_file, shell_file, "-c", shell_command, (char *)0);
587
588 fprintf (stderr, "Cannot exec %s: %s.\n", shell_file,
589 errno < sys_nerr ? sys_errlist[errno] : "unknown error");
590 fflush (stderr);
591 _exit (0177);
592 }
593
594 /* Restore our environment in case a vforked child clob'd it. */
595 environ = save_our_env;
596
597 /* Now that we have a child process, make it our target. */
598 push_target (&child_ops);
599
600 #ifdef CREATE_INFERIOR_HOOK
601 CREATE_INFERIOR_HOOK (pid);
602 #endif
603
604 /* The process was started by the fork that created it,
605 but it will have stopped one instruction after execing the shell.
606 Here we must get it up to actual execution of the real program. */
607
608 inferior_pid = pid; /* Needed for wait_for_inferior stuff below */
609
610 clear_proceed_status ();
611
612 /* We will get a trace trap after one instruction.
613 Continue it automatically. Eventually (after shell does an exec)
614 it will get another trace trap. Then insert breakpoints and continue. */
615
616 #ifdef START_INFERIOR_TRAPS_EXPECTED
617 pending_execs = START_INFERIOR_TRAPS_EXPECTED;
618 #else
619 pending_execs = 2;
620 #endif
621
622 init_wait_for_inferior ();
623
624 /* Set up the "saved terminal modes" of the inferior
625 based on what modes we are starting it with. */
626 target_terminal_init ();
627
628 /* Install inferior's terminal modes. */
629 target_terminal_inferior ();
630
631 while (1)
632 {
633 stop_soon_quietly = 1; /* Make wait_for_inferior be quiet */
634 wait_for_inferior ();
635 if (stop_signal != SIGTRAP)
636 {
637 /* Let shell child handle its own signals in its own way */
638 /* FIXME, what if child has exit()ed? Must exit loop somehow */
639 resume (0, stop_signal);
640 }
641 else
642 {
643 /* We handle SIGTRAP, however; it means child did an exec. */
644 if (0 == --pending_execs)
645 break;
646 resume (0, 0); /* Just make it go on */
647 }
648 }
649 stop_soon_quietly = 0;
650
651 /* We are now in the child process of interest, having exec'd the
652 correct program, and are poised at the first instruction of the
653 new program. */
654 #ifdef SOLIB_CREATE_INFERIOR_HOOK
655 SOLIB_CREATE_INFERIOR_HOOK ();
656 #endif
657
658 /* Should this perhaps just be a "proceed" call? FIXME */
659 insert_step_breakpoint ();
660 breakpoints_failed = insert_breakpoints ();
661 if (!breakpoints_failed)
662 {
663 breakpoints_inserted = 1;
664 target_terminal_inferior();
665 /* Start the child program going on its first instruction, single-
666 stepping if we need to. */
667 resume (bpstat_should_step (), 0);
668 wait_for_inferior ();
669 normal_stop ();
670 }
671 }
672
673 /* Start remote-debugging of a machine over a serial link. */
674
675 void
676 start_remote ()
677 {
678 init_wait_for_inferior ();
679 clear_proceed_status ();
680 stop_soon_quietly = 1;
681 trap_expected = 0;
682 wait_for_inferior ();
683 normal_stop ();
684 }
685
686 /* Initialize static vars when a new inferior begins. */
687
688 void
689 init_wait_for_inferior ()
690 {
691 /* These are meaningless until the first time through wait_for_inferior. */
692 prev_pc = 0;
693 prev_sp = 0;
694 prev_func_start = 0;
695 prev_func_name = NULL;
696
697 trap_expected_after_continue = 0;
698 breakpoints_inserted = 0;
699 mark_breakpoints_out ();
700 stop_signal = 0; /* Don't confuse first call to proceed(). */
701 }
702
703
704 /* Attach to process PID, then initialize for debugging it
705 and wait for the trace-trap that results from attaching. */
706
707 void
708 child_attach (args, from_tty)
709 char *args;
710 int from_tty;
711 {
712 char *exec_file;
713 int pid;
714
715 dont_repeat();
716
717 if (!args)
718 error_no_arg ("process-id to attach");
719
720 #ifndef ATTACH_DETACH
721 error ("Can't attach to a process on this machine.");
722 #else
723 pid = atoi (args);
724
725 if (target_has_execution)
726 {
727 if (query ("A program is being debugged already. Kill it? "))
728 target_kill ();
729 else
730 error ("Inferior not killed.");
731 }
732
733 exec_file = (char *) get_exec_file (1);
734
735 if (from_tty)
736 {
737 printf ("Attaching program: %s pid %d\n",
738 exec_file, pid);
739 fflush (stdout);
740 }
741
742 attach (pid);
743 inferior_pid = pid;
744 push_target (&child_ops);
745
746 mark_breakpoints_out ();
747 target_terminal_init ();
748 clear_proceed_status ();
749 stop_soon_quietly = 1;
750 /*proceed (-1, 0, -2);*/
751 target_terminal_inferior ();
752 wait_for_inferior ();
753 #ifdef SOLIB_ADD
754 SOLIB_ADD ((char *)0, from_tty, (struct target_ops *)0);
755 #endif
756 normal_stop ();
757 #endif /* ATTACH_DETACH */
758 }
759 \f
760 /* Wait for control to return from inferior to debugger.
761 If inferior gets a signal, we may decide to start it up again
762 instead of returning. That is why there is a loop in this function.
763 When this function actually returns it means the inferior
764 should be left stopped and GDB should read more commands. */
765
766 void
767 wait_for_inferior ()
768 {
769 WAITTYPE w;
770 int another_trap;
771 int random_signal;
772 CORE_ADDR stop_sp;
773 CORE_ADDR stop_func_start;
774 char *stop_func_name;
775 CORE_ADDR prologue_pc, tmp;
776 int stop_step_resume_break;
777 struct symtab_and_line sal;
778 int remove_breakpoints_on_following_step = 0;
779 int current_line;
780 int handling_longjmp = 0; /* FIXME */
781
782 sal = find_pc_line(prev_pc, 0);
783 current_line = sal.line;
784
785 while (1)
786 {
787 /* Clean up saved state that will become invalid. */
788 pc_changed = 0;
789 flush_cached_frames ();
790 registers_changed ();
791
792 target_wait (&w);
793
794 /* See if the process still exists; clean up if it doesn't. */
795 if (WIFEXITED (w))
796 {
797 target_terminal_ours (); /* Must do this before mourn anyway */
798 if (WEXITSTATUS (w))
799 printf ("\nProgram exited with code 0%o.\n",
800 (unsigned int)WEXITSTATUS (w));
801 else
802 if (!batch_mode())
803 printf ("\nProgram exited normally.\n");
804 fflush (stdout);
805 target_mourn_inferior ();
806 #ifdef NO_SINGLE_STEP
807 one_stepped = 0;
808 #endif
809 stop_print_frame = 0;
810 break;
811 }
812 else if (!WIFSTOPPED (w))
813 {
814 stop_print_frame = 0;
815 stop_signal = WTERMSIG (w);
816 target_terminal_ours (); /* Must do this before mourn anyway */
817 target_kill (); /* kill mourns as well */
818 #ifdef PRINT_RANDOM_SIGNAL
819 printf ("\nProgram terminated: ");
820 PRINT_RANDOM_SIGNAL (stop_signal);
821 #else
822 printf ("\nProgram terminated with signal %d, %s\n",
823 stop_signal,
824 stop_signal < NSIG
825 ? sys_siglist[stop_signal]
826 : "(undocumented)");
827 #endif
828 printf ("The inferior process no longer exists.\n");
829 fflush (stdout);
830 #ifdef NO_SINGLE_STEP
831 one_stepped = 0;
832 #endif
833 break;
834 }
835
836 #ifdef NO_SINGLE_STEP
837 if (one_stepped)
838 single_step (0); /* This actually cleans up the ss */
839 #endif /* NO_SINGLE_STEP */
840
841 stop_pc = read_pc ();
842 set_current_frame ( create_new_frame (read_register (FP_REGNUM),
843 read_pc ()));
844
845 stop_frame_address = FRAME_FP (get_current_frame ());
846 stop_sp = read_register (SP_REGNUM);
847 stop_func_start = 0;
848 stop_func_name = 0;
849 /* Don't care about return value; stop_func_start and stop_func_name
850 will both be 0 if it doesn't work. */
851 (void) find_pc_partial_function (stop_pc, &stop_func_name,
852 &stop_func_start);
853 stop_func_start += FUNCTION_START_OFFSET;
854 another_trap = 0;
855 bpstat_clear (&stop_bpstat);
856 stop_step = 0;
857 stop_stack_dummy = 0;
858 stop_print_frame = 1;
859 stop_step_resume_break = 0;
860 random_signal = 0;
861 stopped_by_random_signal = 0;
862 breakpoints_failed = 0;
863
864 /* Look at the cause of the stop, and decide what to do.
865 The alternatives are:
866 1) break; to really stop and return to the debugger,
867 2) drop through to start up again
868 (set another_trap to 1 to single step once)
869 3) set random_signal to 1, and the decision between 1 and 2
870 will be made according to the signal handling tables. */
871
872 stop_signal = WSTOPSIG (w);
873
874 /* First, distinguish signals caused by the debugger from signals
875 that have to do with the program's own actions.
876 Note that breakpoint insns may cause SIGTRAP or SIGILL
877 or SIGEMT, depending on the operating system version.
878 Here we detect when a SIGILL or SIGEMT is really a breakpoint
879 and change it to SIGTRAP. */
880
881 if (stop_signal == SIGTRAP
882 || (breakpoints_inserted &&
883 (stop_signal == SIGILL
884 || stop_signal == SIGEMT))
885 || stop_soon_quietly)
886 {
887 if (stop_signal == SIGTRAP && stop_after_trap)
888 {
889 stop_print_frame = 0;
890 break;
891 }
892 if (stop_soon_quietly)
893 break;
894
895 /* Don't even think about breakpoints
896 if just proceeded over a breakpoint.
897
898 However, if we are trying to proceed over a breakpoint
899 and end up in sigtramp, then step_resume_break_address
900 will be set and we should check whether we've hit the
901 step breakpoint. */
902 if (stop_signal == SIGTRAP && trap_expected
903 && step_resume_break_address == 0)
904 bpstat_clear (&stop_bpstat);
905 else
906 {
907 /* See if there is a breakpoint at the current PC. */
908 #if DECR_PC_AFTER_BREAK
909 /* Notice the case of stepping through a jump
910 that lands just after a breakpoint.
911 Don't confuse that with hitting the breakpoint.
912 What we check for is that 1) stepping is going on
913 and 2) the pc before the last insn does not match
914 the address of the breakpoint before the current pc. */
915 if (prev_pc == stop_pc - DECR_PC_AFTER_BREAK
916 || !step_range_end
917 || step_resume_break_address
918 || handling_longjmp /* FIXME */)
919 #endif /* DECR_PC_AFTER_BREAK not zero */
920 {
921 /* See if we stopped at the special breakpoint for
922 stepping over a subroutine call. If both are zero,
923 this wasn't the reason for the stop. */
924 if (step_resume_break_address
925 && stop_pc - DECR_PC_AFTER_BREAK
926 == step_resume_break_address)
927 {
928 stop_step_resume_break = 1;
929 if (DECR_PC_AFTER_BREAK)
930 {
931 stop_pc -= DECR_PC_AFTER_BREAK;
932 write_register (PC_REGNUM, stop_pc);
933 pc_changed = 0;
934 }
935 }
936 else
937 {
938 stop_bpstat =
939 bpstat_stop_status (&stop_pc, stop_frame_address);
940 /* Following in case break condition called a
941 function. */
942 stop_print_frame = 1;
943 }
944 }
945 }
946
947 if (stop_signal == SIGTRAP)
948 random_signal
949 = !(bpstat_explains_signal (stop_bpstat)
950 || trap_expected
951 || stop_step_resume_break
952 || PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address)
953 || (step_range_end && !step_resume_break_address));
954 else
955 {
956 random_signal
957 = !(bpstat_explains_signal (stop_bpstat)
958 || stop_step_resume_break
959 /* End of a stack dummy. Some systems (e.g. Sony
960 news) give another signal besides SIGTRAP,
961 so check here as well as above. */
962 || PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address)
963 );
964 if (!random_signal)
965 stop_signal = SIGTRAP;
966 }
967 }
968 else
969 random_signal = 1;
970
971 /* For the program's own signals, act according to
972 the signal handling tables. */
973
974 if (random_signal)
975 {
976 /* Signal not for debugging purposes. */
977 int printed = 0;
978
979 stopped_by_random_signal = 1;
980
981 if (stop_signal >= NSIG
982 || signal_print[stop_signal])
983 {
984 printed = 1;
985 target_terminal_ours_for_output ();
986 #ifdef PRINT_RANDOM_SIGNAL
987 PRINT_RANDOM_SIGNAL (stop_signal);
988 #else
989 printf ("\nProgram received signal %d, %s\n",
990 stop_signal,
991 stop_signal < NSIG
992 ? sys_siglist[stop_signal]
993 : "(undocumented)");
994 #endif /* PRINT_RANDOM_SIGNAL */
995 fflush (stdout);
996 }
997 if (stop_signal >= NSIG
998 || signal_stop[stop_signal])
999 break;
1000 /* If not going to stop, give terminal back
1001 if we took it away. */
1002 else if (printed)
1003 target_terminal_inferior ();
1004
1005 /* Note that virtually all the code below does `if !random_signal'.
1006 Perhaps this code should end with a goto or continue. At least
1007 one (now fixed) bug was caused by this -- a !random_signal was
1008 missing in one of the tests below. */
1009 }
1010
1011 /* Handle cases caused by hitting a breakpoint. */
1012
1013 if (!random_signal)
1014 if (bpstat_explains_signal (stop_bpstat))
1015 {
1016 CORE_ADDR jmp_buf_pc;
1017
1018 switch (stop_bpstat->breakpoint_at->type) /* FIXME */
1019 {
1020 /* If we hit the breakpoint at longjmp, disable it for the
1021 duration of this command. Then, install a temporary
1022 breakpoint at the target of the jmp_buf. */
1023 case bp_longjmp:
1024 disable_longjmp_breakpoint();
1025 remove_breakpoints ();
1026 breakpoints_inserted = 0;
1027 if (!GET_LONGJMP_TARGET(&jmp_buf_pc)) goto keep_going;
1028
1029 /* Need to blow away step-resume breakpoint, as it
1030 interferes with us */
1031 remove_step_breakpoint ();
1032 step_resume_break_address = 0;
1033 stop_step_resume_break = 0;
1034
1035 #if 0 /* FIXME - Need to implement nested temporary breakpoints */
1036 if (step_over_calls > 0)
1037 set_longjmp_resume_breakpoint(jmp_buf_pc,
1038 get_current_frame());
1039 else
1040 #endif /* 0 */
1041 set_longjmp_resume_breakpoint(jmp_buf_pc, NULL);
1042 handling_longjmp = 1; /* FIXME */
1043 goto keep_going;
1044
1045 case bp_longjmp_resume:
1046 remove_breakpoints ();
1047 breakpoints_inserted = 0;
1048 #if 0 /* FIXME - Need to implement nested temporary breakpoints */
1049 if (step_over_calls
1050 && (stop_frame_address
1051 INNER_THAN step_frame_address))
1052 {
1053 another_trap = 1;
1054 goto keep_going;
1055 }
1056 #endif /* 0 */
1057 disable_longjmp_breakpoint();
1058 handling_longjmp = 0; /* FIXME */
1059 break;
1060
1061 default:
1062 fprintf(stderr, "Unknown breakpoint type %d\n",
1063 stop_bpstat->breakpoint_at->type);
1064 case bp_watchpoint:
1065 case bp_breakpoint:
1066 case bp_until:
1067 case bp_finish:
1068 /* Does a breakpoint want us to stop? */
1069 if (bpstat_stop (stop_bpstat))
1070 {
1071 stop_print_frame = bpstat_should_print (stop_bpstat);
1072 goto stop_stepping;
1073 }
1074 /* Otherwise, must remove breakpoints and single-step
1075 to get us past the one we hit. */
1076 else
1077 {
1078 remove_breakpoints ();
1079 remove_step_breakpoint ();
1080 breakpoints_inserted = 0;
1081 another_trap = 1;
1082 }
1083 break;
1084 }
1085 }
1086 else if (stop_step_resume_break)
1087 {
1088 /* But if we have hit the step-resumption breakpoint,
1089 remove it. It has done its job getting us here.
1090 The sp test is to make sure that we don't get hung
1091 up in recursive calls in functions without frame
1092 pointers. If the stack pointer isn't outside of
1093 where the breakpoint was set (within a routine to be
1094 stepped over), we're in the middle of a recursive
1095 call. Not true for reg window machines (sparc)
1096 because the must change frames to call things and
1097 the stack pointer doesn't have to change if it
1098 the bp was set in a routine without a frame (pc can
1099 be stored in some other window).
1100
1101 The removal of the sp test is to allow calls to
1102 alloca. Nasty things were happening. Oh, well,
1103 gdb can only handle one level deep of lack of
1104 frame pointer. */
1105
1106 /*
1107 Disable test for step_frame_address match so that we always stop even if the
1108 frames don't match. Reason: if we hit the step_resume_breakpoint, there is
1109 no way to temporarily disable it so that we can step past it. If we leave
1110 the breakpoint in, then we loop forever repeatedly hitting, but never
1111 getting past the breakpoint. This change keeps nexting over recursive
1112 function calls from hanging gdb.
1113 */
1114 #if 0
1115 if (* step_frame_address == 0
1116 || (step_frame_address == stop_frame_address))
1117 #endif 0
1118 {
1119 remove_step_breakpoint ();
1120 step_resume_break_address = 0;
1121
1122 /* If were waiting for a trap, hitting the step_resume_break
1123 doesn't count as getting it. */
1124 if (trap_expected)
1125 another_trap = 1;
1126 }
1127 }
1128
1129 /* We come here if we hit a breakpoint but should not
1130 stop for it. Possibly we also were stepping
1131 and should stop for that. So fall through and
1132 test for stepping. But, if not stepping,
1133 do not stop. */
1134
1135 /* If this is the breakpoint at the end of a stack dummy,
1136 just stop silently. */
1137 if (!random_signal
1138 && PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address))
1139 {
1140 stop_print_frame = 0;
1141 stop_stack_dummy = 1;
1142 #ifdef HP_OS_BUG
1143 trap_expected_after_continue = 1;
1144 #endif
1145 break;
1146 }
1147
1148 if (step_resume_break_address)
1149 /* Having a step-resume breakpoint overrides anything
1150 else having to do with stepping commands until
1151 that breakpoint is reached. */
1152 ;
1153 /* If stepping through a line, keep going if still within it. */
1154 else if (!random_signal
1155 && step_range_end
1156 && stop_pc >= step_range_start
1157 && stop_pc < step_range_end
1158 /* The step range might include the start of the
1159 function, so if we are at the start of the
1160 step range and either the stack or frame pointers
1161 just changed, we've stepped outside */
1162 && !(stop_pc == step_range_start
1163 && stop_frame_address
1164 && (stop_sp INNER_THAN prev_sp
1165 || stop_frame_address != step_frame_address)))
1166 {
1167 ;
1168 }
1169
1170 /* We stepped out of the stepping range. See if that was due
1171 to a subroutine call that we should proceed to the end of. */
1172 else if (!random_signal && step_range_end)
1173 {
1174 if (stop_func_start)
1175 {
1176 prologue_pc = stop_func_start;
1177 SKIP_PROLOGUE (prologue_pc);
1178 }
1179
1180 /* Did we just take a signal? */
1181 if (IN_SIGTRAMP (stop_pc, stop_func_name)
1182 && !IN_SIGTRAMP (prev_pc, prev_func_name))
1183 {
1184 /* This code is needed at least in the following case:
1185 The user types "next" and then a signal arrives (before
1186 the "next" is done). */
1187 /* We've just taken a signal; go until we are back to
1188 the point where we took it and one more. */
1189 step_resume_break_address = prev_pc;
1190 step_resume_break_duplicate =
1191 breakpoint_here_p (step_resume_break_address);
1192 if (breakpoints_inserted)
1193 insert_step_breakpoint ();
1194 /* Make sure that the stepping range gets us past
1195 that instruction. */
1196 if (step_range_end == 1)
1197 step_range_end = (step_range_start = prev_pc) + 1;
1198 remove_breakpoints_on_following_step = 1;
1199 goto save_pc;
1200 }
1201
1202 /* ==> See comments at top of file on this algorithm. <==*/
1203
1204 if (stop_pc == stop_func_start
1205 && (stop_func_start != prev_func_start
1206 || prologue_pc != stop_func_start
1207 || stop_sp != prev_sp))
1208 {
1209 /* It's a subroutine call.
1210 (0) If we are not stepping over any calls ("stepi"), we
1211 just stop.
1212 (1) If we're doing a "next", we want to continue through
1213 the call ("step over the call").
1214 (2) If we are in a function-call trampoline (a stub between
1215 the calling routine and the real function), locate
1216 the real function and change stop_func_start.
1217 (3) If we're doing a "step", and there are no debug symbols
1218 at the target of the call, we want to continue through
1219 it ("step over the call").
1220 (4) Otherwise, we want to stop soon, after the function
1221 prologue ("step into the call"). */
1222
1223 if (step_over_calls == 0)
1224 {
1225 /* I presume that step_over_calls is only 0 when we're
1226 supposed to be stepping at the assembly language level. */
1227 stop_step = 1;
1228 break;
1229 }
1230
1231 if (step_over_calls > 0)
1232 goto step_over_function;
1233
1234 tmp = SKIP_TRAMPOLINE_CODE (stop_pc);
1235 if (tmp != NULL)
1236 stop_func_start = tmp;
1237
1238 if (find_pc_function (stop_func_start) != 0)
1239 goto step_into_function;
1240
1241 step_over_function:
1242 /* A subroutine call has happened. */
1243 /* Set a special breakpoint after the return */
1244 step_resume_break_address =
1245 ADDR_BITS_REMOVE
1246 (SAVED_PC_AFTER_CALL (get_current_frame ()));
1247 step_resume_break_duplicate
1248 = breakpoint_here_p (step_resume_break_address);
1249 if (breakpoints_inserted)
1250 insert_step_breakpoint ();
1251 goto save_pc;
1252
1253 step_into_function:
1254 /* Subroutine call with source code we should not step over.
1255 Do step to the first line of code in it. */
1256 SKIP_PROLOGUE (stop_func_start);
1257 sal = find_pc_line (stop_func_start, 0);
1258 /* Use the step_resume_break to step until
1259 the end of the prologue, even if that involves jumps
1260 (as it seems to on the vax under 4.2). */
1261 /* If the prologue ends in the middle of a source line,
1262 continue to the end of that source line.
1263 Otherwise, just go to end of prologue. */
1264 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
1265 /* no, don't either. It skips any code that's
1266 legitimately on the first line. */
1267 #else
1268 if (sal.end && sal.pc != stop_func_start)
1269 stop_func_start = sal.end;
1270 #endif
1271
1272 if (stop_func_start == stop_pc)
1273 {
1274 /* We are already there: stop now. */
1275 stop_step = 1;
1276 break;
1277 }
1278 else
1279 /* Put the step-breakpoint there and go until there. */
1280 {
1281 step_resume_break_address = stop_func_start;
1282
1283 step_resume_break_duplicate
1284 = breakpoint_here_p (step_resume_break_address);
1285 if (breakpoints_inserted)
1286 insert_step_breakpoint ();
1287 /* Do not specify what the fp should be when we stop
1288 since on some machines the prologue
1289 is where the new fp value is established. */
1290 step_frame_address = 0;
1291 /* And make sure stepping stops right away then. */
1292 step_range_end = step_range_start;
1293 }
1294 goto save_pc;
1295 }
1296
1297 /* We've wandered out of the step range (but haven't done a
1298 subroutine call or return). */
1299
1300 sal = find_pc_line(stop_pc, 0);
1301
1302 if (step_range_end == 1 || /* stepi or nexti */
1303 sal.line == 0 || /* ...or no line # info */
1304 (stop_pc == sal.pc /* ...or we're at the start */
1305 && current_line != sal.line)) { /* of a different line */
1306 /* Stop because we're done stepping. */
1307 stop_step = 1;
1308 break;
1309 } else {
1310 /* We aren't done stepping, and we have line number info for $pc.
1311 Optimize by setting the step_range for the line.
1312 (We might not be in the original line, but if we entered a
1313 new line in mid-statement, we continue stepping. This makes
1314 things like for(;;) statements work better.) */
1315 step_range_start = sal.pc;
1316 step_range_end = sal.end;
1317 goto save_pc;
1318 }
1319 abort(); /* We never fall through here */
1320 }
1321
1322 if (trap_expected
1323 && IN_SIGTRAMP (stop_pc, stop_func_name)
1324 && !IN_SIGTRAMP (prev_pc, prev_func_name))
1325 {
1326 /* What has happened here is that we have just stepped the inferior
1327 with a signal (because it is a signal which shouldn't make
1328 us stop), thus stepping into sigtramp.
1329
1330 So we need to set a step_resume_break_address breakpoint
1331 and continue until we hit it, and then step. */
1332 step_resume_break_address = prev_pc;
1333 /* Always 1, I think, but it's probably easier to have
1334 the step_resume_break as usual rather than trying to
1335 re-use the breakpoint which is already there. */
1336 step_resume_break_duplicate =
1337 breakpoint_here_p (step_resume_break_address);
1338 if (breakpoints_inserted)
1339 insert_step_breakpoint ();
1340 remove_breakpoints_on_following_step = 1;
1341 another_trap = 1;
1342 }
1343
1344 /* My apologies to the gods of structured programming. */
1345 /* Come to this label when you need to resume the inferior. It's really much
1346 cleaner at this time to do a goto than to try and figure out what the
1347 if-else chain ought to look like!! */
1348
1349 keep_going:
1350
1351 save_pc:
1352 /* Save the pc before execution, to compare with pc after stop. */
1353 prev_pc = read_pc (); /* Might have been DECR_AFTER_BREAK */
1354 prev_func_start = stop_func_start; /* Ok, since if DECR_PC_AFTER
1355 BREAK is defined, the
1356 original pc would not have
1357 been at the start of a
1358 function. */
1359 prev_func_name = stop_func_name;
1360 prev_sp = stop_sp;
1361
1362 /* If we did not do break;, it means we should keep
1363 running the inferior and not return to debugger. */
1364
1365 if (trap_expected && stop_signal != SIGTRAP)
1366 {
1367 /* We took a signal (which we are supposed to pass through to
1368 the inferior, else we'd have done a break above) and we
1369 haven't yet gotten our trap. Simply continue. */
1370 resume ((step_range_end && !step_resume_break_address)
1371 || (trap_expected && !step_resume_break_address)
1372 || bpstat_should_step (),
1373 stop_signal);
1374 }
1375 else
1376 {
1377 /* Either the trap was not expected, but we are continuing
1378 anyway (the user asked that this signal be passed to the
1379 child)
1380 -- or --
1381 The signal was SIGTRAP, e.g. it was our signal, but we
1382 decided we should resume from it.
1383
1384 We're going to run this baby now!
1385
1386 Insert breakpoints now, unless we are trying
1387 to one-proceed past a breakpoint. */
1388 /* If we've just finished a special step resume and we don't
1389 want to hit a breakpoint, pull em out. */
1390 if (!step_resume_break_address &&
1391 remove_breakpoints_on_following_step)
1392 {
1393 remove_breakpoints_on_following_step = 0;
1394 remove_breakpoints ();
1395 breakpoints_inserted = 0;
1396 }
1397 else if (!breakpoints_inserted &&
1398 (step_resume_break_address != 0 || !another_trap))
1399 {
1400 insert_step_breakpoint ();
1401 breakpoints_failed = insert_breakpoints ();
1402 if (breakpoints_failed)
1403 break;
1404 breakpoints_inserted = 1;
1405 }
1406
1407 trap_expected = another_trap;
1408
1409 if (stop_signal == SIGTRAP)
1410 stop_signal = 0;
1411
1412 #ifdef SHIFT_INST_REGS
1413 /* I'm not sure when this following segment applies. I do know, now,
1414 that we shouldn't rewrite the regs when we were stopped by a
1415 random signal from the inferior process. */
1416
1417 if (!bpstat_explains_signal (stop_bpstat)
1418 && (stop_signal != SIGCLD)
1419 && !stopped_by_random_signal)
1420 {
1421 CORE_ADDR pc_contents = read_register (PC_REGNUM);
1422 CORE_ADDR npc_contents = read_register (NPC_REGNUM);
1423 if (pc_contents != npc_contents)
1424 {
1425 write_register (NNPC_REGNUM, npc_contents);
1426 write_register (NPC_REGNUM, pc_contents);
1427 }
1428 }
1429 #endif /* SHIFT_INST_REGS */
1430
1431 resume ((!step_resume_break_address
1432 && !handling_longjmp
1433 && (step_range_end
1434 || trap_expected))
1435 || bpstat_should_step (),
1436 stop_signal);
1437 }
1438 }
1439
1440 stop_stepping:
1441 if (target_has_execution)
1442 {
1443 /* Assuming the inferior still exists, set these up for next
1444 time, just like we did above if we didn't break out of the
1445 loop. */
1446 prev_pc = read_pc ();
1447 prev_func_start = stop_func_start;
1448 prev_func_name = stop_func_name;
1449 prev_sp = stop_sp;
1450 }
1451 }
1452 \f
1453 /* Here to return control to GDB when the inferior stops for real.
1454 Print appropriate messages, remove breakpoints, give terminal our modes.
1455
1456 STOP_PRINT_FRAME nonzero means print the executing frame
1457 (pc, function, args, file, line number and line text).
1458 BREAKPOINTS_FAILED nonzero means stop was due to error
1459 attempting to insert breakpoints. */
1460
1461 void
1462 normal_stop ()
1463 {
1464 /* Make sure that the current_frame's pc is correct. This
1465 is a correction for setting up the frame info before doing
1466 DECR_PC_AFTER_BREAK */
1467 if (target_has_execution)
1468 (get_current_frame ())->pc = read_pc ();
1469
1470 if (breakpoints_failed)
1471 {
1472 target_terminal_ours_for_output ();
1473 print_sys_errmsg ("ptrace", breakpoints_failed);
1474 printf ("Stopped; cannot insert breakpoints.\n\
1475 The same program may be running in another process.\n");
1476 }
1477
1478 if (target_has_execution)
1479 remove_step_breakpoint ();
1480
1481 if (target_has_execution && breakpoints_inserted)
1482 if (remove_breakpoints ())
1483 {
1484 target_terminal_ours_for_output ();
1485 printf ("Cannot remove breakpoints because program is no longer writable.\n\
1486 It might be running in another process.\n\
1487 Further execution is probably impossible.\n");
1488 }
1489
1490 breakpoints_inserted = 0;
1491
1492 /* Delete the breakpoint we stopped at, if it wants to be deleted.
1493 Delete any breakpoint that is to be deleted at the next stop. */
1494
1495 breakpoint_auto_delete (stop_bpstat);
1496
1497 /* If an auto-display called a function and that got a signal,
1498 delete that auto-display to avoid an infinite recursion. */
1499
1500 if (stopped_by_random_signal)
1501 disable_current_display ();
1502
1503 if (step_multi && stop_step)
1504 return;
1505
1506 target_terminal_ours ();
1507
1508 if (!target_has_stack)
1509 return;
1510
1511 /* Select innermost stack frame except on return from a stack dummy routine,
1512 or if the program has exited. Print it without a level number if
1513 we have changed functions or hit a breakpoint. Print source line
1514 if we have one. */
1515 if (!stop_stack_dummy)
1516 {
1517 select_frame (get_current_frame (), 0);
1518
1519 if (stop_print_frame)
1520 {
1521 int source_only;
1522
1523 source_only = bpstat_print (stop_bpstat);
1524 source_only = source_only ||
1525 ( stop_step
1526 && step_frame_address == stop_frame_address
1527 && step_start_function == find_pc_function (stop_pc));
1528
1529 print_stack_frame (selected_frame, -1, source_only? -1: 1);
1530
1531 /* Display the auto-display expressions. */
1532 do_displays ();
1533 }
1534 }
1535
1536 /* Save the function value return registers, if we care.
1537 We might be about to restore their previous contents. */
1538 if (proceed_to_finish)
1539 read_register_bytes (0, stop_registers, REGISTER_BYTES);
1540
1541 if (stop_stack_dummy)
1542 {
1543 /* Pop the empty frame that contains the stack dummy.
1544 POP_FRAME ends with a setting of the current frame, so we
1545 can use that next. */
1546 POP_FRAME;
1547 select_frame (get_current_frame (), 0);
1548 }
1549 }
1550 \f
1551 static void
1552 insert_step_breakpoint ()
1553 {
1554 if (step_resume_break_address && !step_resume_break_duplicate)
1555 target_insert_breakpoint (step_resume_break_address,
1556 step_resume_break_shadow);
1557 }
1558
1559 static void
1560 remove_step_breakpoint ()
1561 {
1562 if (step_resume_break_address && !step_resume_break_duplicate)
1563 target_remove_breakpoint (step_resume_break_address,
1564 step_resume_break_shadow);
1565 }
1566 \f
1567 static void
1568 sig_print_header ()
1569 {
1570 printf_filtered ("Signal\t\tStop\tPrint\tPass to program\tDescription\n");
1571 }
1572
1573 static void
1574 sig_print_info (number)
1575 int number;
1576 {
1577 char *abbrev = sig_abbrev(number);
1578 if (abbrev == NULL)
1579 printf_filtered ("%d\t\t", number);
1580 else
1581 printf_filtered ("SIG%s (%d)\t", abbrev, number);
1582 printf_filtered ("%s\t", signal_stop[number] ? "Yes" : "No");
1583 printf_filtered ("%s\t", signal_print[number] ? "Yes" : "No");
1584 printf_filtered ("%s\t\t", signal_program[number] ? "Yes" : "No");
1585 printf_filtered ("%s\n", sys_siglist[number]);
1586 }
1587
1588 /* Specify how various signals in the inferior should be handled. */
1589
1590 static void
1591 handle_command (args, from_tty)
1592 char *args;
1593 int from_tty;
1594 {
1595 register char *p = args;
1596 int signum = 0;
1597 register int digits, wordlen;
1598 char *nextarg;
1599
1600 if (!args)
1601 error_no_arg ("signal to handle");
1602
1603 while (*p)
1604 {
1605 /* Find the end of the next word in the args. */
1606 for (wordlen = 0;
1607 p[wordlen] && p[wordlen] != ' ' && p[wordlen] != '\t';
1608 wordlen++);
1609 /* Set nextarg to the start of the word after the one we just
1610 found, and null-terminate this one. */
1611 if (p[wordlen] == '\0')
1612 nextarg = p + wordlen;
1613 else
1614 {
1615 p[wordlen] = '\0';
1616 nextarg = p + wordlen + 1;
1617 }
1618
1619
1620 for (digits = 0; p[digits] >= '0' && p[digits] <= '9'; digits++);
1621
1622 if (signum == 0)
1623 {
1624 /* It is the first argument--must be the signal to operate on. */
1625 if (digits == wordlen)
1626 {
1627 /* Numeric. */
1628 signum = atoi (p);
1629 if (signum <= 0 || signum >= NSIG)
1630 {
1631 p[wordlen] = '\0';
1632 error ("Invalid signal %s given as argument to \"handle\".", p);
1633 }
1634 }
1635 else
1636 {
1637 /* Symbolic. */
1638 signum = sig_number (p);
1639 if (signum == -1)
1640 error ("No such signal \"%s\"", p);
1641 }
1642
1643 if (signum == SIGTRAP || signum == SIGINT)
1644 {
1645 if (!query ("SIG%s is used by the debugger.\nAre you sure you want to change it? ", sig_abbrev (signum)))
1646 error ("Not confirmed.");
1647 }
1648 }
1649 /* Else, if already got a signal number, look for flag words
1650 saying what to do for it. */
1651 else if (!strncmp (p, "stop", wordlen))
1652 {
1653 signal_stop[signum] = 1;
1654 signal_print[signum] = 1;
1655 }
1656 else if (wordlen >= 2 && !strncmp (p, "print", wordlen))
1657 signal_print[signum] = 1;
1658 else if (wordlen >= 2 && !strncmp (p, "pass", wordlen))
1659 signal_program[signum] = 1;
1660 else if (!strncmp (p, "ignore", wordlen))
1661 signal_program[signum] = 0;
1662 else if (wordlen >= 3 && !strncmp (p, "nostop", wordlen))
1663 signal_stop[signum] = 0;
1664 else if (wordlen >= 4 && !strncmp (p, "noprint", wordlen))
1665 {
1666 signal_print[signum] = 0;
1667 signal_stop[signum] = 0;
1668 }
1669 else if (wordlen >= 4 && !strncmp (p, "nopass", wordlen))
1670 signal_program[signum] = 0;
1671 else if (wordlen >= 3 && !strncmp (p, "noignore", wordlen))
1672 signal_program[signum] = 1;
1673 /* Not a number and not a recognized flag word => complain. */
1674 else
1675 {
1676 error ("Unrecognized flag word: \"%s\".", p);
1677 }
1678
1679 /* Find start of next word. */
1680 p = nextarg;
1681 while (*p == ' ' || *p == '\t') p++;
1682 }
1683
1684 if (from_tty)
1685 {
1686 /* Show the results. */
1687 sig_print_header ();
1688 sig_print_info (signum);
1689 }
1690 }
1691
1692 /* Print current contents of the tables set by the handle command. */
1693
1694 static void
1695 signals_info (signum_exp)
1696 char *signum_exp;
1697 {
1698 register int i;
1699 sig_print_header ();
1700
1701 if (signum_exp)
1702 {
1703 /* First see if this is a symbol name. */
1704 i = sig_number (signum_exp);
1705 if (i == -1)
1706 {
1707 /* Nope, maybe it's an address which evaluates to a signal
1708 number. */
1709 i = parse_and_eval_address (signum_exp);
1710 if (i >= NSIG || i < 0)
1711 error ("Signal number out of bounds.");
1712 }
1713 sig_print_info (i);
1714 return;
1715 }
1716
1717 printf_filtered ("\n");
1718 for (i = 0; i < NSIG; i++)
1719 {
1720 QUIT;
1721
1722 sig_print_info (i);
1723 }
1724
1725 printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
1726 }
1727 \f
1728 /* Save all of the information associated with the inferior<==>gdb
1729 connection. INF_STATUS is a pointer to a "struct inferior_status"
1730 (defined in inferior.h). */
1731
1732 void
1733 save_inferior_status (inf_status, restore_stack_info)
1734 struct inferior_status *inf_status;
1735 int restore_stack_info;
1736 {
1737 inf_status->pc_changed = pc_changed;
1738 inf_status->stop_signal = stop_signal;
1739 inf_status->stop_pc = stop_pc;
1740 inf_status->stop_frame_address = stop_frame_address;
1741 inf_status->stop_step = stop_step;
1742 inf_status->stop_stack_dummy = stop_stack_dummy;
1743 inf_status->stopped_by_random_signal = stopped_by_random_signal;
1744 inf_status->trap_expected = trap_expected;
1745 inf_status->step_range_start = step_range_start;
1746 inf_status->step_range_end = step_range_end;
1747 inf_status->step_frame_address = step_frame_address;
1748 inf_status->step_over_calls = step_over_calls;
1749 inf_status->step_resume_break_address = step_resume_break_address;
1750 inf_status->stop_after_trap = stop_after_trap;
1751 inf_status->stop_soon_quietly = stop_soon_quietly;
1752 /* Save original bpstat chain here; replace it with copy of chain.
1753 If caller's caller is walking the chain, they'll be happier if we
1754 hand them back the original chain when restore_i_s is called. */
1755 inf_status->stop_bpstat = stop_bpstat;
1756 stop_bpstat = bpstat_copy (stop_bpstat);
1757 inf_status->breakpoint_proceeded = breakpoint_proceeded;
1758 inf_status->restore_stack_info = restore_stack_info;
1759 inf_status->proceed_to_finish = proceed_to_finish;
1760
1761 bcopy (stop_registers, inf_status->stop_registers, REGISTER_BYTES);
1762
1763 record_selected_frame (&(inf_status->selected_frame_address),
1764 &(inf_status->selected_level));
1765 return;
1766 }
1767
1768 void
1769 restore_inferior_status (inf_status)
1770 struct inferior_status *inf_status;
1771 {
1772 FRAME fid;
1773 int level = inf_status->selected_level;
1774
1775 pc_changed = inf_status->pc_changed;
1776 stop_signal = inf_status->stop_signal;
1777 stop_pc = inf_status->stop_pc;
1778 stop_frame_address = inf_status->stop_frame_address;
1779 stop_step = inf_status->stop_step;
1780 stop_stack_dummy = inf_status->stop_stack_dummy;
1781 stopped_by_random_signal = inf_status->stopped_by_random_signal;
1782 trap_expected = inf_status->trap_expected;
1783 step_range_start = inf_status->step_range_start;
1784 step_range_end = inf_status->step_range_end;
1785 step_frame_address = inf_status->step_frame_address;
1786 step_over_calls = inf_status->step_over_calls;
1787 step_resume_break_address = inf_status->step_resume_break_address;
1788 stop_after_trap = inf_status->stop_after_trap;
1789 stop_soon_quietly = inf_status->stop_soon_quietly;
1790 bpstat_clear (&stop_bpstat);
1791 stop_bpstat = inf_status->stop_bpstat;
1792 breakpoint_proceeded = inf_status->breakpoint_proceeded;
1793 proceed_to_finish = inf_status->proceed_to_finish;
1794
1795 bcopy (inf_status->stop_registers, stop_registers, REGISTER_BYTES);
1796
1797 /* The inferior can be gone if the user types "print exit(0)"
1798 (and perhaps other times). */
1799 if (target_has_stack && inf_status->restore_stack_info)
1800 {
1801 fid = find_relative_frame (get_current_frame (),
1802 &level);
1803
1804 /* If inf_status->selected_frame_address is NULL, there was no
1805 previously selected frame. */
1806 if (fid == 0 ||
1807 FRAME_FP (fid) != inf_status->selected_frame_address ||
1808 level != 0)
1809 {
1810 #if 1
1811 /* I'm not sure this error message is a good idea. I have
1812 only seen it occur after "Can't continue previously
1813 requested operation" (we get called from do_cleanups), in
1814 which case it just adds insult to injury (one confusing
1815 error message after another. Besides which, does the
1816 user really care if we can't restore the previously
1817 selected frame? */
1818 fprintf (stderr, "Unable to restore previously selected frame.\n");
1819 #endif
1820 select_frame (get_current_frame (), 0);
1821 return;
1822 }
1823
1824 select_frame (fid, inf_status->selected_level);
1825 }
1826 }
1827
1828 \f
1829 void
1830 _initialize_infrun ()
1831 {
1832 register int i;
1833
1834 add_info ("signals", signals_info,
1835 "What debugger does when program gets various signals.\n\
1836 Specify a signal number as argument to print info on that signal only.");
1837
1838 add_com ("handle", class_run, handle_command,
1839 "Specify how to handle a signal.\n\
1840 Args are signal number followed by flags.\n\
1841 Flags allowed are \"stop\", \"print\", \"pass\",\n\
1842 \"nostop\", \"noprint\" or \"nopass\".\n\
1843 Print means print a message if this signal happens.\n\
1844 Stop means reenter debugger if this signal happens (implies print).\n\
1845 Pass means let program see this signal; otherwise program doesn't know.\n\
1846 Pass and Stop may be combined.");
1847
1848 for (i = 0; i < NSIG; i++)
1849 {
1850 signal_stop[i] = 1;
1851 signal_print[i] = 1;
1852 signal_program[i] = 1;
1853 }
1854
1855 /* Signals caused by debugger's own actions
1856 should not be given to the program afterwards. */
1857 signal_program[SIGTRAP] = 0;
1858 signal_program[SIGINT] = 0;
1859
1860 /* Signals that are not errors should not normally enter the debugger. */
1861 #ifdef SIGALRM
1862 signal_stop[SIGALRM] = 0;
1863 signal_print[SIGALRM] = 0;
1864 #endif /* SIGALRM */
1865 #ifdef SIGVTALRM
1866 signal_stop[SIGVTALRM] = 0;
1867 signal_print[SIGVTALRM] = 0;
1868 #endif /* SIGVTALRM */
1869 #ifdef SIGPROF
1870 signal_stop[SIGPROF] = 0;
1871 signal_print[SIGPROF] = 0;
1872 #endif /* SIGPROF */
1873 #ifdef SIGCHLD
1874 signal_stop[SIGCHLD] = 0;
1875 signal_print[SIGCHLD] = 0;
1876 #endif /* SIGCHLD */
1877 #ifdef SIGCLD
1878 signal_stop[SIGCLD] = 0;
1879 signal_print[SIGCLD] = 0;
1880 #endif /* SIGCLD */
1881 #ifdef SIGIO
1882 signal_stop[SIGIO] = 0;
1883 signal_print[SIGIO] = 0;
1884 #endif /* SIGIO */
1885 #ifdef SIGURG
1886 signal_stop[SIGURG] = 0;
1887 signal_print[SIGURG] = 0;
1888 #endif /* SIGURG */
1889 }