BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.bin / gdb / infrun.c
CommitLineData
af359dea
C
1/*-
2 * This code is derived from software copyrighted by the Free Software
3 * Foundation.
4 *
5 * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
6 * Modified 1990 by Van Jacobson at Lawrence Berkeley Laboratory.
7 */
8
9#ifndef lint
10static char sccsid[] = "@(#)infrun.c 6.4 (Berkeley) 5/8/91";
11#endif /* not lint */
12
13/* Start and stop the inferior process, for GDB.
14 Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
15
16This file is part of GDB.
17
18GDB is free software; you can redistribute it and/or modify
19it under the terms of the GNU General Public License as published by
20the Free Software Foundation; either version 1, or (at your option)
21any later version.
22
23GDB is distributed in the hope that it will be useful,
24but WITHOUT ANY WARRANTY; without even the implied warranty of
25MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26GNU General Public License for more details.
27
28You should have received a copy of the GNU General Public License
29along with GDB; see the file COPYING. If not, write to
30the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
31
32/* Notes on the algorithm used in wait_for_inferior to determine if we
33 just did a subroutine call when stepping. We have the following
34 information at that point:
35
36 Current and previous (just before this step) pc.
37 Current and previous sp.
38 Current and previous start of current function.
39
40 If the start's of the functions don't match, then
41
42 a) We did a subroutine call.
43
44 In this case, the pc will be at the beginning of a function.
45
46 b) We did a subroutine return.
47
48 Otherwise.
49
50 c) We did a longjmp.
51
52 If we did a longjump, we were doing "nexti", since a next would
53 have attempted to skip over the assembly language routine in which
54 the longjmp is coded and would have simply been the equivalent of a
55 continue. I consider this ok behaivior. We'd like one of two
56 things to happen if we are doing a nexti through the longjmp()
57 routine: 1) It behaves as a stepi, or 2) It acts like a continue as
58 above. Given that this is a special case, and that anybody who
59 thinks that the concept of sub calls is meaningful in the context
60 of a longjmp, I'll take either one. Let's see what happens.
61
62 Acts like a subroutine return. I can handle that with no problem
63 at all.
64
65 -->So: If the current and previous beginnings of the current
66 function don't match, *and* the pc is at the start of a function,
67 we've done a subroutine call. If the pc is not at the start of a
68 function, we *didn't* do a subroutine call.
69
70 -->If the beginnings of the current and previous function do match,
71 either:
72
73 a) We just did a recursive call.
74
75 In this case, we would be at the very beginning of a
76 function and 1) it will have a prologue (don't jump to
77 before prologue, or 2) (we assume here that it doesn't have
78 a prologue) there will have been a change in the stack
79 pointer over the last instruction. (Ie. it's got to put
80 the saved pc somewhere. The stack is the usual place. In
81 a recursive call a register is only an option if there's a
82 prologue to do something with it. This is even true on
83 register window machines; the prologue sets up the new
84 window. It might not be true on a register window machine
85 where the call instruction moved the register window
86 itself. Hmmm. One would hope that the stack pointer would
87 also change. If it doesn't, somebody send me a note, and
88 I'll work out a more general theory.
89 randy@wheaties.ai.mit.edu). This is true (albeit slipperly
90 so) on all machines I'm aware of:
91
92 m68k: Call changes stack pointer. Regular jumps don't.
93
94 sparc: Recursive calls must have frames and therefor,
95 prologues.
96
97 vax: All calls have frames and hence change the
98 stack pointer.
99
100 b) We did a return from a recursive call. I don't see that we
101 have either the ability or the need to distinguish this
102 from an ordinary jump. The stack frame will be printed
103 when and if the frame pointer changes; if we are in a
104 function without a frame pointer, it's the users own
105 lookout.
106
107 c) We did a jump within a function. We assume that this is
108 true if we didn't do a recursive call.
109
110 d) We are in no-man's land ("I see no symbols here"). We
111 don't worry about this; it will make calls look like simple
112 jumps (and the stack frames will be printed when the frame
113 pointer moves), which is a reasonably non-violent response.
114
115#if 0
116 We skip this; it causes more problems than it's worth.
117#ifdef SUN4_COMPILER_FEATURE
118 We do a special ifdef for the sun 4, forcing it to single step
119 into calls which don't have prologues. This means that we can't
120 nexti over leaf nodes, we can probably next over them (since they
121 won't have debugging symbols, usually), and we can next out of
122 functions returning structures (with a "call .stret4" at the end).
123#endif
124#endif
125*/
126
127
128
129
130
131#include <stdio.h>
132#include "defs.h"
133#include "param.h"
134#include "symtab.h"
135#include "frame.h"
136#include "inferior.h"
137#include "wait.h"
138
139#include <signal.h>
140
141/* unistd.h is needed to #define X_OK */
142#ifdef USG
143#include <unistd.h>
144#else
145#include <sys/file.h>
146#endif
147
148#ifdef UMAX_PTRACE
149#include <aouthdr.h>
150#include <sys/param.h>
151#include <sys/ptrace.h>
152#endif /* UMAX_PTRACE */
153
154/* Required by <sys/user.h>. */
155#include <sys/types.h>
156/* Required by <sys/user.h>, at least on system V. */
157#include <sys/dir.h>
158/* Needed by IN_SIGTRAMP on some machines (e.g. vax). */
159#include <sys/param.h>
160/* Needed by IN_SIGTRAMP on some machines (e.g. vax). */
161#include <sys/user.h>
162
163extern char *sys_siglist[];
164extern int errno;
165
166/* Sigtramp is a routine that the kernel calls (which then calls the
167 signal handler). On most machines it is a library routine that
168 is linked into the executable.
169
170 This macro, given a program counter value and the name of the
171 function in which that PC resides (which can be null if the
172 name is not known), returns nonzero if the PC and name show
173 that we are in sigtramp.
174
175 On most machines just see if the name is sigtramp (and if we have
176 no name, assume we are not in sigtramp). */
177#if !defined (IN_SIGTRAMP)
178#define IN_SIGTRAMP(pc, name) \
179 name && !strcmp ("_sigtramp", name)
180#endif
181
182/* Tables of how to react to signals; the user sets them. */
183
184static char signal_stop[NSIG];
185static char signal_print[NSIG];
186static char signal_program[NSIG];
187
188/* Nonzero if breakpoints are now inserted in the inferior. */
189
190static int breakpoints_inserted;
191
192/* Function inferior was in as of last step command. */
193
194static struct symbol *step_start_function;
195
196/* This is the sequence of bytes we insert for a breakpoint. */
197
198static char break_insn[] = BREAKPOINT;
199
200/* Nonzero => address for special breakpoint for resuming stepping. */
201
202static CORE_ADDR step_resume_break_address;
203
204/* Original contents of the byte where the special breakpoint is. */
205
206static char step_resume_break_shadow[sizeof break_insn];
207
208/* Nonzero means the special breakpoint is a duplicate
209 so it has not itself been inserted. */
210
211static int step_resume_break_duplicate;
212
213/* Nonzero if we are expecting a trace trap and should proceed from it.
214 2 means expecting 2 trace traps and should continue both times.
215 That occurs when we tell sh to exec the program: we will get
216 a trap after the exec of sh and a second when the program is exec'd. */
217
218static int trap_expected;
219
220/* Nonzero if the next time we try to continue the inferior, it will
221 step one instruction and generate a spurious trace trap.
222 This is used to compensate for a bug in HP-UX. */
223
224static int trap_expected_after_continue;
225
226/* Nonzero means expecting a trace trap
227 and should stop the inferior and return silently when it happens. */
228
229int stop_after_trap;
230
231/* Nonzero means expecting a trace trap due to attaching to a process. */
232
233int stop_after_attach;
234
235/* Nonzero if pc has been changed by the debugger
236 since the inferior stopped. */
237
238int pc_changed;
239
240/* Nonzero if debugging a remote machine via a serial link or ethernet. */
241
242int remote_debugging;
243
244/* Nonzero if program stopped due to error trying to insert breakpoints. */
245
246static int breakpoints_failed;
247
248/* Nonzero if inferior is in sh before our program got exec'd. */
249
250static int running_in_shell;
251
252/* Nonzero after stop if current stack frame should be printed. */
253
254static int stop_print_frame;
255
256#ifdef NO_SINGLE_STEP
257extern int one_stepped; /* From machine dependent code */
258extern void single_step (); /* Same. */
259#endif /* NO_SINGLE_STEP */
260
261static void insert_step_breakpoint ();
262static void remove_step_breakpoint ();
263static void wait_for_inferior ();
264static void normal_stop ();
265
266\f
267/* Clear out all variables saying what to do when inferior is continued.
268 First do this, then set the ones you want, then call `proceed'. */
269
270void
271clear_proceed_status ()
272{
273 trap_expected = 0;
274 step_range_start = 0;
275 step_range_end = 0;
276 step_frame_address = 0;
277 step_over_calls = -1;
278 step_resume_break_address = 0;
279 stop_after_trap = 0;
280 stop_after_attach = 0;
281
282 /* Discard any remaining commands left by breakpoint we had stopped at. */
283 clear_breakpoint_commands ();
284}
285
286/* Basic routine for continuing the program in various fashions.
287
288 ADDR is the address to resume at, or -1 for resume where stopped.
289 SIGNAL is the signal to give it, or 0 for none,
290 or -1 for act according to how it stopped.
291 STEP is nonzero if should trap after one instruction.
292 -1 means return after that and print nothing.
293 You should probably set various step_... variables
294 before calling here, if you are stepping.
295
296 You should call clear_proceed_status before calling proceed. */
297
298void
299proceed (addr, signal, step)
300 CORE_ADDR addr;
301 int signal;
302 int step;
303{
304 int oneproc = 0;
305
306 if (step > 0)
307 step_start_function = find_pc_function (read_pc ());
308 if (step < 0)
309 stop_after_trap = 1;
310
311 if (addr == -1)
312 {
313 /* If there is a breakpoint at the address we will resume at,
314 step one instruction before inserting breakpoints
315 so that we do not stop right away. */
316
317 if (!pc_changed && breakpoint_here_p (read_pc ()))
318 oneproc = 1;
319 }
320 else
321 {
322 write_register (PC_REGNUM, addr);
323#ifdef NPC_REGNUM
324 write_register (NPC_REGNUM, addr + 4);
325#endif
326 }
327
328 if (trap_expected_after_continue)
329 {
330 /* If (step == 0), a trap will be automatically generated after
331 the first instruction is executed. Force step one
332 instruction to clear this condition. This should not occur
333 if step is nonzero, but it is harmless in that case. */
334 oneproc = 1;
335 trap_expected_after_continue = 0;
336 }
337
338 if (oneproc)
339 /* We will get a trace trap after one instruction.
340 Continue it automatically and insert breakpoints then. */
341 trap_expected = 1;
342 else
343 {
344 int temp = insert_breakpoints ();
345 if (temp)
346 {
347 print_sys_errmsg ("ptrace", temp);
348 error ("Cannot insert breakpoints.\n\
349The same program may be running in another process.");
350 }
351 breakpoints_inserted = 1;
352 }
353
354 /* Install inferior's terminal modes. */
355 terminal_inferior ();
356
357 if (signal >= 0)
358 stop_signal = signal;
359 /* If this signal should not be seen by program,
360 give it zero. Used for debugging signals. */
361 else if (stop_signal < NSIG && !signal_program[stop_signal])
362 stop_signal= 0;
363
364 /* Resume inferior. */
365 resume (oneproc || step, stop_signal);
366
367 /* Wait for it to stop (if not standalone)
368 and in any case decode why it stopped, and act accordingly. */
369
370 wait_for_inferior ();
371 normal_stop ();
372}
373
374/* Writing the inferior pc as a register calls this function
375 to inform infrun that the pc has been set in the debugger. */
376
377void
378writing_pc (val)
379 CORE_ADDR val;
380{
381 stop_pc = val;
382 pc_changed = 1;
383}
384
385/* Start an inferior process for the first time.
386 Actually it was started by the fork that created it,
387 but it will have stopped one instruction after execing sh.
388 Here we must get it up to actual execution of the real program. */
389
390void
391start_inferior ()
392{
393 /* We will get a trace trap after one instruction.
394 Continue it automatically. Eventually (after shell does an exec)
395 it will get another trace trap. Then insert breakpoints and continue. */
396
397#ifdef START_INFERIOR_TRAPS_EXPECTED
398 trap_expected = START_INFERIOR_TRAPS_EXPECTED;
399#else
400 trap_expected = 2;
401#endif
402
403 running_in_shell = 0; /* Set to 1 at first SIGTRAP, 0 at second. */
404 trap_expected_after_continue = 0;
405 breakpoints_inserted = 0;
406 mark_breakpoints_out ();
407
408 /* Set up the "saved terminal modes" of the inferior
409 based on what modes we are starting it with. */
410 terminal_init_inferior ();
411
412 /* Install inferior's terminal modes. */
413 terminal_inferior ();
414
415 if (remote_debugging)
416 {
417 trap_expected = 0;
418 fetch_inferior_registers();
419 set_current_frame (create_new_frame (read_register (FP_REGNUM),
420 read_pc ()));
421 stop_frame_address = FRAME_FP (get_current_frame());
422 inferior_pid = 3;
423 if (insert_breakpoints())
424 fatal("Can't insert breakpoints");
425 breakpoints_inserted = 1;
426 proceed(-1, -1, 0);
427 }
428 else
429 {
430 wait_for_inferior ();
431 normal_stop ();
432 }
433}
434
435/* Start or restart remote-debugging of a machine over a serial link. */
436
437void
438restart_remote ()
439{
440 clear_proceed_status ();
441 running_in_shell = 0;
442 trap_expected = 0;
443 stop_after_attach = 1;
444 inferior_pid = 3;
445 wait_for_inferior ();
446 normal_stop();
447}
448
449void
450start_remote ()
451{
452 breakpoints_inserted = 0;
453 mark_breakpoints_out ();
454 restart_remote();
455}
456
457#ifdef ATTACH_DETACH
458
459/* Attach to process PID, then initialize for debugging it
460 and wait for the trace-trap that results from attaching. */
461
462void
463attach_program (pid)
464 int pid;
465{
466 attach (pid);
467 inferior_pid = pid;
468
469 mark_breakpoints_out ();
470 terminal_init_inferior ();
471 clear_proceed_status ();
472 stop_after_attach = 1;
473 /*proceed (-1, 0, -2);*/
474 terminal_inferior ();
475 wait_for_inferior ();
476 normal_stop ();
477}
478#endif /* ATTACH_DETACH */
479\f
480/* Wait for control to return from inferior to debugger.
481 If inferior gets a signal, we may decide to start it up again
482 instead of returning. That is why there is a loop in this function.
483 When this function actually returns it means the inferior
484 should be left stopped and GDB should read more commands. */
485
486static void
487wait_for_inferior ()
488{
489 register int pid;
490 WAITTYPE w;
491 CORE_ADDR pc;
492 int tem;
493 int another_trap;
494 int random_signal;
495 CORE_ADDR stop_sp, prev_sp;
496 CORE_ADDR prev_func_start, stop_func_start;
497 char *prev_func_name, *stop_func_name;
498 CORE_ADDR prologue_pc;
499 int stop_step_resume_break;
500 CORE_ADDR step_resume_break_sp;
501 int newmisc;
502 int newfun_pc;
503 struct symtab_and_line sal;
504 int prev_pc;
505 extern CORE_ADDR text_end;
506 int remove_breakpoints_on_following_step = 0;
507
508 prev_pc = read_pc ();
509 (void) find_pc_partial_function (prev_pc, &prev_func_name,
510 &prev_func_start);
511 prev_func_start += FUNCTION_START_OFFSET;
512 prev_sp = read_register (SP_REGNUM);
513
514 while (1)
515 {
516 /* Clean up saved state that will become invalid. */
517 pc_changed = 0;
518 flush_cached_frames ();
519
520 if (remote_debugging)
521 remote_wait (&w);
522 else
523 {
524 pid = wait (&w);
525 if (pid != inferior_pid)
526 continue;
527 }
528
529 /* See if the process still exists; clean up if it doesn't. */
530 if (WIFEXITED (w))
531 {
532 terminal_ours_for_output ();
533 if (WEXITSTATUS (w))
534 printf ("\nProgram exited with code 0%o.\n", WEXITSTATUS (w));
535 else
536 printf ("\nProgram exited normally.\n");
537 fflush (stdout);
538 inferior_died ();
539#ifdef NO_SINGLE_STEP
540 one_stepped = 0;
541#endif
542 stop_print_frame = 0;
543 break;
544 }
545 else if (!WIFSTOPPED (w))
546 {
547 kill_inferior ();
548 stop_print_frame = 0;
549 stop_signal = WTERMSIG (w);
550 terminal_ours_for_output ();
551 printf ("\nProgram terminated with signal %d, %s\n",
552 stop_signal,
553 stop_signal < NSIG
554 ? sys_siglist[stop_signal]
555 : "(undocumented)");
556 printf ("The inferior process no longer exists.\n");
557 fflush (stdout);
558#ifdef NO_SINGLE_STEP
559 one_stepped = 0;
560#endif
561 break;
562 }
563
564#ifdef NO_SINGLE_STEP
565 if (one_stepped)
566 single_step (0); /* This actually cleans up the ss */
567#endif /* NO_SINGLE_STEP */
568
569 fetch_inferior_registers ();
570 stop_pc = read_pc ();
571 set_current_frame ( create_new_frame (read_register (FP_REGNUM),
572 read_pc ()));
573
574 stop_frame_address = FRAME_FP (get_current_frame ());
575 stop_sp = read_register (SP_REGNUM);
576 stop_func_start = 0;
577 stop_func_name = 0;
578 /* Don't care about return value; stop_func_start and stop_func_name
579 will both be 0 if it doesn't work. */
580 (void) find_pc_partial_function (stop_pc, &stop_func_name,
581 &stop_func_start);
582 stop_func_start += FUNCTION_START_OFFSET;
583 another_trap = 0;
584 stop_breakpoint = 0;
585 stop_step = 0;
586 stop_stack_dummy = 0;
587 stop_print_frame = 1;
588 stop_step_resume_break = 0;
589 random_signal = 0;
590 stopped_by_random_signal = 0;
591 breakpoints_failed = 0;
592
593 /* Look at the cause of the stop, and decide what to do.
594 The alternatives are:
595 1) break; to really stop and return to the debugger,
596 2) drop through to start up again
597 (set another_trap to 1 to single step once)
598 3) set random_signal to 1, and the decision between 1 and 2
599 will be made according to the signal handling tables. */
600
601 stop_signal = WSTOPSIG (w);
602
603 /* First, distinguish signals caused by the debugger from signals
604 that have to do with the program's own actions.
605 Note that breakpoint insns may cause SIGTRAP or SIGILL
606 or SIGEMT, depending on the operating system version.
607 Here we detect when a SIGILL or SIGEMT is really a breakpoint
608 and change it to SIGTRAP. */
609
610 if (stop_signal == SIGTRAP
611 || (breakpoints_inserted &&
612 (stop_signal == SIGILL
613 || stop_signal == SIGEMT))
614 || stop_after_attach)
615 {
616 if (stop_signal == SIGTRAP && stop_after_trap)
617 {
618 stop_print_frame = 0;
619 break;
620 }
621 if (stop_after_attach)
622 break;
623 /* Don't even think about breakpoints
624 if still running the shell that will exec the program
625 or if just proceeded over a breakpoint. */
626 if (stop_signal == SIGTRAP && trap_expected)
627 stop_breakpoint = 0;
628 else
629 {
630 /* See if there is a breakpoint at the current PC. */
631#if DECR_PC_AFTER_BREAK
632 /* Notice the case of stepping through a jump
633 that leads just after a breakpoint.
634 Don't confuse that with hitting the breakpoint.
635 What we check for is that 1) stepping is going on
636 and 2) the pc before the last insn does not match
637 the address of the breakpoint before the current pc. */
638 if (!(prev_pc != stop_pc - DECR_PC_AFTER_BREAK
639 && step_range_end && !step_resume_break_address))
640#endif /* DECR_PC_AFTER_BREAK not zero */
641 {
642 /* See if we stopped at the special breakpoint for
643 stepping over a subroutine call. */
644 if (stop_pc - DECR_PC_AFTER_BREAK
645 == step_resume_break_address)
646 {
647 stop_step_resume_break = 1;
648 if (DECR_PC_AFTER_BREAK)
649 {
650 stop_pc -= DECR_PC_AFTER_BREAK;
651 write_register (PC_REGNUM, stop_pc);
652 pc_changed = 0;
653 }
654 }
655 else
656 {
657 stop_breakpoint =
658 breakpoint_stop_status (stop_pc, stop_frame_address);
659 /* Following in case break condition called a
660 function. */
661 stop_print_frame = 1;
662 if (stop_breakpoint && DECR_PC_AFTER_BREAK)
663 {
664 stop_pc -= DECR_PC_AFTER_BREAK;
665 write_register (PC_REGNUM, stop_pc);
666#ifdef NPC_REGNUM
667 write_register (NPC_REGNUM, stop_pc + 4);
668#endif
669 pc_changed = 0;
670 }
671 }
672 }
673 }
674
675 if (stop_signal == SIGTRAP)
676 random_signal
677 = !(stop_breakpoint || trap_expected
678 || stop_step_resume_break
679#ifndef CANNOT_EXECUTE_STACK
680 || (stop_sp INNER_THAN stop_pc
681 && stop_pc INNER_THAN stop_frame_address)
682#else
683 || stop_pc == text_end - 2
684#endif
685 || (step_range_end && !step_resume_break_address));
686 else
687 {
688 random_signal
689 = !(stop_breakpoint
690 || stop_step_resume_break
691#ifdef sony_news
692 || (stop_sp INNER_THAN stop_pc
693 && stop_pc INNER_THAN stop_frame_address)
694#endif
695
696 );
697 if (!random_signal)
698 stop_signal = SIGTRAP;
699 }
700 }
701 else
702 random_signal = 1;
703
704 /* For the program's own signals, act according to
705 the signal handling tables. */
706
707 if (random_signal
708 && !(running_in_shell && stop_signal == SIGSEGV))
709 {
710 /* Signal not for debugging purposes. */
711 int printed = 0;
712
713 stopped_by_random_signal = 1;
714
715 if (stop_signal >= NSIG
716 || signal_print[stop_signal])
717 {
718 printed = 1;
719 terminal_ours_for_output ();
720 printf ("\nProgram received signal %d, %s\n",
721 stop_signal,
722 stop_signal < NSIG
723 ? sys_siglist[stop_signal]
724 : "(undocumented)");
725 fflush (stdout);
726 }
727 if (stop_signal >= NSIG
728 || signal_stop[stop_signal])
729 break;
730 /* If not going to stop, give terminal back
731 if we took it away. */
732 else if (printed)
733 terminal_inferior ();
734 }
735
736 /* Handle cases caused by hitting a breakpoint. */
737
738 if (!random_signal
739 && (stop_breakpoint || stop_step_resume_break))
740 {
741 /* Does a breakpoint want us to stop? */
742 if (stop_breakpoint && stop_breakpoint != -1
743 && stop_breakpoint != -0x1000001)
744 {
745 /* 0x1000000 is set in stop_breakpoint as returned by
746 breakpoint_stop_status to indicate a silent
747 breakpoint. */
748 if ((stop_breakpoint > 0 ? stop_breakpoint :
749 -stop_breakpoint)
750 & 0x1000000)
751 {
752 stop_print_frame = 0;
753 if (stop_breakpoint > 0)
754 stop_breakpoint -= 0x1000000;
755 else
756 stop_breakpoint += 0x1000000;
757 }
758 break;
759 }
760 /* But if we have hit the step-resumption breakpoint,
761 remove it. It has done its job getting us here.
762 The sp test is to make sure that we don't get hung
763 up in recursive calls in functions without frame
764 pointers. If the stack pointer isn't outside of
765 where the breakpoint was set (within a routine to be
766 stepped over), we're in the middle of a recursive
767 call. Not true for reg window machines (sparc)
768 because the must change frames to call things and
769 the stack pointer doesn't have to change if it
770 the bp was set in a routine without a frame (pc can
771 be stored in some other window).
772
773 The removal of the sp test is to allow calls to
774 alloca. Nasty things were happening. Oh, well,
775 gdb can only handle one level deep of lack of
776 frame pointer. */
777 if (stop_step_resume_break
778 && (step_frame_address == 0
779 || (stop_frame_address == step_frame_address)))
780 {
781 remove_step_breakpoint ();
782 step_resume_break_address = 0;
783 }
784 /* Otherwise, must remove breakpoints and single-step
785 to get us past the one we hit. */
786 else
787 {
788 remove_breakpoints ();
789 remove_step_breakpoint ();
790 breakpoints_inserted = 0;
791 another_trap = 1;
792 }
793
794 /* We come here if we hit a breakpoint but should not
795 stop for it. Possibly we also were stepping
796 and should stop for that. So fall through and
797 test for stepping. But, if not stepping,
798 do not stop. */
799 }
800
801 /* If this is the breakpoint at the end of a stack dummy,
802 just stop silently. */
803#ifndef CANNOT_EXECUTE_STACK
804 if (stop_sp INNER_THAN stop_pc
805 && stop_pc INNER_THAN stop_frame_address)
806#else
807 if (stop_pc == text_end - 2)
808#endif
809 {
810 stop_print_frame = 0;
811 stop_stack_dummy = 1;
812#ifdef HP_OS_BUG
813 trap_expected_after_continue = 1;
814#endif
815 break;
816 }
817
818 if (step_resume_break_address)
819 /* Having a step-resume breakpoint overrides anything
820 else having to do with stepping commands until
821 that breakpoint is reached. */
822 ;
823 /* If stepping through a line, keep going if still within it. */
824 else if (!random_signal
825 && step_range_end
826 && stop_pc >= step_range_start
827 && stop_pc < step_range_end
828 /* The step range might include the start of the
829 function, so if we are at the start of the
830 step range and either the stack or frame pointers
831 just changed, we've stepped outside */
832 && !(stop_pc == step_range_start
833 && stop_frame_address
834 && (stop_sp INNER_THAN prev_sp
835 || stop_frame_address != step_frame_address)))
836 {
837 /* Don't step through the return from a function
838 unless that is the first instruction stepped through. */
839 if (ABOUT_TO_RETURN (stop_pc))
840 {
841 stop_step = 1;
842 break;
843 }
844 }
845
846 /* We stepped out of the stepping range. See if that was due
847 to a subroutine call that we should proceed to the end of. */
848 else if (!random_signal && step_range_end)
849 {
850 if (stop_func_start)
851 {
852 prologue_pc = stop_func_start;
853 SKIP_PROLOGUE (prologue_pc);
854 }
855
856 /* Did we just take a signal? */
857 if (IN_SIGTRAMP (stop_pc, stop_func_name)
858 && !IN_SIGTRAMP (prev_pc, prev_func_name))
859 {
860 /* This code is needed at least in the following case:
861 The user types "next" and then a signal arrives (before
862 the "next" is done). */
863 /* We've just taken a signal; go until we are back to
864 the point where we took it and one more. */
865 step_resume_break_address = prev_pc;
866 step_resume_break_duplicate =
867 breakpoint_here_p (step_resume_break_address);
868 step_resume_break_sp = stop_sp;
869 if (breakpoints_inserted)
870 insert_step_breakpoint ();
871 /* Make sure that the stepping range gets us past
872 that instruction. */
873 if (step_range_end == 1)
874 step_range_end = (step_range_start = prev_pc) + 1;
875 remove_breakpoints_on_following_step = 1;
876 }
877
878 /* ==> See comments at top of file on this algorithm. <==*/
879
880 else if (stop_pc == stop_func_start
881 && (stop_func_start != prev_func_start
882 || prologue_pc != stop_func_start
883 || stop_sp != prev_sp))
884 {
885 /* It's a subroutine call */
886 if (step_over_calls > 0
887 || (step_over_calls && find_pc_function (stop_pc) == 0))
888 {
889 /* A subroutine call has happened. */
890 /* Set a special breakpoint after the return */
891 step_resume_break_address =
892 SAVED_PC_AFTER_CALL (get_current_frame ());
893 step_resume_break_duplicate
894 = breakpoint_here_p (step_resume_break_address);
895 step_resume_break_sp = stop_sp;
896 if (breakpoints_inserted)
897 insert_step_breakpoint ();
898 }
899 /* Subroutine call with source code we should not step over.
900 Do step to the first line of code in it. */
901 else if (step_over_calls)
902 {
903 SKIP_PROLOGUE (stop_func_start);
904 sal = find_pc_line (stop_func_start, 0);
905 /* Use the step_resume_break to step until
906 the end of the prologue, even if that involves jumps
907 (as it seems to on the vax under 4.2). */
908 /* If the prologue ends in the middle of a source line,
909 continue to the end of that source line.
910 Otherwise, just go to end of prologue. */
911#ifdef PROLOGUE_FIRSTLINE_OVERLAP
912 /* no, don't either. It skips any code that's
913 legitimately on the first line. */
914#else
915 if (sal.end && sal.pc != stop_func_start)
916 stop_func_start = sal.end;
917#endif
918
919 if (stop_func_start == stop_pc)
920 {
921 /* We are already there: stop now. */
922 stop_step = 1;
923 break;
924 }
925 else
926 /* Put the step-breakpoint there and go until there. */
927 {
928 step_resume_break_address = stop_func_start;
929 step_resume_break_sp = stop_sp;
930
931 step_resume_break_duplicate
932 = breakpoint_here_p (step_resume_break_address);
933 if (breakpoints_inserted)
934 insert_step_breakpoint ();
935 /* Do not specify what the fp should be when we stop
936 since on some machines the prologue
937 is where the new fp value is established. */
938 step_frame_address = 0;
939 /* And make sure stepping stops right away then. */
940 step_range_end = step_range_start;
941 }
942 }
943 else
944 {
945 /* We get here only if step_over_calls is 0 and we
946 just stepped into a subroutine. I presume
947 that step_over_calls is only 0 when we're
948 supposed to be stepping at the assembly
949 language level.*/
950 stop_step = 1;
951 break;
952 }
953 }
954 /* No subroutince call; stop now. */
955 else
956 {
957 stop_step = 1;
958 break;
959 }
960 }
961
962 /* Save the pc before execution, to compare with pc after stop. */
963 prev_pc = read_pc (); /* Might have been DECR_AFTER_BREAK */
964 prev_func_start = stop_func_start; /* Ok, since if DECR_PC_AFTER
965 BREAK is defined, the
966 original pc would not have
967 been at the start of a
968 function. */
969 prev_func_name = stop_func_name;
970 prev_sp = stop_sp;
971
972 /* If we did not do break;, it means we should keep
973 running the inferior and not return to debugger. */
974
975 /* If trap_expected is 2, it means continue once more
976 and insert breakpoints at the next trap.
977 If trap_expected is 1 and the signal was SIGSEGV, it means
978 the shell is doing some memory allocation--just resume it
979 with SIGSEGV.
980 Otherwise insert breakpoints now, and possibly single step. */
981
982 if (trap_expected > 1)
983 {
984 trap_expected--;
985 running_in_shell = 1;
986 resume (0, 0);
987 }
988 else if (running_in_shell && stop_signal == SIGSEGV)
989 {
990 resume (0, SIGSEGV);
991 }
992 else if (trap_expected && stop_signal != SIGTRAP)
993 {
994 /* We took a signal which we are supposed to pass through to
995 the inferior and we haven't yet gotten our trap. Simply
996 continue. */
997 resume ((step_range_end && !step_resume_break_address)
998 || trap_expected,
999 stop_signal);
1000 }
1001 else
1002 {
1003 /* Here, we are not awaiting another exec to get
1004 the program we really want to debug.
1005 Insert breakpoints now, unless we are trying
1006 to one-proceed past a breakpoint. */
1007 running_in_shell = 0;
1008 /* If we've just finished a special step resume and we don't
1009 want to hit a breakpoint, pull em out. */
1010 if (!step_resume_break_address &&
1011 remove_breakpoints_on_following_step)
1012 {
1013 remove_breakpoints_on_following_step = 0;
1014 remove_breakpoints ();
1015 breakpoints_inserted = 0;
1016 }
1017 else if (!breakpoints_inserted && !another_trap)
1018 {
1019 insert_step_breakpoint ();
1020 breakpoints_failed = insert_breakpoints ();
1021 if (breakpoints_failed)
1022 break;
1023 breakpoints_inserted = 1;
1024 }
1025
1026 trap_expected = another_trap;
1027
1028 if (stop_signal == SIGTRAP)
1029 stop_signal = 0;
1030
1031 resume ((step_range_end && !step_resume_break_address)
1032 || trap_expected,
1033 stop_signal);
1034 }
1035 }
1036}
1037\f
1038/* Here to return control to GDB when the inferior stops for real.
1039 Print appropriate messages, remove breakpoints, give terminal our modes.
1040
1041 RUNNING_IN_SHELL nonzero means the shell got a signal before
1042 exec'ing the program we wanted to run.
1043 STOP_PRINT_FRAME nonzero means print the executing frame
1044 (pc, function, args, file, line number and line text).
1045 BREAKPOINTS_FAILED nonzero means stop was due to error
1046 attempting to insert breakpoints. */
1047
1048static void
1049normal_stop ()
1050{
1051 /* Make sure that the current_frame's pc is correct. This
1052 is a correction for setting up the frame info before doing
1053 DECR_PC_AFTER_BREAK */
1054 if (inferior_pid)
1055 (get_current_frame ())->pc = read_pc ();
1056
1057 if (breakpoints_failed)
1058 {
1059 terminal_ours_for_output ();
1060 print_sys_errmsg ("ptrace", breakpoints_failed);
1061 printf ("Stopped; cannot insert breakpoints.\n\
1062The same program may be running in another process.\n");
1063 }
1064
1065 if (inferior_pid)
1066 remove_step_breakpoint ();
1067
1068 if (inferior_pid && breakpoints_inserted)
1069 if (remove_breakpoints ())
1070 {
1071 terminal_ours_for_output ();
1072 printf ("Cannot remove breakpoints because program is no longer writable.\n\
1073It must be running in another process.\n\
1074Further execution is probably impossible.\n");
1075 }
1076
1077 breakpoints_inserted = 0;
1078
1079 /* Delete the breakpoint we stopped at, if it wants to be deleted.
1080 Delete any breakpoint that is to be deleted at the next stop. */
1081
1082 breakpoint_auto_delete (stop_breakpoint);
1083
1084 /* If an auto-display called a function and that got a signal,
1085 delete that auto-display to avoid an infinite recursion. */
1086
1087 if (stopped_by_random_signal)
1088 disable_current_display ();
1089
1090 if (step_multi && stop_step)
1091 return;
1092
1093 terminal_ours ();
1094
1095 if (running_in_shell)
1096 {
1097 if (stop_signal == SIGSEGV)
1098 {
1099 char *exec_file = (char *) get_exec_file (1);
1100
1101 if (access (exec_file, X_OK) != 0)
1102 printf ("The file \"%s\" is not executable.\n", exec_file);
1103 else
1104 /* I don't think we should ever get here.
1105 wait_for_inferior now ignores SIGSEGV's which happen in
1106 the shell (since the Bourne shell (/bin/sh) has some
1107 rather, er, uh, *unorthodox* memory management
1108 involving catching SIGSEGV). */
1109 printf ("\
1110You have just encountered a bug in \"sh\". GDB starts your program\n\
1111by running \"sh\" with a command to exec your program.\n\
1112This is so that \"sh\" will process wildcards and I/O redirection.\n\
1113This time, \"sh\" crashed.\n\
1114\n\
1115One known bug in \"sh\" bites when the environment takes up a lot of space.\n\
1116Try \"info env\" to see the environment; then use \"delete env\" to kill\n\
1117some variables whose values are large; then do \"run\" again.\n\
1118\n\
1119If that works, you might want to put those \"delete env\" commands\n\
1120into a \".gdbinit\" file in this directory so they will happen every time.\n");
1121 }
1122 /* Don't confuse user with his program's symbols on sh's data. */
1123 stop_print_frame = 0;
1124 }
1125
1126 if (inferior_pid == 0)
1127 return;
1128
1129 /* Select innermost stack frame except on return from a stack dummy routine,
1130 or if the program has exited. */
1131 if (!stop_stack_dummy)
1132 {
1133 select_frame (get_current_frame (), 0);
1134
1135 if (stop_print_frame)
1136 {
1137 if (stop_breakpoint > 0)
1138 printf ("\nBpt %d, ", stop_breakpoint);
1139 print_sel_frame (stop_step
1140 && step_frame_address == stop_frame_address
1141 && step_start_function == find_pc_function (stop_pc));
1142 /* Display the auto-display expressions. */
1143 do_displays ();
1144 }
1145 }
1146
1147 if (stop_stack_dummy)
1148 {
1149 /* Pop the empty frame that contains the stack dummy.
1150 POP_FRAME ends with a setting of the current frame, so we
1151 can use that next. */
1152#ifndef NEW_CALL_FUNCTION
1153 POP_FRAME;
1154#endif
1155 select_frame (get_current_frame (), 0);
1156 }
1157}
1158\f
1159static void
1160insert_step_breakpoint ()
1161{
1162 if (step_resume_break_address && !step_resume_break_duplicate)
1163 {
1164 read_memory (step_resume_break_address,
1165 step_resume_break_shadow, sizeof break_insn);
1166 write_memory (step_resume_break_address,
1167 break_insn, sizeof break_insn);
1168 }
1169}
1170
1171static void
1172remove_step_breakpoint ()
1173{
1174 if (step_resume_break_address && !step_resume_break_duplicate)
1175 write_memory (step_resume_break_address, step_resume_break_shadow,
1176 sizeof break_insn);
1177}
1178\f
1179/* Specify how various signals in the inferior should be handled. */
1180
1181static void
1182handle_command (args, from_tty)
1183 char *args;
1184 int from_tty;
1185{
1186 register char *p = args;
1187 int signum = 0;
1188 register int digits, wordlen;
1189
1190 if (!args)
1191 error_no_arg ("signal to handle");
1192
1193 while (*p)
1194 {
1195 /* Find the end of the next word in the args. */
1196 for (wordlen = 0; p[wordlen] && p[wordlen] != ' ' && p[wordlen] != '\t';
1197 wordlen++);
1198 for (digits = 0; p[digits] >= '0' && p[digits] <= '9'; digits++);
1199
1200 /* If it is all digits, it is signal number to operate on. */
1201 if (digits == wordlen)
1202 {
1203 signum = atoi (p);
1204 if (signum <= 0 || signum >= NSIG)
1205 {
1206 p[wordlen] = '\0';
1207 error ("Invalid signal %s given as argument to \"handle\".", p);
1208 }
1209 if (signum == SIGTRAP || signum == SIGINT)
1210 {
1211 if (!query ("Signal %d is used by the debugger.\nAre you sure you want to change it? ", signum))
1212 error ("Not confirmed.");
1213 }
1214 }
1215 else if (signum == 0)
1216 error ("First argument is not a signal number.");
1217
1218 /* Else, if already got a signal number, look for flag words
1219 saying what to do for it. */
1220 else if (!strncmp (p, "stop", wordlen))
1221 {
1222 signal_stop[signum] = 1;
1223 signal_print[signum] = 1;
1224 }
1225 else if (wordlen >= 2 && !strncmp (p, "print", wordlen))
1226 signal_print[signum] = 1;
1227 else if (wordlen >= 2 && !strncmp (p, "pass", wordlen))
1228 signal_program[signum] = 1;
1229 else if (!strncmp (p, "ignore", wordlen))
1230 signal_program[signum] = 0;
1231 else if (wordlen >= 3 && !strncmp (p, "nostop", wordlen))
1232 signal_stop[signum] = 0;
1233 else if (wordlen >= 4 && !strncmp (p, "noprint", wordlen))
1234 {
1235 signal_print[signum] = 0;
1236 signal_stop[signum] = 0;
1237 }
1238 else if (wordlen >= 4 && !strncmp (p, "nopass", wordlen))
1239 signal_program[signum] = 0;
1240 else if (wordlen >= 3 && !strncmp (p, "noignore", wordlen))
1241 signal_program[signum] = 1;
1242 /* Not a number and not a recognized flag word => complain. */
1243 else
1244 {
1245 p[wordlen] = 0;
1246 error ("Unrecognized flag word: \"%s\".", p);
1247 }
1248
1249 /* Find start of next word. */
1250 p += wordlen;
1251 while (*p == ' ' || *p == '\t') p++;
1252 }
1253
1254 if (from_tty)
1255 {
1256 /* Show the results. */
1257 printf ("Number\tStop\tPrint\tPass to program\tDescription\n");
1258 printf ("%d\t", signum);
1259 printf ("%s\t", signal_stop[signum] ? "Yes" : "No");
1260 printf ("%s\t", signal_print[signum] ? "Yes" : "No");
1261 printf ("%s\t\t", signal_program[signum] ? "Yes" : "No");
1262 printf ("%s\n", sys_siglist[signum]);
1263 }
1264}
1265
1266/* Print current contents of the tables set by the handle command. */
1267
1268static void
1269signals_info (signum_exp)
1270 char *signum_exp;
1271{
1272 register int i;
1273 printf_filtered ("Number\tStop\tPrint\tPass to program\tDescription\n");
1274
1275 if (signum_exp)
1276 {
1277 i = parse_and_eval_address (signum_exp);
1278 if (i >= NSIG || i < 0)
1279 error ("Signal number out of bounds.");
1280 printf_filtered ("%d\t", i);
1281 printf_filtered ("%s\t", signal_stop[i] ? "Yes" : "No");
1282 printf_filtered ("%s\t", signal_print[i] ? "Yes" : "No");
1283 printf_filtered ("%s\t\t", signal_program[i] ? "Yes" : "No");
1284 printf_filtered ("%s\n", sys_siglist[i]);
1285 return;
1286 }
1287
1288 printf_filtered ("\n");
1289 for (i = 0; i < NSIG; i++)
1290 {
1291 QUIT;
1292
1293 printf_filtered ("%d\t", i);
1294 printf_filtered ("%s\t", signal_stop[i] ? "Yes" : "No");
1295 printf_filtered ("%s\t", signal_print[i] ? "Yes" : "No");
1296 printf_filtered ("%s\t\t", signal_program[i] ? "Yes" : "No");
1297 printf_filtered ("%s\n", sys_siglist[i]);
1298 }
1299
1300 printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
1301}
1302\f
1303/* Save all of the information associated with the inferior<==>gdb
1304 connection. INF_STATUS is a pointer to a "struct inferior_status"
1305 (defined in inferior.h). */
1306
1307struct command_line *get_breakpoint_commands ();
1308
1309void
1310save_inferior_status (inf_status, restore_stack_info)
1311 struct inferior_status *inf_status;
1312 int restore_stack_info;
1313{
1314 inf_status->pc_changed = pc_changed;
1315 inf_status->stop_signal = stop_signal;
1316 inf_status->stop_pc = stop_pc;
1317 inf_status->stop_frame_address = stop_frame_address;
1318 inf_status->stop_breakpoint = stop_breakpoint;
1319 inf_status->stop_step = stop_step;
1320 inf_status->stop_stack_dummy = stop_stack_dummy;
1321 inf_status->stopped_by_random_signal = stopped_by_random_signal;
1322 inf_status->trap_expected = trap_expected;
1323 inf_status->step_range_start = step_range_start;
1324 inf_status->step_range_end = step_range_end;
1325 inf_status->step_frame_address = step_frame_address;
1326 inf_status->step_over_calls = step_over_calls;
1327 inf_status->step_resume_break_address = step_resume_break_address;
1328 inf_status->stop_after_trap = stop_after_trap;
1329 inf_status->stop_after_attach = stop_after_attach;
1330 inf_status->breakpoint_commands = get_breakpoint_commands ();
1331 inf_status->restore_stack_info = restore_stack_info;
1332
1333 read_register_bytes(0, inf_status->register_context, REGISTER_BYTES);
1334 record_selected_frame (&(inf_status->selected_frame_address),
1335 &(inf_status->selected_level));
1336 return;
1337}
1338
1339void
1340restore_inferior_status (inf_status)
1341 struct inferior_status *inf_status;
1342{
1343 FRAME fid;
1344 int level = inf_status->selected_level;
1345
1346 pc_changed = inf_status->pc_changed;
1347 stop_signal = inf_status->stop_signal;
1348 stop_pc = inf_status->stop_pc;
1349 stop_frame_address = inf_status->stop_frame_address;
1350 stop_breakpoint = inf_status->stop_breakpoint;
1351 stop_step = inf_status->stop_step;
1352 stop_stack_dummy = inf_status->stop_stack_dummy;
1353 stopped_by_random_signal = inf_status->stopped_by_random_signal;
1354 trap_expected = inf_status->trap_expected;
1355 step_range_start = inf_status->step_range_start;
1356 step_range_end = inf_status->step_range_end;
1357 step_frame_address = inf_status->step_frame_address;
1358 step_over_calls = inf_status->step_over_calls;
1359 step_resume_break_address = inf_status->step_resume_break_address;
1360 stop_after_trap = inf_status->stop_after_trap;
1361 stop_after_attach = inf_status->stop_after_attach;
1362 set_breakpoint_commands (inf_status->breakpoint_commands);
1363
1364 write_register_bytes(0, inf_status->register_context, REGISTER_BYTES);
1365
1366 /* The inferior can be gone if the user types "print exit(0)"
1367 (and perhaps other times). */
1368 if (have_inferior_p() && inf_status->restore_stack_info)
1369 {
1370 flush_cached_frames();
1371 set_current_frame(create_new_frame(read_register (FP_REGNUM),
1372 read_pc()));
1373
1374 fid = find_relative_frame (get_current_frame (), &level);
1375
1376 if (fid == 0 ||
1377 FRAME_FP (fid) != inf_status->selected_frame_address ||
1378 level != 0)
1379 {
1380 /* I'm not sure this error message is a good idea. I have
1381 only seen it occur after "Can't continue previously
1382 requested operation" (we get called from do_cleanups), in
1383 which case it just adds insult to injury (one confusing
1384 error message after another. Besides which, does the
1385 user really care if we can't restore the previously
1386 selected frame? */
1387 fprintf (stderr, "Unable to restore previously selected frame.\n");
1388 select_frame (get_current_frame (), 0);
1389 return;
1390 }
1391
1392 select_frame (fid, inf_status->selected_level);
1393 }
1394 return;
1395}
1396
1397\f
1398void
1399_initialize_infrun ()
1400{
1401 register int i;
1402
1403 add_info ("signals", signals_info,
1404 "What debugger does when program gets various signals.\n\
1405Specify a signal number as argument to print info on that signal only.");
1406
1407 add_com ("handle", class_run, handle_command,
1408 "Specify how to handle a signal.\n\
1409Args are signal number followed by flags.\n\
1410Flags allowed are \"stop\", \"print\", \"pass\",\n\
1411 \"nostop\", \"noprint\" or \"nopass\".\n\
1412Print means print a message if this signal happens.\n\
1413Stop means reenter debugger if this signal happens (implies print).\n\
1414Pass means let program see this signal; otherwise program doesn't know.\n\
1415Pass and Stop may be combined.");
1416
1417 for (i = 0; i < NSIG; i++)
1418 {
1419 signal_stop[i] = 1;
1420 signal_print[i] = 1;
1421 signal_program[i] = 1;
1422 }
1423
1424 /* Signals caused by debugger's own actions
1425 should not be given to the program afterwards. */
1426 signal_program[SIGTRAP] = 0;
1427 signal_program[SIGINT] = 0;
1428
1429 /* Signals that are not errors should not normally enter the debugger. */
1430#ifdef SIGALRM
1431 signal_stop[SIGALRM] = 0;
1432 signal_print[SIGALRM] = 0;
1433#endif /* SIGALRM */
1434#ifdef SIGVTALRM
1435 signal_stop[SIGVTALRM] = 0;
1436 signal_print[SIGVTALRM] = 0;
1437#endif /* SIGVTALRM */
1438#ifdef SIGPROF
1439 signal_stop[SIGPROF] = 0;
1440 signal_print[SIGPROF] = 0;
1441#endif /* SIGPROF */
1442#ifdef SIGCHLD
1443 signal_stop[SIGCHLD] = 0;
1444 signal_print[SIGCHLD] = 0;
1445#endif /* SIGCHLD */
1446#ifdef SIGCLD
1447 signal_stop[SIGCLD] = 0;
1448 signal_print[SIGCLD] = 0;
1449#endif /* SIGCLD */
1450#ifdef SIGIO
1451 signal_stop[SIGIO] = 0;
1452 signal_print[SIGIO] = 0;
1453#endif /* SIGIO */
1454#ifdef SIGURG
1455 signal_stop[SIGURG] = 0;
1456 signal_print[SIGURG] = 0;
1457#endif /* SIGURG */
1458}
1459