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