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