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