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