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