gdb-3.5
[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 <stdio.h>
120 #include "defs.h"
121 #include "param.h"
122 #include "symtab.h"
123 #include "frame.h"
124 #include "inferior.h"
125 #include "wait.h"
126
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 /* This code is needed at least in the following case:
846 The user types "next" and then a signal arrives (before
847 the "next" is done). */
848 /* We've just taken a signal; go until we are back to
849 the point where we took it and one more. */
850 step_resume_break_address = prev_pc;
851 step_resume_break_duplicate =
852 breakpoint_here_p (step_resume_break_address);
853 step_resume_break_sp = stop_sp;
854 if (breakpoints_inserted)
855 insert_step_breakpoint ();
856 /* Make sure that the stepping range gets us past
857 that instruction. */
858 if (step_range_end == 1)
859 step_range_end = (step_range_start = prev_pc) + 1;
860 remove_breakpoints_on_following_step = 1;
861 }
862
863 /* ==> See comments at top of file on this algorithm. <==*/
864
865 else if (stop_pc == stop_func_start
866 && (stop_func_start != prev_func_start
867 || prologue_pc != stop_func_start
868 || stop_sp != prev_sp))
869 {
870 /* It's a subroutine call */
871 if (step_over_calls > 0
872 || (step_over_calls && find_pc_function (stop_pc) == 0))
873 {
874 /* A subroutine call has happened. */
875 /* Set a special breakpoint after the return */
876 step_resume_break_address =
877 SAVED_PC_AFTER_CALL (get_current_frame ());
878 step_resume_break_duplicate
879 = breakpoint_here_p (step_resume_break_address);
880 step_resume_break_sp = stop_sp;
881 if (breakpoints_inserted)
882 insert_step_breakpoint ();
883 }
884 /* Subroutine call with source code we should not step over.
885 Do step to the first line of code in it. */
886 else if (step_over_calls)
887 {
888 SKIP_PROLOGUE (stop_func_start);
889 sal = find_pc_line (stop_func_start, 0);
890 /* Use the step_resume_break to step until
891 the end of the prologue, even if that involves jumps
892 (as it seems to on the vax under 4.2). */
893 /* If the prologue ends in the middle of a source line,
894 continue to the end of that source line.
895 Otherwise, just go to end of prologue. */
896 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
897 /* no, don't either. It skips any code that's
898 legitimately on the first line. */
899 #else
900 if (sal.end && sal.pc != stop_func_start)
901 stop_func_start = sal.end;
902 #endif
903
904 if (stop_func_start == stop_pc)
905 {
906 /* We are already there: stop now. */
907 stop_step = 1;
908 break;
909 }
910 else
911 /* Put the step-breakpoint there and go until there. */
912 {
913 step_resume_break_address = stop_func_start;
914 step_resume_break_sp = stop_sp;
915
916 step_resume_break_duplicate
917 = breakpoint_here_p (step_resume_break_address);
918 if (breakpoints_inserted)
919 insert_step_breakpoint ();
920 /* Do not specify what the fp should be when we stop
921 since on some machines the prologue
922 is where the new fp value is established. */
923 step_frame_address = 0;
924 /* And make sure stepping stops right away then. */
925 step_range_end = step_range_start;
926 }
927 }
928 else
929 {
930 /* We get here only if step_over_calls is 0 and we
931 just stepped into a subroutine. I presume
932 that step_over_calls is only 0 when we're
933 supposed to be stepping at the assembly
934 language level.*/
935 stop_step = 1;
936 break;
937 }
938 }
939 /* No subroutince call; stop now. */
940 else
941 {
942 stop_step = 1;
943 break;
944 }
945 }
946
947 /* Save the pc before execution, to compare with pc after stop. */
948 prev_pc = read_pc (); /* Might have been DECR_AFTER_BREAK */
949 prev_func_start = stop_func_start; /* Ok, since if DECR_PC_AFTER
950 BREAK is defined, the
951 original pc would not have
952 been at the start of a
953 function. */
954 prev_func_name = stop_func_name;
955 prev_sp = stop_sp;
956
957 /* If we did not do break;, it means we should keep
958 running the inferior and not return to debugger. */
959
960 /* If trap_expected is 2, it means continue once more
961 and insert breakpoints at the next trap.
962 If trap_expected is 1 and the signal was SIGSEGV, it means
963 the shell is doing some memory allocation--just resume it
964 with SIGSEGV.
965 Otherwise insert breakpoints now, and possibly single step. */
966
967 if (trap_expected > 1)
968 {
969 trap_expected--;
970 running_in_shell = 1;
971 resume (0, 0);
972 }
973 else if (running_in_shell && stop_signal == SIGSEGV)
974 {
975 resume (0, SIGSEGV);
976 }
977 else if (trap_expected && stop_signal != SIGTRAP)
978 {
979 /* We took a signal which we are supposed to pass through to
980 the inferior and we haven't yet gotten our trap. Simply
981 continue. */
982 resume ((step_range_end && !step_resume_break_address)
983 || trap_expected,
984 stop_signal);
985 }
986 else
987 {
988 /* Here, we are not awaiting another exec to get
989 the program we really want to debug.
990 Insert breakpoints now, unless we are trying
991 to one-proceed past a breakpoint. */
992 running_in_shell = 0;
993 /* If we've just finished a special step resume and we don't
994 want to hit a breakpoint, pull em out. */
995 if (!step_resume_break_address &&
996 remove_breakpoints_on_following_step)
997 {
998 remove_breakpoints_on_following_step = 0;
999 remove_breakpoints ();
1000 breakpoints_inserted = 0;
1001 }
1002 else if (!breakpoints_inserted && !another_trap)
1003 {
1004 insert_step_breakpoint ();
1005 breakpoints_failed = insert_breakpoints ();
1006 if (breakpoints_failed)
1007 break;
1008 breakpoints_inserted = 1;
1009 }
1010
1011 trap_expected = another_trap;
1012
1013 if (stop_signal == SIGTRAP)
1014 stop_signal = 0;
1015
1016 resume ((step_range_end && !step_resume_break_address)
1017 || trap_expected,
1018 stop_signal);
1019 }
1020 }
1021 }
1022 \f
1023 /* Here to return control to GDB when the inferior stops for real.
1024 Print appropriate messages, remove breakpoints, give terminal our modes.
1025
1026 RUNNING_IN_SHELL nonzero means the shell got a signal before
1027 exec'ing the program we wanted to run.
1028 STOP_PRINT_FRAME nonzero means print the executing frame
1029 (pc, function, args, file, line number and line text).
1030 BREAKPOINTS_FAILED nonzero means stop was due to error
1031 attempting to insert breakpoints. */
1032
1033 static void
1034 normal_stop ()
1035 {
1036 /* Make sure that the current_frame's pc is correct. This
1037 is a correction for setting up the frame info before doing
1038 DECR_PC_AFTER_BREAK */
1039 if (inferior_pid)
1040 (get_current_frame ())->pc = read_pc ();
1041
1042 if (breakpoints_failed)
1043 {
1044 terminal_ours_for_output ();
1045 print_sys_errmsg ("ptrace", breakpoints_failed);
1046 printf ("Stopped; cannot insert breakpoints.\n\
1047 The same program may be running in another process.\n");
1048 }
1049
1050 if (inferior_pid)
1051 remove_step_breakpoint ();
1052
1053 if (inferior_pid && breakpoints_inserted)
1054 if (remove_breakpoints ())
1055 {
1056 terminal_ours_for_output ();
1057 printf ("Cannot remove breakpoints because program is no longer writable.\n\
1058 It must be running in another process.\n\
1059 Further execution is probably impossible.\n");
1060 }
1061
1062 breakpoints_inserted = 0;
1063
1064 /* Delete the breakpoint we stopped at, if it wants to be deleted.
1065 Delete any breakpoint that is to be deleted at the next stop. */
1066
1067 breakpoint_auto_delete (stop_breakpoint);
1068
1069 /* If an auto-display called a function and that got a signal,
1070 delete that auto-display to avoid an infinite recursion. */
1071
1072 if (stopped_by_random_signal)
1073 disable_current_display ();
1074
1075 if (step_multi && stop_step)
1076 return;
1077
1078 terminal_ours ();
1079
1080 if (running_in_shell)
1081 {
1082 if (stop_signal == SIGSEGV)
1083 {
1084 char *exec_file = (char *) get_exec_file (1);
1085
1086 if (access (exec_file, X_OK) != 0)
1087 printf ("The file \"%s\" is not executable.\n", exec_file);
1088 else
1089 /* I don't think we should ever get here.
1090 wait_for_inferior now ignores SIGSEGV's which happen in
1091 the shell (since the Bourne shell (/bin/sh) has some
1092 rather, er, uh, *unorthodox* memory management
1093 involving catching SIGSEGV). */
1094 printf ("\
1095 You have just encountered a bug in \"sh\". GDB starts your program\n\
1096 by running \"sh\" with a command to exec your program.\n\
1097 This is so that \"sh\" will process wildcards and I/O redirection.\n\
1098 This time, \"sh\" crashed.\n\
1099 \n\
1100 One known bug in \"sh\" bites when the environment takes up a lot of space.\n\
1101 Try \"info env\" to see the environment; then use \"delete env\" to kill\n\
1102 some variables whose values are large; then do \"run\" again.\n\
1103 \n\
1104 If that works, you might want to put those \"delete env\" commands\n\
1105 into a \".gdbinit\" file in this directory so they will happen every time.\n");
1106 }
1107 /* Don't confuse user with his program's symbols on sh's data. */
1108 stop_print_frame = 0;
1109 }
1110
1111 if (inferior_pid == 0)
1112 return;
1113
1114 /* Select innermost stack frame except on return from a stack dummy routine,
1115 or if the program has exited. */
1116 if (!stop_stack_dummy)
1117 {
1118 select_frame (get_current_frame (), 0);
1119
1120 if (stop_print_frame)
1121 {
1122 if (stop_breakpoint > 0)
1123 printf ("\nBpt %d, ", stop_breakpoint);
1124 print_sel_frame (stop_step
1125 && step_frame_address == stop_frame_address
1126 && step_start_function == find_pc_function (stop_pc));
1127 /* Display the auto-display expressions. */
1128 do_displays ();
1129 }
1130 }
1131
1132 /* Save the function value return registers
1133 We might be about to restore their previous contents. */
1134 read_register_bytes (0, stop_registers, REGISTER_BYTES);
1135
1136 if (stop_stack_dummy)
1137 {
1138 /* Pop the empty frame that contains the stack dummy.
1139 POP_FRAME ends with a setting of the current frame, so we
1140 can use that next. */
1141 POP_FRAME;
1142 select_frame (get_current_frame (), 0);
1143 }
1144 }
1145 \f
1146 static void
1147 insert_step_breakpoint ()
1148 {
1149 if (step_resume_break_address && !step_resume_break_duplicate)
1150 {
1151 read_memory (step_resume_break_address,
1152 step_resume_break_shadow, sizeof break_insn);
1153 write_memory (step_resume_break_address,
1154 break_insn, sizeof break_insn);
1155 }
1156 }
1157
1158 static void
1159 remove_step_breakpoint ()
1160 {
1161 if (step_resume_break_address && !step_resume_break_duplicate)
1162 write_memory (step_resume_break_address, step_resume_break_shadow,
1163 sizeof break_insn);
1164 }
1165 \f
1166 /* Specify how various signals in the inferior should be handled. */
1167
1168 static void
1169 handle_command (args, from_tty)
1170 char *args;
1171 int from_tty;
1172 {
1173 register char *p = args;
1174 int signum = 0;
1175 register int digits, wordlen;
1176
1177 if (!args)
1178 error_no_arg ("signal to handle");
1179
1180 while (*p)
1181 {
1182 /* Find the end of the next word in the args. */
1183 for (wordlen = 0; p[wordlen] && p[wordlen] != ' ' && p[wordlen] != '\t';
1184 wordlen++);
1185 for (digits = 0; p[digits] >= '0' && p[digits] <= '9'; digits++);
1186
1187 /* If it is all digits, it is signal number to operate on. */
1188 if (digits == wordlen)
1189 {
1190 signum = atoi (p);
1191 if (signum <= 0 || signum >= NSIG)
1192 {
1193 p[wordlen] = '\0';
1194 error ("Invalid signal %s given as argument to \"handle\".", p);
1195 }
1196 if (signum == SIGTRAP || signum == SIGINT)
1197 {
1198 if (!query ("Signal %d is used by the debugger.\nAre you sure you want to change it? ", signum))
1199 error ("Not confirmed.");
1200 }
1201 }
1202 else if (signum == 0)
1203 error ("First argument is not a signal number.");
1204
1205 /* Else, if already got a signal number, look for flag words
1206 saying what to do for it. */
1207 else if (!strncmp (p, "stop", wordlen))
1208 {
1209 signal_stop[signum] = 1;
1210 signal_print[signum] = 1;
1211 }
1212 else if (wordlen >= 2 && !strncmp (p, "print", wordlen))
1213 signal_print[signum] = 1;
1214 else if (wordlen >= 2 && !strncmp (p, "pass", wordlen))
1215 signal_program[signum] = 1;
1216 else if (!strncmp (p, "ignore", wordlen))
1217 signal_program[signum] = 0;
1218 else if (wordlen >= 3 && !strncmp (p, "nostop", wordlen))
1219 signal_stop[signum] = 0;
1220 else if (wordlen >= 4 && !strncmp (p, "noprint", wordlen))
1221 {
1222 signal_print[signum] = 0;
1223 signal_stop[signum] = 0;
1224 }
1225 else if (wordlen >= 4 && !strncmp (p, "nopass", wordlen))
1226 signal_program[signum] = 0;
1227 else if (wordlen >= 3 && !strncmp (p, "noignore", wordlen))
1228 signal_program[signum] = 1;
1229 /* Not a number and not a recognized flag word => complain. */
1230 else
1231 {
1232 p[wordlen] = 0;
1233 error ("Unrecognized flag word: \"%s\".", p);
1234 }
1235
1236 /* Find start of next word. */
1237 p += wordlen;
1238 while (*p == ' ' || *p == '\t') p++;
1239 }
1240
1241 if (from_tty)
1242 {
1243 /* Show the results. */
1244 printf ("Number\tStop\tPrint\tPass to program\tDescription\n");
1245 printf ("%d\t", signum);
1246 printf ("%s\t", signal_stop[signum] ? "Yes" : "No");
1247 printf ("%s\t", signal_print[signum] ? "Yes" : "No");
1248 printf ("%s\t\t", signal_program[signum] ? "Yes" : "No");
1249 printf ("%s\n", sys_siglist[signum]);
1250 }
1251 }
1252
1253 /* Print current contents of the tables set by the handle command. */
1254
1255 static void
1256 signals_info (signum_exp)
1257 char *signum_exp;
1258 {
1259 register int i;
1260 printf_filtered ("Number\tStop\tPrint\tPass to program\tDescription\n");
1261
1262 if (signum_exp)
1263 {
1264 i = parse_and_eval_address (signum_exp);
1265 if (i >= NSIG || i < 0)
1266 error ("Signal number out of bounds.");
1267 printf_filtered ("%d\t", i);
1268 printf_filtered ("%s\t", signal_stop[i] ? "Yes" : "No");
1269 printf_filtered ("%s\t", signal_print[i] ? "Yes" : "No");
1270 printf_filtered ("%s\t\t", signal_program[i] ? "Yes" : "No");
1271 printf_filtered ("%s\n", sys_siglist[i]);
1272 return;
1273 }
1274
1275 printf_filtered ("\n");
1276 for (i = 0; i < NSIG; i++)
1277 {
1278 QUIT;
1279
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 }
1286
1287 printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
1288 }
1289 \f
1290 /* Save all of the information associated with the inferior<==>gdb
1291 connection. INF_STATUS is a pointer to a "struct inferior_status"
1292 (defined in inferior.h). */
1293
1294 struct command_line *get_breakpoint_commands ();
1295
1296 void
1297 save_inferior_status (inf_status, restore_stack_info)
1298 struct inferior_status *inf_status;
1299 int restore_stack_info;
1300 {
1301 inf_status->pc_changed = pc_changed;
1302 inf_status->stop_signal = stop_signal;
1303 inf_status->stop_pc = stop_pc;
1304 inf_status->stop_frame_address = stop_frame_address;
1305 inf_status->stop_breakpoint = stop_breakpoint;
1306 inf_status->stop_step = stop_step;
1307 inf_status->stop_stack_dummy = stop_stack_dummy;
1308 inf_status->stopped_by_random_signal = stopped_by_random_signal;
1309 inf_status->trap_expected = trap_expected;
1310 inf_status->step_range_start = step_range_start;
1311 inf_status->step_range_end = step_range_end;
1312 inf_status->step_frame_address = step_frame_address;
1313 inf_status->step_over_calls = step_over_calls;
1314 inf_status->step_resume_break_address = step_resume_break_address;
1315 inf_status->stop_after_trap = stop_after_trap;
1316 inf_status->stop_after_attach = stop_after_attach;
1317 inf_status->breakpoint_commands = get_breakpoint_commands ();
1318 inf_status->restore_stack_info = restore_stack_info;
1319
1320 bcopy (stop_registers, inf_status->stop_registers, REGISTER_BYTES);
1321
1322 record_selected_frame (&(inf_status->selected_frame_address),
1323 &(inf_status->selected_level));
1324 return;
1325 }
1326
1327 void
1328 restore_inferior_status (inf_status)
1329 struct inferior_status *inf_status;
1330 {
1331 FRAME fid;
1332 int level = inf_status->selected_level;
1333
1334 pc_changed = inf_status->pc_changed;
1335 stop_signal = inf_status->stop_signal;
1336 stop_pc = inf_status->stop_pc;
1337 stop_frame_address = inf_status->stop_frame_address;
1338 stop_breakpoint = inf_status->stop_breakpoint;
1339 stop_step = inf_status->stop_step;
1340 stop_stack_dummy = inf_status->stop_stack_dummy;
1341 stopped_by_random_signal = inf_status->stopped_by_random_signal;
1342 trap_expected = inf_status->trap_expected;
1343 step_range_start = inf_status->step_range_start;
1344 step_range_end = inf_status->step_range_end;
1345 step_frame_address = inf_status->step_frame_address;
1346 step_over_calls = inf_status->step_over_calls;
1347 step_resume_break_address = inf_status->step_resume_break_address;
1348 stop_after_trap = inf_status->stop_after_trap;
1349 stop_after_attach = inf_status->stop_after_attach;
1350 set_breakpoint_commands (inf_status->breakpoint_commands);
1351
1352 bcopy (inf_status->stop_registers, stop_registers, REGISTER_BYTES);
1353
1354 /* The inferior can be gone if the user types "print exit(0)"
1355 (and perhaps other times). */
1356 if (have_inferior_p() && inf_status->restore_stack_info)
1357 {
1358 fid = find_relative_frame (get_current_frame (),
1359 &level);
1360
1361 if (fid == 0 ||
1362 FRAME_FP (fid) != inf_status->selected_frame_address ||
1363 level != 0)
1364 {
1365 /* I'm not sure this error message is a good idea. I have
1366 only seen it occur after "Can't continue previously
1367 requested operation" (we get called from do_cleanups), in
1368 which case it just adds insult to injury (one confusing
1369 error message after another. Besides which, does the
1370 user really care if we can't restore the previously
1371 selected frame? */
1372 fprintf (stderr, "Unable to restore previously selected frame.\n");
1373 select_frame (get_current_frame (), 0);
1374 return;
1375 }
1376
1377 select_frame (fid, inf_status->selected_level);
1378 }
1379 return;
1380 }
1381
1382 \f
1383 void
1384 _initialize_infrun ()
1385 {
1386 register int i;
1387
1388 add_info ("signals", signals_info,
1389 "What debugger does when program gets various signals.\n\
1390 Specify a signal number as argument to print info on that signal only.");
1391
1392 add_com ("handle", class_run, handle_command,
1393 "Specify how to handle a signal.\n\
1394 Args are signal number followed by flags.\n\
1395 Flags allowed are \"stop\", \"print\", \"pass\",\n\
1396 \"nostop\", \"noprint\" or \"nopass\".\n\
1397 Print means print a message if this signal happens.\n\
1398 Stop means reenter debugger if this signal happens (implies print).\n\
1399 Pass means let program see this signal; otherwise program doesn't know.\n\
1400 Pass and Stop may be combined.");
1401
1402 for (i = 0; i < NSIG; i++)
1403 {
1404 signal_stop[i] = 1;
1405 signal_print[i] = 1;
1406 signal_program[i] = 1;
1407 }
1408
1409 /* Signals caused by debugger's own actions
1410 should not be given to the program afterwards. */
1411 signal_program[SIGTRAP] = 0;
1412 signal_program[SIGINT] = 0;
1413
1414 /* Signals that are not errors should not normally enter the debugger. */
1415 #ifdef SIGALRM
1416 signal_stop[SIGALRM] = 0;
1417 signal_print[SIGALRM] = 0;
1418 #endif /* SIGALRM */
1419 #ifdef SIGVTALRM
1420 signal_stop[SIGVTALRM] = 0;
1421 signal_print[SIGVTALRM] = 0;
1422 #endif /* SIGVTALRM */
1423 #ifdef SIGPROF
1424 signal_stop[SIGPROF] = 0;
1425 signal_print[SIGPROF] = 0;
1426 #endif /* SIGPROF */
1427 #ifdef SIGCHLD
1428 signal_stop[SIGCHLD] = 0;
1429 signal_print[SIGCHLD] = 0;
1430 #endif /* SIGCHLD */
1431 #ifdef SIGCLD
1432 signal_stop[SIGCLD] = 0;
1433 signal_print[SIGCLD] = 0;
1434 #endif /* SIGCLD */
1435 #ifdef SIGIO
1436 signal_stop[SIGIO] = 0;
1437 signal_print[SIGIO] = 0;
1438 #endif /* SIGIO */
1439 #ifdef SIGURG
1440 signal_stop[SIGURG] = 0;
1441 signal_print[SIGURG] = 0;
1442 #endif /* SIGURG */
1443 }
1444
This page took 0.076654 seconds and 5 git commands to generate.