gdb-2.8.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 #include "defs.h"
22 #include "initialize.h"
23 #include "param.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "wait.h"
28
29 #include <stdio.h>
30 #include <signal.h>
31 #include <a.out.h>
32
33 #ifdef UMAX_PTRACE
34 #include <sys/param.h>
35 #include <sys/ptrace.h>
36 #endif UMAX_PTRACE
37
38 extern char *sys_siglist[];
39 extern int errno;
40
41 /* Tables of how to react to signals; the user sets them. */
42
43 static char signal_stop[NSIG];
44 static char signal_print[NSIG];
45 static char signal_program[NSIG];
46
47 /* Nonzero if breakpoints are now inserted in the inferior. */
48
49 static int breakpoints_inserted;
50
51 /* Function inferior was in as of last step command. */
52
53 static struct symbol *step_start_function;
54
55 /* This is the sequence of bytes we insert for a breakpoint. */
56
57 static char break_insn[] = BREAKPOINT;
58
59 /* Nonzero => address for special breakpoint for resuming stepping. */
60
61 static CORE_ADDR step_resume_break_address;
62
63 /* Original contents of the byte where the special breakpoint is. */
64
65 static char step_resume_break_shadow[sizeof break_insn];
66
67 /* Nonzero means the special breakpoint is a duplicate
68 so it has not itself been inserted. */
69
70 static int step_resume_break_duplicate;
71
72 /* Nonzero if we are expecting a trace trap and should proceed from it.
73 2 means expecting 2 trace traps and should continue both times.
74 That occurs when we tell sh to exec the program: we will get
75 a trap after the exec of sh and a second when the program is exec'd. */
76
77 static int trap_expected;
78
79 /* Nonzero if the next time we try to continue the inferior, it will
80 step one instruction and generate a spurious trace trap.
81 This is used to compensate for a bug in HP-UX. */
82
83 static int trap_expected_after_continue;
84
85 /* Nonzero means expecting a trace trap
86 and should stop the inferior and return silently when it happens. */
87
88 static int stop_after_trap;
89
90 /* Nonzero means expecting a trace trap due to attaching to a process. */
91
92 static int stop_after_attach;
93
94 /* Nonzero if pc has been changed by the debugger
95 since the inferior stopped. */
96
97 int pc_changed;
98
99 /* Nonzero if debugging a remote machine via a serial link or ethernet. */
100
101 int remote_debugging;
102
103 /* Save register contents here when about to pop a stack dummy frame. */
104
105 char stop_registers[REGISTER_BYTES];
106
107 /* Nonzero if program stopped due to error trying to insert breakpoints. */
108
109 static int breakpoints_failed;
110
111 /* Nonzero if inferior is in sh before our program got exec'd. */
112
113 static int running_in_shell;
114
115 /* Nonzero after stop if current stack frame should be printed. */
116
117 static int stop_print_frame;
118
119 #ifdef NO_SINGLE_STEP
120 /* Non-zero if we just simulated a single-step ptrace call. This is
121 needed because we cannot remove the breakpoints in the inferior
122 process until after the `wait' in `wait_for_inferior'.
123 Used for sun4. */
124 int one_stepped;
125 #endif /* NO_SINGLE_STEP */
126
127 static void insert_step_breakpoint ();
128 static void remove_step_breakpoint ();
129 static void wait_for_inferior ();
130 static void normal_stop ();
131
132 START_FILE
133 \f
134 /* Clear out all variables saying what to do when inferior is continued.
135 First do this, then set the ones you want, then call `proceed'. */
136
137 void
138 clear_proceed_status ()
139 {
140 trap_expected = 0;
141 step_range_start = 0;
142 step_range_end = 0;
143 step_frame = 0;
144 step_over_calls = -1;
145 step_resume_break_address = 0;
146 stop_after_trap = 0;
147 stop_after_attach = 0;
148
149 /* Discard any remaining commands left by breakpoint we had stopped at. */
150 clear_breakpoint_commands ();
151 }
152
153 /* Basic routine for continuing the program in various fashions.
154
155 ADDR is the address to resume at, or -1 for resume where stopped.
156 SIGNAL is the signal to give it, or 0 for none,
157 or -1 for act according to how it stopped.
158 STEP is nonzero if should trap after one instruction.
159 -1 means return after that and print nothing.
160 You should probably set various step_... variables
161 before calling here, if you are stepping.
162
163 You should call clear_proceed_status before calling proceed. */
164
165 void
166 proceed (addr, signal, step)
167 CORE_ADDR addr;
168 int signal;
169 int step;
170 {
171 int oneproc = 0;
172
173 if (step > 0)
174 step_start_function = find_pc_function (read_pc ());
175 if (step < 0)
176 stop_after_trap = 1;
177
178 if (addr == -1)
179 {
180 /* If there is a breakpoint at the address we will resume at,
181 step one instruction before inserting breakpoints
182 so that we do not stop right away. */
183
184 if (!pc_changed && breakpoint_here_p (read_pc ()))
185 oneproc = 1;
186 }
187 else
188 {
189 write_register (PC_REGNUM, addr);
190 #ifdef NPC_REGNUM
191 write_register (NPC_REGNUM, addr+4);
192 #endif
193 }
194
195 if (trap_expected_after_continue)
196 {
197 /* If (step == 0), a trap will be automatically generated after
198 the first instruction is executed. Force step one
199 instruction to clear this condition. This should not occur
200 if step is nonzero, but it is harmless in that case. */
201 oneproc = 1;
202 trap_expected_after_continue = 0;
203 }
204
205 if (oneproc)
206 /* We will get a trace trap after one instruction.
207 Continue it automatically and insert breakpoints then. */
208 trap_expected = 1;
209 else
210 {
211 int temp = insert_breakpoints ();
212 if (temp)
213 {
214 print_sys_errmsg ("ptrace", temp);
215 error ("Cannot insert breakpoints.\n\
216 The same program may be running in another process.");
217 }
218 breakpoints_inserted = 1;
219 }
220
221 /* Install inferior's terminal modes. */
222 terminal_inferior ();
223
224 if (signal >= 0)
225 stop_signal = signal;
226 /* If this signal should not be seen by program,
227 give it zero. Used for debugging signals. */
228 else if (stop_signal < NSIG && !signal_program[stop_signal])
229 stop_signal= 0;
230
231 /* Resume inferior. */
232 resume (oneproc || step, stop_signal);
233
234 /* Wait for it to stop (if not standalone)
235 and in any case decode why it stopped, and act accordingly. */
236
237 wait_for_inferior ();
238 normal_stop ();
239 }
240
241 /* Writing the inferior pc as a register calls this function
242 to inform infrun that the pc has been set in the debugger. */
243
244 writing_pc (val)
245 CORE_ADDR val;
246 {
247 stop_pc = val;
248 pc_changed = 1;
249 }
250
251 /* Start an inferior process for the first time.
252 Actually it was started by the fork that created it,
253 but it will have stopped one instruction after execing sh.
254 Here we must get it up to actual execution of the real program. */
255
256 start_inferior ()
257 {
258 /* We will get a trace trap after one instruction.
259 Continue it automatically. Eventually (after shell does an exec)
260 it will get another trace trap. Then insert breakpoints and continue. */
261 trap_expected = 2;
262 running_in_shell = 0; /* Set to 1 at first SIGTRAP, 0 at second. */
263 trap_expected_after_continue = 0;
264 breakpoints_inserted = 0;
265 mark_breakpoints_out ();
266
267 /* Set up the "saved terminal modes" of the inferior
268 based on what modes we are starting it with. */
269 terminal_init_inferior ();
270
271 /* Install inferior's terminal modes. */
272 terminal_inferior ();
273
274 if (remote_debugging)
275 {
276 trap_expected = 0;
277 fetch_inferior_registers();
278 set_current_frame (read_register(FP_REGNUM));
279 stop_frame = get_current_frame();
280 inferior_pid = 3;
281 if (insert_breakpoints())
282 fatal("Can't insert breakpoints");
283 breakpoints_inserted = 1;
284 proceed(-1, -1, 0);
285 }
286 else
287 {
288 wait_for_inferior ();
289 normal_stop ();
290 }
291 }
292
293 /* Start remote-debugging of a machine over a serial link. */
294
295 void
296 start_remote ()
297 {
298 clear_proceed_status ();
299 running_in_shell = 0;
300 trap_expected = 0;
301 inferior_pid = 3;
302 breakpoints_inserted = 0;
303 mark_breakpoints_out ();
304 wait_for_inferior ();
305 normal_stop();
306 }
307
308 #ifdef ATTACH_DETACH
309
310 /* Attach to process PID, then initialize for debugging it
311 and wait for the trace-trap that results from attaching. */
312
313 void
314 attach_program (pid)
315 int pid;
316 {
317 attach (pid);
318 inferior_pid = pid;
319
320 mark_breakpoints_out ();
321 terminal_init_inferior ();
322 clear_proceed_status ();
323 stop_after_attach = 1;
324 /*proceed (-1, 0, -2);*/
325 wait_for_inferior ();
326 normal_stop ();
327 }
328 #endif /* ATTACH_DETACH */
329 \f
330 /* Wait for control to return from inferior to debugger.
331 If inferior gets a signal, we may decide to start it up again
332 instead of returning. That is why there is a loop in this function.
333 When this function actually returns it means the inferior
334 should be left stopped and GDB should read more commands. */
335
336 static void
337 wait_for_inferior ()
338 {
339 register int pid;
340 WAITTYPE w;
341 CORE_ADDR pc;
342 int tem;
343 int another_trap;
344 int random_signal;
345 CORE_ADDR stop_sp;
346 int stop_step_resume_break;
347 int newmisc;
348 int newfun_pc;
349 struct symbol *newfun;
350 struct symtab_and_line sal;
351 int prev_pc;
352
353 prev_pc = read_pc ();
354
355 while (1)
356 {
357 if (remote_debugging)
358 remote_wait (&w);
359 else
360 {
361 pid = wait (&w);
362 if (pid != inferior_pid)
363 continue;
364 }
365
366 #ifdef NO_SINGLE_STEP
367 if (one_stepped)
368 {
369 single_step (0);
370 }
371 #endif /* NO_SINGLE_STEP */
372
373 pc_changed = 0;
374 fetch_inferior_registers ();
375 stop_pc = read_pc ();
376 set_current_frame (read_register (FP_REGNUM));
377 stop_frame = get_current_frame ();
378 stop_sp = read_register (SP_REGNUM);
379 another_trap = 0;
380 stop_breakpoint = 0;
381 stop_step = 0;
382 stop_stack_dummy = 0;
383 stop_print_frame = 1;
384 stop_step_resume_break = 0;
385 random_signal = 0;
386 breakpoints_failed = 0;
387
388 /* Look at the cause of the stop, and decide what to do.
389 The alternatives are:
390 1) break; to really stop and return to the debugger,
391 2) drop through to start up again
392 (set another_trap to 1 to single step once)
393 3) set random_signal to 1, and the decision between 1 and 2
394 will be made according to the signal handling tables. */
395
396 if (WIFEXITED (w))
397 {
398 terminal_ours_for_output ();
399 if (WRETCODE (w))
400 printf ("\nProgram exited with code 0%o.\n", WRETCODE (w));
401 else
402 printf ("\nProgram exited normally.\n");
403 fflush (stdout);
404 inferior_died ();
405 stop_print_frame = 0;
406 break;
407 }
408 else if (!WIFSTOPPED (w))
409 {
410 kill_inferior ();
411 stop_print_frame = 0;
412 stop_signal = WTERMSIG (w);
413 terminal_ours_for_output ();
414 printf ("\nProgram terminated with signal %d, %s\n",
415 stop_signal,
416 stop_signal < NSIG
417 ? sys_siglist[stop_signal]
418 : "(undocumented)");
419 printf ("The inferior process no longer exists.\n");
420 fflush (stdout);
421 break;
422 }
423 else
424 {
425 stop_signal = WSTOPSIG (w);
426
427 /* First, distinguish signals caused by the debugger from signals
428 that have to do with the program's own actions.
429 Note that breakpoint insns may cause SIGTRAP or SIGILL
430 or SIGEMT, depending on the operating system version.
431 Here we detect when a SIGILL or SIGEMT is really a breakpoint
432 and change it to SIGTRAP. */
433
434 if (stop_signal == SIGTRAP
435 || (breakpoints_inserted &&
436 (stop_signal == SIGILL
437 || stop_signal == SIGEMT))
438 || stop_after_attach)
439 {
440 if (stop_signal == SIGTRAP && stop_after_trap)
441 {
442 stop_print_frame = 0;
443 break;
444 }
445 if (stop_after_attach)
446 break;
447 /* Don't even think about breakpoints
448 if still running the shell that will exec the program
449 or if just proceeded over a breakpoint. */
450 if (stop_signal == SIGTRAP && trap_expected)
451 stop_breakpoint = 0;
452 else
453 /* See if there is a breakpoint at the current PC. */
454 #if DECR_PC_AFTER_BREAK
455 /* Notice the case of stepping through a jump
456 that leads just after a breakpoint.
457 Don't confuse that with hitting the breakpoint.
458 What we check for is that 1) stepping is going on
459 and 2) the pc before the last insn does not match
460 the address of the breakpoint before the current pc. */
461 if (!(prev_pc != stop_pc - DECR_PC_AFTER_BREAK
462 && step_range_end && !step_resume_break_address))
463 #endif /* DECR_PC_AFTER_BREAK not zero */
464 {
465 select_frame (stop_frame, 0); /* For condition exprs. */
466 stop_breakpoint = breakpoint_stop_status (stop_pc, stop_frame);
467 /* Following in case break condition called a function. */
468 stop_print_frame = 1;
469 if (stop_breakpoint && DECR_PC_AFTER_BREAK)
470 {
471 stop_pc -= DECR_PC_AFTER_BREAK;
472 write_register (PC_REGNUM, stop_pc);
473 #ifdef NPC_REGNUM
474 write_register (NPC_REGNUM, stop_pc + 4);
475 #endif
476 pc_changed = 0;
477 }
478 }
479 /* See if we stopped at the special breakpoint for
480 stepping over a subroutine call. */
481 if (stop_pc - DECR_PC_AFTER_BREAK == step_resume_break_address)
482 {
483 stop_step_resume_break = 1;
484 if (DECR_PC_AFTER_BREAK)
485 {
486 stop_pc -= DECR_PC_AFTER_BREAK;
487 write_register (PC_REGNUM, stop_pc);
488 #ifdef NPC_REGNUM
489 write_register (PC_REGNUM, stop_pc + 4);
490 #endif
491 pc_changed = 0;
492 }
493 }
494
495 if (stop_signal == SIGTRAP)
496 random_signal
497 = !(stop_breakpoint || trap_expected
498 || stop_step_resume_break
499 || (stop_sp INNER_THAN stop_pc && stop_pc INNER_THAN stop_frame)
500 || (step_range_end && !step_resume_break_address));
501 else
502 {
503 random_signal
504 = !(stop_breakpoint || stop_step_resume_break);
505 if (!random_signal)
506 stop_signal = SIGTRAP;
507 }
508 }
509 else
510 random_signal = 1;
511
512 /* For the program's own signals, act according to
513 the signal handling tables. */
514
515 if (random_signal
516 && !(running_in_shell && stop_signal == SIGSEGV))
517 {
518 /* Signal not for debugging purposes. */
519 int printed = 0;
520
521 if (stop_signal >= NSIG
522 || signal_print[stop_signal])
523 {
524 printed = 1;
525 terminal_ours_for_output ();
526 printf ("\nProgram received signal %d, %s\n",
527 stop_signal,
528 stop_signal < NSIG
529 ? sys_siglist[stop_signal]
530 : "(undocumented)");
531 fflush (stdout);
532 }
533 if (stop_signal >= NSIG
534 || signal_stop[stop_signal])
535 break;
536 /* If not going to stop, give terminal back
537 if we took it away. */
538 else if (printed)
539 terminal_inferior ();
540 }
541
542 /* Handle cases caused by hitting a breakpoint. */
543
544 if (!random_signal
545 && (stop_breakpoint || stop_step_resume_break))
546 {
547 /* Does a breakpoint want us to stop? */
548 if (stop_breakpoint && stop_breakpoint != -1)
549 {
550 /* 0x1000000 is set in stop_breakpoint as returned by
551 breakpoint_status_p to indicate a silent breakpoint. */
552 if (stop_breakpoint > 0 && stop_breakpoint & 0x1000000)
553 {
554 stop_breakpoint &= ~0x1000000;
555 stop_print_frame = 0;
556 }
557 break;
558 }
559 /* But if we have hit the step-resumption breakpoint,
560 remove it. It has done its job getting us here. */
561 if (stop_step_resume_break
562 && (step_frame == 0 || stop_frame == step_frame))
563 {
564 remove_step_breakpoint ();
565 step_resume_break_address = 0;
566 }
567 /* Otherwise, must remove breakpoints and single-step
568 to get us past the one we hit. */
569 else
570 {
571 remove_breakpoints ();
572 remove_step_breakpoint ();
573 breakpoints_inserted = 0;
574 another_trap = 1;
575 }
576
577 /* We come here if we hit a breakpoint but should not
578 stop for it. Possibly we also were stepping
579 and should stop for that. So fall through and
580 test for stepping. But, if not stepping,
581 do not stop. */
582 }
583
584 /* If this is the breakpoint at the end of a stack dummy,
585 just stop silently. */
586 if (stop_sp INNER_THAN stop_pc && stop_pc INNER_THAN stop_frame)
587 {
588 stop_print_frame = 0;
589 stop_stack_dummy = 1;
590 #ifdef HP9K320
591 trap_expected_after_continue = 1;
592 #endif
593 break;
594 }
595
596 if (step_resume_break_address)
597 /* Having a step-resume breakpoint overrides anything
598 else having to do with stepping commands until
599 that breakpoint is reached. */
600 ;
601 /* If stepping through a line, keep going if still within it. */
602 else if (!random_signal
603 && step_range_end
604 && stop_pc >= step_range_start
605 && stop_pc < step_range_end)
606 {
607 /* Don't step through the return from a function
608 unless that is the first instruction stepped through. */
609 if (ABOUT_TO_RETURN (stop_pc))
610 {
611 stop_step = 1;
612 break;
613 }
614 }
615
616 /* We stepped out of the stepping range. See if that was due
617 to a subroutine call that we should proceed to the end of. */
618 else if (!random_signal && step_range_end)
619 {
620 newfun = find_pc_function (stop_pc);
621 newmisc = -1;
622 if (newfun)
623 {
624 newfun_pc = BLOCK_START (SYMBOL_BLOCK_VALUE (newfun))
625 + FUNCTION_START_OFFSET;
626 }
627 else
628 {
629 newmisc = find_pc_misc_function (stop_pc);
630 if (newmisc >= 0)
631 newfun_pc = misc_function_vector[newmisc].address
632 + FUNCTION_START_OFFSET;
633 else newfun_pc = 0;
634 }
635 if (stop_pc == newfun_pc
636 && (step_over_calls > 0 || (step_over_calls && newfun == 0)))
637 {
638 /* A subroutine call has happened. */
639 /* Set a special breakpoint after the return */
640 step_resume_break_address = SAVED_PC_AFTER_CALL (stop_frame);
641 step_resume_break_duplicate
642 = breakpoint_here_p (step_resume_break_address);
643 if (breakpoints_inserted)
644 insert_step_breakpoint ();
645 }
646 /* Subroutine call with source code we should not step over.
647 Do step to the first line of code in it. */
648 else if (stop_pc == newfun_pc && step_over_calls)
649 {
650 SKIP_PROLOGUE (newfun_pc);
651 sal = find_pc_line (newfun_pc, 0);
652 /* Use the step_resume_break to step until
653 the end of the prologue, even if that involves jumps
654 (as it seems to on the vax under 4.2). */
655 /* If the prologue ends in the middle of a source line,
656 continue to the end of that source line.
657 Otherwise, just go to end of prologue. */
658 if (sal.end && sal.pc != newfun_pc)
659 newfun_pc = sal.end;
660
661 if (newfun_pc == stop_pc)
662 /* We are already there: stop now. */
663 stop_step = 1;
664 else
665 /* Put the step-breakpoint there and go until there. */
666 {
667 step_resume_break_address = newfun_pc;
668
669 step_resume_break_duplicate
670 = breakpoint_here_p (step_resume_break_address);
671 if (breakpoints_inserted)
672 insert_step_breakpoint ();
673 /* Do not specify what the fp should be when we stop
674 since on some machines the prologue
675 is where the new fp value is established. */
676 step_frame = 0;
677 /* And make sure stepping stops right away then. */
678 step_range_end = step_range_start;
679 }
680 }
681 /* No subroutince call; stop now. */
682 else
683 {
684 stop_step = 1;
685 break;
686 }
687 }
688 }
689
690 /* Save the pc before execution, to compare with pc after stop. */
691 prev_pc = read_pc ();
692
693 /* If we did not do break;, it means we should keep
694 running the inferior and not return to debugger. */
695
696 /* If trap_expected is 2, it means continue once more
697 and insert breakpoints at the next trap.
698 If trap_expected is 1 and the signal was SIGSEGV, it means
699 the shell is doing some memory allocation--just resume it
700 with SIGSEGV.
701 Otherwise insert breakpoints now, and possibly single step. */
702
703 if (trap_expected > 1)
704 {
705 trap_expected--;
706 running_in_shell = 1;
707 resume (0, 0);
708 }
709 else if (running_in_shell && stop_signal == SIGSEGV)
710 {
711 resume (0, SIGSEGV);
712 }
713 else
714 {
715 /* Here, we are not awaiting another exec to get
716 the program we really want to debug.
717 Insert breakpoints now, unless we are trying
718 to one-proceed past a breakpoint. */
719 running_in_shell = 0;
720 if (!breakpoints_inserted && !another_trap)
721 {
722 insert_step_breakpoint ();
723 breakpoints_failed = insert_breakpoints ();
724 if (breakpoints_failed)
725 break;
726 breakpoints_inserted = 1;
727 }
728
729 trap_expected = another_trap;
730
731 if (stop_signal == SIGTRAP)
732 stop_signal = 0;
733
734 resume ((step_range_end && !step_resume_break_address)
735 || trap_expected,
736 stop_signal);
737 }
738 }
739 }
740 \f
741 /* Here to return control to GDB when the inferior stops for real.
742 Print appropriate messages, remove breakpoints, give terminal our modes.
743
744 RUNNING_IN_SHELL nonzero means the shell got a signal before
745 exec'ing the program we wanted to run.
746 STOP_PRINT_FRAME nonzero means print the executing frame
747 (pc, function, args, file, line number and line text).
748 BREAKPOINTS_FAILED nonzero means stop was due to error
749 attempting to insert breakpoints. */
750
751 static void
752 normal_stop ()
753 {
754 if (breakpoints_failed)
755 {
756 terminal_ours_for_output ();
757 print_sys_errmsg ("ptrace", breakpoints_failed);
758 printf ("Stopped; cannot insert breakpoints.\n\
759 The same program may be running in another process.\n");
760 }
761
762 if (inferior_pid)
763 remove_step_breakpoint ();
764
765 if (inferior_pid && breakpoints_inserted)
766 if (remove_breakpoints ())
767 {
768 terminal_ours_for_output ();
769 printf ("Cannot remove breakpoints because program is no longer writable.\n\
770 It must be running in another process.\n\
771 Further execution is probably impossible.\n");
772 }
773
774 breakpoints_inserted = 0;
775
776 /* Delete the breakpoint we stopped at, if it wants to be deleted.
777 Delete any breakpoint that is to be deleted at the next stop. */
778
779 breakpoint_auto_delete (stop_breakpoint);
780
781 /* If an auto-display called a function and that got a signal,
782 delete that auto-display to avoid an infinite recursion. */
783
784 delete_current_display ();
785
786 if (step_multi && stop_step)
787 return;
788
789 terminal_ours ();
790
791 if (running_in_shell)
792 {
793 if (stop_signal == SIGSEGV)
794 printf ("\
795 You have just encountered a bug in \"sh\". GDB starts your program\n\
796 by running \"sh\" with a command to exec your program.\n\
797 This is so that \"sh\" will process wildcards and I/O redirection.\n\
798 This time, \"sh\" crashed.\n\
799 \n\
800 One known bug in \"sh\" bites when the environment takes up a lot of space.\n\
801 Try \"info env\" to see the environment; then use \"unset-env\" to kill\n\
802 some variables whose values are large; then do \"run\" again.\n\
803 \n\
804 If that works, you might want to put those \"unset-env\" commands\n\
805 into a \".gdbinit\" file in this directory so they will happen every time.\n");
806 /* Don't confuse user with his program's symbols on sh's data. */
807 stop_print_frame = 0;
808 }
809
810 if (inferior_pid == 0)
811 return;
812
813 /* Select innermost stack frame except on return from a stack dummy routine,
814 or if the program has exited. */
815 if (!stop_stack_dummy)
816 {
817 select_frame (stop_frame, 0);
818
819 if (stop_print_frame)
820 {
821 if (stop_breakpoint > 0)
822 printf ("\nBpt %d, ", stop_breakpoint);
823 print_sel_frame (stop_step
824 && step_frame == stop_frame
825 && step_start_function == find_pc_function (stop_pc));
826 /* Display the auto-display expressions. */
827 do_displays ();
828 }
829 }
830
831 /* Save the function value return registers
832 We might be about to restore their previous contents. */
833 read_register_bytes (0, stop_registers, REGISTER_BYTES);
834
835 if (stop_stack_dummy)
836 {
837 /* Pop the empty frame that contains the stack dummy. */
838 POP_FRAME;
839 select_frame (read_register (FP_REGNUM), 0);
840 }
841 }
842 \f
843 static void
844 insert_step_breakpoint ()
845 {
846 if (step_resume_break_address && !step_resume_break_duplicate)
847 {
848 read_memory (step_resume_break_address,
849 step_resume_break_shadow, sizeof break_insn);
850 write_memory (step_resume_break_address,
851 break_insn, sizeof break_insn);
852 }
853 }
854
855 static void
856 remove_step_breakpoint ()
857 {
858 if (step_resume_break_address && !step_resume_break_duplicate)
859 write_memory (step_resume_break_address, step_resume_break_shadow,
860 sizeof break_insn);
861 }
862 \f
863 /* Specify how various signals in the inferior should be handled. */
864
865 static void
866 handle_command (args, from_tty)
867 char *args;
868 int from_tty;
869 {
870 register char *p = args;
871 int signum;
872 register int digits, wordlen;
873
874 if (!args)
875 error_no_arg ("signal to handle");
876
877 while (*p)
878 {
879 /* Find the end of the next word in the args. */
880 for (wordlen = 0; p[wordlen] && p[wordlen] != ' ' && p[wordlen] != '\t';
881 wordlen++);
882 for (digits = 0; p[digits] >= '0' && p[digits] <= '9'; digits++);
883
884 /* If it is all digits, it is signal number to operate on. */
885 if (digits == wordlen)
886 {
887 signum = atoi (p);
888 if (signum == SIGTRAP || signum == SIGINT)
889 {
890 if (!query ("Signal %d is used by the debugger.\nAre you sure you want to change it? ", signum))
891 error ("Not confirmed.");
892 }
893 }
894 else if (signum == 0)
895 error ("First argument is not a signal number.");
896
897 /* Else, if already got a signal number, look for flag words
898 saying what to do for it. */
899 else if (!strncmp (p, "stop", wordlen))
900 {
901 signal_stop[signum] = 1;
902 signal_print[signum] = 1;
903 }
904 else if (wordlen >= 2 && !strncmp (p, "print", wordlen))
905 signal_print[signum] = 1;
906 else if (wordlen >= 2 && !strncmp (p, "pass", wordlen))
907 signal_program[signum] = 1;
908 else if (!strncmp (p, "ignore", wordlen))
909 signal_program[signum] = 0;
910 else if (wordlen >= 3 && !strncmp (p, "nostop", wordlen))
911 signal_stop[signum] = 0;
912 else if (wordlen >= 4 && !strncmp (p, "noprint", wordlen))
913 {
914 signal_print[signum] = 0;
915 signal_stop[signum] = 0;
916 }
917 else if (wordlen >= 4 && !strncmp (p, "nopass", wordlen))
918 signal_program[signum] = 0;
919 else if (wordlen >= 3 && !strncmp (p, "noignore", wordlen))
920 signal_program[signum] = 1;
921 /* Not a number and not a recognized flag word => complain. */
922 else
923 {
924 p[wordlen] = 0;
925 error ("Unrecognized flag word: \"%s\".", p);
926 }
927
928 /* Find start of next word. */
929 p += wordlen;
930 while (*p == ' ' || *p == '\t') p++;
931 }
932
933 if (from_tty)
934 {
935 /* Show the results. */
936 printf ("Number\tStop\tPrint\tPass to program\tDescription\n");
937 printf ("%d\t", signum);
938 printf ("%s\t", signal_stop[signum] ? "Yes" : "No");
939 printf ("%s\t", signal_print[signum] ? "Yes" : "No");
940 printf ("%s\t\t", signal_program[signum] ? "Yes" : "No");
941 printf ("%s\n", sys_siglist[signum]);
942 }
943 }
944
945 /* Print current contents of the tables set by the handle command. */
946
947 static void
948 signals_info (signum_exp)
949 char *signum_exp;
950 {
951 register int i;
952 printf ("Number\tStop\tPrint\tPass to program\tDescription\n");
953
954 if (signum_exp)
955 {
956 i = parse_and_eval_address (signum_exp);
957 printf ("%d\t", i);
958 printf ("%s\t", signal_stop[i] ? "Yes" : "No");
959 printf ("%s\t", signal_print[i] ? "Yes" : "No");
960 printf ("%s\t\t", signal_program[i] ? "Yes" : "No");
961 printf ("%s\n", sys_siglist[i]);
962 return;
963 }
964
965 printf ("\n");
966 for (i = 0; i < NSIG; i++)
967 {
968 QUIT;
969 if (i > 0 && i % 16 == 0)
970 {
971 printf ("[Type Return to see more]");
972 fflush (stdout);
973 read_line ();
974 }
975 printf ("%d\t", i);
976 printf ("%s\t", signal_stop[i] ? "Yes" : "No");
977 printf ("%s\t", signal_print[i] ? "Yes" : "No");
978 printf ("%s\t\t", signal_program[i] ? "Yes" : "No");
979 printf ("%s\n", sys_siglist[i]);
980 }
981
982 printf ("\nUse the \"handle\" command to change these tables.\n");
983 }
984 \f
985 static
986 initialize ()
987 {
988 register int i;
989
990 add_info ("signals", signals_info,
991 "What debugger does when program gets various signals.\n\
992 Specify a signal number as argument to print info on that signal only.");
993
994 add_com ("handle", class_run, handle_command,
995 "Specify how to handle a signal.\n\
996 Args are signal number followed by flags.\n\
997 Flags allowed are \"stop\", \"print\", \"pass\",\n\
998 \"nostop\", \"noprint\" or \"nopass\".\n\
999 Print means print a message if this signal happens.\n\
1000 Stop means reenter debugger if this signal happens (implies print).\n\
1001 Pass means let program see this signal; otherwise program doesn't know.\n\
1002 Pass and Stop may be combined.");
1003
1004 for (i = 0; i < NSIG; i++)
1005 {
1006 signal_stop[i] = 1;
1007 signal_print[i] = 1;
1008 signal_program[i] = 1;
1009 }
1010
1011 /* Signals caused by debugger's own actions
1012 should not be given to the program afterwards. */
1013 signal_program[SIGTRAP] = 0;
1014 signal_program[SIGINT] = 0;
1015
1016 /* Signals that are not errors should not normally enter the debugger. */
1017 #ifdef SIGALRM
1018 signal_stop[SIGALRM] = 0;
1019 signal_print[SIGALRM] = 0;
1020 #endif /* SIGALRM */
1021 #ifdef SIGVTALRM
1022 signal_stop[SIGVTALRM] = 0;
1023 signal_print[SIGVTALRM] = 0;
1024 #endif /* SIGVTALRM */
1025 #ifdef SIGPROF
1026 signal_stop[SIGPROF] = 0;
1027 signal_print[SIGPROF] = 0;
1028 #endif /* SIGPROF */
1029 #ifdef SIGCHLD
1030 signal_stop[SIGCHLD] = 0;
1031 signal_print[SIGCHLD] = 0;
1032 #endif /* SIGCHLD */
1033 #ifdef SIGCLD
1034 signal_stop[SIGCLD] = 0;
1035 signal_print[SIGCLD] = 0;
1036 #endif /* SIGCLD */
1037 #ifdef SIGIO
1038 signal_stop[SIGIO] = 0;
1039 signal_print[SIGIO] = 0;
1040 #endif /* SIGIO */
1041 #ifdef SIGURG
1042 signal_stop[SIGURG] = 0;
1043 signal_print[SIGURG] = 0;
1044 #endif /* SIGURG */
1045 }
1046
1047 #ifdef NO_SINGLE_STEP
1048 /* This code was written by Gary Beihl (beihl@mcc.com).
1049 It was modified by Michael Tiemann (tiemann@corto.inria.fr). */
1050
1051 /* Simulate single-step ptrace call for sun4. */
1052
1053 typedef enum
1054 {
1055 b_error, not_branch, bicc, bicca, ba, baa, ticc, ta,
1056 } branch_type;
1057
1058 static CORE_ADDR next_pc, pc8, target;
1059 static int brkpc8, brktrg;
1060 typedef char binsn_quantum[sizeof break_insn];
1061 static binsn_quantum break_mem[3];
1062
1063 int
1064 single_step (signal)
1065 int signal;
1066 {
1067 branch_type br, isabranch();
1068
1069 next_pc = read_register (NPC_REGNUM);
1070 pc8 = read_register (PC_REGNUM) + 8; /* branch not taken */
1071
1072 if (!one_stepped)
1073 {
1074 /* Always set breakpoint for NPC. */
1075 read_memory (next_pc, break_mem[0], sizeof break_insn);
1076 write_memory (next_pc, break_insn, sizeof break_insn);
1077
1078 /* printf ("set break at %x\n",next_pc); */
1079 br = isabranch (pc8 - 8, &target);
1080 brkpc8 = brktrg = 0;
1081
1082 if (br == bicca && pc8 != next_pc)
1083 {
1084 /* Handle branches with care */
1085 brkpc8 = 1;
1086 read_memory (pc8, break_mem[1], sizeof break_insn);
1087 write_memory (pc8, break_insn, sizeof break_insn);
1088 }
1089 else if (br == baa && target != next_pc)
1090 {
1091 brktrg = 1;
1092 read_memory (target, break_mem[2], sizeof break_insn);
1093 write_memory (target, break_insn, sizeof break_insn);
1094 }
1095
1096 /* Let it go */
1097 ptrace (7, inferior_pid, 1, signal);
1098 one_stepped = 1;
1099 return;
1100 }
1101 else
1102 {
1103 /* Remove breakpoints */
1104 write_memory (next_pc, break_mem[0], sizeof break_insn);
1105
1106 if (brkpc8)
1107 {
1108 write_memory (pc8, break_mem[1], sizeof break_insn);
1109 }
1110 if (brktrg)
1111 {
1112 write_memory (target, break_mem[2], sizeof break_insn);
1113 }
1114 one_stepped = 0;
1115 }
1116 }
1117
1118 #endif /* NO_SINGLE_STEP */
1119
1120 static int save_insn_opcodes[] = { 0x03000000, 0x82007ee0, 0x9de38001, 0x03000000, 0x82007ee0, 0x91d02001, 0x01000000 };
1121
1122 void
1123 do_save_insn (size)
1124 int size;
1125 {
1126 int g1 = read_register (1);
1127 CORE_ADDR sp = read_register (SP_REGNUM);
1128 CORE_ADDR pc = read_register (PC_REGNUM);
1129 #ifdef NPC_REGNUM
1130 CORE_ADDR npc = read_register (NPC_REGNUM);
1131 #endif
1132 CORE_ADDR fake_pc = sp - sizeof (save_insn_opcodes);
1133 save_insn_opcodes[0] = 0x03000000 | ((-size >> 12) & 0x3fffff);
1134 save_insn_opcodes[1] = 0x82006000 | (-size & 0x3ff);
1135 save_insn_opcodes[3] = 0x03000000 | ((g1 >> 12) & 0x3fffff);
1136 save_insn_opcodes[4] = 0x82006000 | (g1 & 0x3ff);
1137 write_memory (fake_pc, save_insn_opcodes, sizeof (save_insn_opcodes));
1138 clear_proceed_status ();
1139 stop_after_trap = 1;
1140
1141 proceed (fake_pc, 0, 0);
1142
1143 write_register (PC_REGNUM, pc);
1144 #ifdef NPC_REGNUM
1145 write_register (NPC_REGNUM, npc);
1146 #endif
1147 }
1148
1149 static int restore_insn_opcodes[] = { 0x81e80000, 0x91d02001, 0x01000000 };
1150
1151 void
1152 do_restore_insn (raw_buffer)
1153 char raw_buffer[];
1154 {
1155 CORE_ADDR pc = read_memory_integer (*(int *)&raw_buffer[REGISTER_BYTE (PC_REGNUM)], 4);
1156 CORE_ADDR sp = read_register (SP_REGNUM);
1157 #ifdef NPC_REGNUM
1158 CORE_ADDR npc = *(int *)&raw_buffer[REGISTER_BYTE (NPC_REGNUM)] != 0
1159 ? read_memory_integer (*(int *)&raw_buffer[REGISTER_BYTE (NPC_REGNUM)], 4) : pc + 4;
1160 #endif
1161 CORE_ADDR fake_pc = sp - sizeof (restore_insn_opcodes);
1162 int saved_stop_stack_dummy = stop_stack_dummy;
1163
1164 if (*(int *)&raw_buffer[REGISTER_BYTE (PC_REGNUM)] == 0)
1165 abort ();
1166
1167 write_memory (fake_pc, restore_insn_opcodes, sizeof (restore_insn_opcodes));
1168 clear_proceed_status ();
1169 stop_after_trap = 1;
1170
1171 proceed (fake_pc, 0, 0);
1172
1173 stop_stack_dummy = saved_stop_stack_dummy;
1174 write_register (PC_REGNUM, pc);
1175 #ifdef NPC_REGNUM
1176 write_register (NPC_REGNUM, npc);
1177 #endif
1178
1179 /* Select innermost stack frame except on return from a stack dummy routine,
1180 or if the program has exited. */
1181 if (!stop_stack_dummy)
1182 {
1183 select_frame (stop_frame, 0);
1184 }
1185 else
1186 {
1187 select_frame (read_register (FP_REGNUM), 0);
1188 }
1189 }
1190
1191 END_FILE
This page took 1.596512 seconds and 5 git commands to generate.