]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/infrun.c
c4e58ce949b87a377b8277fac8ad3d5460ed6df5
[thirdparty/binutils-gdb.git] / gdb / infrun.c
1 /* Target-struct-independent code to start (run) and stop an inferior process.
2 Copyright 1986, 1987, 1988, 1989, 1991, 1992, 1993
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
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 starts 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 bug-gdb@prep.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
105 #include "defs.h"
106 #include <string.h>
107 #include <ctype.h>
108 #include "symtab.h"
109 #include "frame.h"
110 #include "inferior.h"
111 #include "breakpoint.h"
112 #include "wait.h"
113 #include "gdbcore.h"
114 #include "gdbcmd.h"
115 #include "target.h"
116
117 #include <signal.h>
118
119 /* unistd.h is needed to #define X_OK */
120 #ifdef USG
121 #include <unistd.h>
122 #else
123 #include <sys/file.h>
124 #endif
125
126 /* Prototypes for local functions */
127
128 static void
129 signals_info PARAMS ((char *, int));
130
131 static void
132 handle_command PARAMS ((char *, int));
133
134 static void
135 sig_print_info PARAMS ((int));
136
137 static void
138 sig_print_header PARAMS ((void));
139
140 static void
141 resume_cleanups PARAMS ((int));
142
143 static int
144 hook_stop_stub PARAMS ((char *));
145
146 /* GET_LONGJMP_TARGET returns the PC at which longjmp() will resume the
147 program. It needs to examine the jmp_buf argument and extract the PC
148 from it. The return value is non-zero on success, zero otherwise. */
149 #ifndef GET_LONGJMP_TARGET
150 #define GET_LONGJMP_TARGET(PC_ADDR) 0
151 #endif
152
153
154 /* Some machines have trampoline code that sits between function callers
155 and the actual functions themselves. If this machine doesn't have
156 such things, disable their processing. */
157 #ifndef SKIP_TRAMPOLINE_CODE
158 #define SKIP_TRAMPOLINE_CODE(pc) 0
159 #endif
160
161 /* For SVR4 shared libraries, each call goes through a small piece of
162 trampoline code in the ".init" section. IN_SOLIB_TRAMPOLINE evaluates
163 to nonzero if we are current stopped in one of these. */
164 #ifndef IN_SOLIB_TRAMPOLINE
165 #define IN_SOLIB_TRAMPOLINE(pc,name) 0
166 #endif
167
168 /* On some systems, the PC may be left pointing at an instruction that won't
169 actually be executed. This is usually indicated by a bit in the PSW. If
170 we find ourselves in such a state, then we step the target beyond the
171 nullified instruction before returning control to the user so as to avoid
172 confusion. */
173
174 #ifndef INSTRUCTION_NULLIFIED
175 #define INSTRUCTION_NULLIFIED 0
176 #endif
177
178 /* Tables of how to react to signals; the user sets them. */
179
180 static unsigned char *signal_stop;
181 static unsigned char *signal_print;
182 static unsigned char *signal_program;
183
184 #define SET_SIGS(nsigs,sigs,flags) \
185 do { \
186 int signum = (nsigs); \
187 while (signum-- > 0) \
188 if ((sigs)[signum]) \
189 (flags)[signum] = 1; \
190 } while (0)
191
192 #define UNSET_SIGS(nsigs,sigs,flags) \
193 do { \
194 int signum = (nsigs); \
195 while (signum-- > 0) \
196 if ((sigs)[signum]) \
197 (flags)[signum] = 0; \
198 } while (0)
199
200
201 /* Command list pointer for the "stop" placeholder. */
202
203 static struct cmd_list_element *stop_command;
204
205 /* Nonzero if breakpoints are now inserted in the inferior. */
206
207 static int breakpoints_inserted;
208
209 /* Function inferior was in as of last step command. */
210
211 static struct symbol *step_start_function;
212
213 /* Nonzero if we are expecting a trace trap and should proceed from it. */
214
215 static int trap_expected;
216
217 /* Nonzero if the next time we try to continue the inferior, it will
218 step one instruction and generate a spurious trace trap.
219 This is used to compensate for a bug in HP-UX. */
220
221 static int trap_expected_after_continue;
222
223 /* Nonzero means expecting a trace trap
224 and should stop the inferior and return silently when it happens. */
225
226 int stop_after_trap;
227
228 /* Nonzero means expecting a trap and caller will handle it themselves.
229 It is used after attach, due to attaching to a process;
230 when running in the shell before the child program has been exec'd;
231 and when running some kinds of remote stuff (FIXME?). */
232
233 int stop_soon_quietly;
234
235 /* Nonzero if proceed is being used for a "finish" command or a similar
236 situation when stop_registers should be saved. */
237
238 int proceed_to_finish;
239
240 /* Save register contents here when about to pop a stack dummy frame,
241 if-and-only-if proceed_to_finish is set.
242 Thus this contains the return value from the called function (assuming
243 values are returned in a register). */
244
245 char stop_registers[REGISTER_BYTES];
246
247 /* Nonzero if program stopped due to error trying to insert breakpoints. */
248
249 static int breakpoints_failed;
250
251 /* Nonzero after stop if current stack frame should be printed. */
252
253 static int stop_print_frame;
254
255 #ifdef NO_SINGLE_STEP
256 extern int one_stepped; /* From machine dependent code */
257 extern void single_step (); /* Same. */
258 #endif /* NO_SINGLE_STEP */
259
260 \f
261 /* Things to clean up if we QUIT out of resume (). */
262 /* ARGSUSED */
263 static void
264 resume_cleanups (arg)
265 int arg;
266 {
267 normal_stop ();
268 }
269
270 /* Resume the inferior, but allow a QUIT. This is useful if the user
271 wants to interrupt some lengthy single-stepping operation
272 (for child processes, the SIGINT goes to the inferior, and so
273 we get a SIGINT random_signal, but for remote debugging and perhaps
274 other targets, that's not true).
275
276 STEP nonzero if we should step (zero to continue instead).
277 SIG is the signal to give the inferior (zero for none). */
278 void
279 resume (step, sig)
280 int step;
281 int sig;
282 {
283 struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
284 QUIT;
285
286 #ifdef CANNOT_STEP_BREAKPOINT
287 /* Most targets can step a breakpoint instruction, thus executing it
288 normally. But if this one cannot, just continue and we will hit
289 it anyway. */
290 if (step && breakpoints_inserted && breakpoint_here_p (read_pc ()))
291 step = 0;
292 #endif
293
294 #ifdef NO_SINGLE_STEP
295 if (step) {
296 single_step(sig); /* Do it the hard way, w/temp breakpoints */
297 step = 0; /* ...and don't ask hardware to do it. */
298 }
299 #endif
300
301 /* Handle any optimized stores to the inferior NOW... */
302 #ifdef DO_DEFERRED_STORES
303 DO_DEFERRED_STORES;
304 #endif
305
306 /* Install inferior's terminal modes. */
307 target_terminal_inferior ();
308
309 target_resume (-1, step, sig);
310 discard_cleanups (old_cleanups);
311 }
312
313 \f
314 /* Clear out all variables saying what to do when inferior is continued.
315 First do this, then set the ones you want, then call `proceed'. */
316
317 void
318 clear_proceed_status ()
319 {
320 trap_expected = 0;
321 step_range_start = 0;
322 step_range_end = 0;
323 step_frame_address = 0;
324 step_over_calls = -1;
325 stop_after_trap = 0;
326 stop_soon_quietly = 0;
327 proceed_to_finish = 0;
328 breakpoint_proceeded = 1; /* We're about to proceed... */
329
330 /* Discard any remaining commands or status from previous stop. */
331 bpstat_clear (&stop_bpstat);
332 }
333
334 /* Basic routine for continuing the program in various fashions.
335
336 ADDR is the address to resume at, or -1 for resume where stopped.
337 SIGGNAL is the signal to give it, or 0 for none,
338 or -1 for act according to how it stopped.
339 STEP is nonzero if should trap after one instruction.
340 -1 means return after that and print nothing.
341 You should probably set various step_... variables
342 before calling here, if you are stepping.
343
344 You should call clear_proceed_status before calling proceed. */
345
346 void
347 proceed (addr, siggnal, step)
348 CORE_ADDR addr;
349 int siggnal;
350 int step;
351 {
352 int oneproc = 0;
353
354 if (step > 0)
355 step_start_function = find_pc_function (read_pc ());
356 if (step < 0)
357 stop_after_trap = 1;
358
359 if (addr == (CORE_ADDR)-1)
360 {
361 /* If there is a breakpoint at the address we will resume at,
362 step one instruction before inserting breakpoints
363 so that we do not stop right away. */
364
365 if (breakpoint_here_p (read_pc ()))
366 oneproc = 1;
367 }
368 else
369 write_pc (addr);
370
371 if (trap_expected_after_continue)
372 {
373 /* If (step == 0), a trap will be automatically generated after
374 the first instruction is executed. Force step one
375 instruction to clear this condition. This should not occur
376 if step is nonzero, but it is harmless in that case. */
377 oneproc = 1;
378 trap_expected_after_continue = 0;
379 }
380
381 if (oneproc)
382 /* We will get a trace trap after one instruction.
383 Continue it automatically and insert breakpoints then. */
384 trap_expected = 1;
385 else
386 {
387 int temp = insert_breakpoints ();
388 if (temp)
389 {
390 print_sys_errmsg ("ptrace", temp);
391 error ("Cannot insert breakpoints.\n\
392 The same program may be running in another process.");
393 }
394 breakpoints_inserted = 1;
395 }
396
397 if (siggnal >= 0)
398 stop_signal = siggnal;
399 /* If this signal should not be seen by program,
400 give it zero. Used for debugging signals. */
401 else if (stop_signal < NSIG && !signal_program[stop_signal])
402 stop_signal= 0;
403
404 /* Resume inferior. */
405 resume (oneproc || step || bpstat_should_step (), stop_signal);
406
407 /* Wait for it to stop (if not standalone)
408 and in any case decode why it stopped, and act accordingly. */
409
410 wait_for_inferior ();
411 normal_stop ();
412 }
413
414 /* Record the pc and sp of the program the last time it stopped.
415 These are just used internally by wait_for_inferior, but need
416 to be preserved over calls to it and cleared when the inferior
417 is started. */
418 static CORE_ADDR prev_pc;
419 static CORE_ADDR prev_sp;
420 static CORE_ADDR prev_func_start;
421 static char *prev_func_name;
422
423 \f
424 /* Start remote-debugging of a machine over a serial link. */
425
426 void
427 start_remote ()
428 {
429 init_wait_for_inferior ();
430 clear_proceed_status ();
431 stop_soon_quietly = 1;
432 trap_expected = 0;
433 wait_for_inferior ();
434 normal_stop ();
435 }
436
437 /* Initialize static vars when a new inferior begins. */
438
439 void
440 init_wait_for_inferior ()
441 {
442 /* These are meaningless until the first time through wait_for_inferior. */
443 prev_pc = 0;
444 prev_sp = 0;
445 prev_func_start = 0;
446 prev_func_name = NULL;
447
448 trap_expected_after_continue = 0;
449 breakpoints_inserted = 0;
450 breakpoint_init_inferior ();
451 stop_signal = 0; /* Don't confuse first call to proceed(). */
452 }
453
454 static void
455 delete_breakpoint_current_contents (arg)
456 PTR arg;
457 {
458 struct breakpoint **breakpointp = (struct breakpoint **)arg;
459 if (*breakpointp != NULL)
460 delete_breakpoint (*breakpointp);
461 }
462 \f
463 /* Wait for control to return from inferior to debugger.
464 If inferior gets a signal, we may decide to start it up again
465 instead of returning. That is why there is a loop in this function.
466 When this function actually returns it means the inferior
467 should be left stopped and GDB should read more commands. */
468
469 void
470 wait_for_inferior ()
471 {
472 struct cleanup *old_cleanups;
473 WAITTYPE w;
474 int another_trap;
475 int random_signal;
476 CORE_ADDR stop_sp = 0;
477 CORE_ADDR stop_func_start;
478 CORE_ADDR stop_func_end;
479 char *stop_func_name;
480 CORE_ADDR prologue_pc = 0, tmp;
481 struct symtab_and_line sal;
482 int remove_breakpoints_on_following_step = 0;
483 int current_line;
484 int handling_longjmp = 0; /* FIXME */
485 struct breakpoint *step_resume_breakpoint = NULL;
486 int pid;
487
488 old_cleanups = make_cleanup (delete_breakpoint_current_contents,
489 &step_resume_breakpoint);
490 sal = find_pc_line(prev_pc, 0);
491 current_line = sal.line;
492
493 /* Are we stepping? */
494 #define CURRENTLY_STEPPING() ((step_resume_breakpoint == NULL \
495 && !handling_longjmp \
496 && (step_range_end \
497 || trap_expected)) \
498 || bpstat_should_step ())
499
500 while (1)
501 {
502 /* Clean up saved state that will become invalid. */
503 flush_cached_frames ();
504 registers_changed ();
505
506 pid = target_wait (-1, &w);
507
508 #ifdef SIGTRAP_STOP_AFTER_LOAD
509
510 /* Somebody called load(2), and it gave us a "trap signal after load".
511 Ignore it gracefully. */
512
513 SIGTRAP_STOP_AFTER_LOAD (w);
514 #endif
515
516 /* See if the process still exists; clean up if it doesn't. */
517 if (WIFEXITED (w))
518 {
519 target_terminal_ours (); /* Must do this before mourn anyway */
520 if (WEXITSTATUS (w))
521 printf_filtered ("\nProgram exited with code 0%o.\n",
522 (unsigned int)WEXITSTATUS (w));
523 else
524 if (!batch_mode())
525 printf_filtered ("\nProgram exited normally.\n");
526 fflush (stdout);
527 target_mourn_inferior ();
528 #ifdef NO_SINGLE_STEP
529 one_stepped = 0;
530 #endif
531 stop_print_frame = 0;
532 break;
533 }
534 else if (!WIFSTOPPED (w))
535 {
536 char *signame;
537
538 stop_print_frame = 0;
539 stop_signal = WTERMSIG (w);
540 target_terminal_ours (); /* Must do this before mourn anyway */
541 target_kill (); /* kill mourns as well */
542 #ifdef PRINT_RANDOM_SIGNAL
543 printf_filtered ("\nProgram terminated: ");
544 PRINT_RANDOM_SIGNAL (stop_signal);
545 #else
546 printf_filtered ("\nProgram terminated with signal ");
547 signame = strsigno (stop_signal);
548 if (signame == NULL)
549 printf_filtered ("%d", stop_signal);
550 else
551 /* Do we need to print the number in addition to the name? */
552 printf_filtered ("%s (%d)", signame, stop_signal);
553 printf_filtered (", %s\n", safe_strsignal (stop_signal));
554 #endif
555 printf_filtered ("The program no longer exists.\n");
556 fflush (stdout);
557 #ifdef NO_SINGLE_STEP
558 one_stepped = 0;
559 #endif
560 break;
561 }
562
563 stop_signal = WSTOPSIG (w);
564
565 if (pid != inferior_pid)
566 {
567 int save_pid = inferior_pid;
568
569 inferior_pid = pid; /* Setup for target memory/regs */
570 registers_changed ();
571 stop_pc = read_pc ();
572 inferior_pid = save_pid;
573 registers_changed ();
574 }
575 else
576 stop_pc = read_pc ();
577
578 if (stop_signal == SIGTRAP
579 && breakpoint_here_p (stop_pc - DECR_PC_AFTER_BREAK))
580 if (!breakpoint_thread_match (stop_pc - DECR_PC_AFTER_BREAK, pid))
581 {
582 /* Saw a breakpoint, but it was hit by the wrong thread. Just continue. */
583 if (breakpoints_inserted)
584 {
585 remove_breakpoints ();
586 target_resume (pid, 1, 0); /* Single step */
587 target_wait (pid, NULL);
588 insert_breakpoints ();
589 }
590 target_resume (-1, 0, 0);
591 continue;
592 }
593 else
594 if (pid != inferior_pid)
595 goto switch_thread;
596
597 if (pid != inferior_pid)
598 {
599 int printed = 0;
600
601 if (!in_thread_list (pid))
602 {
603 fprintf (stderr, "[New %s]\n", target_pid_to_str (pid));
604 add_thread (pid);
605
606 target_resume (-1, 0, 0);
607 continue;
608 }
609 else
610 {
611 if (stop_signal >= NSIG || signal_print[stop_signal])
612 {
613 char *signame;
614
615 printed = 1;
616 target_terminal_ours_for_output ();
617 printf_filtered ("\nProgram received signal ");
618 signame = strsigno (stop_signal);
619 if (signame == NULL)
620 printf_filtered ("%d", stop_signal);
621 else
622 printf_filtered ("%s (%d)", signame, stop_signal);
623 printf_filtered (", %s\n", safe_strsignal (stop_signal));
624
625 fflush (stdout);
626 }
627
628 if (stop_signal == SIGTRAP
629 || stop_signal >= NSIG
630 || signal_stop[stop_signal])
631 {
632 switch_thread:
633 inferior_pid = pid;
634 printf_filtered ("[Switching to %s]\n", target_pid_to_str (pid));
635
636 flush_cached_frames ();
637 registers_changed ();
638 trap_expected = 0;
639 if (step_resume_breakpoint)
640 {
641 delete_breakpoint (step_resume_breakpoint);
642 step_resume_breakpoint = NULL;
643 }
644 prev_pc = 0;
645 prev_sp = 0;
646 prev_func_name = NULL;
647 step_range_start = 0;
648 step_range_end = 0;
649 step_frame_address = 0;
650 handling_longjmp = 0;
651 another_trap = 0;
652 }
653 else
654 {
655 if (printed)
656 target_terminal_inferior ();
657
658 /* Clear the signal if it should not be passed. */
659 if (signal_program[stop_signal] == 0)
660 stop_signal = 0;
661
662 target_resume (-1, 0, stop_signal);
663 continue;
664 }
665 }
666 }
667
668 same_pid:
669
670 #ifdef NO_SINGLE_STEP
671 if (one_stepped)
672 single_step (0); /* This actually cleans up the ss */
673 #endif /* NO_SINGLE_STEP */
674
675 /* If PC is pointing at a nullified instruction, then step beyond it so that
676 the user won't be confused when GDB appears to be ready to execute it. */
677
678 if (INSTRUCTION_NULLIFIED)
679 {
680 resume (1, 0);
681 continue;
682 }
683
684 set_current_frame ( create_new_frame (read_fp (), stop_pc));
685
686 stop_frame_address = FRAME_FP (get_current_frame ());
687 stop_sp = read_sp ();
688 stop_func_start = 0;
689 stop_func_end = 0;
690 stop_func_name = 0;
691 /* Don't care about return value; stop_func_start and stop_func_name
692 will both be 0 if it doesn't work. */
693 find_pc_partial_function (stop_pc, &stop_func_name, &stop_func_start,
694 &stop_func_end);
695 stop_func_start += FUNCTION_START_OFFSET;
696 another_trap = 0;
697 bpstat_clear (&stop_bpstat);
698 stop_step = 0;
699 stop_stack_dummy = 0;
700 stop_print_frame = 1;
701 random_signal = 0;
702 stopped_by_random_signal = 0;
703 breakpoints_failed = 0;
704
705 /* Look at the cause of the stop, and decide what to do.
706 The alternatives are:
707 1) break; to really stop and return to the debugger,
708 2) drop through to start up again
709 (set another_trap to 1 to single step once)
710 3) set random_signal to 1, and the decision between 1 and 2
711 will be made according to the signal handling tables. */
712
713 /* First, distinguish signals caused by the debugger from signals
714 that have to do with the program's own actions.
715 Note that breakpoint insns may cause SIGTRAP or SIGILL
716 or SIGEMT, depending on the operating system version.
717 Here we detect when a SIGILL or SIGEMT is really a breakpoint
718 and change it to SIGTRAP. */
719
720 if (stop_signal == SIGTRAP
721 || (breakpoints_inserted &&
722 (stop_signal == SIGILL
723 #ifdef SIGEMT
724 || stop_signal == SIGEMT
725 #endif
726 ))
727 || stop_soon_quietly)
728 {
729 if (stop_signal == SIGTRAP && stop_after_trap)
730 {
731 stop_print_frame = 0;
732 break;
733 }
734 if (stop_soon_quietly)
735 break;
736
737 /* Don't even think about breakpoints
738 if just proceeded over a breakpoint.
739
740 However, if we are trying to proceed over a breakpoint
741 and end up in sigtramp, then step_resume_breakpoint
742 will be set and we should check whether we've hit the
743 step breakpoint. */
744 if (stop_signal == SIGTRAP && trap_expected
745 && step_resume_breakpoint == NULL)
746 bpstat_clear (&stop_bpstat);
747 else
748 {
749 /* See if there is a breakpoint at the current PC. */
750 stop_bpstat = bpstat_stop_status
751 (&stop_pc, stop_frame_address,
752 #if DECR_PC_AFTER_BREAK
753 /* Notice the case of stepping through a jump
754 that lands just after a breakpoint.
755 Don't confuse that with hitting the breakpoint.
756 What we check for is that 1) stepping is going on
757 and 2) the pc before the last insn does not match
758 the address of the breakpoint before the current pc. */
759 (prev_pc != stop_pc - DECR_PC_AFTER_BREAK
760 && CURRENTLY_STEPPING ())
761 #else /* DECR_PC_AFTER_BREAK zero */
762 0
763 #endif /* DECR_PC_AFTER_BREAK zero */
764 );
765 /* Following in case break condition called a
766 function. */
767 stop_print_frame = 1;
768 }
769
770 if (stop_signal == SIGTRAP)
771 random_signal
772 = !(bpstat_explains_signal (stop_bpstat)
773 || trap_expected
774 #ifndef CALL_DUMMY_BREAKPOINT_OFFSET
775 || PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address)
776 #endif /* No CALL_DUMMY_BREAKPOINT_OFFSET. */
777 || (step_range_end && step_resume_breakpoint == NULL));
778 else
779 {
780 random_signal
781 = !(bpstat_explains_signal (stop_bpstat)
782 /* End of a stack dummy. Some systems (e.g. Sony
783 news) give another signal besides SIGTRAP,
784 so check here as well as above. */
785 #ifndef CALL_DUMMY_BREAKPOINT_OFFSET
786 || PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address)
787 #endif /* No CALL_DUMMY_BREAKPOINT_OFFSET. */
788 );
789 if (!random_signal)
790 stop_signal = SIGTRAP;
791 }
792 }
793 else
794 random_signal = 1;
795
796 /* For the program's own signals, act according to
797 the signal handling tables. */
798
799 if (random_signal)
800 {
801 /* Signal not for debugging purposes. */
802 int printed = 0;
803
804 stopped_by_random_signal = 1;
805
806 if (stop_signal >= NSIG
807 || signal_print[stop_signal])
808 {
809 char *signame;
810 printed = 1;
811 target_terminal_ours_for_output ();
812 #ifdef PRINT_RANDOM_SIGNAL
813 PRINT_RANDOM_SIGNAL (stop_signal);
814 #else
815 printf_filtered ("\nProgram received signal ");
816 signame = strsigno (stop_signal);
817 if (signame == NULL)
818 printf_filtered ("%d", stop_signal);
819 else
820 /* Do we need to print the number as well as the name? */
821 printf_filtered ("%s (%d)", signame, stop_signal);
822 printf_filtered (", %s\n", safe_strsignal (stop_signal));
823 #endif /* PRINT_RANDOM_SIGNAL */
824 fflush (stdout);
825 }
826 if (stop_signal >= NSIG
827 || signal_stop[stop_signal])
828 break;
829 /* If not going to stop, give terminal back
830 if we took it away. */
831 else if (printed)
832 target_terminal_inferior ();
833
834 /* Clear the signal if it should not be passed. */
835 if (signal_program[stop_signal] == 0)
836 stop_signal = 0;
837
838 /* I'm not sure whether this needs to be check_sigtramp2 or
839 whether it could/should be keep_going. */
840 goto check_sigtramp2;
841 }
842
843 /* Handle cases caused by hitting a breakpoint. */
844 {
845 CORE_ADDR jmp_buf_pc;
846 struct bpstat_what what;
847
848 what = bpstat_what (stop_bpstat);
849
850 if (what.call_dummy)
851 {
852 stop_stack_dummy = 1;
853 #ifdef HP_OS_BUG
854 trap_expected_after_continue = 1;
855 #endif
856 }
857
858 switch (what.main_action)
859 {
860 case BPSTAT_WHAT_SET_LONGJMP_RESUME:
861 /* If we hit the breakpoint at longjmp, disable it for the
862 duration of this command. Then, install a temporary
863 breakpoint at the target of the jmp_buf. */
864 disable_longjmp_breakpoint();
865 remove_breakpoints ();
866 breakpoints_inserted = 0;
867 if (!GET_LONGJMP_TARGET(&jmp_buf_pc)) goto keep_going;
868
869 /* Need to blow away step-resume breakpoint, as it
870 interferes with us */
871 if (step_resume_breakpoint != NULL)
872 {
873 delete_breakpoint (step_resume_breakpoint);
874 step_resume_breakpoint = NULL;
875 what.step_resume = 0;
876 }
877
878 #if 0
879 /* FIXME - Need to implement nested temporary breakpoints */
880 if (step_over_calls > 0)
881 set_longjmp_resume_breakpoint(jmp_buf_pc,
882 get_current_frame());
883 else
884 #endif /* 0 */
885 set_longjmp_resume_breakpoint(jmp_buf_pc, NULL);
886 handling_longjmp = 1; /* FIXME */
887 goto keep_going;
888
889 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
890 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME_SINGLE:
891 remove_breakpoints ();
892 breakpoints_inserted = 0;
893 #if 0
894 /* FIXME - Need to implement nested temporary breakpoints */
895 if (step_over_calls
896 && (stop_frame_address
897 INNER_THAN step_frame_address))
898 {
899 another_trap = 1;
900 goto keep_going;
901 }
902 #endif /* 0 */
903 disable_longjmp_breakpoint();
904 handling_longjmp = 0; /* FIXME */
905 if (what.main_action == BPSTAT_WHAT_CLEAR_LONGJMP_RESUME)
906 break;
907 /* else fallthrough */
908
909 case BPSTAT_WHAT_SINGLE:
910 if (breakpoints_inserted)
911 remove_breakpoints ();
912 breakpoints_inserted = 0;
913 another_trap = 1;
914 /* Still need to check other stuff, at least the case
915 where we are stepping and step out of the right range. */
916 break;
917
918 case BPSTAT_WHAT_STOP_NOISY:
919 stop_print_frame = 1;
920 /* We are about to nuke the step_resume_breakpoint via the
921 cleanup chain, so no need to worry about it here. */
922 goto stop_stepping;
923
924 case BPSTAT_WHAT_STOP_SILENT:
925 stop_print_frame = 0;
926 /* We are about to nuke the step_resume_breakpoint via the
927 cleanup chain, so no need to worry about it here. */
928 goto stop_stepping;
929
930 case BPSTAT_WHAT_KEEP_CHECKING:
931 break;
932 }
933
934 if (what.step_resume)
935 {
936 delete_breakpoint (step_resume_breakpoint);
937 step_resume_breakpoint = NULL;
938
939 /* If were waiting for a trap, hitting the step_resume_break
940 doesn't count as getting it. */
941 if (trap_expected)
942 another_trap = 1;
943 }
944 }
945
946 /* We come here if we hit a breakpoint but should not
947 stop for it. Possibly we also were stepping
948 and should stop for that. So fall through and
949 test for stepping. But, if not stepping,
950 do not stop. */
951
952 #ifndef CALL_DUMMY_BREAKPOINT_OFFSET
953 /* This is the old way of detecting the end of the stack dummy.
954 An architecture which defines CALL_DUMMY_BREAKPOINT_OFFSET gets
955 handled above. As soon as we can test it on all of them, all
956 architectures should define it. */
957
958 /* If this is the breakpoint at the end of a stack dummy,
959 just stop silently, unless the user was doing an si/ni, in which
960 case she'd better know what she's doing. */
961
962 if (PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address)
963 && !step_range_end)
964 {
965 stop_print_frame = 0;
966 stop_stack_dummy = 1;
967 #ifdef HP_OS_BUG
968 trap_expected_after_continue = 1;
969 #endif
970 break;
971 }
972 #endif /* No CALL_DUMMY_BREAKPOINT_OFFSET. */
973
974 if (step_resume_breakpoint)
975 /* Having a step-resume breakpoint overrides anything
976 else having to do with stepping commands until
977 that breakpoint is reached. */
978 /* I suspect this could/should be keep_going, because if the
979 check_sigtramp2 check succeeds, then it will put in another
980 step_resume_breakpoint, and we aren't (yet) prepared to nest
981 them. */
982 goto check_sigtramp2;
983
984 if (step_range_end == 0)
985 /* Likewise if we aren't even stepping. */
986 /* I'm not sure whether this needs to be check_sigtramp2 or
987 whether it could/should be keep_going. */
988 goto check_sigtramp2;
989
990 /* If stepping through a line, keep going if still within it. */
991 if (stop_pc >= step_range_start
992 && stop_pc < step_range_end
993 /* The step range might include the start of the
994 function, so if we are at the start of the
995 step range and either the stack or frame pointers
996 just changed, we've stepped outside */
997 && !(stop_pc == step_range_start
998 && stop_frame_address
999 && (stop_sp INNER_THAN prev_sp
1000 || stop_frame_address != step_frame_address)))
1001 {
1002 /* We might be doing a BPSTAT_WHAT_SINGLE and getting a signal.
1003 So definately need to check for sigtramp here. */
1004 goto check_sigtramp2;
1005 }
1006
1007 /* We stepped out of the stepping range. See if that was due
1008 to a subroutine call that we should proceed to the end of. */
1009
1010 /* Did we just take a signal? */
1011 if (IN_SIGTRAMP (stop_pc, stop_func_name)
1012 && !IN_SIGTRAMP (prev_pc, prev_func_name))
1013 {
1014 /* This code is needed at least in the following case:
1015 The user types "next" and then a signal arrives (before
1016 the "next" is done). */
1017 /* We've just taken a signal; go until we are back to
1018 the point where we took it and one more. */
1019 {
1020 struct symtab_and_line sr_sal;
1021
1022 sr_sal.pc = prev_pc;
1023 sr_sal.symtab = NULL;
1024 sr_sal.line = 0;
1025 step_resume_breakpoint =
1026 set_momentary_breakpoint (sr_sal, get_current_frame (),
1027 bp_step_resume);
1028 if (breakpoints_inserted)
1029 insert_breakpoints ();
1030 }
1031
1032 /* If this is stepi or nexti, make sure that the stepping range
1033 gets us past that instruction. */
1034 if (step_range_end == 1)
1035 /* FIXME: Does this run afoul of the code below which, if
1036 we step into the middle of a line, resets the stepping
1037 range? */
1038 step_range_end = (step_range_start = prev_pc) + 1;
1039
1040 remove_breakpoints_on_following_step = 1;
1041 goto keep_going;
1042 }
1043
1044 if (stop_func_start)
1045 {
1046 /* Do this after the IN_SIGTRAMP check; it might give
1047 an error. */
1048 prologue_pc = stop_func_start;
1049 SKIP_PROLOGUE (prologue_pc);
1050 }
1051
1052 /* ==> See comments at top of file on this algorithm. <==*/
1053
1054 if ((stop_pc < stop_func_start
1055 || stop_pc >= stop_func_end
1056 || stop_pc == stop_func_start
1057 || IN_SOLIB_TRAMPOLINE (stop_pc, stop_func_name))
1058 && (stop_func_start != prev_func_start
1059 || prologue_pc != stop_func_start
1060 || stop_sp != prev_sp))
1061 {
1062 /* It's a subroutine call. */
1063
1064 if (step_over_calls == 0)
1065 {
1066 /* I presume that step_over_calls is only 0 when we're
1067 supposed to be stepping at the assembly language level
1068 ("stepi"). Just stop. */
1069 stop_step = 1;
1070 break;
1071 }
1072
1073 if (step_over_calls > 0)
1074 /* We're doing a "next". */
1075 goto step_over_function;
1076
1077 /* If we are in a function call trampoline (a stub between
1078 the calling routine and the real function), locate the real
1079 function. That's what tells us (a) whether we want to step
1080 into it at all, and (b) what prologue we want to run to
1081 the end of, if we do step into it. */
1082 tmp = SKIP_TRAMPOLINE_CODE (stop_pc);
1083 if (tmp != 0)
1084 stop_func_start = tmp;
1085
1086 /* If we have line number information for the function we
1087 are thinking of stepping into, step into it.
1088
1089 If there are several symtabs at that PC (e.g. with include
1090 files), just want to know whether *any* of them have line
1091 numbers. find_pc_line handles this. */
1092 {
1093 struct symtab_and_line tmp_sal;
1094
1095 tmp_sal = find_pc_line (stop_func_start, 0);
1096 if (tmp_sal.line != 0)
1097 goto step_into_function;
1098 }
1099
1100 step_over_function:
1101 /* A subroutine call has happened. */
1102 {
1103 /* Set a special breakpoint after the return */
1104 struct symtab_and_line sr_sal;
1105 sr_sal.pc =
1106 ADDR_BITS_REMOVE
1107 (SAVED_PC_AFTER_CALL (get_current_frame ()));
1108 sr_sal.symtab = NULL;
1109 sr_sal.line = 0;
1110 step_resume_breakpoint =
1111 set_momentary_breakpoint (sr_sal, get_current_frame (),
1112 bp_step_resume);
1113 if (breakpoints_inserted)
1114 insert_breakpoints ();
1115 }
1116 goto keep_going;
1117
1118 step_into_function:
1119 /* Subroutine call with source code we should not step over.
1120 Do step to the first line of code in it. */
1121 SKIP_PROLOGUE (stop_func_start);
1122 sal = find_pc_line (stop_func_start, 0);
1123 /* Use the step_resume_break to step until
1124 the end of the prologue, even if that involves jumps
1125 (as it seems to on the vax under 4.2). */
1126 /* If the prologue ends in the middle of a source line,
1127 continue to the end of that source line.
1128 Otherwise, just go to end of prologue. */
1129 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
1130 /* no, don't either. It skips any code that's
1131 legitimately on the first line. */
1132 #else
1133 if (sal.end && sal.pc != stop_func_start)
1134 stop_func_start = sal.end;
1135 #endif
1136
1137 if (stop_func_start == stop_pc)
1138 {
1139 /* We are already there: stop now. */
1140 stop_step = 1;
1141 break;
1142 }
1143 else
1144 /* Put the step-breakpoint there and go until there. */
1145 {
1146 struct symtab_and_line sr_sal;
1147
1148 sr_sal.pc = stop_func_start;
1149 sr_sal.symtab = NULL;
1150 sr_sal.line = 0;
1151 /* Do not specify what the fp should be when we stop
1152 since on some machines the prologue
1153 is where the new fp value is established. */
1154 step_resume_breakpoint =
1155 set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
1156 if (breakpoints_inserted)
1157 insert_breakpoints ();
1158
1159 /* And make sure stepping stops right away then. */
1160 step_range_end = step_range_start;
1161 }
1162 goto keep_going;
1163 }
1164
1165 /* We've wandered out of the step range (but haven't done a
1166 subroutine call or return). (Is that true? I think we get
1167 here if we did a return and maybe a longjmp). */
1168
1169 sal = find_pc_line(stop_pc, 0);
1170
1171 if (step_range_end == 1)
1172 {
1173 /* It is stepi or nexti. We always want to stop stepping after
1174 one instruction. */
1175 stop_step = 1;
1176 break;
1177 }
1178
1179 if (sal.line == 0)
1180 {
1181 /* We have no line number information. That means to stop
1182 stepping (does this always happen right after one instruction,
1183 when we do "s" in a function with no line numbers,
1184 or can this happen as a result of a return or longjmp?). */
1185 stop_step = 1;
1186 break;
1187 }
1188
1189 if (stop_pc == sal.pc && current_line != sal.line)
1190 {
1191 /* We are at the start of a different line. So stop. Note that
1192 we don't stop if we step into the middle of a different line.
1193 That is said to make things like for (;;) statements work
1194 better. */
1195 stop_step = 1;
1196 break;
1197 }
1198
1199 /* We aren't done stepping.
1200
1201 Optimize by setting the stepping range to the line.
1202 (We might not be in the original line, but if we entered a
1203 new line in mid-statement, we continue stepping. This makes
1204 things like for(;;) statements work better.) */
1205 step_range_start = sal.pc;
1206 step_range_end = sal.end;
1207 goto keep_going;
1208
1209 check_sigtramp2:
1210 if (trap_expected
1211 && IN_SIGTRAMP (stop_pc, stop_func_name)
1212 && !IN_SIGTRAMP (prev_pc, prev_func_name))
1213 {
1214 /* What has happened here is that we have just stepped the inferior
1215 with a signal (because it is a signal which shouldn't make
1216 us stop), thus stepping into sigtramp.
1217
1218 So we need to set a step_resume_break_address breakpoint
1219 and continue until we hit it, and then step. FIXME: This should
1220 be more enduring than a step_resume breakpoint; we should know
1221 that we will later need to keep going rather than re-hitting
1222 the breakpoint here (see testsuite/gdb.t06/signals.exp where
1223 it says "exceedingly difficult"). */
1224 struct symtab_and_line sr_sal;
1225
1226 sr_sal.pc = prev_pc;
1227 sr_sal.symtab = NULL;
1228 sr_sal.line = 0;
1229 step_resume_breakpoint =
1230 set_momentary_breakpoint (sr_sal, get_current_frame (),
1231 bp_step_resume);
1232 if (breakpoints_inserted)
1233 insert_breakpoints ();
1234
1235 remove_breakpoints_on_following_step = 1;
1236 another_trap = 1;
1237 }
1238
1239 keep_going:
1240 /* Come to this label when you need to resume the inferior.
1241 It's really much cleaner to do a goto than a maze of if-else
1242 conditions. */
1243
1244 /* Save the pc before execution, to compare with pc after stop. */
1245 prev_pc = read_pc (); /* Might have been DECR_AFTER_BREAK */
1246 prev_func_start = stop_func_start; /* Ok, since if DECR_PC_AFTER
1247 BREAK is defined, the
1248 original pc would not have
1249 been at the start of a
1250 function. */
1251 prev_func_name = stop_func_name;
1252 prev_sp = stop_sp;
1253
1254 /* If we did not do break;, it means we should keep
1255 running the inferior and not return to debugger. */
1256
1257 if (trap_expected && stop_signal != SIGTRAP)
1258 {
1259 /* We took a signal (which we are supposed to pass through to
1260 the inferior, else we'd have done a break above) and we
1261 haven't yet gotten our trap. Simply continue. */
1262 resume (CURRENTLY_STEPPING (), stop_signal);
1263 }
1264 else
1265 {
1266 /* Either the trap was not expected, but we are continuing
1267 anyway (the user asked that this signal be passed to the
1268 child)
1269 -- or --
1270 The signal was SIGTRAP, e.g. it was our signal, but we
1271 decided we should resume from it.
1272
1273 We're going to run this baby now!
1274
1275 Insert breakpoints now, unless we are trying
1276 to one-proceed past a breakpoint. */
1277 /* If we've just finished a special step resume and we don't
1278 want to hit a breakpoint, pull em out. */
1279 if (step_resume_breakpoint == NULL &&
1280 remove_breakpoints_on_following_step)
1281 {
1282 remove_breakpoints_on_following_step = 0;
1283 remove_breakpoints ();
1284 breakpoints_inserted = 0;
1285 }
1286 else if (!breakpoints_inserted &&
1287 (step_resume_breakpoint != NULL || !another_trap))
1288 {
1289 breakpoints_failed = insert_breakpoints ();
1290 if (breakpoints_failed)
1291 break;
1292 breakpoints_inserted = 1;
1293 }
1294
1295 trap_expected = another_trap;
1296
1297 if (stop_signal == SIGTRAP)
1298 stop_signal = 0;
1299
1300 #ifdef SHIFT_INST_REGS
1301 /* I'm not sure when this following segment applies. I do know, now,
1302 that we shouldn't rewrite the regs when we were stopped by a
1303 random signal from the inferior process. */
1304 /* FIXME: Shouldn't this be based on the valid bit of the SXIP?
1305 (this is only used on the 88k). */
1306
1307 if (!bpstat_explains_signal (stop_bpstat)
1308 && (stop_signal != SIGCLD)
1309 && !stopped_by_random_signal)
1310 SHIFT_INST_REGS();
1311 #endif /* SHIFT_INST_REGS */
1312
1313 resume (CURRENTLY_STEPPING (), stop_signal);
1314 }
1315 }
1316
1317 stop_stepping:
1318 if (target_has_execution)
1319 {
1320 /* Assuming the inferior still exists, set these up for next
1321 time, just like we did above if we didn't break out of the
1322 loop. */
1323 prev_pc = read_pc ();
1324 prev_func_start = stop_func_start;
1325 prev_func_name = stop_func_name;
1326 prev_sp = stop_sp;
1327 }
1328 do_cleanups (old_cleanups);
1329 }
1330 \f
1331 /* Here to return control to GDB when the inferior stops for real.
1332 Print appropriate messages, remove breakpoints, give terminal our modes.
1333
1334 STOP_PRINT_FRAME nonzero means print the executing frame
1335 (pc, function, args, file, line number and line text).
1336 BREAKPOINTS_FAILED nonzero means stop was due to error
1337 attempting to insert breakpoints. */
1338
1339 void
1340 normal_stop ()
1341 {
1342 /* Make sure that the current_frame's pc is correct. This
1343 is a correction for setting up the frame info before doing
1344 DECR_PC_AFTER_BREAK */
1345 if (target_has_execution && get_current_frame())
1346 (get_current_frame ())->pc = read_pc ();
1347
1348 if (breakpoints_failed)
1349 {
1350 target_terminal_ours_for_output ();
1351 print_sys_errmsg ("ptrace", breakpoints_failed);
1352 printf_filtered ("Stopped; cannot insert breakpoints.\n\
1353 The same program may be running in another process.\n");
1354 }
1355
1356 if (target_has_execution && breakpoints_inserted)
1357 if (remove_breakpoints ())
1358 {
1359 target_terminal_ours_for_output ();
1360 printf_filtered ("Cannot remove breakpoints because program is no longer writable.\n\
1361 It might be running in another process.\n\
1362 Further execution is probably impossible.\n");
1363 }
1364
1365 breakpoints_inserted = 0;
1366
1367 /* Delete the breakpoint we stopped at, if it wants to be deleted.
1368 Delete any breakpoint that is to be deleted at the next stop. */
1369
1370 breakpoint_auto_delete (stop_bpstat);
1371
1372 /* If an auto-display called a function and that got a signal,
1373 delete that auto-display to avoid an infinite recursion. */
1374
1375 if (stopped_by_random_signal)
1376 disable_current_display ();
1377
1378 if (step_multi && stop_step)
1379 return;
1380
1381 target_terminal_ours ();
1382
1383 /* Look up the hook_stop and run it if it exists. */
1384
1385 if (stop_command->hook)
1386 {
1387 catch_errors (hook_stop_stub, (char *)stop_command->hook,
1388 "Error while running hook_stop:\n", RETURN_MASK_ALL);
1389 }
1390
1391 if (!target_has_stack)
1392 return;
1393
1394 /* Select innermost stack frame except on return from a stack dummy routine,
1395 or if the program has exited. Print it without a level number if
1396 we have changed functions or hit a breakpoint. Print source line
1397 if we have one. */
1398 if (!stop_stack_dummy)
1399 {
1400 select_frame (get_current_frame (), 0);
1401
1402 if (stop_print_frame)
1403 {
1404 int source_only;
1405
1406 source_only = bpstat_print (stop_bpstat);
1407 source_only = source_only ||
1408 ( stop_step
1409 && step_frame_address == stop_frame_address
1410 && step_start_function == find_pc_function (stop_pc));
1411
1412 print_stack_frame (selected_frame, -1, source_only? -1: 1);
1413
1414 /* Display the auto-display expressions. */
1415 do_displays ();
1416 }
1417 }
1418
1419 /* Save the function value return registers, if we care.
1420 We might be about to restore their previous contents. */
1421 if (proceed_to_finish)
1422 read_register_bytes (0, stop_registers, REGISTER_BYTES);
1423
1424 if (stop_stack_dummy)
1425 {
1426 /* Pop the empty frame that contains the stack dummy.
1427 POP_FRAME ends with a setting of the current frame, so we
1428 can use that next. */
1429 POP_FRAME;
1430 select_frame (get_current_frame (), 0);
1431 }
1432 }
1433
1434 static int
1435 hook_stop_stub (cmd)
1436 char *cmd;
1437 {
1438 execute_user_command ((struct cmd_list_element *)cmd, 0);
1439 return (0);
1440 }
1441 \f
1442 int signal_stop_state (signo)
1443 int signo;
1444 {
1445 return ((signo >= 0 && signo < NSIG) ? signal_stop[signo] : 0);
1446 }
1447
1448 int signal_print_state (signo)
1449 int signo;
1450 {
1451 return ((signo >= 0 && signo < NSIG) ? signal_print[signo] : 0);
1452 }
1453
1454 int signal_pass_state (signo)
1455 int signo;
1456 {
1457 return ((signo >= 0 && signo < NSIG) ? signal_program[signo] : 0);
1458 }
1459
1460 static void
1461 sig_print_header ()
1462 {
1463 printf_filtered ("Signal\t\tStop\tPrint\tPass to program\tDescription\n");
1464 }
1465
1466 static void
1467 sig_print_info (number)
1468 int number;
1469 {
1470 char *name;
1471
1472 if ((name = strsigno (number)) == NULL)
1473 printf_filtered ("%d\t\t", number);
1474 else
1475 printf_filtered ("%s (%d)\t", name, number);
1476 printf_filtered ("%s\t", signal_stop[number] ? "Yes" : "No");
1477 printf_filtered ("%s\t", signal_print[number] ? "Yes" : "No");
1478 printf_filtered ("%s\t\t", signal_program[number] ? "Yes" : "No");
1479 printf_filtered ("%s\n", safe_strsignal (number));
1480 }
1481
1482 /* Specify how various signals in the inferior should be handled. */
1483
1484 static void
1485 handle_command (args, from_tty)
1486 char *args;
1487 int from_tty;
1488 {
1489 char **argv;
1490 int digits, wordlen;
1491 int sigfirst, signum, siglast;
1492 int allsigs;
1493 int nsigs;
1494 unsigned char *sigs;
1495 struct cleanup *old_chain;
1496
1497 if (args == NULL)
1498 {
1499 error_no_arg ("signal to handle");
1500 }
1501
1502 /* Allocate and zero an array of flags for which signals to handle. */
1503
1504 nsigs = signo_max () + 1;
1505 sigs = (unsigned char *) alloca (nsigs);
1506 memset (sigs, 0, nsigs);
1507
1508 /* Break the command line up into args. */
1509
1510 argv = buildargv (args);
1511 if (argv == NULL)
1512 {
1513 nomem (0);
1514 }
1515 old_chain = make_cleanup (freeargv, (char *) argv);
1516
1517 /* Walk through the args, looking for signal numbers, signal names, and
1518 actions. Signal numbers and signal names may be interspersed with
1519 actions, with the actions being performed for all signals cumulatively
1520 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
1521
1522 while (*argv != NULL)
1523 {
1524 wordlen = strlen (*argv);
1525 for (digits = 0; isdigit ((*argv)[digits]); digits++) {;}
1526 allsigs = 0;
1527 sigfirst = siglast = -1;
1528
1529 if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
1530 {
1531 /* Apply action to all signals except those used by the
1532 debugger. Silently skip those. */
1533 allsigs = 1;
1534 sigfirst = 0;
1535 siglast = nsigs - 1;
1536 }
1537 else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
1538 {
1539 SET_SIGS (nsigs, sigs, signal_stop);
1540 SET_SIGS (nsigs, sigs, signal_print);
1541 }
1542 else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
1543 {
1544 UNSET_SIGS (nsigs, sigs, signal_program);
1545 }
1546 else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
1547 {
1548 SET_SIGS (nsigs, sigs, signal_print);
1549 }
1550 else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
1551 {
1552 SET_SIGS (nsigs, sigs, signal_program);
1553 }
1554 else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
1555 {
1556 UNSET_SIGS (nsigs, sigs, signal_stop);
1557 }
1558 else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
1559 {
1560 SET_SIGS (nsigs, sigs, signal_program);
1561 }
1562 else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
1563 {
1564 UNSET_SIGS (nsigs, sigs, signal_print);
1565 UNSET_SIGS (nsigs, sigs, signal_stop);
1566 }
1567 else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
1568 {
1569 UNSET_SIGS (nsigs, sigs, signal_program);
1570 }
1571 else if (digits > 0)
1572 {
1573 sigfirst = siglast = atoi (*argv);
1574 if ((*argv)[digits] == '-')
1575 {
1576 siglast = atoi ((*argv) + digits + 1);
1577 }
1578 if (sigfirst > siglast)
1579 {
1580 /* Bet he didn't figure we'd think of this case... */
1581 signum = sigfirst;
1582 sigfirst = siglast;
1583 siglast = signum;
1584 }
1585 if (sigfirst < 0 || sigfirst >= nsigs)
1586 {
1587 error ("Signal %d not in range 0-%d", sigfirst, nsigs - 1);
1588 }
1589 if (siglast < 0 || siglast >= nsigs)
1590 {
1591 error ("Signal %d not in range 0-%d", siglast, nsigs - 1);
1592 }
1593 }
1594 else if ((signum = strtosigno (*argv)) != 0)
1595 {
1596 sigfirst = siglast = signum;
1597 }
1598 else
1599 {
1600 /* Not a number and not a recognized flag word => complain. */
1601 error ("Unrecognized or ambiguous flag word: \"%s\".", *argv);
1602 }
1603
1604 /* If any signal numbers or symbol names were found, set flags for
1605 which signals to apply actions to. */
1606
1607 for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
1608 {
1609 switch (signum)
1610 {
1611 case SIGTRAP:
1612 case SIGINT:
1613 if (!allsigs && !sigs[signum])
1614 {
1615 if (query ("%s is used by the debugger.\nAre you sure you want to change it? ", strsigno (signum)))
1616 {
1617 sigs[signum] = 1;
1618 }
1619 else
1620 {
1621 printf ("Not confirmed, unchanged.\n");
1622 fflush (stdout);
1623 }
1624 }
1625 break;
1626 default:
1627 sigs[signum] = 1;
1628 break;
1629 }
1630 }
1631
1632 argv++;
1633 }
1634
1635 target_notice_signals(inferior_pid);
1636
1637 if (from_tty)
1638 {
1639 /* Show the results. */
1640 sig_print_header ();
1641 for (signum = 0; signum < nsigs; signum++)
1642 {
1643 if (sigs[signum])
1644 {
1645 sig_print_info (signum);
1646 }
1647 }
1648 }
1649
1650 do_cleanups (old_chain);
1651 }
1652
1653 /* Print current contents of the tables set by the handle command. */
1654
1655 static void
1656 signals_info (signum_exp, from_tty)
1657 char *signum_exp;
1658 int from_tty;
1659 {
1660 register int i;
1661 sig_print_header ();
1662
1663 if (signum_exp)
1664 {
1665 /* First see if this is a symbol name. */
1666 i = strtosigno (signum_exp);
1667 if (i == 0)
1668 {
1669 /* Nope, maybe it's an address which evaluates to a signal
1670 number. */
1671 i = parse_and_eval_address (signum_exp);
1672 if (i >= NSIG || i < 0)
1673 error ("Signal number out of bounds.");
1674 }
1675 sig_print_info (i);
1676 return;
1677 }
1678
1679 printf_filtered ("\n");
1680 for (i = 0; i < NSIG; i++)
1681 {
1682 QUIT;
1683
1684 sig_print_info (i);
1685 }
1686
1687 printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
1688 }
1689 \f
1690 /* Save all of the information associated with the inferior<==>gdb
1691 connection. INF_STATUS is a pointer to a "struct inferior_status"
1692 (defined in inferior.h). */
1693
1694 void
1695 save_inferior_status (inf_status, restore_stack_info)
1696 struct inferior_status *inf_status;
1697 int restore_stack_info;
1698 {
1699 inf_status->stop_signal = stop_signal;
1700 inf_status->stop_pc = stop_pc;
1701 inf_status->stop_frame_address = stop_frame_address;
1702 inf_status->stop_step = stop_step;
1703 inf_status->stop_stack_dummy = stop_stack_dummy;
1704 inf_status->stopped_by_random_signal = stopped_by_random_signal;
1705 inf_status->trap_expected = trap_expected;
1706 inf_status->step_range_start = step_range_start;
1707 inf_status->step_range_end = step_range_end;
1708 inf_status->step_frame_address = step_frame_address;
1709 inf_status->step_over_calls = step_over_calls;
1710 inf_status->stop_after_trap = stop_after_trap;
1711 inf_status->stop_soon_quietly = stop_soon_quietly;
1712 /* Save original bpstat chain here; replace it with copy of chain.
1713 If caller's caller is walking the chain, they'll be happier if we
1714 hand them back the original chain when restore_i_s is called. */
1715 inf_status->stop_bpstat = stop_bpstat;
1716 stop_bpstat = bpstat_copy (stop_bpstat);
1717 inf_status->breakpoint_proceeded = breakpoint_proceeded;
1718 inf_status->restore_stack_info = restore_stack_info;
1719 inf_status->proceed_to_finish = proceed_to_finish;
1720
1721 memcpy (inf_status->stop_registers, stop_registers, REGISTER_BYTES);
1722
1723 read_register_bytes (0, inf_status->registers, REGISTER_BYTES);
1724
1725 record_selected_frame (&(inf_status->selected_frame_address),
1726 &(inf_status->selected_level));
1727 return;
1728 }
1729
1730 struct restore_selected_frame_args {
1731 FRAME_ADDR frame_address;
1732 int level;
1733 };
1734
1735 static int restore_selected_frame PARAMS ((char *));
1736
1737 /* Restore the selected frame. args is really a struct
1738 restore_selected_frame_args * (declared as char * for catch_errors)
1739 telling us what frame to restore. Returns 1 for success, or 0 for
1740 failure. An error message will have been printed on error. */
1741 static int
1742 restore_selected_frame (args)
1743 char *args;
1744 {
1745 struct restore_selected_frame_args *fr =
1746 (struct restore_selected_frame_args *) args;
1747 FRAME fid;
1748 int level = fr->level;
1749
1750 fid = find_relative_frame (get_current_frame (), &level);
1751
1752 /* If inf_status->selected_frame_address is NULL, there was no
1753 previously selected frame. */
1754 if (fid == 0 ||
1755 FRAME_FP (fid) != fr->frame_address ||
1756 level != 0)
1757 {
1758 warning ("Unable to restore previously selected frame.\n");
1759 return 0;
1760 }
1761 select_frame (fid, fr->level);
1762 return(1);
1763 }
1764
1765 void
1766 restore_inferior_status (inf_status)
1767 struct inferior_status *inf_status;
1768 {
1769 stop_signal = inf_status->stop_signal;
1770 stop_pc = inf_status->stop_pc;
1771 stop_frame_address = inf_status->stop_frame_address;
1772 stop_step = inf_status->stop_step;
1773 stop_stack_dummy = inf_status->stop_stack_dummy;
1774 stopped_by_random_signal = inf_status->stopped_by_random_signal;
1775 trap_expected = inf_status->trap_expected;
1776 step_range_start = inf_status->step_range_start;
1777 step_range_end = inf_status->step_range_end;
1778 step_frame_address = inf_status->step_frame_address;
1779 step_over_calls = inf_status->step_over_calls;
1780 stop_after_trap = inf_status->stop_after_trap;
1781 stop_soon_quietly = inf_status->stop_soon_quietly;
1782 bpstat_clear (&stop_bpstat);
1783 stop_bpstat = inf_status->stop_bpstat;
1784 breakpoint_proceeded = inf_status->breakpoint_proceeded;
1785 proceed_to_finish = inf_status->proceed_to_finish;
1786
1787 memcpy (stop_registers, inf_status->stop_registers, REGISTER_BYTES);
1788
1789 /* The inferior can be gone if the user types "print exit(0)"
1790 (and perhaps other times). */
1791 if (target_has_execution)
1792 write_register_bytes (0, inf_status->registers, REGISTER_BYTES);
1793
1794 /* The inferior can be gone if the user types "print exit(0)"
1795 (and perhaps other times). */
1796
1797 /* FIXME: If we are being called after stopping in a function which
1798 is called from gdb, we should not be trying to restore the
1799 selected frame; it just prints a spurious error message (The
1800 message is useful, however, in detecting bugs in gdb (like if gdb
1801 clobbers the stack)). In fact, should we be restoring the
1802 inferior status at all in that case? . */
1803
1804 if (target_has_stack && inf_status->restore_stack_info)
1805 {
1806 struct restore_selected_frame_args fr;
1807 fr.level = inf_status->selected_level;
1808 fr.frame_address = inf_status->selected_frame_address;
1809 /* The point of catch_errors is that if the stack is clobbered,
1810 walking the stack might encounter a garbage pointer and error()
1811 trying to dereference it. */
1812 if (catch_errors (restore_selected_frame, &fr,
1813 "Unable to restore previously selected frame:\n",
1814 RETURN_MASK_ERROR) == 0)
1815 /* Error in restoring the selected frame. Select the innermost
1816 frame. */
1817 select_frame (get_current_frame (), 0);
1818 }
1819 }
1820
1821 \f
1822 void
1823 _initialize_infrun ()
1824 {
1825 register int i;
1826 register int numsigs;
1827
1828 add_info ("signals", signals_info,
1829 "What debugger does when program gets various signals.\n\
1830 Specify a signal number as argument to print info on that signal only.");
1831 add_info_alias ("handle", "signals", 0);
1832
1833 add_com ("handle", class_run, handle_command,
1834 "Specify how to handle a signal.\n\
1835 Args are signal numbers and actions to apply to those signals.\n\
1836 Signal numbers may be numeric (ex. 11) or symbolic (ex. SIGSEGV).\n\
1837 Numeric ranges may be specified with the form LOW-HIGH (ex. 14-21).\n\
1838 The special arg \"all\" is recognized to mean all signals except those\n\
1839 used by the debugger, typically SIGTRAP and SIGINT.\n\
1840 Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
1841 \"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
1842 Stop means reenter debugger if this signal happens (implies print).\n\
1843 Print means print a message if this signal happens.\n\
1844 Pass means let program see this signal; otherwise program doesn't know.\n\
1845 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
1846 Pass and Stop may be combined.");
1847
1848 stop_command = add_cmd ("stop", class_obscure, not_just_help_class_command,
1849 "There is no `stop' command, but you can set a hook on `stop'.\n\
1850 This allows you to set a list of commands to be run each time execution\n\
1851 of the program stops.", &cmdlist);
1852
1853 numsigs = signo_max () + 1;
1854 signal_stop = (unsigned char *)
1855 xmalloc (sizeof (signal_stop[0]) * numsigs);
1856 signal_print = (unsigned char *)
1857 xmalloc (sizeof (signal_print[0]) * numsigs);
1858 signal_program = (unsigned char *)
1859 xmalloc (sizeof (signal_program[0]) * numsigs);
1860 for (i = 0; i < numsigs; i++)
1861 {
1862 signal_stop[i] = 1;
1863 signal_print[i] = 1;
1864 signal_program[i] = 1;
1865 }
1866
1867 /* Signals caused by debugger's own actions
1868 should not be given to the program afterwards. */
1869 signal_program[SIGTRAP] = 0;
1870 signal_program[SIGINT] = 0;
1871
1872 /* Signals that are not errors should not normally enter the debugger. */
1873 #ifdef SIGALRM
1874 signal_stop[SIGALRM] = 0;
1875 signal_print[SIGALRM] = 0;
1876 #endif /* SIGALRM */
1877 #ifdef SIGVTALRM
1878 signal_stop[SIGVTALRM] = 0;
1879 signal_print[SIGVTALRM] = 0;
1880 #endif /* SIGVTALRM */
1881 #ifdef SIGPROF
1882 signal_stop[SIGPROF] = 0;
1883 signal_print[SIGPROF] = 0;
1884 #endif /* SIGPROF */
1885 #ifdef SIGCHLD
1886 signal_stop[SIGCHLD] = 0;
1887 signal_print[SIGCHLD] = 0;
1888 #endif /* SIGCHLD */
1889 #ifdef SIGCLD
1890 signal_stop[SIGCLD] = 0;
1891 signal_print[SIGCLD] = 0;
1892 #endif /* SIGCLD */
1893 #ifdef SIGIO
1894 signal_stop[SIGIO] = 0;
1895 signal_print[SIGIO] = 0;
1896 #endif /* SIGIO */
1897 #ifdef SIGURG
1898 signal_stop[SIGURG] = 0;
1899 signal_print[SIGURG] = 0;
1900 #endif /* SIGURG */
1901 }