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