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